jsonrtc.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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;
  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. cJSON_AddItemToObject(time_json, "seconds", json_sec);
  35. cJSON_AddItemToObject(time_json, "minutes", json_min);
  36. cJSON_AddItemToObject(time_json, "hour", json_hour);
  37. cJSON_AddItemToObject(time_json, "mday", json_mday);
  38. cJSON_AddItemToObject(time_json, "month", json_mon);
  39. cJSON_AddItemToObject(time_json, "year", json_year);
  40. result_string = cJSON_Print(time_json);
  41. cJSON_Delete(time_json);
  42. http_response_header(req, "Content-Type", "application/json");
  43. http_response(req, 200, result_string, strlen(result_string));
  44. return KORE_RESULT_OK;
  45. } else if (req->method == HTTP_METHOD_POST) {
  46. struct kore_buf * buf;
  47. u_int8_t data[128];
  48. int ret;
  49. char * body;
  50. buf = kore_buf_alloc(128);
  51. printf("Starting reading of body\n");
  52. while(1) {
  53. ret = http_body_read(req, data, sizeof(data));
  54. if (ret == -1) {
  55. kore_buf_free(buf);
  56. http_response(req, 500, NULL, 0);
  57. return (KORE_RESULT_OK);
  58. }
  59. if (ret == 0) {
  60. break;
  61. }
  62. kore_buf_append(buf,data,ret);
  63. }
  64. body = kore_buf_stringify(buf, NULL);
  65. kore_buf_free(buf);
  66. time_json = cJSON_Parse(body);
  67. if (time_json == NULL) {
  68. http_response(req, 500, NULL, 0);
  69. return (KORE_RESULT_OK);
  70. }
  71. time.tm_sec = cJSON_GetObjectItemCaseSensitive(time_json, "seconds")->valueint;
  72. time.tm_min = cJSON_GetObjectItemCaseSensitive(time_json, "minutes")->valueint;
  73. time.tm_hour = cJSON_GetObjectItemCaseSensitive(time_json, "hour")->valueint;
  74. time.tm_mday = cJSON_GetObjectItemCaseSensitive(time_json, "mday")->valueint;
  75. time.tm_mon = cJSON_GetObjectItemCaseSensitive(time_json, "month")->valueint;
  76. time.tm_year = cJSON_GetObjectItemCaseSensitive(time_json, "year")->valueint;
  77. cJSON_Delete(time_json);
  78. fdesc = open("/dev/rtc", O_WRONLY);
  79. ret = ioctl(fdesc, RTC_SET_TIME, &time);
  80. close(fdesc);
  81. if (ret) {
  82. http_response(req, 500, NULL, 0);
  83. return (KORE_RESULT_OK);
  84. }
  85. http_response(req, 200, NULL, 0);
  86. return KORE_RESULT_OK;
  87. } else {
  88. http_response(req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
  89. return (KORE_RESULT_OK);
  90. }
  91. }