server_main.cpp 15 KB

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