-
Notifications
You must be signed in to change notification settings - Fork 3
/
aws_iot_ble.c
75 lines (64 loc) · 2.01 KB
/
aws_iot_ble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* SPDX-FileCopyrightText: 2022 Kyunghwan Kwon <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
#include "cbor/cbor.h"
#define SSID_MAXLEN 32
#define PASS_MAXLEN 32
#define BSSID_LEN 6
struct aws_request {
uint8_t type;
uint8_t nr_networks;
uint16_t scan_timeout_msec;
uint8_t ssid[SSID_MAXLEN];
uint8_t ssid_len;
uint8_t passwd[PASS_MAXLEN];
uint8_t passwd_len;
uint8_t bssid[BSSID_LEN];
uint8_t bssid_len;
};
static void parse_integer(const cbor_reader_t *reader,
const struct cbor_parser *parser,
const cbor_item_t *item, void *arg)
{
struct aws_request *p = (struct aws_request *)arg;
if (strcmp(parser->key, "w") == 0) {
cbor_decode(reader, item, &p->type, sizeof(p->type));
} else if (strcmp(parser->key, "h") == 0) {
cbor_decode(reader, item,
&p->nr_networks, sizeof(p->nr_networks));
} else if (strcmp(parser->key, "t") == 0) {
cbor_decode(reader, item, &p->scan_timeout_msec,
sizeof(p->scan_timeout_msec));
}
}
static void parse_string(const cbor_reader_t *reader,
const struct cbor_parser *parser,
const cbor_item_t *item, void *arg)
{
struct aws_request *p = (struct aws_request *)arg;
if (strcmp(parser->key, "r") == 0) {
cbor_decode(reader, item, p->ssid, sizeof(p->ssid));
p->ssid_len = item->size;
} else if (strcmp(parser->key, "m") == 0) {
cbor_decode(reader, item, p->passwd, sizeof(p->passwd));
p->passwd_len = item->size;
}
}
static const struct cbor_parser parsers[] = {
{ .key = "w", .run = parse_integer }, /* message type */
{ .key = "h", .run = parse_integer }, /* number of networks */
{ .key = "t", .run = parse_integer }, /* scan timeout */
{ .key = "r", .run = parse_string }, /* ssid */
{ .key = "m", .run = parse_string }, /* password */
};
static void on_aws_request(const void *msg, uint16_t msglen)
{
struct aws_request aws_request;
cbor_reader_t reader;
cbor_item_t items[32];
cbor_reader_init(&reader, items, sizeof(items) / sizeof(*items));
cbor_unmarshal(&reader, parsers, sizeof(parsers) / sizeof(*parsers),
msg, msglen, &aws_request);
}