Browse Source

[Improve][Modularization][Module_OcppBackend]

2020.08.28 / Folus Wen

Actions:
1. EVSE/Modularization/ocppfiles/Module_OcppBackend.c disable Module_BackupPH monitor and start.
2. EVSE/Modularization/ocppfiles/Module_OcppBackend.c implemen not valid message parsing prevent process crash.
   handleCancelReservationRequest()
   handleChangeAvailabilityRequest()
   handleChangeConfigurationRequest()
   handleDataTransferRequest()
   handleGetCompositeScheduleRequest()
   handleRemoteStopTransactionRequest()
   handleUnlockConnectorRequest()
   UpdateFirmwareProcess()

Files:
1. As follow commit history

Image version: D0.00.XX.XXXX.XX
Image checksum: XXXXXXXX

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 4 years ago
parent
commit
62bb715643

+ 120 - 116
EVSE/Modularization/ocppfiles/MessageHandler.c

@@ -5916,7 +5916,7 @@ int handleCancelReservationRequest(char *uuid, char *payload)
 	mtrace();
 	int result = FAIL;
     int gunNO = 0;
-	int reservationIdInt =0;
+	int reservationIdInt = -1;
 	char comfirmstr[20];
     DEBUG_INFO("handleCancelReservationRequest...\n");
 
@@ -5925,104 +5925,107 @@ int handleCancelReservationRequest(char *uuid, char *payload)
 	if(!is_error(CancelReservation))
 	{
 		// Required data
-		reservationIdInt = json_object_get_int(json_object_object_get(CancelReservation, "reservationId"));
+		if(json_object_object_get(CancelReservation, "reservationId") != NULL)
+			reservationIdInt = json_object_get_int(json_object_object_get(CancelReservation, "reservationId"));
 	}
 	json_object_put(CancelReservation);
-
+	DEBUG_INFO("reservationIdInt = %d\n", reservationIdInt);
 
 	memset(comfirmstr, 0, ARRAY_SIZE(comfirmstr));
 	sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Rejected]);
 
-	//0: Booting, 1: idle, 2: authorizing, 3: preparing, 4: charging, 5: terminating, 6: alarm, 7: fault, 8: Reserved, 9: maintain
-	//check Transaction active
-	//J: CHAdeMO   U: CCS1 combo   E: CCS2 combo  G: GBT DC
-	for (int index = 0; index < CHAdeMO_QUANTITY; index++)
+	if(reservationIdInt != -1)
 	{
-		if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].ReservationId == reservationIdInt)
+		//0: Booting, 1: idle, 2: authorizing, 3: preparing, 4: charging, 5: terminating, 6: alarm, 7: fault, 8: Reserved, 9: maintain
+		//check Transaction active
+		//J: CHAdeMO   U: CCS1 combo   E: CCS2 combo  G: GBT DC
+		for (int index = 0; index < CHAdeMO_QUANTITY; index++)
 		{
-			sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
-			sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
-
-			if(gunType[2] == 'J')
+			if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].ReservationId == reservationIdInt)
 			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index + 1;
-			}
-			else
-			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index;
-			}
+				sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
+				sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
 
-			ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
-			goto end;
+				if(gunType[2] == 'J')
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index + 1;
+				}
+				else
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index;
+				}
+
+				ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
+				goto end;
+			}
 		}
-	}
 
-	for (int index = 0; index < CCS_QUANTITY; index++)
-	{
-		if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].ReservationId == reservationIdInt)
+		for (int index = 0; index < CCS_QUANTITY; index++)
 		{
-			sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
-			sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
-			if((gunType[2] == 'U') || (gunType[2] == 'E'))
-			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index + 1;
-			}
-			else
+			if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].ReservationId == reservationIdInt)
 			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index;
-			}
+				sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
+				sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
+				if((gunType[2] == 'U') || (gunType[2] == 'E'))
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index + 1;
+				}
+				else
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index;
+				}
 
-			ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
-			goto end;
+				ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
+				goto end;
+			}
 		}
