server_main.cpp 13 KB

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