server_main.cpp 13 KB

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