server_camera.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "server_camera.h"
  2. #include <string>
  3. #include "string.h"
  4. #include "esp_camera.h"
  5. #include "ClassControllCamera.h"
  6. #include "MainFlowControl.h"
  7. #include "ClassLogFile.h"
  8. #include "esp_log.h"
  9. #include "basic_auth.h"
  10. #include "../../include/defines.h"
  11. static const char *TAG = "server_cam";
  12. void PowerResetCamera()
  13. {
  14. // Use reset only if pin is available
  15. if (CAM_PIN_PWDN == GPIO_NUM_NC)
  16. {
  17. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "No power down pin availbale to reset camera");
  18. }
  19. else
  20. {
  21. ESP_LOGD(TAG, "Resetting camera by power down line");
  22. gpio_config_t conf = {
  23. .pin_bit_mask = 1ULL << CAM_PIN_PWDN,
  24. .mode = GPIO_MODE_OUTPUT,
  25. .pull_up_en = GPIO_PULLUP_DISABLE,
  26. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  27. .intr_type = GPIO_INTR_DISABLE,
  28. };
  29. gpio_config(&conf);
  30. // carefull, logic is inverted compared to reset pin
  31. ESP_LOGD(TAG, "Camera power down");
  32. gpio_set_level(CAM_PIN_PWDN, 1);
  33. vTaskDelay(pdMS_TO_TICKS(1000));
  34. ESP_LOGD(TAG, "Camera power up");
  35. gpio_set_level(CAM_PIN_PWDN, 0);
  36. vTaskDelay(pdMS_TO_TICKS(1000));
  37. }
  38. }
  39. esp_err_t handler_lightOn(httpd_req_t *req)
  40. {
  41. #ifdef DEBUG_DETAIL_ON
  42. LogFile.WriteHeapInfo("handler_lightOn - Start");
  43. ESP_LOGD(TAG, "handler_lightOn uri: %s", req->uri);
  44. #endif
  45. if (Camera.getCameraInitSuccessful())
  46. {
  47. Camera.LightOnOff(true);
  48. const char *resp_str = (const char *)req->user_ctx;
  49. httpd_resp_send(req, resp_str, strlen(resp_str));
  50. }
  51. else
  52. {
  53. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lighton not available!");
  54. return ESP_ERR_NOT_FOUND;
  55. }
  56. #ifdef DEBUG_DETAIL_ON
  57. LogFile.WriteHeapInfo("handler_lightOn - Done");
  58. #endif
  59. return ESP_OK;
  60. }
  61. esp_err_t handler_lightOff(httpd_req_t *req)
  62. {
  63. #ifdef DEBUG_DETAIL_ON
  64. LogFile.WriteHeapInfo("handler_lightOff - Start");
  65. ESP_LOGD(TAG, "handler_lightOff uri: %s", req->uri);
  66. #endif
  67. if (Camera.getCameraInitSuccessful())
  68. {
  69. Camera.LightOnOff(false);
  70. const char *resp_str = (const char *)req->user_ctx;
  71. httpd_resp_send(req, resp_str, strlen(resp_str));
  72. }
  73. else
  74. {
  75. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /lightoff not available!");
  76. return ESP_ERR_NOT_FOUND;
  77. }
  78. #ifdef DEBUG_DETAIL_ON
  79. LogFile.WriteHeapInfo("handler_lightOff - Done");
  80. #endif
  81. return ESP_OK;
  82. }
  83. esp_err_t handler_capture(httpd_req_t *req)
  84. {
  85. #ifdef DEBUG_DETAIL_ON
  86. LogFile.WriteHeapInfo("handler_capture - Start");
  87. #endif
  88. if (Camera.getCameraInitSuccessful())
  89. {
  90. #ifdef DEBUG_DETAIL_ON
  91. ESP_LOGD(TAG, "Size: %d, Quality: %d", CCstatus.ImageFrameSize, CCstatus.ImageQuality);
  92. #endif
  93. esp_err_t result;
  94. result = Camera.CaptureToHTTP(req);
  95. #ifdef DEBUG_DETAIL_ON
  96. LogFile.WriteHeapInfo("handler_capture - Done");
  97. #endif
  98. return result;
  99. }
  100. else
  101. {
  102. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture not available!");
  103. return ESP_ERR_NOT_FOUND;
  104. }
  105. }
  106. esp_err_t handler_capture_with_light(httpd_req_t *req)
  107. {
  108. #ifdef DEBUG_DETAIL_ON
  109. LogFile.WriteHeapInfo("handler_capture_with_light - Start");
  110. #endif
  111. if (Camera.getCameraInitSuccessful())
  112. {
  113. char _query[100];
  114. char _delay[10];
  115. int delay = 2500;
  116. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  117. {
  118. ESP_LOGD(TAG, "Query: %s", _query);
  119. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  120. {
  121. #ifdef DEBUG_DETAIL_ON
  122. ESP_LOGD(TAG, "Delay: %s", _delay);
  123. #endif
  124. delay = atoi(_delay);
  125. if (delay < 0)
  126. {
  127. delay = 0;
  128. }
  129. }
  130. }
  131. #ifdef DEBUG_DETAIL_ON
  132. ESP_LOGD(TAG, "Size: %d, Quality: %d", CCstatus.ImageFrameSize, CCstatus.ImageQuality);
  133. #endif
  134. const esp_err_t result = Camera.CaptureToHTTP(req, delay);
  135. #ifdef DEBUG_DETAIL_ON
  136. LogFile.WriteHeapInfo("handler_capture_with_light - Done");
  137. #endif
  138. return result;
  139. }
  140. else
  141. {
  142. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /capture_with_flashlight not available!");
  143. return ESP_ERR_NOT_FOUND;
  144. }
  145. }
  146. esp_err_t handler_capture_save_to_file(httpd_req_t *req)
  147. {
  148. #ifdef DEBUG_DETAIL_ON
  149. LogFile.WriteHeapInfo("handler_capture_save_to_file - Start");
  150. #endif
  151. if (Camera.getCameraInitSuccessful())
  152. {
  153. char _query[100];
  154. char _delay[10];
  155. int delay = 0;
  156. char filename[100] = {0};
  157. std::string fn = "/sdcard/";
  158. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  159. {
  160. ESP_LOGD(TAG, "Query: %s", _query);
  161. if (httpd_query_key_value(_query, "filename", filename, sizeof(filename)) == ESP_OK)
  162. {
  163. if (filename[0] == '\0' || strchr(filename, '/') != NULL ||
  164. strchr(filename, '\\') != NULL || strcmp(filename, ".") == 0 ||
  165. strcmp(filename, "..") == 0)
  166. {
  167. httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid filename");
  168. return ESP_ERR_INVALID_ARG;
  169. }
  170. fn.append(filename);
  171. #ifdef DEBUG_DETAIL_ON
  172. ESP_LOGD(TAG, "Filename: %s", fn.c_str());
  173. #endif
  174. }
  175. else
  176. {
  177. fn.append("noname.jpg");
  178. }
  179. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  180. {
  181. #ifdef DEBUG_DETAIL_ON
  182. ESP_LOGD(TAG, "Delay: %s", _delay);
  183. #endif
  184. delay = atoi(_delay);
  185. if (delay < 0)
  186. {
  187. delay = 0;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. fn.append("noname.jpg");
  194. }
  195. #ifdef DEBUG_DETAIL_ON
  196. ESP_LOGD(TAG, "Size: %d, Quality: %d", CCstatus.ImageFrameSize, CCstatus.ImageQuality);
  197. #endif
  198. const esp_err_t result = Camera.CaptureToFile(fn, delay);
  199. if (result != ESP_OK)
  200. {
  201. if (result == ESP_ERR_TIMEOUT)
  202. {
  203. httpd_resp_set_status(req, "503 Service Unavailable");
  204. httpd_resp_send(req, "Camera is busy", HTTPD_RESP_USE_STRLEN);
  205. }
  206. else
  207. {
  208. httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
  209. "Image capture or file save failed");
  210. }
  211. return result;
  212. }
  213. const esp_err_t responseResult = httpd_resp_send(req, fn.c_str(), fn.length());
  214. #ifdef DEBUG_DETAIL_ON
  215. LogFile.WriteHeapInfo("handler_capture_save_to_file - Done");
  216. #endif
  217. return responseResult;
  218. }
  219. else
  220. {
  221. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Camera not initialized: REST API /save not available!");
  222. return ESP_ERR_NOT_FOUND;
  223. }
  224. }
  225. void register_server_camera_uri(httpd_handle_t server)
  226. {
  227. #ifdef DEBUG_DETAIL_ON
  228. ESP_LOGI(TAG, "server_part_camera - Registering URI handlers");
  229. #endif
  230. httpd_uri_t camuri = {};
  231. camuri.method = HTTP_GET;
  232. camuri.uri = "/lighton";
  233. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_lightOn);
  234. camuri.user_ctx = (void *)"Light On";
  235. httpd_register_uri_handler(server, &camuri);
  236. camuri.uri = "/lightoff";
  237. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_lightOff);
  238. camuri.user_ctx = (void *)"Light Off";
  239. httpd_register_uri_handler(server, &camuri);
  240. camuri.uri = "/capture";
  241. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_capture);
  242. camuri.user_ctx = NULL;
  243. httpd_register_uri_handler(server, &camuri);
  244. camuri.uri = "/capture_with_flashlight";
  245. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_capture_with_light);
  246. camuri.user_ctx = NULL;
  247. httpd_register_uri_handler(server, &camuri);
  248. camuri.uri = "/save";
  249. camuri.handler = APPLY_BASIC_AUTH_FILTER(handler_capture_save_to_file);
  250. camuri.user_ctx = NULL;
  251. httpd_register_uri_handler(server, &camuri);
  252. }