JsonParser.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. //char *str = "[ ]";
  100. if(strcmp((const char *)tempin,"[ ]") == 0)
  101. {
  102. printf("Message is empty arrary. \n");
  103. return;
  104. }
  105. if(tempin[0] != '\0')//if(jobj != NULL)
  106. {
  107. MsgType = tempin[1] - '0';
  108. //printf("MsgType=%d\n",MsgType);
  109. c = 0;
  110. if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
  111. {
  112. return;
  113. }
  114. while ((tempin[4+c] != '\"') && (tempin[4+c] != '\0'))
  115. {
  116. UniqueId[c] = tempin[4+c];
  117. //printf("i=%d UniqueId[c]=%c\n",c, UniqueId[c]);
  118. c++;
  119. }
  120. UniqueId[c] = '\0';
  121. //printf("UniqueId=%s\n",UniqueId);
  122. if(UniqueId[0] == '\0')
  123. {
  124. return;
  125. }
  126. CheckTransactionPacket(UniqueId);
  127. switch (MsgType)
  128. {
  129. case MESSAGE_TYPE_CALL:
  130. //DEBUG_INFO("MESSAGE_TYPE_CALL\n");
  131. c = 0;
  132. while (tempin[4+4+strlen(UniqueId)-1+c] != '\"')
  133. {
  134. Action[c] = tempin[4+4+strlen(UniqueId)-1+c];
  135. //printf("i=%d Action[c]=%c\n",c, Action[c]);
  136. c++;
  137. }
  138. Action[c] = '\0';
  139. //printf("Action=%s\n",Action);
  140. c = 0;
  141. templen= 4+4+3+strlen(UniqueId)-1+strlen(Action)-1;
  142. while ((c+ templen) < (strlen(tempin)-1))
  143. {
  144. Payload[c] = tempin[templen + c];
  145. //printf("i=%d Payload[c]=%c\n",c, Payload[c]);
  146. c++;
  147. }
  148. Payload[c] = '\0';
  149. //printf("Payload=%s\n",Payload);
  150. CallHandler(UniqueId,Action,Payload);
  151. break;
  152. case MESSAGE_TYPE_CALLRESULT:
  153. c = 0;
  154. templen= 4+3+strlen(UniqueId)-1;
  155. while ((c+ templen) < (strlen(tempin)-1))
  156. {
  157. Payload[c] = tempin[templen + c];
  158. //printf("i=%d sstr=%c\n",c, Payload[c]);
  159. c++;
  160. }
  161. Payload[c] = '\0';
  162. //printf("Payload=%s\n",Payload);
  163. 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*/)
  164. {
  165. hashmap_operation(2, UniqueId, key_value);//hashmap_operation(2,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
  166. char * const testdup = strdup(key_value/*mapItem->key_value*/);
  167. //printf("original string: %s (@%p)\n", testdup, testdup);
  168. substr = strtok(testdup, del);
  169. while (substr != NULL) {
  170. arr[count] = substr;
  171. // printf(" arr string: %s (@%p)\n", arr[count], substr);
  172. // printf("#%d sub string: %s (@%p)\n", count++, substr, substr);
  173. count++;
  174. substr = strtok(NULL, del);
  175. }
  176. i=0;
  177. sprintf(Action, "%s", *(arr+i++));
  178. gun_index = atoi(*(arr+i++));
  179. CallResultHandler(Action, Payload, gun_index);
  180. free(testdup);
  181. }
  182. break;
  183. case MESSAGE_TYPE_CALLERROR:
  184. //DEBUG_INFO("MESSAGE_TYPE_CALLERROR\n");
  185. c = 0;
  186. while (tempin[4+4+strlen(UniqueId)-1+c] != '\"')
  187. {
  188. ErrorCode[c] = tempin[4+4+strlen(UniqueId)-1+c] ;
  189. // printf("i=%d sstr=%c\n",c, ErrorCode[c]);
  190. c++;
  191. }
  192. ErrorCode[c] = '\0';
  193. // DEBUG_INFO("ErrorCode=%s\n",ErrorCode);
  194. c = 0;
  195. while (tempin[4+4+4+strlen(UniqueId)-1+strlen(ErrorCode)-1+c] != '\"')
  196. {
  197. ErrorDescription[c] = tempin[4+4+4+strlen(UniqueId)-1+strlen(ErrorCode)-1+c];
  198. //printf("i=%d sstr=%c\n",c, ErrorDescription[c]);
  199. c++;
  200. }
  201. ErrorDescription[c] = '\0';
  202. // DEBUG_INFO("ErrorDescription=%s\n",ErrorDescription);
  203. c = 0;
  204. templen= 4+4+4+3+strlen(UniqueId)-1+strlen(ErrorCode)-1+strlen(ErrorDescription)-1;
  205. while ((c+ templen) < (strlen(tempin)-1))
  206. {
  207. Payload[c] = tempin[templen + c];
  208. // printf("i=%d sstr=%c\n",c, Payload[c]);
  209. c++;
  210. }
  211. Payload[c] = '\0';
  212. // DEBUG_INFO("Payload=%s\n",Payload);
  213. 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*/)
  214. {
  215. hashmap_operation(2, UniqueId, key_value);//hashmap_operation(2,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
  216. sprintf(Action, "%s", key_value/*mapItem->key_value*/);
  217. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. else
  225. {
  226. printf("Message is null. cant parse messgae. \n");
  227. }
  228. }
  229. int CallHandler(char *uuid, char *str1,char *payload)
  230. {
  231. static int CallHandlerNumber = 0;
  232. static int CallHandlerIndex = 0;
  233. int (*callfptr)(char *uuid,char *payload);
  234. //DEBUG_INFO("enter CallHandler\n");
  235. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  236. for(int i= 0; i < CallHandlerNumber ; i ++ )
  237. {
  238. if(strcmp(requestNames[i],str1) == 0)
  239. {
  240. CallHandlerIndex = i ;
  241. break;
  242. }
  243. }
  244. callfptr = NULL;
  245. callfptr = funcalls[CallHandlerIndex];
  246. if(callfptr == NULL)
  247. {
  248. //printf("callfptr is null\n");
  249. }
  250. if ( callfptr )
  251. {
  252. //printf("exec CallHandler ... \n");
  253. callfptr(uuid, payload);
  254. callfptr = NULL;
  255. return PASS;
  256. }
  257. callfptr = NULL;
  258. return FAIL;
  259. }
  260. void CallResultHandler(char *str1, char *payload, int gun_index)
  261. {
  262. static int CallResultHandlerNumber = 0;
  263. static int CallResultHandlerIndex = 0;
  264. void (*callResultfptr)(char *payload, int gun_index );
  265. //printf("enter CallResultHandler\n");
  266. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  267. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  268. {
  269. if(strcmp(responseNames[i],str1) == 0)
  270. {
  271. CallResultHandlerIndex = i ;
  272. break;
  273. }
  274. }
  275. callResultfptr = NULL;
  276. callResultfptr = funs[CallResultHandlerIndex];
  277. if(callResultfptr == NULL)
  278. {
  279. //printf("callResultfptr is null\n");
  280. }
  281. if ( callResultfptr )
  282. {
  283. callResultfptr(payload, gun_index);
  284. }
  285. callResultfptr = NULL;
  286. }
  287. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  288. {
  289. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  290. callErrorfptr = NULL;
  291. callErrorfptr = funcallerror[0];
  292. //printf("CallErrorHandler \n");
  293. if(callErrorfptr == NULL)
  294. {
  295. printf("callErrorfptr is null\n");
  296. }
  297. if ( callErrorfptr )
  298. {
  299. //printf("callErrorfptr is not null\n");
  300. callErrorfptr(id, errorCode, errorDescription, payload);
  301. }
  302. callErrorfptr = NULL;
  303. }