server_main.cpp 13 KB

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