jsonrtc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * This file is part of the jsonrtc.
  3. * written in 2018 by Helmut Pozimski.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 2.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <kore/kore.h>
  18. #include <kore/http.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <sys/ioctl.h>
  23. #include <linux/rtc.h>
  24. #include <cjson/cJSON.h>
  25. #define IOCTL_DS13307_ALRM _IOR('p', 0x15, unsigned char)
  26. int page(struct http_request *);
  27. int handle_time(struct http_request *);
  28. int handle_wkalrm(struct http_request *);
  29. int
  30. page(struct http_request *req)
  31. {
  32. http_response(req, 200, NULL, 0);
  33. return (KORE_RESULT_OK);
  34. }
  35. static char * read_body(struct http_request *req) {
  36. int ret;
  37. struct kore_buf * buf;
  38. u_int8_t data[128];
  39. char * body;
  40. buf = kore_buf_alloc(128);
  41. while(1) {
  42. ret = http_body_read(req, data, sizeof(data));
  43. if (ret == -1) {
  44. kore_buf_free(buf);
  45. return NULL;
  46. } else if (ret == 0) {
  47. break;
  48. }
  49. kore_buf_append(buf,data,ret);
  50. }
  51. body = kore_buf_stringify(buf, NULL);
  52. kore_buf_free(buf);
  53. return body;
  54. }
  55. int handle_time(struct http_request *req) {
  56. int fdesc;
  57. struct rtc_time time;
  58. int ret;
  59. char *result_string;
  60. cJSON *time_json, *json_min, *json_hour, *json_mday,
  61. *json_mon, *json_year, *json_sec, *json_wday;
  62. if(req->method == HTTP_METHOD_GET) {
  63. time_json = cJSON_CreateObject();
  64. fdesc = open("/dev/rtc", O_RDONLY);
  65. ret = ioctl(fdesc, RTC_RD_TIME, &time);
  66. close(fdesc);
  67. if (!ret) {
  68. json_sec = cJSON_CreateNumber(time.tm_sec);
  69. json_min = cJSON_CreateNumber(time.tm_min);
  70. json_hour = cJSON_CreateNumber(time.tm_hour);
  71. json_mday = cJSON_CreateNumber(time.tm_mday);
  72. json_mon = cJSON_CreateNumber(time.tm_mon);
  73. json_year = cJSON_CreateNumber(time.tm_year);
  74. json_wday = cJSON_CreateNumber(time.tm_wday);
  75. cJSON_AddItemToObject(time_json, "seconds", json_sec);
  76. cJSON_AddItemToObject(time_json, "minutes", json_min);
  77. cJSON_AddItemToObject(time_json, "hour", json_hour);
  78. cJSON_AddItemToObject(time_json, "mday", json_mday);
  79. cJSON_AddItemToObject(time_json, "month", json_mon);
  80. cJSON_AddItemToObject(time_json, "year", json_year);
  81. cJSON_AddItemToObject(time_json, "wday", json_wday);
  82. result_string = cJSON_Print(time_json);
  83. cJSON_Delete(time_json);
  84. http_response_header(req, "Content-Type", "application/json");
  85. http_response(req, 200, result_string, strlen(result_string));
  86. return KORE_RESULT_OK;
  87. }
  88. cJSON_Delete(time_json);
  89. result_string = "Error: could not get time from /dev/rtc\n";
  90. http_response(req, 500, result_string, strlen(result_string));
  91. return KORE_RESULT_OK;
  92. } else if (req->method == HTTP_METHOD_POST) {
  93. char * body;
  94. body = read_body(req);
  95. if (body == NULL) {
  96. result_string = "Error: could not read request body\n";
  97. http_response(req, 500, result_string, strlen(result_string));
  98. return (KORE_RESULT_OK);
  99. }
  100. time_json = cJSON_Parse(body);
  101. if (time_json == NULL) {
  102. result_string = "Error: could not parse json\n";
  103. http_response(req, 500, result_string, strlen(result_string));
  104. return (KORE_RESULT_OK);
  105. }
  106. json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
  107. json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
  108. json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
  109. json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
  110. json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
  111. json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
  112. json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
  113. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  114. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  115. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
  116. time.tm_sec = json_sec->valueint;
  117. time.tm_min = json_min->valueint;
  118. time.tm_hour = json_hour->valueint;
  119. time.tm_mday = json_mday->valueint;
  120. time.tm_mon = json_mon->valueint;
  121. time.tm_year = json_year->valueint;
  122. time.tm_wday = json_wday->valueint;
  123. } else {
  124. cJSON_Delete(time_json);
  125. result_string = "Error: invalid or missing values in json object\n";
  126. http_response(req, 500, result_string, strlen(result_string));
  127. return (KORE_RESULT_OK);
  128. }
  129. cJSON_Delete(time_json);
  130. fdesc = open("/dev/rtc", O_WRONLY);
  131. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  132. close(fdesc);
  133. if (ret) {
  134. result_string = "Error: Could not set time via /dev/rtc\n";
  135. http_response(req, 500, result_string, strlen(result_string));
  136. return (KORE_RESULT_OK);
  137. }
  138. http_response(req, 200, NULL, 0);
  139. return KORE_RESULT_OK;
  140. } else {
  141. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  142. return (KORE_RESULT_OK);
  143. }
  144. }
  145. int handle_wkalrm(struct http_request *req) {
  146. char *result_string;
  147. struct rtc_wkalrm alarm;
  148. int fdesc, ret;
  149. unsigned char alarm_bit;
  150. cJSON *alarm_json, *json_min, *json_hour, *json_mday,
  151. *json_mon, *json_year, *json_sec, *json_wday,
  152. *json_enabled, *json_pending;
  153. if(req->method == HTTP_METHOD_GET) {
  154. alarm_json = cJSON_CreateObject();
  155. fdesc = open("/dev/rtc", O_RDONLY);
  156. ret = ioctl(fdesc, RTC_WKALM_RD, &alarm);
  157. ret |= ioctl(fdesc, IOCTL_DS13307_ALRM, &alarm_bit);
  158. close(fdesc);
  159. if (!ret) {
  160. json_sec = cJSON_CreateNumber(alarm.time.tm_sec);
  161. json_min = cJSON_CreateNumber(alarm.time.tm_min);
  162. json_hour = cJSON_CreateNumber(alarm.time.tm_hour);
  163. json_mday = cJSON_CreateNumber(alarm.time.tm_mday);
  164. json_mon = cJSON_CreateNumber(alarm.time.tm_mon);
  165. json_year = cJSON_CreateNumber(alarm.time.tm_year);
  166. json_wday = cJSON_CreateNumber(alarm.time.tm_wday);
  167. json_enabled = cJSON_CreateNumber(alarm.enabled);
  168. json_pending = cJSON_CreateNumber(alarm_bit);
  169. cJSON_AddItemToObject(alarm_json, "seconds", json_sec);
  170. cJSON_AddItemToObject(alarm_json, "minutes", json_min);
  171. cJSON_AddItemToObject(alarm_json, "hour", json_hour);
  172. cJSON_AddItemToObject(alarm_json, "mday", json_mday);
  173. cJSON_AddItemToObject(alarm_json, "month", json_mon);
  174. cJSON_AddItemToObject(alarm_json, "year", json_year);
  175. cJSON_AddItemToObject(alarm_json, "wday", json_wday);
  176. cJSON_AddItemToObject(alarm_json, "enabled", json_enabled);
  177. cJSON_AddItemToObject(alarm_json, "pending", json_pending);
  178. result_string = cJSON_Print(alarm_json);
  179. cJSON_Delete(alarm_json);
  180. http_response_header(req, "Content-Type", "application/json");
  181. http_response(req, 200, result_string, strlen(result_string));
  182. return KORE_RESULT_OK;
  183. }
  184. cJSON_Delete(alarm_json);
  185. result_string = "Error: could not get alarm from /dev/rtc\n";
  186. http_response(req, 500, result_string, strlen(result_string));
  187. return KORE_RESULT_OK;
  188. } else if (req->method == HTTP_METHOD_POST) {
  189. char * body = read_body(req);
  190. if (body == NULL) {
  191. result_string = "Error: could not read request body\n";
  192. http_response(req, 500, result_string, strlen(result_string));
  193. return (KORE_RESULT_OK);
  194. }
  195. alarm_json = cJSON_Parse(body);
  196. if (alarm_json == NULL) {
  197. result_string = "Error: could not parse json\n";
  198. http_response(req, 500, result_string, strlen(result_string));
  199. return (KORE_RESULT_OK);
  200. }
  201. json_sec = cJSON_GetObjectItemCaseSensitive(alarm_json, "seconds");
  202. json_min = cJSON_GetObjectItemCaseSensitive(alarm_json, "minutes");
  203. json_hour = cJSON_GetObjectItemCaseSensitive(alarm_json, "hour");
  204. json_mday = cJSON_GetObjectItemCaseSensitive(alarm_json, "mday");
  205. json_mon = cJSON_GetObjectItemCaseSensitive(alarm_json, "month");
  206. json_year = cJSON_GetObjectItemCaseSensitive(alarm_json, "year");
  207. json_wday = cJSON_GetObjectItemCaseSensitive(alarm_json, "wday");
  208. json_enabled = cJSON_GetObjectItemCaseSensitive(alarm_json, "enabled");
  209. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  210. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  211. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday) &&
  212. cJSON_IsNumber(json_enabled)) {
  213. alarm.time.tm_sec = json_sec->valueint;
  214. alarm.time.tm_min = json_min->valueint;
  215. alarm.time.tm_hour = json_hour->valueint;
  216. alarm.time.tm_mday = json_mday->valueint;
  217. alarm.time.tm_mon = json_mon->valueint;
  218. alarm.time.tm_year = json_year->valueint;
  219. alarm.time.tm_wday = json_wday->valueint;
  220. alarm.enabled = json_enabled->valueint;
  221. } else {
  222. cJSON_Delete(alarm_json);
  223. result_string = "Error: invalid or missing values in json object\n";
  224. http_response(req, 500, result_string, strlen(result_string));
  225. return (KORE_RESULT_OK);
  226. }
  227. cJSON_Delete(alarm_json);
  228. fdesc = open("/dev/rtc", O_WRONLY);
  229. ret = ioctl(fdesc, RTC_WKALM_SET, &alarm);
  230. close(fdesc);
  231. if (ret) {
  232. result_string = "Error: Could not set alarm via /dev/rtc\n";
  233. http_response(req, 500, result_string, strlen(result_string));
  234. return (KORE_RESULT_OK);
  235. }
  236. http_response(req, 200, NULL, 0);
  237. return KORE_RESULT_OK;
  238. } else {
  239. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  240. return (KORE_RESULT_OK);
  241. }
  242. }