JsonParser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "Module_OcppBackend.h"
  2. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload);
  3. int CallHandler(char *uuid, char *str1,char *payload);
  4. void CallResultHandler(char *str1,char *payload, int gun_index);
  5. extern void CheckTransactionPacket(char *uuid);
  6. typedef void (*FunCallErrorPtr)(char *id, char *errorCode, char *errorDescription,char *payload);
  7. typedef void (*FunPtr)(char *payload, int gun_index);
  8. typedef int (*FunCallPtr)(char *uuid, char *payload);
  9. typedef enum boolean { FALSE, TRUE } BOOL;
  10. static char *requestNames[] = { "CancelReservation",
  11. "ChangeAvailability",
  12. "ChangeConfiguration",
  13. "ClearCache",
  14. "ClearChargingProfile",
  15. "DataTransfer",
  16. "GetCompositeSchedule",
  17. "GetConfiguration",
  18. "GetDiagnostics",
  19. "GetLocalListVersion",
  20. "RemoteStartTransaction",
  21. "RemoteStopTransaction",
  22. "ReserveNow",
  23. "Reset",
  24. "SendLocalList",
  25. "SetChargingProfile",
  26. "TriggerMessage",
  27. "UnlockConnector",
  28. "UpdateFirmware" };
  29. static char *responseNames[] = {"Authorize",
  30. "BootNotification",
  31. "DataTransfer",
  32. "DiagnosticsStatusNotification",
  33. "FirmwareStatusNotification",
  34. "Heartbeat",
  35. "MeterValues",
  36. "StartTransaction",
  37. "StatusNotification",
  38. "StopTransaction" };
  39. static FunPtr funs[] = {handleAuthorizeResponse,
  40. handleBootNotificationResponse,
  41. handleDataTransferResponse,
  42. handleDiagnosticsStatusNotificationResponse,
  43. handleFirmwareStatusNotificationResponse,
  44. handleHeartbeatResponse,
  45. handleMeterValuesResponse,
  46. handleStartTransactionResponse,
  47. handleStatusNotificationResponse,
  48. handleStopTransactionnResponse };
  49. static FunCallPtr funcalls[] = {handleCancelReservationRequest,
  50. handleChangeAvailabilityRequest,
  51. handleChangeConfigurationRequest,
  52. handleClearCacheRequest,
  53. handleClearChargingProfileRequest,
  54. handleDataTransferRequest,
  55. handleGetCompositeScheduleRequest,
  56. handleGetConfigurationRequest,
  57. handleGetDiagnosticsRequest,
  58. handleGetLocalListVersionRequest,
  59. handleRemoteStartRequest,
  60. handleRemoteStopTransactionRequest,
  61. handleReserveNowTransactionRequest,
  62. handleResetRequest,
  63. handleSendLocalListRequest,
  64. handleSetChargingProfileRequest,
  65. handleTriggerMessageRequest,
  66. handleUnlockConnectorRequest,
  67. handleUpdateFirmwareRequest };
  68. static FunCallErrorPtr funcallerror[] = { handleError };
  69. //==========================================
  70. // Receive Message routine
  71. //==========================================
  72. void ReceivedMessage(void *in, size_t len)
  73. {
  74. printf("ReceivedMessage\n");
  75. //char tempin[1024*4];
  76. char tempin[65536];
  77. int MsgType = 0;
  78. //char UniqueId[37],Action[33],Payload[10241],ErrorCode[129],ErrorDescription[513];
  79. char UniqueId[37],Action[33],Payload[64824],ErrorCode[129],ErrorDescription[513];
  80. char *arr[2]= {};
  81. int gun_index = 0;
  82. const char *del = ",";
  83. char *substr = NULL;
  84. int count = 0;
  85. int i = 0;
  86. //int c = 0;
  87. //int templen = 0;
  88. char key_value[VALUE_MAX_LENGTH];
  89. //parsing received message and do something
  90. memset(key_value, 0, sizeof key_value);
  91. memset(UniqueId, 0, sizeof UniqueId);
  92. memset(Action, 0, sizeof Action);
  93. memset(Payload, 0, sizeof Payload);
  94. memset(ErrorCode, 0, sizeof ErrorCode);
  95. memset(ErrorDescription, 0, sizeof ErrorDescription);
  96. memset(tempin, 0, 1024*4);
  97. strcpy(tempin, (const char *)in);
  98. memset( (void *)in, 0, sizeof(char)*len );
  99. MsgType = json_object_get_int(json_object_array_get_idx(json_tokener_parse(tempin), 0));
  100. sprintf(UniqueId, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 1)));
  101. //char *str = "[ ]";
  102. if(strcmp((const char *)tempin,"[ ]") == 0)
  103. {
  104. printf("Message is empty arrary. \n");
  105. return;
  106. }
  107. if(tempin[0] != '\0')//if(jobj != NULL)
  108. {
  109. //MsgType = tempin[1] - '0';
  110. //printf("MsgType=%d\n",MsgType);
  111. //c = 0;
  112. if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
  113. {
  114. return;
  115. }
  116. /*
  117. while ((tempin[4+c] != '\"') && (tempin[4+c] != '\0'))
  118. {
  119. UniqueId[c] = tempin[4+c];
  120. //printf("i=%d UniqueId[c]=%c\n",c, UniqueId[c]);
  121. c++;
  122. }
  123. UniqueId[c] = '\0';*/
  124. //printf("UniqueId=%s\n",UniqueId);
  125. if(UniqueId[0] == '\0')
  126. {
  127. return;
  128. }
  129. CheckTransactionPacket(UniqueId);
  130. switch (MsgType)
  131. {
  132. case MESSAGE_TYPE_CALL:
  133. //DEBUG_INFO("MESSAGE_TYPE_CALL\n");
  134. /*
  135. c = 0;
  136. while (tempin[4+4+strlen(UniqueId)-1+c] != '\"')
  137. {
  138. Action[c] = tempin[4+4+strlen(UniqueId)-1+c];
  139. //printf("i=%d Action[c]=%c\n",c, Action[c]);
  140. c++;
  141. }
  142. Action[c] = '\0';*/
  143. sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 2)));
  144. //printf("Action=%s\n",Action);
  145. /*
  146. c = 0;
  147. templen= 4+4+3+strlen(UniqueId)-1+strlen(Action)-1;
  148. while ((c+ templen) < (strlen(tempin)-1))
  149. {
  150. Payload[c] = tempin[templen + c];
  151. //printf("i=%d Payload[c]=%c\n",c, Payload[c]);
  152. c++;
  153. }
  154. Payload[c] = '\0';*/
  155. //printf("Payload=%s\n",Payload);
  156. sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 3)));
  157. CallHandler(UniqueId,Action,Payload);
  158. break;
  159. case MESSAGE_TYPE_CALLRESULT:
  160. /*
  161. c = 0;
  162. templen= 4+3+strlen(UniqueId)-1;
  163. while ((c+ templen) < (strlen(tempin)-1))
  164. {
  165. Payload[c] = tempin[templen + c];
  166. //printf("i=%d sstr=%c\n",c, Payload[c]);
  167. c++;
  168. }
  169. Payload[c] = '\0';*/
  170. sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 2)));
  171. //printf("Payload=%s\n",Payload);
  172. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)//if(hashmap_operation(1,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
  173. {
  174. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);//hashmap_operation(2,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
  175. char * const testdup = strdup(key_value/*mapItem->key_value*/);
  176. //printf("original string: %s (@%p)\n", testdup, testdup);
  177. substr = strtok(testdup, del);
  178. while (substr != NULL) {
  179. arr[count] = substr;
  180. // printf(" arr string: %s (@%p)\n", arr[count], substr);
  181. // printf("#%d sub string: %s (@%p)\n", count++, substr, substr);
  182. count++;
  183. substr = strtok(NULL, del);
  184. }
  185. i=0;
  186. sprintf(Action, "%s", *(arr+i++));
  187. gun_index = atoi(*(arr+i++));
  188. CallResultHandler(Action, Payload, gun_index);
  189. free(testdup);
  190. }
  191. break;
  192. case MESSAGE_TYPE_CALLERROR:
  193. //DEBUG_INFO("MESSAGE_TYPE_CALLERROR\n");
  194. /*
  195. c = 0;
  196. while (tempin[4+4+strlen(UniqueId)-1+c] != '\"')
  197. {
  198. ErrorCode[c] = tempin[4+4+strlen(UniqueId)-1+c] ;
  199. // printf("i=%d sstr=%c\n",c, ErrorCode[c]);
  200. c++;
  201. }
  202. ErrorCode[c] = '\0';*/
  203. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 2)));
  204. // DEBUG_INFO("ErrorCode=%s\n",ErrorCode);
  205. /*
  206. c = 0;
  207. while (tempin[4+4+4+strlen(UniqueId)-1+strlen(ErrorCode)-1+c] != '\"')
  208. {
  209. ErrorDescription[c] = tempin[4+4+4+strlen(UniqueId)-1+strlen(ErrorCode)-1+c];
  210. //printf("i=%d sstr=%c\n",c, ErrorDescription[c]);
  211. c++;
  212. }
  213. ErrorDescription[c] = '\0';*/
  214. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 3)));
  215. // DEBUG_INFO("ErrorDescription=%s\n",ErrorDescription);
  216. /*
  217. c = 0;
  218. templen= 4+4+4+3+strlen(UniqueId)-1+strlen(ErrorCode)-1+strlen(ErrorDescription)-1;
  219. while ((c+ templen) < (strlen(tempin)-1))
  220. {
  221. Payload[c] = tempin[templen + c];
  222. // printf("i=%d sstr=%c\n",c, Payload[c]);
  223. c++;
  224. }
  225. Payload[c] = '\0';*/
  226. sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(json_tokener_parse(tempin), 4)));
  227. // DEBUG_INFO("Payload=%s\n",Payload);
  228. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)//if(hashmap_operation(1,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
  229. {
  230. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);//hashmap_operation(2,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
  231. sprintf(Action, "%s", key_value/*mapItem->key_value*/);
  232. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  233. }
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. else
  240. {
  241. printf("Message is null. cant parse messgae. \n");
  242. }
  243. }
  244. int CallHandler(char *uuid, char *str1,char *payload)
  245. {
  246. static int CallHandlerNumber = 0;
  247. static int CallHandlerIndex = 0;
  248. int (*callfptr)(char *uuid,char *payload);
  249. //DEBUG_INFO("enter CallHandler\n");
  250. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  251. for(int i= 0; i < CallHandlerNumber ; i ++ )
  252. {
  253. if(strcmp(requestNames[i],str1) == 0)
  254. {
  255. CallHandlerIndex = i ;
  256. break;
  257. }
  258. }
  259. callfptr = NULL;
  260. callfptr = funcalls[CallHandlerIndex];
  261. if(callfptr == NULL)
  262. {
  263. //printf("callfptr is null\n");
  264. }
  265. if ( callfptr )
  266. {
  267. //printf("exec CallHandler ... \n");
  268. callfptr(uuid, payload);
  269. callfptr = NULL;
  270. return PASS;
  271. }
  272. callfptr = NULL;
  273. return FAIL;
  274. }
  275. void CallResultHandler(char *str1, char *payload, int gun_index)
  276. {
  277. static int CallResultHandlerNumber = 0;
  278. static int CallResultHandlerIndex = 0;
  279. void (*callResultfptr)(char *payload, int gun_index );
  280. //printf("enter CallResultHandler\n");
  281. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  282. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  283. {
  284. if(strcmp(responseNames[i],str1) == 0)
  285. {
  286. CallResultHandlerIndex = i ;
  287. break;
  288. }
  289. }
  290. callResultfptr = NULL;
  291. callResultfptr = funs[CallResultHandlerIndex];
  292. if(callResultfptr == NULL)
  293. {
  294. //printf("callResultfptr is null\n");
  295. }
  296. if ( callResultfptr )
  297. {
  298. callResultfptr(payload, gun_index);
  299. }
  300. callResultfptr = NULL;
  301. }
  302. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  303. {
  304. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  305. callErrorfptr = NULL;
  306. callErrorfptr = funcallerror[0];
  307. //printf("CallErrorHandler \n");
  308. if(callErrorfptr == NULL)
  309. {
  310. printf("callErrorfptr is null\n");
  311. }
  312. if ( callErrorfptr )
  313. {
  314. //printf("callErrorfptr is not null\n");
  315. callErrorfptr(id, errorCode, errorDescription, payload);
  316. }
  317. callErrorfptr = NULL;
  318. }