jsonrtc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <kore/kore.h>
  2. #include <kore/http.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. #include <linux/rtc.h>
  8. #include <cjson/cJSON.h>
  9. int page(struct http_request *);
  10. int handle_time(struct http_request *);
  11. int
  12. page(struct http_request *req)
  13. {
  14. http_response(req, 200, NULL, 0);
  15. return (KORE_RESULT_OK);
  16. }
  17. int handle_time(struct http_request *req) {
  18. int fdesc;
  19. struct rtc_time time;
  20. int ret;
  21. char *msg;
  22. cJSON *time_json, *json_min, *json_hour, *json_mday,
  23. *json_mon, *json_year, *json_sec, *json_wday;
  24. if(req->method == HTTP_METHOD_GET) {
  25. char *result_string;
  26. time_json = cJSON_CreateObject();
  27. fdesc = open("/dev/rtc", O_RDONLY);
  28. ret = ioctl(fdesc, RTC_RD_TIME, &time);
  29. close(fdesc);
  30. if (!ret) {
  31. json_sec = cJSON_CreateNumber(time.tm_sec);
  32. json_min = cJSON_CreateNumber(time.tm_min);
  33. json_hour = cJSON_CreateNumber(time.tm_hour);
  34. json_mday = cJSON_CreateNumber(time.tm_mday);
  35. json_mon = cJSON_CreateNumber(time.tm_mon);
  36. json_year = cJSON_CreateNumber(time.tm_year);
  37. json_wday = cJSON_CreateNumber(time.tm_wday);
  38. cJSON_AddItemToObject(time_json, "seconds", json_sec);
  39. cJSON_AddItemToObject(time_json, "minutes", json_min);
  40. cJSON_AddItemToObject(time_json, "hour", json_hour);
  41. cJSON_AddItemToObject(time_json, "mday", json_mday);
  42. cJSON_AddItemToObject(time_json, "month", json_mon);
  43. cJSON_AddItemToObject(time_json, "year", json_year);
  44. cJSON_AddItemToObject(time_json, "wday", json_wday);
  45. result_string = cJSON_Print(time_json);
  46. cJSON_Delete(time_json);
  47. http_response_header(req, "Content-Type", "application/json");
  48. http_response(req, 200, result_string, strlen(result_string));
  49. return KORE_RESULT_OK;
  50. }
  51. cJSON_Delete(time_json);
  52. msg = "Error: could not get time from /dev/rtc\n";
  53. http_response(req, 200, msg, strlen(msg));
  54. return KORE_RESULT_OK;
  55. } else if (req->method == HTTP_METHOD_POST) {
  56. struct kore_buf * buf;
  57. u_int8_t data[128];
  58. char * body;
  59. buf = kore_buf_alloc(128);
  60. while(1) {
  61. ret = http_body_read(req, data, sizeof(data));
  62. if (ret == -1) {
  63. kore_buf_free(buf);
  64. msg = "Error: could not read body data\n";
  65. http_response(req, 500, msg, strlen(msg));
  66. return (KORE_RESULT_OK);
  67. }
  68. if (ret == 0) {
  69. break;
  70. }
  71. kore_buf_append(buf,data,ret);
  72. }
  73. body = kore_buf_stringify(buf, NULL);
  74. kore_buf_free(buf);
  75. time_json = cJSON_Parse(body);
  76. if (time_json == NULL) {
  77. msg = "Error: could not parse json\n";
  78. http_response(req, 500, msg, strlen(msg));
  79. return (KORE_RESULT_OK);
  80. }
  81. json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
  82. json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
  83. json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
  84. json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
  85. json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
  86. json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
  87. json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
  88. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  89. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  90. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
  91. time.tm_sec = json_sec->valueint;
  92. time.tm_min = json_min->valueint;
  93. time.tm_hour = json_hour->valueint;
  94. time.tm_mday = json_mday->valueint;
  95. time.tm_mon = json_mon->valueint;
  96. time.tm_year = json_year->valueint;
  97. time.tm_wday = json_wday->valueint;
  98. } else {
  99. cJSON_Delete(time_json);
  100. msg = "Error: invalid or missing values in json object\n";
  101. http_response(req, 500, msg, strlen(msg));
  102. return (KORE_RESULT_OK);
  103. }
  104. cJSON_Delete(time_json);
  105. fdesc = open("/dev/rtc", O_WRONLY);
  106. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  107. close(fdesc);
  108. if (ret) {
  109. msg = "Error: Could not set time via /dev/rtc\n";
  110. http_response(req, 500, msg, strlen(msg));
  111. return (KORE_RESULT_OK);
  112. }
  113. http_response(req, 200, NULL, 0);
  114. return KORE_RESULT_OK;
  115. } else {
  116. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  117. return (KORE_RESULT_OK);
  118. }
  119. }