server_main.cpp 13 KB

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