msharee9 commented on a change in pull request #674: Minificpp 1007 - ECU C2 
integration.
URL: https://github.com/apache/nifi-minifi-cpp/pull/674#discussion_r371381093
 
 

 ##########
 File path: nanofi/src/coap/c2protocol.c
 ##########
 @@ -0,0 +1,403 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "cbor.h"
+#include <coap/c2protocol.h>
+#include <coap/c2agent.h>
+#include <coap/c2payload.h>
+#include "utlist.h"
+
+#include <string.h>
+
+void free_agent_manifest(c2heartbeat_t * hb) {
+    if (!hb) return;
+
+    int i;
+    for (i = 0; i < hb->ag_manifest.num_ecus; ++i) {
+        free((void *)hb->ag_manifest.ecus[i].input);
+        free((void *)hb->ag_manifest.ecus[i].output);
+        free((void *)hb->ag_manifest.ecus[i].name);
+        free_properties(hb->ag_manifest.ecus[i].ip_args);
+        free_properties(hb->ag_manifest.ecus[i].op_args);
+    }
+    free(hb->ag_manifest.ecus);
+
+    /*for (i = 0; i < hb->ag_manifest.io.num_ips; ++i) {
+        free(hb->ag_manifest.io.input_params[i].name);
+        int n;
+        for (n = 0; n < hb->ag_manifest.io.input_params[i].num_params; ++n) {
+            free(hb->ag_manifest.io.input_params[i].params[n]);
+        }
+        free(hb->ag_manifest.io.input_params[i].params);
+    }
+    free(hb->ag_manifest.io.input_params);
+
+    for (i = 0; i < hb->ag_manifest.io.num_ops; ++i) {
+        free(hb->ag_manifest.io.output_params[i].name);
+        int n;
+        for (n = 0; n < hb->ag_manifest.io.output_params[i].num_params; ++n) {
+            free(hb->ag_manifest.io.output_params[i].params[n]);
+        }
+        free(hb->ag_manifest.io.output_params[i].params);
+    }
+    free(hb->ag_manifest.io.output_params);*/
+}
+
+void free_c2heartbeat(c2heartbeat_t * c2_heartbeat) {
+    if (!c2_heartbeat) {
+        return;
+    }
+    const char * machine_arch = 
c2_heartbeat->device_info.system_info.machine_arch;
+    free((void *)machine_arch);
+    c2_heartbeat->device_info.system_info.machine_arch = NULL;
+
+    const char * ac = c2_heartbeat->agent_info.agent_class;
+    free((void *)ac);
+    c2_heartbeat->agent_info.agent_class = NULL;
+
+    const char * id = c2_heartbeat->device_info.ident;
+    free((void *)id);
+    c2_heartbeat->device_info.ident = NULL;
+
+    const char * devid = c2_heartbeat->device_info.network_info.device_id;
+    free((void *)devid);
+    c2_heartbeat->device_info.network_info.device_id = NULL;
+    if (c2_heartbeat->has_ag_manifest) {
+        free_agent_manifest(c2_heartbeat);
+    }
+}
+
+void build_cbor_map(cbor_item_t * cbor_map, uint16_t key, value_t value);
+
+void build_cbor_list(cbor_item_t * item, value_t value) {
+    if (!item || !cbor_isa_array(item)) return;
+
+    switch (value.val_type) {
+    case UINT8_TYPE: {
+        cbor_array_push(item, cbor_move(cbor_build_uint8(value.v_uint8)));
+        break;
+    }
+    case UINT16_TYPE: {
+        cbor_array_push(item, cbor_move(cbor_build_uint16(value.v_uint16)));
+        break;
+    }
+    case UINT32_TYPE: {
+        cbor_array_push(item, cbor_move(cbor_build_uint32(value.v_uint32)));
+        break;
+    }
+    case UINT64_TYPE: {
+        cbor_array_push(item, cbor_move(cbor_build_uint64(value.v_uint64)));
+        break;
+    }
+    case STRING_TYPE: {
+        cbor_array_push(item, cbor_move(cbor_build_string(value.v_str)));
+        break;
+    }
+    case HASH_TYPE: {
+        cbor_item_t * map = cbor_new_indefinite_map();
+        struct c2_payload_map * el, *tmp;
+        HASH_ITER(hh, value.v_map, el, tmp) {
+            build_cbor_map(map, el->key, el->value);
+        }
+        cbor_array_push(item, cbor_move(map));
+        break;
+    }
+    case LIST_TYPE: {
+        cbor_item_t * list = cbor_new_indefinite_array();
+        c2_payload_list_t * el;
+        LL_FOREACH(value.v_maplist, el) {
+            build_cbor_list(list, el->value);
+        }
+        cbor_array_push(item, cbor_move(list));
+        break;
+    }
+    case PROP_TYPE: {
+        cbor_item_t * pmap = cbor_new_indefinite_map();
+        properties_t * el, *tmp;
+        HASH_ITER(hh, value.v_props, el, tmp) {
+            cbor_map_add(pmap,
+                         (struct cbor_pair){
+                            .key = cbor_move(cbor_build_string(el->key)),
+                            .value = cbor_move(cbor_build_string(el->value))
+                         });
+        }
+        cbor_array_push(item, pmap);
+        break;
+    }
+    default:
+        break;
+    }
+}
+
+void build_cbor_map(cbor_item_t * cbor_map, uint16_t key, value_t value) {
+    if (!cbor_map || !cbor_isa_map(cbor_map)) return;
+
+    switch (value.val_type) {
+    case UINT8_TYPE: {
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = cbor_move(cbor_build_uint8(value.v_uint8))});
+        break;
+    }
+    case UINT16_TYPE: {
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = 
cbor_move(cbor_build_uint16(value.v_uint16))});
+        break;
+    }
+    case UINT32_TYPE: {
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = 
cbor_move(cbor_build_uint32(value.v_uint32))});
+        break;
+    }
+    case UINT64_TYPE: {
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = 
cbor_move(cbor_build_uint64(value.v_uint64))});
+        break;
+    }
+    case STRING_TYPE: {
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = cbor_move(cbor_build_string(value.v_str))});
+        break;
+    }
+    case HASH_TYPE: {
+        cbor_item_t * map_item = cbor_new_indefinite_map();
+        struct c2_payload_map * el, *tmp;
+        HASH_ITER(hh, value.v_map, el, tmp) {
+            build_cbor_map(map_item, el->key, el->value);
+        }
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = cbor_move(map_item)
+                     });
+        break;
+    }
+    case LIST_TYPE: {
+        cbor_item_t * list_item = cbor_new_indefinite_array();
+        c2_payload_list_t * el;
+        LL_FOREACH(value.v_maplist, el) {
+            build_cbor_list(list_item, el->value);
+        }
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = cbor_move(list_item)});
+        break;
+    }
+    case PROP_TYPE: {
+        cbor_item_t * map_item = cbor_new_indefinite_map();
+        properties_t * el, *tmp;
+        HASH_ITER(hh, value.v_props, el, tmp) {
+            cbor_map_add(map_item,
+                         (struct cbor_pair){
+                            .key = cbor_move(cbor_build_string(el->key)),
+                            .value = cbor_move(cbor_build_string(el->value))});
+        }
+        cbor_map_add(cbor_map,
+                     (struct cbor_pair){
+                         .key = cbor_move(cbor_build_uint16(key)),
+                         .value = cbor_move(map_item)});
+        break;
+    }
+    default:
+        break;
+    }
+}
+
+cbor_item_t * cborize_c2_payload(const c2_payload_map_t * c2payload) {
+    if (!c2payload) {
+        return NULL;
+    }
+    c2_payload_map_t * el, *tmp;
+    cbor_item_t * c2_cbor = cbor_new_indefinite_map();
+    HASH_ITER(hh, c2payload, el, tmp) {
+        build_cbor_map(c2_cbor, el->key, el->value);
+    }
+    return c2_cbor;
+}
+
+size_t serialize_payload(const c2_payload_map_t * c2payload, char ** buff, 
size_t * length) {
+    cbor_item_t * head = NULL;
+    if (!c2payload || (head = cborize_c2_payload(c2payload)) == NULL) {
+        *buff = NULL;
+        *length = 0;
+        return 0;
+    }
+    *length = cbor_serialize_alloc(head, buff, length);
+    cbor_decref(&head);
+    return *length;
+}
+
+properties_t * extract_properties(const struct cbor_pair * kvps, size_t sz) {
+    properties_t * props = NULL;
+    int i;
+    for (i = 0; i < sz; ++i) {
+        unsigned char * key_item = cbor_string_handle(kvps[i].key);
+        size_t key_len = cbor_string_length(kvps[i].key);
+        char * key = (char *)malloc(key_len + 1);
+        memset(key, 0, key_len + 1);
+        memcpy(key, key_item, key_len);
+
+        unsigned char * val_item = cbor_string_handle(kvps[i].value);
+        size_t val_len = cbor_string_length(kvps[i].value);
+        char * val = (char *)malloc(val_len + 1);
+        memcpy(val, val_item, val_len);
+        properties_t * prop = (properties_t *)malloc(sizeof(properties_t));
+        prop->key = key;
+        prop->value = val;
+        HASH_ADD_KEYPTR(hh, props, key, key_len, prop);
+    }
+    return props;
+}
+
+value_t load_cbor_items(cbor_item_t * item) {
+    assert(item);
+    if (cbor_isa_uint(item)) {
+        switch (cbor_int_get_width(item)) {
+        case CBOR_INT_8:
+            return value_uint8(cbor_get_uint8(item));
+        case CBOR_INT_16:
+            return value_uint16(cbor_get_uint16(item));
+        case CBOR_INT_32:
+            return value_uint32(cbor_get_uint32(item));
+        case CBOR_INT_64:
+            return value_uint64(cbor_get_uint64(item));
+        default:
+            return value_none();
+        }
+    }
+
+    if (cbor_isa_string(item)) {
+         unsigned char * str = cbor_string_handle(item);
+         size_t sz = cbor_string_length(item);
+         return value_nstring(str, sz);
+    }
+
+    if (cbor_isa_map(item)) {
+        c2_payload_map_t * map = NULL;
+        if (cbor_map_size(item) == 0) {
+            return value_map(map);
+        }
+        struct cbor_pair * kvps = cbor_map_handle(item);
+        size_t sz = cbor_map_size(item);
+        //check if we are parsing properties
 
 Review comment:
   Tagging will help better.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to