• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hcs_deal.h"
18 #include <stdlib.h>
19 #include <vector>
20 #include "hcs_dm_parser.h"
21 #include "metadata_enum_map.h"
22 
23 namespace OHOS::Camera {
HcsDeal(const std::string & pathName)24 HcsDeal::HcsDeal(const std::string &pathName) : sPathName(pathName), pDevResIns(nullptr), pRootNode(nullptr) {}
25 
~HcsDeal()26 HcsDeal::~HcsDeal()
27 {
28     ReleaseHcsTree();
29     pDevResIns = nullptr;
30     pRootNode = nullptr;
31 }
32 
SetHcsPathName(const std::string & pathName)33 void HcsDeal::SetHcsPathName(const std::string &pathName)
34 {
35     sPathName = pathName;
36 }
37 
Init()38 RetCode HcsDeal::Init()
39 {
40     ReleaseHcsTree();
41     pDevResIns = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
42     if (pDevResIns == nullptr) {
43         CAMERA_LOGE("get hcs interface failed.");
44         return RC_ERROR;
45     }
46 
47     CAMERA_LOGD("pathname = %{public}s", sPathName.c_str());
48     SetHcsBlobPath(sPathName.c_str());
49     pRootNode = pDevResIns->GetRootNode();
50     if (pRootNode == nullptr) {
51         CAMERA_LOGE("GetRootNode failed");
52         return RC_ERROR;
53     }
54     if (pRootNode->name != nullptr) {
55         CAMERA_LOGI("pRootNode = %{public}s", pRootNode->name);
56     }
57 
58     DealHcsData();
59 
60     return RC_OK;
61 }
62 
DealHcsData()63 RetCode HcsDeal::DealHcsData()
64 {
65     const struct DeviceResourceNode *cameraHostConfig = pDevResIns->GetChildNode(pRootNode, "camera_host_config");
66     if (cameraHostConfig == nullptr) {
67         return RC_ERROR;
68     }
69     if (pRootNode->name != nullptr) {
70         CAMERA_LOGI("pRootNode = %{public}s", pRootNode->name);
71     }
72     if (cameraHostConfig->name == nullptr) {
73         CAMERA_LOGW("cameraHostConfig->name is null");
74         return RC_ERROR;
75     }
76     CAMERA_LOGD("cameraHostConfig = %{public}s", cameraHostConfig->name);
77 
78     const struct DeviceResourceNode *childNodeTmp = nullptr;
79     DEV_RES_NODE_FOR_EACH_CHILD_NODE(cameraHostConfig, childNodeTmp)
80     {
81         if (childNodeTmp != nullptr && childNodeTmp->name != nullptr) {
82             std::string nodeName = std::string(childNodeTmp->name);
83             CAMERA_LOGI("cameraHostConfig subnode name = %{public}s", nodeName.c_str());
84             if (nodeName.find(std::string("ability"), 0) != std::string::npos) {
85                 DealCameraAbility(*childNodeTmp);
86             }
87         }
88     }
89 
90     return RC_OK;
91 }
92 
DealCameraAbility(const struct DeviceResourceNode & node)93 RetCode HcsDeal::DealCameraAbility(const struct DeviceResourceNode &node)
94 {
95     CAMERA_LOGI("nodeName = %{public}s", node.name);
96 
97     const char *cameraId = nullptr;
98     int32_t ret = pDevResIns->GetString(&node, "logicCameraId", &cameraId, nullptr);
99     if (ret != 0) {
100         CAMERA_LOGW("get logic cameraid failed");
101         return RC_ERROR;
102     }
103     CAMERA_LOGD("logic cameraid is %{public}s", cameraId);
104 
105     std::vector<std::string> phyCameraIds;
106     (void)DealPhysicsCameraId(node, phyCameraIds);
107     if (!phyCameraIds.empty() && cameraId != nullptr) {
108         cameraIdMap_.insert(std::make_pair(std::string(cameraId), phyCameraIds));
109     }
110 
111     const struct DeviceResourceNode *metadataNode = pDevResIns->GetChildNode(&node, "metadata");
112     if (metadataNode == nullptr || cameraId == nullptr) {
113         CAMERA_LOGW("metadataNode is null or cameraId is null");
114         return RC_ERROR;
115     }
116     RetCode rc = DealMetadata(cameraId, *metadataNode);
117     if (rc != RC_OK) {
118         CAMERA_LOGW("deal metadata failed");
119         return RC_ERROR;
120     }
121 
122     for (CameraIdMap::iterator itr = cameraIdMap_.begin(); itr != cameraIdMap_.end(); ++itr) {
123         CAMERA_LOGD("cameraId = %{public}s", itr->first.c_str());
124         for (auto &str : itr->second) {
125             CAMERA_LOGD("phyCameraId = %{public}s", str.c_str());
126         }
127     }
128 
129     return RC_OK;
130 }
131 
DealPhysicsCameraId(const struct DeviceResourceNode & node,std::vector<std::string> & cameraIds)132 RetCode HcsDeal::DealPhysicsCameraId(const struct DeviceResourceNode &node, std::vector<std::string> &cameraIds)
133 {
134     const char *nodeValue = nullptr;
135     int32_t elemNum = pDevResIns->GetElemNum(&node, "physicsCameraIds");
136     for (int i = 0; i < elemNum; i++) {
137         pDevResIns->GetStringArrayElem(&node, "physicsCameraIds", i, &nodeValue, nullptr);
138         cameraIds.push_back(std::string(nodeValue));
139     }
140 
141     return RC_OK;
142 }
143 
DealMetadata(const std::string & cameraId,const struct DeviceResourceNode & node)144 RetCode HcsDeal::DealMetadata(const std::string &cameraId, const struct DeviceResourceNode &node)
145 {
146     struct DeviceResourceAttr *drAttr = nullptr;
147     DEV_RES_NODE_FOR_EACH_ATTR(&node, drAttr) {}
148 
149     CAMERA_LOGD("metadata = %{public}s", node.name);
150     const int ENTRY_CAPACITY = 30;
151     const int DATA_CAPACITY = 2000;
152     std::shared_ptr<CameraMetadata> metadata = std::make_shared<CameraMetadata>(ENTRY_CAPACITY, DATA_CAPACITY);
153     DealAeAvailableAntiBandingModes(node, metadata);
154     DealAeAvailableModes(node, metadata);
155     DealAvailableFpsRange(node, metadata);
156     DealCameraPosition(node, metadata);
157     DealCameraType(node, metadata);
158     DealCameraConnectionType(node, metadata);
159     DealCameraMemoryType(node, metadata);
160     DealCameraFaceDetectMaxNum(node, metadata);
161     DealAeCompensationRange(node, metadata);
162     DealAeCompensationSteps(node, metadata);
163     DealAvailableAwbModes(node, metadata);
164     DealSensitivityRange(node, metadata);
165     DealFaceDetectMode(node, metadata);
166     DealFocalLength(node, metadata);
167     DealAvailableFocusModes(node, metadata);
168     DealAvailableExposureModes(node, metadata);
169     DealAvailableMetereModes(node, metadata);
170     DealAvalialbleFlashModes(node, metadata);
171     DealMirrorSupported(node, metadata);
172     DealAvaliableBasicConfigurations(node, metadata);
173     DealSensorOrientation(node, metadata);
174     DealAvalialbleVideoStabilizationModes(node, metadata);
175     DealAvalialbleFlash(node, metadata);
176     DealAvalialbleAutoFocus(node, metadata);
177     DealZoomRationRange(node, metadata);
178     DealJpegOrientation(node, metadata);
179     cameraMetadataMap_.insert(std::make_pair(cameraId, metadata));
180 
181     return RC_OK;
182 }
183 
DealAeAvailableAntiBandingModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)184 RetCode HcsDeal::DealAeAvailableAntiBandingModes(
185     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
186 {
187     const char *nodeValue = nullptr;
188     std::vector<uint8_t> aeAvailableAntiBandingModeUint8s;
189     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "aeAvailableAntiBandingModes");
190     for (int i = 0; i < elemNum; i++) {
191         pDevResIns->GetStringArrayElem(&metadataNode, "aeAvailableAntiBandingModes", i, &nodeValue, nullptr);
192         aeAvailableAntiBandingModeUint8s.push_back(AeAntibandingModeMap[std::string(nodeValue)]);
193         CAMERA_LOGD("aeAvailableAntiBandingModes = %{public}s", nodeValue);
194     }
195     bool ret = metadata->addEntry(OHOS_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, aeAvailableAntiBandingModeUint8s.data(),
196         aeAvailableAntiBandingModeUint8s.size());
197     if (!ret) {
198         CAMERA_LOGE("aeAvailableAntiBandingModes add failed");
199         return RC_ERROR;
200     }
201     CAMERA_LOGD("aeAvailableAntiBandingModes add success");
202     return RC_OK;
203 }
204 
DealAeAvailableModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)205 RetCode HcsDeal::DealAeAvailableModes(
206     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
207 {
208     int32_t hcbRet = -1;
209     const char *nodeValue = nullptr;
210     std::vector<uint8_t> aeAvailableModesU8;
211     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "aeAvailableModes");
212     for (int i = 0; i < elemNum; i++) {
213         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "aeAvailableModes", i, &nodeValue, nullptr);
214         if (hcbRet != 0) {
215             CAMERA_LOGD("get aeAvailableModes failed");
216             continue;
217         }
218         aeAvailableModesU8.push_back(AeModeMap[std::string(nodeValue)]);
219         CAMERA_LOGD("aeAvailableModes = %{public}s", nodeValue);
220     }
221     bool ret =
222         metadata->addEntry(OHOS_CONTROL_AE_AVAILABLE_MODES, aeAvailableModesU8.data(), aeAvailableModesU8.size());
223     if (!ret) {
224         CAMERA_LOGE("aeAvailableModes add failed");
225         return RC_ERROR;
226     }
227     CAMERA_LOGD("aeAvailableModes add success");
228     return RC_OK;
229 }
230 
DealAvailableFpsRange(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)231 RetCode HcsDeal::DealAvailableFpsRange(
232     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
233 {
234     int32_t hcbRet = -1;
235     uint32_t nodeValue;
236     std::vector<int32_t> availableFpsRange;
237     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "availableFpsRange");
238     constexpr uint32_t groupLen = 2;
239 
240     if (elemNum != groupLen) {
241         CAMERA_LOGE("availableFpsRange hcs file configuration error");
242         return RC_ERROR;
243     }
244 
245     for (int i = 0; i < elemNum; i++) {
246         hcbRet = pDevResIns->GetUint32ArrayElem(&metadataNode, "availableFpsRange", i, &nodeValue, -1);
247         if (hcbRet != 0) {
248             CAMERA_LOGD("get availableFpsRange failed");
249             continue;
250         }
251         availableFpsRange.push_back(static_cast<int32_t>(nodeValue));
252         CAMERA_LOGD("get availableFpsRange:%{public}d", nodeValue);
253     }
254     bool ret = metadata->addEntry(OHOS_ABILITY_FPS_RANGES, availableFpsRange.data(), availableFpsRange.size());
255     if (!ret) {
256         CAMERA_LOGE("availableFpsRange add failed");
257         return RC_ERROR;
258     }
259     CAMERA_LOGD("availableFpsRange add success");
260     return RC_OK;
261 }
262 
DealCameraPosition(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)263 RetCode HcsDeal::DealCameraPosition(
264     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
265 {
266     CAMERA_LOGD("cameraPosition in...");
267     const char *nodeValue = nullptr;
268     uint8_t cameraPosition = 0;
269 
270     int32_t rc = pDevResIns->GetString(&metadataNode, "cameraPosition", &nodeValue, nullptr);
271     if (rc != 0 || (nodeValue == nullptr)) {
272         CAMERA_LOGE("get cameraPosition failed");
273         return RC_ERROR;
274     }
275 
276     cameraPosition = CameraPositionMap[std::string(nodeValue)];
277     CAMERA_LOGD("cameraPosition  = %{public}d", cameraPosition);
278 
279     bool ret = metadata->addEntry(
280         OHOS_ABILITY_CAMERA_POSITION, static_cast<const void *>(&cameraPosition), sizeof(cameraPosition));
281     if (!ret) {
282         CAMERA_LOGE("cameraPosition add failed");
283         return RC_ERROR;
284     }
285     CAMERA_LOGD("cameraPosition add success");
286     return RC_OK;
287 }
288 
DealCameraType(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)289 RetCode HcsDeal::DealCameraType(
290     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
291 {
292     CAMERA_LOGD("cameraType in...");
293     const char *nodeValue = nullptr;
294     uint8_t cameraType = 0;
295 
296     int32_t rc = pDevResIns->GetString(&metadataNode, "cameraType", &nodeValue, nullptr);
297     if (rc != 0 || (nodeValue == nullptr)) {
298         CAMERA_LOGE("get cameraType failed");
299         return RC_ERROR;
300     }
301 
302     cameraType = CameraTypeMap[std::string(nodeValue)];
303     CAMERA_LOGD("cameraType  = %{public}d", cameraType);
304 
305     bool ret = metadata->addEntry(OHOS_ABILITY_CAMERA_TYPE, static_cast<const void *>(&cameraType), sizeof(cameraType));
306     if (!ret) {
307         CAMERA_LOGE("cameraType add failed");
308         return RC_ERROR;
309     }
310     CAMERA_LOGD("cameraType add success");
311     return RC_OK;
312 }
313 
DealCameraConnectionType(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)314 RetCode HcsDeal::DealCameraConnectionType(
315     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
316 {
317     CAMERA_LOGD("cameraConnectionType in...");
318     const char *nodeValue = nullptr;
319     uint8_t cameraConnectionType = 0;
320 
321     int32_t rc = pDevResIns->GetString(&metadataNode, "cameraConnectionType", &nodeValue, nullptr);
322     if (rc != 0 || (nodeValue == nullptr)) {
323         CAMERA_LOGE("get cameraConnectionType failed");
324         return RC_ERROR;
325     }
326 
327     cameraConnectionType = cameraConnectionTypeMap[std::string(nodeValue)];
328     CAMERA_LOGD("cameraConnectionType  = %{public}d", cameraConnectionType);
329 
330     bool ret = metadata->addEntry(OHOS_ABILITY_CAMERA_CONNECTION_TYPE, static_cast<const void *>(&cameraConnectionType),
331         sizeof(cameraConnectionType));
332     if (!ret) {
333         CAMERA_LOGE("cameraConnectionType add failed");
334         return RC_ERROR;
335     }
336     CAMERA_LOGD("cameraConnectionType add success");
337     return RC_OK;
338 }
339 
DealCameraMemoryType(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)340 RetCode HcsDeal::DealCameraMemoryType(
341     const struct DeviceResourceNode &metadataNode,
342     std::shared_ptr<CameraMetadata> &metadata)
343 {
344     CAMERA_LOGD("cameraMemoryType in...");
345     const char *nodeValue = nullptr;
346     uint8_t cameraMemoryType = 0;
347     int32_t rc = pDevResIns->GetString(&metadataNode, "cameraMemoryType", &nodeValue, nullptr);
348     if (rc != 0 || (nodeValue == nullptr)) {
349         CAMERA_LOGE("get cameraMemoryType failed");
350         return RC_ERROR;
351     }
352     auto findIf = CameraMemoryTypeMap.find(std::string(nodeValue));
353     if (findIf == CameraMemoryTypeMap.end()) {
354         CAMERA_LOGE("value of cameraMemoryType err.[%{public}s]", nodeValue);
355         return RC_ERROR;
356     }
357     cameraMemoryType = CameraMemoryTypeMap[std::string(nodeValue)];
358     CAMERA_LOGD("cameraMemoryType  = %{public}d", cameraMemoryType);
359     bool ret = metadata->addEntry(OHOS_ABILITY_MEMORY_TYPE,
360         static_cast<const void*>(&cameraMemoryType), sizeof(cameraMemoryType));
361     if (!ret) {
362         CAMERA_LOGE("cameraMemoryType add failed");
363         return RC_ERROR;
364     }
365     CAMERA_LOGD("cameraMemoryType add success");
366     return RC_OK;
367 }
368 
DealCameraFaceDetectMaxNum(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)369 RetCode HcsDeal::DealCameraFaceDetectMaxNum(
370     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
371 {
372     const char *pNodeValue = nullptr;
373     uint8_t faceDetectMaxNum;
374 
375     int32_t rc = pDevResIns->GetString(&metadataNode, "faceDetectMaxNum", &pNodeValue, nullptr);
376     if (rc != 0 || (pNodeValue == nullptr)) {
377         CAMERA_LOGE("get faceDetectMaxNum failed");
378         return RC_ERROR;
379     }
380 
381     faceDetectMaxNum = atoi(pNodeValue);
382     CAMERA_LOGD("faceDetectMaxNum  = %{public}f", faceDetectMaxNum);
383 
384     bool ret = metadata->addEntry(OHOS_STATISTICS_FACE_DETECT_MAX_NUM, static_cast<const void *>(&faceDetectMaxNum), 1);
385     if (!ret) {
386         CAMERA_LOGE("faceDetectMaxNum add failed");
387         return RC_ERROR;
388     }
389     CAMERA_LOGD("faceDetectMaxNum add success");
390     return RC_OK;
391 }
392 
DealAeCompensationRange(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)393 RetCode HcsDeal::DealAeCompensationRange(
394     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
395 {
396     std::vector<int32_t> aeCompensationRange;
397     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "aeCompensationRange");
398     uint32_t nodeValue;
399     for (int i = 0; i < elemNum; i++) {
400         pDevResIns->GetUint32ArrayElem(&metadataNode, "aeCompensationRange", i, &nodeValue, -1);
401         aeCompensationRange.push_back(static_cast<int32_t>(nodeValue));
402     }
403 
404     bool ret =
405         metadata->addEntry(OHOS_CONTROL_AE_COMPENSATION_RANGE, aeCompensationRange.data(), aeCompensationRange.size());
406     if (!ret) {
407         CAMERA_LOGD("aeCompensationRange add failed");
408         return RC_ERROR;
409     }
410     CAMERA_LOGI("aeCompensationRange add success");
411     return RC_OK;
412 }
413 
DealAeCompensationSteps(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)414 RetCode HcsDeal::DealAeCompensationSteps(
415     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
416 {
417     constexpr const char *AE_COMPENSATION_STEPS = "aeCompensationSteps";
418     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, AE_COMPENSATION_STEPS);
419     uint32_t nodeValue;
420     camera_rational_t aeCompensationStep;
421     constexpr uint32_t groupLen = 2;
422 
423     if (elemNum != groupLen) {
424         CAMERA_LOGE("aeCompensationSteps hcs file configuration error");
425         return RC_ERROR;
426     }
427 
428     pDevResIns->GetUint32ArrayElem(&metadataNode, AE_COMPENSATION_STEPS, 0, &nodeValue, -1);
429     aeCompensationStep.numerator = (int32_t)nodeValue;
430     pDevResIns->GetUint32ArrayElem(&metadataNode, AE_COMPENSATION_STEPS, 1, &nodeValue, -1);
431     aeCompensationStep.denominator = (int32_t)nodeValue;
432 
433     bool ret = metadata->addEntry(OHOS_CONTROL_AE_COMPENSATION_STEP, &aeCompensationStep, 1);
434     if (!ret) {
435         CAMERA_LOGE("aeCompensationSteps add failed");
436         return RC_ERROR;
437     }
438     CAMERA_LOGD("aeCompensationSteps add success");
439     return RC_OK;
440 }
441 
DealAvailableAwbModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)442 RetCode HcsDeal::DealAvailableAwbModes(
443     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
444 {
445     int32_t hcbRet = -1;
446     const char *nodeValue = nullptr;
447     std::vector<uint8_t> availableAwbModes;
448     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "availableAwbModes");
449     for (int i = 0; i < elemNum; i++) {
450         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "availableAwbModes", i, &nodeValue, nullptr);
451         if (hcbRet != 0) {
452             CAMERA_LOGD("get availableAwbModes failed");
453             continue;
454         }
455         availableAwbModes.push_back(AwbModeMap[std::string(nodeValue)]);
456     }
457     bool ret = metadata->addEntry(OHOS_CONTROL_AWB_AVAILABLE_MODES, availableAwbModes.data(), availableAwbModes.size());
458     if (!ret) {
459         CAMERA_LOGE("availableAwbModes add failed");
460         return RC_ERROR;
461     }
462     CAMERA_LOGD("availableAwbModes add success");
463     return RC_OK;
464 }
465 
DealSensitivityRange(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)466 RetCode HcsDeal::DealSensitivityRange(
467     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
468 {
469     std::vector<int32_t> sensitivityRange;
470     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "sensitivityRange");
471     CAMERA_LOGD("sensitivityRange elemNum = %{public}d", elemNum);
472     uint32_t nodeValue;
473     for (int i = 0; i < elemNum; i++) {
474         pDevResIns->GetUint32ArrayElem(&metadataNode, "sensitivityRange", i, &nodeValue, -1);
475         sensitivityRange.push_back(static_cast<int32_t>(nodeValue));
476     }
477 
478     bool ret = metadata->addEntry(OHOS_SENSOR_INFO_SENSITIVITY_RANGE, sensitivityRange.data(), sensitivityRange.size());
479     if (!ret) {
480         CAMERA_LOGI("sensitivityRange add failed");
481         return RC_ERROR;
482     }
483     CAMERA_LOGD("sensitivityRange add success");
484     return RC_OK;
485 }
486 
DealFaceDetectMode(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)487 RetCode HcsDeal::DealFaceDetectMode(
488     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
489 {
490     const char *pNodeValue = nullptr;
491     int32_t rc = pDevResIns->GetString(&metadataNode, "faceDetectMode", &pNodeValue, nullptr);
492     if (rc != 0) {
493         CAMERA_LOGI("get faceDetectMode failed");
494         return RC_ERROR;
495     }
496 
497     bool ret = metadata->addEntry(OHOS_STATISTICS_FACE_DETECT_MODE, &(FaceDetectModeMap[std::string(pNodeValue)]), 1);
498     if (!ret) {
499         CAMERA_LOGI("faceDetectMode add failed");
500         return RC_ERROR;
501     }
502     CAMERA_LOGD("faceDetectMode add success");
503     return RC_OK;
504 }
505 
DealAvailableResultKeys(const struct DeviceResourceNode & metadataNode,std::shared_ptr<CameraMetadata> & metadata)506 RetCode HcsDeal::DealAvailableResultKeys(
507     const struct DeviceResourceNode &metadataNode, std::shared_ptr<CameraMetadata> &metadata)
508 {
509     int32_t hcbRet = -1;
510     const char *nodeValue = nullptr;
511     std::vector<int32_t> availableResultKeys;
512     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "availableResultKeys");
513     for (int i = 0; i < elemNum; i++) {
514         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "availableResultKeys", i, &nodeValue, nullptr);
515         if (hcbRet != 0) {
516             CAMERA_LOGI("get availableResultKeys failed");
517             continue;
518         }
519         availableResultKeys.push_back(MetadataTagMap[std::string(nodeValue)]);
520     }
521     bool ret = metadata->addEntry(
522         OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, availableResultKeys.data(), availableResultKeys.size());
523     if (!ret) {
524         CAMERA_LOGI("availableResultKeys add failed");
525         return RC_ERROR;
526     }
527     CAMERA_LOGD("availableResultKeys add success");
528     return RC_OK;
529 }
530 
DealFocalLength(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)531 RetCode HcsDeal::DealFocalLength(
532     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
533 {
534     const char *pNodeValue = nullptr;
535     float focalLength;
536 
537     int32_t rc = pDevResIns->GetString(&metadataNode, "focalLength", &pNodeValue, nullptr);
538     if (rc != 0 || (pNodeValue == nullptr)) {
539         CAMERA_LOGE("get focalLength failed");
540         return RC_ERROR;
541     }
542 
543     focalLength = atof(pNodeValue);
544     CAMERA_LOGD("focalLength  = %{public}f", focalLength);
545 
546     bool ret = metadata->addEntry(OHOS_ABILITY_FOCAL_LENGTH, static_cast<const void *>(&focalLength), 1);
547     if (!ret) {
548         CAMERA_LOGE("focalLength add failed");
549         return RC_ERROR;
550     }
551     CAMERA_LOGD("focalLength add success");
552     return RC_OK;
553 }
554 
DealAvailableFocusModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)555 RetCode HcsDeal::DealAvailableFocusModes(
556     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
557 {
558     uint8_t hcbRet = 0;
559     const char *nodeValue = nullptr;
560     std::vector<uint8_t> focusAvailableModes;
561 
562     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "focusAvailableModes");
563     CAMERA_LOGD("elemNum = %{public}d", elemNum);
564 
565     for (int i = 0; i < elemNum; i++) {
566         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "focusAvailableModes", i, &nodeValue, nullptr);
567         if (hcbRet != 0) {
568             CAMERA_LOGE("get focusAvailableModes failed");
569             continue;
570         }
571         CAMERA_LOGD("nodeValue = %{public}s", nodeValue);
572         focusAvailableModes.push_back(FocusModeMap[std::string(nodeValue)]);
573     }
574     bool ret = metadata->addEntry(OHOS_ABILITY_FOCUS_MODES, focusAvailableModes.data(), focusAvailableModes.size());
575     if (!ret) {
576         CAMERA_LOGE("focusAvailableModes add failed");
577         return RC_ERROR;
578     }
579     CAMERA_LOGD("focusAvailableModes add success");
580     return RC_OK;
581 }
582 
DealAvailableExposureModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)583 RetCode HcsDeal::DealAvailableExposureModes(
584     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
585 {
586     uint8_t hcbRet = 0;
587     const char *nodeValue = nullptr;
588     std::vector<uint8_t> exposureModeResult;
589 
590     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "exposureAvailableModes");
591     CAMERA_LOGD("elemNum = %{public}d", elemNum);
592 
593     for (int i = 0; i < elemNum; i++) {
594         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "exposureAvailableModes", i, &nodeValue, nullptr);
595         if (hcbRet != 0) {
596             CAMERA_LOGE("get exposureModeResult failed");
597             continue;
598         }
599         CAMERA_LOGD("nodeValue = %{public}s", nodeValue);
600         exposureModeResult.push_back(ExposureModeMap[std::string(nodeValue)]);
601     }
602     bool ret = metadata->addEntry(OHOS_ABILITY_EXPOSURE_MODES, exposureModeResult.data(), exposureModeResult.size());
603     if (!ret) {
604         CAMERA_LOGE("exposureModeResult add failed");
605         return RC_ERROR;
606     }
607     CAMERA_LOGD("exposureModeResult add success");
608     return RC_OK;
609 }
610 
DealAvailableMetereModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)611 RetCode HcsDeal::DealAvailableMetereModes(
612     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
613 {
614     uint8_t hcbRet = 0;
615     const char *nodeValue = nullptr;
616     std::vector<uint8_t> meterModeResult;
617 
618     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "meterAvailableModes");
619     CAMERA_LOGD("elemNum = %{public}d", elemNum);
620 
621     for (int i = 0; i < elemNum; i++) {
622         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "meterAvailableModes", i, &nodeValue, nullptr);
623         if (hcbRet != 0) {
624             CAMERA_LOGE("get meterModeResult failed");
625             continue;
626         }
627         CAMERA_LOGD("nodeValue = %{public}s", nodeValue);
628         meterModeResult.push_back(meterModeMap[std::string(nodeValue)]);
629     }
630     bool ret = metadata->addEntry(OHOS_ABILITY_METER_MODES, meterModeResult.data(), meterModeResult.size());
631     if (!ret) {
632         CAMERA_LOGE("meterModeResult add failed");
633         return RC_ERROR;
634     }
635     CAMERA_LOGD("meterModeResult add success");
636     return RC_OK;
637 }
638 
DealAvalialbleFlashModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)639 RetCode HcsDeal::DealAvalialbleFlashModes(
640     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
641 {
642     uint8_t hcbRet = 0;
643     const char *nodeValue = nullptr;
644     std::vector<uint8_t> flashAvailableModeUint8s;
645 
646     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "flashAvailableModes");
647     CAMERA_LOGD("elemNum = %{public}d", elemNum);
648 
649     for (int i = 0; i < elemNum; i++) {
650         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "flashAvailableModes", i, &nodeValue, nullptr);
651         if (hcbRet != 0) {
652             CAMERA_LOGE("get flashAvailableModes failed");
653             continue;
654         }
655         CAMERA_LOGD("nodeValue = %{public}s", nodeValue);
656         flashAvailableModeUint8s.push_back(FlashModeMap[std::string(nodeValue)]);
657     }
658     bool ret =
659         metadata->addEntry(OHOS_ABILITY_FLASH_MODES, flashAvailableModeUint8s.data(), flashAvailableModeUint8s.size());
660     if (!ret) {
661         CAMERA_LOGE("flashAvailableModes add failed");
662         return RC_ERROR;
663     }
664     CAMERA_LOGD("flashAvailableModes add success");
665     return RC_OK;
666 }
667 
DealMirrorSupported(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)668 RetCode HcsDeal::DealMirrorSupported(
669     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
670 {
671     const char *nodeValue = nullptr;
672     uint8_t mirrorSupportU8;
673 
674     int32_t rc = pDevResIns->GetString(&metadataNode, "mirrorSupported", &nodeValue, nullptr);
675     if (rc != 0 || (nodeValue == nullptr)) {
676         CAMERA_LOGE("get mirrorSupported failed");
677         return RC_ERROR;
678     }
679 
680     mirrorSupportU8 = mirrorMap[std::string(nodeValue)];
681     CAMERA_LOGD("mirrorSupportU8  = %{public}d", mirrorSupportU8);
682 
683     bool ret =
684         metadata->addEntry(OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED, static_cast<const void *>(&mirrorSupportU8), 1);
685     if (!ret) {
686         CAMERA_LOGE("mirrorSupported add failed");
687         return RC_ERROR;
688     }
689     CAMERA_LOGD("mirrorSupported add success");
690     return RC_OK;
691 }
692 
DealAvaliableBasicConfigurations(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)693 RetCode HcsDeal::DealAvaliableBasicConfigurations(
694     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
695 {
696     uint32_t nodeValue;
697     std::vector<int32_t> basicConfigAvaliableInt32s;
698 
699     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "basicAvailableConfigurations");
700     CAMERA_LOGD("elemNum = %{public}d", elemNum);
701 
702     constexpr int STREAM_INFO_ITEM_LENGTH = 3;
703     for (int i = 0; i < elemNum; i++) {
704         pDevResIns->GetUint32ArrayElem(&metadataNode, "basicAvailableConfigurations", i, &nodeValue, -1);
705         CAMERA_LOGD("nodeValue = %{public}d", nodeValue);
706 
707         if (i % STREAM_INFO_ITEM_LENGTH == 0) {
708             basicConfigAvaliableInt32s.push_back(formatArray[static_cast<int32_t>(nodeValue) - 1]);
709         } else {
710             basicConfigAvaliableInt32s.push_back(static_cast<int32_t>(nodeValue));
711         }
712     }
713 
714     bool ret = metadata->addEntry(OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS, basicConfigAvaliableInt32s.data(),
715         basicConfigAvaliableInt32s.size());
716     if (!ret) {
717         CAMERA_LOGD("basicAvailableConfigurations add failed");
718         return RC_ERROR;
719     }
720     CAMERA_LOGI("basicAvailableConfigurations add success");
721     return RC_OK;
722 }
723 
DealSensorOrientation(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)724 RetCode HcsDeal::DealSensorOrientation(
725     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
726 {
727     const char *nodeValue = nullptr;
728     int32_t sensorOrientation;
729 
730     int32_t rc = pDevResIns->GetString(&metadataNode, "sensorOrientationSupported", &nodeValue, nullptr);
731     if (rc != 0 || (nodeValue == nullptr)) {
732         CAMERA_LOGE("get sensorOrientationSupported failed");
733         return RC_ERROR;
734     }
735 
736     sensorOrientation = atoi(nodeValue);
737     CAMERA_LOGI("sensorOrientation  = %{public}d", sensorOrientation);
738 
739     constexpr uint32_t DATA_COUNT = 1;
740     bool ret = metadata->addEntry(OHOS_SENSOR_ORIENTATION, static_cast<const void *>(&sensorOrientation), DATA_COUNT);
741     if (!ret) {
742         CAMERA_LOGE("sensorOrientationSupported add failed");
743         return RC_ERROR;
744     }
745     CAMERA_LOGI("sensorOrientationSupported add success");
746     return RC_OK;
747 }
748 
DealAvalialbleVideoStabilizationModes(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)749 RetCode HcsDeal::DealAvalialbleVideoStabilizationModes(
750     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
751 {
752     uint8_t hcbRet = 0;
753     const char *nodeValue = nullptr;
754     std::vector<uint8_t> videoStabilizationAvailableModes;
755 
756     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "videoStabilizationAvailableModes");
757     CAMERA_LOGI("elemNum = %{public}d", elemNum);
758     for (int i = 0; i < elemNum; i++) {
759         hcbRet =
760             pDevResIns->GetStringArrayElem(&metadataNode, "videoStabilizationAvailableModes", i, &nodeValue, nullptr);
761         if (hcbRet != 0) {
762             CAMERA_LOGE("get videoStabilizationAvailableModes failed");
763             continue;
764         }
765         CAMERA_LOGI("nodeValue = %{public}s", nodeValue);
766         videoStabilizationAvailableModes.push_back(videoStabilizationMap[std::string(nodeValue)]);
767     }
768     bool ret = metadata->addEntry(OHOS_ABILITY_VIDEO_STABILIZATION_MODES, videoStabilizationAvailableModes.data(),
769         videoStabilizationAvailableModes.size());
770     if (!ret) {
771         CAMERA_LOGE("videoStabilizationAvailableModes add failed");
772         return RC_ERROR;
773     }
774     CAMERA_LOGI("videoStabilizationAvailableModes add success");
775     return RC_OK;
776 }
777 
DealAvalialbleFlash(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)778 RetCode HcsDeal::DealAvalialbleFlash(
779     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
780 {
781     const char *nodeValue = nullptr;
782     uint8_t flashAvailable;
783 
784     int32_t rc = pDevResIns->GetString(&metadataNode, "flashAvailable", &nodeValue, nullptr);
785     if (rc != 0 || (nodeValue == nullptr)) {
786         CAMERA_LOGE("get flashAvailable failed");
787         return RC_ERROR;
788     }
789 
790     if (flashAvailableMap.count(std::string(nodeValue)) == 0) {
791         CAMERA_LOGE("flashAvailable invalid argument");
792         return RC_ERROR;
793     }
794     flashAvailable = flashAvailableMap[std::string(nodeValue)];
795     CAMERA_LOGI("flashAvailable  = %{public}d", flashAvailable);
796 
797     constexpr uint32_t DATA_COUNT = 1;
798     bool ret = metadata->addEntry(OHOS_ABILITY_FLASH_AVAILABLE, static_cast<const void *>(&flashAvailable), DATA_COUNT);
799     if (!ret) {
800         CAMERA_LOGE("flashAvailable add failed");
801         return RC_ERROR;
802     }
803     CAMERA_LOGI("flashAvailable add success");
804     return RC_OK;
805 }
806 
DealAvalialbleAutoFocus(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)807 RetCode HcsDeal::DealAvalialbleAutoFocus(
808     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
809 {
810     uint8_t hcbRet = 0;
811     const char *nodeValue = nullptr;
812     std::vector<uint8_t> afAvailable;
813 
814     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "afAvailable");
815     CAMERA_LOGI("elemNum = %{public}d", elemNum);
816     for (int i = 0; i < elemNum; i++) {
817         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "afAvailable", i, &nodeValue, nullptr);
818         if (hcbRet != 0) {
819             CAMERA_LOGE("get afAvailable failed");
820             continue;
821         }
822         CAMERA_LOGI("nodeValue = %{public}s", nodeValue);
823         if (AfModeMap.count(std::string(nodeValue)) == 0) {
824             CAMERA_LOGE("afAvailable invalid argument");
825             return RC_ERROR;
826         }
827         afAvailable.push_back(AfModeMap[std::string(nodeValue)]);
828     }
829     bool ret = metadata->addEntry(OHOS_CONTROL_AF_AVAILABLE_MODES, afAvailable.data(), afAvailable.size());
830     if (!ret) {
831         CAMERA_LOGE("afAvailable add failed");
832         return RC_ERROR;
833     }
834     CAMERA_LOGI("afAvailable add success");
835     return RC_OK;
836 }
837 
DealZoomRationRange(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)838 RetCode HcsDeal::DealZoomRationRange(
839     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
840 {
841     uint8_t hcbRet = 0;
842     const char *nodeValue = nullptr;
843     std::vector<float> zoomRatioRange;
844 
845     int32_t elemNum = pDevResIns->GetElemNum(&metadataNode, "zoomRatioRange");
846     CAMERA_LOGI("elemNum = %{public}d", elemNum);
847 
848     constexpr uint32_t GROUP_LEN = 2;
849     if (elemNum % GROUP_LEN != 0) {
850         CAMERA_LOGE("zoomRatioRange hcs file configuration error");
851         return RC_ERROR;
852     }
853 
854     for (int i = 0; i < elemNum; i++) {
855         hcbRet = pDevResIns->GetStringArrayElem(&metadataNode, "zoomRatioRange", i, &nodeValue, nullptr);
856         if (hcbRet != 0) {
857             CAMERA_LOGE("get zoomRatioRange failed");
858             continue;
859         }
860         CAMERA_LOGI("nodeValue = %{public}s", nodeValue);
861         zoomRatioRange.push_back(atof(nodeValue));
862     }
863 
864     for (int i = 0; i < elemNum - 1;) {
865         if (zoomRatioRange[i + 1] < zoomRatioRange[i]) {
866             CAMERA_LOGE("zoomRatioRange invalid argument");
867             return RC_ERROR;
868         }
869         constexpr uint32_t INDEX_INTERVAL = 2;
870         i = i + INDEX_INTERVAL;
871     }
872 
873     bool ret = metadata->addEntry(OHOS_ABILITY_ZOOM_RATIO_RANGE, zoomRatioRange.data(), zoomRatioRange.size());
874     if (!ret) {
875         CAMERA_LOGE("zoomRatioRange add failed");
876         return RC_ERROR;
877     }
878     CAMERA_LOGI("zoomRatioRange add success");
879     return RC_OK;
880 }
881 
DealJpegOrientation(const struct DeviceResourceNode & metadataNode,std::shared_ptr<Camera::CameraMetadata> & metadata)882 RetCode HcsDeal::DealJpegOrientation(
883     const struct DeviceResourceNode &metadataNode, std::shared_ptr<Camera::CameraMetadata> &metadata)
884 {
885     const char *nodeValue = nullptr;
886     int32_t jpegOrientation;
887 
888     int32_t rc = pDevResIns->GetString(&metadataNode, "jpegOrientation", &nodeValue, nullptr);
889     if (rc != 0 || (nodeValue == nullptr)) {
890         CAMERA_LOGE("get jpegOrientation failed");
891         return RC_ERROR;
892     }
893 
894     jpegOrientation = atoi(nodeValue);
895     CAMERA_LOGI("jpegOrientation  = %{public}d", jpegOrientation);
896 
897     if (jpegOrientation != OHOS_CAMERA_JPEG_ROTATION_0 && jpegOrientation != OHOS_CAMERA_JPEG_ROTATION_90 &&
898         jpegOrientation != OHOS_CAMERA_JPEG_ROTATION_180 && jpegOrientation != OHOS_CAMERA_JPEG_ROTATION_270) {
899         CAMERA_LOGE("jpegOrientation invalid argument");
900         return RC_ERROR;
901     }
902 
903     constexpr uint32_t DATA_COUNT = 1;
904     bool ret = metadata->addEntry(OHOS_JPEG_ORIENTATION, static_cast<const void *>(&jpegOrientation), DATA_COUNT);
905     if (!ret) {
906         CAMERA_LOGE("jpegOrientation add failed");
907         return RC_ERROR;
908     }
909     CAMERA_LOGI("jpegOrientation add success");
910     return RC_OK;
911 }
912 
GetMetadata(CameraMetadataMap & metadataMap) const913 RetCode HcsDeal::GetMetadata(CameraMetadataMap &metadataMap) const
914 {
915     metadataMap = cameraMetadataMap_;
916     return RC_OK;
917 }
918 
GetCameraId(CameraIdMap & cameraIdMap) const919 RetCode HcsDeal::GetCameraId(CameraIdMap &cameraIdMap) const
920 {
921     cameraIdMap = cameraIdMap_;
922     return RC_OK;
923 }
924 } // namespace OHOS::Camera
925