#include #include #include #include #include #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 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: 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++)); gun_index = atoi(*(arr+i++)); CallResultHandler(Action, Payload, gun_index); free(testdup); #if 0 //test ( receive -send ) char difftest[34]={0}; time_t seconds; seconds = time(NULL); for(int i=3; i<37; i++) { difftest[i-3]= UniqueId[i]; } printf("receive - sent =%d (seconds)\n",(int)(seconds)-atoi(difftest)); printf("\n messageid=%s\n",difftest); printf("\n send time=%d (seconds)\n",atoi(difftest)); printf("\n recieve time=%d (seconds)\n",seconds); if(((int)(seconds)-atoi(difftest)) > 1) { printf("receive - sent =%d (seconds)\n",(int)(seconds)-atoi(difftest)); printf("\n messageid=%s\n",difftest); printf("\n send time=%d (seconds)\n",atoi(difftest)); printf("\n recieve time=%d (seconds)\n",seconds); DEBUG_INFO("receive - sent =%d (seconds)\n",(int)(seconds)-atoi(difftest)); DEBUG_INFO("\n messageid=%s\n",difftest); DEBUG_INFO("\n send time=%d (seconds)\n",atoi(difftest)); DEBUG_INFO("\n recieve time=%d (seconds)\n",seconds); } #endif } 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; }