server_main.cpp 15 KB

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