server_main.cpp 13 KB

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