JsonParser.c 11 KB

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