JsonParser.c 11 KB

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