void clear_all_device_info() { int i = 0; for (i = 0; i < MAXDEVICECOUNT; i++) { if (registed_devices[i] != NULL) { printf("Free:%d\n", i); free_device_info(registed_devices[i]); registed_devices[i] = NULL; } } /* IoT_Device_Info *temp = all_registed_device; int num = 0; while (temp != NULL) { num++; printf("Free:%d\n",num); if (temp->nextDevice != NULL) { all_registed_device = (IoT_Device_Info*)temp->nextDevice; free_device_info(temp); temp = all_registed_device; } else { free_device_info(temp); temp = NULL; } } */ }
void add_new_device(IoTIp iotip,JsonData jsonData,int socketIndex) { char *keys[2]; const int temp_buffer_size = 200; int i = 0; IoT_Device_Info *newDevice = (IoT_Device_Info*)malloc(sizeof(IoT_Device_Info));; newDevice->proxied = 0; newDevice->socket_index = socketIndex; keys[0] = "IOTDEV"; //newDevice->ip.ip = (char*)malloc(sizeof(char)*iotip.ipLength+1); memset(newDevice->ip.ip, '\0', IP_ADDR_SIZE); strcpy(newDevice->ip.ip, iotip.ip); char temp_str[temp_buffer_size]; keys[1] = "DeviceID"; get_jsfile_value(keys, 2, jsonData, temp_str); newDevice->devID = (char*)malloc(sizeof(char)*strlen(temp_str) + 1); strcpy(newDevice->devID, temp_str); keys[1] = "FunctionGroup"; get_jsfile_value(keys, 2, jsonData, temp_str); newDevice->funGroup = (char*)malloc(sizeof(char)*strlen(temp_str) + 1); strcpy(newDevice->funGroup, temp_str); newDevice->devDesc = (char*)malloc(sizeof(char)*strlen(jsonData.content) + 1); strcpy(newDevice->devDesc, jsonData.content); //int len1 = strlen(jsonData.content); //int len2 = strlen(newDevice->devDesc); //Check if the device id is already exists //IoT_Device_Info *existsDevice = find_device_with_id(newDevice->devID); int deviceIndex = find_device_index_with_id(newDevice->devID); if (deviceIndex>=0) //if device is already exists { printf("%s is already exists!! update the exists device infomation\n",registed_devices[deviceIndex]->devID); free_device_info(registed_devices[deviceIndex]); registed_devices[deviceIndex] = newDevice; } else //a new device, add into all_registed_device link-list { //attach new device in head for (i = 0; i < MAXDEVICECOUNT; i++) { if (registed_devices[i] == NULL) { registed_devices[i] = newDevice; break; } } } }
void handle_managent_package(IoT_Package *package_info) { //printAllChar(package_info->data, package_info->data_length); const int temp_str_len = 50; int deviceIndex = -1; char str_temp[temp_str_len] = { '\0' }; JsonData jsonData; charcat(jsonData.content, package_info->data, 0, package_info->data_length); init_token(&jsonData); range_t range; range.start = jsonData.tokens[1].start; range.end = jsonData.tokens[1].end; getPatteren(range, jsonData.content, str_temp); //Register if (strcmp("IOTDEV", str_temp) == 0) { puts("A Register package include device info"); add_new_device(package_info->sor_ip, jsonData, package_info->belongSocketIdx); } //Command else if (strcmp("IOTCMD", str_temp) == 0) { puts("A Command package"); IoT_Command cmd = generate_iot_command(); decode_iot_cmd(package_info->data, &cmd); if (cmd.cmd_type != command_t_Management) { puts("IOTCMD has wrong command type"); return; } //if (strcmp(cmd.ID,"Dis_All") == 0 && cmd.cmd_type==command_t_Management) if (memcmp(cmd.ID, "Dis_",4) == 0) { discover_package_replier(package_info,&cmd); } else if (strcmp(cmd.ID, "Del_Dev") == 0) { deviceIndex = find_device_index_with_ipstr(cmd.Value); if (deviceIndex >= 0) { printf("Device[%d]: has bean deleted\n", deviceIndex); free_device_info(registed_devices[deviceIndex]); registed_devices[deviceIndex] = NULL; } } else if (strcmp(cmd.ID, "Prx_Add") == 0) { deviceIndex = find_device_index_with_ipstr(cmd.Value); if (deviceIndex >= 0) { printf("Device[%d]: has bean proxied\n", deviceIndex); registed_devices[deviceIndex]->proxied = 1; } } else if (strcmp(cmd.ID, "Prx_Rmv") == 0) { deviceIndex = find_device_index_with_ipstr(cmd.Value); if (deviceIndex >= 0) { printf("Device[%d]: proxy released\n", deviceIndex); registed_devices[deviceIndex]->proxied = 0; } } else if (strcmp(cmd.ID, "Re_Conf") == 0) { printf("Reload config"); } else { printf("unknow manage command:%s", cmd.ID); } } }
int alloc_and_load_device_info(const char *filename, const char *device, const char *speed) { /* Sets up all delay cache variables from a file. For an example of a REF * file, please look in eda_toolkit.tar.gz, specifically stratix.ref. Returns * PLDM_TRUE for SUCCESS/PLDM_FALSE for ERROR. */ int ilength, iresult; float fversion; char *ctemp, *cdevice; FILE *fp; if (filename == NULL || device == NULL || speed == NULL) { return PLDM_FALSE; } fp = fopen(filename, "r"); if (fp == NULL) { return PLDM_FALSE; } if (g_cache_is_loaded == PLDM_TRUE) { free_device_info(); } ctemp = (char *)l_malloc_array(0, PLDM_BUFFER_LENGTH, sizeof(char)); l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); ilength = (int)strlen(ctemp) - 1; g_delay_cache_version = (char *)l_malloc_array(0, ilength + 1, sizeof(char)); strcpy(g_delay_cache_version, &ctemp[1]); l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); fversion = 0.0f; sscanf(ctemp, "#Version %f", &fversion); /* Check version compability */ if (fversion < PLDM_SUPPORTED_VERSION) { l_free_array(ctemp, 0, sizeof(char)); free_device_info(); return PLDM_FALSE; } /* #<device>-<speed> */ ilength = (int)strlen(device) + (int)strlen(speed) + 2; cdevice = (char *)l_malloc_array(0, ilength + 1, sizeof(char)); sprintf(cdevice, "#%s-%s", device, speed); l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); /* Loop through devices to find correct device */ while (strcmp(cdevice, ctemp) != 0) { if (l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp) == NULL) { l_free_array(cdevice, 0, sizeof(char)); l_free_array(ctemp, 0, sizeof(char)); free_device_info(); return PLDM_FALSE; } } l_free_array(cdevice, 0, sizeof(char)); iresult = PLDM_TRUE; /* Start loading device info */ if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("arch_type", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); ilength = (int)strlen(ctemp); g_arch_type = (char *)l_malloc_array(0, ilength + 1, sizeof(char)); strcpy(g_arch_type, ctemp); } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("sub_arch_type", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); ilength = (int)strlen(ctemp); g_sub_arch_type = (char *)l_malloc_array(0, ilength + 1, sizeof(char)); strcpy(g_sub_arch_type, ctemp); } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("nx", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { iresult = l_load_int(fp, &g_nx); if (iresult && (g_nx < 0 || g_nx > PLDM_MAX_DIMENSION)) { iresult = PLDM_FALSE; } } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("ny", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { iresult = l_load_int(fp, &g_ny); if (iresult && (g_ny < 0 || g_ny > PLDM_MAX_DIMENSION)) { iresult = PLDM_FALSE; } } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("phys_loc", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { g_phys_loc_flags0 = (int **)l_malloc_matrix( 0, g_nx - 1, 0, g_ny - 1, sizeof(int)); iresult = l_load_int_matrix(fp, g_phys_loc_flags0, 0, g_nx - 1, 0, g_ny - 1); } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("cache_wire_delay", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { g_cache_wire_delay = (int ****)l_malloc_matrix4( 0, NUM_END_POINTS - 1, 0, NUM_END_POINTS - 1, 0, g_nx - 1, 0, g_ny - 1, sizeof(float)); iresult = l_load_int_matrix4(fp, g_cache_wire_delay, 0, NUM_END_POINTS - 1, 0, NUM_END_POINTS - 1, 0, g_nx - 1, 0, g_ny - 1); } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("cache_xymap_of_wire_delays", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { g_cache_xymap_of_wire_delays = (int ***)l_malloc_matrix3( 0, NUM_DELAYS_WITH_XY_MAP - 1, 0, g_nx - 1, 0, g_ny - 1, sizeof(int)); iresult = l_load_int_matrix3(fp, g_cache_xymap_of_wire_delays, 0, NUM_DELAYS_WITH_XY_MAP - 1, 0, g_nx - 1, 0, g_ny - 1); } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("cache_precise_average_wire_delays", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { g_cache_precise_average_wire_delays = (int *)l_malloc_array( 0, NUM_CACHED_DELAY_TYPES - 1, sizeof(int)); iresult = l_load_int_array(fp, g_cache_precise_average_wire_delays, 0, NUM_CACHED_DELAY_TYPES - 1); } if (iresult) { l_fgets(ctemp, PLDM_BUFFER_LENGTH, fp); if (strcmp("cache_precise_switch_delays", ctemp) != 0) { iresult = PLDM_FALSE; } } if (iresult) { g_cache_precise_switch_delays = (int **)l_malloc_matrix( 0, NUM_CACHED_DELAY_TYPES - 1, 0, NUM_CACHED_DELAY_TYPES - 1, sizeof(int)); iresult = l_load_int_matrix(fp, g_cache_precise_switch_delays, 0, NUM_CACHED_DELAY_TYPES - 1, 0, NUM_CACHED_DELAY_TYPES - 1); } l_free_array(ctemp, 0, sizeof(char)); fclose(fp); if (iresult) { g_cache_is_loaded = PLDM_TRUE; } else { g_cache_is_loaded = PLDM_FALSE; } return g_cache_is_loaded; }