123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <assert.h>
- #include "hashmap.h"
- #include "HashTable.h"
- //#include "json-c/arraylist.h"
- //#include "json-c/json_tokener.h"
- #include "arraylist.h"
- #include "json_tokener.h"
- //#include "json.h"
- //#include "./lib/Headers/json-c/JsonParser.h"
- #include "JsonParser.h"
- //#include "parse_flags.h"
- //#include "./lib/Headers/json-c/linkhash.h"
- #include "linkhash.h"
- #include "MessageHandler.h"
- //#include "TransactionQueue.h"
- #define MESSAGE_TYPE_CALL 2
- #define MESSAGE_TYPE_CALLRESULT 3
- #define MESSAGE_TYPE_CALLERROR 4
- #define PASS 1
- #define FAIL -1
- #if 0
- void handleAuthorizeResponse(char *payload);
- void handleBootNotificationResponse(char *payload);
- void handleDataTransferResponse(char *payload);
- void handleDiagnosticsStatusNotificationResponse(char *payload);
- #endif
- extern HashTable *tableHandleRequest;
- extern HashTable *tableHandleresponse;
- extern HashTable *tableHandleError ;
- extern map_t hashMap;
- extern data_struct_t* mapItem;
- extern struct node Node;
- extern void split(char **arr, char *str, const char *del);
- void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload);
- int CallHandler(char *uuid, char *str1,char *payload);
- void CallResultHandler(char *str1,char *payload, int gun_index);
- extern void CheckTransactionPacket(char *uuid);
- typedef void (*FunCallErrorPtr)(char *id, char *errorCode, char *errorDescription,char *payload); // 先宣告函式指標
- typedef void (*FunPtr)(char *payload, int gun_index); // 先宣告函式指標
- typedef int (*FunCallPtr)(char *uuid, char *payload); // 先宣告函式指標
- FunPtr funs[] = { handleAuthorizeResponse, handleBootNotificationResponse, handleDataTransferResponse, \
- handleDiagnosticsStatusNotificationResponse, \
- handleFirmwareStatusNotificationResponse, \
- handleHeartbeatResponse, handleMeterValuesResponse, \
- handleStartTransactionResponse, \
- handleStatusNotificationResponse, \
- handleStopTransactionnResponse }; // 將函式集中在陣列中。
- FunCallPtr funcalls[] = { handleCancelReservationRequest, handleChangeAvailabilityRequest, handleChangeConfigurationRequest,handleClearCacheRequest, \
- handleClearChargingProfileRequest, handleDataTransferRequest, handleGetCompositeScheduleRequest, handleGetConfigurationRequest, \
- handleGetDiagnosticsRequest, handleGetLocalListVersionRequest, handleRemoteStartRequest, handleRemoteStopTransactionRequest, \
- handleReserveNowTransactionRequest, handleResetRequest, handleSendLocalListRequest, handleSetChargingProfileRequest, \
- handleTriggerMessageRequest, handleUnlockConnectorRequest, handleUpdateFirmwareRequest }; // 將函式集中在陣列中。
- FunCallErrorPtr funcallerror[] = { handleError };
- /*printing the value corresponding to boolean, double, integer and strings*/
- void print_json_value(json_object *jobj){
- enum json_type type;
- type = json_object_get_type(jobj); /*Getting the type of the json object*/
- printf("type: %d",type);
- switch (type) {
- case json_type_null: break;
- case json_type_boolean: printf("json_type_boolean\n");
- printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
- break;
- case json_type_double: printf("json_type_double\n");
- printf(" value: %lf\n", json_object_get_double(jobj));
- break;
- case json_type_int: printf("json_type_int\n");
- printf(" value: %d\n", json_object_get_int(jobj));
- break;
- case json_type_string: printf("json_type_string\n");
- printf(" value: %s\n", json_object_get_string(jobj));
- break;
- case json_type_object:
- case json_type_array:
- break;
- }
- }
- void json_parse_array( json_object *jobj, char *key) {
- void json_parse(json_object * jobj); /*Forward Declaration*/
- enum json_type type;
- json_object *jarray = jobj; /*Simply get the array*/
- if(key) {
- jarray = json_object_object_get(jobj, key); /*Getting the array if it is a key value pair*/
- }
- int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/
- printf("Array Length: %d\n",arraylen);
- int i;
- json_object * jvalue;
- for (i=0; i< arraylen; i++){
- jvalue = json_object_array_get_idx(jarray, i); /*Getting the array element at position i*/
- type = json_object_get_type(jvalue);
- if (type == json_type_array) {
- json_parse_array(jvalue, NULL);
- }
- else if (type != json_type_object) {
- printf("value[%d]: ",i);
- print_json_value(jvalue);
- }
- else {
- json_parse(jvalue);
- }
- }
- }
- /*Parsing the json object*/
- void json_parse(json_object * jobj) {
- enum json_type type;
- struct array_list *json_list;
- int MsgType;
- char UniqueId[37],Action[33],Payload[10241],ErrorCode[129],ErrorDescription[513];
- char *arr[2]= {};
- int gun_index = 0;
- const char *del = ",";
- char *substr = NULL;
- int count = 0;
- int i = 0;
- char key_value[VALUE_MAX_LENGTH];
- //parsing received message and do something
- memset(key_value, 0, sizeof key_value);
- memset(UniqueId, 0, sizeof UniqueId);
- memset(Action, 0, sizeof Action);
- memset(Payload, 0, sizeof Payload);
- memset(ErrorCode, 0, sizeof ErrorCode);
- memset(ErrorDescription, 0, sizeof ErrorDescription);
- if(json_object_get_type(jobj) == json_type_array)
- {
- /* Get array of tests */
- json_list = json_object_get_array(jobj);
- /* Get test */
- struct json_object *jsonObject = (struct json_object *) array_list_get_idx(json_list, 0);
- sprintf(UniqueId,"%s", json_object_get_string(json_object_array_get_idx(jobj, 1)));
- printf("UniqueId %s\n", UniqueId);
- #if 1
- // check Transaction-related messages
- CheckTransactionPacket(UniqueId);
- #endif
- MsgType = json_object_get_int(jsonObject);
- switch (MsgType)
- {
- case MESSAGE_TYPE_CALL:
- printf("MESSAGE_TYPE_CALL\n");
- sprintf(Action, "%s", json_object_get_string(json_object_array_get_idx(jobj, 2)));
- sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(jobj, 3)));
- CallHandler(UniqueId,Action,Payload);
- break;
- case MESSAGE_TYPE_CALLRESULT:
- printf("MESSAGE_TYPE_CALLRESULT\n");
- sprintf(Payload, "%s", json_object_get_string(json_object_array_get_idx(jobj, 2)));
- #ifdef SystemLogMessage
- //DEBUG_INFO("Message type: CallResult\n");
- //DEBUG_INFO("Message: %s\n", (char *)in);
- #endif
- if(hashmap_operation(1,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
- {
- printf("\test 1\n");
- hashmap_operation(2,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
- printf("\test 2\n");
- //split(arr, mapItem->key_value, del);
- char * const testdup = strdup(key_value/*mapItem->key_value*/);
- printf("original string: %s (@%p)\n", testdup, testdup);
- substr = strtok(testdup, del);
- while (substr != NULL) {
- arr[count] = substr;
- printf(" arr string: %s (@%p)\n", arr[count], substr);
- printf("#%d sub string: %s (@%p)\n", count++, substr, substr);
- substr = strtok(NULL, del);
- }
- i=0;
- sprintf(Action, "%s", *(arr+i++));
- printf("Action=%s\n",Action);
- gun_index = atoi(*(arr+i++));
- printf("gun_index=%d\n",gun_index);
- #ifdef Debug
- DEBUG_INFO("<<<<<%s response\n", Action);
- DEBUG_INFO("Payload: %s\n", Payload);
- #endif
- CallResultHandler(Action, Payload, gun_index);
- #ifdef Debug
- DEBUG_INFO("After pull hash length: %d\n", hashmap_length(hashMap));
- #endif
- free(testdup);
- }
- break;
- case MESSAGE_TYPE_CALLERROR:
- printf("MESSAGE_TYPE_CALLERROR\n");
- sprintf(ErrorCode, "%s", json_object_get_string(json_object_array_get_idx(jobj, 2)));
- sprintf(ErrorDescription, "%s", json_object_get_string(json_object_array_get_idx(jobj, 3)));
- #ifdef SystemLogMessage
- //DEBUG_INFO("Message type: CallError\n");
- //DEBUG_INFO("Message: %s\n", (char *)in);
- #endif
- if(hashmap_operation(1,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
- {
- hashmap_operation(2,hashMap, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
- sprintf(Action, "%s", key_value/*mapItem->key_value*/);
- #ifdef Debug
- DEBUG_INFO("<<<<<%s response\n", Action);
- DEBUG_INFO("ErrorCode: %s\n", ErrorCode);
- DEBUG_INFO("ErrorDescription: %s\n", ErrorDescription);
- #endif
- /*
- * TODO Handle server error response
- */
- CallErrorHandler(UniqueId,ErrorCode, ErrorDescription, "");
- #ifdef Debug
- DEBUG_INFO("After pull hash length: %d\n", hashmap_length(hashMap));
- #endif
- }
- break;
- default:
- break;
- }
- // json_parse_array(jobj, NULL); -- remove temporally
- }
- #if 0
- else
- {
- json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
- printf("key: \%s:\n", key);
- type = json_object_get_type(val);
- printf("type: %d",type);
- switch (type) {
- case json_type_null: break;
- case json_type_boolean:
- case json_type_double:
- case json_type_int:
- case json_type_string: print_json_value(val);
- break;
- case json_type_object: printf("json_type_object\n");
- jobj = json_object_object_get(jobj, key);
- json_parse(jobj);
- break;
- case json_type_array: printf("type: json_type_array, ");
- json_parse_array(jobj, key);
- break;
- }
- }
- }
- #endif
- //free json object
- // json_object_put(json_list); -- remove temporally
- }
- //==========================================
- // Receive Message routine
- //==========================================
- void ReceivedMessage(void *in, size_t len)
- {
- printf("ReceivedMessage\n");
- char tempin[1024*4];
- memset(tempin, 0, 1024*4);
- strcpy(tempin, (const char *)in);
- memset( (void *)in, 0, sizeof(char)*len );
- json_object * jobj = json_tokener_parse((const char *)tempin); //json_tokener_parse((const char *)in); //json_tokener_parse(msg); //json_tokener_parse((char *)in);
- if(jobj != NULL)
- {
- printf("new_obj.to_string()=%s\n", json_object_to_json_string(jobj));
- printf("1\n");
- //parse json object
- json_parse(jobj);
- //free json object
- json_object_put(jobj);
- }
- else
- {
- printf("jobj is null. cant parse messgae. \n");
- }
- //json_object_put(jobj);
- }
- void ClientCoreProfile(HashTable* HandleRequest, HashTable* Handleresponse)
- {
- char *requestNames[] = { "CancelReservation", "ChangeAvailability", "ChangeConfiguration", "ClearCache", \
- "ClearChargingProfile", "DataTransfer", "GetCompositeSchedule", "GetConfiguration", \
- "GetDiagnostics", "GetLocalListVersion", "RemoteStartTransaction", "RemoteStopTransaction", \
- "ReserveNow", "Reset", "SendLocalList", "SetChargingProfile", "TriggerMessage", "UnlockConnector", "UpdateFirmware" };
- char *responseNames[] = { "Authorize", "BootNotification", "DataTransfer", \
- "DiagnosticsStatusNotification", \
- "FirmwareStatusNotification", \
- "Heartbeat", "MeterValues", "StartTransaction", "StatusNotification", \
- "StopTransaction" };
- int i;
- //Handle Server Request
- for (i=0; i<19; i++)
- HashTablePut(HandleRequest, requestNames[i], funcalls[i]);
- //Handle Server Response
- for (i=0; i<10; i++)
- HashTablePut(Handleresponse, responseNames[i], funs[i]);
- }
- int CallHandler(char *uuid, char *str1,char *payload)
- {
- int (*callfptr)(char *uuid,char *payload);
- callfptr = NULL;
- callfptr = HashTableGet(tableHandleRequest, str1);
- printf("CallHandler \n");
- printf("action: %s\n", str1);
- if(callfptr == NULL)
- {
- printf("callfptr is null\n");
- }
- if ( callfptr )
- {
- printf("\test 3\n");
- return callfptr(uuid, payload);
- printf("\test 4\n");
- }
- callfptr = NULL;
- return FAIL;
- }
- void CallResultHandler(char *str1, char *payload, int gun_index)
- {
- void (*callResultfptr)(char *payload, int gun_index );
- callResultfptr = NULL;
- callResultfptr = HashTableGet(tableHandleresponse, str1);
- printf("CallResultHandler \n");
- if(callResultfptr == NULL)
- {
- printf("callResultfptr is null\n");
- }
- if ( callResultfptr )
- {
- printf("\test 3\n");
- callResultfptr(payload, gun_index);
- printf("\test 4\n");
- }
- callResultfptr = NULL;
- }
- void CallErrorHandler(char *id, char *errorCode, char *errorDescription,char *payload)
- {
- void (*callErrorfptr)(char *id, char *errorCode, char *errorDescription,char *payload );
- callErrorfptr = NULL;
- callErrorfptr = funcallerror[0];
- printf("CallErrorHandler \n");
- if(callErrorfptr == NULL)
- {
- printf("callErrorfptr is null\n");
- }
- if ( callErrorfptr )
- {
- printf("callErrorfptr is not null\n");
- callErrorfptr(id, errorCode, errorDescription, payload);
- }
- callErrorfptr = NULL;
- }
|