server_main.cpp 12 KB

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