Эх сурвалжийг харах

[Improve][Modularization][OcppBackend]

2020.10.20 / Folus Wen

Actions:
1. Align clock meterValue sent out after receive first hearbeat message response.
2. Backend URL paring auto detect 80/443 port without input.

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 жил өмнө
parent
commit
194e0c1f1d

+ 51 - 15
EVSE/Modularization/ocppfiles/MessageHandler.c

@@ -2099,9 +2099,10 @@ void checkCompositeSchedule(uint8_t connectorId, uint32_t durationReq, struct St
 		}
 	}
 
-	DEBUG_INFO("Composite schedule start: %s\n", compositeProfile->ChargingSchedule.StartSchedule);
-	DEBUG_INFO("Composite schedule duration: %d\n", compositeProfile->ChargingSchedule.Duration);
-	DEBUG_INFO("Composite rate unit: %s\n", compositeProfile->ChargingSchedule.ChargingRateUnit);
+	DEBUG_INFO("Connector-%d composite schedule ready.\n", connectorId);
+	DEBUG_INFO("Schedule start: %s\n", compositeProfile->ChargingSchedule.StartSchedule);
+	DEBUG_INFO("Schedule duration: %d\n", compositeProfile->ChargingSchedule.Duration);
+	DEBUG_INFO("Rate unit: %s\n", compositeProfile->ChargingSchedule.ChargingRateUnit);
 	for(int idxPeriod=0;idxPeriod<ARRAY_SIZE(compositeProfile->ChargingSchedule.ChargingSchedulePeriod);idxPeriod++)
 	{
 		if(compositeProfile->ChargingSchedule.ChargingSchedulePeriod[idxPeriod].StartPeriod >= 0)
@@ -5642,7 +5643,10 @@ int sendMeterValuesRequest(int gun_index, ReadingContext dataType)
 	   (ShmOCPP16Data->MeterValues[gun_index].TransactionId != -1))
 		queue_operation(QUEUE_OPERATION_ADD, guid, queuedata );
 	else
-		LWS_Send(queuedata +2);
+	{
+		if(FirstHeartBeat)
+			LWS_Send(queuedata +2);
+	}
 
 	return result;
 }
@@ -15176,32 +15180,64 @@ int GetOcppServerURL()
 	memset(OcppProtocol, 0, ARRAY_SIZE(OcppProtocol));
 	memset(OcppHost, 0, ARRAY_SIZE(OcppHost));
 	memset(OcppTempPath, 0, ARRAY_SIZE(OcppTempPath));
