Browse Source

implement writing to nvmem on DS1307

Helmut Pozimski 5 years ago
parent
commit
33a63a19c1
1 changed files with 57 additions and 5 deletions
  1. 57 5
      jsonrtc/src/jsonrtc.c

+ 57 - 5
jsonrtc/src/jsonrtc.c

@@ -268,32 +268,84 @@ int detect_device(struct http_request *req) {
 int handle_nvmem(struct http_request *req) {
 	int fdesc, bytes_read;
 	unsigned char buf[56];
-	char result_string[113];
+	char r_string[113];
 	char buf2[3];
 	char * result_object;
+	char * result_string;
 	cJSON *json_response, *json_string;
 	if(req->method == HTTP_METHOD_GET) {
 		fdesc = open("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", O_RDONLY);
 		bytes_read = read(fdesc, &buf, 56);
+		close(fdesc);
 		if(bytes_read == 56) {
-			result_string[112] = '\0';
+			r_string[112] = '\0';
 			for(int i=0; i<56; i++) {
 				if (buf[i] <= 15) {
 					sprintf(buf2, "0%x", buf[i]);
 				} else {
 					sprintf(buf2, "%x", buf[i]);
 				}
-				strcpy(result_string + i*2, buf2);
+				strcpy(r_string + i*2, buf2);
 			}
 			json_response = cJSON_CreateObject();
-			json_string = cJSON_CreateString(result_string);
+			json_string = cJSON_CreateString(r_string);
 			cJSON_AddItemToObject(json_response, "content", json_string);	
 			result_object = cJSON_Print(json_response);
                         cJSON_Delete(json_response);
                         http_response_header(req, "Content-Type", "application/json");
 			http_response(req, 200, result_object, strlen(result_object));
-		}	
+		} else {
+                        result_string = "Error: could not read from nvmem\n";
+                        http_response(req, 500, result_string, strlen(result_string));
+                }
 		return KORE_RESULT_OK;
+	} else if (req->method == HTTP_METHOD_POST) {
+		unsigned char to_write[56];
+		int buf3 = 0;
+		int bytes_written;
+		cJSON *body_json, *input_json;
+		char * input_string;
+		char * body = read_body(req);
+		if (body == NULL) {
+                        result_string = "Error: could not read request body\n";
+                        http_response(req, 500, result_string, strlen(result_string));
+                        return (KORE_RESULT_OK);
+                }
+		body_json = cJSON_Parse(body);
+		input_json = cJSON_GetObjectItemCaseSensitive(body_json, "content");
+		if (cJSON_IsString(input_json)) {
+			input_string = input_json->valuestring;	
+		} else {
+			result_string = "Error: invalid value in json content\n";
+                        http_response(req, 500, result_string, strlen(result_string));
+                        return (KORE_RESULT_OK);
+		}
+		for (int i=0; i<112; i++) {
+			if(i%2 == 0) {
+				buf3 = 0;
+			} else {
+				buf3 = buf3 <<4;
+			}
+			if((input_string[i] >= '0') && (input_string[i] <= '9')) {
+				buf3 += input_string[i] - '0';
+			} else if ((input_string[i] >= 'A') && (input_string[i] <= 'Z')) {
+				buf3 += input_string[i] - 55;
+			}
+			if (i%2 != 0) {
+				to_write[i/2] = (unsigned char) buf3;
+			}
+		}
+		cJSON_Delete(body_json);
+		fdesc = open("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", O_WRONLY);
+		bytes_written = write(fdesc, to_write, 56);
+		close(fdesc);
+		if (bytes_written == 56) {
+			http_response(req, 200, NULL, 0);
+                        return (KORE_RESULT_OK);
+		}
+		result_string = "Error: could not write to nvmem\n";
+		http_response(req, 500, result_string, strlen(result_string));
+		return (KORE_RESULT_OK);
 	} else {
                 http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
                 return (KORE_RESULT_OK);