JsonParser.c 8.6 KB

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