api.c 5.8 KB

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