server_main.cpp 13 KB

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