Select one of the symbols to view example projects that use it.
 
Outline
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <esp_err.h>
#include <esp_log.h>
#include <sys/queue.h>
#include <protocomm.h>
#include <protocomm_security.h>
#include "protocomm_priv.h"
TAG
protocomm_new()
protocomm_delete(protocomm_t *)
search_endpoint(protocomm_t *, const char *)
protocomm_add_endpoint_internal(protocomm_t *, const char *, protocomm_req_handler_t, void *, uint32_t)
protocomm_add_endpoint(protocomm_t *, const char *, protocomm_req_handler_t, void *)
protocomm_remove_endpoint(protocomm_t *, const char *)
protocomm_open_session(protocomm_t *, uint32_t)
protocomm_close_session(protocomm_t *, uint32_t)
protocomm_req_handle(protocomm_t *, const char *, uint32_t, const uint8_t *, ssize_t, uint8_t **, ssize_t *)
protocomm_common_security_handler(uint32_t, const uint8_t *, ssize_t, uint8_t **, ssize_t *, void *)
protocomm_set_security(protocomm_t *, const char *, const protocomm_security_t *, const void *)
protocomm_unset_security(protocomm_t *, const char *)
protocomm_version_handler(uint32_t, const uint8_t *, ssize_t, uint8_t **, ssize_t *, void *)
protocomm_set_version(protocomm_t *, const char *, const char *)
protocomm_unset_version(protocomm_t *, const char *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/protocomm/src/common/protocomm.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdlib.h> #include <string.h> #include <inttypes.h> #include <esp_err.h> #include <esp_log.h> #include <sys/queue.h> #include <protocomm.h> #include <protocomm_security.h> #include "protocomm_priv.h"9 includes static const char *TAG = "protocomm"; protocomm_t *protocomm_new(void) { protocomm_t *pc; pc = (protocomm_t *) calloc(1, sizeof(protocomm_t)); if (!pc) { ESP_LOGE(TAG, "Error allocating protocomm"); return NULL; }{...} SLIST_INIT(&pc->endpoints); return pc; }{ ... } void protocomm_delete(protocomm_t *pc) { if (pc == NULL) { return; }{...} protocomm_ep_t *it, *tmp; /* Remove endpoints first */ SLIST_FOREACH_SAFE(it, &pc->endpoints, next, tmp) { free(it); }{...} /* Free memory allocated to version string */ if (pc->ver) { free((void *)pc->ver); }{...} /* Free memory allocated to security */ if (pc->sec && pc->sec->cleanup) { pc->sec->cleanup(pc->sec_inst); }{...} if (pc->sec_params) { free(pc->sec_params); }{...} free(pc); }{ ... } static protocomm_ep_t *search_endpoint(protocomm_t *pc, const char *ep_name) { protocomm_ep_t *it; SLIST_FOREACH(it, &pc->endpoints, next) { if (strcmp(it->ep_name, ep_name) == 0) { return it; }{...} }{...} return NULL; }{ ... } static esp_err_t protocomm_add_endpoint_internal(protocomm_t *pc, const char *ep_name, protocomm_req_handler_t h, void *priv_data, uint32_t flag) { if ((pc == NULL) || (ep_name == NULL) || (h == NULL)) { return ESP_ERR_INVALID_ARG; }{...} protocomm_ep_t *ep; esp_err_t ret; ep = search_endpoint(pc, ep_name); if (ep) { ESP_LOGE(TAG, "Endpoint with this name already exists"); return ESP_FAIL; }{...} if (pc->add_endpoint) { ret = pc->add_endpoint(ep_name, h, priv_data); if (ret != ESP_OK) { ESP_LOGE(TAG, "Error adding endpoint"); return ret; }{...} }{...} ep = (protocomm_ep_t *) calloc(1, sizeof(protocomm_ep_t)); if (!ep) { ESP_LOGE(TAG, "Error allocating endpoint resource"); return ESP_ERR_NO_MEM; }{...} /* Initialize ep handler */ ep->ep_name = ep_name; ep->req_handler = h; ep->priv_data = priv_data; ep->flag = flag; /* Add endpoint to the head of the singly linked list */ SLIST_INSERT_HEAD(&pc->endpoints, ep, next); return ESP_OK; }{ ... } esp_err_t protocomm_add_endpoint(protocomm_t *pc, const char *ep_name, protocomm_req_handler_t h, void *priv_data) { return protocomm_add_endpoint_internal(pc, ep_name, h, priv_data, REQ_EP); }{ ... } esp_err_t protocomm_remove_endpoint(protocomm_t *pc, const char *ep_name) { if ((pc == NULL) || (ep_name == NULL)) { return ESP_ERR_INVALID_ARG; }{...} if (pc->remove_endpoint) { pc->remove_endpoint(ep_name); }{...} protocomm_ep_t *it, *tmp; SLIST_FOREACH_SAFE(it, &pc->endpoints, next, tmp) { if (strcmp(ep_name, it->ep_name) == 0) { SLIST_REMOVE(&pc->endpoints, it, protocomm_ep, next); free(it); return ESP_OK; }{...} }{...} return ESP_ERR_NOT_FOUND; }{ ... } esp_err_t protocomm_open_session(protocomm_t *pc, uint32_t session_id) { if (!pc) { return ESP_ERR_INVALID_ARG; }{...} if (pc->sec && pc->sec->new_transport_session) { esp_err_t ret = pc->sec->new_transport_session(pc->sec_inst, session_id); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to launch new session with ID: %" PRId32, session_id); return ret; }{...} }{...} return ESP_OK; }{ ... } esp_err_t protocomm_close_session(protocomm_t *pc, uint32_t session_id) { if (!pc) { return ESP_ERR_INVALID_ARG; }{...} if (pc->sec && pc->sec->close_transport_session) { esp_err_t ret = pc->sec->close_transport_session(pc->sec_inst, session_id); if (ret != ESP_OK) { ESP_LOGE(TAG, "Error while closing session with ID: %" PRId32, session_id); return ret; }{...} }{...} return ESP_OK; }{ ... } esp_err_t protocomm_req_handle(protocomm_t *pc, const char *ep_name, uint32_t session_id, const uint8_t *inbuf, ssize_t inlen, uint8_t **outbuf, ssize_t *outlen) { if (!pc || !ep_name || !outbuf || !outlen) { ESP_LOGE(TAG, "Invalid params %p %p", pc, ep_name); return ESP_ERR_INVALID_ARG; }{...} *outbuf = NULL; *outlen = 0; protocomm_ep_t *ep = search_endpoint(pc, ep_name); if (!ep) { ESP_LOGE(TAG, "No registered endpoint for %s", ep_name); return ESP_ERR_NOT_FOUND; }{...} esp_err_t ret = ESP_FAIL; if (ep->flag & SEC_EP) { /* Call the registered endpoint handler for establishing secure session */ ret = ep->req_handler(session_id, inbuf, inlen, outbuf, outlen, ep->priv_data); ESP_LOGD(TAG, "SEC_EP Req handler returned %d", ret); }{...} else if (ep->flag & REQ_EP) { if (pc->sec && pc->sec->decrypt) { /* Decrypt the data first */ ssize_t dec_inbuf_len = 0; uint8_t *dec_inbuf = NULL; ret = pc->sec->decrypt(pc->sec_inst, session_id, inbuf, inlen, &dec_inbuf, &dec_inbuf_len); if (ret != ESP_OK) { ESP_LOGE(TAG, "Decryption of response failed for endpoint %s", ep_name); free(dec_inbuf); return ret; }{...} /* Invoke the request handler */ uint8_t *plaintext_resp = NULL; ssize_t plaintext_resp_len = 0; ret = ep->req_handler(session_id, dec_inbuf, dec_inbuf_len, &plaintext_resp, &plaintext_resp_len, ep->priv_data); if (ret != ESP_OK) { ESP_LOGE(TAG, "Request handler for %s failed", ep_name); free(plaintext_resp); free(dec_inbuf); return ret; }{...} /* We don't need decrypted data anymore */ free(dec_inbuf); uint8_t *enc_resp = NULL; ssize_t enc_resp_len = 0; ret = pc->sec->encrypt(pc->sec_inst, session_id, plaintext_resp, plaintext_resp_len, &enc_resp, &enc_resp_len); if (ret != ESP_OK) { ESP_LOGE(TAG, "Encryption of response failed for endpoint %s", ep_name); free(enc_resp); free(plaintext_resp); return ret; }{...} /* We no more need plaintext response */ free(plaintext_resp); /* Set outbuf and outlen appropriately */ *outbuf = enc_resp; *outlen = enc_resp_len; }{...} else { /* No encryption */ ret = ep->req_handler(session_id, inbuf, inlen, outbuf, outlen, ep->priv_data); ESP_LOGD(TAG, "No encrypt ret %d", ret); }{...} }{...} else if (ep->flag & VER_EP) { ret = ep->req_handler(session_id, inbuf, inlen, outbuf, outlen, ep->priv_data); ESP_LOGD(TAG, "VER_EP Req handler returned %d", ret); }{...} return ret; }{ ... } static int protocomm_common_security_handler(uint32_t session_id, const uint8_t *inbuf, ssize_t inlen, uint8_t **outbuf, ssize_t *outlen, void *priv_data) { protocomm_t *pc = (protocomm_t *) priv_data; if (pc->sec && pc->sec->security_req_handler) { return pc->sec->security_req_handler(pc->sec_inst, pc->sec_params, session_id, inbuf, inlen, outbuf, outlen, priv_data); }{...} return ESP_OK; }{ ... } esp_err_t protocomm_set_security(protocomm_t *pc, const char *ep_name, const protocomm_security_t *sec, const void *sec_params) { if ((pc == NULL) || (ep_name == NULL) || (sec == NULL)) { return ESP_ERR_INVALID_ARG; }{...} if (pc->sec) { return ESP_ERR_INVALID_STATE; }{...} esp_err_t ret = protocomm_add_endpoint_internal(pc, ep_name, protocomm_common_security_handler, (void *) pc, SEC_EP); if (ret != ESP_OK) { ESP_LOGE(TAG, "Error adding security endpoint"); return ret; }{...} if (sec->init) { ret = sec->init(&pc->sec_inst); if (ret != ESP_OK) { ESP_LOGE(TAG, "Error initializing security"); protocomm_remove_endpoint(pc, ep_name); return ret; }{...} }{...} pc->sec = sec; /* sec params is not needed and thus checked in case of security 0 */ if (pc->sec->ver == 1) { if (sec_params) { pc->sec_params = calloc(1, sizeof(protocomm_security1_params_t)); if (pc->sec_params == NULL) { ESP_LOGE(TAG, "Error allocating memory for security1 params"); ret = ESP_ERR_NO_MEM; goto cleanup; }{...} memcpy((void *)pc->sec_params, sec_params, sizeof(protocomm_security1_params_t)); }{...} }{...} else if (pc->sec->ver == 2) { if (sec_params) { pc->sec_params = calloc(1, sizeof(protocomm_security2_params_t)); if (pc->sec_params == NULL) { ESP_LOGE(TAG, "Error allocating memory for security2 params"); ret = ESP_ERR_NO_MEM; goto cleanup; }{...} memcpy((void *)pc->sec_params, sec_params, sizeof(protocomm_security2_params_t)); }{...} else { ESP_LOGE(TAG, "Security params cannot be null"); ret = ESP_ERR_INVALID_ARG; goto cleanup; }{...} }{...} return ESP_OK; cleanup: if (pc->sec && pc->sec->cleanup) { pc->sec->cleanup(pc->sec_inst); pc->sec_inst = NULL; pc->sec = NULL; }{...} protocomm_remove_endpoint(pc, ep_name); return ret; }{ ... } esp_err_t protocomm_unset_security(protocomm_t *pc, const char *ep_name) { if ((pc == NULL) || (ep_name == NULL)) { return ESP_FAIL; }{...} if (pc->sec && pc->sec->cleanup) { pc->sec->cleanup(pc->sec_inst); pc->sec_inst = NULL; pc->sec = NULL; }{...} if (pc->sec_params) { free(pc->sec_params); pc->sec_params = NULL; }{...} return protocomm_remove_endpoint(pc, ep_name); }{ ... } static int protocomm_version_handler(uint32_t session_id, const uint8_t *inbuf, ssize_t inlen, uint8_t **outbuf, ssize_t *outlen, void *priv_data) { protocomm_t *pc = (protocomm_t *) priv_data; if (!pc->ver) { *outlen = 0; *outbuf = NULL; return ESP_OK; }{...} /* Output is a non null terminated string with length specified */ *outlen = strlen(pc->ver); *outbuf = malloc(*outlen); if (*outbuf == NULL) { ESP_LOGE(TAG, "Failed to allocate memory for version response"); return ESP_ERR_NO_MEM; }{...} memcpy(*outbuf, pc->ver, *outlen); return ESP_OK; }{ ... } esp_err_t protocomm_set_version(protocomm_t *pc, const char *ep_name, const char *version) { if ((pc == NULL) || (ep_name == NULL) || (version == NULL)) { return ESP_ERR_INVALID_ARG; }{...} if (pc->ver) { return ESP_ERR_INVALID_STATE; }{...} pc->ver = strdup(version); if (pc->ver == NULL) { ESP_LOGE(TAG, "Error allocating version string"); return ESP_ERR_NO_MEM; }{...} esp_err_t ret = protocomm_add_endpoint_internal(pc, ep_name, protocomm_version_handler, (void *) pc, VER_EP); if (ret != ESP_OK) { ESP_LOGE(TAG, "Error adding version endpoint"); return ret; }{...} return ESP_OK; }{ ... } esp_err_t protocomm_unset_version(protocomm_t *pc, const char *ep_name) { if ((pc == NULL) || (ep_name == NULL)) { return ESP_ERR_INVALID_ARG; }{...} if (pc->ver) { free((char *)pc->ver); pc->ver = NULL; }{...} return protocomm_remove_endpoint(pc, ep_name); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.