dialog_channel_run.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <curses.h>
  16. #define LOG_LEVEL 3
  17. #define LOG_PREFIX "dlg_chn_run"
  18. #include <syslog.h>
  19. #include <camera_manager.h>
  20. #include <camera_string.h>
  21. #include "param.h"
  22. #include "platform_action.h"
  23. #include "app_dialogs.h"
  24. extern cams_t *cam_session;
  25. #define _CHECK_SESSION_RETURN() \
  26. do { \
  27. if (session == NULL) { \
  28. LOG_E("session is NULL\n"); \
  29. return -EPERM; \
  30. } \
  31. } while (0)
  32. static int do_camera_event_action(cams_t *session, csi_camera_event_s *event)
  33. {
  34. _CHECK_SESSION_RETURN();
  35. int ret = 0;
  36. for (uint32_t i = 0; i < CSI_CAMERA_EVENT_MAX_COUNT; i++) {
  37. camera_event_action_t *event_action =
  38. &(session->camera_event_action[i].camera);
  39. if (event_action->event != event->id)
  40. continue;
  41. LOG_D("Found camera action:%s\n",
  42. camera_string_camera_action(event_action->action));
  43. if(event_action->action == CAMERA_ACTION_NONE) {
  44. LOG_D("CHANNEL CAMERA_ACTION_NONE!\n");
  45. continue;
  46. }
  47. if(event_action->action > (CAMERA_ACTION_LOG_PRINT|CAMERA_ACTION_CAPTURE_FRAME)) {
  48. LOG_E("Unkonw camera event action: %d\n", event_action->action);
  49. ret = -1;
  50. }
  51. for(uint32_t j = 0; j < CAMERA_ACTION_MAX_COUNT; j++) {
  52. switch (event_action->action & 1 << j) {
  53. case CAMERA_ACTION_LOG_PRINT:
  54. LOG_D("CAMERA CAMERA_ACTION_LOG_PRINT\n");
  55. if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_WARNING) {
  56. LOG_D("LOG_PRINT CSI_CAMERA_EVENT_WARNING\n");
  57. } else if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_ERROR) {
  58. LOG_D("LOG PRINT CSI_CAMERA_EVENT_ERROR\n");
  59. } else if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_SENSOR_FIRST_IMAGE_ARRIVE) {
  60. LOG_D("LOG PRINT CSI_CAMERA_EVENT_SENSOR_FIRST_IMAGE_ARRIVE\n");
  61. } else if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_ISP_3A_ADJUST_READY) {
  62. LOG_D("LOG PRINT CSI_CAMERA_EVENT_ISP_3A_ADJUST_READY\n");
  63. } else {
  64. ret = -1;
  65. LOG_E("CAMERA CAMERA_ACTION_LOG_ERROR\n");
  66. }
  67. break;
  68. case CAMERA_ACTION_CAPTURE_FRAME:
  69. LOG_D("CAMERA CAMERA_ACTION_CAPTURE_FRAME\n");
  70. if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_WARNING) {
  71. LOG_D("CAPTURE FRAME CSI_CAMERA_EVENT_WARNING\n");
  72. } else if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_ERROR) {
  73. LOG_D("CAPTURE FRAME CSI_CAMERA_EVENT_ERROR\n");
  74. } else if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_SENSOR_FIRST_IMAGE_ARRIVE) {
  75. LOG_D("CAPTURE FRAME CSI_CAMERA_EVENT_SENSOR_FIRST_IMAGE_ARRIVE\n");
  76. } else if((event_action->event & 1 << i) == CSI_CAMERA_EVENT_ISP_3A_ADJUST_READY) {
  77. LOG_D("CAPTURE FRAME CSI_CAMERA_EVENT_ISP_3A_ADJUST_READY\n");
  78. } else {
  79. ret = -1;
  80. LOG_D("CAMERA CAMERA_ACTION_CAPTURE_FRAME_ERROR\n");
  81. }
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. }
  88. return 0;
  89. }
  90. static int do_channel_event_action(cams_t *session, csi_camera_event_s *event)
  91. {
  92. _CHECK_SESSION_RETURN();
  93. csi_frame_s frame;
  94. int ret = 0;
  95. camera_event_action_union_t *event_action_u =
  96. session->channel_event_action[event->type - CSI_CAMERA_EVENT_TYPE_CHANNEL0];
  97. LOG_D("do_channel_event_action()!\n");
  98. for (uint32_t i = 0; i < CSI_CAMERA_CHANNEL_EVENT_MAX_COUNT; i++) {
  99. camera_channel_event_action_t *event_action = &(event_action_u[i].channel);
  100. if (event_action->event != event->id)
  101. continue;
  102. if (event_action->action == 0) {
  103. printf("event_action->action == 0\n");
  104. break;
  105. }
  106. LOG_D("Found channel action:%s\n",
  107. camera_string_channel_action(event_action->action));
  108. if(event_action->action == CAMERA_CHANNEL_ACTION_NONE) {
  109. LOG_D("CHANNEL CAMERA_ACTION_NONE!\n");
  110. continue;
  111. }
  112. if(event_action->action > (CAMERA_CHANNEL_ACTION_LOG_PRINT|CAMERA_CHANNEL_ACTION_CAPTURE_FRAME|CAMERA_CHANNEL_ACTION_DISPLAY_FRAME)) {
  113. ret = -1;
  114. LOG_E("Unkonw channel event action: %d\n", event_action->action);
  115. }
  116. for(uint32_t j = 0; j < CAMERA_CHANNEL_ACTION_MAX_COUNT; j++) {
  117. switch (event_action->action & 1 << j) {
  118. case CAMERA_CHANNEL_ACTION_LOG_PRINT:
  119. LOG_D("CHANNEL CAMERA_ACTION_LOG_PRINT\n");
  120. if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_FRAME_READY) {
  121. LOG_D("LOG_PRINT CSI_CAMERA_CHANNEL_EVENT_FRAME_READY\n");
  122. } else if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_FRAME_PUT) {
  123. LOG_D("LOG_PRINT CSI_CAMERA_CHANNEL_EVENT_FRAME_PUT\n");
  124. } else if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_OVERFLOW) {
  125. LOG_D("LOG_PRINT CSI_CAMERA_CHANNEL_EVENT_OVERFLOW\n");
  126. } else {
  127. ret = -1;
  128. LOG_E("CHANNEL CAMERA_ACTION_LOG_ERROR\n");
  129. }
  130. break;
  131. case CAMERA_CHANNEL_ACTION_CAPTURE_FRAME:
  132. LOG_D("CHANNEL CAMERA_ACTION_CAPTURE_FRAME\n");
  133. /* To show the image can do below steps:
  134. * 1. save capture img to be /tmp/capture.jpg
  135. * 2. img2txt -W 80 -H 60 -f ansi /tmp/capture.jpg > /tmp/capture.txt
  136. * 3. malloc a img_buf and store /tmp/capture.txt into it
  137. * 4. printf(img_buf) to show on log console screen
  138. */
  139. if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_FRAME_READY) {
  140. int timeout = -1;
  141. int read_frame_count = csi_camera_get_frame_count(NULL, event->type - CSI_CAMERA_EVENT_TYPE_CHANNEL0);
  142. LOG_D("read_frame_count = %d\n",read_frame_count);
  143. for (int i = 0; i < read_frame_count; i++) {
  144. csi_camera_get_frame(NULL,event->type - CSI_CAMERA_EVENT_TYPE_CHANNEL0, &frame, timeout);
  145. if(frame.img.usr_addr[0] != NULL) {
  146. camera_action_image_save(&frame);
  147. csi_camera_put_frame(&frame);
  148. }
  149. }
  150. } else if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_FRAME_PUT) {
  151. LOG_D("CSI_CAMERA_CHANNEL_EVENT_FRAME_PUT\n");
  152. } else if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_OVERFLOW) {
  153. LOG_D("CSI_CAMERA_CHANNEL_EVENT_OVERFLOW\n");
  154. } else {
  155. ret = -1;
  156. LOG_E("CHANNEL CAMERA_ACTION_CAPTURE_FRAME_ERROR\n");
  157. }
  158. break;
  159. case CAMERA_CHANNEL_ACTION_DISPLAY_FRAME:
  160. LOG_D("CHANNEL CAMERA_ACTION_DISPLAY_FRAME");
  161. if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_FRAME_READY) {
  162. int timeout = -1;
  163. int read_frame_count = csi_camera_get_frame_count(NULL, event->type - CSI_CAMERA_EVENT_TYPE_CHANNEL0);
  164. LOG_D("read_frame_count = %d\n",read_frame_count);
  165. //for (int i = 0; i < read_frame_count; i++) {
  166. csi_camera_get_frame(NULL,event->type - CSI_CAMERA_EVENT_TYPE_CHANNEL0, &frame, timeout);
  167. if(frame.img.usr_addr[0] != NULL) {
  168. camera_action_image_display(&frame);
  169. LOG_D("frame->usr_addr = %p\n",frame.img.usr_addr[0]);
  170. csi_camera_put_frame(&frame);
  171. }
  172. //}
  173. } else if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_FRAME_PUT) {
  174. LOG_D("CSI_CAMERA_CHANNEL_EVENT_FRAME_PUT\n");
  175. } else if((event_action->event & 1 << i) == CSI_CAMERA_CHANNEL_EVENT_OVERFLOW) {
  176. LOG_D("CSI_CAMERA_CHANNEL_EVENT_OVERFLOW\n");
  177. } else {
  178. ret = -1;
  179. LOG_E("CHANNEL CAMERA_ACTION_DISPLAY_FRAME_ERROR\n");
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. }
  187. return ret;
  188. }
  189. /* action: 0:close, 1:open, 2:start/stop */
  190. int dialog_channel_run(void)
  191. {
  192. int ret_key = KEY_ESC;
  193. int ret;
  194. char str_buf[256];
  195. csi_camera_channel_cfg_s *selected_chn = NULL;
  196. if (cam_session == NULL) {
  197. LOG_E("cam_session is NULL\n");
  198. return KEY_ESC;
  199. }
  200. if (cam_session->camera_handle == NULL) {
  201. csi_camera_info_s *info =
  202. &(cam_session->camera_infos.info[cam_session->camera_id]);
  203. snprintf(str_buf, sizeof(str_buf),
  204. "Please open camera first\n");
  205. dialog_textbox_simple("Error", str_buf, 6, 30);
  206. return KEY_ESC;
  207. }
  208. again:
  209. item_reset();
  210. if (camera_channel_query_list(cam_session) != 0) {
  211. LOG_E("camera_channel_query_list() failed\n");
  212. snprintf(str_buf, sizeof(str_buf),
  213. "Failed to query channel list from Camera failed!\n");
  214. dialog_textbox_simple("Error", str_buf, 10, 40);
  215. return KEY_ESC;
  216. }
  217. /* Prepare for checklist nodes */
  218. for (int i = CSI_CAMERA_CHANNEL_0; i < CSI_CAMERA_CHANNEL_MAX_COUNT; i++) {
  219. csi_camera_channel_cfg_s *channel = &(cam_session->chn_cfg[i]);
  220. if (channel->status == CSI_CAMERA_CHANNEL_INVALID) {
  221. continue;
  222. }
  223. item_make("Channel[%d]", channel->chn_id);
  224. item_add_str(" (%s)", camera_string_chn_status(channel->status));
  225. item_set_data(channel);
  226. if (channel->status == CSI_CAMERA_CHANNEL_RUNNING || channel->status == CSI_CAMERA_CHANNEL_OPENED) {
  227. item_set_tag('X');
  228. item_set_selected(1);
  229. }
  230. }
  231. char *button_names[] = {"Start", "Stop", "Cancel"};
  232. ret = dialog_checklist(
  233. "Start/Stop Channel", /* Title */
  234. "Select the channel to start or stop",
  235. CHECKLIST_HEIGTH_MIN + 10,
  236. WIN_COLS - CHECKLIST_WIDTH_MIN - 16, 8,
  237. button_names, ARRAY_SIZE(button_names));
  238. int selected = item_activate_selected();
  239. LOG_D("ret=%d, selected=%s\n", ret, selected ? "true" : "false");
  240. switch (ret) {
  241. case 0: // button is: start
  242. if (!selected || item_data() == NULL)
  243. break;
  244. /* Show operation result */
  245. selected_chn = (csi_camera_channel_cfg_s *)item_data();
  246. if (selected_chn->status != CSI_CAMERA_CHANNEL_OPENED) {
  247. /* Show message bar */
  248. snprintf(str_buf, sizeof(str_buf),
  249. "The Camera[%d] channel[%d] can't start, current status is '%s'\n",
  250. cam_session->camera_id, selected_chn->chn_id,
  251. camera_string_chn_status(selected_chn->status));
  252. message(str_buf, 2);
  253. goto again;
  254. }
  255. camera_register_event_action(cam_session,
  256. do_camera_event_action,
  257. do_channel_event_action);
  258. LOG_D("camera_channel_start selected_chn->chn_id = %x\n",selected_chn->chn_id);
  259. ret = camera_channel_start(cam_session, selected_chn->chn_id);
  260. /* Show message bar */
  261. snprintf(str_buf, sizeof(str_buf),
  262. "Start Camera[%d] channel[%d] %s\n",
  263. cam_session->camera_id, selected_chn->chn_id,
  264. (ret == 0) ? "OK" : "failed");
  265. message(str_buf, (ret == 0) ? 1 : 2);
  266. goto again;
  267. break;
  268. case 1: // button is: stop
  269. if (!selected || item_data() == NULL)
  270. break;
  271. /* Show operation result */
  272. selected_chn = (csi_camera_channel_cfg_s *)item_data();
  273. if (selected_chn->status != CSI_CAMERA_CHANNEL_RUNNING) {
  274. /* Show message bar */
  275. snprintf(str_buf, sizeof(str_buf),
  276. "The Camera[%d] channel[%d] can't stop, current status is '%s'\n",
  277. cam_session->camera_id, selected_chn->chn_id,
  278. camera_string_chn_status(selected_chn->status));
  279. message(str_buf, 2);
  280. goto again;
  281. }
  282. ret = camera_channel_stop(cam_session, selected_chn->chn_id);
  283. /* Show message bar */
  284. snprintf(str_buf, sizeof(str_buf),
  285. "Stop Camera[%d] channel[%d] %s\n",
  286. cam_session->camera_id, selected_chn->chn_id,
  287. (ret == 0) ? "OK" : "failed");
  288. message(str_buf, (ret == 0) ? 1 : 2);
  289. goto again;
  290. break;
  291. case 2: // button is: cancel
  292. case KEY_ESC: // specific KEY
  293. ret = KEY_ESC;
  294. break;
  295. default:
  296. LOG_W("Oops? why return ret=%d\n", ret);
  297. ret = KEY_ESC;
  298. break;
  299. }
  300. LOG_D("ret=%d\n", ret);
  301. return ret;
  302. }