api.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. uint16_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. cJSON_AddNumberToObject(root, "hour", wakeup_minutes / 60);
  45. cJSON_AddNumberToObject(root, "minute", wakeup_minutes % 60);
  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. int hour = cJSON_GetObjectItem(root, "hour")->valueint;
  89. int minute = cJSON_GetObjectItem(root, "minute")->valueint;
  90. if (validate_time(hour, minute)) {
  91. esp_err_t ret = write_wakeup_time_str(wakeup_str, hour * 60 + minute);
  92. if (ret == ESP_OK) {
  93. httpd_resp_sendstr(req, "");
  94. } else {
  95. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while writing data");
  96. }
  97. } else {
  98. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Time values outside allowed range");
  99. }
  100. cJSON_Delete(root);
  101. return ESP_OK;
  102. }
  103. static const httpd_uri_t wakeup_put = {
  104. .uri = "/v1/wakeup/*",
  105. .method = HTTP_PUT,
  106. .handler = wakeup_put_handler
  107. };
  108. static esp_err_t wakeup_delete_handler(httpd_req_t *req) {
  109. char* wakeup_str = extract_wakeup(req->uri);
  110. if (!day_is_valid(wakeup_str)) {
  111. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid day requested");
  112. return ESP_OK;
  113. }
  114. esp_err_t ret = delete_wakeup_time_str(wakeup_str);
  115. if (ret == ESP_OK) {
  116. httpd_resp_sendstr(req, "");
  117. return ret;
  118. } else if (ret == ESP_ERR_NVS_NOT_FOUND) {
  119. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Entry does not exist");
  120. }
  121. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while deleting data");
  122. return ESP_OK;
  123. }
  124. static const httpd_uri_t wakeup_delete = {
  125. .uri = "/v1/wakeup/*",
  126. .method = HTTP_DELETE,
  127. .handler = wakeup_delete_handler
  128. };
  129. httpd_handle_t start_webserver() {
  130. httpd_handle_t server;
  131. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  132. config.lru_purge_enable = true;
  133. config.uri_match_fn = httpd_uri_match_wildcard;
  134. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  135. if (httpd_start(&server, &config) == ESP_OK) {
  136. ESP_LOGI(TAG, "Registering URI handlers");
  137. httpd_register_uri_handler(server, &wakeup_get);
  138. httpd_register_uri_handler(server, &wakeup_put);
  139. httpd_register_uri_handler(server, &wakeup_delete);
  140. return server;
  141. }
  142. ESP_LOGI(TAG, "Error starting server!");
  143. return NULL;
  144. }
  145. esp_err_t stop_webserver(httpd_handle_t server) {
  146. return httpd_stop(server);
  147. }
  148. void disconnect_handler(void* arg, esp_event_base_t event_base,
  149. int32_t event_id, void* event_data) {
  150. httpd_handle_t* server = (httpd_handle_t*) arg;
  151. if (*server) {
  152. ESP_LOGI(TAG, "Stopping webserver");
  153. if (stop_webserver(*server) == ESP_OK) {
  154. *server = NULL;
  155. } else {
  156. ESP_LOGE(TAG, "Failed to stop http server");
  157. }
  158. }
  159. }
  160. void connect_handler(void* arg, esp_event_base_t event_base,
  161. int32_t event_id, void* event_data) {
  162. httpd_handle_t* server = (httpd_handle_t*) arg;
  163. if (*server == NULL) {
  164. ESP_LOGI(TAG, "Starting webserver");
  165. *server = start_webserver();
  166. }
  167. }