server_main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. else 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. else 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. else 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. else if (_task.compare("FirmwareVersion") == 0)
  61. {
  62. string buf;
  63. if (std::string(GIT_TAG) == "") { // Tag not set, show branch
  64. buf = "Development-Branch: " + std::string(GIT_BRANCH);
  65. }
  66. else { // Tag is set, ignore branch
  67. buf = "Release: " + std::string(GIT_TAG);
  68. }
  69. buf = buf + " (Commit: " + std::string(GIT_REV) + ")";
  70. httpd_resp_sendstr_chunk(req, buf.c_str());
  71. httpd_resp_sendstr_chunk(req, NULL);
  72. return ESP_OK;
  73. }
  74. else if (_task.compare("HTMLVersion") == 0)
  75. {
  76. httpd_resp_sendstr_chunk(req, getHTMLversion().c_str());
  77. httpd_resp_sendstr_chunk(req, NULL);
  78. return ESP_OK;
  79. }
  80. else if (_task.compare("Hostname") == 0)
  81. {
  82. std::string zw;
  83. zw = std::string(hostname);
  84. httpd_resp_sendstr_chunk(req, zw.c_str());
  85. httpd_resp_sendstr_chunk(req, NULL);
  86. return ESP_OK;
  87. }
  88. else if (_task.compare("IP") == 0)
  89. {
  90. std::string *zw;
  91. zw = getIPAddress();
  92. httpd_resp_sendstr_chunk(req, zw->c_str());
  93. httpd_resp_sendstr_chunk(req, NULL);
  94. return ESP_OK;
  95. }
  96. else if (_task.compare("SSID") == 0)
  97. {
  98. std::string *zw;
  99. zw = getSSID();
  100. httpd_resp_sendstr_chunk(req, zw->c_str());
  101. httpd_resp_sendstr_chunk(req, NULL);
  102. return ESP_OK;
  103. }
  104. else if (_task.compare("FlowStatus") == 0)
  105. {
  106. std::string zw;
  107. zw = std::string("FlowStatus");
  108. httpd_resp_sendstr_chunk(req, zw.c_str());
  109. httpd_resp_sendstr_chunk(req, NULL);
  110. return ESP_OK;
  111. }
  112. else if (_task.compare("SDCardPartitionSize") == 0)
  113. {
  114. std::string zw;
  115. zw = getSDCardPartitionSize();
  116. httpd_resp_sendstr_chunk(req, zw.c_str());
  117. httpd_resp_sendstr_chunk(req, NULL);
  118. return ESP_OK;
  119. }
  120. else if (_task.compare("SDCardFreePartitionSpace") == 0)
  121. {
  122. std::string zw;
  123. zw = getSDCardFreePartitionSpace();
  124. httpd_resp_sendstr_chunk(req, zw.c_str());
  125. httpd_resp_sendstr_chunk(req, NULL);
  126. return ESP_OK;
  127. }
  128. else if (_task.compare("SDCardPartitionAllocationSize") == 0)
  129. {
  130. std::string zw;
  131. zw = getSDCardPartitionAllocationSize();
  132. httpd_resp_sendstr_chunk(req, zw.c_str());
  133. httpd_resp_sendstr_chunk(req, NULL);
  134. return ESP_OK;
  135. }
  136. else if (_task.compare("SDCardManufacturer") == 0)
  137. {
  138. std::string zw;
  139. zw = getSDCardManufacturer();
  140. httpd_resp_sendstr_chunk(req, zw.c_str());
  141. httpd_resp_sendstr_chunk(req, NULL);
  142. return ESP_OK;
  143. }
  144. else if (_task.compare("SDCardName") == 0)
  145. {
  146. std::string zw;
  147. zw = getSDCardName();
  148. httpd_resp_sendstr_chunk(req, zw.c_str());
  149. httpd_resp_sendstr_chunk(req, NULL);
  150. return ESP_OK;
  151. }
  152. else if (_task.compare("SDCardCapacity") == 0)
  153. {
  154. std::string zw;
  155. zw = getSDCardCapacity();
  156. httpd_resp_sendstr_chunk(req, zw.c_str());
  157. httpd_resp_sendstr_chunk(req, NULL);
  158. return ESP_OK;
  159. }
  160. else if (_task.compare("SDCardSectorSize") == 0)
  161. {
  162. std::string zw;
  163. zw = getSDCardSectorSize();
  164. httpd_resp_sendstr_chunk(req, zw.c_str());
  165. httpd_resp_sendstr_chunk(req, NULL);
  166. return ESP_OK;
  167. }
  168. return ESP_OK;
  169. }
  170. esp_err_t starttime_get_handler(httpd_req_t *req)
  171. {
  172. httpd_resp_send(req, starttime.c_str(), strlen(starttime.c_str()));
  173. /* Respond with an empty chunk to signal HTTP response completion */
  174. httpd_resp_send_chunk(req, NULL, 0);
  175. return ESP_OK;
  176. }
  177. esp_err_t hello_main_handler(httpd_req_t *req)
  178. {
  179. #ifdef DEBUG_DETAIL_ON
  180. LogFile.WriteHeapInfo("hello_main_handler - Start");
  181. #endif
  182. char filepath[50];
  183. ESP_LOGD(TAG, "uri: %s\n", req->uri);
  184. int _pos;
  185. esp_err_t res;
  186. char *base_path = (char*) req->user_ctx;
  187. std::string filetosend(base_path);
  188. const char *filename = get_path_from_uri(filepath, base_path,
  189. req->uri - 1, sizeof(filepath));
  190. ESP_LOGD(TAG, "1 uri: %s, filename: %s, filepath: %s", req->uri, filename, filepath);
  191. if ((strcmp(req->uri, "/") == 0))
  192. {
  193. {
  194. filetosend = filetosend + "/html/index.html";
  195. }
  196. }
  197. else
  198. {
  199. filetosend = filetosend + "/html" + std::string(req->uri);
  200. _pos = filetosend.find("?");
  201. if (_pos > -1){
  202. filetosend = filetosend.substr(0, _pos);
  203. }
  204. }
  205. if (filetosend == "/sdcard/html/index.html") {
  206. if (isSetSystemStatusFlag(SYSTEM_STATUS_PSRAM_BAD) || // Initialization failed with crritical errors!
  207. isSetSystemStatusFlag(SYSTEM_STATUS_CAM_BAD)) {
  208. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "We have a critical error, not serving main page!");
  209. char buf[20];
  210. std::string message = "<h1>AI on the Edge Device</h1><b>We have one or more critical errors:</b><br>";
  211. for (int i = 0; i < 32; i++) {
  212. if (isSetSystemStatusFlag((SystemStatusFlag_t)(1<<i))) {
  213. snprintf(buf, sizeof(buf), "0x%08X", 1<<i);
  214. message += std::string(buf) + "<br>";
  215. }
  216. }
  217. message += "<br>Please check <a href=\"https://github.com/jomjol/AI-on-the-edge-device/wiki/Error-Codes\" target=_blank>github.com/jomjol/AI-on-the-edge-device/wiki/Error-Codes</a> for more information!";
  218. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, message.c_str());
  219. return ESP_FAIL;
  220. }
  221. else if (isSetupModusActive()) {
  222. ESP_LOGD(TAG, "System is in setup mode --> index.html --> setup.html");
  223. filetosend = "/sdcard/html/setup.html";
  224. }
  225. }
  226. ESP_LOGD(TAG, "Filename: %s", filename);
  227. ESP_LOGD(TAG, "File requested: %s", filetosend.c_str());
  228. if (!filename) {
  229. ESP_LOGE(TAG, "Filename is too long");
  230. /* Respond with 414 Error */
  231. httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long");
  232. return ESP_FAIL;
  233. }
  234. res = send_file(req, filetosend);
  235. /* Respond with an empty chunk to signal HTTP response completion */
  236. httpd_resp_send_chunk(req, NULL, 0);
  237. if (res != ESP_OK)
  238. return res;
  239. /* Respond with an empty chunk to signal HTTP response completion */
  240. // httpd_resp_sendstr(req, "");
  241. // httpd_resp_send_chunk(req, NULL, 0);
  242. #ifdef DEBUG_DETAIL_ON
  243. LogFile.WriteHeapInfo("hello_main_handler - Stop");
  244. #endif
  245. return ESP_OK;
  246. }
  247. esp_err_t img_tmp_handler(httpd_req_t *req)
  248. {
  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 = filetosend + "/img_tmp/" + std::string(filename);
  257. ESP_LOGD(TAG, "File to upload: %s", filetosend.c_str());
  258. esp_err_t res = send_file(req, filetosend);
  259. if (res != ESP_OK)
  260. return res;
  261. /* Respond with an empty chunk to signal HTTP response completion */
  262. httpd_resp_send_chunk(req, NULL, 0);
  263. return ESP_OK;
  264. }
  265. esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
  266. {
  267. #ifdef DEBUG_DETAIL_ON
  268. LogFile.WriteHeapInfo("img_tmp_virtual_handler - Start");
  269. #endif
  270. char filepath[50];
  271. ESP_LOGD(TAG, "uri: %s", req->uri);
  272. char *base_path = (char*) req->user_ctx;
  273. std::string filetosend(base_path);
  274. const char *filename = get_path_from_uri(filepath, base_path,
  275. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  276. ESP_LOGD(TAG, "1 uri: %s, filename: %s, filepath: %s", req->uri, filename, filepath);
  277. filetosend = std::string(filename);
  278. ESP_LOGD(TAG, "File to upload: %s", filetosend.c_str());
  279. if (filetosend == "raw.jpg")
  280. {
  281. return GetRawJPG(req);
  282. }
  283. esp_err_t zw = GetJPG(filetosend, req);
  284. if (zw == ESP_OK)
  285. return ESP_OK;
  286. // File wird nicht intern bereit gestellt --> klassischer weg:
  287. #ifdef DEBUG_DETAIL_ON
  288. LogFile.WriteHeapInfo("img_tmp_virtual_handler - Done");
  289. #endif
  290. return img_tmp_handler(req);
  291. }
  292. esp_err_t sysinfo_handler(httpd_req_t *req)
  293. {
  294. const char* resp_str;
  295. std::string zw;
  296. std::string cputemp = std::to_string(temperatureRead());
  297. std::string gitversion = libfive_git_version();
  298. std::string buildtime = build_time();
  299. std::string gitbranch = libfive_git_branch();
  300. std::string gittag = libfive_git_version();
  301. std::string gitrevision = libfive_git_revision();
  302. std::string htmlversion = getHTMLversion();
  303. char freeheapmem[11];
  304. sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
  305. tcpip_adapter_ip_info_t ip_info;
  306. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  307. const char *hostname;
  308. ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));
  309. zw = "[\
  310. {\
  311. \"firmware\" : \"" + gitversion + "\",\
  312. \"buildtime\" : \"" + buildtime + "\",\
  313. \"gitbranch\" : \"" + gitbranch + "\",\
  314. \"gittag\" : \"" + gittag + "\",\
  315. \"gitrevision\" : \"" + gitrevision + "\",\
  316. \"html\" : \"" + htmlversion + "\",\
  317. \"cputemp\" : \"" + cputemp + "\",\
  318. \"hostname\" : \"" + hostname + "\",\
  319. \"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\",\
  320. \"freeHeapMem\" : \"" + freeheapmem + "\"\
  321. }\
  322. ]";
  323. resp_str = zw.c_str();
  324. httpd_resp_set_type(req, "application/json");
  325. httpd_resp_send(req, resp_str, strlen(resp_str));
  326. /* Respond with an empty chunk to signal HTTP response completion */
  327. httpd_resp_send_chunk(req, NULL, 0);
  328. return ESP_OK;
  329. }
  330. void register_server_main_uri(httpd_handle_t server, const char *base_path)
  331. {
  332. httpd_uri_t info_get_handle = {
  333. .uri = "/info", // Match all URIs of type /path/to/file
  334. .method = HTTP_GET,
  335. .handler = info_get_handler,
  336. .user_ctx = (void*) base_path // Pass server data as context
  337. };
  338. httpd_register_uri_handler(server, &info_get_handle);
  339. httpd_uri_t sysinfo_handle = {
  340. .uri = "/sysinfo", // Match all URIs of type /path/to/file
  341. .method = HTTP_GET,
  342. .handler = sysinfo_handler,
  343. .user_ctx = (void*) base_path // Pass server data as context
  344. };
  345. httpd_register_uri_handler(server, &sysinfo_handle);
  346. httpd_uri_t starttime_tmp_handle = {
  347. .uri = "/starttime", // Match all URIs of type /path/to/file
  348. .method = HTTP_GET,
  349. .handler = starttime_get_handler,
  350. .user_ctx = NULL // Pass server data as context
  351. };
  352. httpd_register_uri_handler(server, &starttime_tmp_handle);
  353. httpd_uri_t img_tmp_handle = {
  354. .uri = "/img_tmp/*", // Match all URIs of type /path/to/file
  355. .method = HTTP_GET,
  356. .handler = img_tmp_virtual_handler,
  357. .user_ctx = (void*) base_path // Pass server data as context
  358. };
  359. httpd_register_uri_handler(server, &img_tmp_handle);
  360. httpd_uri_t main_rest_handle = {
  361. .uri = "/*", // Match all URIs of type /path/to/file
  362. .method = HTTP_GET,
  363. .handler = hello_main_handler,
  364. .user_ctx = (void*) base_path // Pass server data as context
  365. };
  366. httpd_register_uri_handler(server, &main_rest_handle);
  367. }
  368. httpd_handle_t start_webserver(void)
  369. {
  370. httpd_handle_t server = NULL;
  371. httpd_config_t config = { };
  372. config.task_priority = tskIDLE_PRIORITY+3; //20221211: before: tskIDLE_PRIORITY+1; // 20210924 --> vorher +5
  373. config.stack_size = 32768; //20210921 --> vorher 32768 // bei 32k stürzt das Programm beim Bilderaufnehmen ab
  374. config.core_id = 0; //20221211 --> force all not flow related tasks to CPU0, before: tskNO_AFFINITY;
  375. config.server_port = 80;
  376. config.ctrl_port = 32768;
  377. config.max_open_sockets = 5; //20210921 --> vorher 7
  378. config.max_uri_handlers = 37; // vorher 24, 20220511: 35
  379. config.max_resp_headers = 8;
  380. config.backlog_conn = 5;
  381. config.lru_purge_enable = true; // dadurch werden alte Verbindungen gekappt, falls neue benögt werden.
  382. config.recv_wait_timeout = 5; // default: 5 20210924 --> vorher 30
  383. config.send_wait_timeout = 5; // default: 5 20210924 --> vorher 30
  384. config.global_user_ctx = NULL;
  385. config.global_user_ctx_free_fn = NULL;
  386. config.global_transport_ctx = NULL;
  387. config.global_transport_ctx_free_fn = NULL;
  388. config.open_fn = NULL;
  389. config.close_fn = NULL;
  390. // config.uri_match_fn = NULL;
  391. config.uri_match_fn = httpd_uri_match_wildcard;
  392. starttime = gettimestring("%Y%m%d-%H%M%S");
  393. // Start the httpd server
  394. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  395. if (httpd_start(&server, &config) == ESP_OK) {
  396. // Set URI handlers
  397. ESP_LOGI(TAG, "Registering URI handlers");
  398. return server;
  399. }
  400. ESP_LOGI(TAG, "Error starting server!");
  401. return NULL;
  402. }
  403. void stop_webserver(httpd_handle_t server)
  404. {
  405. httpd_stop(server);
  406. }
  407. void disconnect_handler(void* arg, esp_event_base_t event_base,
  408. int32_t event_id, void* event_data)
  409. {
  410. httpd_handle_t* server = (httpd_handle_t*) arg;
  411. if (*server) {
  412. ESP_LOGI(TAG, "Stopping webserver");
  413. stop_webserver(*server);
  414. *server = NULL;
  415. }
  416. }
  417. void connect_handler(void* arg, esp_event_base_t event_base,
  418. int32_t event_id, void* event_data)
  419. {
  420. httpd_handle_t* server = (httpd_handle_t*) arg;
  421. if (*server == NULL) {
  422. ESP_LOGI(TAG, "Starting webserver");
  423. *server = start_webserver();
  424. }
  425. }