Преглед изворни кода

2022-11-24/Jerry Wang
[OCPP 1.6]
Action:
1. Improve the ConfigurationKey related functions with json-c method.

File:
1. EVSE/Modularization/ocppfiles/MessageHandler.c
--> Action 1

Jerry Wang пре 2 година
родитељ
комит
e42e3ab09f
1 измењених фајлова са 71 додато и 99 уклоњено
  1. 71 99
      EVSE/Modularization/ocppfiles/MessageHandler.c

+ 71 - 99
EVSE/Modularization/ocppfiles/MessageHandler.c

@@ -9226,69 +9226,60 @@ int sendGetConfigurationConfirmation(char *uuid)
 	DEBUG_INFO("sendGetConfigurationConfirmation...\n");
 
 	MaxKeySupported = atoi((const char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[GetConfigurationMaxKeys].ItemData);
-
-	sprintf(message,"[%d,\"%s\",{\"configurationKey\":["
-			,MESSAGE_TYPE_CALLRESULT
-			,uuid );
-
 	 //configuration key
+	json_object *GetConfiguration = json_object_new_object();
+	json_object *configurationKey = json_object_new_array();
 	for(int idx_sample=0;idx_sample< MaxKeySupported/*43*/;idx_sample++)
 	{
 		if(strcmp((const char *)ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Key, "")!= 0)
 		{
-			if (sentConfigurationNumber == 0)
-			{
-				sprintf(message + strlen(message), "{\"key\":\"%s\",\"readonly\":%s,\"value\":\"%s\"}"
-						, ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Key
-						, atoi((const char *)ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].ReadOnly) == 1 ? "true":"false"
-						, ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Value );
-			}
-			else
-			{
-				sprintf(message + strlen(message), ", {\"key\":\"%s\",\"readonly\":%s,\"value\":\"%s\"}"
-						, ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Key
-						, atoi((const char *)ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].ReadOnly) == 1 ? "true":"false"
-						, ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Value );
-			}
+			json_object *keyValue = json_object_new_object();
+			json_object_object_add(keyValue, "key", json_object_new_string((char*)ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Key));
+			json_object_object_add(keyValue, "readonly", atoi((const char *)ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].ReadOnly) == 1?json_object_new_boolean(1):json_object_new_boolean(0));
+			json_object_object_add(keyValue, "value", json_object_new_string((char*)ShmOCPP16Data->GetConfiguration.ResponseConfigurationKey[idx_sample].Value));
+
+			json_object_array_add(configurationKey, keyValue);
 
 			sentConfigurationNumber = sentConfigurationNumber + 1;
 		}
 	}
 
-
-	sprintf(message + strlen(message), "]");
-
-
+	json_object *unknownKey = json_object_new_array();
 	if(UnknownKeynum != 0)
 	{
-		sprintf(message + strlen(message), ",\"unknownKey\":[");
-		//unkown key
 		for(int idx_sample=0;idx_sample< UnknownKeynum ;idx_sample++)
 		{
-			// json_object *jstring1 = json_object_new_string((const char *)((ShmOCPP16Data->GetConfiguration.ResponseUnknownKey + idx_sample)->Item));
+			DEBUG_INFO("unknown key:%s\n", ShmOCPP16Data->GetConfiguration.ResponseUnknownKey[idx_sample].Item);
+			json_object_array_add(unknownKey, json_object_new_string((char*)ShmOCPP16Data->GetConfiguration.ResponseUnknownKey[idx_sample].Item));
 
-			DEBUG_INFO("unkown key:%s\n", ShmOCPP16Data->GetConfiguration.ResponseUnknownKey[idx_sample].Item);
-
-			if(sentunConfigurationNumber == 0)
-			{
-				sprintf(message + strlen(message), "\"%s\""
-						, ShmOCPP16Data->GetConfiguration.ResponseUnknownKey[idx_sample].Item );
-			}
-			else
-			{
-				sprintf(message + strlen(message), ",\"%s\""
-						, ShmOCPP16Data->GetConfiguration.ResponseUnknownKey[idx_sample].Item );
-			}
 			sentunConfigurationNumber = sentunConfigurationNumber + 1;
 		}
+	}
+
+	if((sentConfigurationNumber>0) && (sentunConfigurationNumber>0))
+	{
+		json_object_object_add(GetConfiguration, "configurationKey", configurationKey);
+		json_object_object_add(GetConfiguration, "unknownKey", unknownKey);
 
-		sprintf(message + strlen(message), "]");
 	}
+	else if(sentConfigurationNumber>0)
+	{
+		json_object_object_add(GetConfiguration, "configurationKey", configurationKey);
+		json_object_put(unknownKey);
+	}
+	else
+	{
+		json_object_object_add(GetConfiguration, "unknownKey", unknownKey);
+		json_object_put(configurationKey);
+	}
+	sprintf(message,"[%d,\"%s\",%s]"
+						,MESSAGE_TYPE_CALLRESULT
+						,uuid
+						,json_object_to_json_string_ext(GetConfiguration, JSON_C_TO_STRING_PLAIN));
 
-	sprintf(message + strlen(message), "}]");
+	json_object_put(GetConfiguration);
 
 	LWS_Send(message);
-
 	return result;
 }
 
@@ -17526,9 +17517,6 @@ int initialConfigurationTable(void)
 	FILE *fp;
 	FILE *outfile;
 	char str[512]={0};
