1 /*
2  * Copyright (c) 2023-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 "dps_metadata_info.h"
17 #include "dp_log.h"
18 #include <message_parcel.h>
19 
20 #define DPS_MAX_USER_DATA_COUNT 1000
21 
22 namespace OHOS {
23 namespace CameraStandard {
24 
ReadFromParcel(MessageParcel & parcel)25 DpsMetadataError DpsMetadata::ReadFromParcel(MessageParcel &parcel)
26 {
27     int32_t size = parcel.ReadInt32();
28     if (size > DPS_MAX_USER_DATA_COUNT) {
29         return DPS_METADATA_INTERNAL_ERROR;
30     }
31 
32     DpsMetadataError ret = DPS_METADATA_OK;
33     for (int32_t i = 0; i < size; i++) {
34         auto key = parcel.ReadString();
35         auto type = static_cast<DpsDataType>(parcel.ReadInt32());
36         switch (type) {
37             case DpsDataType::i32: {
38                 ret = Set(key, type, parcel.ReadInt32());
39                 break;
40             }
41             case DpsDataType::i64: {
42                 ret = Set(key, type, parcel.ReadInt64());
43                 break;
44             }
45             case DpsDataType::f64: {
46                 ret = Set(key, type, parcel.ReadDouble());
47                 break;
48             }
49             case DpsDataType::string: {
50                 ret = Set(key, type, parcel.ReadString());
51                 break;
52             }
53             default: break;
54         }
55 
56         if (ret != DPS_METADATA_OK) {
57             break;
58         }
59     }
60     return ret;
61 }
62 
WriteToParcel(MessageParcel & parcel)63 DpsMetadataError DpsMetadata::WriteToParcel(MessageParcel &parcel)
64 {
65     parcel.WriteInt32(datas.size());
66     for (const auto &[key, data] : datas) {
67         parcel.WriteString(key);
68         parcel.WriteInt32(static_cast<int32_t>(data.type));
69         switch (data.type) {
70             case DpsDataType::i32: {
71                 int32_t i32 = -1;
72                 auto dpVal = std::any_cast<int32_t>(&data.val);
73                 if (dpVal != nullptr) {
74                     i32 = *dpVal;
75                 }
76                 parcel.WriteInt32(i32);
77                 break;
78             }
79             case DpsDataType::i64: {
80                 int64_t i64 = -1;
81                 auto dpVal = std::any_cast<int64_t>(&data.val);
82                 if (dpVal != nullptr) {
83                     i64 = *dpVal;
84                 }
85                 parcel.WriteInt64(i64);
86                 break;
87             }
88             case DpsDataType::f64: {
89                 double f64 = -1;
90                 auto dpVal = std::any_cast<double>(&data.val);
91                 if (dpVal != nullptr) {
92                     f64 = *dpVal;
93                 }
94                 parcel.WriteDouble(f64);
95                 break;
96             }
97             case DpsDataType::string: {
98                 std::string string = "-1";
99                 auto dpVal = std::any_cast<std::string>(&data.val);
100                 if (dpVal != nullptr) {
101                     string = *dpVal;
102                 }
103                 parcel.WriteString(string);
104                 break;
105             }
106             default:
107                 break;
108         }
109     }
110     return DPS_METADATA_OK;
111 }
112 
Get(const std::string & key,int32_t & value) const113 DpsMetadataError DpsMetadata::Get(const std::string &key, int32_t &value) const
114 {
115     return Get<int32_t>(key, DpsDataType::i32, value);
116 }
117 
Get(const std::string & key,int64_t & value) const118 DpsMetadataError DpsMetadata::Get(const std::string &key, int64_t &value) const
119 {
120     return Get<int64_t>(key, DpsDataType::i64, value);
121 }
122 
Get(const std::string & key,double & value) const123 DpsMetadataError DpsMetadata::Get(const std::string &key, double &value) const
124 {
125     return Get<double>(key, DpsDataType::f64, value);
126 }
127 
Get(const std::string & key,std::string & value) const128 DpsMetadataError DpsMetadata::Get(const std::string &key, std::string &value) const
129 {
130     return Get<std::string>(key, DpsDataType::string, value);
131 }
132 
Set(const std::string & key,int32_t value)133 DpsMetadataError DpsMetadata::Set(const std::string &key, int32_t value)
134 {
135     return Set(key, DpsDataType::i32, value);
136 }
137 
Set(const std::string & key,int64_t value)138 DpsMetadataError DpsMetadata::Set(const std::string &key, int64_t value)
139 {
140     return Set(key, DpsDataType::i64, value);
141 }
142 
Set(const std::string & key,double value)143 DpsMetadataError DpsMetadata::Set(const std::string &key, double value)
144 {
145     return Set(key, DpsDataType::f64, value);
146 }
147 
Set(const std::string & key,const std::string & value)148 DpsMetadataError DpsMetadata::Set(const std::string &key, const std::string& value)
149 {
150     return Set(key, DpsDataType::string, value);
151 }
152 
153 template<class T>
Get(const std::string & key,DpsDataType type,T & value) const154 DpsMetadataError DpsMetadata::Get(const std::string &key, DpsDataType type, T &value) const
155 {
156     auto it = datas.find(key);
157     DP_CHECK_RETURN_RET(it == datas.end(), DPS_METADATA_ERROR_NO_ENTRY);
158     DP_CHECK_RETURN_RET(it->second.type != type, DPS_METADATA_ERROR_TYPE_ERROR);
159     auto dpVal = std::any_cast<T>(&it->second.val);
160     DP_CHECK_RETURN_RET(dpVal == nullptr, DPS_METADATA_ERROR_TYPE_ERROR);
161     value = *dpVal;
162     return DPS_METADATA_OK;
163 }
164 
Set(const std::string & key,DpsDataType type,const std::any & val)165 DpsMetadataError DpsMetadata::Set(const std::string &key, DpsDataType type, const std::any& val)
166 {
167     auto it = datas.find(key);
168     DP_CHECK_RETURN_RET(it == datas.end() && datas.size() > DPS_MAX_USER_DATA_COUNT,
169         DPS_METADATA_ERROR_OUT_OF_RANGE);
170     datas[key].type = type;
171     datas[key].val = val;
172     return DPS_METADATA_OK;
173 }
174 
175 } // namespace CameraStandard
176 } // namespace OHOS
177