jsonrtc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. cJSON *time_json, *json_min, *json_hour, *json_mday,
  21. *json_mon, *json_year, *json_sec, *json_wday;
  22. if(req->method == HTTP_METHOD_GET) {
  23. char *result_string;
  24. time_json = cJSON_CreateObject();
  25. fdesc = open("/dev/rtc", O_RDONLY);
  26. ioctl(fdesc, RTC_RD_TIME, &time);
  27. close(fdesc);
  28. json_sec = cJSON_CreateNumber(time.tm_sec);
  29. json_min = cJSON_CreateNumber(time.tm_min);
  30. json_hour = cJSON_CreateNumber(time.tm_hour);
  31. json_mday = cJSON_CreateNumber(time.tm_mday);
  32. json_mon = cJSON_CreateNumber(time.tm_mon);
  33. json_year = cJSON_CreateNumber(time.tm_year);
  34. json_wday = cJSON_CreateNumber(time.tm_wday);
  35. cJSON_AddItemToObject(time_json, "seconds", json_sec);
  36. cJSON_AddItemToObject(time_json, "minutes", json_min);
  37. cJSON_AddItemToObject(time_json, "hour", json_hour);
  38. cJSON_AddItemToObject(time_json, "mday", json_mday);
  39. cJSON_AddItemToObject(time_json, "month", json_mon);
  40. cJSON_AddItemToObject(time_json, "year", json_year);
  41. cJSON_AddItemToObject(time_json, "wday", json_wday);
  42. result_string = cJSON_Print(time_json);
  43. cJSON_Delete(time_json);
  44. http_response_header(req, "Content-Type", "application/json");
  45. http_response(req, 200, result_string, strlen(result_string));
  46. return KORE_RESULT_OK;
  47. } else if (req->method == HTTP_METHOD_POST) {
  48. struct kore_buf * buf;
  49. u_int8_t data[128];
  50. int ret;
  51. char * body;
  52. buf = kore_buf_alloc(128);
  53. while(1) {
  54. ret = http_body_read(req, data, sizeof(data));
  55. if (ret == -1) {
  56. kore_buf_free(buf);
  57. http_response(req, 500, NULL, 0);
  58. return (KORE_RESULT_OK);
  59. }
  60. if (ret == 0) {
  61. break;
  62. }
  63. kore_buf_append(buf,data,ret);
  64. }
  65. body = kore_buf_stringify(buf, NULL);
  66. kore_buf_free(buf);
  67. time_json = cJSON_Parse(body);
  68. if (time_json == NULL) {
  69. http_response(req, 500, NULL, 0);
  70. return (KORE_RESULT_OK);
  71. }
  72. json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
  73. json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
  74. json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
  75. json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
  76. json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
  77. json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
  78. json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
  79. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  80. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  81. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
  82. time.tm_sec = json_sec->valueint;
  83. time.tm_min = json_min->valueint;
  84. time.tm_hour = json_hour->valueint;
  85. time.tm_mday = json_mday->valueint;
  86. time.tm_mon = json_mon->valueint;
  87. time.tm_year = json_year->valueint;
  88. time.tm_wday = json_wday->valueint;
  89. } else {
  90. cJSON_Delete(time_json);
  91. http_response(req, 500, NULL, 0);
  92. return (KORE_RESULT_OK);
  93. }
  94. cJSON_Delete(time_json);
  95. fdesc = open("/dev/rtc", O_WRONLY);
  96. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  97. close(fdesc);
  98. if (ret) {
  99. http_response(req, 500, NULL, 0);
  100. return (KORE_RESULT_OK);
  101. }
  102. http_response(req, 200, NULL, 0);
  103. return KORE_RESULT_OK;
  104. } else {
  105. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  106. return (KORE_RESULT_OK);
  107. }
  108. }