JsonParser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "Module_OcppBackend20.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. "CertificateSigned",
  12. "ChangeAvailability",
  13. "ClearCache",
  14. "ClearChargingProfile",
  15. "ClearDisplayMessage",
  16. "ClearVariableMonitoring",
  17. "CostUpdated",
  18. "CustomerInformation",
  19. "DataTransfer",
  20. "DeleteCertificate",
  21. "GetBaseReport",
  22. "GetChargingProfiles",
  23. "GetCompositeSchedule",
  24. "GetDisplayMessages",
  25. "GetInstalledCertificateIds",
  26. "GetLocalListVersion",
  27. "GetLog",
  28. "GetMonitoringReport",
  29. "GetReport",
  30. "GetTransactionStatus",
  31. "GetVariables",
  32. "InstallCertificate",
  33. "PublishFirmware",
  34. "RequestStartTransaction",
  35. "RequestStopTransaction",
  36. "ReserveNow",
  37. "Reset",
  38. "SendLocalList",
  39. "SetChargingProfile",
  40. "SetDisplayMessage",
  41. "SetMonitoringBase",
  42. "SetMonitoringLevel",
  43. "SetNetworkProfile",
  44. "SetVariableMonitoring",
  45. "SetVariables",
  46. "TriggerMessage",
  47. "UnlockConnector",
  48. "UnpublishFirmware",
  49. "UpdateFirmware",
  50. "Unknown"};
  51. static char *responseNames[] = {"Authorize",
  52. "BootNotification",
  53. "ClearedChargingLimit",
  54. "DataTransfer",
  55. "FirmwareStatusNotification",
  56. "Get15118EVCertificate",
  57. "GetCertificateStatus",
  58. "Heartbeat",
  59. "LogStatusNotification",
  60. "MeterValues",
  61. "NotifyChargingLimit",
  62. "NotifyCustomerInformation",
  63. "NotifyDisplayMessages",
  64. "NotifyEVChargingNeeds",
  65. "NotifyEVChargingSchedule",
  66. "NotifyEvent",
  67. "NotifyMonitoringReport",
  68. "NotifyReport",
  69. "PublishFirmwareStatusNotification",
  70. "ReportChargingProfiles",
  71. "ReservationStatusUpdate",
  72. "SecurityEventNotification",
  73. "SignCertificate",
  74. "StatusNotification",
  75. "TransactionEvent" };
  76. static FunPtr funs[] = {handleAuthorizeResponse,
  77. handleBootNotificationResponse,
  78. handleClearedChargingLimitResponse,
  79. handleDataTransferResponse,
  80. handleFirmwareStatusNotificationResponse,
  81. handleGet15118EVCertificateResponse,
  82. handleGetCertificateStatusResponse,
  83. handleHeartbeatResponse,
  84. handleLogStatusNotificationResponse,
  85. handleMeterValuesResponse,
  86. handleNotifyChargingLimitResponse,
  87. handleNotifyCustomerInformationResponse,
  88. handleNotifyDisplayMessagesResponse,
  89. handleNotifyEVChargingNeedsResponse,
  90. handleNotifyEVChargingScheduleResponse,
  91. handleNotifyEventResponse,
  92. handleNotifyMonitoringReportResponse,
  93. handleNotifyReportResponse,
  94. handlePublishFirmwareStatusNotificationResponse,
  95. handleReportChargingProfilesResponse,
  96. handleReservationStatusUpdateResponse,
  97. handleSecurityEventNotificationResponse,
  98. handleSignCertificateResponse,
  99. handleStatusNotificationResponse,
  100. handleTransactionEventResponse};
  101. static FunCallPtr funcalls[] = {handleCancelReservationRequest,
  102. handleCertificateSignedRequest,
  103. handleChangeAvailabilityRequest,
  104. handleClearCacheRequest,
  105. handleClearChargingProfileRequest,
  106. handleClearDisplayMessageRequest,
  107. handleClearVariableMonitoringRequest,
  108. handleCostUpdatedRequest,
  109. handleCustomerInformationRequest,
  110. handleDataTransferRequest,
  111. handleDeleteCertificateRequest,
  112. handleGetBaseReportRequest,
  113. handleGetChargingProfilesRequest,
  114. handleGetCompositeScheduleRequest,
  115. handleGetDisplayMessagesRequest,
  116. handleGetInstalledCertificateIdsRequest,
  117. handleGetLocalListVersionRequest,
  118. handleGetLogRequest,
  119. handleGetMonitoringReportRequest,
  120. handleGetReportRequest,
  121. handleGetTransactionStatusRequest,
  122. handleGetVariablesRequest,
  123. handleInstallCertificateRequest,
  124. handlePublishFirmwareRequest,
  125. handleRequestStartTransactionRequest,
  126. handleRequestStopTransactionRequest,
  127. handleReserveNowRequest,
  128. handleResetRequest,
  129. handleSendLocalListRequest,
  130. handleSetChargingProfileRequest,
  131. handleSetDisplayMessageRequest,
  132. handleSetMonitoringBaseRequest,
  133. handleSetMonitoringLevelRequest,
  134. handleSetNetworkProfileRequest,
  135. handleSetVariableMonitoringRequest,
  136. handleSetVariablesRequest,
  137. handleTriggerMessageRequest,
  138. handleUnlockConnectorRequest,
  139. handleUnpublishFirmwareRequest,
  140. handleUpdateFirmwareRequest,
  141. handleUnknownRequest};
  142. static FunCallErrorPtr funcallerror[] = { handleError };
  143. //==========================================
  144. // Receive Message routine
  145. //==========================================
  146. void ReceivedMessage(void *in, size_t len)
  147. {
  148. //DEBUG_INFO("ReceivedMessage\n");
  149. char tempin[WEBSOCKET_BUFFER_SIZE];
  150. int MsgType = 0;
  151. char UniqueId[37],Action[33],Payload[WEBSOCKET_BUFFER_SIZE-712],ErrorCode[129],ErrorDescription[513];
  152. char *arr[2]= {};
  153. int gun_index = 0;
  154. const char *del = ",";
  155. char *substr = NULL;
  156. int count = 0;
  157. int i = 0;
  158. char key_value[VALUE_MAX_LENGTH];
  159. //parsing received message and do something
  160. memset(key_value, 0, sizeof key_value);
  161. memset(UniqueId, 0, sizeof UniqueId);
  162. memset(Action, 0, sizeof Action);
  163. memset(Payload, 0, sizeof Payload);
  164. memset(ErrorCode, 0, sizeof ErrorCode);
  165. memset(ErrorDescription, 0, sizeof ErrorDescription);
  166. memset(tempin, 0, sizeof tempin);
  167. strcpy(tempin, (const char *)in);
  168. memset( (void *)in, 0, sizeof(char)*len );
  169. if(tempin[0] != '\0')
  170. {
  171. if(strcmp((const char *)tempin,"[ ]") == 0)
  172. {
  173. DEBUG_WARN("Message is empty array.\n");
  174. return;
  175. }
  176. json_object *obj = NULL;
  177. obj = json_tokener_parse(tempin);
  178. if(!is_error(obj))
  179. {
  180. MsgType = json_object_get_int(json_object_array_get_idx(obj, 0));
  181. sprintf(UniqueId, "%s", json_object_get_string(json_object_array_get_idx(obj, 1)));
  182. if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
  183. {
  184. DEBUG_WARN("Message type not valid.\n");
  185. return;
  186. }
  187. if(UniqueId[0] == '\0')
  188. {
  189. DEBUG_WARN("Message unique id is null.\n");
  190. return;
  191. }
  192. CheckTransactionPacket(UniqueId);
  193. switch (MsgType)
  194. {
  195. case MESSAGE_TYPE_CALL:
  196. sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  197. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 3), JSON_C_TO_STRING_PLAIN));
  198. if(GetServerSign()==TRUE || ((strstr(Action,"TriggerMessage")!=NULL) && (strstr(Payload,"BootNotification")!=NULL)))
  199. CallHandler(UniqueId,Action,Payload);
  200. else
  201. {
  202. char callError[WEBSOCKET_BUFFER_SIZE];
  203. sprintf(callError, "[%d,\"%s\",\"%s\",\"%s\",{}]",MESSAGE_TYPE_CALLERROR, UniqueId, "SecurityError", "Not sign in with BootNotification yet.");
  204. LWS_Send(callError);
  205. }
  206. break;
  207. case MESSAGE_TYPE_CALLRESULT:
  208. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 2), JSON_C_TO_STRING_PLAIN));
  209. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  210. {
  211. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  212. char * const testdup = strdup(key_value);
  213. substr = strtok(testdup, del);
  214. while (substr != NULL)
  215. {
  216. arr[count] = substr;
  217. count++;
  218. substr = strtok(NULL, del);
  219. }
  220. i=0;
  221. sprintf(Action, "%s", *(arr+i++));
  222. gun_index = atoi(*(arr+i++));
  223. CallResultHandler(Action, Payload, gun_index);
  224. free(testdup);
  225. }
  226. break;
  227. case MESSAGE_TYPE_CALLERROR:
  228. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  229. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(obj, 3)));
  230. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 4), JSON_C_TO_STRING_PLAIN));
  231. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  232. {
  233. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  234. sprintf(Action, "%s", key_value);
  235. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. else
  243. {
  244. DEBUG_WARN("Message is invalid JSON format.\n");
  245. }
  246. json_object_put(obj);
  247. }
  248. else
  249. {
  250. DEBUG_WARN("Message is null. can't parse message.\n");
  251. }
  252. }
  253. int CallHandler(char *uuid, char *str1,char *payload)
  254. {
  255. int result = FAIL;
  256. int CallHandlerNumber = 0;
  257. int CallHandlerIndex = (ARRAY_SIZE(requestNames)-1);
  258. int (*callfptr)(char *uuid,char *payload);
  259. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  260. for(int i= 0; i < CallHandlerNumber ; i ++ )
  261. {
  262. if(strcmp(requestNames[i],str1) == 0)
  263. {
  264. CallHandlerIndex = i ;
  265. break;
  266. }
  267. }
  268. callfptr = NULL;
  269. callfptr = funcalls[CallHandlerIndex];
  270. if(callfptr == NULL)
  271. {}
  272. if ( callfptr )
  273. {
  274. callfptr(uuid, payload);
  275. result = PASS;
  276. }
  277. callfptr = NULL;
  278. return result;
  279. }
  280. void CallResultHandler(char *str1, char *payload, int gun_index)
  281. {
  282. static int CallResultHandlerNumber = 0;
  283. static int CallResultHandlerIndex = 0;
  284. void (*callResultfptr)(char *payload, int gun_index );
  285. //printf("enter CallResultHandler\n");
  286. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  287. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  288. {
  289. if(strcmp(responseNames[i],str1) == 0)
  290. {
  291. CallResultHandlerIndex = i ;
  292. break;
  293. }
  294. }
  295. callResultfptr = NULL;
  296. callResultfptr = funs[CallResultHandlerIndex];
  297. if(callResultfptr == NULL)
  298. {
  299. //printf("callResultfptr is null\n");
  300. }
  301. if ( callResultfptr )
  302. {
  303. callResultfptr(payload, gun_index);
  304. }
  305. callResultfptr = NULL;
  306. }
  307. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  308. {
  309. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  310. callErrorfptr = NULL;
  311. callErrorfptr = funcallerror[0];
  312. //printf("CallErrorHandler \n");
  313. if(callErrorfptr == NULL)
  314. {
  315. printf("callErrorfptr is null\n");
  316. }
  317. if ( callErrorfptr )
  318. {
  319. //printf("callErrorfptr is not null\n");
  320. callErrorfptr(id, errorCode, errorDescription, payload);
  321. }
  322. callErrorfptr = NULL;
  323. }