JsonParser.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "Module_OcppBackend.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. "ChangeAvailability",
  14. "ChangeConfiguration",
  15. "ClearCache",
  16. "ClearChargingProfile",
  17. "DataTransfer",
  18. "GetCompositeSchedule",
  19. "GetConfiguration",
  20. "GetDiagnostics",
  21. "GetLocalListVersion",
  22. "RemoteStartTransaction",
  23. "RemoteStopTransaction",
  24. "ReserveNow",
  25. "Reset",
  26. "SendLocalList",
  27. "SetChargingProfile",
  28. "TriggerMessage",
  29. "UnlockConnector",
  30. "UpdateFirmware",
  31. "CertificateSigned",
  32. "DeleteCertificate",
  33. "ExtendedTriggerMessage",
  34. "GetInstalledCertificateIds",
  35. "GetLog",
  36. "InstallCertificate",
  37. "SignedUpdateFirmware",
  38. "Unknown"};
  39. static char *responseNames[] = {"Authorize",
  40. "BootNotification",
  41. "DataTransfer",
  42. "DiagnosticsStatusNotification",
  43. "FirmwareStatusNotification",
  44. "Heartbeat",
  45. "MeterValues",
  46. "StartTransaction",
  47. "StatusNotification",
  48. "StopTransaction",
  49. "LogStatusNotification",
  50. "SecurityEventNotification",
  51. "SignCertificate",
  52. "SignedFirmwareStatusNotification"};
  53. static FunPtr funs[] = {handleAuthorizeResponse,
  54. handleBootNotificationResponse,
  55. handleDataTransferResponse,
  56. handleDiagnosticsStatusNotificationResponse,
  57. handleFirmwareStatusNotificationResponse,
  58. handleHeartbeatResponse,
  59. handleMeterValuesResponse,
  60. handleStartTransactionResponse,
  61. handleStatusNotificationResponse,
  62. handleStopTransactionnResponse,
  63. handleLogStatusNotificationResponse,
  64. handleSecurityEventNotificationResponse,
  65. handleSignCertificateResponse,
  66. handleSignedFirmwareStatusNotificationResponse};
  67. static FunCallPtr funcalls[] = {handleCancelReservationRequest,
  68. handleChangeAvailabilityRequest,
  69. handleChangeConfigurationRequest,
  70. handleClearCacheRequest,
  71. handleClearChargingProfileRequest,
  72. handleDataTransferRequest,
  73. handleGetCompositeScheduleRequest,
  74. handleGetConfigurationRequest,
  75. handleGetDiagnosticsRequest,
  76. handleGetLocalListVersionRequest,
  77. handleRemoteStartRequest,
  78. handleRemoteStopTransactionRequest,
  79. handleReserveNowTransactionRequest,
  80. handleResetRequest,
  81. handleSendLocalListRequest,
  82. handleSetChargingProfileRequest,
  83. handleTriggerMessageRequest,
  84. handleUnlockConnectorRequest,
  85. handleUpdateFirmwareRequest,
  86. handleCertificateSignedRequest,
  87. handleDeleteCertificateRequest,
  88. handleExtendedTriggerMessageRequest,
  89. handleGetInstalledCertificateIdsRequest,
  90. handleGetLogRequest,
  91. handleInstallCertificateRequest,
  92. handleSignedUpdateFirmwareRequest,
  93. handleUnknownRequest};
  94. static FunCallErrorPtr funcallerror[] = { handleError };
  95. //==========================================
  96. // Receive Message routine
  97. //==========================================
  98. void ReceivedMessage(void *in, size_t len)
  99. {
  100. //DEBUG_INFO("ReceivedMessage\n");
  101. char tempin[WEBSOCKET_BUFFER_SIZE];
  102. int MsgType = 0;
  103. char UniqueId[37],Action[33],Payload[WEBSOCKET_BUFFER_SIZE-712],ErrorCode[129],ErrorDescription[513];
  104. char *arr[2]= {};
  105. int gun_index = 0;
  106. const char *del = ",";
  107. char *substr = NULL;
  108. int count = 0;
  109. int i = 0;
  110. char key_value[VALUE_MAX_LENGTH];
  111. //parsing received message and do something
  112. memset(key_value, 0, sizeof key_value);
  113. memset(UniqueId, 0, sizeof UniqueId);
  114. memset(Action, 0, sizeof Action);
  115. memset(Payload, 0, sizeof Payload);
  116. memset(ErrorCode, 0, sizeof ErrorCode);
  117. memset(ErrorDescription, 0, sizeof ErrorDescription);
  118. memset(tempin, 0, sizeof tempin);
  119. strcpy(tempin, (const char *)in);
  120. memset( (void *)in, 0, sizeof(char)*len );
  121. if(tempin[0] != '\0')
  122. {
  123. if(strcmp((const char *)tempin,"[ ]") == 0 || strcmp((const char *)tempin,"[]") == 0)
  124. {
  125. DEBUG_WARN("Message is empty array.\n");
  126. return;
  127. }
  128. json_object *obj = NULL;
  129. obj = json_tokener_parse(tempin);
  130. if(!is_error(obj))
  131. {
  132. if(json_object_is_type(obj, json_type_array))
  133. {
  134. if(json_object_array_length(obj) < 3)
  135. {
  136. DEBUG_WARN("Message parsed array count is less than 3.\n");
  137. return;
  138. }
  139. }
  140. else
  141. {
  142. DEBUG_WARN("Message is not an array type.\n");
  143. return;
  144. }
  145. MsgType = json_object_get_int(json_object_array_get_idx(obj, 0));
  146. sprintf(UniqueId, "%s", json_object_get_string(json_object_array_get_idx(obj, 1)));
  147. if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
  148. {
  149. DEBUG_WARN("Message type not valid.\n");
  150. return;
  151. }
  152. if(UniqueId[0] == '\0')
  153. {
  154. DEBUG_WARN("Message unique id is null.\n");
  155. return;
  156. }
  157. CheckTransactionPacket(UniqueId);
  158. switch (MsgType)
  159. {
  160. case MESSAGE_TYPE_CALL:
  161. sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  162. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 3), JSON_C_TO_STRING_PLAIN));
  163. CallHandler(UniqueId,Action,Payload);
  164. break;
  165. case MESSAGE_TYPE_CALLRESULT:
  166. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 2), JSON_C_TO_STRING_PLAIN));
  167. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  168. {
  169. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  170. char * const testdup = strdup(key_value);
  171. substr = strtok(testdup, del);
  172. while (substr != NULL)
  173. {
  174. arr[count] = substr;
  175. count++;
  176. substr = strtok(NULL, del);
  177. }
  178. i=0;
  179. sprintf(Action, "%s", *(arr+i++));
  180. gun_index = atoi(*(arr+i++));
  181. CallResultHandler(Action, Payload, gun_index);
  182. free(testdup);
  183. }
  184. break;
  185. case MESSAGE_TYPE_CALLERROR:
  186. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  187. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(obj, 3)));
  188. sprintf(Payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 4), JSON_C_TO_STRING_PLAIN));
  189. if(hashmap_operation(HASH_OP_GET, UniqueId, key_value) == TRUE)
  190. {
  191. hashmap_operation(HASH_OP_REMOVE, UniqueId, key_value);
  192. sprintf(Action, "%s", key_value);
  193. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. else
  201. {
  202. DEBUG_WARN("Message is invalid JSON format.\n");
  203. }
  204. json_object_put(obj);
  205. }
  206. else
  207. {
  208. DEBUG_WARN("Message is null. can't parse message.\n");
  209. }
  210. }
  211. int CallHandler(char *uuid, char *str1,char *payload)
  212. {
  213. int result = FAIL;
  214. int CallHandlerNumber = 0;
  215. int CallHandlerIndex = (ARRAY_SIZE(requestNames)-1);
  216. int (*callfptr)(char *uuid,char *payload);
  217. CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
  218. for(int i= 0; i < CallHandlerNumber ; i ++ )
  219. {
  220. if(strcmp(requestNames[i],str1) == 0)
  221. {
  222. CallHandlerIndex = i ;
  223. break;
  224. }
  225. }
  226. callfptr = NULL;
  227. callfptr = funcalls[CallHandlerIndex];
  228. if(callfptr == NULL)
  229. {}
  230. if ( callfptr )
  231. {
  232. callfptr(uuid, payload);
  233. result = PASS;
  234. }
  235. callfptr = NULL;
  236. return result;
  237. }
  238. void CallResultHandler(char *str1, char *payload, int gun_index)
  239. {
  240. static int CallResultHandlerNumber = 0;
  241. static int CallResultHandlerIndex = 0;
  242. void (*callResultfptr)(char *payload, int gun_index );
  243. CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
  244. for(int i= 0; i < CallResultHandlerNumber ; i ++ )
  245. {
  246. if(strcmp(responseNames[i],str1) == 0)
  247. {
  248. CallResultHandlerIndex = i ;
  249. break;
  250. }
  251. }
  252. callResultfptr = NULL;
  253. callResultfptr = funs[CallResultHandlerIndex];
  254. if(callResultfptr == NULL)
  255. {}
  256. if ( callResultfptr )
  257. {
  258. callResultfptr(payload, gun_index);
  259. }
  260. callResultfptr = NULL;
  261. }
  262. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  263. {
  264. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  265. callErrorfptr = NULL;
  266. callErrorfptr = funcallerror[0];
  267. if(callErrorfptr == NULL)
  268. {
  269. DEBUG_ERROR("callErrorfptr is null\n");
  270. }
  271. if ( callErrorfptr )
  272. {
  273. callErrorfptr(id, errorCode, errorDescription, payload);
  274. }
  275. callErrorfptr = NULL;
  276. }