server_main.cpp 15 KB

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