server_main.cpp 13 KB

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