JsonParser.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "hashmap.h"
  7. #include "HashTable.h"
  8. //#include "json-c/arraylist.h"
  9. //#include "json-c/json_tokener.h"
  10. #include "arraylist.h"
  11. #include "json_tokener.h"
  12. //#include "json.h"
  13. //#include "./lib/Headers/json-c/JsonParser.h"
  14. #include "JsonParser.h"
  15. //#include "parse_flags.h"
  16. //#include "./lib/Headers/json-c/linkhash.h"
  17. #include "linkhash.h"
  18. #include "MessageHandler.h"
  19. //#include "TransactionQueue.h"
  20. #define MESSAGE_TYPE_CALL 2
  21. #define MESSAGE_TYPE_CALLRESULT 3
  22. #define MESSAGE_TYPE_CALLERROR 4
  23. #define PASS 1
  24. #define FAIL -1
  25. #if 0
  26. void handleAuthorizeResponse(char *payload);
  27. void handleBootNotificationResponse(char *payload);
  28. void handleDataTransferResponse(char *payload);
  29. void handleDiagnosticsStatusNotificationResponse(char *payload);
  30. #endif
  31. extern HashTable *tableHandleRequest;
  32. extern HashTable *tableHandleresponse;
  33. extern HashTable *tableHandleError ;
  34. extern map_t hashMap;
  35. extern data_struct_t* mapItem;
  36. extern struct node Node;
  37. extern void split(char **arr, char *str, const char *del);
  38. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload);
  39. int CallHandler(char *uuid, char *str1,char *payload);
  40. void CallResultHandler(char *str1,char *payload, int gun_index);
  41. extern void CheckTransactionPacket(char *uuid);
  42. typedef void (*FunCallErrorPtr)(char *id, char *errorCode, char *errorDescription,char *payload); // 先宣告函式指標
  43. typedef void (*FunPtr)(char *payload, int gun_index); // 先宣告函式指標
  44. typedef int (*FunCallPtr)(char *uuid, char *payload); // 先宣告函式指標
  45. FunPtr funs[] = { handleAuthorizeResponse, handleBootNotificationResponse, handleDataTransferResponse, \
  46. handleDiagnosticsStatusNotificationResponse, \
  47. handleFirmwareStatusNotificationResponse, \
  48. handleHeartbeatResponse, handleMeterValuesResponse, \
  49. handleStartTransactionResponse, \
  50. handleStatusNotificationResponse, \
  51. handleStopTransactionnResponse }; // 將函式集中在陣列中。
  52. FunCallPtr funcalls[] = { handleCancelReservationRequest, handleChangeAvailabilityRequest, handleChangeConfigurationRequest,handleClearCacheRequest, \
  53. handleClearChargingProfileRequest, handleDataTransferRequest, handleGetCompositeScheduleRequest, handleGetConfigurationRequest, \
  54. handleGetDiagnosticsRequest, handleGetLocalListVersionRequest, handleRemoteStartRequest, handleRemoteStopTransactionRequest, \
  55. handleReserveNowTransactionRequest, handleResetRequest, handleSendLocalListRequest, handleSetChargingProfileRequest, \
  56. handleTriggerMessageRequest, handleUnlockConnectorRequest, handleUpdateFirmwareRequest }; // 將函式集中在陣列中。
  57. FunCallErrorPtr funcallerror[] = { handleError };
  58. /*printing the value corresponding to boolean, double, integer and strings*/
  59. void print_json_value(json_object *jobj){
  60. enum json_type type;
  61. type = json_object_get_type(jobj); /*Getting the type of the json object*/
  62. printf("type: %d",type);
  63. switch (type) {
  64. case json_type_null: break;
  65. case json_type_boolean: printf("json_type_boolean\n");
  66. printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
  67. break;
  68. case json_type_double: printf("json_type_double\n");
  69. printf(" value: %lf\n", json_object_get_double(jobj));
  70. break;
  71. case json_type_int: printf("json_type_int\n");
  72. printf(" value: %d\n", json_object_get_int(jobj));
  73. break;
  74. case json_type_string: printf("json_type_string\n");
  75. printf(" value: %s\n", json_object_get_string(jobj));
  76. break;
  77. case json_type_object:
  78. case json_type_array:
  79. break;
  80. }
  81. }
  82. void json_parse_array( json_object *jobj, char *key) {
  83. void json_parse(json_object * jobj); /*Forward Declaration*/
  84. enum json_type type;
  85. json_object *jarray = jobj; /*Simply get the array*/
  86. if(key) {
  87. jarray = json_object_object_get(jobj, key); /*Getting the array if it is a key value pair*/
  88. }
  89. int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/
  90. printf("Array Length: %d\n",arraylen);
  91. int i;
  92. json_object * jvalue;
  93. for (i=0; i< arraylen; i++){
  94. jvalue = json_object_array_get_idx(jarray, i); /*Getting the array element at position i*/
  95. type = json_object_get_type(jvalue);
  96. if (type == json_type_array) {
  97. json_parse_array(jvalue, NULL);
  98. }
  99. else if (type != json_type_object) {
  100. printf("value[%d]: ",i);
  101. print_json_value(jvalue);
  102. }
  103. else {
  104. json_parse(jvalue);
  105. }
  106. }
  107. }
  108. /*Parsing the json object*/
  109. void json_parse(json_object * jobj) {
  110. enum json_type type;
  111. struct array_list *json_list;
  112. int MsgType;
  113. char UniqueId[37],Action[33],Payload[10241],ErrorCode[129],ErrorDescription[513];
  114. char *arr[2]= {};
  115. int gun_index = 0;
  116. const char *del = ",";
  117. char *substr = NULL;
  118. int count = 0;
  119. int i = 0;
  120. char key_value[VALUE_MAX_LENGTH];
  121. //parsing received message and do something
  122. memset(key_value, 0, sizeof key_value);
  123. memset(UniqueId, 0, sizeof UniqueId);
  124. memset(Action, 0, sizeof Action);
  125. memset(Payload, 0, sizeof Payload);
  126. memset(ErrorCode, 0, sizeof ErrorCode);
  127. memset(ErrorDescription, 0, sizeof ErrorDescription);
  128. if(json_object_get_type(jobj) == json_type_array)
  129. {
  130. /* Get array of tests */
  131. json_list = json_object_get_array(jobj);
  132. /* Get test */
  133. struct json_object *jsonObject = (struct json_object *) array_list_get_idx(json_list, 0);
  134. sprintf(UniqueId,"%s", json_object_get_string(json_object_array_get_idx(jobj, 1)));
  135. printf("UniqueId %s\n", UniqueId);
  136. #if 1
  137. // check Transaction-related messages
  138. CheckTransactionPacket(UniqueId);
  139. #endif
  140. MsgType = json_object_get_int(jsonObject);
  141. switch (MsgType)
  142. {
  143. case MESSAGE_TYPE_CALL:
  144. printf("MESSAGE_TYPE_CALL\n");
  145. sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(jobj, 2)));
  146. sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(jobj, 3)));
  147. CallHandler(UniqueId,Action,Payload);
  148. break;
  149. case MESSAGE_TYPE_CALLRESULT:
  150. printf("MESSAGE_TYPE_CALLRESULT\n");
  151. sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(jobj, 2)));
  152. #ifdef SystemLogMessage
  153. //DEBUG_INFO("Message type: CallResult\n");
  154. //DEBUG_INFO("Message: %s\n", (char *)in);
  155. #endif
  156. if(hashmap_operation(1,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
  157. {
  158. printf("\test 1\n");
  159. hashmap_operation(2,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
  160. printf("\test 2\n");
  161. //split(arr, mapItem->key_value, del);
  162. char * const testdup = strdup(key_value/*mapItem->key_value*/);
  163. printf("original string: %s (@%p)\n", testdup, testdup);
  164. substr = strtok(testdup, del);
  165. while (substr != NULL) {
  166. arr[count] = substr;
  167. printf(" arr string: %s (@%p)\n", arr[count], substr);
  168. printf("#%d sub string: %s (@%p)\n", count++, substr, substr);
  169. substr = strtok(NULL, del);
  170. }
  171. i=0;
  172. sprintf(Action, "%s", *(arr+i++));
  173. printf("Action=%s\n",Action);
  174. gun_index = atoi(*(arr+i++));
  175. printf("gun_index=%d\n",gun_index);
  176. #ifdef Debug
  177. DEBUG_INFO("<<<<<%s response\n", Action);
  178. DEBUG_INFO("Payload: %s\n", Payload);
  179. #endif
  180. CallResultHandler(Action, Payload, gun_index);
  181. #ifdef Debug
  182. DEBUG_INFO("After pull hash length: %d\n", hashmap_length(hashMap));
  183. #endif
  184. free(testdup);
  185. }
  186. break;
  187. case MESSAGE_TYPE_CALLERROR:
  188. printf("MESSAGE_TYPE_CALLERROR\n");
  189. sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(jobj, 2)));
  190. sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(jobj, 3)));
  191. #ifdef SystemLogMessage
  192. //DEBUG_INFO("Message type: CallError\n");
  193. //DEBUG_INFO("Message: %s\n", (char *)in);
  194. #endif
  195. if(hashmap_operation(1,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
  196. {
  197. hashmap_operation(2,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
  198. sprintf(Action, "%s", key_value/*mapItem->key_value*/);
  199. #ifdef Debug
  200. DEBUG_INFO("<<<<<%s response\n", Action);
  201. DEBUG_INFO("ErrorCode: %s\n", ErrorCode);
  202. DEBUG_INFO("ErrorDescription: %s\n", ErrorDescription);
  203. #endif
  204. /*
  205. * TODO Handle server error response
  206. */
  207. CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
  208. #ifdef Debug
  209. DEBUG_INFO("After pull hash length: %d\n", hashmap_length(hashMap));
  210. #endif
  211. }
  212. break;
  213. default:
  214. break;
  215. }
  216. // json_parse_array(jobj, NULL); -- remove temporally
  217. }
  218. #if 0
  219. else
  220. {
  221. json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
  222. printf("key: \%s:\n", key);
  223. type = json_object_get_type(val);
  224. printf("type: %d",type);
  225. switch (type) {
  226. case json_type_null: break;
  227. case json_type_boolean:
  228. case json_type_double:
  229. case json_type_int:
  230. case json_type_string: print_json_value(val);
  231. break;
  232. case json_type_object: printf("json_type_object\n");
  233. jobj = json_object_object_get(jobj, key);
  234. json_parse(jobj);
  235. break;
  236. case json_type_array: printf("type: json_type_array, ");
  237. json_parse_array(jobj, key);
  238. break;
  239. }
  240. }
  241. }
  242. #endif
  243. //free json object
  244. // json_object_put(json_list); -- remove temporally
  245. }
  246. //==========================================
  247. // Receive Message routine
  248. //==========================================
  249. void ReceivedMessage(void *in, size_t len)
  250. {
  251. printf("ReceivedMessage\n");
  252. char tempin[1024*4];
  253. memset(tempin, 0, 1024*4);
  254. strcpy(tempin, (const char *)in);
  255. memset( (void *)in, 0, sizeof(char)*len );
  256. json_object * jobj = json_tokener_parse((const char *)tempin); //json_tokener_parse((const char *)in); //json_tokener_parse(msg); //json_tokener_parse((char *)in);
  257. if(jobj != NULL)
  258. {
  259. printf("new_obj.to_string()=%s\n", json_object_to_json_string(jobj));
  260. printf("1\n");
  261. //parse json object
  262. json_parse(jobj);
  263. //free json object
  264. json_object_put(jobj);
  265. }
  266. else
  267. {
  268. printf("jobj is null. cant parse messgae. \n");
  269. }
  270. //json_object_put(jobj);
  271. }
  272. void ClientCoreProfile(HashTable* HandleRequest, HashTable* Handleresponse)
  273. {
  274. char *requestNames[] = { "CancelReservation", "ChangeAvailability", "ChangeConfiguration", "ClearCache", \
  275. "ClearChargingProfile", "DataTransfer", "GetCompositeSchedule", "GetConfiguration", \
  276. "GetDiagnostics", "GetLocalListVersion", "RemoteStartTransaction", "RemoteStopTransaction", \
  277. "ReserveNow", "Reset", "SendLocalList", "SetChargingProfile", "TriggerMessage", "UnlockConnector", "UpdateFirmware" };
  278. char *responseNames[] = { "Authorize", "BootNotification", "DataTransfer", \
  279. "DiagnosticsStatusNotification", \
  280. "FirmwareStatusNotification", \
  281. "Heartbeat", "MeterValues", "StartTransaction", "StatusNotification", \
  282. "StopTransaction" };
  283. int i;
  284. //Handle Server Request
  285. for (i=0; i<19; i++)
  286. HashTablePut(HandleRequest, requestNames[i], funcalls[i]);
  287. //Handle Server Response
  288. for (i=0; i<10; i++)
  289. HashTablePut(Handleresponse, responseNames[i], funs[i]);
  290. }
  291. int CallHandler(char *uuid, char *str1,char *payload)
  292. {
  293. int (*callfptr)(char *uuid,char *payload);
  294. callfptr = NULL;
  295. callfptr = HashTableGet(tableHandleRequest, str1);
  296. printf("CallHandler \n");
  297. printf("action: %s\n", str1);
  298. if(callfptr == NULL)
  299. {
  300. printf("callfptr is null\n");
  301. }
  302. if ( callfptr )
  303. {
  304. printf("\test 3\n");
  305. return callfptr(uuid, payload);
  306. printf("\test 4\n");
  307. }
  308. callfptr = NULL;
  309. return FAIL;
  310. }
  311. void CallResultHandler(char *str1, char *payload, int gun_index)
  312. {
  313. void (*callResultfptr)(char *payload, int gun_index );
  314. callResultfptr = NULL;
  315. callResultfptr = HashTableGet(tableHandleresponse, str1);
  316. printf("CallResultHandler \n");
  317. if(callResultfptr == NULL)
  318. {
  319. printf("callResultfptr is null\n");
  320. }
  321. if ( callResultfptr )
  322. {
  323. printf("\test 3\n");
  324. callResultfptr(payload, gun_index);
  325. printf("\test 4\n");
  326. }
  327. callResultfptr = NULL;
  328. }
  329. void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
  330. {
  331. void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
  332. callErrorfptr = NULL;
  333. callErrorfptr = funcallerror[0];
  334. printf("CallErrorHandler \n");
  335. if(callErrorfptr == NULL)
  336. {
  337. printf("callErrorfptr is null\n");
  338. }
  339. if ( callErrorfptr )
  340. {
  341. printf("callErrorfptr is not null\n");
  342. callErrorfptr(id, errorCode, errorDescription, payload);
  343. }
  344. callErrorfptr = NULL;
  345. }