123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <assert.h>
- #include "hashmap.h"
- #include "MessageHandler.h"
- #include "SystemLogMessage.h"
- #define MESSAGE_TYPE_CALL 2
- #define MESSAGE_TYPE_CALLRESULT 3
- #define MESSAGE_TYPE_CALLERROR 4
- #define PASS 1
- #define FAIL -1
- //extern map_t hashMap;
- //extern data_struct_t* mapItem; --- remove temporally
- //extern data_struct_t mapItem[0];
- //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); // 先宣告函式指標
- typedef enum boolean { FALSE, TRUE } BOOL;
- static char *requestNames[] = { "CancelReservation", "ChangeAvailability", "ChangeConfiguration", "ClearCache", \
- "ClearChargingProfile", "DataTransfer", "GetCompositeSchedule", "GetConfiguration", \
- "GetDiagnostics", "GetLocalListVersion", "RemoteStartTransaction", "RemoteStopTransaction", \
- "ReserveNow", "Reset", "SendLocalList", "SetChargingProfile", "TriggerMessage", "UnlockConnector", "UpdateFirmware" };
- static char *responseNames[] = { "Authorize", "BootNotification", "DataTransfer", \
- "DiagnosticsStatusNotification", \
- "FirmwareStatusNotification", \
- "Heartbeat", "MeterValues", "StartTransaction", "StatusNotification", \
- "StopTransaction" };
- static FunPtr funs[] = { handleAuthorizeResponse, handleBootNotificationResponse, handleDataTransferResponse, \
- handleDiagnosticsStatusNotificationResponse, \
- handleFirmwareStatusNotificationResponse, \
- handleHeartbeatResponse, handleMeterValuesResponse, \
- handleStartTransactionResponse, \
- handleStatusNotificationResponse, \
- handleStopTransactionnResponse }; //
- static FunCallPtr funcalls[] = { handleCancelReservationRequest, handleChangeAvailabilityRequest, handleChangeConfigurationRequest,handleClearCacheRequest, \
- handleClearChargingProfileRequest, handleDataTransferRequest, handleGetCompositeScheduleRequest, handleGetConfigurationRequest, \
- handleGetDiagnosticsRequest, handleGetLocalListVersionRequest, handleRemoteStartRequest, handleRemoteStopTransactionRequest, \
- handleReserveNowTransactionRequest, handleResetRequest, handleSendLocalListRequest, handleSetChargingProfileRequest, \
- handleTriggerMessageRequest, handleUnlockConnectorRequest, handleUpdateFirmwareRequest }; //
- static FunCallErrorPtr funcallerror[] = { handleError };
- //==========================================
- // Receive Message routine
- //==========================================
- void ReceivedMessage(void *in, size_t len)
- {
- printf("ReceivedMessage\n");
- char tempin[1024*4];
- int MsgType = 0;
- 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;
- int c = 0;
- //int templen = 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);
- memset(tempin, 0, 1024*4);
- strcpy(tempin, (const char *)in);
- memset( (void *)in, 0, sizeof(char)*len );
- //char *str = "[ ]";
- if(strcmp((const char *)tempin,"[ ]") == 0)
- {
- printf("Message is empty arrary. \n");
- return;
- }
- if(tempin[0] != '\0')//if(jobj != NULL)
- {
- MsgType = tempin[1] - '0';
- //printf("MsgType=%d\n",MsgType);
- c = 0;
- if((MsgType != 2) && (MsgType != 3) && (MsgType != 4) )
- {
- return;
- }
- while ((tempin[4+c] != '\"') && (tempin[4+c] != '\0'))
- {
- UniqueId[c] = tempin[4+c];
- //printf("i=%d UniqueId[c]=%c\n",c, UniqueId[c]);
- c++;
- }
- UniqueId[c] = '\0';
- //printf("UniqueId=%s\n",UniqueId);
- if(UniqueId[0] == '\0')
- {
- return;
- }
- CheckTransactionPacket(UniqueId);
- switch (MsgType)
- {
- case MESSAGE_TYPE_CALL:
- //DEBUG_INFO("MESSAGE_TYPE_CALL\n");
- c = 0;
- while (tempin[4+4+strlen(UniqueId)-1+c] != '\"')
- {
- Action[c] = tempin[4+4+strlen(UniqueId)-1+c];
- //printf("i=%d Action[c]=%c\n",c, Action[c]);
- c++;
- }
- Action[c] = '\0';
- //printf("Action=%s\n",Action);
- c = 0;
- int templen= 4+4+3+strlen(UniqueId)-1+strlen(Action)-1;
- while ((c+ templen) < (strlen(tempin)-1))
- {
- Payload[c] = tempin[templen + c];
- //printf("i=%d Payload[c]=%c\n",c, Payload[c]);
- c++;
- }
- Payload[c] = '\0';
- //printf("Payload=%s\n",Payload);
- CallHandler(UniqueId,Action,Payload);
- break;
- case MESSAGE_TYPE_CALLRESULT:
- //DEBUG_INFO("MESSAGE_TYPE_CALLRESULT\n");
- c = 0;
- templen= 4+3+strlen(UniqueId)-1;
- while ((c+ templen) < (strlen(tempin)-1))
- {
- Payload[c] = tempin[templen + c];
- //printf("i=%d sstr=%c\n",c, Payload[c]);
- c++;
- }
- Payload[c] = '\0';
- //printf("Payload=%s\n",Payload);
- if(hashmap_operation(1, UniqueId, key_value) == TRUE)//if(hashmap_operation(1,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
- {
- hashmap_operation(2, UniqueId, key_value);//hashmap_operation(2,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/);//hashmap_remove(hashMap, UniqueId);
- 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);
- count++;
- 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:
- //DEBUG_INFO("MESSAGE_TYPE_CALLERROR\n");
- c = 0;
- while (tempin[4+4+strlen(UniqueId)-1+c] != '\"')
- {
- ErrorCode[c] = tempin[4+4+strlen(UniqueId)-1+c] ;
- // printf("i=%d sstr=%c\n",c, ErrorCode[c]);
- c++;
- }
- ErrorCode[c] = '\0';
- // DEBUG_INFO("ErrorCode=%s\n",ErrorCode);
- c = 0;
- while (tempin[4+4+4+strlen(UniqueId)-1+strlen(ErrorCode)-1+c] != '\"')
- {
- ErrorDescription[c] = tempin[4+4+4+strlen(UniqueId)-1+strlen(ErrorCode)-1+c];
- //printf("i=%d sstr=%c\n",c, ErrorDescription[c]);
- c++;
- }
- ErrorDescription[c] = '\0';
- // DEBUG_INFO("ErrorDescription=%s\n",ErrorDescription);
- c = 0;
- templen= 4+4+4+3+strlen(UniqueId)-1+strlen(ErrorCode)-1+strlen(ErrorDescription)-1;
- while ((c+ templen) < (strlen(tempin)-1))
- {
- Payload[c] = tempin[templen + c];
- // printf("i=%d sstr=%c\n",c, Payload[c]);
- c++;
- }
- Payload[c] = '\0';
- // DEBUG_INFO("Payload=%s\n",Payload);
- if(hashmap_operation(1, UniqueId, key_value) == TRUE)//if(hashmap_operation(1,NULL/*hashMap*/, UniqueId, mapItem, key_value/*(void**)(&mapItem)*/) == MAP_OK/*hashmap_get(hashMap, UniqueId, (void**)(&mapItem)) == MAP_OK*/)
- {
- hashmap_operation(2, UniqueId, key_value);//hashmap_operation(2,NULL/*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;
- }
- }
- else
- {
- printf("Message is null. cant parse messgae. \n");
- }
- }
- int CallHandler(char *uuid, char *str1,char *payload)
- {
- static int CallHandlerNumber = 0;
- static int CallHandlerIndex = 0;
- int (*callfptr)(char *uuid,char *payload);
- //DEBUG_INFO("enter CallHandler\n");
- CallHandlerNumber = sizeof(requestNames)/sizeof(requestNames[0]);
- for(int i= 0; i < CallHandlerNumber ; i ++ )
- {
- if(strcmp(requestNames[i],str1) == 0)
- {
- CallHandlerIndex = i ;
- break;
- }
- }
- callfptr = NULL;
- callfptr = funcalls[CallHandlerIndex];
- if(callfptr == NULL)
- {
- //printf("callfptr is null\n");
- }
- if ( callfptr )
- {
- //printf("exec CallHandler ... \n");
- callfptr(uuid, payload);
- callfptr = NULL;
- return PASS;
- }
- callfptr = NULL;
- return FAIL;
- }
- void CallResultHandler(char *str1, char *payload, int gun_index)
- {
- static int CallResultHandlerNumber = 0;
- static int CallResultHandlerIndex = 0;
- void (*callResultfptr)(char *payload, int gun_index );
- //printf("enter CallResultHandler\n");
- CallResultHandlerNumber = sizeof(responseNames)/sizeof(responseNames[0]);
- for(int i= 0; i < CallResultHandlerNumber ; i ++ )
- {
- if(strcmp(responseNames[i],str1) == 0)
- {
- CallResultHandlerIndex = i ;
- break;
- }
- }
- callResultfptr = NULL;
- callResultfptr = funs[CallResultHandlerIndex];
- if(callResultfptr == NULL)
- {
- //printf("callResultfptr is null\n");
- }
- if ( callResultfptr )
- {
- callResultfptr(payload, gun_index);
- }
- 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;
- }
|