JsonParser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. "RemoteStartTransaction",
  35. "RemoteStopTransaction",
  36. "ReserveNow",
  37. "Reset",
  38. "SendLocalList",
  39. "SetChargingProfile",
  40. "SetDisplayMessages",
  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. "TransactionEven" };
  76. static FunPtr funs[] = {handleAuthorizeResponse,
  77. handleBootNotificationResponse,
  78. handleClearedChargingLimitResponse,
  79. handleDataTransferResponse,
  80. handleFirmwareStatusNotificationResponse,
  81. handleGet15118EVCertificateResponse,
  82. hanldeGetCertificateStatusResponse,
  83. handleHeartbeatResponse,
  84. hanldeLogStatusNotificationResponse,
  85. handleMeterValuesResponse,
  86. handleNotifyChargingLimitResponse,
  87. hanldeNotifyCustomerInformationResponse,
  88. hanldeNotifyDisplayMessagesResponse,
  89. hanldeNotifyEVChargingNeedsResponse,
  90. hanldeNotifyEVChargingScheduleResponse,
  91. hanldeNotifyEventResponse,
  92. hanldeNotifyMonitoringReportResponse,
  93. hanldeNotifyReportResponse,
  94. hanldePublishFirmwareStatusNotificationResponse,
  95. hanldeReportChargingProfilesResponse,
  96. hanldeReservationStatusUpdateResponse,
  97. hanldeSecurityEventNotificationResponse,
  98. hanldeSignCertificateResponse,
  99. handleStatusNotificationResponse,
  100. hanldeTransactionEvenResponse};
  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. handleRemoteStartTransactionRequest,
  126. handleRemoteStopTransactionRequest,
  127. handleReserveNowRequest,
  128. handleResetRequest,
  129. handleSendLocalListRequest,
  130. handleSetChargingProfileRequest,
  131. handleSetDisplayMessagesRequest,
  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[65536];
  150. int MsgType = 0;
  151. char UniqueId[37],Action[33],Payload[64824],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, 1024*4);
  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. CallHandler(UniqueId,Action,Payload);
  199. break;
  200. case MESSAGE_TYPE_CALLRESULT:
  201. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 2), JSON_C_TO_STRING_PLAIN));
  202. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  203. {
  204. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  205. char * const testdup = strdup(key_value);
  206. substr = strtok(testdup, del);
  207. while (substr != NULL)
  208. {
  209. arr[count] = substr;
  210. count++;
  211. substr = strtok(NULL, del);
  212. }
  213. i=0;
  214. sprintf(Action, "%s", *(arr+i++));
  215. gun_index = atoi(*(arr+i++));
  216. CallResultHandler(Action, Payload, gun_index);
  217. free(testdup);
  218. }
  219. break;
  220. case MESSAGE_TYPE_CALLERROR:
  221. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  222. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(obj, 3)));
  223. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 4), 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. sprintf(Action, "%s", key_value);
  228. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. else
  236. {
  237. DEBUG_WARN("Message is invalid JSON format.\n");
  238. }
  239. json_object_put(obj);
  240. }
  241. else
  242. {
  243. DEBUG_WARN("Message is null. can't parse message.\n");
  244. }
  245. }
  246. int CallHandler(char *uuid, char *str1,char *payload)
  247. {
  248. int result = FAIL;
  249. int CallHandlerNumber = 0;
  250. int CallHandlerIndex = (ARRAY_SIZE(requestNames)-1);
  251. int (*callfptr)(char *uuid,char *payload);
  252. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  253. for(int i= 0; i < CallHandlerNumber ; i ++ )
  254. {
  255. if(strcmp(requestNames[i],str1) == 0)
  256. {
  257. CallHandlerIndex = i ;
  258. break;
  259. }
  260. }
  261. callfptr = NULL;
  262. callfptr = funcalls[CallHandlerIndex];
  263. if(callfptr == NULL)
  264. {}
  265. if ( callfptr )
  266. {
  267. callfptr(uuid, payload);
  268. result = PASS;
  269. }
  270. callfptr = NULL;
  271. return result;
  272. }
  273. void CallResultHandler(char *str1, char *payload, int gun_index)
  274. {
  275. static int CallResultHandlerNumber = 0;
  276. static int CallResultHandlerIndex = 0;
  277. void (*callResultfptr)(char *payload, int gun_index );
  278. //printf("enter CallResultHandler\n");
  279. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  280. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  281. {
  282. if(strcmp(responseNames[i],str1) == 0)
  283. {
  284. CallResultHandlerIndex = i ;
  285. break;
  286. }
  287. }
  288. callResultfptr = NULL;
  289. callResultfptr = funs[CallResultHandlerIndex];
  290. if(callResultfptr == NULL)
  291. {
  292. //printf("callResultfptr is null\n");
  293. }
  294. if ( callResultfptr )
  295. {
  296. callResultfptr(payload, gun_index);
  297. }
  298. callResultfptr = NULL;
  299. }
  300. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  301. {
  302. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  303. callErrorfptr = NULL;
  304. callErrorfptr = funcallerror[0];
  305. //printf("CallErrorHandler \n");
  306. if(callErrorfptr == NULL)
  307. {
  308. printf("callErrorfptr is null\n");
  309. }
  310. if ( callErrorfptr )
  311. {
  312. //printf("callErrorfptr is not null\n");
  313. callErrorfptr(id, errorCode, errorDescription, payload);
  314. }
  315. callErrorfptr = NULL;
  316. }