jsonrtc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 handle_wkalrm(struct http_request *);
  12. int
  13. page(struct http_request *req)
  14. {
  15. http_response(req, 200, NULL, 0);
  16. return (KORE_RESULT_OK);
  17. }
  18. int handle_time(struct http_request *req) {
  19. int fdesc;
  20. struct rtc_time time;
  21. int ret;
  22. char *msg;
  23. cJSON *time_json, *json_min, *json_hour, *json_mday,
  24. *json_mon, *json_year, *json_sec, *json_wday;
  25. if(req->method == HTTP_METHOD_GET) {
  26. char *result_string;
  27. time_json = cJSON_CreateObject();
  28. fdesc = open("/dev/rtc", O_RDONLY);
  29. ret = ioctl(fdesc, RTC_RD_TIME, &time);
  30. close(fdesc);
  31. if (!ret) {
  32. json_sec = cJSON_CreateNumber(time.tm_sec);
  33. json_min = cJSON_CreateNumber(time.tm_min);
  34. json_hour = cJSON_CreateNumber(time.tm_hour);
  35. json_mday = cJSON_CreateNumber(time.tm_mday);
  36. json_mon = cJSON_CreateNumber(time.tm_mon);
  37. json_year = cJSON_CreateNumber(time.tm_year);
  38. json_wday = cJSON_CreateNumber(time.tm_wday);
  39. cJSON_AddItemToObject(time_json, "seconds", json_sec);
  40. cJSON_AddItemToObject(time_json, "minutes", json_min);
  41. cJSON_AddItemToObject(time_json, "hour", json_hour);
  42. cJSON_AddItemToObject(time_json, "mday", json_mday);
  43. cJSON_AddItemToObject(time_json, "month", json_mon);
  44. cJSON_AddItemToObject(time_json, "year", json_year);
  45. cJSON_AddItemToObject(time_json, "wday", json_wday);
  46. result_string = cJSON_Print(time_json);
  47. cJSON_Delete(time_json);
  48. http_response_header(req, "Content-Type", "application/json");
  49. http_response(req, 200, result_string, strlen(result_string));
  50. return KORE_RESULT_OK;
  51. }
  52. cJSON_Delete(time_json);
  53. msg = "Error: could not get time from /dev/rtc\n";
  54. http_response(req, 500, msg, strlen(msg));
  55. return KORE_RESULT_OK;
  56. } else if (req->method == HTTP_METHOD_POST) {
  57. struct kore_buf * buf;
  58. u_int8_t data[128];
  59. char * body;
  60. buf = kore_buf_alloc(128);
  61. while(1) {
  62. ret = http_body_read(req, data, sizeof(data));
  63. if (ret == -1) {
  64. kore_buf_free(buf);
  65. msg = "Error: could not read body data\n";
  66. http_response(req, 500, msg, strlen(msg));
  67. return (KORE_RESULT_OK);
  68. }
  69. if (ret == 0) {
  70. break;
  71. }
  72. kore_buf_append(buf,data,ret);
  73. }
  74. body = kore_buf_stringify(buf, NULL);
  75. kore_buf_free(buf);
  76. time_json = cJSON_Parse(body);
  77. if (time_json == NULL) {
  78. msg = "Error: could not parse json\n";
  79. http_response(req, 500, msg, strlen(msg));
  80. return (KORE_RESULT_OK);
  81. }
  82. json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
  83. json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
  84. json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
  85. json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
  86. json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
  87. json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
  88. json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
  89. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  90. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  91. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
  92. time.tm_sec = json_sec->valueint;
  93. time.tm_min = json_min->valueint;
  94. time.tm_hour = json_hour->valueint;
  95. time.tm_mday = json_mday->valueint;
  96. time.tm_mon = json_mon->valueint;
  97. time.tm_year = json_year->valueint;
  98. time.tm_wday = json_wday->valueint;
  99. } else {
  100. cJSON_Delete(time_json);
  101. msg = "Error: invalid or missing values in json object\n";
  102. http_response(req, 500, msg, strlen(msg));
  103. return (KORE_RESULT_OK);
  104. }
  105. cJSON_Delete(time_json);
  106. fdesc = open("/dev/rtc", O_WRONLY);
  107. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  108. close(fdesc);
  109. if (ret) {
  110. msg = "Error: Could not set time via /dev/rtc\n";
  111. http_response(req, 500, msg, strlen(msg));
  112. return (KORE_RESULT_OK);
  113. }
  114. http_response(req, 200, NULL, 0);
  115. return KORE_RESULT_OK;
  116. } else {
  117. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  118. return (KORE_RESULT_OK);
  119. }
  120. }
  121. int handle_wkalrm(struct http_request *req) {
  122. char *result_string;
  123. struct rtc_wkalrm alarm;
  124. int fdesc, ret;
  125. cJSON *alarm_json, *json_min, *json_hour, *json_mday,
  126. *json_mon, *json_year, *json_sec, *json_wday,
  127. *json_enabled, *json_pending;
  128. if(req->method == HTTP_METHOD_GET) {
  129. alarm_json = cJSON_CreateObject();
  130. fdesc = open("/dev/rtc", O_RDONLY);
  131. ret = ioctl(fdesc, RTC_WKALM_RD, &alarm);
  132. close(fdesc);
  133. if (!ret) {
  134. json_sec = cJSON_CreateNumber(alarm.time.tm_sec);
  135. json_min = cJSON_CreateNumber(alarm.time.tm_min);
  136. json_hour = cJSON_CreateNumber(alarm.time.tm_hour);
  137. json_mday = cJSON_CreateNumber(alarm.time.tm_mday);
  138. json_mon = cJSON_CreateNumber(alarm.time.tm_mon);
  139. json_year = cJSON_CreateNumber(alarm.time.tm_year);
  140. json_wday = cJSON_CreateNumber(alarm.time.tm_wday);
  141. json_enabled = cJSON_CreateNumber(alarm.enabled);
  142. json_pending = cJSON_CreateNumber(alarm.pending);
  143. cJSON_AddItemToObject(alarm_json, "seconds", json_sec);
  144. cJSON_AddItemToObject(alarm_json, "minutes", json_min);
  145. cJSON_AddItemToObject(alarm_json, "hour", json_hour);
  146. cJSON_AddItemToObject(alarm_json, "mday", json_mday);
  147. cJSON_AddItemToObject(alarm_json, "month", json_mon);
  148. cJSON_AddItemToObject(alarm_json, "year", json_year);
  149. cJSON_AddItemToObject(alarm_json, "wday", json_wday);
  150. cJSON_AddItemToObject(alarm_json, "enabled", json_enabled);
  151. cJSON_AddItemToObject(alarm_json, "pending", json_pending);
  152. result_string = cJSON_Print(alarm_json);
  153. cJSON_Delete(alarm_json);
  154. http_response_header(req, "Content-Type", "application/json");
  155. http_response(req, 200, result_string, strlen(result_string));
  156. return KORE_RESULT_OK;
  157. }
  158. cJSON_Delete(alarm_json);
  159. result_string = "Error: could not get alarm from /dev/rtc\n";
  160. http_response(req, 500, result_string, strlen(result_string));
  161. return KORE_RESULT_OK;
  162. } else {
  163. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  164. return (KORE_RESULT_OK);
  165. }
  166. }