server_main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. //#define DEBUG_DETAIL_ON
  11. httpd_handle_t server = NULL;
  12. std::string starttime = "";
  13. static const char *TAG_SERVERMAIN = "server-main";
  14. /* An HTTP GET handler */
  15. esp_err_t info_get_handler(httpd_req_t *req)
  16. {
  17. #ifdef DEBUG_DETAIL_ON
  18. LogFile.WriteHeapInfo("info_get_handler - Start");
  19. #endif
  20. LogFile.WriteToFile("info_get_handler");
  21. char _query[200];
  22. char _valuechar[30];
  23. std::string _task;
  24. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  25. {
  26. printf("Query: "); printf(_query); printf("\n");
  27. if (httpd_query_key_value(_query, "type", _valuechar, 30) == ESP_OK)
  28. {
  29. printf("type is found"); printf(_valuechar); printf("\n");
  30. _task = std::string(_valuechar);
  31. }
  32. };
  33. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  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. if (_task.compare("FlowStatus") == 0)
  107. {
  108. std::string zw;
  109. zw = std::string("FlowStatus");
  110. httpd_resp_sendstr_chunk(req, zw.c_str());
  111. httpd_resp_sendstr_chunk(req, NULL);
  112. return ESP_OK;
  113. }
  114. #ifdef DEBUG_DETAIL_ON
  115. LogFile.WriteHeapInfo("info_get_handler - Done");
  116. #endif
  117. return ESP_OK;
  118. }
  119. esp_err_t starttime_get_handler(httpd_req_t *req)
  120. {
  121. #ifdef DEBUG_DETAIL_ON
  122. LogFile.WriteHeapInfo("starttime_get_handler - Start");
  123. #endif
  124. httpd_resp_send(req, starttime.c_str(), strlen(starttime.c_str()));
  125. /* Respond with an empty chunk to signal HTTP response completion */
  126. httpd_resp_send_chunk(req, NULL, 0);
  127. #ifdef DEBUG_DETAIL_ON
  128. LogFile.WriteHeapInfo("starttime_get_handler - Done");
  129. #endif
  130. return ESP_OK;
  131. }
  132. esp_err_t hello_main_handler(httpd_req_t *req)
  133. {
  134. #ifdef DEBUG_DETAIL_ON
  135. LogFile.WriteHeapInfo("hello_main_handler - Start");
  136. #endif
  137. char filepath[50];
  138. printf("uri: %s\n", req->uri);
  139. int _pos;
  140. esp_err_t res;
  141. char *base_path = (char*) req->user_ctx;
  142. std::string filetosend(base_path);
  143. const char *filename = get_path_from_uri(filepath, base_path,
  144. req->uri - 1, sizeof(filepath));
  145. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  146. if ((strcmp(req->uri, "/") == 0))
  147. {
  148. {
  149. filetosend = filetosend + "/html/index.html";
  150. }
  151. }
  152. else
  153. {
  154. filetosend = filetosend + "/html" + std::string(req->uri);
  155. _pos = filetosend.find("?");
  156. if (_pos > -1){
  157. filetosend = filetosend.substr(0, _pos);
  158. }
  159. }
  160. if (filetosend == "/sdcard/html/index.html" && isSetupModusActive()) {
  161. printf("System ist im Setupmodus --> index.html --> setup.html");
  162. filetosend = "/sdcard/html/setup.html";
  163. }
  164. printf("Filename: %s\n", filename);
  165. printf("File requested: %s\n", filetosend.c_str());
  166. if (!filename) {
  167. ESP_LOGE(TAG_SERVERMAIN, "Filename is too long");
  168. /* Respond with 500 Internal Server Error */
  169. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  170. return ESP_FAIL;
  171. }
  172. res = send_file(req, filetosend);
  173. if (res != ESP_OK)
  174. return res;
  175. /* Respond with an empty chunk to signal HTTP response completion */
  176. // httpd_resp_sendstr(req, "");
  177. httpd_resp_send_chunk(req, NULL, 0);
  178. #ifdef DEBUG_DETAIL_ON
  179. LogFile.WriteHeapInfo("hello_main_handler - Stop");
  180. #endif
  181. return ESP_OK;
  182. }
  183. esp_err_t img_tmp_handler(httpd_req_t *req)
  184. {
  185. char filepath[50];
  186. printf("uri: %s\n", req->uri);
  187. char *base_path = (char*) req->user_ctx;
  188. std::string filetosend(base_path);
  189. const char *filename = get_path_from_uri(filepath, base_path,
  190. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  191. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  192. filetosend = filetosend + "/img_tmp/" + std::string(filename);
  193. printf("File to upload: %s\n", filetosend.c_str());
  194. esp_err_t res = send_file(req, filetosend);
  195. if (res != ESP_OK)
  196. return res;
  197. /* Respond with an empty chunk to signal HTTP response completion */
  198. httpd_resp_send_chunk(req, NULL, 0);
  199. return ESP_OK;
  200. }
  201. esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
  202. {
  203. #ifdef DEBUG_DETAIL_ON
  204. LogFile.WriteHeapInfo("img_tmp_virtual_handler - Start");
  205. #endif
  206. char filepath[50];
  207. printf("uri: %s\n", req->uri);
  208. char *base_path = (char*) req->user_ctx;
  209. std::string filetosend(base_path);
  210. const char *filename = get_path_from_uri(filepath, base_path,
  211. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  212. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  213. filetosend = std::string(filename);
  214. printf("File to upload: %s\n", filetosend.c_str());
  215. if (filetosend == "raw.jpg")
  216. {
  217. return GetRawJPG(req);
  218. }
  219. esp_err_t zw = GetJPG(filetosend, req);
  220. if (zw == ESP_OK)
  221. return ESP_OK;
  222. // File wird nicht intern bereit gestellt --> klassischer weg:
  223. #ifdef DEBUG_DETAIL_ON
  224. LogFile.WriteHeapInfo("img_tmp_virtual_handler - Done");
  225. #endif
  226. return img_tmp_handler(req);
  227. }
  228. esp_err_t sysinfo_handler(httpd_req_t *req)
  229. {
  230. #ifdef DEBUG_DETAIL_ON
  231. LogFile.WriteHeapInfo("sysinfo_handler - Start");
  232. #endif
  233. const char* resp_str;
  234. std::string zw;
  235. std::string cputemp = std::to_string(temperatureRead());
  236. std::string gitversion = libfive_git_version();
  237. std::string buildtime = build_time();
  238. std::string gitbranch = libfive_git_branch();
  239. std::string gitbasebranch = git_base_branch();
  240. std::string htmlversion = getHTMLversion();
  241. char freeheapmem[11];
  242. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  243. tcpip_adapter_ip_info_t ip_info;
  244. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  245. const char *hostname;
  246. ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));
  247. zw = "[\
  248. {\
  249. \"firmware\" : \"" + gitversion + "\",\
  250. \"buildtime\" : \"" + buildtime + "\",\
  251. \"gitbranch\" : \"" + gitbranch + "\",\
  252. \"gitbasebranch\" : \"" + gitbasebranch + "\",\
  253. \"html\" : \"" + htmlversion + "\",\
  254. \"cputemp\" : \"" + cputemp + "\",\
  255. \"hostname\" : \"" + hostname + "\",\
  256. \"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\",\
  257. \"freeHeapMem\" : \"" + freeheapmem + "\"\
  258. }\
  259. ]";
  260. resp_str = zw.c_str();
  261. httpd_resp_set_type(req, "application/json");
  262. httpd_resp_send(req, resp_str, strlen(resp_str));
  263. /* Respond with an empty chunk to signal HTTP response completion */
  264. httpd_resp_send_chunk(req, NULL, 0);
  265. #ifdef DEBUG_DETAIL_ON
  266. LogFile.WriteHeapInfo("sysinfo_handler - Done");
  267. #endif
  268. return ESP_OK;
  269. }
  270. void register_server_main_uri(httpd_handle_t server, const char *base_path)
  271. {
  272. httpd_uri_t info_get_handle = {
  273. .uri = "/version", // Match all URIs of type /path/to/file
  274. .method = HTTP_GET,
  275. .handler = info_get_handler,
  276. .user_ctx = (void*) base_path // Pass server data as context
  277. };
  278. httpd_register_uri_handler(server, &info_get_handle);
  279. httpd_uri_t sysinfo_handle = {
  280. .uri = "/sysinfo", // Match all URIs of type /path/to/file
  281. .method = HTTP_GET,
  282. .handler = sysinfo_handler,
  283. .user_ctx = (void*) base_path // Pass server data as context
  284. };
  285. httpd_register_uri_handler(server, &sysinfo_handle);
  286. httpd_uri_t starttime_tmp_handle = {
  287. .uri = "/starttime", // Match all URIs of type /path/to/file
  288. .method = HTTP_GET,
  289. .handler = starttime_get_handler,
  290. .user_ctx = NULL // Pass server data as context
  291. };
  292. httpd_register_uri_handler(server, &starttime_tmp_handle);
  293. httpd_uri_t img_tmp_handle = {
  294. .uri = "/img_tmp/*", // Match all URIs of type /path/to/file
  295. .method = HTTP_GET,
  296. .handler = img_tmp_virtual_handler,
  297. .user_ctx = (void*) base_path // Pass server data as context
  298. };
  299. httpd_register_uri_handler(server, &img_tmp_handle);
  300. httpd_uri_t main_rest_handle = {
  301. .uri = "/*", // Match all URIs of type /path/to/file
  302. .method = HTTP_GET,
  303. .handler = hello_main_handler,
  304. .user_ctx = (void*) base_path // Pass server data as context
  305. };
  306. httpd_register_uri_handler(server, &main_rest_handle);
  307. }
  308. httpd_handle_t start_webserver(void)
  309. {
  310. httpd_handle_t server = NULL;
  311. httpd_config_t config = { };
  312. config.task_priority = tskIDLE_PRIORITY+5;
  313. config.stack_size = 32768; //20210921 --> vorher 32768 // bei 32k stürzt das Programm beim Bilderaufnehmen ab
  314. config.core_id = tskNO_AFFINITY;
  315. config.server_port = 80;
  316. config.ctrl_port = 32768;
  317. config.max_open_sockets = 5; //20210921 --> vorher 7
  318. config.max_uri_handlers = 24;
  319. config.max_resp_headers = 8;
  320. config.backlog_conn = 5;
  321. config.lru_purge_enable = true; // dadurch werden alte Verbindungen gekappt, falls neue benögt werden.
  322. config.recv_wait_timeout = 30; // default: 5
  323. config.send_wait_timeout = 30; // default: 5
  324. config.global_user_ctx = NULL;
  325. config.global_user_ctx_free_fn = NULL;
  326. config.global_transport_ctx = NULL;
  327. config.global_transport_ctx_free_fn = NULL;
  328. config.open_fn = NULL;
  329. config.close_fn = NULL;
  330. config.lru_purge_enable = true; // neu, um schlechte Serverbindung zu verhindern
  331. // config.uri_match_fn = NULL;
  332. config.uri_match_fn = httpd_uri_match_wildcard;
  333. starttime = gettimestring("%Y%m%d-%H%M%S");
  334. // Start the httpd server
  335. ESP_LOGI(TAG_SERVERMAIN, "Starting server on port: '%d'", config.server_port);
  336. if (httpd_start(&server, &config) == ESP_OK) {
  337. // Set URI handlers
  338. ESP_LOGI(TAG_SERVERMAIN, "Registering URI handlers");
  339. return server;
  340. }
  341. ESP_LOGI(TAG_SERVERMAIN, "Error starting server!");
  342. return NULL;
  343. }
  344. void stop_webserver(httpd_handle_t server)
  345. {
  346. httpd_stop(server);
  347. }
  348. void disconnect_handler(void* arg, esp_event_base_t event_base,
  349. int32_t event_id, void* event_data)
  350. {
  351. httpd_handle_t* server = (httpd_handle_t*) arg;
  352. if (*server) {
  353. ESP_LOGI(TAG_SERVERMAIN, "Stopping webserver");
  354. stop_webserver(*server);
  355. *server = NULL;
  356. }
  357. }
  358. void connect_handler(void* arg, esp_event_base_t event_base,
  359. int32_t event_id, void* event_data)
  360. {
  361. httpd_handle_t* server = (httpd_handle_t*) arg;
  362. if (*server == NULL) {
  363. ESP_LOGI(TAG_SERVERMAIN, "Starting webserver");
  364. *server = start_webserver();
  365. }
  366. }