|
@@ -21,44 +21,52 @@ page(struct http_request *req)
|
|
|
int handle_time(struct http_request *req) {
|
|
|
int fdesc;
|
|
|
struct rtc_time time;
|
|
|
+ int ret;
|
|
|
+ char *msg;
|
|
|
cJSON *time_json, *json_min, *json_hour, *json_mday,
|
|
|
*json_mon, *json_year, *json_sec, *json_wday;
|
|
|
if(req->method == HTTP_METHOD_GET) {
|
|
|
char *result_string;
|
|
|
time_json = cJSON_CreateObject();
|
|
|
fdesc = open("/dev/rtc", O_RDONLY);
|
|
|
- ioctl(fdesc, RTC_RD_TIME, &time);
|
|
|
+ ret = ioctl(fdesc, RTC_RD_TIME, &time);
|
|
|
close(fdesc);
|
|
|
- json_sec = cJSON_CreateNumber(time.tm_sec);
|
|
|
- json_min = cJSON_CreateNumber(time.tm_min);
|
|
|
- json_hour = cJSON_CreateNumber(time.tm_hour);
|
|
|
- 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);
|
|
|
+ if (!ret) {
|
|
|
+ json_sec = cJSON_CreateNumber(time.tm_sec);
|
|
|
+ json_min = cJSON_CreateNumber(time.tm_min);
|
|
|
+ json_hour = cJSON_CreateNumber(time.tm_hour);
|
|
|
+ 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");
|
|
|
+ http_response(req, 200, result_string, strlen(result_string));
|
|
|
+ return KORE_RESULT_OK;
|
|
|
+ }
|
|
|
cJSON_Delete(time_json);
|
|
|
- http_response_header(req, "Content-Type", "application/json");
|
|
|
- http_response(req, 200, result_string, strlen(result_string));
|
|
|
+ msg = "Error: could not get time from /dev/rtc\n";
|
|
|
+ http_response(req, 200, msg, strlen(msg));
|
|
|
return KORE_RESULT_OK;
|
|
|
} else if (req->method == HTTP_METHOD_POST) {
|
|
|
struct kore_buf * buf;
|
|
|
u_int8_t data[128];
|
|
|
- int ret;
|
|
|
char * body;
|
|
|
buf = kore_buf_alloc(128);
|
|
|
while(1) {
|
|
|
ret = http_body_read(req, data, sizeof(data));
|
|
|
if (ret == -1) {
|
|
|
kore_buf_free(buf);
|
|
|
- http_response(req, 500, NULL, 0);
|
|
|
+ msg = "Error: could not read body data\n";
|
|
|
+ http_response(req, 500, msg, strlen(msg));
|
|
|
return (KORE_RESULT_OK);
|
|
|
}
|
|
|
if (ret == 0) {
|
|
@@ -70,7 +78,8 @@ int handle_time(struct http_request *req) {
|
|
|
kore_buf_free(buf);
|
|
|
time_json = cJSON_Parse(body);
|
|
|
if (time_json == NULL) {
|
|
|
- http_response(req, 500, NULL, 0);
|
|
|
+ msg = "Error: could not parse json\n";
|
|
|
+ http_response(req, 500, msg, strlen(msg));
|
|
|
return (KORE_RESULT_OK);
|
|
|
}
|
|
|
json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
|
|
@@ -92,7 +101,8 @@ int handle_time(struct http_request *req) {
|
|
|
time.tm_wday = json_wday->valueint;
|
|
|
} else {
|
|
|
cJSON_Delete(time_json);
|
|
|
- http_response(req, 500, NULL, 0);
|
|
|
+ msg = "Error: invalid or missing values in json object\n";
|
|
|
+ http_response(req, 500, msg, strlen(msg));
|
|
|
return (KORE_RESULT_OK);
|
|
|
}
|
|
|
cJSON_Delete(time_json);
|
|
@@ -100,7 +110,8 @@ int handle_time(struct http_request *req) {
|
|
|
ret = ioctl(fdesc, RTC_SET_TIME, &time);
|
|
|
close(fdesc);
|
|
|
if (ret) {
|
|
|
- http_response(req, 500, NULL, 0);
|
|
|
+ msg = "Error: Could not set time via /dev/rtc\n";
|
|
|
+ http_response(req, 500, msg, strlen(msg));
|
|
|
return (KORE_RESULT_OK);
|
|
|
}
|
|
|
http_response(req, 200, NULL, 0);
|