server_main.cpp 13 KB

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