xiaoxiang781216 commented on code in PR #7798: URL: https://github.com/apache/nuttx/pull/7798#discussion_r1042998779
########## arch/sim/src/sim/sim_hostvideo.h: ########## @@ -0,0 +1,57 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_hostvideo.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_UP_VIDEO_HOST_H Review Comment: ```suggestion #ifndef __ARCH_SIM_SRC_SIM_SIM_HOSTVIDEO_H ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; Review Comment: ```suggestion } sim_video_priv_t; ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; Review Comment: ```suggestion return -ENOTTY; ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; Review Comment: ```suggestion static video_priv_t g_priv; ``` ########## boards/sim/sim/sim/src/sim_bringup.c: ########## @@ -291,6 +292,18 @@ int sim_bringup(void) } #endif +#ifdef CONFIG_SIM_VIDEO + /* Initialize and register the simulated framebuffer driver */ Review Comment: ```suggestion /* Initialize and register the simulated video driver */ ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, + imgsensor_fmt_to_v4l2(fmt[IMGDATA_FMT_MAIN].pixelformat), + interval->denominator, interval->numerator); +} + +static int sim_video_stop_capture(imgsensor_stream_type_t type) +{ + return 0; +} + +static int sim_video_data_init() +{ + memset(&priv, 0, sizeof(priv)); + priv.vdev = host_video_init(0, HOST_VIDEO_DEV_PATH); + if (priv.vdev == NULL) + { + return -ENODEV; + } + + return 0; +} + +static int sim_video_data_uninit() +{ + return 0; +} + +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval) +{ + return 0; +} + +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback) +{ + int ret; + + priv.capture_cb = callback; + ret = host_video_start_capture(priv.vdev); + if (ret < 0) + { + return ret; + } + + return 0; +} + +static int sim_video_data_stop_capture() +{ + priv.next_buf = (uintptr_t)NULL; + return host_video_stop_capture(priv.vdev); +} + +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size) +{ + if (!addr || (uintptr_t)(addr) & 0x1f) + { + return -EINVAL; + } + + return 0; +} + +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size) +{ + priv.next_buf = (uintptr_t)addr; + priv.buf_size = size; + return 0; +} + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat) +{ + uint32_t fourcc; + switch (pixelformat) + { + case IMGSENSOR_PIX_FMT_YUV420P: + fourcc = V4L2_PIX_FMT_YUV420; + break; + + case IMGSENSOR_PIX_FMT_YUYV: + fourcc = V4L2_PIX_FMT_YUYV; + break; + + case IMGSENSOR_PIX_FMT_JPEG_WITH_SUBIMG: + fourcc = V4L2_PIX_FMT_JPEG; + break; + + case IMGSENSOR_PIX_FMT_JPEG: + fourcc = V4L2_PIX_FMT_JPEG; + break; + + case IMGSENSOR_PIX_FMT_RGB565: + fourcc = V4L2_PIX_FMT_RGB565; + break; + + case IMGSENSOR_PIX_FMT_UYVY: + fourcc = V4L2_PIX_FMT_UYVY; + break; + + default: + + /* Unsupported format */ + + fourcc = 0; + } + + return fourcc; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int sim_video_initialize(void) +{ + imgsensor_register(&g_sim_video_ops); + imgdata_register(&g_sim_video_data_ops); + return 0; +} + +int sim_video_uninitialize(void) +{ + return 0; +} + +void sim_video_loop(void) +{ + int ret; + + if (priv.next_buf) + { + ret = host_video_dq_buf(priv.vdev, + (uint8_t *)priv.next_buf, priv.buf_size); Review Comment: why not change next_buf to uint8_t * to avoid the cast ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, + imgsensor_fmt_to_v4l2(fmt[IMGDATA_FMT_MAIN].pixelformat), + interval->denominator, interval->numerator); +} + +static int sim_video_stop_capture(imgsensor_stream_type_t type) +{ + return 0; +} + +static int sim_video_data_init() +{ + memset(&priv, 0, sizeof(priv)); + priv.vdev = host_video_init(0, HOST_VIDEO_DEV_PATH); Review Comment: move to sim_video_init ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, + imgsensor_fmt_to_v4l2(fmt[IMGDATA_FMT_MAIN].pixelformat), + interval->denominator, interval->numerator); +} + +static int sim_video_stop_capture(imgsensor_stream_type_t type) +{ + return 0; +} + +static int sim_video_data_init() +{ + memset(&priv, 0, sizeof(priv)); + priv.vdev = host_video_init(0, HOST_VIDEO_DEV_PATH); + if (priv.vdev == NULL) + { + return -ENODEV; + } + + return 0; +} + +static int sim_video_data_uninit() +{ + return 0; +} + +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval) +{ + return 0; +} + +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback) +{ + int ret; + + priv.capture_cb = callback; + ret = host_video_start_capture(priv.vdev); Review Comment: ```suggestion return host_video_start_capture(priv.vdev); ``` ########## arch/sim/src/sim/posix/sim_host_v4l2.c: ########## @@ -0,0 +1,358 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_host_v4l2.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <linux/videodev2.h> +#include <sys/mman.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAX_REQBUFS 3 +#define MAX_VIDEO_DEVS 1 + +#define WARN(fmt, ...) \ + syslog(LOG_WARNING, "sim_host_video: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + int fd; + struct v4l2_requestbuffers reqbuf; + void *addrs[MAX_REQBUFS]; + size_t buflen[MAX_REQBUFS]; +} _host_video_dev_t; Review Comment: ```suggestion } host_video_dev_t; ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ Review Comment: all "video" in the comment change to "Video" ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; Review Comment: ```suggestion return -ENOTTY; ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH Review Comment: remove, let' use CONFIG_HOST_VIDEO_DEV_PATH directly ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ Review Comment: ```suggestion /* Video image data operations */ ``` ########## arch/sim/Kconfig: ########## @@ -265,6 +265,33 @@ endchoice endif +config SIM_VIDEO + bool "Simulated video support" + depends on VIDEO + default y + +if SIM_VIDEO + +choice + prompt "Simulated video device type" + default SIM_VIDEO_V4L2 + +config SIM_VIDEO_V4L2 + bool "V4L2 camera support on sim" + depends on HOST_LINUX + +endchoice + +config HOST_VIDEO_DEV_PATH + string "Host video device path" + default "/dev/video0" + +config VIDEO_DEV_PATH Review Comment: ```suggestion config SIM_VIDEO_DEV_PATH ``` ########## arch/sim/src/sim/sim_hostvideo.h: ########## @@ -0,0 +1,57 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_hostvideo.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_UP_VIDEO_HOST_H +#define __ARCH_SIM_SRC_SIM_UP_VIDEO_HOST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <linux/videodev2.h> Review Comment: remove, linux specific header file should only include in implementation file ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, + imgsensor_fmt_to_v4l2(fmt[IMGDATA_FMT_MAIN].pixelformat), + interval->denominator, interval->numerator); +} + +static int sim_video_stop_capture(imgsensor_stream_type_t type) +{ + return 0; +} + +static int sim_video_data_init() +{ + memset(&priv, 0, sizeof(priv)); + priv.vdev = host_video_init(0, HOST_VIDEO_DEV_PATH); + if (priv.vdev == NULL) + { + return -ENODEV; + } + + return 0; +} + +static int sim_video_data_uninit() +{ + return 0; +} + +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval) +{ + return 0; +} + +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback) +{ + int ret; + + priv.capture_cb = callback; + ret = host_video_start_capture(priv.vdev); + if (ret < 0) + { + return ret; + } + + return 0; +} + +static int sim_video_data_stop_capture() +{ + priv.next_buf = (uintptr_t)NULL; + return host_video_stop_capture(priv.vdev); +} + +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size) +{ + if (!addr || (uintptr_t)(addr) & 0x1f) Review Comment: ```suggestion if (!addr || ((uintptr_t)addr) & 0x1f)) ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, Review Comment: ```suggestion fmt[IMGDATA_FMT_MAIN].height, ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; Review Comment: ```suggestion return -ENOTTY; ``` ########## arch/sim/src/sim/sim_hostvideo.h: ########## @@ -0,0 +1,57 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_hostvideo.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_UP_VIDEO_HOST_H +#define __ARCH_SIM_SRC_SIM_UP_VIDEO_HOST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <linux/videodev2.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +typedef void * host_video_dev_t; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +bool host_video_is_available(const char *host_video_dev_path); +host_video_dev_t host_video_init(int dev_id, Review Comment: why need dev_id? ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, + imgsensor_fmt_to_v4l2(fmt[IMGDATA_FMT_MAIN].pixelformat), + interval->denominator, interval->numerator); +} + +static int sim_video_stop_capture(imgsensor_stream_type_t type) +{ + return 0; +} + +static int sim_video_data_init() +{ + memset(&priv, 0, sizeof(priv)); + priv.vdev = host_video_init(0, HOST_VIDEO_DEV_PATH); + if (priv.vdev == NULL) + { + return -ENODEV; + } + + return 0; +} + +static int sim_video_data_uninit() +{ + return 0; +} + +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval) +{ + return 0; +} + +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback) +{ + int ret; + + priv.capture_cb = callback; + ret = host_video_start_capture(priv.vdev); + if (ret < 0) + { + return ret; + } + + return 0; +} + +static int sim_video_data_stop_capture() +{ + priv.next_buf = (uintptr_t)NULL; + return host_video_stop_capture(priv.vdev); +} + +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size) +{ + if (!addr || (uintptr_t)(addr) & 0x1f) + { + return -EINVAL; + } + + return 0; +} + +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size) +{ + priv.next_buf = (uintptr_t)addr; Review Comment: why not change next_buf to uint8_t * to avoid the cast ########## arch/sim/src/sim/posix/sim_host_v4l2.c: ########## @@ -0,0 +1,358 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_host_v4l2.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <linux/videodev2.h> +#include <sys/mman.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAX_REQBUFS 3 +#define MAX_VIDEO_DEVS 1 + +#define WARN(fmt, ...) \ + syslog(LOG_WARNING, "sim_host_video: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + int fd; + struct v4l2_requestbuffers reqbuf; + void *addrs[MAX_REQBUFS]; + size_t buflen[MAX_REQBUFS]; +} _host_video_dev_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static _host_video_dev_t vdevs[MAX_VIDEO_DEVS]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_video_ioctl(int fd, int request, void *arg) +{ + int r; + + do + { + r = ioctl(fd, request, arg); + } + while (-1 == r && EINTR == errno); + + return r; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +bool host_video_is_available(const char *host_video_dev_path) +{ + if (access(host_video_dev_path, F_OK) == 0) Review Comment: return access(host_video_dev_path, F_OK) >= 0 ########## arch/sim/src/sim/posix/sim_host_v4l2.c: ########## @@ -0,0 +1,358 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_host_v4l2.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <linux/videodev2.h> +#include <sys/mman.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAX_REQBUFS 3 Review Comment: why not use Kconfig value ########## arch/sim/src/sim/posix/sim_host_v4l2.c: ########## @@ -0,0 +1,358 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_host_v4l2.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <linux/videodev2.h> +#include <sys/mman.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAX_REQBUFS 3 +#define MAX_VIDEO_DEVS 1 + +#define WARN(fmt, ...) \ + syslog(LOG_WARNING, "sim_host_video: " fmt "\n", ##__VA_ARGS__) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + int fd; + struct v4l2_requestbuffers reqbuf; + void *addrs[MAX_REQBUFS]; + size_t buflen[MAX_REQBUFS]; +} _host_video_dev_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static _host_video_dev_t vdevs[MAX_VIDEO_DEVS]; Review Comment: remove global and MAX_VIDEO_DEVS, let' malloc and free ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); + if (type == IMGSENSOR_STREAM_TYPE_VIDEO) + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, interval->denominator, + interval->numerator); + } + else + { + return host_video_try_fmt(priv.vdev, fmt->width, fmt->height, + v4l2_fmt, 0, 0); + } +} + +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return validate_format(type, nr_fmt, fmt, interval); +} + +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + return host_video_set_fmt(priv.vdev, fmt[IMGDATA_FMT_MAIN].width, + fmt[IMGDATA_FMT_MAIN].height, + imgsensor_fmt_to_v4l2(fmt[IMGDATA_FMT_MAIN].pixelformat), + interval->denominator, interval->numerator); +} + +static int sim_video_stop_capture(imgsensor_stream_type_t type) +{ + return 0; +} + +static int sim_video_data_init() +{ + memset(&priv, 0, sizeof(priv)); + priv.vdev = host_video_init(0, HOST_VIDEO_DEV_PATH); + if (priv.vdev == NULL) + { + return -ENODEV; + } + + return 0; +} + +static int sim_video_data_uninit() +{ + return 0; +} + +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval) +{ + return 0; +} + +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback) +{ + int ret; + + priv.capture_cb = callback; + ret = host_video_start_capture(priv.vdev); + if (ret < 0) + { + return ret; + } + + return 0; +} + +static int sim_video_data_stop_capture() +{ + priv.next_buf = (uintptr_t)NULL; Review Comment: ```suggestion priv.next_buf = NULL; ``` ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, + imgsensor_format_t *fmt, + imgsensor_interval_t *interval) +{ + if (nr_fmt > 1) + { + return -ENOTSUP; + } + + uint32_t v4l2_fmt = imgsensor_fmt_to_v4l2(fmt->pixelformat); Review Comment: move the definition to the first ########## arch/sim/src/sim/sim_video.c: ########## @@ -0,0 +1,359 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_video.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <arch/board/board.h> +#include <nuttx/config.h> +#include <nuttx/semaphore.h> +#include <nuttx/signal.h> +#include <nuttx/video/imgsensor.h> +#include <nuttx/video/imgdata.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/ioctl.h> + +#include "sim_hostvideo.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define HOST_VIDEO_DEV_PATH CONFIG_HOST_VIDEO_DEV_PATH +#define ENQUEUE(q, addr, size) (q).addr[q.num] = addr; \ + (q).size[q.num++] = size; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +typedef struct +{ + imgdata_capture_t capture_cb; + uint32_t buf_size; + uintptr_t next_buf; + host_video_dev_t vdev; +} video_priv_t; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* video image sensor operations */ + +static bool sim_video_is_available(void); +static int sim_video_init(void); +static int sim_video_uninit(void); +static const char *sim_video_get_driver_name(void); +static int sim_video_validate_frame_setting(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_start_capture(imgsensor_stream_type_t type, + uint8_t nr_datafmt, + imgsensor_format_t *datafmts, + imgsensor_interval_t *interval); +static int sim_video_stop_capture(imgsensor_stream_type_t type); +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value); +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value); +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value); + +/* video image data operations */ + +static int sim_video_data_init(void); +static int sim_video_data_uninit(void); +static int sim_video_data_validate_frame_setting(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval); +static int sim_video_data_start_capture(uint8_t nr_datafmt, + imgdata_format_t *datafmt, + imgdata_interval_t *interval, + imgdata_capture_t callback); +static int sim_video_data_stop_capture(void); +static int sim_video_data_validate_buf(uint8_t *addr, uint32_t size); +static int sim_video_data_set_buf(uint8_t *addr, uint32_t size); + +static uint32_t imgsensor_fmt_to_v4l2(uint32_t pixelformat); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct imgsensor_ops_s g_sim_video_ops = +{ + .is_available = sim_video_is_available, + .init = sim_video_init, + .uninit = sim_video_uninit, + .get_driver_name = sim_video_get_driver_name, + .validate_frame_setting = sim_video_validate_frame_setting, + .start_capture = sim_video_start_capture, + .stop_capture = sim_video_stop_capture, + .get_supported_value = sim_video_get_supported_value, + .get_value = sim_video_get_value, + .set_value = sim_video_set_value, +}; + +static const struct imgdata_ops_s g_sim_video_data_ops = + { + .init = sim_video_data_init, + .uninit = sim_video_data_uninit, + .validate_buf = sim_video_data_validate_buf, + .set_buf = sim_video_data_set_buf, + .validate_frame_setting = sim_video_data_validate_frame_setting, + .start_capture = sim_video_data_start_capture, + .stop_capture = sim_video_data_stop_capture, + }; + +static video_priv_t priv; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool sim_video_is_available(void) +{ + return host_video_is_available(HOST_VIDEO_DEV_PATH); +} + +static int sim_video_init(void) +{ + return 0; +} + +static int sim_video_uninit(void) +{ + return host_video_uninit(priv.vdev); +} + +static const char *sim_video_get_driver_name(void) +{ + return "V4L2 NuttX Sim Driver"; +} + +static int sim_video_get_supported_value(uint32_t id, + imgsensor_supported_value_t *value) +{ + return 0; +} + +static int sim_video_get_value(uint32_t id, uint32_t size, + imgsensor_value_t *value) +{ + return 0; +} + +static int sim_video_set_value(uint32_t id, uint32_t size, + imgsensor_value_t value) +{ + return 0; +} + +static int validate_format(imgsensor_stream_type_t type, int nr_fmt, Review Comment: merge to sim_video_validate_frame_setting -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org