-	}
 
 
-	for (int index = 0; index < GB_QUANTITY; index++)
-	{
-		if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].ReservationId == reservationIdInt)
+		for (int index = 0; index < GB_QUANTITY; index++)
 		{
-			sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
-			sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
-			if(gunType[2] == 'G')
+			if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].ReservationId == reservationIdInt)
 			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index + 1;
-			}
-			else
-			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index;
-			}
+				sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
+				sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
+				if(gunType[2] == 'G')
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index + 1;
+				}
+				else
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index;
+				}
 
-			ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
-			goto end;
+				ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
+				goto end;
+			}
 		}
-	}
 
 
-	for (int index = 0; index < AC_QUANTITY; index++)
-	{
-		if (ShmSysConfigAndInfo->SysInfo.AcChargingData[index].ReservationId == reservationIdInt)
+		for (int index = 0; index < AC_QUANTITY; index++)
 		{
-			sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
-			sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
-
-			if(ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D')
+			if (ShmSysConfigAndInfo->SysInfo.AcChargingData[index].ReservationId == reservationIdInt)
 			{
-				gunNO = 1; //ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index ;
-			}
-			else
-			{
-				gunNO = ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index;
-			}
+				sprintf(comfirmstr, "%s", CancelReservationStatusStr[CancelReservationStatus_Accepted] );
+				sprintf((char *)ShmOCPP16Data->CancelReservation[ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index].ResponseStatus, "%s", comfirmstr );
 
-			ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
-			goto end;
+				if(ShmSysConfigAndInfo->SysConfig.ModelName[0]=='D')
+				{
+					gunNO = 1; //ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index ;
+				}
+				else
+				{
+					gunNO = ShmSysConfigAndInfo->SysInfo.AcChargingData[index].Index;
+				}
+
+				ShmOCPP16Data->CsMsg.bits[gunNO].CancelReservationReq = 1;
+				goto end;
+			}
 		}
 	}
 
 	//The reservationId does NOT match the reservationId
 	sendCancelReservationConfirmation(uuid, comfirmstr);
-
 end:
 	// Fill in ocpp packet uuid
 	strcpy((char *)ShmOCPP16Data->CancelReservation[gunNO].guid, uuid);
@@ -6047,8 +6050,11 @@ int handleChangeAvailabilityRequest(char *uuid, char *payload)
 	if(!is_error(ChangeAvailability))
 	{
 		// Required data
-		gunIndex = json_object_get_int(json_object_object_get(ChangeAvailability, "connectorId"));
-		sprintf((char*)typeStr, "%s", json_object_get_string(json_object_object_get(ChangeAvailability, "type")));
+		if(json_object_object_get(ChangeAvailability, "connectorId") != NULL)
+			gunIndex = json_object_get_int(json_object_object_get(ChangeAvailability, "connectorId"));
+
+		if(json_object_object_get(ChangeAvailability, "type") != NULL)
+			sprintf((char*)typeStr, "%s", json_object_get_string(json_object_object_get(ChangeAvailability, "type")));
 	}
 	json_object_put(ChangeAvailability);
 
@@ -6465,8 +6471,11 @@ int handleChangeConfigurationRequest(char *uuid, char *payload)
 	if(!is_error(ChangeConfiguration))
 	{
 		// Required data
-		sprintf((char*)keystr, "%s", json_object_get_string(json_object_object_get(ChangeConfiguration, "key")));
-		sprintf((char*)valuestr, "%s", json_object_get_string(json_object_object_get(ChangeConfiguration, "value")));
+		if(json_object_object_get(ChangeConfiguration, "key") != NULL)
+			sprintf((char*)keystr, "%s", json_object_get_string(json_object_object_get(ChangeConfiguration, "key")));
+
+		if(json_object_object_get(ChangeConfiguration, "value") != NULL)
+			sprintf((char*)valuestr, "%s", json_object_get_string(json_object_object_get(ChangeConfiguration, "value")));
 	}
 	json_object_put(ChangeConfiguration);
 
