server_main.cpp 14 KB

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