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 #include "sensor_controller.h"
17 #include <cinttypes>
18 #include "securec.h"
19
20 namespace OHOS::Camera {
21 std::map<int32_t, uint32_t> SensorController::tagV4L2CidMap_ = {
22 {OHOS_SENSOR_EXPOSURE_TIME, V4L2_CID_EXPOSURE_AUTO},
23 {OHOS_CONTROL_AWB_MODE, V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE},
24 {OHOS_CONTROL_FOCUS_MODE, V4L2_CID_FOCUS_AUTO},
25 {OHOS_CONTROL_EXPOSURE_MODE, V4L2_CID_EXPOSURE_AUTO},
26 {OHOS_CONTROL_FLASH_MODE, V4L2_CID_FLASH_LED_MODE},
27 };
28 std::map<int32_t, TagFunType> SensorController::tagMethodMap_ = {
29 {OHOS_SENSOR_EXPOSURE_TIME, SensorController::GetExposureTime},
30 {OHOS_CONTROL_FOCUS_MODE, SensorController::GetFocusMode},
31 {OHOS_CONTROL_EXPOSURE_MODE, SensorController::GetExposureMode},
32 {OHOS_CONTROL_FLASH_MODE, SensorController::GetFlashMode},
33 };
SensorController()34 SensorController::SensorController() {}
35
SensorController(std::string hardwareName)36 SensorController::SensorController(std::string hardwareName) : IController(hardwareName), buffCont_(0) {}
37
~SensorController()38 SensorController::~SensorController() {}
39
Init()40 RetCode SensorController::Init()
41 {
42 sensorVideo_ = std::make_shared<HosV4L2Dev>();
43 if (sensorVideo_ == nullptr) {
44 CAMERA_LOGE("%s Create HosV4L2Dev fail", __FUNCTION__);
45 return RC_ERROR;
46 }
47
48 // push default value
49 constexpr uint32_t FPS_FIVE = 5;
50 constexpr uint32_t FPS_TEN = 10;
51 fpsRange_.push_back(FPS_FIVE);
52 fpsRange_.push_back(FPS_TEN);
53 return RC_OK;
54 }
55
PowerUp()56 RetCode SensorController::PowerUp()
57 {
58 RetCode rc = RC_OK;
59 if (GetPowerOnState() == false) {
60 SetPowerOnState(true);
61 CAMERA_LOGI("%s Sensor Powerup", __FUNCTION__);
62 return rc;
63 }
64 return rc;
65 }
66
PowerDown()67 RetCode SensorController::PowerDown()
68 {
69 RetCode rc = RC_OK;
70 if (GetPowerOnState() == true) {
71 SetPowerOnState(false);
72 sensorVideo_->StopStream(GetName());
73 sensorVideo_->ReleaseBuffers(GetName());
74 sensorVideo_->stop(GetName());
75 CAMERA_LOGI("%s Sensor PowerDown", __FUNCTION__);
76 return rc;
77 }
78 return rc;
79 }
80
Configure(std::shared_ptr<CameraMetadata> meta)81 RetCode SensorController::Configure(std::shared_ptr<CameraMetadata> meta)
82 {
83 return SendSensorMetaData(meta);
84 };
85
ConfigFps(std::shared_ptr<CameraMetadata> meta)86 RetCode SensorController::ConfigFps(std::shared_ptr<CameraMetadata> meta)
87 {
88 if (meta == nullptr) {
89 CAMERA_LOGE("Meta is nullptr");
90 return RC_ERROR;
91 }
92 common_metadata_header_t *data = meta->get();
93 if (data == nullptr) {
94 CAMERA_LOGE("Data is nullptr");
95 return RC_ERROR;
96 }
97 RetCode rc = SendFpsMetaData(data);
98 if (rc == RC_ERROR) {
99 CAMERA_LOGE("SendFpsMetaData fail");
100 }
101 return rc;
102 }
103
ConfigStart()104 RetCode SensorController::ConfigStart()
105 {
106 CAMERA_LOGI("%s ConfigStart", __FUNCTION__);
107 std::lock_guard<std::mutex> lock(startSensorLock_);
108 RetCode rc = RC_OK;
109 if (startSensorState_ == false) {
110 configState_ = true;
111 rc = sensorVideo_->start(GetName());
112 if (rc == RC_ERROR) {
113 CAMERA_LOGE("ConfigStart fail");
114 return rc;
115 }
116 }
117 return rc;
118 }
119
ConfigStop()120 RetCode SensorController::ConfigStop()
121 {
122 CAMERA_LOGI("%s ConfigStop", __FUNCTION__);
123 std::lock_guard<std::mutex> lock(startSensorLock_);
124 RetCode rc = RC_OK;
125 if (startSensorState_ == false && configState_ == true) {
126 rc = sensorVideo_->stop(GetName());
127 configState_ = false;
128 }
129 return rc;
130 }
131
Start(int buffCont,DeviceFormat & format)132 RetCode SensorController::Start(int buffCont, DeviceFormat& format)
133 {
134 CAMERA_LOGI("%s Start", __FUNCTION__);
135 std::lock_guard<std::mutex> lock(startSensorLock_);
136 RetCode rc = RC_OK;
137 if (startSensorState_ == false) {
138 buffCont_ = buffCont;
139 startSensorState_ = true;
140 sensorVideo_->start(GetName());
141 sensorVideo_->ConfigSys(GetName(), CMD_V4L2_SET_FORMAT, format);
142 sensorVideo_->ReqBuffers(GetName(), buffCont_);
143 }
144 return rc;
145 };
146
Stop()147 RetCode SensorController::Stop()
148 {
149 CAMERA_LOGI("%s Stop", __FUNCTION__);
150 std::lock_guard<std::mutex> lock(startSensorLock_);
151 RetCode rc = RC_OK;
152 if (startSensorState_ == true) {
153 sensorVideo_->StopStream(GetName());
154 sensorVideo_->ReleaseBuffers(GetName());
155 sensorVideo_->stop(GetName());
156 startSensorState_ = false;
157 configState_ = false;
158 }
159 return rc;
160 };
161
SendFrameBuffer(std::shared_ptr<FrameSpec> buffer)162 RetCode SensorController::SendFrameBuffer(std::shared_ptr<FrameSpec> buffer)
163 {
164 RetCode ret = RC_OK;
165 if (buffCont_ >= 1) {
166 CAMERA_LOGI("buffCont_ %{public}d", buffCont_);
167 sensorVideo_->CreatBuffer(GetName(), buffer);
168 if (buffCont_ == 1) {
169 ret = sensorVideo_->StartStream(GetName());
170 }
171 buffCont_--;
172 } else {
173 ret = sensorVideo_->QueueBuffer(GetName(), buffer);
174 }
175 return ret;
176 }
177
SetNodeCallBack(const NodeBufferCb cb)178 void SensorController::SetNodeCallBack(const NodeBufferCb cb)
179 {
180 CAMERA_LOGI("SensorController SetNodeCallBack entry");
181 nodeBufferCb_ = cb;
182 sensorVideo_->SetV4L2DevCallback([&](std::shared_ptr<FrameSpec> buffer) {
183 BufferCallback(buffer);
184 });
185 }
186
BufferCallback(std::shared_ptr<FrameSpec> buffer)187 void SensorController::BufferCallback(std::shared_ptr<FrameSpec> buffer)
188 {
189 if (nodeBufferCb_ == nullptr) {
190 CAMERA_LOGE("nodeBufferCb_ is nullptr");
191 return;
192 }
193
194 constexpr uint32_t UNIT_COUNT = 1000;
195 struct timeval tv;
196 gettimeofday(&tv, NULL);
197 int64_t timestamp =
198 static_cast<uint64_t>(tv.tv_sec) * UNIT_COUNT * UNIT_COUNT * UNIT_COUNT + tv.tv_usec * UNIT_COUNT;
199 buffer->buffer_->SetEsTimestamp(timestamp);
200 nodeBufferCb_(buffer);
201 }
202
GetAbilityMetaData(std::shared_ptr<CameraMetadata> meta)203 RetCode SensorController::GetAbilityMetaData(std::shared_ptr<CameraMetadata> meta)
204 {
205 return GetSensorMetaData(meta);
206 }
207
SetAbilityMetaDataTag(std::vector<int32_t> abilityMetaDataTag)208 void SensorController::SetAbilityMetaDataTag(std::vector<int32_t> abilityMetaDataTag)
209 {
210 std::lock_guard<std::mutex> lock(metaDataSetlock_);
211 std::vector<int32_t>().swap(abilityMetaData_);
212 for (auto it = abilityMetaDataTag.cbegin(); it != abilityMetaDataTag.cend(); it++) {
213 switch (*it) {
214 case OHOS_SENSOR_COLOR_CORRECTION_GAINS: {
215 abilityMetaData_.push_back((*it));
216 break;
217 }
218 case OHOS_SENSOR_EXPOSURE_TIME: {
219 abilityMetaData_.push_back(*it);
220 break;
221 }
222 case OHOS_CONTROL_EXPOSURE_MODE: {
223 abilityMetaData_.push_back((*it));
224 break;
225 }
226 case OHOS_CONTROL_AE_EXPOSURE_COMPENSATION: {
227 abilityMetaData_.push_back((*it));
228 break;
229 }
230 case OHOS_CONTROL_FOCUS_MODE: {
231 abilityMetaData_.push_back((*it));
232 break;
233 }
234 case OHOS_CONTROL_METER_MODE: {
235 abilityMetaData_.push_back((*it));
236 break;
237 }
238 case OHOS_CONTROL_FLASH_MODE: {
239 abilityMetaData_.push_back((*it));
240 break;
241 }
242 case OHOS_CONTROL_FPS_RANGES: {
243 abilityMetaData_.push_back((*it));
244 break;
245 }
246 default:
247 break;
248 }
249 }
250 }
251
GetSensorMetaData(std::shared_ptr<CameraMetadata> meta)252 RetCode SensorController::GetSensorMetaData(std::shared_ptr<CameraMetadata> meta)
253 {
254 RetCode rc = RC_OK;
255 int32_t outValue = 0;
256
257 std::lock_guard<std::mutex> lock(metaDataSetlock_);
258 for (auto &keyTag : abilityMetaData_) {
259 auto hasCmdMem = tagV4L2CidMap_.find(keyTag);
260 if (hasCmdMem == tagV4L2CidMap_.end()) {
261 continue;
262 }
263
264 rc = sensorVideo_->QuerySetting(GetName(), tagV4L2CidMap_[keyTag], &outValue);
265 if (rc == RC_ERROR) {
266 continue;
267 }
268
269 auto hasFuncMem = tagMethodMap_.find(keyTag);
270 if (hasFuncMem == tagMethodMap_.end()) {
271 continue;
272 }
273 tagMethodMap_[keyTag](this, meta, outValue);
274 }
275
276 // dummy value start
277 outValue = 1;
278 GetExposureCompensation(this, meta, outValue);
279 outValue = 0;
280 GetFocusMode(this, meta, outValue);
281 outValue = 1;
282 GetMeterMode(this, meta, outValue);
283 GetFpsRange(this, meta);
284 outValue = 1;
285 GetFlashMode(this, meta, outValue);
286 rc = RC_OK;
287 // dummy value end
288
289 return rc;
290 }
291
GetAEMetaData(std::shared_ptr<CameraMetadata> meta)292 RetCode SensorController::GetAEMetaData(std::shared_ptr<CameraMetadata> meta)
293 {
294 static int64_t oldExpoTime = 0;
295 int64_t expoTime = 0;
296 RetCode rc = RC_ERROR;
297 std::lock_guard<std::mutex> metaDataLock(metaDataSetlock_);
298 for (auto iter = abilityMetaData_.cbegin(); iter != abilityMetaData_.cend(); iter++) {
299 if (*iter == OHOS_SENSOR_EXPOSURE_TIME) {
300 rc = sensorVideo_->QuerySetting(GetName(), CMD_AE_EXPO, (int*)&expoTime);
301 CAMERA_LOGD("%s Get CMD_AE_EXPOTIME [%" PRId64 "]", __FUNCTION__, expoTime);
302 if (rc == RC_ERROR) {
303 CAMERA_LOGE("%s CMD_AE_EXPO QuerySetting fail", __FUNCTION__);
304 return rc;
305 }
306 if (oldExpoTime != expoTime) {
307 std::lock_guard<std::mutex> flagLock(metaDataFlaglock_);
308 metaDataFlag_ = true;
309 oldExpoTime = expoTime;
310 }
311 meta->addEntry(OHOS_SENSOR_EXPOSURE_TIME, &expoTime, 1);
312 }
313 }
314 return rc;
315 }
316
GetAWBMetaData(std::shared_ptr<CameraMetadata> meta)317 RetCode SensorController::GetAWBMetaData(std::shared_ptr<CameraMetadata> meta)
318 {
319 static float oldColorGains[4] = {0};
320 float colorGains[4] = {0};
321 int value = 0;
322 RetCode rc = RC_ERROR;
323 std::lock_guard<std::mutex> metaDataLock(metaDataSetlock_);
324 for (auto iter = abilityMetaData_.cbegin(); iter != abilityMetaData_.cend(); iter++) {
325 if (*iter == OHOS_SENSOR_COLOR_CORRECTION_GAINS) {
326 rc = sensorVideo_->QuerySetting(GetName(), CMD_AWB_COLORGAINS, &value);
327 if (rc == RC_ERROR) {
328 CAMERA_LOGE("%s CMD_AWB_COLORGAINS QuerySetting fail", __FUNCTION__);
329 return rc;
330 }
331 colorGains[0] = (float)value;
332 int gainsSize = 4;
333 if (!CheckNumequal(oldColorGains, colorGains, gainsSize)) {
334 std::lock_guard<std::mutex> flagLock(metaDataFlaglock_);
335 metaDataFlag_ = true;
336 (void)memcpy_s(oldColorGains, sizeof(oldColorGains) / sizeof(float), colorGains,
337 gainsSize * sizeof(float));
338 }
339 meta->addEntry(OHOS_SENSOR_COLOR_CORRECTION_GAINS, &colorGains, 4); // 4:data size
340 }
341 }
342 return rc;
343 }
344
GetFocusMode(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)345 void SensorController::GetFocusMode(SensorController *sensorController,
346 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
347 {
348 if (meta == nullptr) {
349 CAMERA_LOGE("meta is nullptr");
350 return;
351 }
352 uint8_t focusMode = value;
353
354 CAMERA_LOGI("Get CMD_FOCUS_MODE [%{public}]", focusMode);
355 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
356 sensorController->metaDataFlag_ = true;
357 meta->addEntry(OHOS_CONTROL_FOCUS_MODE, &focusMode, 1);
358 }
359
GetFocusState(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)360 void SensorController::GetFocusState(SensorController *sensorController,
361 std::shared_ptr<CameraMetadata> meta,
362 const int32_t &value)
363 {
364 if (meta == nullptr) {
365 CAMERA_LOGE("meta is nullptr");
366 return;
367 }
368 uint8_t focusState = value;
369
370 CAMERA_LOGI("Get CMD_FOCUS_SATE [%{public}]", focusState);
371 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
372 sensorController->metaDataFlag_ = true;
373 meta->addEntry(OHOS_CONTROL_FOCUS_STATE, &focusState, 1);
374 }
375
GetExposureMode(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)376 void SensorController::GetExposureMode(SensorController *sensorController,
377 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
378 {
379 if (meta == nullptr) {
380 CAMERA_LOGE("meta is nullptr");
381 return;
382 }
383 uint8_t exposureMode = value;
384
385 CAMERA_LOGI("Get CMD_FEXPOSURE_MODE [%{public}]", exposureMode);
386 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
387 sensorController->metaDataFlag_ = true;
388 meta->addEntry(OHOS_CONTROL_EXPOSURE_MODE, &exposureMode, 1);
389 }
390
GetExposureTime(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)391 void SensorController::GetExposureTime(SensorController *sensorController,
392 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
393 {
394 if (meta == nullptr) {
395 CAMERA_LOGE("meta is nullptr");
396 return;
397 }
398 int64_t exposureTime = value;
399
400 CAMERA_LOGI("Get CMD_FEXPOSURE_TIME [%{public}]", exposureTime);
401 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
402 sensorController->metaDataFlag_ = true;
403 meta->addEntry(OHOS_SENSOR_EXPOSURE_TIME, &exposureTime, 1);
404 }
405
GetExposureCompensation(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)406 void SensorController::GetExposureCompensation(SensorController *sensorController,
407 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
408 {
409 if (meta == nullptr) {
410 CAMERA_LOGE("meta is nullptr");
411 return;
412 }
413 int32_t exposureCompensation = value;
414 constexpr uint32_t DATA_COUNT = 1;
415 CAMERA_LOGI("Get CMD_FEXPOSURE_COMPENSATION [%{public}]", exposureCompensation);
416 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
417 sensorController->metaDataFlag_ = true;
418 meta->addEntry(OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &exposureCompensation, DATA_COUNT);
419
420 // dummy data
421 uint8_t videoStabiliMode = OHOS_CAMERA_VIDEO_STABILIZATION_OFF;
422 meta->addEntry(OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &videoStabiliMode, DATA_COUNT);
423 }
424
GetMeterMode(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)425 void SensorController::GetMeterMode(SensorController *sensorController,
426 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
427 {
428 if (meta == nullptr) {
429 CAMERA_LOGE("meta is nullptr");
430 return;
431 }
432 uint8_t meterMode = value;
433
434 CAMERA_LOGI("Get CMD_METER_MODE [%{public}]", meterMode);
435 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
436 sensorController->metaDataFlag_ = true;
437 meta->addEntry(OHOS_CONTROL_METER_MODE, &meterMode, 1);
438 }
439
GetExposureState(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)440 void SensorController::GetExposureState(SensorController *sensorController,
441 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
442 {
443 if (meta == nullptr) {
444 CAMERA_LOGE("meta is nullptr");
445 return;
446 }
447 uint8_t exposureState = value;
448
449 CAMERA_LOGI("Get CMD_EXPOSURE_STATE [%{public}]", exposureState);
450 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
451 sensorController->metaDataFlag_ = true;
452 meta->addEntry(OHOS_CONTROL_EXPOSURE_STATE, &exposureState, 1);
453 }
454
GetFlashMode(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)455 void SensorController::GetFlashMode(SensorController *sensorController,
456 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
457 {
458 if (meta == nullptr) {
459 CAMERA_LOGE("meta is nullptr");
460 return;
461 }
462 uint8_t flashMode = value;
463
464 CAMERA_LOGI("Get CMD_FLASH_MODE [%{public}]", flashMode);
465 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
466 sensorController->metaDataFlag_ = true;
467 meta->addEntry(OHOS_CONTROL_FLASH_MODE, &flashMode, 1);
468 }
469
GetCaptureMirror(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta,const int32_t & value)470 void SensorController::GetCaptureMirror(SensorController *sensorController,
471 std::shared_ptr<CameraMetadata> meta, const int32_t &value)
472 {
473 if (meta == nullptr) {
474 CAMERA_LOGE("meta is nullptr");
475 return;
476 }
477 uint8_t captureMirror = value;
478
479 CAMERA_LOGI("Get CMD_CAPTURE_MIRROR [%{public}]", captureMirror);
480 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
481 sensorController->metaDataFlag_ = true;
482 meta->addEntry(OHOS_CONTROL_CAPTURE_MIRROR, &captureMirror, 1);
483 }
484
GetFpsRange(SensorController * sensorController,std::shared_ptr<CameraMetadata> meta)485 void SensorController::GetFpsRange(SensorController *sensorController,
486 std::shared_ptr<CameraMetadata> meta)
487 {
488 if (meta == nullptr) {
489 CAMERA_LOGE("meta is nullptr");
490 return;
491 }
492
493 for (auto iter = abilityMetaData_.cbegin(); iter != abilityMetaData_.cend(); iter++) {
494 if (*iter == OHOS_CONTROL_FPS_RANGES) {
495 DeviceFormat format;
496 std::lock_guard<std::mutex> lock(sensorController->metaDataFlaglock_);
497 sensorController->metaDataFlag_ = true;
498 RetCode rc = sensorVideo_->ConfigSys(GetName(), CMD_V4L2_GET_FPS, format);
499 if (rc == RC_ERROR) {
500 CAMERA_LOGE("CMD_V4L2_GET_FPS ConfigSys fail");
501 }
502
503 // dummy data
504 meta->addEntry(OHOS_CONTROL_FPS_RANGES, fpsRange_.data(), fpsRange_.size());
505 }
506 }
507 }
508
SendSensorMetaData(std::shared_ptr<CameraMetadata> meta)509 RetCode SensorController::SendSensorMetaData(std::shared_ptr<CameraMetadata> meta)
510 {
511 if (meta == nullptr) {
512 CAMERA_LOGE("meta is nullptr");
513 return RC_ERROR;
514 }
515 common_metadata_header_t *data = meta->get();
516 if (data == nullptr) {
517 CAMERA_LOGE("data is nullptr");
518 return RC_ERROR;
519 }
520 RetCode rc = SendAWBMetaData(data);
521 if (rc == RC_ERROR) {
522 CAMERA_LOGE("SendAWBMetaData fail");
523 }
524 rc = SendAWBLockMetaData(data);
525 if (rc == RC_ERROR) {
526 CAMERA_LOGE("SendAWBLockMetaData fail");
527 }
528 rc = SendExposureMetaData(data);
529 if (rc == RC_ERROR) {
530 CAMERA_LOGE("SendExposureMetaData fail");
531 }
532 rc = SendAELockMetaData(data);
533 if (rc == RC_ERROR) {
534 CAMERA_LOGE("SendAELockMetaData fail");
535 }
536 rc = SendFocusMetaData(data);
537 if (rc == RC_ERROR) {
538 CAMERA_LOGE("SendFocusMetaData fail");
539 }
540 rc = SendFocusRegionsMetaData(data);
541 if (rc == RC_ERROR) {
542 CAMERA_LOGE("SendFocusRegionsMetaData fail");
543 }
544 rc = SendMeterMetaData(data);
545 if (rc == RC_ERROR) {
546 CAMERA_LOGE("SendMeterMetaData fail");
547 }
548 rc = SendFlashMetaData(data);
549 if (rc == RC_ERROR) {
550 CAMERA_LOGE("SendFlashMetaData fail");
551 }
552 return rc;
553 }
554
SendAEMetaData(common_metadata_header_t * data)555 RetCode SensorController::SendAEMetaData(common_metadata_header_t *data)
556 {
557 RetCode rc = RC_OK;
558 camera_metadata_item_t entry;
559 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &entry);
560 if (ret == 0) {
561 int32_t expo = *(entry.data.i32);
562 if (expo != 0) {
563 int32_t aemode = 1;
564 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPO, (int*)&aemode);
565 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPOTIME, (int*)&expo);
566 CAMERA_LOGI("%s Set CMD_AE_EXPO EXPOTIME[%d] EXPO[%d]", __FUNCTION__, expo, aemode);
567 } else {
568 int32_t aemode = 0;
569 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPO, (int*)&aemode);
570 CAMERA_LOGI("%s Set CMD_AE_EXPOTIME [%d]", __FUNCTION__, aemode);
571 }
572 if (rc == RC_ERROR) {
573 CAMERA_LOGE("%s Send CMD_AE_EXPOTIME fail", __FUNCTION__);
574 return rc;
575 }
576 }
577 return rc;
578 }
579
SendAWBMetaData(common_metadata_header_t * data)580 RetCode SensorController::SendAWBMetaData(common_metadata_header_t *data)
581 {
582 uint8_t awbMode = 0;
583 RetCode rc = RC_OK;
584 camera_metadata_item_t entry;
585 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_AWB_MODE, &entry);
586 if (ret == 0) {
587 awbMode = *(entry.data.u8);
588 int awbModeVal = V4L2_WHITE_BALANCE_AUTO;
589 // Can not find tag for OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT and OHOS_CAMERA_AWB_MODE_TWILIGHT
590 // in V4L2, so set V4L2_WHITE_BALANCE_AUTO.
591 if (awbMode == OHOS_CAMERA_AWB_MODE_AUTO ||
592 awbMode == OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT ||
593 awbMode == OHOS_CAMERA_AWB_MODE_TWILIGHT) {
594 awbModeVal = V4L2_WHITE_BALANCE_AUTO;
595 }
596
597 if (awbMode == OHOS_CAMERA_AWB_MODE_OFF) {
598 awbModeVal = V4L2_WHITE_BALANCE_MANUAL;
599 }
600
601 if (awbMode == OHOS_CAMERA_AWB_MODE_INCANDESCENT) {
602 awbModeVal = V4L2_WHITE_BALANCE_INCANDESCENT;
603 }
604
605 if (awbMode == OHOS_CAMERA_AWB_MODE_FLUORESCENT) {
606 awbModeVal = V4L2_WHITE_BALANCE_FLUORESCENT;
607 }
608
609 if (awbMode == OHOS_CAMERA_AWB_MODE_DAYLIGHT) {
610 awbModeVal = V4L2_WHITE_BALANCE_DAYLIGHT;
611 }
612
613 if (awbMode == OHOS_CAMERA_AWB_MODE_CLOUDY_DAYLIGHT) {
614 awbModeVal = V4L2_WHITE_BALANCE_CLOUDY;
615 }
616
617 if (awbMode == OHOS_CAMERA_AWB_MODE_SHADE) {
618 awbModeVal = V4L2_WHITE_BALANCE_SHADE;
619 }
620
621 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AWB_MODE, &awbModeVal);
622 CAMERA_LOGI("Set CMD_AWB_MODE [%{public}d]", awbMode);
623 if (rc == RC_OK) {
624 CAMERA_LOGI("Send OHOS_CONTROL_AWB_MODE value=%{public}d success", awbModeVal);
625 } else {
626 CAMERA_LOGE("Send OHOS_CONTROL_AWB_MODE value=%{public}d failed", awbModeVal);
627 }
628 }
629 return rc;
630 }
631
SendAWBLockMetaData(common_metadata_header_t * data)632 RetCode SensorController::SendAWBLockMetaData(common_metadata_header_t *data)
633 {
634 uint8_t awbLock = 0;
635 RetCode rc = RC_OK;
636 camera_metadata_item_t entry;
637 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_AWB_LOCK, &entry);
638 if (ret == 0) {
639 awbLock = *(entry.data.u8);
640 int curLock = 0;
641 sensorVideo_->QuerySetting(GetName(), V4L2_CID_3A_LOCK, &curLock);
642 if (awbLock == OHOS_CAMERA_AWB_LOCK_ON) {
643 // set the position of AWB bit to 1;
644 curLock |= V4L2_LOCK_WHITE_BALANCE;
645 } else if (awbLock == OHOS_CAMERA_AWB_LOCK_OFF) {
646 // set the position of AWB bit to 0;
647 curLock &= ~V4L2_LOCK_WHITE_BALANCE;
648 }
649 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AWB_LOCK, &curLock);
650 CAMERA_LOGI("Set CMD_AWB_LOCK [%{public}d]", awbLock);
651 if (rc == RC_OK) {
652 CAMERA_LOGI("Send OHOS_CONTROL_AWB_LOCK value=%{public}d success", curLock);
653 } else {
654 CAMERA_LOGE("Send OHOS_CONTROL_AWB_LOCK value=%{public}d failed", curLock);
655 }
656 }
657 return rc;
658 }
659
SendExposureMetaData(common_metadata_header_t * data)660 RetCode SensorController::SendExposureMetaData(common_metadata_header_t *data)
661 {
662 RetCode rc = RC_OK;
663 camera_metadata_item_t entry;
664
665 SendExposureModeMetaData(data);
666
667 int64_t exposureTime = 0;
668 int ret = FindCameraMetadataItem(data, OHOS_SENSOR_EXPOSURE_TIME, &entry);
669 if (ret == 0) {
670 exposureTime = *(entry.data.i64);
671 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_EXPOTIME, (int*)&exposureTime);
672 CAMERA_LOGI("Set CMD_AE_EXPOTIME [%{public}d]", exposureTime);
673 if (rc == RC_OK) {
674 CAMERA_LOGI("Send OHOS_SENSOR_EXPOSURE_TIME value=%{public}d success", exposureTime);
675 } else {
676 CAMERA_LOGE("Send OHOS_SENSOR_EXPOSURE_TIME value=%{public}d failed", exposureTime);
677 }
678 }
679
680 int32_t exposureCompensation = 0;
681 ret = FindCameraMetadataItem(data, OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, &entry);
682 if (ret == 0) {
683 exposureCompensation = *(entry.data.i32);
684 rc = sensorVideo_->UpdateSetting(GetName(), CMD_EXPOSURE_COMPENSATION,
685 (int*)&exposureCompensation);
686 CAMERA_LOGI("Set CMD_EXPOSURE_COMPENSATION [%{public}d]", exposureCompensation);
687 if (rc == RC_ERROR) {
688 CAMERA_LOGE("Send CMD_EXPOSURE_COMPENSATION fail");
689 }
690 }
691
692 return rc;
693 }
694
CheckRetCodeValue(RetCode rc)695 void SensorController::CheckRetCodeValue(RetCode rc)
696 {
697 if (rc == RC_ERROR) {
698 CAMERA_LOGE("Send CMD_EXPOSURE_LOCK fail");
699 }
700 }
701
CheckUpdateSettingRetCode(RetCode rc,int exposureVal)702 void SensorController::CheckUpdateSettingRetCode(RetCode rc, int exposureVal)
703 {
704 if (rc == RC_OK) {
705 CAMERA_LOGI("Send OHOS_CONTROL_EXPOSURE_MODE value=%{public}d success", exposureVal);
706 } else {
707 CAMERA_LOGE("Send OHOS_CONTROL_EXPOSURE_MODE value=%{public}d failed", exposureVal);
708 }
709 }
710
SendExposureModeMetaData(common_metadata_header_t * data)711 RetCode SensorController::SendExposureModeMetaData(common_metadata_header_t *data)
712 {
713 RetCode rc = RC_OK;
714 camera_metadata_item_t entry;
715 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_EXPOSURE_MODE, &entry);
716 if (ret == 0) {
717 uint8_t aeLock = 0;
718 bool isAutoMode = false;
719 uint8_t exposureMode = *(entry.data.u8);
720 int exposureVal = V4L2_EXPOSURE_AUTO;
721 if (exposureMode == OHOS_CAMERA_EXPOSURE_MODE_MANUAL) {
722 exposureVal = V4L2_EXPOSURE_MANUAL;
723 } else if (exposureMode == OHOS_CAMERA_EXPOSURE_MODE_CONTINUOUS_AUTO) {
724 exposureVal = V4L2_EXPOSURE_AUTO;
725 isAutoMode = true;
726 } else if (exposureMode == OHOS_CAMERA_EXPOSURE_MODE_AUTO) {
727 exposureVal = V4L2_EXPOSURE_AUTO;
728 isAutoMode = true;
729 }
730 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_AE_LOCK, &entry);
731 if (ret == 0) {
732 aeLock = *(entry.data.u8);
733 }
734 if (aeLock == 0) {
735 int curLock = 0;
736 auto queryResult = sensorVideo_->QuerySetting(GetName(), V4L2_CID_3A_LOCK, &curLock);
737 curLock = exposureMode == OHOS_CAMERA_EXPOSURE_MODE_LOCKED ?
738 curLock | V4L2_LOCK_EXPOSURE : curLock & ~V4L2_LOCK_EXPOSURE;
739 if (queryResult == RC_OK) {
740 rc = sensorVideo_->UpdateSetting(GetName(), CMD_EXPOSURE_LOCK, &curLock);
741 CAMERA_LOGI("Set CMD_EXPOSURE_LOCK [%{public}d]", exposureMode);
742 CheckRetCodeValue(rc);
743 }
744 if (isAutoMode == true) {
745 rc = SendExposureAutoModeMetaData(data);
746 } else {
747 rc = sensorVideo_->UpdateSetting(GetName(), CMD_EXPOSURE_MODE, (int*)&exposureVal);
748 CAMERA_LOGI("Set CMD_EXPOSURE_MODE [%{public}d]", exposureMode);
749 CheckUpdateSettingRetCode(rc, exposureVal);
750 }
751 }
752 }
753 return rc;
754 }
755
SendExposureAutoModeMetaData(common_metadata_header_t * data)756 RetCode SensorController::SendExposureAutoModeMetaData(common_metadata_header_t *data)
757 {
758 RetCode rc = RC_OK;
759 uint8_t exposureMode = 0;
760 camera_metadata_item_t entry;
761 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_EXPOSURE_MODE, &entry);
762 if (ret == 0) {
763 exposureMode = *(entry.data.u8);
764 int exposureVal = V4L2_EXPOSURE_AUTO;
765
766 if (exposureMode == OHOS_CAMERA_EXPOSURE_MODE_CONTINUOUS_AUTO ||
767 exposureMode == OHOS_CAMERA_EXPOSURE_MODE_AUTO) {
768 exposureVal = V4L2_EXPOSURE_AUTO;
769 rc = sensorVideo_->UpdateSetting(GetName(), CMD_EXPOSURE_MODE, (int*)&exposureVal);
770 }
771 if (rc == RC_ERROR) {
772 exposureVal = V4L2_EXPOSURE_SHUTTER_PRIORITY;
773 rc = sensorVideo_->UpdateSetting(GetName(), CMD_EXPOSURE_MODE, (int*)&exposureVal);
774 }
775 if (rc == RC_ERROR) {
776 exposureVal = V4L2_EXPOSURE_APERTURE_PRIORITY;
777 rc = sensorVideo_->UpdateSetting(GetName(), CMD_EXPOSURE_MODE, (int*)&exposureVal);
778 }
779 if (rc == RC_OK) {
780 CAMERA_LOGI("Send OHOS_CONTROL_EXPOSURE_MODE value=%{public}d success", exposureVal);
781 } else {
782 CAMERA_LOGE("Send OHOS_CONTROL_EXPOSURE_MODE value=%{public}d failed", exposureVal);
783 }
784 }
785 return rc;
786 }
787
SendAELockMetaData(common_metadata_header_t * data)788 RetCode SensorController::SendAELockMetaData(common_metadata_header_t *data)
789 {
790 uint8_t aeLock = 0;
791 RetCode rc = RC_OK;
792 camera_metadata_item_t entry;
793 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_AE_LOCK, &entry);
794 if (ret == 0) {
795 aeLock = *(entry.data.u8);
796 int curLock = 0;
797 sensorVideo_->QuerySetting(GetName(), V4L2_CID_3A_LOCK, &curLock);
798 if (aeLock == OHOS_CAMERA_AE_LOCK_ON) {
799 // set the position of AE bit to 1;
800 curLock |= V4L2_LOCK_EXPOSURE;
801 } else if (aeLock == OHOS_CAMERA_AE_LOCK_OFF) {
802 // set the position of AE bit to 0;
803 curLock &= ~V4L2_LOCK_EXPOSURE;
804 }
805
806 rc = sensorVideo_->UpdateSetting(GetName(), CMD_AE_LOCK, &curLock);
807 CAMERA_LOGI("Set CMD_AE_LOCK [%{public}d]", aeLock);
808 if (rc == RC_OK) {
809 CAMERA_LOGI("Send OHOS_CONTROL_AE_LOCK value=%{public}d success", curLock);
810 } else {
811 CAMERA_LOGE("Send OHOS_CONTROL_AE_LOCK value=%{public}d failed", curLock);
812 }
813 }
814 return rc;
815 }
816
SendFocusMetaData(common_metadata_header_t * data)817 RetCode SensorController::SendFocusMetaData(common_metadata_header_t *data)
818 {
819 RetCode rc = RC_OK;
820 uint8_t focusMode = 0;
821 AdapterCmd focusModeCmd = CMD_AUTO_FOCUS_START;
822 camera_metadata_item_t entry;
823 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FOCUS_MODE, &entry);
824 if (ret == 0) {
825 focusMode = *(entry.data.u8);
826 int curLock = 0;
827 auto queryResult = sensorVideo_->QuerySetting(GetName(), V4L2_CID_3A_LOCK, &curLock);
828 if (focusMode == OHOS_CAMERA_FOCUS_MODE_LOCKED) {
829 curLock |= V4L2_LOCK_FOCUS;
830 } else {
831 curLock &= ~V4L2_LOCK_FOCUS;
832 }
833
834 if (queryResult == RC_OK) {
835 rc = sensorVideo_->UpdateSetting(GetName(), CMD_FOCUS_LOCK, &curLock);
836 CAMERA_LOGI("Set CMD_FOCUS_LOCK [%{public}d]", focusMode);
837 if (rc == RC_OK) {
838 CAMERA_LOGI("Send OHOS_CONTROL_FOCUS_MODE value=%{public}d success", focusMode);
839 } else {
840 CAMERA_LOGE("Send OHOS_CONTROL_FOCUS_MODE value=%{public}d failed", focusMode);
841 }
842 }
843
844 int focusVal = 0;
845 if (focusMode == OHOS_CAMERA_FOCUS_MODE_CONTINUOUS_AUTO) {
846 focusVal = 1;
847 } else if (focusMode == OHOS_CAMERA_FOCUS_MODE_MANUAL) {
848 focusVal = 0;
849 focusModeCmd = CMD_AUTO_FOCUS_STOP;
850 } else if (focusMode == OHOS_CAMERA_FOCUS_MODE_AUTO) {
851 focusVal = 0;
852 focusModeCmd = CMD_AUTO_FOCUS_START;
853 }
854
855 rc = sensorVideo_->UpdateSetting(GetName(), CMD_FOCUS_AUTO, &focusVal);
856 CAMERA_LOGI("Set CMD_FOCUS_AUTO [%{public}d]", focusMode);
857 if (rc == RC_OK) {
858 CAMERA_LOGI("Send OHOS_CONTROL_FOCUS_MODE value=[%{public}d] success", focusMode);
859 } else {
860 CAMERA_LOGI("Send OHOS_CONTROL_FOCUS_MODE value=[%{public}d] failed", focusMode);
861 }
862
863 focusVal = 1;
864 rc = sensorVideo_->UpdateSetting(GetName(), focusModeCmd, &focusVal);
865 CAMERA_LOGI("Set CMD_FOCUS_AUTO [%{public}d]", focusMode);
866 if (rc == RC_ERROR) {
867 CAMERA_LOGE("Send CMD_FOCUS_AUTO fail");
868 }
869 }
870 return rc;
871 }
872
SendFocusRegionsMetaData(common_metadata_header_t * data)873 RetCode SensorController::SendFocusRegionsMetaData(common_metadata_header_t *data)
874 {
875 RetCode rc = RC_OK;
876 std::vector<int32_t> afRegions;
877 const uint32_t GROUP_LEN = 2;
878 camera_metadata_item_t entry;
879 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_AF_REGIONS, &entry);
880 if (ret == 0) {
881 for (int i = 0; i < entry.count; i++) {
882 afRegions.push_back(*(entry.data.i32 + i));
883 CAMERA_LOGI("Set afRegions [%{public}d]", *(entry.data.i32 + i));
884 }
885 if (afRegions.size() != GROUP_LEN) {
886 CAMERA_LOGE("afRegions size error");
887 return RC_ERROR;
888 }
889
890 int32_t averageVal = (afRegions[0] + afRegions[1]) / GROUP_LEN;
891 rc = sensorVideo_->UpdateSetting(GetName(), CMD_FOCUS_ABSOLUTE, (int*)&averageVal);
892 if (rc == RC_ERROR) {
893 CAMERA_LOGE("Send CMD_FOCUS_ABSOLUTE fail");
894 }
895 }
896
897 return rc;
898 }
899
SendMeterMetaData(common_metadata_header_t * data)900 RetCode SensorController::SendMeterMetaData(common_metadata_header_t *data)
901 {
902 RetCode rc = RC_OK;
903 uint8_t meterMode = 0;
904 camera_metadata_item_t entry;
905 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_METER_MODE, &entry);
906 if (ret == 0) {
907 meterMode = *(entry.data.u8);
908 int meterModeVal = 0;
909 if (meterMode == OHOS_CAMERA_SPOT_METERING) {
910 meterModeVal = V4L2_EXPOSURE_METERING_SPOT;
911 }
912 if (meterMode == OHOS_CAMERA_REGION_METERING) {
913 meterModeVal = V4L2_EXPOSURE_METERING_MATRIX;
914 }
915 if (meterMode == OHOS_CAMERA_OVERALL_METERING) {
916 meterModeVal = V4L2_EXPOSURE_METERING_AVERAGE;
917 }
918 rc = sensorVideo_->UpdateSetting(GetName(), CMD_METER_MODE, &meterModeVal);
919 CAMERA_LOGI("Set CMD_METER_MODE [%{public}d]", meterMode);
920 if (rc == RC_OK) {
921 CAMERA_LOGI("Send OHOS_CONTROL_METER_MODE value=%{public}d success", meterModeVal);
922 } else {
923 CAMERA_LOGE("Send OHOS_CONTROL_METER_MODE value=%{public}d failed", meterModeVal);
924 }
925 }
926
927 std::vector<int32_t> meterPoint;
928 ret = FindCameraMetadataItem(data, OHOS_CONTROL_METER_POINT, &entry);
929 if (ret == 0) {
930 for (int i = 0; i < entry.count; i++) {
931 meterPoint.push_back(*(entry.data.i32 + i));
932 CAMERA_LOGI("Set CMD_METER_POINT [%{public}d]", *(entry.data.i32 + i));
933 }
934 rc = sensorVideo_->UpdateSetting(GetName(), CMD_METER_POINT, (int*)&meterPoint);
935 if (rc == RC_ERROR) {
936 CAMERA_LOGE("Send CMD_METER_POINT fail");
937 }
938 }
939
940 return rc;
941 }
942
SendFlashMetaData(common_metadata_header_t * data)943 RetCode SensorController::SendFlashMetaData(common_metadata_header_t *data)
944 {
945 RetCode rc = RC_OK;
946 camera_metadata_item_t entry;
947 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FLASH_MODE, &entry);
948 if (ret == 0) {
949 uint8_t flashMode = *(entry.data.u8);
950 rc = sensorVideo_->UpdateSetting(GetName(), CMD_FLASH_MODE, (int*)&flashMode);
951 CAMERA_LOGI("Set CMD_FLASH_MODE [%{public}d]", flashMode);
952 if (rc == RC_ERROR) {
953 CAMERA_LOGE("Send CMD_FLASH_MODE fail");
954 }
955 }
956
957 return rc;
958 }
959
SendFpsMetaData(common_metadata_header_t * data)960 RetCode SensorController::SendFpsMetaData(common_metadata_header_t *data)
961 {
962 RetCode rc = RC_OK;
963 camera_metadata_item_t entry;
964 constexpr uint32_t GROUP_LEN = 2;
965 DeviceFormat format;
966 fpsRange_.clear();
967 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FPS_RANGES, &entry);
968 if (ret == 0) {
969 for (int i = 0; i < entry.count; i++) {
970 fpsRange_.push_back(*(entry.data.i32 + i));
971 }
972 if (fpsRange_.size() != GROUP_LEN) {
973 CAMERA_LOGE("fpsRange size error");
974 return RC_ERROR;
975 }
976 CAMERA_LOGI("Set CMD_FPS_RANGE [%{public}d, %{public}d]", fpsRange_[0], fpsRange_[1]);
977 format.fmtdesc.fps.denominator = (fpsRange_[0] + fpsRange_[1]) / GROUP_LEN;
978 format.fmtdesc.fps.numerator = 1;
979 CAMERA_LOGI("fps.denominator: %{public}d, fps.numerator: %{public}d",
980 format.fmtdesc.fps.denominator, format.fmtdesc.fps.numerator);
981 rc = sensorVideo_->ConfigSys(GetName(), CMD_V4L2_SET_FPS, format);
982 if (rc == RC_ERROR) {
983 CAMERA_LOGE("Send CMD_FPS_RANGE fail");
984 }
985 }
986
987 return rc;
988 }
989
Flush(int32_t streamId)990 RetCode SensorController::Flush(int32_t streamId)
991 {
992 CAMERA_LOGD("SensorController::Flush streamId = %{public}d", streamId);
993 return sensorVideo_->Flush(GetName());
994 }
995
SetMemoryType(uint8_t & memType)996 void SensorController::SetMemoryType(uint8_t &memType)
997 {
998 sensorVideo_->SetMemoryType(memType);
999 return;
1000 }
1001 } // namespace OHOS::Camera
1002