@@ -7216,7 +7225,8 @@ int handleDataTransferRequest(char *uuid, char *payload)
 		if(!is_error(DataTransfer))
 		{
 			// Required data
-			sprintf((char*)tempvendorId, "%s", json_object_get_string(json_object_object_get(DataTransfer, "vendorId")));
+			if(json_object_object_get(DataTransfer, "vendorId") != NULL)
+				sprintf((char*)tempvendorId, "%s", json_object_get_string(json_object_object_get(DataTransfer, "vendorId")));
 
 			// Optional data
 			if(json_object_object_get(DataTransfer, "messageId") != NULL)
@@ -7298,8 +7308,15 @@ int handleGetCompositeScheduleRequest(char *uuid, char *payload)
 	if(!is_error(GetCompositeSchedule))
 	{
 		// Required data
-		connectorIdInt= json_object_get_int(json_object_object_get(GetCompositeSchedule, "connectorId"));
-		durationInt = json_object_get_int(json_object_object_get(GetCompositeSchedule, "duration"));
+		if(json_object_object_get(GetCompositeSchedule, "connectorId") != NULL)
+			connectorIdInt = json_object_get_int(json_object_object_get(GetCompositeSchedule, "connectorId"));
+		else
+			connectorIdInt = -1;
+
+		if(json_object_object_get(GetCompositeSchedule, "duration"))
+			durationInt = json_object_get_int(json_object_object_get(GetCompositeSchedule, "duration"));
+		else
+			durationInt = 86400;
 
 		// Optional data
 		if(json_object_object_get(GetCompositeSchedule, "chargingRateUnit") != NULL)
@@ -8138,13 +8155,11 @@ int handleRemoteStopTransactionRequest(char *uuid, char *payload)
 	int GunNO = 0;
 	int tempIndex = 0;
 	int transactionIdInt=0;
-	int transactionIdIsNULL= FALSE;
-	char sstr[16]={ 0 },sstrtemp[50]={ 0 };//sstr[200]={ 0 };
-	int c = 0;
-	char *loc;
+	int transactionIdIsNULL= TRUE;
 	char comfirmstr[20];
+	json_object *RemoteStopTransaction;
 
-//[2,"ff522854-0dea-436e-87ba-23a229269994","RemoteStopTransaction",{"transactionId":1373618380}]
+	//[2,"ff522854-0dea-436e-87ba-23a229269994","RemoteStopTransaction",{"transactionId":1373618380}]
 	DEBUG_INFO("handleRemoteStopTransactionRequest...\n");
 
 	if(server_pending == TRUE)
@@ -8152,31 +8167,20 @@ int handleRemoteStopTransactionRequest(char *uuid, char *payload)
 		return 0;
 	}
 
-	strcpy(sstrtemp, stringtrimspace(payload));
-
-	c=0;
-	loc = strstr(sstrtemp, "transactionId");
-
-	if(loc == NULL)
-	{
-		transactionIdIsNULL= TRUE;
-	}
-	else
+	RemoteStopTransaction = json_tokener_parse(payload);
+	if(!is_error(RemoteStopTransaction))
 	{
-		memset(sstr ,0, ARRAY_SIZE(sstr) );
-		while ((loc[strlen("transactionId")+2+c] != '}') && (loc[strlen("transactionId")+2+c] != ','))
+		if(json_object_object_get(RemoteStopTransaction, "transactionId") != NULL)
 		{
-			sstr[c] = loc[strlen("transactionId")+2+c];
-			//printf("i=%d sstr=%c\n",c, sstr[c]);
-			c++;
+			transactionIdInt = json_object_get_int(json_object_object_get(RemoteStopTransaction, "transactionId"));
+			transactionIdIsNULL = FALSE;
 		}
-		sstr[c] = '\0';
-		transactionIdInt = atoi(sstr);
 	}
+	json_object_put(RemoteStopTransaction);
+
 
 	if(transactionIdIsNULL == FALSE)
 	{
-
 		for(int gun_index=0;gun_index < gunTotalNumber;gun_index++)
 	    {
 			if(ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId == transactionIdInt)
@@ -8292,7 +8296,9 @@ int handleRemoteStopTransactionRequest(char *uuid, char *payload)
 		{
 			strcpy(comfirmstr, RemoteStartStopStatusStr[RemoteStartStopStatus_Rejected]);
 		}
-	  }
+	}
+	else
+		strcpy(comfirmstr, RemoteStartStopStatusStr[RemoteStartStopStatus_Rejected]);
 
 	sendRemoteStopTransactionConfirmation(uuid, comfirmstr);
 	return result;
@@ -10433,29 +10439,24 @@ int handleUnlockConnectorRequest(char *uuid, char *payload)
 {
 	mtrace();
 	int result = FAIL;
-	char sstr[6]={0},sstrtemp[50]={ 0 };
 	int connectorIdInt =0;
 	char comfirmstr[20]={0};
-	int c = 0;
 	int tempIndex = 0;
-	char *loc;
+	json_object *UnlockConnector;
 
-//[2,"ba1cbd49-2a76-493a-8f76-fa23e7606532","UnlockConnector",{"connectorId":1}]
+	//[2,"ba1cbd49-2a76-493a-8f76-fa23e7606532","UnlockConnector",{"connectorId":1}]
 	DEBUG_INFO("handleUnlockConnectorRequest ...\n");
-
-	strcpy(sstrtemp, stringtrimspace(payload));
-	c = 0;
-	loc = strstr(sstrtemp, "connectorId");
-	memset(sstr ,0, ARRAY_SIZE(sstr) );
-	while (loc[strlen("connectorId")+2+c] != '}')
+	UnlockConnector = json_tokener_parse(payload);
+	if(!is_error(UnlockConnector))
 	{
-		sstr[c] = loc[strlen("connectorId")+2+c];
-		//printf("i=%d sstr=%c\n",c, sstr[c]);
-		c++;
+		if(json_object_object_get(UnlockConnector, "connectorId") != NULL)
+		{
+			connectorIdInt = json_object_get_int(json_object_object_get(UnlockConnector, "connectorId"));
+		}
 	}
-	sstr[c] = '\0';
-	connectorIdInt = atoi(sstr);
-	DEBUG_INFO("\n unlock connectorIdInt=%d\n",connectorIdInt);
+	json_object_put(UnlockConnector);
+
+	DEBUG_INFO("Unlock connectorIdInt = %d\n",connectorIdInt);
 
 	if(gunTotalNumber == 0)
 	{
@@ -10607,8 +10608,11 @@ void *UpdateFirmwareProcess(void *data)
 	if(!is_error(UpdateFirmware))
 	{
 		// Required data
-		sprintf((char*)locationstr, "%s", json_object_get_string(json_object_object_get(UpdateFirmware, "location")));
-		sprintf((char*)retrieveDatestr, "%s", json_object_get_string(json_object_object_get(UpdateFirmware, "retrieveDate")));
+		if(json_object_object_get(UpdateFirmware, "location") != NULL)
+			sprintf((char*)locationstr, "%s", json_object_get_string(json_object_object_get(UpdateFirmware, "location")));
+
+		if(json_object_object_get(UpdateFirmware, "retrieveDate") != NULL)
+			sprintf((char*)retrieveDatestr, "%s", json_object_get_string(json_object_object_get(UpdateFirmware, "retrieveDate")));
 
 		// Optional data
 		if(json_object_object_get(UpdateFirmware, "retries"))

+ 2 - 1
EVSE/Modularization/ocppfiles/Module_OcppBackend.c

@@ -1198,11 +1198,12 @@ void* processWatchdog()
 			startTimeDog = time((time_t*)NULL);
 		}
 		
+		/*
 		if(system("pidof -s Module_PhBackend > /dev/null") != 0)
 		{
 			DEBUG_INFO("Module_PhBackend not running, restart it.\r\n");
 			system("/root/Module_PhBackend &");
-		}
+		}*/
 
 		sleep(1);
 	}