Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <esp_err.h>
#include <esp_log.h>
#include <protocomm.h>
#include <protocomm_security0.h>
#include <protocomm_security1.h>
#include <protocomm_security2.h>
#include <esp_local_ctrl.h>
#include "esp_local_ctrl_priv.h"
#include "esp_local_ctrl.pb-c.h"
#define ESP_LOCAL_CTRL_VERSION
inst_ctx
local_ctrl_inst_ctx
TAG
esp_local_ctrl_start(const esp_local_ctrl_config_t *)
esp_local_ctrl_stop()
esp_local_ctrl_get_property_index(const char *)
esp_local_ctrl_add_property(const esp_local_ctrl_prop_t *)
esp_local_ctrl_remove_property(const char *)
esp_local_ctrl_get_property(const char *)
esp_local_ctrl_get_prop_count(size_t *)
esp_local_ctrl_get_prop_values(size_t, uint32_t *, esp_local_ctrl_prop_t *, esp_local_ctrl_prop_val_t *)
esp_local_ctrl_set_prop_values(size_t, uint32_t *, const esp_local_ctrl_prop_val_t *)
esp_local_ctrl_set_handler(const char *, protocomm_req_handler_t, void *)
Files
ESP-IDF
components
app_trace
app_update
bootloader_support
bt
cmock
console
cxx
driver
efuse
esp_adc
esp_app_format
esp_bootloader_format
esp_coex
esp_common
esp_driver_ana_cmpr
esp_driver_cam
esp_driver_dac
esp_driver_gpio
esp_driver_gptimer
esp_driver_i2c
esp_driver_i2s
esp_driver_jpeg
esp_driver_ledc
esp_driver_mcpwm
esp_driver_parlio
esp_driver_pcnt
esp_driver_rmt
esp_driver_sdio
esp_driver_sdm
esp_driver_sdmmc
esp_driver_sdspi
esp_driver_spi
esp_driver_tsens
esp_driver_uart
esp_driver_usb_serial_jtag
esp_eth
esp_event
esp_gdbstub
esp_hid
esp_http_client
esp_http_server
esp_https_ota
esp_https_server
esp_hw_support
esp_lcd
esp_local_ctrl
include
proto-c
src
esp_mm
esp_netif
esp_partition
esp_phy
esp_pm
esp_psram
esp_ringbuf
esp_rom
esp_security
esp_system
esp_timer
esp_vfs_console
esp_wifi
esp-tls
espcoredump
hal
heap
http_parser
ieee802154
log
mqtt
newlib
nvs_flash
nvs_sec_provider
openthread
perfmon
protobuf-c
protocomm
pthread
rt
sdmmc
soc
spi_flash
spiffs
tcp_transport
ulp
unity
vfs
wear_levelling
wifi_provisioning
wpa_supplicant
xtensa
examples
lwIP
FreeRTOS
cJSON
mbedTLS
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/esp_local_ctrl/src/esp_local_ctrl.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdio.h> #include <string.h> #include <inttypes.h> #include <esp_err.h> #include <esp_log.h> #include <protocomm.h> #include <protocomm_security0.h> #include <protocomm_security1.h> #include <protocomm_security2.h> #include <esp_local_ctrl.h> #include "esp_local_ctrl_priv.h" #include "esp_local_ctrl.pb-c.h"12 includes #define ESP_LOCAL_CTRL_VERSION "v1.0" struct inst_ctx { protocomm_t *pc; esp_local_ctrl_config_t config; esp_local_ctrl_prop_t **props; size_t props_count; }{ ... }; struct inst_ctx *local_ctrl_inst_ctx; static const char *TAG = "esp_local_ctrl"; esp_err_t esp_local_ctrl_start(const esp_local_ctrl_config_t *config) { esp_err_t ret; if (!config) { ESP_LOGE(TAG, "NULL configuration provided"); return ESP_ERR_INVALID_ARG; }{...} if (!config->transport) { ESP_LOGE(TAG, "No transport provided"); return ESP_ERR_INVALID_ARG; }{...} if (config->max_properties == 0) { ESP_LOGE(TAG, "max_properties must be greater than 0"); return ESP_ERR_INVALID_ARG; }{...} if (!config->handlers.get_prop_values || !config->handlers.set_prop_values) { ESP_LOGE(TAG, "Handlers cannot be null"); return ESP_ERR_INVALID_ARG; }{...} if (local_ctrl_inst_ctx) { ESP_LOGW(TAG, "Service already active"); return ESP_ERR_INVALID_STATE; }{...} local_ctrl_inst_ctx = calloc(1, sizeof(struct inst_ctx)); if (!local_ctrl_inst_ctx) { ESP_LOGE(TAG, "Failed to allocate memory for instance"); return ESP_ERR_NO_MEM; }{...} memcpy(&local_ctrl_inst_ctx->config, config, sizeof(local_ctrl_inst_ctx->config)); local_ctrl_inst_ctx->props = calloc(local_ctrl_inst_ctx->config.max_properties, sizeof(esp_local_ctrl_prop_t *)); if (!local_ctrl_inst_ctx->props) { ESP_LOGE(TAG, "Failed to allocate memory for properties"); free(local_ctrl_inst_ctx); local_ctrl_inst_ctx = NULL; return ESP_ERR_NO_MEM; }{...} /* Since the config structure will be different for different transport modes, each transport may * implement a `copy_config()` function, which accepts a configuration structure as input and * creates a copy of that, which can be kept in the context structure of the `esp_local_ctrl` instance. * This copy can be later be freed using `free_config()` *//* ... */ if (config->transport->copy_config) { ret = config->transport->copy_config(&local_ctrl_inst_ctx->config.transport_config, &config->transport_config); if (ret != ESP_OK) { esp_local_ctrl_stop(); return ret; }{...} }{...} /* For a selected transport mode, endpoints may need to be declared prior to starting the * `esp_local_ctrl` service, e.g. in case of BLE. By declaration it means that the transport layer * allocates some resources for an endpoint, and later, after service has started, a handler * is assigned for that endpoint *//* ... */ if (config->transport->declare_ep) { /* UUIDs are 16bit unique IDs for each endpoint. This may or may not be relevant for * a chosen transport. We reserve all values from FF50 to FFFF for the internal endpoints. * The remaining endpoints can be used by the application for its own custom endpoints *//* ... */ uint16_t start_uuid = 0xFF50; ret = config->transport->declare_ep(&local_ctrl_inst_ctx->config.transport_config, "esp_local_ctrl/version", start_uuid++); if (ret != ESP_OK) { esp_local_ctrl_stop(); return ret; }{...} ret = config->transport->declare_ep(&local_ctrl_inst_ctx->config.transport_config, "esp_local_ctrl/session", start_uuid++); if (ret != ESP_OK) { esp_local_ctrl_stop(); return ret; }{...} ret = config->transport->declare_ep(&local_ctrl_inst_ctx->config.transport_config, "esp_local_ctrl/control", start_uuid++); if (ret != ESP_OK) { esp_local_ctrl_stop(); return ret; }{...} }{...} local_ctrl_inst_ctx->pc = protocomm_new(); if (!local_ctrl_inst_ctx->pc) { ESP_LOGE(TAG, "Failed to create new protocomm instance"); esp_local_ctrl_stop(); return ESP_FAIL; }{...} if (config->transport->start_service) { ret = config->transport->start_service(local_ctrl_inst_ctx->pc, &local_ctrl_inst_ctx->config.transport_config); if (ret != ESP_OK) { esp_local_ctrl_stop(); return ret; }{...} }{...} ret = protocomm_set_version(local_ctrl_inst_ctx->pc, "esp_local_ctrl/version", ESP_LOCAL_CTRL_VERSION); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to set version endpoint"); esp_local_ctrl_stop(); return ret; }{...} protocomm_security_t *proto_sec_handle = NULL; switch (local_ctrl_inst_ctx->config.proto_sec.version) { case PROTOCOM_SEC_CUSTOM: proto_sec_handle = local_ctrl_inst_ctx->config.proto_sec.custom_handle; break;... case PROTOCOM_SEC1: #ifdef CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_1 proto_sec_handle = (protocomm_security_t *) &protocomm_security1; #else // Enable SECURITY_VERSION_1 in Protocomm configuration menu return ESP_ERR_NOT_SUPPORTED;/* ... */ #endif break;... case PROTOCOM_SEC2: #ifdef CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2 proto_sec_handle = (protocomm_security_t *) &protocomm_security2; break;/* ... */ #else // Enable SECURITY_VERSION_2 in Protocomm configuration menu return ESP_ERR_NOT_SUPPORTED;/* ... */ #endif... case PROTOCOM_SEC0: default: #ifdef CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0 proto_sec_handle = (protocomm_security_t *) &protocomm_security0; #else // Enable SECURITY_VERSION_0 in Protocomm configuration menu return ESP_ERR_NOT_SUPPORTED;/* ... */ #endif break;... }{...} ret = protocomm_set_security(local_ctrl_inst_ctx->pc, "esp_local_ctrl/session", proto_sec_handle, local_ctrl_inst_ctx->config.proto_sec.sec_params); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to set session endpoint"); esp_local_ctrl_stop(); return ret; }{...} ret = protocomm_add_endpoint(local_ctrl_inst_ctx->pc, "esp_local_ctrl/control", esp_local_ctrl_data_handler, NULL); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to set control endpoint"); esp_local_ctrl_stop(); return ret; }{...} return ESP_OK; }{ ... } esp_err_t esp_local_ctrl_stop(void) { if (local_ctrl_inst_ctx) { if (local_ctrl_inst_ctx->config.transport->free_config) { local_ctrl_inst_ctx->config.transport->free_config(&local_ctrl_inst_ctx->config.transport_config); }{...} if (local_ctrl_inst_ctx->pc) { if (local_ctrl_inst_ctx->config.transport->stop_service) { local_ctrl_inst_ctx->config.transport->stop_service(local_ctrl_inst_ctx->pc); }{...} protocomm_delete(local_ctrl_inst_ctx->pc); }{...} if (local_ctrl_inst_ctx->config.handlers.usr_ctx_free_fn) { local_ctrl_inst_ctx->config.handlers.usr_ctx_free_fn( local_ctrl_inst_ctx->config.handlers.usr_ctx); }{...} /* Iterate through all properties one by one and free them */ for (uint32_t i = 0; i < local_ctrl_inst_ctx->config.max_properties; i++) { if (local_ctrl_inst_ctx->props[i] == NULL) { continue; }{...} /* Release memory allocated for property data */ free(local_ctrl_inst_ctx->props[i]->name); if (local_ctrl_inst_ctx->props[i]->ctx_free_fn) { local_ctrl_inst_ctx->props[i]->ctx_free_fn(local_ctrl_inst_ctx->props[i]->ctx); }{...} free(local_ctrl_inst_ctx->props[i]); }{...} free(local_ctrl_inst_ctx->props); free(local_ctrl_inst_ctx); local_ctrl_inst_ctx = NULL; }{...} return ESP_OK; }{ ... } static int esp_local_ctrl_get_property_index(const char *name) { if (!local_ctrl_inst_ctx || !name) { return -1; }{...} /* Iterate through all properties one by one * and find the one with matching name *//* ... */ for (uint32_t i = 0; i < local_ctrl_inst_ctx->props_count; i++) { if (strcmp(local_ctrl_inst_ctx->props[i]->name, name) == 0) { return i; }{...} }{...} return -1; }{ ... } esp_err_t esp_local_ctrl_add_property(const esp_local_ctrl_prop_t *prop) { if (!local_ctrl_inst_ctx) { ESP_LOGE(TAG, "Service not running"); return ESP_ERR_INVALID_STATE; }{...} if (!prop || !prop->name) { return ESP_ERR_INVALID_ARG; }{...} if (esp_local_ctrl_get_property_index(prop->name) >= 0) { ESP_LOGE(TAG, "Property with name %s exists", prop->name); return ESP_ERR_INVALID_STATE; }{...} if (local_ctrl_inst_ctx->config.max_properties == local_ctrl_inst_ctx->props_count) { ESP_LOGE(TAG, "Max properties limit reached. Cannot add property %s", prop->name); return ESP_ERR_NO_MEM; }{...} uint32_t i = local_ctrl_inst_ctx->props_count; local_ctrl_inst_ctx->props[i] = calloc(1, sizeof(esp_local_ctrl_prop_t)); if (!local_ctrl_inst_ctx->props[i]) { ESP_LOGE(TAG, "Failed to allocate memory for new property %s", prop->name); return ESP_ERR_NO_MEM; }{...} local_ctrl_inst_ctx->props[i]->name = strdup(prop->name); if (!local_ctrl_inst_ctx->props[i]->name) { ESP_LOGE(TAG, "Failed to allocate memory for property data %s", prop->name); free(local_ctrl_inst_ctx->props[i]); local_ctrl_inst_ctx->props[i] = NULL; return ESP_ERR_NO_MEM; }{...} local_ctrl_inst_ctx->props[i]->type = prop->type; local_ctrl_inst_ctx->props[i]->size = prop->size; local_ctrl_inst_ctx->props[i]->flags = prop->flags; local_ctrl_inst_ctx->props[i]->ctx = prop->ctx; local_ctrl_inst_ctx->props[i]->ctx_free_fn = prop->ctx_free_fn; local_ctrl_inst_ctx->props_count++; return ESP_OK; }{ ... } esp_err_t esp_local_ctrl_remove_property(const char *name) { int idx = esp_local_ctrl_get_property_index(name); if (idx < 0) { ESP_LOGE(TAG, "Property %s not found", name); return ESP_ERR_NOT_FOUND; }{...} /* Release memory allocated for property data */ if (local_ctrl_inst_ctx->props[idx]->ctx_free_fn) { local_ctrl_inst_ctx->props[idx]->ctx_free_fn( local_ctrl_inst_ctx->props[idx]->ctx); }{...} free(local_ctrl_inst_ctx->props[idx]->name); free(local_ctrl_inst_ctx->props[idx]); local_ctrl_inst_ctx->props[idx++] = NULL; /* Move the following properties forward, so that there is * no empty space between two properties *//* ... */ for (uint32_t i = idx; i < local_ctrl_inst_ctx->props_count; i++) { if (local_ctrl_inst_ctx->props[i] == NULL) { break; }{...} local_ctrl_inst_ctx->props[i-1] = local_ctrl_inst_ctx->props[i]; }{...} local_ctrl_inst_ctx->props_count--; return ESP_OK; }{ ... } const esp_local_ctrl_prop_t *esp_local_ctrl_get_property(const char *name) { int idx = esp_local_ctrl_get_property_index(name); if (idx < 0) { ESP_LOGE(TAG, "Property %s not found", name); return NULL; }{...} return local_ctrl_inst_ctx->props[idx]; }{ ... } esp_err_t esp_local_ctrl_get_prop_count(size_t *count) { if (!local_ctrl_inst_ctx) { ESP_LOGE(TAG, "Service not running"); return ESP_ERR_INVALID_STATE; }{...} if (!count) { return ESP_ERR_INVALID_ARG; }{...} *count = local_ctrl_inst_ctx->props_count; return ESP_OK; }{ ... } esp_err_t esp_local_ctrl_get_prop_values(size_t total_indices, uint32_t *indices, esp_local_ctrl_prop_t *props, esp_local_ctrl_prop_val_t *values) { if (!local_ctrl_inst_ctx) { ESP_LOGE(TAG, "Service not running"); return ESP_ERR_INVALID_STATE; }{...} if (!indices || !props || !values) { return ESP_ERR_INVALID_ARG; }{...} /* Convert indices to names */ for (size_t i = 0; i < total_indices; i++) { if (indices[i] >= local_ctrl_inst_ctx->props_count) { ESP_LOGE(TAG, "Invalid property index %" PRId32, indices[i]); return ESP_ERR_INVALID_ARG; }{...} props[i].name = local_ctrl_inst_ctx->props[indices[i]]->name; props[i].type = local_ctrl_inst_ctx->props[indices[i]]->type; props[i].flags = local_ctrl_inst_ctx->props[indices[i]]->flags; props[i].size = local_ctrl_inst_ctx->props[indices[i]]->size; props[i].ctx = local_ctrl_inst_ctx->props[indices[i]]->ctx; }{...} esp_local_ctrl_handlers_t *h = &local_ctrl_inst_ctx->config.handlers; esp_err_t ret = h->get_prop_values(total_indices, props, values, h->usr_ctx); /* Properties with fixed sizes need to be checked */ for (size_t i = 0; i < total_indices; i++) { if (local_ctrl_inst_ctx->props[indices[i]]->size != 0) { values[i].size = local_ctrl_inst_ctx->props[indices[i]]->size; }{...} }{...} return ret; }{ ... } esp_err_t esp_local_ctrl_set_prop_values(size_t total_indices, uint32_t *indices, const esp_local_ctrl_prop_val_t *values) { if (!local_ctrl_inst_ctx) { ESP_LOGE(TAG, "Service not running"); return ESP_ERR_INVALID_STATE; }{...} if (!indices || !values) { return ESP_ERR_INVALID_ARG; }{...} esp_local_ctrl_prop_t *props = calloc(total_indices, sizeof(esp_local_ctrl_prop_t)); if (!props) { ESP_LOGE(TAG, "Unable to allocate memory for properties array"); return ESP_ERR_NO_MEM; }{...} for (size_t i = 0; i < total_indices; i++) { if (indices[i] >= local_ctrl_inst_ctx->props_count) { ESP_LOGE(TAG, "Invalid property index %" PRId32, indices[i]); free(props); return ESP_ERR_INVALID_ARG; }{...} /* Properties with fixed sizes need to be checked */ if ((local_ctrl_inst_ctx->props[indices[i]]->size != values[i].size) && (local_ctrl_inst_ctx->props[indices[i]]->size != 0)) { ESP_LOGE(TAG, "Invalid property size %d. Expected %d", values[i].size, local_ctrl_inst_ctx->props[indices[i]]->size); free(props); return ESP_ERR_INVALID_ARG; }{...} props[i].name = local_ctrl_inst_ctx->props[indices[i]]->name; props[i].type = local_ctrl_inst_ctx->props[indices[i]]->type; props[i].flags = local_ctrl_inst_ctx->props[indices[i]]->flags; props[i].size = local_ctrl_inst_ctx->props[indices[i]]->size; props[i].ctx = local_ctrl_inst_ctx->props[indices[i]]->ctx; }{...} esp_local_ctrl_handlers_t *h = &local_ctrl_inst_ctx->config.handlers; esp_err_t ret = h->set_prop_values(total_indices, props, values, h->usr_ctx); free(props); return ret; }{ ... } esp_err_t esp_local_ctrl_set_handler(const char *ep_name, protocomm_req_handler_t handler, void *priv_data) { esp_err_t ret = ESP_ERR_INVALID_STATE; if (local_ctrl_inst_ctx) { ret = protocomm_add_endpoint(local_ctrl_inst_ctx->pc, ep_name, handler, priv_data); }{...} if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to register endpoint handler"); }{...} return ret; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.