server_main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. printf("uri: %s\n", req->uri);
  113. int _pos;
  114. esp_err_t res;
  115. char *base_path = (char*) req->user_ctx;
  116. std::string filetosend(base_path);
  117. const char *filename = get_path_from_uri(filepath, base_path,
  118. req->uri - 1, sizeof(filepath));
  119. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  120. if ((strcmp(req->uri, "/") == 0))
  121. {
  122. {
  123. filetosend = filetosend + "/html/index.html";
  124. }
  125. }
  126. else
  127. {
  128. filetosend = filetosend + "/html" + std::string(req->uri);
  129. _pos = filetosend.find("?");
  130. if (_pos > -1){
  131. filetosend = filetosend.substr(0, _pos);
  132. }
  133. }
  134. if (filetosend == "/sdcard/html/index.html" && isSetupModusActive()) {
  135. printf("System ist im Setupmodus --> index.html --> setup.html");
  136. filetosend = "/sdcard/html/setup.html";
  137. }
  138. printf("Filename: %s\n", filename);
  139. printf("File requested: %s\n", filetosend.c_str());
  140. if (!filename) {
  141. ESP_LOGE(TAG, "Filename is too long");
  142. /* Respond with 500 Internal Server Error */
  143. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long");
  144. return ESP_FAIL;
  145. }
  146. res = send_file(req, filetosend);
  147. if (res != ESP_OK)
  148. return res;
  149. /* Respond with an empty chunk to signal HTTP response completion */
  150. httpd_resp_send_chunk(req, NULL, 0);
  151. return ESP_OK;
  152. }
  153. esp_err_t img_tmp_handler(httpd_req_t *req)
  154. {
  155. char filepath[50];
  156. printf("uri: %s\n", req->uri);
  157. char *base_path = (char*) req->user_ctx;
  158. std::string filetosend(base_path);
  159. const char *filename = get_path_from_uri(filepath, base_path,
  160. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  161. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  162. filetosend = filetosend + "/img_tmp/" + std::string(filename);
  163. printf("File to upload: %s\n", filetosend.c_str());
  164. esp_err_t res = send_file(req, filetosend);
  165. if (res != ESP_OK)
  166. return res;
  167. /* Respond with an empty chunk to signal HTTP response completion */
  168. httpd_resp_send_chunk(req, NULL, 0);
  169. return ESP_OK;
  170. }
  171. esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
  172. {
  173. char filepath[50];
  174. printf("uri: %s\n", req->uri);
  175. char *base_path = (char*) req->user_ctx;
  176. std::string filetosend(base_path);
  177. const char *filename = get_path_from_uri(filepath, base_path,
  178. req->uri + sizeof("/img_tmp/") - 1, sizeof(filepath));
  179. printf("1 uri: %s, filename: %s, filepath: %s\n", req->uri, filename, filepath);
  180. filetosend = std::string(filename);
  181. printf("File to upload: %s\n", filetosend.c_str());
  182. if (filetosend == "raw.jpg")
  183. {
  184. return GetRawJPG(req);
  185. }
  186. esp_err_t zw = GetJPG(filetosend, req);
  187. if (zw == ESP_OK)
  188. return ESP_OK;
  189. // File wird nicht intern bereit gestellt --> klassischer weg:
  190. return img_tmp_handler(req);
  191. }
  192. esp_err_t sysinfo_handler(httpd_req_t *req)
  193. {
  194. const char* resp_str;
  195. std::string zw;
  196. std::string cputemp = std::to_string(temperatureRead());
  197. std::string gitversion = libfive_git_version();
  198. std::string buildtime = build_time();
  199. std::string gitbranch = libfive_git_branch();
  200. std::string gitbasebranch = git_base_branch();
  201. std::string htmlversion = getHTMLversion();
  202. tcpip_adapter_ip_info_t ip_info;
  203. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
  204. const char *hostname;
  205. ESP_ERROR_CHECK(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname));
  206. zw = "[\
  207. {\
  208. \"firmware\" : \"" + gitversion + "\",\
  209. \"buildtime\" : \"" + buildtime + "\",\
  210. \"gitbranch\" : \"" + gitbranch + "\",\
  211. \"gitbasebranch\" : \"" + gitbasebranch + "\",\
  212. \"html\" : \"" + htmlversion + "\",\
  213. \"cputemp\" : \"" + cputemp + "\",\
  214. \"hostname\" : \"" + hostname + "\",\
  215. \"IPv4\" : \"" + ip4addr_ntoa(&ip_info.ip) + "\"\
  216. }\
  217. ]";
  218. resp_str = zw.c_str();
  219. httpd_resp_set_type(req, "application/json");
  220. httpd_resp_send(req, resp_str, strlen(resp_str));
  221. /* Respond with an empty chunk to signal HTTP response completion */
  222. httpd_resp_send_chunk(req, NULL, 0);
  223. return ESP_OK;
  224. }
  225. void register_server_main_uri(httpd_handle_t server, const char *base_path)
  226. {
  227. httpd_uri_t info_get_handle = {
  228. .uri = "/version", // Match all URIs of type /path/to/file
  229. .method = HTTP_GET,
  230. .handler = info_get_handler,
  231. .user_ctx = (void*) base_path // Pass server data as context
  232. };
  233. httpd_register_uri_handler(server, &info_get_handle);
  234. httpd_uri_t sysinfo_handle = {
  235. .uri = "/sysinfo", // Match all URIs of type /path/to/file
  236. .method = HTTP_GET,
  237. .handler = sysinfo_handler,
  238. .user_ctx = (void*) base_path // Pass server data as context
  239. };
  240. httpd_register_uri_handler(server, &sysinfo_handle);
  241. httpd_uri_t starttime_tmp_handle = {
  242. .uri = "/starttime", // Match all URIs of type /path/to/file
  243. .method = HTTP_GET,
  244. .handler = starttime_get_handler,
  245. .user_ctx = NULL // Pass server data as context
  246. };
  247. httpd_register_uri_handler(server, &starttime_tmp_handle);
  248. httpd_uri_t img_tmp_handle = {
  249. .uri = "/img_tmp/*", // Match all URIs of type /path/to/file
  250. .method = HTTP_GET,
  251. .handler = img_tmp_virtual_handler,
  252. .user_ctx = (void*) base_path // Pass server data as context
  253. };
  254. httpd_register_uri_handler(server, &img_tmp_handle);
  255. httpd_uri_t main_rest_handle = {
  256. .uri = "/*", // Match all URIs of type /path/to/file
  257. .method = HTTP_GET,
  258. .handler = hello_main_handler,
  259. .user_ctx = (void*) base_path // Pass server data as context
  260. };
  261. httpd_register_uri_handler(server, &main_rest_handle);
  262. }
  263. httpd_handle_t start_webserver(void)
  264. {
  265. httpd_handle_t server = NULL;
  266. httpd_config_t config = { };
  267. config.task_priority = tskIDLE_PRIORITY+5;
  268. config.stack_size = 16384; // bei 32k stürzt das Programm beim Bilderaufnehmen ab
  269. config.core_id = tskNO_AFFINITY;
  270. config.server_port = 80;
  271. config.ctrl_port = 32768;
  272. config.max_open_sockets = 7;
  273. config.max_uri_handlers = 24;
  274. config.max_resp_headers = 8;
  275. config.backlog_conn = 5;
  276. config.lru_purge_enable = false;
  277. config.recv_wait_timeout = 30; // default: 5
  278. config.send_wait_timeout = 30; // default: 5
  279. config.global_user_ctx = NULL;
  280. config.global_user_ctx_free_fn = NULL;
  281. config.global_transport_ctx = NULL;
  282. config.global_transport_ctx_free_fn = NULL;
  283. config.open_fn = NULL;
  284. config.close_fn = NULL;
  285. // config.uri_match_fn = NULL;
  286. config.uri_match_fn = httpd_uri_match_wildcard;
  287. starttime = gettimestring("%Y%m%d-%H%M%S");
  288. // Start the httpd server
  289. ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
  290. if (httpd_start(&server, &config) == ESP_OK) {
  291. // Set URI handlers
  292. ESP_LOGI(TAG, "Registering URI handlers");
  293. return server;
  294. }
  295. ESP_LOGI(TAG, "Error starting server!");
  296. return NULL;
  297. }
  298. void stop_webserver(httpd_handle_t server)
  299. {
  300. httpd_stop(server);
  301. }
  302. void disconnect_handler(void* arg, esp_event_base_t event_base,
  303. int32_t event_id, void* event_data)
  304. {
  305. httpd_handle_t* server = (httpd_handle_t*) arg;
  306. if (*server) {
  307. ESP_LOGI(TAG, "Stopping webserver");
  308. stop_webserver(*server);
  309. *server = NULL;
  310. }
  311. }
  312. void connect_handler(void* arg, esp_event_base_t event_base,
  313. int32_t event_id, void* event_data)
  314. {
  315. httpd_handle_t* server = (httpd_handle_t*) arg;
  316. if (*server == NULL) {
  317. ESP_LOGI(TAG, "Starting webserver");
  318. *server = start_webserver();
  319. }
  320. }