123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include <kore/kore.h>
- #include <kore/http.h>
- #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;
- 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);
- ret = ioctl(fdesc, RTC_RD_TIME, &time);
- close(fdesc);
- 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);
- 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];
- char * body;
- buf = kore_buf_alloc(128);
- while(1) {
- ret = http_body_read(req, data, sizeof(data));
- if (ret == -1) {
- kore_buf_free(buf);
- msg = "Error: could not read body data\n";
- http_response(req, 500, msg, strlen(msg));
- 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) {
- 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");
- 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);
- 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);
- fdesc = open("/dev/rtc", O_WRONLY);
- ret = ioctl(fdesc, RTC_SET_TIME, &time);
- close(fdesc);
- if (ret) {
- 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);
- return KORE_RESULT_OK;
- } else {
- http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
- return (KORE_RESULT_OK);
- }
- }
|