JsonParser.c 7.8 KB

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