server_main.cpp 12 KB

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