api.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <string.h>
  7. #include <esp_http_server.h>
  8. #include <esp_event.h>
  9. #include <esp_log.h>
  10. #include <esp_err.h>
  11. #include <cJSON.h>
  12. #include "storage.h"
  13. #define TAG "api"
  14. #define BUF_LEN 1000
  15. static TaskHandle_t* alarm_task_handle = NULL;
  16. static char* extract_wakeup(const char* uri) {
  17. return strrchr(uri, '/') +1;
  18. }
  19. static uint8_t contains_weekday(char* param) {
  20. uint8_t result = 0;
  21. for (int i = 1; i<=NUM_WEEK_DAYS; i++) {
  22. if (strcmp(WEEK_DAYS[i], param) == 0) {
  23. result = 1;
  24. break;
  25. }
  26. }
  27. return result;
  28. }
  29. static uint8_t day_is_valid(char* day) {
  30. return strlen(day) == 8 || contains_weekday(day);
  31. }
  32. static esp_err_t wakeup_get_handler(httpd_req_t *req) {
  33. char* wakeup_str = extract_wakeup(req->uri);
  34. if (!day_is_valid(wakeup_str)) {
  35. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid day requested");
  36. return ESP_OK;
  37. }
  38. int16_t wakeup_minutes;
  39. esp_err_t ret = read_wakeup_time_str(wakeup_str, &wakeup_minutes);
  40. if (ret == ESP_OK) {
  41. httpd_resp_set_type(req, "application/json");
  42. cJSON *root = cJSON_CreateObject();
  43. if (wakeup_minutes >= 0) {
  44. cJSON_AddNumberToObject(root, "hour", wakeup_minutes / 60);
  45. cJSON_AddNumberToObject(root, "minute", wakeup_minutes % 60);
  46. }
  47. const char *response = cJSON_Print(root);
  48. httpd_resp_sendstr(req, response);
  49. } else if (ret == ESP_ERR_NVS_NOT_FOUND) {
  50. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Requested entry not found");
  51. } else {
  52. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Request could not be processed");
  53. }
  54. return ESP_OK;
  55. }
  56. static const httpd_uri_t wakeup_get = {
  57. .uri = "/v1/wakeup/*",
  58. .method = HTTP_GET,
  59. .handler = wakeup_get_handler
  60. };
  61. static uint8_t validate_time(int hour, int minute) {
  62. return (hour >= 0 && hour < 24) && (minute >= 0 && minute < 60);
  63. }
  64. static void notify_alarm_task() {
  65. if (alarm_task_handle != NULL) {
  66. xTaskNotify(*alarm_task_handle, 0, eNoAction);
  67. }
  68. }
  69. static esp_err_t wakeup_put_handler(httpd_req_t *req) {
  70. char* wakeup_str = extract_wakeup(req->uri);
  71. char buf[BUF_LEN];
  72. if (!day_is_valid(wakeup_str)) {
  73. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid day requested");
  74. return ESP_OK;
  75. }
  76. if (req->content_len >= BUF_LEN) {
  77. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Maximum content length exceeded");
  78. return ESP_OK;
  79. }
  80. int bytes_received = httpd_req_recv(req, buf, BUF_LEN);
  81. if (bytes_received == 0) {
  82. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "No content received");
  83. return ESP_OK;
  84. } else if (bytes_received < 0) {
  85. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Request could not be processed");
  86. return ESP_OK;
  87. }
  88. buf[req->content_len] = '\0';
  89. cJSON *root = cJSON_Parse(buf);
  90. if (root == NULL) {
  91. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Could not parse object");
  92. return ESP_OK;
  93. }
  94. int16_t minute_of_day;
  95. if (cJSON_GetArraySize(root) == 0) {
  96. minute_of_day = -1;
  97. } else {
  98. int16_t hour = cJSON_GetObjectItem(root, "hour")->valueint;
  99. int16_t minute = cJSON_GetObjectItem(root, "minute")->valueint;
  100. if (validate_time(hour, minute)) {
  101. minute_of_day = hour * 60 + minute;
  102. } else {
  103. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Time values outside allowed range");
  104. cJSON_Delete(root);
  105. return ESP_OK;
  106. }
  107. }
  108. esp_err_t ret = write_wakeup_time_str(wakeup_str, minute_of_day);
  109. if (ret == ESP_OK) {
  110. httpd_resp_sendstr(req, "");
  111. notify_alarm_task();
  112. } else {
  113. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while writing data");
  114. }
  115. cJSON_Delete(root);
  116. return ESP_OK;
  117. }
  118. static const httpd_uri_t wakeup_put = {
  119. .uri = "/v1/wakeup/*",
  120. .method = HTTP_PUT,
  121. .handler = wakeup_put_handler
  122. };
  123. static esp_err_t wakeup_delete_handler(httpd_req_t *req) {
  124. char* wakeup_str = extract_wakeup(req->uri);
  125. if (!day_is_valid(wakeup_str)) {
  126. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid day requested");
  127. return ESP_OK;
  128. }
  129. esp_err_t ret = delete_wakeup_time_str(wakeup_str);
  130. if (ret == ESP_OK) {
  131. httpd_resp_sendstr(req, "");
  132. return ret;
  133. } else if (ret == ESP_ERR_NVS_NOT_FOUND) {
  134. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Entry does not exist");
  135. }
  136. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while deleting data");
  137. return ESP_OK;
  138. }
  139. static const httpd_uri_t wakeup_delete = {
  140. .uri = "/v1/wakeup/*",
  141. .method = HTTP_DELETE,
  142. .handler = wakeup_delete_handler
  143. };
  144. httpd_handle_t start_webserver(TaskHandle_t* task_handle) {
  145. httpd_handle_t server;
  146. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  147. config.lru_purge_enable = true;
  148. config.uri_match_fn = httpd_uri_match_wildcard;
  149. if (task_handle != NULL) {
  150. alarm_task_handle = task_handle;
  151. }
  152. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  153. if (httpd_start(&server, &config) == ESP_OK) {
  154. ESP_LOGI(TAG, "Registering URI handlers");
  155. httpd_register_uri_handler(server, &wakeup_get);
  156. httpd_register_uri_handler(server, &wakeup_put);
  157. httpd_register_uri_handler(server, &wakeup_delete);
  158. return server;
  159. }
  160. ESP_LOGI(TAG, "Error starting server!");
  161. return NULL;
  162. }
  163. esp_err_t stop_webserver(httpd_handle_t server) {
  164. return httpd_stop(server);
  165. }
  166. void disconnect_handler(void* arg, esp_event_base_t event_base,
  167. int32_t event_id, void* event_data) {
  168. httpd_handle_t* server = (httpd_handle_t*) arg;
  169. if (*server) {
  170. ESP_LOGI(TAG, "Stopping webserver");
  171. if (stop_webserver(*server) == ESP_OK) {
  172. *server = NULL;
  173. } else {
  174. ESP_LOGE(TAG, "Failed to stop http server");
  175. }
  176. }
  177. }
  178. void connect_handler(void* arg, esp_event_base_t event_base,
  179. int32_t event_id, void* event_data) {
  180. httpd_handle_t* server = (httpd_handle_t*) arg;
  181. if (*server == NULL) {
  182. ESP_LOGI(TAG, "Starting webserver");
  183. *server = start_webserver(NULL);
  184. }
  185. }