Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * Copyright (C) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
4 : *
5 : * @file nnstreamer-edge-data.c
6 : * @date 7 Sep 2022
7 : * @brief Util functions for edge data.
8 : * @see https://github.com/nnstreamer/nnstreamer
9 : * @author Gichan Jang <gichan2.jang@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 :
13 : #include "nnstreamer-edge-data.h"
14 : #include "nnstreamer-edge-log.h"
15 : #include "nnstreamer-edge-util.h"
16 : #include "nnstreamer-edge-metadata.h"
17 :
18 : #define NNS_EDGE_DATA_KEY (0xeddaedda)
19 :
20 : /**
21 : * @brief Internal data structure for the header of the serialized edge data.
22 : */
23 : typedef struct
24 : {
25 : uint32_t key;
26 : uint64_t version;
27 : uint32_t num_mem;
28 : nns_size_t data_len[NNS_EDGE_DATA_LIMIT];
29 : nns_size_t meta_len;
30 : } nns_edge_data_header_s;
31 :
32 : /**
33 : * @brief Internal data structure for edge data.
34 : */
35 : typedef struct
36 : {
37 : uint32_t magic;
38 : pthread_mutex_t lock;
39 : uint32_t num;
40 : nns_edge_raw_data_s data[NNS_EDGE_DATA_LIMIT];
41 : nns_edge_metadata_h metadata;
42 : } nns_edge_data_s;
43 :
44 : /**
45 : * @brief Create nnstreamer edge data.
46 : */
47 : int
48 171 : nns_edge_data_create (nns_edge_data_h * data_h)
49 : {
50 : nns_edge_data_s *ed;
51 :
52 171 : if (!data_h) {
53 1 : nns_edge_loge ("Invalid param, data_h should not be null.");
54 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
55 : }
56 :
57 170 : ed = (nns_edge_data_s *) calloc (1, sizeof (nns_edge_data_s));
58 170 : if (!ed) {
59 0 : nns_edge_loge ("Failed to allocate memory for edge data.");
60 0 : return NNS_EDGE_ERROR_OUT_OF_MEMORY;
61 : }
62 :
63 170 : nns_edge_lock_init (ed);
64 170 : nns_edge_handle_set_magic (ed, NNS_EDGE_MAGIC);
65 170 : nns_edge_metadata_create (&ed->metadata);
66 :
67 170 : *data_h = ed;
68 170 : return NNS_EDGE_ERROR_NONE;
69 : }
70 :
71 : /**
72 : * @brief Destroy nnstreamer edge data.
73 : */
74 : int
75 172 : nns_edge_data_destroy (nns_edge_data_h data_h)
76 : {
77 : nns_edge_data_s *ed;
78 : unsigned int i;
79 :
80 172 : ed = (nns_edge_data_s *) data_h;
81 172 : if (!ed) {
82 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
83 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
84 : }
85 :
86 171 : if (!nns_edge_handle_is_valid (ed)) {
87 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
88 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
89 : }
90 :
91 170 : nns_edge_lock (ed);
92 170 : nns_edge_handle_set_magic (ed, NNS_EDGE_MAGIC_DEAD);
93 :
94 585 : for (i = 0; i < ed->num; i++) {
95 415 : if (ed->data[i].destroy_cb)
96 122 : ed->data[i].destroy_cb (ed->data[i].data);
97 : }
98 :
99 170 : nns_edge_metadata_destroy (ed->metadata);
100 :
101 170 : nns_edge_unlock (ed);
102 170 : nns_edge_lock_destroy (ed);
103 170 : SAFE_FREE (ed);
104 170 : return NNS_EDGE_ERROR_NONE;
105 : }
106 :
107 : /**
108 : * @brief Internal wrapper function of the nns_edge_data_destroy() to avoid build warning of the incompatibe type casting. (See nns_edge_data_destroy_cb())
109 : */
110 : void
111 0 : nns_edge_data_release_handle (void *data)
112 : {
113 0 : nns_edge_data_h data_h = (nns_edge_data_h) data;
114 :
115 0 : if (data_h) {
116 0 : if (NNS_EDGE_ERROR_NONE != nns_edge_data_destroy (data_h))
117 0 : nns_edge_logw ("Failed to destroy the nns-edge data handle.");
118 : }
119 0 : }
120 :
121 : /**
122 : * @brief Validate edge data handle.
123 : */
124 : int
125 43 : nns_edge_data_is_valid (nns_edge_data_h data_h)
126 : {
127 : nns_edge_data_s *ed;
128 :
129 43 : ed = (nns_edge_data_s *) data_h;
130 43 : if (!ed) {
131 3 : nns_edge_loge ("Invalid param, given edge data handle is null.");
132 3 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
133 : }
134 :
135 40 : if (!nns_edge_handle_is_valid (ed)) {
136 1 : nns_edge_loge ("Invalid param, edge data handle is invalid.");
137 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
138 : }
139 :
140 39 : return NNS_EDGE_ERROR_NONE;
141 : }
142 :
143 : /**
144 : * @brief Copy edge data and return new handle.
145 : */
146 : int
147 81 : nns_edge_data_copy (nns_edge_data_h data_h, nns_edge_data_h * new_data_h)
148 : {
149 : nns_edge_data_s *ed;
150 : nns_edge_data_s *copied;
151 : unsigned int i;
152 : int ret;
153 :
154 81 : ed = (nns_edge_data_s *) data_h;
155 81 : if (!ed) {
156 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
157 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
158 : }
159 :
160 80 : if (!new_data_h) {
161 1 : nns_edge_loge ("Invalid param, new_data_h should not be null.");
162 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
163 : }
164 :
165 79 : if (!nns_edge_handle_is_valid (ed)) {
166 1 : nns_edge_loge ("Invalid param, edge data handle is invalid.");
167 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
168 : }
169 :
170 78 : nns_edge_lock (ed);
171 :
172 78 : ret = nns_edge_data_create (new_data_h);
173 78 : if (ret != NNS_EDGE_ERROR_NONE) {
174 0 : nns_edge_loge ("Failed to create new data handle.");
175 0 : nns_edge_unlock (ed);
176 0 : return ret;
177 : }
178 :
179 78 : copied = (nns_edge_data_s *) (*new_data_h);
180 :
181 78 : copied->num = ed->num;
182 170 : for (i = 0; i < ed->num; i++) {
183 92 : copied->data[i].data = nns_edge_memdup (ed->data[i].data,
184 : ed->data[i].data_len);
185 :
186 92 : if (!copied->data[i].data) {
187 0 : nns_edge_loge ("Failed to copy data, error while allocating new memory.");
188 0 : copied->num = i;
189 0 : ret = NNS_EDGE_ERROR_OUT_OF_MEMORY;
190 0 : goto done;
191 : }
192 :
193 92 : copied->data[i].data_len = ed->data[i].data_len;
194 92 : copied->data[i].destroy_cb = nns_edge_free;
195 : }
196 :
197 78 : ret = nns_edge_metadata_copy (copied->metadata, ed->metadata);
198 :
199 78 : done:
200 78 : if (ret != NNS_EDGE_ERROR_NONE) {
201 0 : nns_edge_data_destroy (*new_data_h);
202 0 : *new_data_h = NULL;
203 : }
204 :
205 78 : nns_edge_unlock (ed);
206 78 : return ret;
207 : }
208 :
209 : /**
210 : * @brief Add raw data into nnstreamer edge data.
211 : */
212 : int
213 307 : nns_edge_data_add (nns_edge_data_h data_h, void *data, nns_size_t data_len,
214 : nns_edge_data_destroy_cb destroy_cb)
215 : {
216 : nns_edge_data_s *ed;
217 :
218 307 : ed = (nns_edge_data_s *) data_h;
219 307 : if (!ed) {
220 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
221 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
222 : }
223 :
224 306 : if (!data || data_len <= 0) {
225 2 : nns_edge_loge ("Invalid param, data should not be null.");
226 2 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
227 : }
228 :
229 304 : if (!nns_edge_handle_is_valid (ed)) {
230 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
231 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
232 : }
233 :
234 303 : nns_edge_lock (ed);
235 :
236 303 : if (ed->num >= NNS_EDGE_DATA_LIMIT) {
237 1 : nns_edge_loge ("Cannot add data, the maximum number of edge data is %d.",
238 : NNS_EDGE_DATA_LIMIT);
239 1 : nns_edge_unlock (ed);
240 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
241 : }
242 :
243 302 : ed->data[ed->num].data = data;
244 302 : ed->data[ed->num].data_len = data_len;
245 302 : ed->data[ed->num].destroy_cb = destroy_cb;
246 302 : ed->num++;
247 :
248 302 : nns_edge_unlock (ed);
249 302 : return NNS_EDGE_ERROR_NONE;
250 : }
251 :
252 : /**
253 : * @brief Remove raw data in edge data.
254 : */
255 : int
256 3 : nns_edge_data_clear (nns_edge_data_h data_h)
257 : {
258 : nns_edge_data_s *ed;
259 : unsigned int i;
260 :
261 3 : ed = (nns_edge_data_s *) data_h;
262 3 : if (!ed) {
263 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
264 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
265 : }
266 :
267 2 : if (!nns_edge_handle_is_valid (ed)) {
268 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
269 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
270 : }
271 :
272 1 : nns_edge_lock (ed);
273 :
274 2 : for (i = 0; i < ed->num; i++) {
275 1 : if (ed->data[i].destroy_cb)
276 1 : ed->data[i].destroy_cb (ed->data[i].data);
277 1 : ed->data[i].data = NULL;
278 1 : ed->data[i].data_len = 0;
279 1 : ed->data[i].destroy_cb = NULL;
280 : }
281 1 : ed->num = 0;
282 :
283 1 : nns_edge_unlock (ed);
284 :
285 1 : return NNS_EDGE_ERROR_NONE;
286 : }
287 :
288 : /**
289 : * @brief Get the n'th edge data.
290 : */
291 : int
292 75 : nns_edge_data_get (nns_edge_data_h data_h, unsigned int index, void **data,
293 : nns_size_t * data_len)
294 : {
295 : nns_edge_data_s *ed;
296 :
297 75 : ed = (nns_edge_data_s *) data_h;
298 75 : if (!ed) {
299 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
300 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
301 : }
302 :
303 74 : if (!data || !data_len) {
304 2 : nns_edge_loge ("Invalid param, data and len should not be null.");
305 2 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
306 : }
307 :
308 72 : if (!nns_edge_handle_is_valid (ed)) {
309 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
310 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
311 : }
312 :
313 71 : nns_edge_lock (ed);
314 :
315 71 : if (index >= ed->num) {
316 1 : nns_edge_loge
317 : ("Invalid param, the number of edge data is %u but requested %uth data.",
318 : ed->num, index);
319 1 : nns_edge_unlock (ed);
320 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
321 : }
322 :
323 70 : *data = ed->data[index].data;
324 70 : *data_len = ed->data[index].data_len;
325 :
326 70 : nns_edge_unlock (ed);
327 70 : return NNS_EDGE_ERROR_NONE;
328 : }
329 :
330 : /**
331 : * @brief Get the number of edge data in handle.
332 : */
333 : int
334 64 : nns_edge_data_get_count (nns_edge_data_h data_h, unsigned int *count)
335 : {
336 : nns_edge_data_s *ed;
337 :
338 64 : ed = (nns_edge_data_s *) data_h;
339 64 : if (!ed) {
340 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
341 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
342 : }
343 :
344 63 : if (!count) {
345 1 : nns_edge_loge ("Invalid param, count should not be null.");
346 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
347 : }
348 :
349 62 : if (!nns_edge_handle_is_valid (ed)) {
350 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
351 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
352 : }
353 :
354 61 : nns_edge_lock (ed);
355 61 : *count = ed->num;
356 61 : nns_edge_unlock (ed);
357 :
358 61 : return NNS_EDGE_ERROR_NONE;
359 : }
360 :
361 : /**
362 : * @brief Set the information of edge data.
363 : */
364 : int
365 76 : nns_edge_data_set_info (nns_edge_data_h data_h, const char *key,
366 : const char *value)
367 : {
368 : nns_edge_data_s *ed;
369 : int ret;
370 :
371 76 : ed = (nns_edge_data_s *) data_h;
372 76 : if (!ed) {
373 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
374 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
375 : }
376 :
377 75 : if (!STR_IS_VALID (key)) {
378 1 : nns_edge_loge ("Invalid param, given key is invalid.");
379 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
380 : }
381 :
382 74 : if (!nns_edge_handle_is_valid (ed)) {
383 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
384 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
385 : }
386 :
387 73 : nns_edge_lock (ed);
388 73 : ret = nns_edge_metadata_set (ed->metadata, key, value);
389 73 : nns_edge_unlock (ed);
390 :
391 73 : return ret;
392 : }
393 :
394 : /**
395 : * @brief Get the information of edge data. Caller should release the returned value using free().
396 : */
397 : int
398 94 : nns_edge_data_get_info (nns_edge_data_h data_h, const char *key, char **value)
399 : {
400 : nns_edge_data_s *ed;
401 : int ret;
402 :
403 94 : ed = (nns_edge_data_s *) data_h;
404 94 : if (!ed) {
405 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
406 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
407 : }
408 :
409 93 : if (!STR_IS_VALID (key)) {
410 2 : nns_edge_loge ("Invalid param, given key is invalid.");
411 2 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
412 : }
413 :
414 91 : if (!value) {
415 1 : nns_edge_loge ("Invalid param, value should not be null.");
416 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
417 : }
418 :
419 90 : if (!nns_edge_handle_is_valid (ed)) {
420 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
421 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
422 : }
423 :
424 89 : nns_edge_lock (ed);
425 89 : ret = nns_edge_metadata_get (ed->metadata, key, value);
426 89 : nns_edge_unlock (ed);
427 :
428 89 : return ret;
429 : }
430 :
431 : /**
432 : * @brief Clear information of edge data.
433 : */
434 : int
435 3 : nns_edge_data_clear_info (nns_edge_data_h data_h)
436 : {
437 : nns_edge_data_s *ed;
438 : int ret;
439 :
440 3 : ed = (nns_edge_data_s *) data_h;
441 3 : if (!ed) {
442 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
443 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
444 : }
445 :
446 2 : if (!nns_edge_handle_is_valid (ed)) {
447 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
448 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
449 : }
450 :
451 1 : nns_edge_lock (ed);
452 1 : ret = nns_edge_metadata_destroy (ed->metadata);
453 1 : if (NNS_EDGE_ERROR_NONE != ret)
454 0 : goto done;
455 1 : ret = nns_edge_metadata_create (&ed->metadata);
456 1 : done:
457 1 : nns_edge_unlock (ed);
458 :
459 1 : return ret;
460 : }
461 :
462 : /**
463 : * @brief Serialize metadata in edge data.
464 : */
465 : int
466 39 : nns_edge_data_serialize_meta (nns_edge_data_h data_h, void **data,
467 : nns_size_t * data_len)
468 : {
469 : nns_edge_data_s *ed;
470 : int ret;
471 :
472 39 : ed = (nns_edge_data_s *) data_h;
473 39 : if (!ed) {
474 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
475 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
476 : }
477 :
478 38 : if (!nns_edge_handle_is_valid (ed)) {
479 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
480 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
481 : }
482 :
483 37 : nns_edge_lock (ed);
484 37 : ret = nns_edge_metadata_serialize (ed->metadata, data, data_len);
485 37 : nns_edge_unlock (ed);
486 :
487 37 : return ret;
488 : }
489 :
490 : /**
491 : * @brief Deserialize metadata in edge data.
492 : */
493 : int
494 34 : nns_edge_data_deserialize_meta (nns_edge_data_h data_h, const void *data,
495 : const nns_size_t data_len)
496 : {
497 : nns_edge_data_s *ed;
498 : int ret;
499 :
500 34 : ed = (nns_edge_data_s *) data_h;
501 34 : if (!ed) {
502 1 : nns_edge_loge ("Invalid param, given edge data handle is null.");
503 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
504 : }
505 :
506 33 : if (!nns_edge_handle_is_valid (ed)) {
507 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
508 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
509 : }
510 :
511 32 : nns_edge_lock (ed);
512 32 : ret = nns_edge_metadata_deserialize (ed->metadata, data, data_len);
513 32 : nns_edge_unlock (ed);
514 :
515 32 : return ret;
516 : }
517 :
518 : /**
519 : * @brief Serialize edge data (meta data + raw data).
520 : */
521 : int
522 13 : nns_edge_data_serialize (nns_edge_data_h data_h, void **data, nns_size_t * len)
523 : {
524 : nns_edge_data_s *ed;
525 : nns_edge_data_header_s edata_header;
526 13 : void *meta_serialized = NULL;
527 : nns_size_t total, header_len, data_len;
528 : char *serialized, *ptr;
529 : unsigned int n;
530 : int ret;
531 :
532 13 : ed = (nns_edge_data_s *) data_h;
533 13 : if (!ed || !data || !len) {
534 2 : nns_edge_loge ("Invalid param, one of the given param is null.");
535 13 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
536 : }
537 :
538 11 : if (!nns_edge_handle_is_valid (ed)) {
539 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
540 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
541 : }
542 :
543 10 : nns_edge_lock (ed);
544 10 : header_len = sizeof (nns_edge_data_header_s);
545 :
546 10 : data_len = 0;
547 10 : edata_header.key = NNS_EDGE_DATA_KEY;
548 10 : edata_header.version = nns_edge_generate_version_key ();
549 10 : edata_header.num_mem = ed->num;
550 22 : for (n = 0; n < ed->num; n++) {
551 12 : edata_header.data_len[n] = ed->data[n].data_len;
552 12 : data_len += ed->data[n].data_len;
553 : }
554 :
555 10 : ret = nns_edge_metadata_serialize (ed->metadata, &meta_serialized,
556 : &edata_header.meta_len);
557 10 : if (NNS_EDGE_ERROR_NONE != ret) {
558 0 : goto done;
559 : }
560 :
561 10 : total = header_len + data_len + edata_header.meta_len;
562 :
563 10 : serialized = ptr = (char *) nns_edge_malloc (total);
564 10 : if (!serialized) {
565 0 : ret = NNS_EDGE_ERROR_OUT_OF_MEMORY;
566 0 : goto done;
567 : }
568 :
569 : /** Copy serialization header of edge data */
570 10 : memcpy (ptr, &edata_header, header_len);
571 10 : ptr += header_len;
572 :
573 : /** Copy edge data */
574 22 : for (n = 0; n < ed->num; n++) {
575 12 : memcpy (ptr, ed->data[n].data, ed->data[n].data_len);
576 12 : ptr += ed->data[n].data_len;
577 : }
578 :
579 : /** Copy edge meta data */
580 10 : memcpy (ptr, meta_serialized, edata_header.meta_len);
581 :
582 10 : *data = serialized;
583 10 : *len = total;
584 :
585 10 : done:
586 10 : SAFE_FREE (meta_serialized);
587 10 : nns_edge_unlock (ed);
588 10 : return ret;
589 : }
590 :
591 : /**
592 : * @brief Deserialize metadata in edge data.
593 : */
594 : int
595 15 : nns_edge_data_deserialize (nns_edge_data_h data_h, const void *data,
596 : const nns_size_t data_len)
597 : {
598 : nns_edge_data_s *ed;
599 : nns_edge_data_header_s *header;
600 : int ret;
601 : unsigned int n;
602 : char *ptr;
603 :
604 15 : ed = (nns_edge_data_s *) data_h;
605 15 : if (!ed || !data) {
606 2 : nns_edge_loge ("Invalid param, one of the given param is null.");
607 2 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
608 : }
609 :
610 13 : if (!nns_edge_handle_is_valid (ed)) {
611 1 : nns_edge_loge ("Invalid param, given edge data is invalid.");
612 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
613 : }
614 :
615 12 : ret = nns_edge_data_is_serialized (data, data_len);
616 12 : if (ret != NNS_EDGE_ERROR_NONE)
617 1 : return ret;
618 :
619 11 : nns_edge_lock (ed);
620 11 : header = (nns_edge_data_header_s *) data;
621 11 : ptr = (char *) data + sizeof (nns_edge_data_header_s);
622 :
623 11 : ed->num = header->num_mem;
624 33 : for (n = 0; n < ed->num; n++) {
625 22 : ed->data[n].data = nns_edge_memdup (ptr, header->data_len[n]);
626 22 : ed->data[n].data_len = header->data_len[n];
627 22 : ed->data[n].destroy_cb = nns_edge_free;
628 :
629 22 : ptr += header->data_len[n];
630 : }
631 :
632 11 : ret = nns_edge_metadata_deserialize (ed->metadata, ptr, header->meta_len);
633 :
634 11 : nns_edge_unlock (ed);
635 11 : return ret;
636 : }
637 :
638 : /**
639 : * @brief Check given data is serialized buffer.
640 : */
641 : int
642 15 : nns_edge_data_is_serialized (const void *data, const nns_size_t data_len)
643 : {
644 : nns_edge_data_header_s *header;
645 : nns_size_t total;
646 : unsigned int n;
647 :
648 15 : if (!data) {
649 1 : nns_edge_loge ("Invalid param, given data is null.");
650 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
651 : }
652 :
653 14 : header = (nns_edge_data_header_s *) data;
654 14 : if (header->key != NNS_EDGE_DATA_KEY) {
655 1 : nns_edge_loge ("Invalid param, given data has invalid format.");
656 1 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
657 : }
658 :
659 13 : if (!nns_edge_parse_version_key (header->version, NULL, NULL, NULL)) {
660 0 : nns_edge_loge ("Invalid param, given data has invalid version.");
661 0 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
662 : }
663 :
664 : /**
665 : * @todo The number of memories in data.
666 : * Total number of memories in edge-data should be less than NNS_EDGE_DATA_LIMIT.
667 : * Fetch nns-edge version info and check allowed memories if NNS_EDGE_DATA_LIMIT is updated.
668 : */
669 13 : if (header->num_mem > NNS_EDGE_DATA_LIMIT) {
670 0 : nns_edge_loge ("Invalid param, given data has invalid memories.");
671 0 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
672 : }
673 :
674 : /* Check mem size */
675 13 : total = sizeof (nns_edge_data_header_s) + header->meta_len;
676 35 : for (n = 0; n < header->num_mem; n++)
677 22 : total += header->data_len[n];
678 :
679 13 : if (total != data_len) {
680 2 : nns_edge_loge ("Invalid param, given data has invalid data size.");
681 2 : return NNS_EDGE_ERROR_INVALID_PARAMETER;
682 : }
683 :
684 11 : return NNS_EDGE_ERROR_NONE;
685 : }
|