jsonrtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 detect_device(struct http_request *);
  30. int handle_nvmem(struct http_request *);
  31. int
  32. page(struct http_request *req)
  33. {
  34. http_response(req, 200, NULL, 0);
  35. return (KORE_RESULT_OK);
  36. }
  37. static char * read_body(struct http_request *req) {
  38. int ret;
  39. struct kore_buf * buf;
  40. u_int8_t data[128];
  41. char * body;
  42. buf = kore_buf_alloc(128);
  43. while(1) {
  44. ret = http_body_read(req, data, sizeof(data));
  45. if (ret == -1) {
  46. kore_buf_free(buf);
  47. return NULL;
  48. } else if (ret == 0) {
  49. break;
  50. }
  51. kore_buf_append(buf,data,ret);
  52. }
  53. body = kore_buf_stringify(buf, NULL);
  54. kore_buf_free(buf);
  55. return body;
  56. }
  57. int handle_time(struct http_request *req) {
  58. int fdesc;
  59. struct rtc_time time;
  60. int ret;
  61. char *result_string;
  62. cJSON *time_json, *json_min, *json_hour, *json_mday,
  63. *json_mon, *json_year, *json_sec, *json_wday;
  64. if(req->method == HTTP_METHOD_GET) {
  65. time_json = cJSON_CreateObject();
  66. fdesc = open("/dev/rtc", O_RDONLY);
  67. ret = ioctl(fdesc, RTC_RD_TIME, &time);
  68. close(fdesc);
  69. if (!ret) {
  70. json_sec = cJSON_CreateNumber(time.tm_sec);
  71. json_min = cJSON_CreateNumber(time.tm_min);
  72. json_hour = cJSON_CreateNumber(time.tm_hour);
  73. json_mday = cJSON_CreateNumber(time.tm_mday);
  74. json_mon = cJSON_CreateNumber(time.tm_mon);
  75. json_year = cJSON_CreateNumber(time.tm_year);
  76. json_wday = cJSON_CreateNumber(time.tm_wday);
  77. cJSON_AddItemToObject(time_json, "seconds", json_sec);
  78. cJSON_AddItemToObject(time_json, "minutes", json_min);
  79. cJSON_AddItemToObject(time_json, "hour", json_hour);
  80. cJSON_AddItemToObject(time_json, "mday", json_mday);
  81. cJSON_AddItemToObject(time_json, "month", json_mon);
  82. cJSON_AddItemToObject(time_json, "year", json_year);
  83. cJSON_AddItemToObject(time_json, "wday", json_wday);
  84. result_string = cJSON_Print(time_json);
  85. cJSON_Delete(time_json);
  86. http_response_header(req, "Content-Type", "application/json");
  87. http_response(req, 200, result_string, strlen(result_string));
  88. return KORE_RESULT_OK;
  89. }
  90. cJSON_Delete(time_json);
  91. result_string = "Error: could not get time from /dev/rtc\n";
  92. http_response(req, 500, result_string, strlen(result_string));
  93. return KORE_RESULT_OK;
  94. } else if (req->method == HTTP_METHOD_POST) {
  95. char * body;
  96. body = read_body(req);
  97. if (body == NULL) {
  98. result_string = "Error: could not read request body\n";
  99. http_response(req, 500, result_string, strlen(result_string));
  100. return (KORE_RESULT_OK);
  101. }
  102. time_json = cJSON_Parse(body);
  103. if (time_json == NULL) {
  104. result_string = "Error: could not parse json\n";
  105. http_response(req, 500, result_string, strlen(result_string));
  106. return (KORE_RESULT_OK);
  107. }
  108. json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
  109. json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
  110. json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
  111. json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
  112. json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
  113. json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
  114. json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
  115. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  116. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  117. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
  118. time.tm_sec = json_sec->valueint;
  119. time.tm_min = json_min->valueint;
  120. time.tm_hour = json_hour->valueint;
  121. time.tm_mday = json_mday->valueint;
  122. time.tm_mon = json_mon->valueint;
  123. time.tm_year = json_year->valueint;
  124. time.tm_wday = json_wday->valueint;
  125. } else {
  126. cJSON_Delete(time_json);
  127. result_string = "Error: invalid or missing values in json object\n";
  128. http_response(req, 500, result_string, strlen(result_string));
  129. return (KORE_RESULT_OK);
  130. }
  131. cJSON_Delete(time_json);
  132. fdesc = open("/dev/rtc", O_WRONLY);
  133. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  134. close(fdesc);
  135. if (ret) {
  136. result_string = "Error: Could not set time via /dev/rtc\n";
  137. http_response(req, 500, result_string, strlen(result_string));
  138. return (KORE_RESULT_OK);
  139. }
  140. http_response(req, 200, NULL, 0);
  141. return KORE_RESULT_OK;
  142. } else {
  143. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  144. return (KORE_RESULT_OK);
  145. }
  146. }
  147. int handle_wkalrm(struct http_request *req) {
  148. char *result_string;
  149. struct rtc_wkalrm alarm;
  150. int fdesc, ret;
  151. unsigned char alarm_bit;
  152. cJSON *alarm_json, *json_min, *json_hour, *json_mday,
  153. *json_mon, *json_year, *json_sec, *json_wday,
  154. *json_enabled, *json_pending;
  155. if(req->method == HTTP_METHOD_GET) {
  156. alarm_json = cJSON_CreateObject();
  157. fdesc = open("/dev/rtc", O_RDONLY);
  158. ret = ioctl(fdesc, RTC_WKALM_RD, &alarm);
  159. ret |= ioctl(fdesc, IOCTL_DS13307_ALRM, &alarm_bit);
  160. close(fdesc);
  161. if (!ret) {
  162. json_sec = cJSON_CreateNumber(alarm.time.tm_sec);
  163. json_min = cJSON_CreateNumber(alarm.time.tm_min);
  164. json_hour = cJSON_CreateNumber(alarm.time.tm_hour);
  165. json_mday = cJSON_CreateNumber(alarm.time.tm_mday);
  166. json_mon = cJSON_CreateNumber(alarm.time.tm_mon);
  167. json_year = cJSON_CreateNumber(alarm.time.tm_year);
  168. json_wday = cJSON_CreateNumber(alarm.time.tm_wday);
  169. json_enabled = cJSON_CreateNumber(alarm.enabled);
  170. json_pending = cJSON_CreateNumber(alarm_bit);
  171. cJSON_AddItemToObject(alarm_json, "seconds", json_sec);
  172. cJSON_AddItemToObject(alarm_json, "minutes", json_min);
  173. cJSON_AddItemToObject(alarm_json, "hour", json_hour);
  174. cJSON_AddItemToObject(alarm_json, "mday", json_mday);
  175. cJSON_AddItemToObject(alarm_json, "month", json_mon);
  176. cJSON_AddItemToObject(alarm_json, "year", json_year);
  177. cJSON_AddItemToObject(alarm_json, "wday", json_wday);
  178. cJSON_AddItemToObject(alarm_json, "enabled", json_enabled);
  179. cJSON_AddItemToObject(alarm_json, "pending", json_pending);
  180. result_string = cJSON_Print(alarm_json);
  181. cJSON_Delete(alarm_json);
  182. http_response_header(req, "Content-Type", "application/json");
  183. http_response(req, 200, result_string, strlen(result_string));
  184. return KORE_RESULT_OK;
  185. }
  186. cJSON_Delete(alarm_json);
  187. result_string = "Error: could not get alarm from /dev/rtc\n";
  188. http_response(req, 500, result_string, strlen(result_string));
  189. return KORE_RESULT_OK;
  190. } else if (req->method == HTTP_METHOD_POST) {
  191. char * body = read_body(req);
  192. if (body == NULL) {
  193. result_string = "Error: could not read request body\n";
  194. http_response(req, 500, result_string, strlen(result_string));
  195. return (KORE_RESULT_OK);
  196. }
  197. alarm_json = cJSON_Parse(body);
  198. if (alarm_json == NULL) {
  199. result_string = "Error: could not parse json\n";
  200. http_response(req, 500, result_string, strlen(result_string));
  201. return (KORE_RESULT_OK);
  202. }
  203. json_sec = cJSON_GetObjectItemCaseSensitive(alarm_json, "seconds");
  204. json_min = cJSON_GetObjectItemCaseSensitive(alarm_json, "minutes");
  205. json_hour = cJSON_GetObjectItemCaseSensitive(alarm_json, "hour");
  206. json_mday = cJSON_GetObjectItemCaseSensitive(alarm_json, "mday");
  207. json_mon = cJSON_GetObjectItemCaseSensitive(alarm_json, "month");
  208. json_year = cJSON_GetObjectItemCaseSensitive(alarm_json, "year");
  209. json_wday = cJSON_GetObjectItemCaseSensitive(alarm_json, "wday");
  210. json_enabled = cJSON_GetObjectItemCaseSensitive(alarm_json, "enabled");
  211. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  212. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  213. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday) &&
  214. cJSON_IsNumber(json_enabled)) {
  215. alarm.time.tm_sec = json_sec->valueint;
  216. alarm.time.tm_min = json_min->valueint;
  217. alarm.time.tm_hour = json_hour->valueint;
  218. alarm.time.tm_mday = json_mday->valueint;
  219. alarm.time.tm_mon = json_mon->valueint;
  220. alarm.time.tm_year = json_year->valueint;
  221. alarm.time.tm_wday = json_wday->valueint;
  222. alarm.enabled = json_enabled->valueint;
  223. } else {
  224. cJSON_Delete(alarm_json);
  225. result_string = "Error: invalid or missing values in json object\n";
  226. http_response(req, 500, result_string, strlen(result_string));
  227. return (KORE_RESULT_OK);
  228. }
  229. cJSON_Delete(alarm_json);
  230. fdesc = open("/dev/rtc", O_WRONLY);
  231. ret = ioctl(fdesc, RTC_WKALM_SET, &alarm);
  232. close(fdesc);
  233. if (ret) {
  234. result_string = "Error: Could not set alarm via /dev/rtc\n";
  235. http_response(req, 500, result_string, strlen(result_string));
  236. return (KORE_RESULT_OK);
  237. }
  238. http_response(req, 200, NULL, 0);
  239. return KORE_RESULT_OK;
  240. } else {
  241. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  242. return (KORE_RESULT_OK);
  243. }
  244. }
  245. int detect_device(struct http_request *req) {
  246. if(req->method == HTTP_METHOD_GET) {
  247. if (access("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", F_OK) != -1) {
  248. http_response(req, 200, "DS1307", 6);
  249. } else {
  250. http_response(req, 200, "DS1337", 6);
  251. }
  252. return KORE_RESULT_OK;
  253. } else {
  254. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  255. return (KORE_RESULT_OK);
  256. }
  257. }
  258. int handle_nvmem(struct http_request *req) {
  259. int fdesc, bytes_read;
  260. unsigned char buf[56];
  261. char result_string[113];
  262. char buf2[3];
  263. char * result_object;
  264. cJSON *json_response, *json_string;
  265. if(req->method == HTTP_METHOD_GET) {
  266. fdesc = open("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", O_RDONLY);
  267. bytes_read = read(fdesc, &buf, 56);
  268. if(bytes_read == 56) {
  269. result_string[112] = '\0';
  270. for(int i=0; i<56; i++) {
  271. if (buf[i] <= 15) {
  272. sprintf(buf2, "0%x", buf[i]);
  273. } else {
  274. sprintf(buf2, "%x", buf[i]);
  275. }
  276. strcpy(result_string + i*2, buf2);
  277. }
  278. json_response = cJSON_CreateObject();
  279. json_string = cJSON_CreateString(result_string);
  280. cJSON_AddItemToObject(json_response, "content", json_string);
  281. result_object = cJSON_Print(json_response);
  282. cJSON_Delete(json_response);
  283. http_response_header(req, "Content-Type", "application/json");
  284. http_response(req, 200, result_object, strlen(result_object));
  285. }
  286. return KORE_RESULT_OK;
  287. } else {
  288. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  289. return (KORE_RESULT_OK);
  290. }
  291. }