1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HOS_CAMERA_SENSOR_CONTROLLER_H
17 #define HOS_CAMERA_SENSOR_CONTROLLER_H
18 
19 #include "icontroller.h"
20 #include "device_manager_adapter.h"
21 #include "v4l2_dev.h"
22 #include "v4l2_common.h"
23 
24 namespace OHOS::Camera {
25 class SensorController;
26 using TagFunType = std::function<void(SensorController*, std::shared_ptr<CameraMetadata>, const int32_t&)>;
27 class SensorController : public IController {
28 public:
29     SensorController();
30     explicit SensorController(std::string hardwareName);
31     virtual ~SensorController();
32     RetCode Init();
33     RetCode PowerUp();
34     RetCode PowerDown();
35     RetCode Configure(std::shared_ptr<CameraMetadata> meta);
36     RetCode ConfigFps(std::shared_ptr<CameraMetadata> meta);
37     RetCode ConfigStart();
38     RetCode ConfigStop();
39     RetCode Start(int buffCont, DeviceFormat& format);
40     RetCode Stop();
41 
42     RetCode SendFrameBuffer(std::shared_ptr<FrameSpec> buffer);
43 
44     void SetNodeCallBack(const NodeBufferCb cb);
45     void BufferCallback(std::shared_ptr<FrameSpec> buffer);
46 
47     void SetAbilityMetaDataTag(std::vector<int32_t> abilityMetaDataTag);
48     RetCode GetAbilityMetaData(std::shared_ptr<CameraMetadata> meta);
49     RetCode Flush(int32_t streamId);
50     void SetMemoryType(uint8_t &memType);
51 
52 private:
53     RetCode SendSensorMetaData(std::shared_ptr<CameraMetadata> meta);
54     RetCode SendAEMetaData(common_metadata_header_t *data);
55     RetCode SendAELockMetaData(common_metadata_header_t *data);
56     RetCode SendAWBMetaData(common_metadata_header_t *data);
57     RetCode SendAWBLockMetaData(common_metadata_header_t *data);
58     RetCode SendExposureMetaData(common_metadata_header_t *data);
59     void CheckRetCodeValue(RetCode rc);
60     void CheckUpdateSettingRetCode(RetCode rc, int exposureVal);
61     RetCode SendExposureModeMetaData(common_metadata_header_t *data);
62     RetCode SendExposureAutoModeMetaData(common_metadata_header_t *data);
63     RetCode SendFocusMetaData(common_metadata_header_t *data);
64     RetCode SendFocusRegionsMetaData(common_metadata_header_t *data);
65     RetCode SendMeterMetaData(common_metadata_header_t *data);
66     RetCode SendFlashMetaData(common_metadata_header_t *data);
67     RetCode SendFpsMetaData(common_metadata_header_t *data);
68     RetCode SetFocusRegions(common_metadata_header_t *data);
69     RetCode GetSensorMetaData(std::shared_ptr<CameraMetadata> meta);
70     RetCode GetAEMetaData(std::shared_ptr<CameraMetadata> meta);
71     RetCode GetAWBMetaData(std::shared_ptr<CameraMetadata> meta);
72     static void GetFocusMode(SensorController *sensorController,
73         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
74     static void GetFocusState(SensorController *sensorController,
75         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
76     static void GetExposureMode(SensorController *sensorController,
77         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
78     static void GetExposureTime(SensorController *sensorController,
79         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
80     static void GetExposureCompensation(SensorController *sensorController,
81         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
82     static void GetMeterMode(SensorController *sensorController,
83         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
84     static void GetExposureState(SensorController *sensorController,
85         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
86     static void GetFlashMode(SensorController *sensorController,
87         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
88     static void GetCaptureMirror(SensorController *sensorController,
89         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
90     static void GetBasicConfigurations(SensorController *sensorController,
91         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
92     void GetFpsRange(SensorController *sensorController,
93         std::shared_ptr<CameraMetadata> meta);
94     static void GetJpegOrientation(SensorController *sensorController,
95         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
96     static void GetJpegQuality(SensorController *sensorController,
97         std::shared_ptr<CameraMetadata> meta, const int32_t &value);
98 
99     template<typename T>
CheckNumequal(T oldnum,T num,int size)100     bool CheckNumequal(T oldnum, T num, int size)
101     {
102         if (oldnum == nullptr || num == nullptr) {
103             CAMERA_LOGE("oldnum or num is nullptr");
104             return false;
105         }
106         for (int i = 0; size > 0; i++, size--) {
107             if (oldnum[i] != num[i]) {
108                 return false;
109             }
110         }
111         return true;
112     };
113     std::mutex startSensorLock_;
114     bool startSensorState_ = false;
115     NodeBufferCb nodeBufferCb_ = nullptr;
116     std::vector<int32_t> abilityMetaData_;
117     std::mutex metaDataSetlock_;
118     std::mutex metaDataFlaglock_;
119     bool metaDataFlag_ = false;
120     int buffCont_;
121     std::shared_ptr<HosV4L2Dev> sensorVideo_;
122     static std::map<int32_t, uint32_t> tagV4L2CidMap_;
123     static std::map<int32_t, TagFunType> tagMethodMap_;
124     std::vector<int32_t> fpsRange_;
125     bool configState_ = false;
126     bool is3aAeLock_ = false;
127 };
128 } // namespace OHOS::Camera
129 #endif
130