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