server_main.cpp 13 KB

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