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 "map_data_sequenceable.h"
17 #include "hdi_log.h"
18 #include <message_parcel.h>
19 namespace OHOS {
20 namespace HDI {
21 namespace Camera {
22 namespace V1_0 {
23 constexpr int32_t BUFFER_DATA_MAGIC = 0x4567;
24 constexpr int32_t BUFFER_MAX_USER_DATA_COUNT = 1000;
25
26 enum ErrorCode : int32_t {
27 ERROR_OK = 0,
28 ERROR_INTERNAL = -1,
29 ERROR_NO_ENTRY = -2,
30 ERROR_TYPE_ERROR = -3,
31 ERROR_OUT_OF_RANGE = -4,
32 };
33
34 #define SET_DATA_FROM_POINTER(data, pointer) do { \
35 if ((pointer) != nullptr) { \
36 (data) = *(pointer); \
37 } \
38 } while (0)
39
Unmarshalling(Parcel & parcel)40 sptr<MapDataSequenceable> MapDataSequenceable::Unmarshalling(Parcel &parcel)
41 {
42 int32_t magic;
43 if (parcel.ReadInt32(magic) == false || magic != BUFFER_DATA_MAGIC) {
44 HDI_CAMERA_LOGW("read failed, magic is error");
45 return nullptr;
46 }
47
48 int32_t size = parcel.ReadInt32();
49 if (size < 0 || size > BUFFER_MAX_USER_DATA_COUNT) {
50 HDI_CAMERA_LOGE("invalid size: %{public}d obtained from Parcel", size);
51 return nullptr;
52 }
53 sptr<MapDataSequenceable> sequenceData(new MapDataSequenceable());
54
55 int32_t ret = ERROR_OK;
56 for (int32_t i = 0; i < size; i++) {
57 auto key = parcel.ReadString();
58 auto type = static_cast<MapDataType>(parcel.ReadInt32());
59 switch (type) {
60 case MapDataType::I32: {
61 ret = sequenceData->Set(key, type, parcel.ReadInt32());
62 break;
63 }
64 case MapDataType::I64: {
65 ret = sequenceData->Set(key, type, parcel.ReadInt64());
66 break;
67 }
68 case MapDataType::F64: {
69 ret = sequenceData->Set(key, type, parcel.ReadDouble());
70 break;
71 }
72 case MapDataType::STRING: {
73 ret = sequenceData->Set(key, type, parcel.ReadString());
74 break;
75 }
76 case MapDataType::U32: {
77 ret = sequenceData->Set(key, type, parcel.ReadUint32());
78 break;
79 }
80 default: break;
81 }
82
83 if (ret != ERROR_OK) {
84 HDI_CAMERA_LOGE("Set extra data failed, return %{public}d", ret);
85 return nullptr;
86 }
87 }
88 return sequenceData;
89 }
90
Marshalling(Parcel & parcel) const91 bool MapDataSequenceable::Marshalling(Parcel &parcel) const
92 {
93 OHOS::MessageParcel &dataParcel = static_cast<OHOS::MessageParcel &>(parcel);
94 std::lock_guard<std::mutex> lockGuard(mtx_);
95 dataParcel.WriteInt32(BUFFER_DATA_MAGIC);
96 dataParcel.WriteInt32(datas_.size());
97 for (const auto &[key, data] : datas_) {
98 dataParcel.WriteString(key);
99 dataParcel.WriteInt32(static_cast<int32_t>(data.type));
100 switch (data.type) {
101 case MapDataType::I32: {
102 int32_t i32 = -1;
103 auto pVal = std::any_cast<int32_t>(&data.val);
104 SET_DATA_FROM_POINTER(i32, pVal);
105 dataParcel.WriteInt32(i32);
106 break;
107 }
108 case MapDataType::I64: {
109 int64_t i64 = -1;
110 auto pVal = std::any_cast<int64_t>(&data.val);
111 SET_DATA_FROM_POINTER(i64, pVal);
112 dataParcel.WriteInt64(i64);
113 break;
114 }
115 case MapDataType::F64: {
116 double f64 = -1;
117 auto pVal = std::any_cast<double>(&data.val);
118 SET_DATA_FROM_POINTER(f64, pVal);
119 dataParcel.WriteDouble(f64);
120 break;
121 }
122 case MapDataType::STRING: {
123 std::string string = "-1";
124 auto pVal = std::any_cast<std::string>(&data.val);
125 SET_DATA_FROM_POINTER(string, pVal);
126 dataParcel.WriteString(string);
127 break;
128 }
129 case MapDataType::U32: {
130 uint32_t u32 = 0;
131 auto pVal = std::any_cast<uint32_t>(&data.val);
132 SET_DATA_FROM_POINTER(u32, pVal);
133 dataParcel.WriteUint32(u32);
134 break;
135 }
136 default:
137 break;
138 }
139 }
140 return true;
141 }
142
Get(const std::string & key,int32_t & value) const143 int32_t MapDataSequenceable::Get(const std::string &key, int32_t &value) const
144 {
145 return Get<int32_t>(key, MapDataType::I32, value);
146 }
147
Get(const std::string & key,uint32_t & value) const148 int32_t MapDataSequenceable::Get(const std::string &key, uint32_t &value) const
149 {
150 return Get<uint32_t>(key, MapDataType::U32, value);
151 }
152
Get(const std::string & key,int64_t & value) const153 int32_t MapDataSequenceable::Get(const std::string &key, int64_t &value) const
154 {
155 return Get<int64_t>(key, MapDataType::I64, value);
156 }
157
Get(const std::string & key,double & value) const158 int32_t MapDataSequenceable::Get(const std::string &key, double &value) const
159 {
160 return Get<double>(key, MapDataType::F64, value);
161 }
162
Get(const std::string & key,std::string & value) const163 int32_t MapDataSequenceable::Get(const std::string &key, std::string &value) const
164 {
165 return Get<std::string>(key, MapDataType::STRING, value);
166 }
167
Set(const std::string & key,int32_t value)168 int32_t MapDataSequenceable::Set(const std::string &key, int32_t value)
169 {
170 return Set(key, MapDataType::I32, value);
171 }
172
Set(const std::string & key,uint32_t value)173 int32_t MapDataSequenceable::Set(const std::string &key, uint32_t value)
174 {
175 return Set(key, MapDataType::U32, value);
176 }
177
Set(const std::string & key,int64_t value)178 int32_t MapDataSequenceable::Set(const std::string &key, int64_t value)
179 {
180 return Set(key, MapDataType::I64, value);
181 }
182
Set(const std::string & key,double value)183 int32_t MapDataSequenceable::Set(const std::string &key, double value)
184 {
185 return Set(key, MapDataType::F64, value);
186 }
187
Set(const std::string & key,const std::string & value)188 int32_t MapDataSequenceable::Set(const std::string &key, const std::string& value)
189 {
190 return Set(key, MapDataType::STRING, value);
191 }
192
193 template<class T>
Get(const std::string & key,MapDataType type,T & value) const194 int32_t MapDataSequenceable::Get(const std::string &key, MapDataType type, T &value) const
195 {
196 std::lock_guard<std::mutex> lockGuard(mtx_);
197 auto it = datas_.find(key);
198 if (it == datas_.end()) {
199 return ERROR_NO_ENTRY;
200 }
201 if (it->second.type != type) {
202 return ERROR_TYPE_ERROR;
203 }
204 auto pVal = std::any_cast<T>(&it->second.val);
205 if (pVal == nullptr) {
206 return ERROR_TYPE_ERROR;
207 }
208 value = *pVal;
209 return ERROR_OK;
210 }
211
Set(const std::string & key,MapDataType type,const std::any & val)212 int32_t MapDataSequenceable::Set(const std::string &key, MapDataType type, const std::any& val)
213 {
214 std::lock_guard<std::mutex> lockGuard(mtx_);
215 auto it = datas_.find(key);
216 if (it == datas_.end() && datas_.size() > BUFFER_MAX_USER_DATA_COUNT) {
217 HDI_CAMERA_LOGW("SurfaceBuffer has too many extra data, cannot save one more!!!");
218 return ERROR_OUT_OF_RANGE;
219 }
220 datas_[key].type = type;
221 datas_[key].val = val;
222 return ERROR_OK;
223 }
224
225 }
226 }
227 }
228 }