Browse Source

implemented validation of json input for reading and writing time and date

Helmut Pozimski 5 years ago
parent
commit
1032d6a0ce
1 changed files with 25 additions and 8 deletions
  1. 25 8
      jsonrtc/src/jsonrtc.c

+ 25 - 8
jsonrtc/src/jsonrtc.c

@@ -22,7 +22,7 @@ int handle_time(struct http_request *req) {
 	int fdesc;
 	struct rtc_time time;
 	cJSON *time_json, *json_min, *json_hour, *json_mday,
-		*json_mon, *json_year, *json_sec;
+		*json_mon, *json_year, *json_sec, *json_wday;
 	if(req->method == HTTP_METHOD_GET) {
 		char *result_string;
 		time_json = cJSON_CreateObject();
@@ -35,12 +35,14 @@ int handle_time(struct http_request *req) {
 		json_mday = cJSON_CreateNumber(time.tm_mday);
 		json_mon = cJSON_CreateNumber(time.tm_mon);
 		json_year = cJSON_CreateNumber(time.tm_year);
+		json_wday = cJSON_CreateNumber(time.tm_wday);
 		cJSON_AddItemToObject(time_json, "seconds", json_sec);
 		cJSON_AddItemToObject(time_json, "minutes", json_min);
 		cJSON_AddItemToObject(time_json, "hour", json_hour);
 		cJSON_AddItemToObject(time_json, "mday", json_mday);
 		cJSON_AddItemToObject(time_json, "month", json_mon);
 		cJSON_AddItemToObject(time_json, "year", json_year);
+		cJSON_AddItemToObject(time_json, "wday", json_wday);
 		result_string = cJSON_Print(time_json);
 		cJSON_Delete(time_json);
 		http_response_header(req, "Content-Type", "application/json");
@@ -52,7 +54,6 @@ int handle_time(struct http_request *req) {
 		int ret;
 		char * body;
 		buf = kore_buf_alloc(128);
-		printf("Starting reading of body\n");
 		while(1) {
 			ret = http_body_read(req, data, sizeof(data));
 			if (ret == -1) {
@@ -72,12 +73,28 @@ int handle_time(struct http_request *req) {
 			http_response(req, 500, NULL, 0);
 			return (KORE_RESULT_OK);
 		}
-		time.tm_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds")->valueint;
-		time.tm_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes")->valueint;
-		time.tm_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour")->valueint;
-		time.tm_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday")->valueint;
-		time.tm_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month")->valueint;
-		time.tm_year = cJSON_GetObjectItemCaseSensitive(time_json, "year")->valueint;
+		json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
+		json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
+		json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
+		json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
+		json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
+		json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
+		json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
+		if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) && 
+			cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) && 
+			cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
+			time.tm_sec = json_sec->valueint;
+			time.tm_min = json_min->valueint;
+			time.tm_hour = json_hour->valueint;
+			time.tm_mday = json_mday->valueint;
+			time.tm_mon = json_mon->valueint;
+			time.tm_year = json_year->valueint;
+			time.tm_wday = json_wday->valueint;
+		} else {
+			cJSON_Delete(time_json);
+			http_response(req, 500, NULL, 0);
+                        return (KORE_RESULT_OK);
+		}
 		cJSON_Delete(time_json);
 		fdesc = open("/dev/rtc", O_WRONLY);
 		ret = ioctl(fdesc, RTC_SET_TIME, &time);