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_r353996883
 
 

 ##########
 File path: nanofi/src/coap/c2agent.c
 ##########
 @@ -0,0 +1,481 @@
+/**
+ *
+ * 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.
+ */
+
+#include "coap/c2structs.h"
+#include "coap/c2agent.h"
+#include "coap/c2protocol.h"
+#include "coap/coapprotocol.h"
+
+#include "nanofi/coap_message.h"
+
+#include <time.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <sys/socket.h>
+#include <sys/utsname.h>
+#include <sys/errno.h>
+
+#include "utlist.h"
+#include <uuid/uuid.h>
+
+#if defined(__APPLE__) && defined(__MACH__)
+#define MAC
+#include <sys/sysctl.h>
+#else
+#include <sys/sysinfo.h>
+#endif
+
+uint64_t get_physical_memory_size() {
+#ifdef MAC
+    uint64_t memsize = 0;
+    size_t len = sizeof(memsize);
+    if (sysctlbyname("hw.memsize", &memsize, &len, NULL, 0) == 0) {
+        return memsize;
+    }
+#else
+    struct sysinfo sys_info;
+    if (sysinfo(&sys_info) == 0) {
+        return sys_info.totalram;
+    }
+#endif
+    return 0;
+}
+
+uint16_t get_num_vcores() {
+#ifdef MAC
+    uint16_t num_cpus = 0;
+    size_t len = sizeof(num_cpus);
+    if (sysctlbyname("hw.logicalcpu", &num_cpus, &len, NULL, 0) == 0) {
+        return num_cpus;
+    }
+    return 0;
+#else
+    long ncpus = sysconf(_SC_NPROCESSORS_ONLN);
+    if (ncpus < 0) {
+        return 0;
+    }
+    return (uint16_t)ncpus;
+#endif
+}
+
+char * get_ipaddr_from_hostname(const char * hostname) {
+    struct addrinfo hints, *infoptr;
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = AF_UNSPEC;
+
+    int result = getaddrinfo(hostname, NULL, &hints, &infoptr);
+    if (result) {
+        printf("error getting addr info getaddrinfo: %s\n", 
gai_strerror(result));
+        return NULL;
+    }
+
+    size_t len = 45;
+    char * host = (char *)malloc((len+1) * sizeof(char));
+    struct addrinfo *p;
+    for (p = infoptr; p != NULL; p = p->ai_next) {
+        if (getnameinfo(p->ai_addr, p->ai_addrlen, host, len, NULL, 0, 
NI_NUMERICHOST) == 0) {
+            freeaddrinfo(infoptr);
+            return host;
+        }
+    }
+
+    freeaddrinfo(infoptr);
+    free(host);
+    return NULL;
+}
+
+int get_machine_architecture(char * buff) {
+    if (!buff) {
+        return -1;
+    }
+    struct utsname name;
+    if (uname(&name) == 0) {
+        strcpy(buff, name.machine);
+        return 0;
+    }
+    return -1;
+}
+
+c2heartbeat_t prepare_c2_heartbeat(const char * agent_id) {
+    c2heartbeat_t c2_heartbeat;
+    memset(&c2_heartbeat, 0, sizeof(c2heartbeat_t));
+
+    if (!agent_id) {
+        c2_heartbeat.is_error = 1;
+        return c2_heartbeat;
+    }
+
+    strcpy(c2_heartbeat.agent_info.ident, agent_id);
+    c2_heartbeat.agent_info.agent_class = (char *)malloc((strlen("default") + 
1) * sizeof(char));
+    strcpy(c2_heartbeat.agent_info.agent_class, "default");
+    c2_heartbeat.agent_info.uptime = time(NULL); //TODO correct me
+
+    //device_info
+    c2_heartbeat.device_info.ident = (char *)malloc((strlen("identifier") + 1) 
* sizeof(char));
+    strcpy(c2_heartbeat.device_info.ident, "identifier");
+    c2_heartbeat.device_info.network_info.device_id = (char 
*)malloc((strlen("device_id") + 1) * sizeof(char));
+    strcpy(c2_heartbeat.device_info.network_info.device_id, "device_id");
+
+    //device_info.network_info
+    size_t len = sizeof(c2_heartbeat.device_info.network_info.host_name);
+    memset(c2_heartbeat.device_info.network_info.host_name, 0, len);
+    if (gethostname(c2_heartbeat.device_info.network_info.host_name, len) < 0) 
{
+        strcpy(c2_heartbeat.device_info.network_info.host_name, "unknown");
+    }
+    char * ipaddr = 
get_ipaddr_from_hostname(c2_heartbeat.device_info.network_info.host_name);
+    if (ipaddr) {
+        strcpy(c2_heartbeat.device_info.network_info.ip_address, ipaddr);
+        free(ipaddr);
+    } else {
+        strcpy(c2_heartbeat.device_info.network_info.ip_address, "unknown");
+    }
+
+    //device_info.system_info
+    memset(c2_heartbeat.device_info.system_info.machine_arch, 0, 
sizeof(c2_heartbeat.device_info.system_info.machine_arch));
+    if 
(get_machine_architecture(c2_heartbeat.device_info.system_info.machine_arch) < 
0) {
+        strcpy(c2_heartbeat.device_info.system_info.machine_arch, "-");
+    }
+    c2_heartbeat.device_info.system_info.physical_mem = 
get_physical_memory_size();
+    c2_heartbeat.device_info.system_info.v_cores = get_num_vcores();
+
+    return c2_heartbeat;
+}
+
+void prepare_agent_manifest(c2context_t * c2_ctx, c2heartbeat_t * hb) {
+    size_t num_ecus = 0;
+    num_ecus = HASH_COUNT(c2_ctx->ecus);
+    hb->has_ag_manifest = 1;
+    agent_manifest ag_manifest;
+    memset(&ag_manifest, 0, sizeof(agent_manifest));
+
+    UUID_FIELD uuid;
+    uuid_generate(uuid);
+    uuid_unparse_lower(uuid, ag_manifest.manifest_id);
+    strcpy(ag_manifest.agent_type, "nanofi-c");
+    strcpy(ag_manifest.version, "0.0.1");
+
+    ag_manifest.num_ecus = num_ecus;
+    ag_manifest.ecus = (ecuinfo *)malloc(num_ecus * sizeof(ecuinfo));
+    memset(ag_manifest.ecus, 0, (num_ecus * sizeof(ecuinfo)));
+
+    ecu_entry_t * el, *tmp;
+    int i = 0;
+    HASH_ITER(hh, c2_ctx->ecus, el, tmp) {
+        strcpy(ag_manifest.ecus[i].uuid, el->uuid);
+        ag_manifest.ecus[i].name = (const char *)malloc(strlen(el->ecu->name) 
+ 1);
+        strcpy((char *)ag_manifest.ecus[i].name, el->ecu->name);
+        char * input;
+        get_input_name(el->ecu, &input);
+        if (input && strlen(input) > 0) {
+            size_t ip_len = strlen(input);
+            ag_manifest.ecus[i].input = (const char *)malloc(ip_len + 1);
+            strcpy((char *)ag_manifest.ecus[i].input, input);
+            free(input);
+        }
+        ag_manifest.ecus[i].ip_args = get_input_args(el->ecu);
+
+        char * output;
+        get_output_name(el->ecu, &output);
+        if (output && strlen(output) > 0) {
+            size_t op_len = strlen(output);
+            ag_manifest.ecus[i].output = (const char *)malloc(op_len + 1);
+            strcpy((char *)ag_manifest.ecus[i].output, output);
+            free(output);
+        }
+        ag_manifest.ecus[i].op_args = get_output_args(el->ecu);
+    }
+    hb->ag_manifest = ag_manifest;
+    hb->ag_manifest.io = get_io_manifest();
+}
+
+c2_response_t * prepare_c2_response(const char * operation_id) {
+    if (!operation_id || strlen(operation_id) == 0) {
+        return NULL;
+    }
+    c2_response_t * c2_resp = (c2_response_t *)malloc(sizeof(c2_response_t));
+    size_t len = strlen(operation_id);
+    c2_resp->ident = (char *)malloc(len + 1);
+    strcpy(c2_resp->ident, operation_id);
+    c2_resp->operation = ACKNOWLEDGE;
+    return c2_resp;
+}
+
+typedef struct {
+    char * ip_name;
+    char * op_name;
+    properties_t * ip_props;
+    properties_t * op_props;
+} io_properties;
+
+char ** parse_tokens(const char * str, size_t len, size_t num_tokens, const 
char * sep) {
+    char * arg = (char *)malloc(len + 1);
+    strcpy(arg, str);
+    char * tok = strtok(arg, sep);
 
 Review comment:
   ok.

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


With regards,
Apache Git Services

Reply via email to