#include #include "cJSON.h" #include #include char *buffer_json; int read_file(const char *name) { FILE *fp; fp = fopen(name, "r"); fseek(fp, 0, SEEK_END); long size = ftell(fp); fseek(fp, 0, SEEK_SET); buffer_json = malloc(size); fread(buffer_json, size, 1, fp); fclose(fp); } int parse_json_file(const char *name , int index, char *vsrcip1 , char *vsrcip2, char *vsrcip3, char *vsrcip4, int index_sink, char *vsinkip1, char *vsinkip2, int * can_port ,int * uart_port ,int *mesh_5g){ read_file(name); cJSON *json =cJSON_Parse(buffer_json); char VSRC[20]; char UART[20]; if(json){ cJSON *json_mesh = cJSON_GetObjectItem(json, "mesh"); cJSON *json_5g = cJSON_GetObjectItem(json, "5G"); *mesh_5g = json_mesh->valueint; if(json_mesh || json_5g ){ if(json_mesh->valueint){ cJSON *json_mesh_ip = cJSON_GetObjectItem(json, "mesh_IP"); if(json_mesh_ip){ cJSON *count = cJSON_GetObjectItem(json_mesh_ip, "vsrc_count"); if( index > count->valueint){ printf("error index > ip_count \n"); return 0; }else{ int n = sprintf(VSRC,"VSRC%d",index); printf("mesh IP%d, %s\n",n,cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),0)->valuestring); strcpy(vsrcip1, cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),0)->valuestring); strcpy(vsrcip2, cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),1)->valuestring); strcpy(vsrcip3, cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),2)->valuestring); strcpy(vsrcip4, cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),3)->valuestring); } count = cJSON_GetObjectItem(json_mesh_ip, "vsink_count"); if( index_sink > count->valueint){ printf("error vsink index > ip_count \n"); return 0; }else{ int n = sprintf(VSRC,"VSINK%d",index_sink); //printf("vsink mesh IP%d, %s\n",n,cJSON_GetObjectItem(json_mesh_ip, VSRC)->valuestring); //strcpy(vsink_ip , cJSON_GetObjectItem(json_mesh_ip, VSRC)->valuestring); strcpy(vsinkip1, cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),0)->valuestring); strcpy(vsinkip2, cJSON_GetArrayItem(cJSON_GetObjectItem(json_mesh_ip, VSRC),1)->valuestring); } } }else if(json_5g->valueint){ cJSON *json_5g_ip = cJSON_GetObjectItem(json, "5G_IP"); if(json_5g_ip){ cJSON *count = cJSON_GetObjectItem(json_5g_ip, "vsrc_count"); if( index > count->valueint){ printf("error index > ip_count \n"); return -1; }else{ int n = sprintf(VSRC,"VSRC%d",index); printf("5G IP%d, %s\n",n,cJSON_GetObjectItem(json_5g_ip, VSRC)->valuestring); strcpy(vsrcip1, cJSON_GetArrayItem(cJSON_GetObjectItem(json_5g_ip, VSRC),0)->valuestring); strcpy(vsrcip2, cJSON_GetArrayItem(cJSON_GetObjectItem(json_5g_ip, VSRC),1)->valuestring); //strcpy(vsrcip1 , cJSON_GetObjectItem(json_5g_ip, VSRC)->valuestring); } count = cJSON_GetObjectItem(json_5g_ip, "vsink_count"); if( index_sink > count->valueint){ printf("error vsink index > ip_count \n"); return 0; }else{ int n = sprintf(VSRC,"VSINK%d",index_sink); //printf("vsink mesh IP%d, %s\n",n,cJSON_GetObjectItem(json_5g_ip, VSRC)->valuestring); strcpy(vsinkip1, cJSON_GetArrayItem(cJSON_GetObjectItem(json_5g_ip, VSRC),0)->valuestring); strcpy(vsinkip2, cJSON_GetArrayItem(cJSON_GetObjectItem(json_5g_ip, VSRC),1)->valuestring); //strcpy(vsink_ip , cJSON_GetObjectItem(json_5g_ip, VSRC)->valuestring); } } } } cJSON *json_can_port = cJSON_GetObjectItem(json, "CANPORT"); if(json_can_port){ cJSON *count = cJSON_GetObjectItem(json_can_port, "count"); #if 1 if( index > count->valueint){ printf("error index > uart_port_count \n"); return -1; }else{ int n = sprintf(UART,"CAN_PORT%d",index); printf("can port %d, %d\n",n,cJSON_GetArrayItem(cJSON_GetObjectItem(json_can_port, UART),0)->valueint); printf("can port %d, %d\n",n,cJSON_GetArrayItem(cJSON_GetObjectItem(json_can_port, UART),1)->valueint); *can_port++ = cJSON_GetArrayItem(cJSON_GetObjectItem(json_can_port, UART),0)->valueint; *can_port++ = cJSON_GetArrayItem(cJSON_GetObjectItem(json_can_port, UART),1)->valueint; } #endif } cJSON *json_uart_port = cJSON_GetObjectItem(json, "UARTPORT"); if(json_uart_port){ printf("uart port %d\n",json_uart_port->valueint); *uart_port = json_uart_port->valueint; #if 0 cJSON *count = cJSON_GetObjectItem(json_uart_port, "count"); if( index > count->valueint){ printf("error index > uart_port_count \n"); return -1; }else{ int n = sprintf(UART,"UART_PORT%d",index); printf("uart port %d, %d\n",n,cJSON_GetArrayItem(cJSON_GetObjectItem(json_uart_port, UART),0)->valueint); printf("uart port %d, %d\n",n,cJSON_GetArrayItem(cJSON_GetObjectItem(json_uart_port, UART),1)->valueint); *port++ = cJSON_GetArrayItem(cJSON_GetObjectItem(json_uart_port, UART),0)->valueint; *port++ = cJSON_GetArrayItem(cJSON_GetObjectItem(json_uart_port, UART),1)->valueint; } #endif } } free(buffer_json); } #if 0 int main(int argc ,char **argv){ char vsrcip1[100]; char vsrcip2[100]; char vsink_ip[100]; int can_port[2]; int uart_port; parse_json_file(argv[1],atoi(argv[2]),vsrcip1,vsrcip2,atoi(argv[3]),vsink_ip, can_port,&uart_port); printf("mesh ip %s %s \n",vsrcip1,vsrcip2); printf("vsinkip %s\n",vsink_ip); printf("can_port %d %d \n",can_port[0],can_port[1]); printf("uart port %d\n",uart_port); } #endif