-	char sstr[256]={0};
-	int c = 0;
-	char *loc;
 	int	confVersion = 23;
 
 	DEBUG_INFO("initialConfigurationTable...version: %d\n", confVersion);
@@ -18107,54 +18095,19 @@ int initialConfigurationTable(void)
 		while( fgets (str, ARRAY_SIZE(str), fp)!=NULL )
 		{
 			//DEBUG_INFO("Get Configuration \n");
-
 			if(strlen(str) > 0)
 			{
-				str[strlen(str) - 1] = '\0'; // eat the newline fgets() stores
-				//*************************key*********************************/
-				if(strstr(str, "key") != NULL)
-				{
-					loc = strstr(str, "key");
-					c = 0;
-					memset(sstr ,0, ARRAY_SIZE(sstr) );
-					while ((loc[strlen("key")+3+c] != '\"') && (c < (ARRAY_SIZE(sstr)-1)))
-					{
-						sstr[c] = loc[strlen("key")+3+c];
-						c++;
-					}
-					sstr[c] = '\0';
-					strcpy(keystr,sstr);
-				}
+				json_object *OCPPConfiguration;
+				OCPPConfiguration = json_tokener_parse(str);
 
-				//*************************readonly*********************************/
-				if(strstr(str, "readonly") != NULL)
-				{
-					loc = strstr(str, "readonly");
-					c = 0;
-					memset(sstr ,0, ARRAY_SIZE(sstr) );
-					while ((loc[strlen("readonly")+2+c] != ',') && (c < (ARRAY_SIZE(sstr)-1)))
-					{
-						sstr[c] = loc[strlen("readonly")+2+c];
-						c++;
-					}
-					sstr[c] = '\0';
-					strcpy(readonlystr,sstr);
-				}
+				if(json_object_object_get(OCPPConfiguration, "key") != NULL)
+					strcpy(keystr, json_object_get_string(json_object_object_get(OCPPConfiguration,"key")));
+				if(json_object_object_get(OCPPConfiguration, "readonly") != NULL)
+					strcpy(readonlystr, json_object_get_string(json_object_object_get(OCPPConfiguration,"readonly")));
+				if(json_object_object_get(OCPPConfiguration, "value") != NULL)
+					strcpy(valuestr, json_object_get_string(json_object_object_get(OCPPConfiguration,"value")));
 
-				//*************************value*********************************/
-				if(strstr(str, "value") != NULL)
-				{
-					loc = strstr(str, "value");
-					c = 0;
-					memset(sstr ,0, ARRAY_SIZE(sstr) );
-					while ((loc[strlen("value")+3+c] != '\"') && (c < (ARRAY_SIZE(sstr)-1)))
-					{
-						sstr[c] = loc[strlen("value")+3+c];
-						c++;
-					}
-					sstr[c] = '\0';
-					strcpy(valuestr,sstr);
-				}
+				json_object_put(OCPPConfiguration);
 
 	//			DEBUG_INFO("keystr=%s\n",keystr);
 	//			DEBUG_INFO("readonlystr=%s\n",readonlystr);
@@ -18958,8 +18911,27 @@ void StoreConfigurationTable(void)
 	strcpy((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemName, "DefaultPrice");
 	strcpy((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, "" );
 	*/
+	json_object *tmp;
+	tmp = json_tokener_parse((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
+	if(!is_error(tmp))
+	{
+		DEBUG_INFO("JSON string type\n");
+		json_object *target = json_object_new_object();
+		json_object_object_add(target, "key", json_object_new_string("DefaultPrice"));
+		json_object_object_add(target, "readonly", (ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemAccessibility?json_object_new_boolean(0):json_object_new_boolean(1)));
+		json_object_object_add(target, "value", json_object_new_string((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData));
+
+
 
-	fprintf(outfile,"{\"key\":\"%s\",\"readonly\":%s,\"value\":\"%s\"}\n","DefaultPrice", "false", (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
+	   fprintf(outfile, "%s\n", json_object_to_json_string_ext(target, JSON_C_TO_STRING_PLAIN));
+		json_object_put(target);
+	}
+	else
+	{
+		DEBUG_INFO("Normal string type\n");
+		fprintf(outfile,"{\"key\":\"%s\",\"readonly\":%s,\"value\":\"%s\"}\n","DefaultPrice", "false", (char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData);
+	}
+	json_object_put(tmp);
 
 	// CustomDisplayCostAndPrice
 	/*
@@ -21384,16 +21356,16 @@ int setKeyValue(char *key, char *value)
 
     if(strcmp(key, "DefaultPrice") == 0)
     {
-    	if((ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemAccessibility == 1))
-    	{
-    		strcpy(str, (const char*)value);
-    		sprintf((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, "%s", str);
-    		isSuccess = ConfigurationStatus_Accepted;
-    	}
-    	else
-    	{
-    	    isSuccess = ConfigurationStatus_Rejected;
-    	}
+		if((ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemAccessibility == 1))
+		{
+			strcpy(str, (const char*)value);
+			sprintf((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[DefaultPrice].ItemData, "%s", str);
+			isSuccess = ConfigurationStatus_Accepted;
+		}
+		else
+		{
+			isSuccess = ConfigurationStatus_Rejected;
+		}
     }
 
     if(strcmp(key, "CustomDisplayCostAndPrice") == 0)