JsonParser.c 12 KB

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