+	struct yuarel url;
+	char urlStr[512];
 
 	if((ShmSysConfigAndInfo->SysConfig.OcppServerURL != NULL) && (strcmp((const char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0) )
 	{
-#if 1
-		sscanf((const char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL,
+		/*sscanf((const char *)ShmSysConfigAndInfo->SysConfig.OcppServerURL,
 						"%[^:]:%*2[/]%[^:]:%i/%[a-zA-Z0-9._/-]",
-						OcppProtocol, OcppHost, &OcppPort, OcppTempPath);
-#endif
-		result = TRUE;
-		goto End;
+						OcppProtocol, OcppHost, &OcppPort, OcppTempPath);*/
+
+		memcpy(urlStr, ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
+		if(yuarel_parse(&url, urlStr) != -1)
+		{
+			sprintf(OcppProtocol, "%s", url.scheme);
+			sprintf(OcppHost, "%s", url.host);
+			sprintf(OcppTempPath, "%s", url.path);
+			if(url.port == 0)
+			{
+				if(strcmp(OcppProtocol, "wss") == 0)
+					OcppPort = 443;
+				else
+					OcppPort = 80;
+			}
+			else
+				OcppPort = url.port;
+
+			result = TRUE;
+		}
 	}
 	else if((ShmOCPP16Data->OcppServerURL != NULL) && (strcmp((const char *)ShmOCPP16Data->OcppServerURL,"") != 0))
 	{
-		sscanf((const char *)ShmOCPP16Data->OcppServerURL,
+		/*sscanf((const char *)ShmOCPP16Data->OcppServerURL,
 								"%[^:]:%*2[/]%[^:]:%i/%[a-zA-Z0-9._/-]",
-								OcppProtocol, OcppHost, &OcppPort, OcppTempPath);
-		result = TRUE;
-		goto End;
+								OcppProtocol, OcppHost, &OcppPort, OcppTempPath);*/
+
+		memcpy(urlStr, ShmOCPP16Data->OcppServerURL, ARRAY_SIZE(ShmOCPP16Data->OcppServerURL));
+		if(yuarel_parse(&url, urlStr) != -1)
+		{
+			sprintf(OcppProtocol, "%s", url.scheme);
+			sprintf(OcppHost, "%s", url.host);
+			sprintf(OcppTempPath, "%s", url.path);
+			if(url.port == 0)
+			{
+				if(strcmp(OcppProtocol, "wss") == 0)
+					OcppPort = 443;
+				else
+					OcppPort = 80;
+			}
+			else
+				OcppPort = url.port;
 
+			result = TRUE;
+		}
 	}
 	else
 	{
 		strcpy(OcppHost,"");
 	}
 
-End:
 	return result;
 }
 

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

@@ -485,7 +485,7 @@ void* ConnectWsServer(void* data)  //int ConnectWsServer()
 	wsi_client = lws_client_connect_via_info(&ConnInfo);
 	if (!wsi_client)
 	{
-		DEBUG_ERROR("lws_client_connect_via_info NG");
+		DEBUG_ERROR("lws_client_connect_via_info NG\n");
 		goto end;
 	}
 

+ 20 - 0
EVSE/Modularization/ocppfiles/Module_OcppBackend.h

@@ -77,6 +77,26 @@
 #define QUEUE_MESSAGE_LENGTH		3584
 
 
+struct yuarel {
+	char *scheme; /* scheme, without ":" and "//" */
+	char *username; /* username, default: NULL */
+	char *password; /* password, default: NULL */
+	char *host; /* hostname or IP address */
+	int port; /* port, default: 0 */
+	char *path; /* path, without leading "/", default: NULL */
+	char *query; /* query, default: NULL */
+	char *fragment; /* fragment, default: NULL */
+};
+
+/* A struct to hold the query string parameter values. */
+struct yuarel_param {
+	char *key;
+	char *val;
+};
+extern int yuarel_parse(struct yuarel *url, char *url_str);
+extern int yuarel_split_path(char *path, char **parts, int max_parts);
+extern int yuarel_parse_query(char *query, char delimiter, struct yuarel_param *params, int max_params);
+
 extern void CheckSystemValue(void);
 extern int FirstHeartBeatResponse(void);
 extern void OCPP_get_TableAuthlocalAllData(void);

+ 273 - 0
EVSE/Modularization/ocppfiles/common.c

@@ -272,3 +272,276 @@ char* random_uuid(char* buf)
 
 	return 0;
 }
+
+//===========================================================
+// URL parsing function
+//===========================================================
+/**
+ * Parse a non null terminated string into an integer.
+ *
+ * str: the string containing the number.
+ * len: Number of characters to parse.
+ */
+static inline int
+natoi(const char *str, size_t len)
+{
+	int i, r = 0;
+	for (i = 0; i < len; i++) {
+		r *= 10;
+		r += str[i] - '0';
+	}
+
+	return r;
+}
+
+/**
+ * Check if a URL is relative (no scheme and hostname).
+ *
+ * url: the string containing the URL to check.
+ *
+ * Returns 1 if relative, otherwise 0.
+ */
+static inline int
+is_relative(const char *url)
+{
+	return (*url == '/') ? 1 : 0;
+}
+
+/**
+ * Parse the scheme of a URL by inserting a null terminator after the scheme.
+ *
+ * str: the string containing the URL to parse. Will be modified.
+ *
+ * Returns a pointer to the hostname on success, otherwise NULL.
+ */
+static inline char *
+parse_scheme(char *str)
+{
+	char *s;
+
+	/* If not found or first in string, return error */
+	s = strchr(str, ':');
+	if (s == NULL || s == str) {
+		return NULL;
+	}
+
+	/* If not followed by two slashes, return error */
+	if (s[1] == '\0' || s[1] != '/' || s[2] == '\0' || s[2] != '/') {
+		return NULL;
+	}
+
+	*s = '\0'; // Replace ':' with NULL
+
+	return s + 3;
+}
+
+/**
+ * Find a character in a string, replace it with '\0' and return the next
+ * character in the string.
+ *
+ * str: the string to search in.
+ * find: the character to search for.
+ *
+ * Returns a pointer to the character after the one to search for. If not
+ * found, NULL is returned.
+ */
+static inline char *
+find_and_terminate(char *str, char find)
+{
+	str = strchr(str, find);
+	if (NULL == str) {
+		return NULL;
+	}
+
+	*str = '\0';
+	return str + 1;
+}
+
+/* Yes, the following functions could be implemented as preprocessor macros
+     instead of inline functions, but I think that this approach will be more
+     clean in this case. */
+static inline char *
+find_fragment(char *str)
+{
+	return find_and_terminate(str, '#');
+}
+
+static inline char *
+find_query(char *str)
+{
+	return find_and_terminate(str, '?');
+}
+
+static inline char *
+find_path(char *str)
+{
+	return find_and_terminate(str, '/');
+}
+
+/**
+ * Parse a URL string to a struct.
+ *
+ * url: pointer to the struct where to store the parsed URL parts.
+ * u:   the string containing the URL to be parsed.
+ *
+ * Returns 0 on success, otherwise -1.
+ */
+int
+yuarel_parse(struct yuarel *url, char *u)
+{
+	if (NULL == url || NULL == u) {
+		return -1;
+	}
+
+	memset(url, 0, sizeof (struct yuarel));
+
+	/* (Fragment) */
+	url->fragment = find_fragment(u);
+
+	/* (Query) */
+	url->query = find_query(u);
+
+	/* Relative URL? Parse scheme and hostname */
+	if (!is_relative(u)) {
+		/* Scheme */
+		url->scheme = u;
+		u = parse_scheme(u);
+		if (u == NULL) {
+			return -1;
+		}
+
+		/* Host */
+		if ('\0' == *u) {
+			return -1;
+		}
+		url->host = u;
+
+		/* (Path) */
+		url->path = find_path(u);
+
+		/* (Credentials) */
+		u = strchr(url->host, '@');
+		if (NULL != u) {
+			/* Missing credentials? */
+			if (u == url->host) {
+				return -1;
+			}
+
+			url->username = url->host;
+			url->host = u + 1;
+			*u = '\0';
+
+			u = strchr(url->username, ':');
+			if (NULL == u) {
+				return -1;
+			}
+
+			url->password = u + 1;
+			*u = '\0';
+		}
+
+		/* Missing hostname? */
+		if ('\0' == *url->host) {
+			return -1;
+		}
+
+		/* (Port) */
+		u = strchr(url->host, ':');
+		if (NULL != u && (NULL == url->path || u < url->path)) {
+			*(u++) = '\0';
+			if ('\0' == *u) {
+				return -1;
+			}
+
+			if (url->path) {
+				url->port = natoi(u, url->path - u - 1);
+			} else {
+				url->port = atoi(u);
+			}
+		}
+
+		/* Missing hostname? */
+		if ('\0' == *url->host) {
+			return -1;
+		}
+	} else {
+		/* (Path) */
+		url->path = find_path(u);
+	}
+
+	return 0;
+}
+
+/**
+ * Split a path into several strings.
+ *
+ * No data is copied, the slashed are used as null terminators and then
+ * pointers to each path part will be stored in **parts. Double slashes will be
+ * treated as one.
+ *
+ * path: the path to split.
+ * parts: a pointer to an array of (char *) where to store the result.
+ * max_parts: max number of parts to parse.
+ */
+int
+yuarel_split_path(char *path, char **parts, int max_parts)
+{
+	int i = 0;
+
+	if (NULL == path || '\0' == *path) {
+		return -1;
+	}
+
+	do {
+		/* Forward to after slashes */
+		while (*path == '/') path++;
+
+		if ('\0' == *path) {
+			break;
+		}
+
+		parts[i++] = path;
+
+		path = strchr(path, '/');
+		if (NULL == path) {
+			break;
+		}
+
+		*(path++) = '\0';
+	} while (i < max_parts);
+
+	return i;
+}
+
+int
+yuarel_parse_query(char *query, char delimiter, struct yuarel_param *params, int max_params)
+{
+	int i = 0;
+
+	if (NULL == query || '\0' == *query) {
+		return -1;
+	}
+
+	params[i++].key = query;
+	while (i < max_params && NULL != (query = strchr(query, delimiter))) {
+		*query = '\0';
+		params[i].key = ++query;
+		params[i].val = NULL;
+
+		/* Go back and split previous param */
+		if (i > 0) {
+			if ((params[i - 1].val = strchr(params[i - 1].key, '=')) != NULL) {
+				*(params[i - 1].val)++ = '\0';
+			}
+		}
+		i++;
+	}
+
+	/* Go back and split last param */
+	if ((params[i - 1].val = strchr(params[i - 1].key, '=')) != NULL) {
+		*(params[i - 1].val)++ = '\0';
+	}
+
+	return i;
+}
+

+ 1 - 0
EVSE/Projects/AW-CCS/Apps/main.c

@@ -3110,6 +3110,7 @@ int main(void)
 						ShmOCPP16Data->SpMsg.bits.AuthorizeReq = OFF;
 						ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
 						ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
+						ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
 					}
 
 					//if time up, clear CCS MSG count