JsonParser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. "ReserveNowTransaction",
  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. static char *responseNames[] = {"Authorize",
  51. "BootNotification",
  52. "ClearedChargingLimit",
  53. "DataTransfer",
  54. "FirmwareStatusNotification",
  55. "Get15118EVCertificate",
  56. "GetCertificateStatus",
  57. "Heartbeat",
  58. "LogStatusNotification",
  59. "MeterValues",
  60. "NotifyChargingLimit",
  61. "NotifyCustomerInformation",
  62. "NotifyDisplayMessages",
  63. "NotifyEVChargingNeeds",
  64. "NotifyEVChargingSchedule",
  65. "NotifyEvent",
  66. "NotifyMonitoringReport",
  67. "NotifyReport",
  68. "PublishFirmwareStatusNotification",
  69. "ReportChargingProfiles",
  70. "ReservationStatusUpdate",
  71. "SecurityEventNotification",
  72. "SignCertificate",
  73. "StatusNotification",
  74. "TransactionEven" };
  75. static FunPtr funs[] = {handleAuthorizeResponse,
  76. handleBootNotificationResponse,
  77. handleClearedChargingLimitResponse,
  78. handleDataTransferResponse,
  79. handleFirmwareStatusNotificationResponse,
  80. handleGet15118EVCertificateResponse,
  81. hanldeGetCertificateStatusResponse,
  82. handleHeartbeatResponse,
  83. hanldeLogStatusNotificationResponse,
  84. handleMeterValuesResponse,
  85. handleNotifyChargingLimitResponse,
  86. hanldeNotifyCustomerInformationResponse,
  87. hanldeNotifyDisplayMessagesResponse,
  88. hanldeNotifyEVChargingNeedsResponse,
  89. hanldeNotifyEVChargingScheduleResponse,
  90. hanldeNotifyEventResponse,
  91. hanldeNotifyMonitoringReportResponse,
  92. hanldeNotifyReportResponse,
  93. hanldePublishFirmwareStatusNotificationResponse,
  94. hanldeReportChargingProfilesResponse,
  95. hanldeReservationStatusUpdateResponse,
  96. hanldeSecurityEventNotificationResponse,
  97. hanldeSignCertificateResponse,
  98. handleStatusNotificationResponse,
  99. hanldeTransactionEvenResponse};
  100. static FunCallPtr funcalls[] = {handleCancelReservationRequest,
  101. handleCertificateSignedRequest,
  102. handleChangeAvailabilityRequest,
  103. handleClearCacheRequest,
  104. handleClearChargingProfileRequest,
  105. handleClearDisplayMessageRequest,
  106. handleClearVariableMonitoringRequest,
  107. handleCostUpdatedRequest,
  108. handleCustomerInformationRequest,
  109. handleDataTransferRequest,
  110. handleDeleteCertificateRequest,
  111. handleGetBaseReportRequest,
  112. handleGetChargingProfilesRequest,
  113. handleGetCompositeScheduleRequest,
  114. handleGetDisplayMessagesRequest,
  115. handleGetInstalledCertificateIdsRequest,
  116. handleGetLocalListVersionRequest,
  117. handleGetLogRequest,
  118. handleGetMonitoringReportRequest,
  119. handleGetReportRequest,
  120. handleGetTransactionStatusRequest,
  121. handleGetVariablesRequest,
  122. handleInstallCertificateRequest,
  123. handlePublishFirmwareRequest,
  124. handleRemoteStartTransactionRequest,
  125. handleRemoteStopTransactionRequest,
  126. handleReserveNowTransactionRequest,
  127. handleResetRequest,
  128. handleSendLocalListRequest,
  129. handleSetChargingProfileRequest,
  130. handleSetDisplayMessageRequest,
  131. handleSetMonitoringBaseRequest,
  132. handleSetMonitoringLevelRequest,
  133. handleSetNetworkProfileRequest,
  134. handleSetVariableMonitoringRequest,
  135. handleSetVariablesRequest,
  136. handleTriggerMessageRequest,
  137. handleUnlockConnectorRequest,
  138. handleUnpublishFirmwareRequest,
  139. handleUpdateFirmwareRequest};
  140. static FunCallErrorPtr funcallerror[] = { handleError };
  141. //==========================================
  142. // Receive Message routine
  143. //==========================================
  144. void ReceivedMessage(void *in, size_t len)
  145. {
  146. //DEBUG_INFO("ReceivedMessage\n");
  147. char tempin[65536];
  148. int MsgType = 0;
  149. char UniqueId[37],Action[33],Payload[64824],ErrorCode[129],ErrorDescription[513];
  150. char *arr[2]= {};
  151. int gun_index = 0;
  152. const char *del = ",";
  153. char *substr = NULL;
  154. int count = 0;
  155. int i = 0;
  156. char key_value[VALUE_MAX_LENGTH];
  157. //parsing received message and do something
  158. memset(key_value, 0, sizeof key_value);
  159. memset(UniqueId, 0, sizeof UniqueId);
  160. memset(Action, 0, sizeof Action);
  161. memset(Payload, 0, sizeof Payload);
  162. memset(ErrorCode, 0, sizeof ErrorCode);
  163. memset(ErrorDescription, 0, sizeof ErrorDescription);
  164. memset(tempin, 0, 1024*4);
  165. strcpy(tempin, (const char *)in);
  166. memset( (void *)in, 0, sizeof(char)*len );
  167. if(tempin[0] != '\0')
  168. {
  169. if(strcmp((const char *)tempin,"[ ]") == 0)
  170. {
  171. DEBUG_WARN("Message is empty array.\n");
  172. return;
  173. }
  174. json_object *obj = NULL;
  175. obj = json_tokener_parse(tempin);
  176. if(!is_error(obj))
  177. {
  178. MsgType = json_object_get_int(json_object_array_get_idx(obj, 0));
  179. sprintf(UniqueId, "%s", json_object_get_string(json_object_array_get_idx(obj, 1)));
  180. if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
  181. {
  182. DEBUG_WARN("Message type not valid.\n");
  183. return;
  184. }
  185. if(UniqueId[0] == '\0')
  186. {
  187. DEBUG_WARN("Message unique id is null.\n");
  188. return;
  189. }
  190. CheckTransactionPacket(UniqueId);
  191. switch (MsgType)
  192. {
  193. case MESSAGE_TYPE_CALL:
  194. sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  195. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 3), JSON_C_TO_STRING_PLAIN));
  196. CallHandler(UniqueId,Action,Payload);
  197. break;
  198. case MESSAGE_TYPE_CALLRESULT:
  199. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 2), JSON_C_TO_STRING_PLAIN));
  200. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  201. {
  202. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  203. char * const testdup = strdup(key_value);
  204. substr = strtok(testdup, del);
  205. while (substr != NULL)
  206. {
  207. arr[count] = substr;
  208. count++;
  209. substr = strtok(NULL, del);
  210. }
  211. i=0;
  212. sprintf(Action, "%s", *(arr+i++));
  213. gun_index = atoi(*(arr+i++));
  214. CallResultHandler(Action, Payload, gun_index);
  215. free(testdup);
  216. }
  217. break;
  218. case MESSAGE_TYPE_CALLERROR:
  219. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  220. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(obj, 3)));
  221. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 4), 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. sprintf(Action, "%s", key_value);
  226. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. else
  234. {
  235. DEBUG_WARN("Message is invalid JSON format.\n");
  236. }
  237. json_object_put(obj);
  238. }
  239. else
  240. {
  241. DEBUG_WARN("Message is null. can't parse message.\n");
  242. }
  243. }
  244. int CallHandler(char *uuid, char *str1,char *payload)
  245. {
  246. static int CallHandlerNumber = 0;
  247. static int CallHandlerIndex = 0;
  248. int (*callfptr)(char *uuid,char *payload);
  249. //DEBUG_INFO("enter CallHandler\n");
  250. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  251. for(int i= 0; i < CallHandlerNumber ; i ++ )
  252. {
  253. if(strcmp(requestNames[i],str1) == 0)
  254. {
  255. CallHandlerIndex = i ;
  256. break;
  257. }
  258. }
  259. callfptr = NULL;
  260. callfptr = funcalls[CallHandlerIndex];
  261. if(callfptr == NULL)
  262. {
  263. //printf("callfptr is null\n");
  264. }
  265. if ( callfptr )
  266. {
  267. //printf("exec CallHandler ... \n");
  268. callfptr(uuid, payload);
  269. callfptr = NULL;
  270. return PASS;
  271. }
  272. callfptr = NULL;
  273. return FAIL;
  274. }
  275. void CallResultHandler(char *str1, char *payload, int gun_index)
  276. {
  277. static int CallResultHandlerNumber = 0;
  278. static int CallResultHandlerIndex = 0;
  279. void (*callResultfptr)(char *payload, int gun_index );
  280. //printf("enter CallResultHandler\n");
  281. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  282. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  283. {
  284. if(strcmp(responseNames[i],str1) == 0)
  285. {
  286. CallResultHandlerIndex = i ;
  287. break;
  288. }
  289. }
  290. callResultfptr = NULL;
  291. callResultfptr = funs[CallResultHandlerIndex];
  292. if(callResultfptr == NULL)
  293. {
  294. //printf("callResultfptr is null\n");
  295. }
  296. if ( callResultfptr )
  297. {
  298. callResultfptr(payload, gun_index);
  299. }
  300. callResultfptr = NULL;
  301. }
  302. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  303. {
  304. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  305. callErrorfptr = NULL;
  306. callErrorfptr = funcallerror[0];
  307. //printf("CallErrorHandler \n");
  308. if(callErrorfptr == NULL)
  309. {
  310. printf("callErrorfptr is null\n");
  311. }
  312. if ( callErrorfptr )
  313. {
  314. //printf("callErrorfptr is not null\n");
  315. callErrorfptr(id, errorCode, errorDescription, payload);
  316. }
  317. callErrorfptr = NULL;
  318. }