-
Notifications
You must be signed in to change notification settings - Fork 3
/
esp32_bt_svc_config.c
343 lines (325 loc) · 11.2 KB
/
esp32_bt_svc_config.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 2014-2018 Cesanta Software Limited
* All rights reserved
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Interface to sys_config over BLE GATT service.
* See README.md for high-level description.
*/
#include <stdlib.h>
#include "common/cs_dbg.h"
#include "common/mbuf.h"
#include "common/mg_str.h"
#include "mgos_config_util.h"
#include "mgos_hal.h"
#include "mgos_sys_config.h"
#include "mgos_utils.h"
#include "esp32_bt_gatts.h"
/* Note: UUIDs below are in reverse, because that's how ESP wants them. */
static const esp_bt_uuid_t mos_cfg_svc_uuid = {
.len = ESP_UUID_LEN_128,
.uuid.uuid128 =
{
/* _mOS_CFG_SVC_ID_, 5f6d4f53-5f43-4647-5f53-56435f49445f */
0x5f, 0x44, 0x49, 0x5f, 0x43, 0x56, 0x53, 0x5f, 0x47, 0x46, 0x43, 0x5f,
0x53, 0x4f, 0x6d, 0x5f,
},
};
static const esp_bt_uuid_t mos_cfg_key_uuid = {
.len = ESP_UUID_LEN_128,
.uuid.uuid128 =
{
/* 0mOS_CFG_key___0, 306d4f53-5f43-4647-5f6b-65795f5f5f30 */
0x30, 0x5f, 0x5f, 0x5f, 0x79, 0x65, 0x6b, 0x5f, 0x47, 0x46, 0x43, 0x5f,
0x53, 0x4f, 0x6d, 0x30,
},
};
static uint16_t mos_cfg_key_ah;
static const esp_bt_uuid_t mos_cfg_value_uuid = {
.len = ESP_UUID_LEN_128,
.uuid.uuid128 =
{
/* 1mOS_CFG_value_1, 316d4f53-5f43-4647-5f76-616c75655f31 */
0x31, 0x5f, 0x65, 0x75, 0x6c, 0x61, 0x76, 0x5f, 0x47, 0x46, 0x43, 0x5f,
0x53, 0x4f, 0x6d, 0x31,
},
};
static uint16_t mos_cfg_value_ah;
static const esp_bt_uuid_t mos_cfg_save_uuid = {
.len = ESP_UUID_LEN_128,
.uuid.uuid128 =
{
/* 2mOS_CFG_save__2, 326d4f53-5f43-4647-5f73-6176655f5f32 */
0x32, 0x5f, 0x5f, 0x65, 0x76, 0x61, 0x73, 0x5f, 0x47, 0x46, 0x43, 0x5f,
0x53, 0x4f, 0x6d, 0x32,
},
};
static uint16_t mos_cfg_save_ah;
const esp_gatts_attr_db_t mos_cfg_gatt_db[7] = {
{
.attr_control = {.auto_rsp = ESP_GATT_AUTO_RSP},
.att_desc =
{
.uuid_length = ESP_UUID_LEN_16,
.uuid_p = (uint8_t *) &primary_service_uuid,
.perm = ESP_GATT_PERM_READ,
.max_length = ESP_UUID_LEN_128,
.length = ESP_UUID_LEN_128,
.value = (uint8_t *) mos_cfg_svc_uuid.uuid.uuid128,
},
},
/* key */
{{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *) &char_decl_uuid, ESP_GATT_PERM_READ, 1, 1,
(uint8_t *) &char_prop_write}},
{{ESP_GATT_RSP_BY_APP},
{ESP_UUID_LEN_128, (uint8_t *) mos_cfg_key_uuid.uuid.uuid128,
ESP_GATT_PERM_WRITE, 0, 0, NULL}},
/* value */
{{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *) &char_decl_uuid, ESP_GATT_PERM_READ, 1, 1,
(uint8_t *) &char_prop_read_write}},
{{ESP_GATT_RSP_BY_APP},
{ESP_UUID_LEN_128, (uint8_t *) mos_cfg_value_uuid.uuid.uuid128,
ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, 0, 0, NULL}},
/* save */
{{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *) &char_decl_uuid, ESP_GATT_PERM_READ, 1, 1,
(uint8_t *) &char_prop_write}},
{{ESP_GATT_RSP_BY_APP},
{ESP_UUID_LEN_128, (uint8_t *) mos_cfg_save_uuid.uuid.uuid128,
ESP_GATT_PERM_WRITE, 0, 0, NULL}},
};
enum bt_cfg_state {
BT_CFG_STATE_KEY_ENTRY = 0,
BT_CFG_STATE_VALUE_ENTRY = 1,
BT_CFG_STATE_VALUE_READ = 2,
BT_CFG_STATE_SAVE = 3,
};
struct bt_cfg_svc_data {
struct mbuf key;
struct mbuf value;
enum bt_cfg_state state;
};
static bool mgos_bt_svc_config_set(struct bt_cfg_svc_data *sd) {
bool ret = false;
const struct mgos_conf_entry *e = mgos_conf_find_schema_entry_s(
mg_mk_str_n(sd->key.buf, sd->key.len), mgos_config_schema());
if (e == NULL) {
LOG(LL_ERROR,
("Config key '%.*s' not found", (int) sd->key.len, sd->key.buf));
return false;
}
/* Make sure value is NUL-terminated, for simplicity. */
mbuf_append(&sd->value, "", 1);
sd->value.len--;
const char *vt = NULL;
char *vp = (((char *) &mgos_sys_config) + e->offset);
/* For simplicity, we only allow setting leaf values. */
switch (e->type) {
case CONF_TYPE_INT: {
int *ivp = (int *) vp;
char *endptr = NULL;
int v = strtol(sd->value.buf, &endptr, 0);
vt = "int";
if (endptr - sd->value.buf == sd->value.len) {
*ivp = v;
ret = true;
LOG(LL_INFO, ("'%.*s' = %d", (int) sd->key.len, sd->key.buf, *ivp));
}
break;
}
case CONF_TYPE_DOUBLE: {
double *dvp = (double *) vp;
char *endptr = NULL;
double v = strtod(sd->value.buf, &endptr);
vt = "float";
if (endptr - sd->value.buf == sd->value.len) {
*dvp = v;
ret = true;
LOG(LL_INFO, ("'%.*s' = %f", (int) sd->key.len, sd->key.buf, *dvp));
}
break;
}
case CONF_TYPE_STRING: {
vt = "string";
char **svp = (char **) vp;
mgos_conf_set_str(svp, sd->value.buf);
LOG(LL_INFO, ("'%.*s' = '%s'", (int) sd->key.len, sd->key.buf,
(*svp ? *svp : "")));
ret = true;
break;
}
case CONF_TYPE_BOOL: {
bool *bvp = (bool *) vp;
const struct mg_str vs = mg_mk_str_n(sd->value.buf, sd->value.len);
vt = "bool";
if (mg_vcmp(&vs, "true") == 0 || mg_vcmp(&vs, "false") == 0) {
*bvp = (mg_vcmp(&vs, "true") == 0);
LOG(LL_INFO, ("'%.*s' = %s", (int) sd->key.len, sd->key.buf,
(*bvp ? "true" : "false")));
ret = true;
}
break;
}
case CONF_TYPE_OBJECT: {
LOG(LL_ERROR, ("Setting objects is not allowed (%.*s)", (int) sd->key.len,
sd->key.buf));
break;
}
}
if (!ret && vt != NULL) {
LOG(LL_ERROR, ("'%.*s': invalid %s value '%.*s'", (int) sd->key.len,
sd->key.buf, vt, (int) sd->value.len, sd->value.buf));
}
return ret;
}
static bool mgos_bt_svc_config_ev(struct esp32_bt_session *bs,
esp_gatts_cb_event_t ev,
esp_ble_gatts_cb_param_t *ep) {
bool ret = false;
struct bt_cfg_svc_data *sd = NULL;
struct esp32_bt_connection *bc = NULL;
if (bs != NULL) { /* CREAT_ATTR_TAB is not associated with any session. */
bc = bs->bc;
sd = (struct bt_cfg_svc_data *) bs->user_data;
}
switch (ev) {
case ESP_GATTS_CREAT_ATTR_TAB_EVT: {
const struct gatts_add_attr_tab_evt_param *p = &ep->add_attr_tab;
uint16_t svch = p->handles[0];
mos_cfg_key_ah = p->handles[2];
mos_cfg_value_ah = p->handles[4];
mos_cfg_save_ah = p->handles[6];
LOG(LL_DEBUG, ("svch = %d key_ah = %d value_ah = %d save_ah = %d", svch,
mos_cfg_key_ah, mos_cfg_value_ah, mos_cfg_save_ah));
break;
}
case ESP_GATTS_CONNECT_EVT: {
sd = (struct bt_cfg_svc_data *) calloc(1, sizeof(*sd));
if (sd == NULL) break;
mbuf_init(&sd->key, 0);
mbuf_init(&sd->value, 0);
sd->state = BT_CFG_STATE_KEY_ENTRY;
bs->user_data = sd;
break;
}
case ESP_GATTS_READ_EVT: {
const struct gatts_read_evt_param *p = &ep->read;
if (sd == NULL || p->handle != mos_cfg_value_ah) break;
if (sd->key.len == 0) {
LOG(LL_ERROR, ("Key to read is not set"));
break;
}
sd->state = BT_CFG_STATE_VALUE_READ;
const struct mgos_conf_entry *e = mgos_conf_find_schema_entry_s(
mg_mk_str_n(sd->key.buf, sd->key.len), mgos_config_schema());
if (e == NULL) {
LOG(LL_ERROR,
("Config key '%.*s' not found", (int) sd->key.len, sd->key.buf));
break;
}
struct mbuf vb;
mbuf_init(&vb, 0);
mgos_conf_emit_cb(&mgos_sys_config, NULL /* base */, e,
false /* pretty */, &vb, NULL /* cb */,
NULL /* cb_param */);
uint16_t to_send = bc->mtu - 1;
if (p->offset > vb.len) break;
if (vb.len - p->offset < to_send) to_send = vb.len - p->offset;
LOG(LL_INFO,
("Read '%.*s' %d @ %d = '%.*s'", (int) sd->key.len, sd->key.buf,
(int) to_send, (int) p->offset, (int) to_send, vb.buf + p->offset));
esp_gatt_rsp_t rsp = {.attr_value = {.handle = mos_cfg_value_ah,
.offset = p->offset,
.len = to_send}};
memcpy(rsp.attr_value.value, vb.buf + p->offset, to_send);
esp_ble_gatts_send_response(bc->gatt_if, bc->conn_id, p->trans_id,
ESP_GATT_OK, &rsp);
ret = true;
mbuf_free(&vb);
break;
}
case ESP_GATTS_WRITE_EVT: {
const struct gatts_write_evt_param *p = &ep->write;
if (sd == NULL) break;
if (p->handle == mos_cfg_key_ah) {
if (sd->state != BT_CFG_STATE_KEY_ENTRY) {
mbuf_free(&sd->key);
mbuf_free(&sd->value);
mbuf_init(&sd->key, p->len);
sd->state = BT_CFG_STATE_KEY_ENTRY;
}
mbuf_append(&sd->key, p->value, p->len);
LOG(LL_DEBUG, ("Key = '%.*s'", (int) sd->key.len, sd->key.buf));
ret = true;
} else if (p->handle == mos_cfg_value_ah) {
if (sd->state != BT_CFG_STATE_VALUE_ENTRY) {
mbuf_free(&sd->value);
mbuf_init(&sd->value, p->len);
sd->state = BT_CFG_STATE_VALUE_ENTRY;
}
if (p->len == 1 && p->value[0] == 0) {
mbuf_free(&sd->value);
mbuf_init(&sd->value, 0);
} else {
mbuf_append(&sd->value, p->value, p->len);
}
LOG(LL_DEBUG, ("Value = '%.*s'", (int) sd->value.len,
(sd->value.buf ? sd->value.buf : "")));
ret = true;
} else if (p->handle == mos_cfg_save_ah) {
sd->state = BT_CFG_STATE_SAVE;
/* NULL value is a legal value, so we check for state here. */
if (sd->key.len > 0 && sd->state != BT_CFG_STATE_VALUE_ENTRY) {
ret = mgos_bt_svc_config_set(sd);
} else {
ret = true; /* Allow save and reboot without setting anything. */
}
if (ret) {
if (p->len == 1 && (p->value[0] == '1' || p->value[0] == '2')) {
char *msg = NULL;
ret = save_cfg(&mgos_sys_config, &msg);
if (!ret) {
LOG(LL_ERROR, ("Error saving config: %s", msg));
} else if (p->value[0] == '2') {
mgos_system_restart_after(100);
}
}
}
}
break;
}
case ESP_GATTS_DISCONNECT_EVT: {
if (sd != NULL) {
mbuf_free(&sd->key);
mbuf_free(&sd->value);
free(sd);
bs->user_data = NULL;
}
break;
}
default:
break;
}
return ret;
}
bool mgos_bt_service_config_init(void) {
if (mgos_sys_config_get_bt_config_svc_enable()) {
mgos_bt_gatts_register_service(mos_cfg_gatt_db, ARRAY_SIZE(mos_cfg_gatt_db),
mgos_bt_svc_config_ev);
}
return true;
}