jsonrtc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_header(req, "Access-Control-Allow-Origin", "*");
  88. http_response(req, 200, result_string, strlen(result_string));
  89. return KORE_RESULT_OK;
  90. }
  91. cJSON_Delete(time_json);
  92. result_string = "Error: could not get time from /dev/rtc\n";
  93. http_response(req, 500, result_string, strlen(result_string));
  94. return KORE_RESULT_OK;
  95. } else if (req->method == HTTP_METHOD_POST) {
  96. char * body;
  97. body = read_body(req);
  98. if (body == NULL) {
  99. result_string = "Error: could not read request body\n";
  100. http_response(req, 500, result_string, strlen(result_string));
  101. return (KORE_RESULT_OK);
  102. }
  103. time_json = cJSON_Parse(body);
  104. if (time_json == NULL) {
  105. result_string = "Error: could not parse json\n";
  106. http_response(req, 500, result_string, strlen(result_string));
  107. return (KORE_RESULT_OK);
  108. }
  109. json_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds");
  110. json_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes");
  111. json_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour");
  112. json_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday");
  113. json_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month");
  114. json_year = cJSON_GetObjectItemCaseSensitive(time_json, "year");
  115. json_wday = cJSON_GetObjectItemCaseSensitive(time_json, "wday");
  116. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  117. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  118. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday)) {
  119. time.tm_sec = json_sec->valueint;
  120. time.tm_min = json_min->valueint;
  121. time.tm_hour = json_hour->valueint;
  122. time.tm_mday = json_mday->valueint;
  123. time.tm_mon = json_mon->valueint;
  124. time.tm_year = json_year->valueint;
  125. time.tm_wday = json_wday->valueint;
  126. } else {
  127. cJSON_Delete(time_json);
  128. result_string = "Error: invalid or missing values in json object\n";
  129. http_response(req, 500, result_string, strlen(result_string));
  130. return (KORE_RESULT_OK);
  131. }
  132. cJSON_Delete(time_json);
  133. fdesc = open("/dev/rtc", O_WRONLY);
  134. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  135. close(fdesc);
  136. if (ret) {
  137. result_string = "Error: Could not set time via /dev/rtc\n";
  138. http_response(req, 500, result_string, strlen(result_string));
  139. return (KORE_RESULT_OK);
  140. }
  141. http_response(req, 200, NULL, 0);
  142. return KORE_RESULT_OK;
  143. } else {
  144. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  145. return (KORE_RESULT_OK);
  146. }
  147. }
  148. int handle_wkalrm(struct http_request *req) {
  149. char *result_string;
  150. struct rtc_wkalrm alarm;
  151. int fdesc, ret;
  152. unsigned char alarm_bit;
  153. cJSON *alarm_json, *json_min, *json_hour, *json_mday,
  154. *json_mon, *json_year, *json_sec, *json_wday,
  155. *json_enabled, *json_pending;
  156. if(req->method == HTTP_METHOD_GET) {
  157. alarm_json = cJSON_CreateObject();
  158. fdesc = open("/dev/rtc", O_RDONLY);
  159. ret = ioctl(fdesc, RTC_WKALM_RD, &alarm);
  160. ret |= ioctl(fdesc, IOCTL_DS13307_ALRM, &alarm_bit);
  161. close(fdesc);
  162. if (!ret) {
  163. json_sec = cJSON_CreateNumber(alarm.time.tm_sec);
  164. json_min = cJSON_CreateNumber(alarm.time.tm_min);
  165. json_hour = cJSON_CreateNumber(alarm.time.tm_hour);
  166. json_mday = cJSON_CreateNumber(alarm.time.tm_mday);
  167. json_mon = cJSON_CreateNumber(alarm.time.tm_mon);
  168. json_year = cJSON_CreateNumber(alarm.time.tm_year);
  169. json_wday = cJSON_CreateNumber(alarm.time.tm_wday);
  170. json_enabled = cJSON_CreateNumber(alarm.enabled);
  171. json_pending = cJSON_CreateNumber(alarm_bit);
  172. cJSON_AddItemToObject(alarm_json, "seconds", json_sec);
  173. cJSON_AddItemToObject(alarm_json, "minutes", json_min);
  174. cJSON_AddItemToObject(alarm_json, "hour", json_hour);
  175. cJSON_AddItemToObject(alarm_json, "mday", json_mday);
  176. cJSON_AddItemToObject(alarm_json, "month", json_mon);
  177. cJSON_AddItemToObject(alarm_json, "year", json_year);
  178. cJSON_AddItemToObject(alarm_json, "wday", json_wday);
  179. cJSON_AddItemToObject(alarm_json, "enabled", json_enabled);
  180. cJSON_AddItemToObject(alarm_json, "pending", json_pending);
  181. result_string = cJSON_Print(alarm_json);
  182. cJSON_Delete(alarm_json);
  183. http_response_header(req, "Content-Type", "application/json");
  184. http_response_header(req, "Access-Control-Allow-Origin", "*");
  185. http_response(req, 200, result_string, strlen(result_string));
  186. return KORE_RESULT_OK;
  187. }
  188. cJSON_Delete(alarm_json);
  189. result_string = "Error: could not get alarm from /dev/rtc\n";
  190. http_response(req, 500, result_string, strlen(result_string));
  191. return KORE_RESULT_OK;
  192. } else if (req->method == HTTP_METHOD_POST) {
  193. char * body = read_body(req);
  194. if (body == NULL) {
  195. result_string = "Error: could not read request body\n";
  196. http_response(req, 500, result_string, strlen(result_string));
  197. return (KORE_RESULT_OK);
  198. }
  199. alarm_json = cJSON_Parse(body);
  200. if (alarm_json == NULL) {
  201. result_string = "Error: could not parse json\n";
  202. http_response(req, 500, result_string, strlen(result_string));
  203. return (KORE_RESULT_OK);
  204. }
  205. json_sec = cJSON_GetObjectItemCaseSensitive(alarm_json, "seconds");
  206. json_min = cJSON_GetObjectItemCaseSensitive(alarm_json, "minutes");
  207. json_hour = cJSON_GetObjectItemCaseSensitive(alarm_json, "hour");
  208. json_mday = cJSON_GetObjectItemCaseSensitive(alarm_json, "mday");
  209. json_mon = cJSON_GetObjectItemCaseSensitive(alarm_json, "month");
  210. json_year = cJSON_GetObjectItemCaseSensitive(alarm_json, "year");
  211. json_wday = cJSON_GetObjectItemCaseSensitive(alarm_json, "wday");
  212. json_enabled = cJSON_GetObjectItemCaseSensitive(alarm_json, "enabled");
  213. if (cJSON_IsNumber(json_sec) && cJSON_IsNumber(json_min) && cJSON_IsNumber(json_hour) &&
  214. cJSON_IsNumber(json_mday) && cJSON_IsNumber(json_mon) &&
  215. cJSON_IsNumber(json_year) && cJSON_IsNumber(json_wday) &&
  216. cJSON_IsNumber(json_enabled)) {
  217. alarm.time.tm_sec = json_sec->valueint;
  218. alarm.time.tm_min = json_min->valueint;
  219. alarm.time.tm_hour = json_hour->valueint;
  220. alarm.time.tm_mday = json_mday->valueint;
  221. alarm.time.tm_mon = json_mon->valueint;
  222. alarm.time.tm_year = json_year->valueint;
  223. alarm.time.tm_wday = json_wday->valueint;
  224. alarm.enabled = json_enabled->valueint;
  225. } else {
  226. cJSON_Delete(alarm_json);
  227. result_string = "Error: invalid or missing values in json object\n";
  228. http_response(req, 500, result_string, strlen(result_string));
  229. return (KORE_RESULT_OK);
  230. }
  231. cJSON_Delete(alarm_json);
  232. fdesc = open("/dev/rtc", O_WRONLY);
  233. ret = ioctl(fdesc, RTC_WKALM_SET, &alarm);
  234. close(fdesc);
  235. if (ret) {
  236. result_string = "Error: Could not set alarm via /dev/rtc\n";
  237. http_response(req, 500, result_string, strlen(result_string));
  238. return (KORE_RESULT_OK);
  239. }
  240. http_response(req, 200, NULL, 0);
  241. return KORE_RESULT_OK;
  242. } else {
  243. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  244. return (KORE_RESULT_OK);
  245. }
  246. }
  247. int detect_device(struct http_request *req) {
  248. if(req->method == HTTP_METHOD_GET) {
  249. if (access("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", F_OK) != -1) {
  250. http_response(req, 200, "DS1307", 6);
  251. } else {
  252. http_response(req, 200, "DS1337", 6);
  253. }
  254. return KORE_RESULT_OK;
  255. } else {
  256. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  257. return (KORE_RESULT_OK);
  258. }
  259. }
  260. int handle_nvmem(struct http_request *req) {
  261. int fdesc, bytes_read;
  262. unsigned char buf[56];
  263. char r_string[113];
  264. char buf2[3];
  265. char * result_object;
  266. char * result_string;
  267. cJSON *json_response, *json_string;
  268. if(req->method == HTTP_METHOD_GET) {
  269. fdesc = open("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", O_RDONLY);
  270. bytes_read = read(fdesc, &buf, 56);
  271. close(fdesc);
  272. if(bytes_read == 56) {
  273. r_string[112] = '\0';
  274. for(int i=0; i<56; i++) {
  275. if (buf[i] <= 15) {
  276. sprintf(buf2, "0%x", buf[i]);
  277. } else {
  278. sprintf(buf2, "%x", buf[i]);
  279. }
  280. strcpy(r_string + i*2, buf2);
  281. }
  282. json_response = cJSON_CreateObject();
  283. json_string = cJSON_CreateString(r_string);
  284. cJSON_AddItemToObject(json_response, "content", json_string);
  285. result_object = cJSON_Print(json_response);
  286. cJSON_Delete(json_response);
  287. http_response_header(req, "Content-Type", "application/json");
  288. http_response_header(req, "Access-Control-Allow-Origin", "*");
  289. http_response(req, 200, result_object, strlen(result_object));
  290. } else {
  291. result_string = "Error: could not read from nvmem\n";
  292. http_response(req, 500, result_string, strlen(result_string));
  293. }
  294. return KORE_RESULT_OK;
  295. } else if (req->method == HTTP_METHOD_POST) {
  296. unsigned char to_write[56];
  297. int buf3 = 0;
  298. int bytes_written;
  299. cJSON *body_json, *input_json;
  300. char * input_string;
  301. char * body = read_body(req);
  302. if (body == NULL) {
  303. result_string = "Error: could not read request body\n";
  304. http_response(req, 500, result_string, strlen(result_string));
  305. return (KORE_RESULT_OK);
  306. }
  307. body_json = cJSON_Parse(body);
  308. input_json = cJSON_GetObjectItemCaseSensitive(body_json, "content");
  309. if (cJSON_IsString(input_json)) {
  310. input_string = input_json->valuestring;
  311. } else {
  312. result_string = "Error: invalid value in json content\n";
  313. http_response(req, 500, result_string, strlen(result_string));
  314. return (KORE_RESULT_OK);
  315. }
  316. for (int i=0; i<112; i++) {
  317. if(i%2 == 0) {
  318. buf3 = 0;
  319. } else {
  320. buf3 = buf3 <<4;
  321. }
  322. if((input_string[i] >= '0') && (input_string[i] <= '9')) {
  323. buf3 += input_string[i] - '0';
  324. } else if ((input_string[i] >= 'A') && (input_string[i] <= 'Z')) {
  325. buf3 += input_string[i] - 55;
  326. }
  327. if (i%2 != 0) {
  328. to_write[i/2] = (unsigned char) buf3;
  329. }
  330. }
  331. cJSON_Delete(body_json);
  332. fdesc = open("/sys/bus/nvmem/devices/ds1307_nvram0/nvmem", O_WRONLY);
  333. bytes_written = write(fdesc, to_write, 56);
  334. close(fdesc);
  335. if (bytes_written == 56) {
  336. http_response(req, 200, NULL, 0);
  337. return (KORE_RESULT_OK);
  338. }
  339. result_string = "Error: could not write to nvmem\n";
  340. http_response(req, 500, result_string, strlen(result_string));
  341. return (KORE_RESULT_OK);
  342. } else {
  343. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  344. return (KORE_RESULT_OK);
  345. }
  346. }