server_main.cpp 13 KB

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