server_main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "server_main.h"
  2. #include <string>
  3. #include "server_help.h"
  4. #include "ClassLogFile.h"
  5. #include "time_sntp.h"
  6. #include "connect_wlan.h"
  7. #include "version.h"
  8. #include "esp_wifi.h"
  9. #include "server_tflite.h"
  10. httpd_handle_t server = NULL;
  11. std::string starttime = "";
  12. /* An HTTP GET handler */
  13. esp_err_t info_get_handler(httpd_req_t *req)
  14. {
  15. LogFile.WriteToFile("info_get_handler");
  16. char _query[200];
  17. char _valuechar[30];
  18. std::string _task;
  19. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  20. {
  21. printf("Query: "); printf(_query); printf("\n");
  22. if (httpd_query_key_value(_query, "type", _valuechar, 30) == ESP_OK)
  23. {
  24. printf("type is found"); printf(_valuechar); printf("\n");
  25. _task = std::string(_valuechar);
  26. }
  27. };
  28. if (_task.compare("GitBranch") == 0)
  29. {
  30. std::string zw;
  31. zw = std::string(libfive_git_branch());
  32. httpd_resp_sendstr_chunk(req, zw.c_str());
  33. httpd_resp_sendstr_chunk(req, NULL);
  34. return ESP_OK;
  35. }
  36. if (_task.compare("GitTag") == 0)
  37. {
  38. std::string zw;
  39. zw = std::string(libfive_git_version());
  40. httpd_resp_sendstr_chunk(req, zw.c_str());
  41. httpd_resp_sendstr_chunk(req, NULL);
  42. return ESP_OK;
  43. }
  44. if (_task.compare("GitRevision") == 0)
  45. {
  46. std::string zw;
  47. zw = std::string(libfive_git_revision());
  48. httpd_resp_sendstr_chunk(req, zw.c_str());
  49. httpd_resp_sendstr_chunk(req, NULL);
  50. return ESP_OK;
  51. }
  52. if (_task.compare("BuildTime") == 0)
  53. {
  54. std::string zw;
  55. zw = std::string(build_time());
  56. httpd_resp_sendstr_chunk(req, zw.c_str());
  57. httpd_resp_sendstr_chunk(req, NULL);
  58. return ESP_OK;
  59. }
  60. if (_task.compare("GitBaseBranch") == 0)
  61. {
  62. std::string zw;
  63. zw = std::string(git_base_branch());
  64. httpd_resp_sendstr_chunk(req, zw.c_str());
  65. httpd_resp_sendstr_chunk(req, NULL);
  66. return ESP_OK;
  67. }
  68. if (_task.compare("HTMLVersion") == 0)
  69. {
  70. std::string zw;
  71. zw = std::string(getHTMLversion());
  72. httpd_resp_sendstr_chunk(req, zw.c_str());
  73. httpd_resp_sendstr_chunk(req, NULL);
  74. return ESP_OK;
  75. }
  76. if (_task.compare("Hostname") == 0)
  77. {
  78. std::string zw;
  79. zw = std::string(getHostname());
  80. httpd_resp_sendstr_chunk(req, zw.c_str());
  81. httpd_resp_sendstr_chunk(req, NULL);
  82. return ESP_OK;
  83. }
  84. if (_task.compare("IP") == 0)
  85. {
  86. std::string zw;
  87. zw = std::string(getIPAddress());
  88. httpd_resp_sendstr_chunk(req, zw.c_str());
  89. httpd_resp_sendstr_chunk(req, NULL);
  90. return ESP_OK;
  91. }
  92. if (_task.compare("SSID") == 0)
  93. {
  94. std::string zw;
  95. zw = std::string(getSSID());
  96. httpd_resp_sendstr_chunk(req, zw.c_str());
  97. httpd_resp_sendstr_chunk(req, NULL);
  98. return ESP_OK;
  99. }
  100. return ESP_OK;
  101. }
  102. esp_err_t starttime_get_handler(httpd_req_t *req)
  103. {
  104. httpd_resp_send(req, starttime.c_str(), strlen(starttime.c_str()));
  105. /* Respond with an empty chunk to signal HTTP response completion */
  106. httpd_resp_send_chunk(req, NULL, 0);
  107. return ESP_OK;
  108. }
  109. esp_err_t hello_main_handler(httpd_req_t *req)
  110. {
  111. char filepath[50];
  112. struct stat file_stat;
  113. printf("uri: %s\n", req->uri);
  114. int _pos;
  115. char *base_path = (char*) req->user_ctx;
  116. std::string filetosend(base_path);
  117. const char *filename = get_path_from_uri(filepath, base_path,
  118. req->uri - 1, sizeof(filepath));
  119. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  120. if ((strcmp(req->uri, "/") == 0))
  121. {
  122. {
  123. filetosend = filetosend + "/html/index.html";
  124. }
  125. }
  126. else
  127. {
  128. filetosend = filetosend + "/html" + std::string(req->uri);
  129. _pos = filetosend.find("?");
  130. if (_pos > -1){
  131. filetosend = filetosend.substr(0, _pos);
  132. }
  133. }
  134. if (filetosend == "/sdcard/html/index.html" && isSetupModusActive()) {
  135. printf("System ist im Setupmodus --> index.html --> setup.html");
  136. filetosend = "/sdcard/html/setup.html";
  137. }
  138. printf("Filename: %s\n", filename);
  139. printf("File requested: %s\n", filetosend.c_str());
  140. if (!filename) {
  141. ESP_LOGE(TAG, "Filename is too long");
  142. /* Respond with 500 Internal Server Error */
  143. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  144. return ESP_FAIL;
  145. }
  146. if (stat(filetosend.c_str(), &file_stat) == -1) {
  147. /* If file not present on SPIFFS check if URI
  148. * corresponds to one of the hardcoded paths */
  149. ESP_LOGE(TAG, "Failed to stat file : %s", filetosend.c_str());
  150. /* Respond with 404 Not Found */
  151. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
  152. return ESP_FAIL;
  153. }
  154. esp_err_t res;
  155. res = httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  156. if (res != ESP_OK)
  157. return res;
  158. res = send_file(req, filetosend, &file_stat);
  159. if (res != ESP_OK)
  160. return res;
  161. /* Respond with an empty chunk to signal HTTP response completion */
  162. httpd_resp_send_chunk(req, NULL, 0);
  163. return ESP_OK;
  164. }
  165. esp_err_t img_tmp_handler(httpd_req_t *req)
  166. {
  167. char filepath[50];
  168. struct stat file_stat;
  169. printf("uri: %s\n", req->uri);
  170. char *base_path = (char*) req->user_ctx;
  171. std::string filetosend(base_path);
  172. const char *filename = get_path_from_uri(filepath, base_path,
  173. req->uri + sizeof("/img_tmp") - 1, sizeof(filepath));
  174. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  175. filetosend = filetosend + "/img_tmp/" + std::string(filename);
  176. printf("File to upload: %s\n", filetosend.c_str());
  177. if (!filename) {
  178. ESP_LOGE(TAG, "Filename is too long");
  179. /* Respond with 500 Internal Server Error */
  180. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  181. return ESP_FAIL;
  182. }
  183. if (stat(filetosend.c_str(), &file_stat) == -1) {
  184. /* If file not present on SPIFFS check if URI
  185. * corresponds to one of the hardcoded paths */
  186. ESP_LOGE(TAG, "Failed to stat file : %s", filetosend.c_str());
  187. /* Respond with 404 Not Found */
  188. httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist");
  189. return ESP_FAIL;
  190. }
  191. esp_err_t res = send_file(req, filetosend, &file_stat);
  192. if (res != ESP_OK)
  193. return res;
  194. /* Respond with an empty chunk to signal HTTP response completion */
  195. httpd_resp_send_chunk(req, NULL, 0);
  196. return ESP_OK;
  197. }
  198. esp_err_t sysinfo_handler(httpd_req_t *req)
  199. {
  200. const char* resp_str;
  201. std::string zw;
  202. std::string cputemp = std::to_string(temperatureRead());
  203. std::string gitversion = libfive_git_version();
  204. std::string buildtime = build_time();
  205. std::string gitbranch = libfive_git_branch();
  206. std::string gitbasebranch = git_base_branch();
  207. std::string htmlversion = getHTMLversion();
  208. tcpip_adapter_ip_info_t ip_info;
  209. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  210. const char *hostname;
  211. ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));
  212. zw = "[\
  213. {\
  214. \"firmware\" : \"" + gitversion + "\",\
  215. \"buildtime\" : \"" + buildtime + "\",\
  216. \"gitbranch\" : \"" + gitbranch + "\",\
  217. \"gitbasebranch\" : \"" + gitbasebranch + "\",\
  218. \"html\" : \"" + htmlversion + "\",\
  219. \"cputemp\" : \"" + cputemp + "\",\
  220. \"hostname\" : \"" + hostname + "\",\
  221. \"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\"\
  222. }\
  223. ]";
  224. resp_str = zw.c_str();
  225. httpd_resp_set_type(req, "application/json");
  226. httpd_resp_send(req, resp_str, strlen(resp_str));
  227. /* Respond with an empty chunk to signal HTTP response completion */
  228. httpd_resp_send_chunk(req, NULL, 0);
  229. return ESP_OK;
  230. }
  231. void register_server_main_uri(httpd_handle_t server, const char *base_path)
  232. {
  233. httpd_uri_t info_get_handle = {
  234. .uri = "/version", // Match all URIs of type /path/to/file
  235. .method = HTTP_GET,
  236. .handler = info_get_handler,
  237. .user_ctx = (void*) base_path // Pass server data as context
  238. };
  239. httpd_register_uri_handler(server, &info_get_handle);
  240. httpd_uri_t sysinfo_handle = {
  241. .uri = "/sysinfo", // Match all URIs of type /path/to/file
  242. .method = HTTP_GET,
  243. .handler = sysinfo_handler,
  244. .user_ctx = (void*) base_path // Pass server data as context
  245. };
  246. httpd_register_uri_handler(server, &sysinfo_handle);
  247. httpd_uri_t starttime_tmp_handle = {
  248. .uri = "/starttime", // Match all URIs of type /path/to/file
  249. .method = HTTP_GET,
  250. .handler = starttime_get_handler,
  251. .user_ctx = NULL // Pass server data as context
  252. };
  253. httpd_register_uri_handler(server, &starttime_tmp_handle);
  254. httpd_uri_t img_tmp_handle = {
  255. .uri = "/img_tmp/*", // Match all URIs of type /path/to/file
  256. .method = HTTP_GET,
  257. .handler = img_tmp_handler,
  258. .user_ctx = (void*) base_path // Pass server data as context
  259. };
  260. httpd_register_uri_handler(server, &img_tmp_handle);
  261. httpd_uri_t main_rest_handle = {
  262. .uri = "/*", // Match all URIs of type /path/to/file
  263. .method = HTTP_GET,
  264. .handler = hello_main_handler,
  265. .user_ctx = (void*) base_path // Pass server data as context
  266. };
  267. httpd_register_uri_handler(server, &main_rest_handle);
  268. }
  269. httpd_handle_t start_webserver(void)
  270. {
  271. httpd_handle_t server = NULL;
  272. httpd_config_t config = { };
  273. config.task_priority = tskIDLE_PRIORITY+5;
  274. config.stack_size = 16384; // bei 32k stürzt das Programm beim Bilderaufnehmen ab
  275. config.core_id = tskNO_AFFINITY;
  276. config.server_port = 80;
  277. config.ctrl_port = 32768;
  278. config.max_open_sockets = 7;
  279. config.max_uri_handlers = 24;
  280. config.max_resp_headers = 8;
  281. config.backlog_conn = 5;
  282. config.lru_purge_enable = false;
  283. config.recv_wait_timeout = 30; // default: 5
  284. config.send_wait_timeout = 30; // default: 5
  285. config.global_user_ctx = NULL;
  286. config.global_user_ctx_free_fn = NULL;
  287. config.global_transport_ctx = NULL;
  288. config.global_transport_ctx_free_fn = NULL;
  289. config.open_fn = NULL;
  290. config.close_fn = NULL;
  291. // config.uri_match_fn = NULL;
  292. config.uri_match_fn = httpd_uri_match_wildcard;
  293. starttime = gettimestring("%Y%m%d-%H%M%S");
  294. // Start the httpd server
  295. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  296. if (httpd_start(&server, &config) == ESP_OK) {
  297. // Set URI handlers
  298. ESP_LOGI(TAG, "Registering URI handlers");
  299. return server;
  300. }
  301. ESP_LOGI(TAG, "Error starting server!");
  302. return NULL;
  303. }
  304. void stop_webserver(httpd_handle_t server)
  305. {
  306. httpd_stop(server);
  307. }
  308. void disconnect_handler(void* arg, esp_event_base_t event_base,
  309. int32_t event_id, void* event_data)
  310. {
  311. httpd_handle_t* server = (httpd_handle_t*) arg;
  312. if (*server) {
  313. ESP_LOGI(TAG, "Stopping webserver");
  314. stop_webserver(*server);
  315. *server = NULL;
  316. }
  317. }
  318. void connect_handler(void* arg, esp_event_base_t event_base,
  319. int32_t event_id, void* event_data)
  320. {
  321. httpd_handle_t* server = (httpd_handle_t*) arg;
  322. if (*server == NULL) {
  323. ESP_LOGI(TAG, "Starting webserver");
  324. *server = start_webserver();
  325. }
  326. }