JsonParser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 || 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. if(json_object_is_type(obj, json_type_array))
  181. {
  182. if(json_object_array_length(obj) < 3)
  183. {
  184. DEBUG_WARN("Message parsed array count is less than 3.\n");
  185. return;
  186. }
  187. }
  188. else
  189. {
  190. DEBUG_WARN("Message is not an array type.\n");
  191. return;
  192. }
  193. MsgType = json_object_get_int(json_object_array_get_idx(obj, 0));
  194. sprintf(UniqueId, "%s", json_object_get_string(json_object_array_get_idx(obj, 1)));
  195. if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
  196. {
  197. DEBUG_WARN("Message type not valid.\n");
  198. return;
  199. }
  200. if(UniqueId[0] == '\0')
  201. {
  202. DEBUG_WARN("Message unique id is null.\n");
  203. return;
  204. }
  205. CheckTransactionPacket(UniqueId);
  206. switch (MsgType)
  207. {
  208. case MESSAGE_TYPE_CALL:
  209. sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  210. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 3), JSON_C_TO_STRING_PLAIN));
  211. if(GetServerSign()==TRUE || ((strstr(Action,"TriggerMessage")!=NULL) && (strstr(Payload,"BootNotification")!=NULL)))
  212. CallHandler(UniqueId,Action,Payload);
  213. else
  214. {
  215. char callError[WEBSOCKET_BUFFER_SIZE];
  216. sprintf(callError, "[%d,\"%s\",\"%s\",\"%s\",{}]",MESSAGE_TYPE_CALLERROR, UniqueId, "SecurityError", "Not sign in with BootNotification yet.");
  217. LWS_Send(callError);
  218. }
  219. break;
  220. case MESSAGE_TYPE_CALLRESULT:
  221. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 2), JSON_C_TO_STRING_PLAIN));
  222. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  223. {
  224. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  225. char * const testdup = strdup(key_value);
  226. substr = strtok(testdup, del);
  227. while (substr != NULL)
  228. {
  229. arr[count] = substr;
  230. count++;
  231. substr = strtok(NULL, del);
  232. }
  233. i=0;
  234. sprintf(Action, "%s", *(arr+i++));
  235. gun_index = atoi(*(arr+i++));
  236. CallResultHandler(Action, Payload, gun_index);
  237. free(testdup);
  238. }
  239. break;
  240. case MESSAGE_TYPE_CALLERROR:
  241. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  242. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(obj, 3)));
  243. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 4), JSON_C_TO_STRING_PLAIN));
  244. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  245. {
  246. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  247. sprintf(Action, "%s", key_value);
  248. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  249. }
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. else
  256. {
  257. DEBUG_WARN("Message is invalid JSON format.\n");
  258. }
  259. json_object_put(obj);
  260. }
  261. else
  262. {
  263. DEBUG_WARN("Message is null. can't parse message.\n");
  264. }
  265. }
  266. int CallHandler(char *uuid, char *str1,char *payload)
  267. {
  268. int result = FAIL;
  269. int CallHandlerNumber = 0;
  270. int CallHandlerIndex = (ARRAY_SIZE(requestNames)-1);
  271. int (*callfptr)(char *uuid,char *payload);
  272. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  273. for(int i= 0; i < CallHandlerNumber ; i ++ )
  274. {
  275. if(strcmp(requestNames[i],str1) == 0)
  276. {
  277. CallHandlerIndex = i ;
  278. break;
  279. }
  280. }
  281. callfptr = NULL;
  282. callfptr = funcalls[CallHandlerIndex];
  283. if(callfptr == NULL)
  284. {}
  285. if ( callfptr )
  286. {
  287. callfptr(uuid, payload);
  288. result = PASS;
  289. }
  290. callfptr = NULL;
  291. return result;
  292. }
  293. void CallResultHandler(char *str1, char *payload, int gun_index)
  294. {
  295. static int CallResultHandlerNumber = 0;
  296. static int CallResultHandlerIndex = 0;
  297. void (*callResultfptr)(char *payload, int gun_index );
  298. //printf("enter CallResultHandler\n");
  299. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  300. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  301. {
  302. if(strcmp(responseNames[i],str1) == 0)
  303. {
  304. CallResultHandlerIndex = i ;
  305. break;
  306. }
  307. }
  308. callResultfptr = NULL;
  309. callResultfptr = funs[CallResultHandlerIndex];
  310. if(callResultfptr == NULL)
  311. {
  312. //printf("callResultfptr is null\n");
  313. }
  314. if ( callResultfptr )
  315. {
  316. callResultfptr(payload, gun_index);
  317. }
  318. callResultfptr = NULL;
  319. }
  320. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  321. {
  322. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  323. callErrorfptr = NULL;
  324. callErrorfptr = funcallerror[0];
  325. //printf("CallErrorHandler \n");
  326. if(callErrorfptr == NULL)
  327. {
  328. printf("callErrorfptr is null\n");
  329. }
  330. if ( callErrorfptr )
  331. {
  332. //printf("callErrorfptr is not null\n");
  333. callErrorfptr(id, errorCode, errorDescription, payload);
  334. }
  335. callErrorfptr = NULL;
  336. }