server_main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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_send_chunk(req, NULL, 0);
  167. #ifdef DEBUG_DETAIL_ON
  168. LogFile.WriteHeapInfo("hello_main_handler - Stop");
  169. #endif
  170. return ESP_OK;
  171. }
  172. esp_err_t img_tmp_handler(httpd_req_t *req)
  173. {
  174. char filepath[50];
  175. printf("uri: %s\n", req->uri);
  176. char *base_path = (char*) req->user_ctx;
  177. std::string filetosend(base_path);
  178. const char *filename = get_path_from_uri(filepath, base_path,
  179. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  180. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  181. filetosend = filetosend + "/img_tmp/" + std::string(filename);
  182. printf("File to upload: %s\n", filetosend.c_str());
  183. esp_err_t res = send_file(req, filetosend);
  184. if (res != ESP_OK)
  185. return res;
  186. /* Respond with an empty chunk to signal HTTP response completion */
  187. httpd_resp_send_chunk(req, NULL, 0);
  188. return ESP_OK;
  189. }
  190. esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
  191. {
  192. #ifdef DEBUG_DETAIL_ON
  193. LogFile.WriteHeapInfo("img_tmp_virtual_handler - Start");
  194. #endif
  195. char filepath[50];
  196. printf("uri: %s\n", req->uri);
  197. char *base_path = (char*) req->user_ctx;
  198. std::string filetosend(base_path);
  199. const char *filename = get_path_from_uri(filepath, base_path,
  200. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  201. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  202. filetosend = std::string(filename);
  203. printf("File to upload: %s\n", filetosend.c_str());
  204. if (filetosend == "raw.jpg")
  205. {
  206. return GetRawJPG(req);
  207. }
  208. esp_err_t zw = GetJPG(filetosend, req);
  209. if (zw == ESP_OK)
  210. return ESP_OK;
  211. // File wird nicht intern bereit gestellt --> klassischer weg:
  212. #ifdef DEBUG_DETAIL_ON
  213. LogFile.WriteHeapInfo("img_tmp_virtual_handler - Done");
  214. #endif
  215. return img_tmp_handler(req);
  216. }
  217. esp_err_t sysinfo_handler(httpd_req_t *req)
  218. {
  219. #ifdef DEBUG_DETAIL_ON
  220. LogFile.WriteHeapInfo("sysinfo_handler - Start");
  221. #endif
  222. const char* resp_str;
  223. std::string zw;
  224. std::string cputemp = std::to_string(temperatureRead());
  225. std::string gitversion = libfive_git_version();
  226. std::string buildtime = build_time();
  227. std::string gitbranch = libfive_git_branch();
  228. std::string gitbasebranch = git_base_branch();
  229. std::string htmlversion = getHTMLversion();
  230. tcpip_adapter_ip_info_t ip_info;
  231. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  232. const char *hostname;
  233. ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));
  234. zw = "[\
  235. {\
  236. \"firmware\" : \"" + gitversion + "\",\
  237. \"buildtime\" : \"" + buildtime + "\",\
  238. \"gitbranch\" : \"" + gitbranch + "\",\
  239. \"gitbasebranch\" : \"" + gitbasebranch + "\",\
  240. \"html\" : \"" + htmlversion + "\",\
  241. \"cputemp\" : \"" + cputemp + "\",\
  242. \"hostname\" : \"" + hostname + "\",\
  243. \"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\"\
  244. }\
  245. ]";
  246. resp_str = zw.c_str();
  247. httpd_resp_set_type(req, "application/json");
  248. httpd_resp_send(req, resp_str, strlen(resp_str));
  249. /* Respond with an empty chunk to signal HTTP response completion */
  250. httpd_resp_send_chunk(req, NULL, 0);
  251. #ifdef DEBUG_DETAIL_ON
  252. LogFile.WriteHeapInfo("sysinfo_handler - Done");
  253. #endif
  254. return ESP_OK;
  255. }
  256. void register_server_main_uri(httpd_handle_t server, const char *base_path)
  257. {
  258. httpd_uri_t info_get_handle = {
  259. .uri = "/version", // Match all URIs of type /path/to/file
  260. .method = HTTP_GET,
  261. .handler = info_get_handler,
  262. .user_ctx = (void*) base_path // Pass server data as context
  263. };
  264. httpd_register_uri_handler(server, &info_get_handle);
  265. httpd_uri_t sysinfo_handle = {
  266. .uri = "/sysinfo", // Match all URIs of type /path/to/file
  267. .method = HTTP_GET,
  268. .handler = sysinfo_handler,
  269. .user_ctx = (void*) base_path // Pass server data as context
  270. };
  271. httpd_register_uri_handler(server, &sysinfo_handle);
  272. httpd_uri_t starttime_tmp_handle = {
  273. .uri = "/starttime", // Match all URIs of type /path/to/file
  274. .method = HTTP_GET,
  275. .handler = starttime_get_handler,
  276. .user_ctx = NULL // Pass server data as context
  277. };
  278. httpd_register_uri_handler(server, &starttime_tmp_handle);
  279. httpd_uri_t img_tmp_handle = {
  280. .uri = "/img_tmp/*", // Match all URIs of type /path/to/file
  281. .method = HTTP_GET,
  282. .handler = img_tmp_virtual_handler,
  283. .user_ctx = (void*) base_path // Pass server data as context
  284. };
  285. httpd_register_uri_handler(server, &img_tmp_handle);
  286. httpd_uri_t main_rest_handle = {
  287. .uri = "/*", // Match all URIs of type /path/to/file
  288. .method = HTTP_GET,
  289. .handler = hello_main_handler,
  290. .user_ctx = (void*) base_path // Pass server data as context
  291. };
  292. httpd_register_uri_handler(server, &main_rest_handle);
  293. }
  294. httpd_handle_t start_webserver(void)
  295. {
  296. httpd_handle_t server = NULL;
  297. httpd_config_t config = { };
  298. config.task_priority = tskIDLE_PRIORITY+5;
  299. config.stack_size = 16384; // bei 32k stürzt das Programm beim Bilderaufnehmen ab
  300. config.core_id = tskNO_AFFINITY;
  301. config.server_port = 80;
  302. config.ctrl_port = 32768;
  303. config.max_open_sockets = 7;
  304. config.max_uri_handlers = 24;
  305. config.max_resp_headers = 8;
  306. config.backlog_conn = 5;
  307. config.lru_purge_enable = false;
  308. config.recv_wait_timeout = 30; // default: 5
  309. config.send_wait_timeout = 30; // default: 5
  310. config.global_user_ctx = NULL;
  311. config.global_user_ctx_free_fn = NULL;
  312. config.global_transport_ctx = NULL;
  313. config.global_transport_ctx_free_fn = NULL;
  314. config.open_fn = NULL;
  315. config.close_fn = NULL;
  316. // config.uri_match_fn = NULL;
  317. config.uri_match_fn = httpd_uri_match_wildcard;
  318. starttime = gettimestring("%Y%m%d-%H%M%S");
  319. // Start the httpd server
  320. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  321. if (httpd_start(&server, &config) == ESP_OK) {
  322. // Set URI handlers
  323. ESP_LOGI(TAG, "Registering URI handlers");
  324. return server;
  325. }
  326. ESP_LOGI(TAG, "Error starting server!");
  327. return NULL;
  328. }
  329. void stop_webserver(httpd_handle_t server)
  330. {
  331. httpd_stop(server);
  332. }
  333. void disconnect_handler(void* arg, esp_event_base_t event_base,
  334. int32_t event_id, void* event_data)
  335. {
  336. httpd_handle_t* server = (httpd_handle_t*) arg;
  337. if (*server) {
  338. ESP_LOGI(TAG, "Stopping webserver");
  339. stop_webserver(*server);
  340. *server = NULL;
  341. }
  342. }
  343. void connect_handler(void* arg, esp_event_base_t event_base,
  344. int32_t event_id, void* event_data)
  345. {
  346. httpd_handle_t* server = (httpd_handle_t*) arg;
  347. if (*server == NULL) {
  348. ESP_LOGI(TAG, "Starting webserver");
  349. *server = start_webserver();
  350. }
  351. }