You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/01/27 17:33:28 UTC

[GitHub] [nifi-minifi-cpp] msharee9 commented on a change in pull request #674: Minificpp 1007 - ECU C2 integration.

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_r371380715
 
 

 ##########
 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: {
 
 Review comment:
   All keys in the C2 protocol are unsigned ints except the keys of properties which happen to be strings. Use semantic tagging here to identify properties (string key and string value pairs). While decoding a cbor serialized data, tag will help determine if the incoming payload is a regular cbor map or a property.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services