|
@@ -1,11 +1,95 @@
|
|
|
#include <kore/kore.h>
|
|
|
#include <kore/http.h>
|
|
|
|
|
|
-int page(struct http_request *);
|
|
|
+#include <fcntl.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <string.h>
|
|
|
+#include <sys/ioctl.h>
|
|
|
+#include <linux/rtc.h>
|
|
|
+#include <cjson/cJSON.h>
|
|
|
+
|
|
|
|
|
|
+int page(struct http_request *);
|
|
|
+int handle_time(struct http_request *);
|
|
|
int
|
|
|
page(struct http_request *req)
|
|
|
{
|
|
|
http_response(req, 200, NULL, 0);
|
|
|
return (KORE_RESULT_OK);
|
|
|
}
|
|
|
+
|
|
|
+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;
|
|
|
+ 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);
|
|
|
+ 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);
|
|
|
+ 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);
|
|
|
+ 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;
|
|
|
+ } 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);
|
|
|
+ printf("Starting reading of body\n");
|
|
|
+ while(1) {
|
|
|
+ ret = http_body_read(req, data, sizeof(data));
|
|
|
+ if (ret == -1) {
|
|
|
+ kore_buf_free(buf);
|
|
|
+ http_response(req, 500, NULL, 0);
|
|
|
+ return (KORE_RESULT_OK);
|
|
|
+ }
|
|
|
+ if (ret == 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ kore_buf_append(buf,data,ret);
|
|
|
+ }
|
|
|
+ body = kore_buf_stringify(buf, NULL);
|
|
|
+ kore_buf_free(buf);
|
|
|
+ time_json = cJSON_Parse(body);
|
|
|
+ if (time_json == NULL) {
|
|
|
+ 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;
|
|
|
+ cJSON_Delete(time_json);
|
|
|
+ fdesc = open("/dev/rtc", O_WRONLY);
|
|
|
+ ret = ioctl(fdesc, RTC_SET_TIME, &time);
|
|
|
+ close(fdesc);
|
|
|
+ if (ret) {
|
|
|
+ http_response(req, 500, NULL, 0);
|
|
|
+ return (KORE_RESULT_OK);
|
|
|
+ }
|
|
|
+ http_response(req, 200, NULL, 0);
|
|
|
+ return KORE_RESULT_OK;
|
|
|
+ } else {
|
|
|
+ http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
|
|
|
+ return (KORE_RESULT_OK);
|
|
|
+ }
|
|
|
+}
|