api.c 5.7 KB

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