1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "Uds"
16
17 #include "uds.h"
18 #include "logger.h"
19 #include "utd_common.h"
20 #include "unified_meta.h"
21 #include "unified_record.h"
22 #include "udmf_capi_common.h"
23 #include "udmf_meta.h"
24 #include "udmf_err_code.h"
25 #include "pixel_map.h"
26 #include "pixelmap_native_impl.h"
27
28 using namespace OHOS::UDMF;
29
GetUdsStrValue(UdsObject * pThis,NdkStructId ndkStructId,const char * pramName)30 static const char* GetUdsStrValue(UdsObject* pThis, NdkStructId ndkStructId, const char* pramName)
31 {
32 if (IsInvalidUdsObjectPtr(pThis, ndkStructId)) {
33 return nullptr;
34 }
35 auto value = pThis->GetUdsValue<std::string>(pramName);
36 return value == nullptr ? nullptr : value->c_str();
37 }
38
IsInvalidUdsObjectPtr(const UdsObject * pThis,int cid)39 bool IsInvalidUdsObjectPtr(const UdsObject* pThis, int cid)
40 {
41 return pThis == nullptr || pThis->cid != cid || pThis->obj == nullptr;
42 }
43
IsInvalidUdsObjectByType(const UdsObject * pThis,const UDType & type)44 bool IsInvalidUdsObjectByType(const UdsObject* pThis, const UDType& type)
45 {
46 switch (type) {
47 case PLAIN_TEXT:
48 return IsInvalidUdsObjectPtr(pThis, UDS_PLAIN_TEXT_STRUCT_ID);
49 case HYPERLINK:
50 return IsInvalidUdsObjectPtr(pThis, UDS_HYPERLINK_STRUCT_ID);
51 case HTML:
52 return IsInvalidUdsObjectPtr(pThis, UDS_HTML_STRUCT_ID);
53 case SYSTEM_DEFINED_APP_ITEM:
54 return IsInvalidUdsObjectPtr(pThis, UDS_APP_ITEM_STRUCT_ID);
55 case FILE_URI:
56 return IsInvalidUdsObjectPtr(pThis, UDS_FILE_URI_STRUCT_ID);
57 case SYSTEM_DEFINED_PIXEL_MAP:
58 return IsInvalidUdsObjectPtr(pThis, UDS_PIXEL_MAP_STRUCT_ID);
59 default:
60 return false;
61 }
62 }
63
UdsObject(const int cid)64 UdsObject::UdsObject(const int cid) : cid(cid) {}
65
OH_UdsPlainText()66 OH_UdsPlainText::OH_UdsPlainText() : UdsObject(NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID) {}
67
OH_UdsHyperlink()68 OH_UdsHyperlink::OH_UdsHyperlink() : UdsObject(NdkStructId::UDS_HYPERLINK_STRUCT_ID) {}
69
OH_UdsHtml()70 OH_UdsHtml::OH_UdsHtml() : UdsObject(NdkStructId::UDS_HTML_STRUCT_ID) {}
71
OH_UdsAppItem()72 OH_UdsAppItem::OH_UdsAppItem() : UdsObject(NdkStructId::UDS_APP_ITEM_STRUCT_ID) {}
73
OH_UdsFileUri()74 OH_UdsFileUri::OH_UdsFileUri() : UdsObject(NdkStructId::UDS_FILE_URI_STRUCT_ID) {}
75
OH_UdsPixelMap()76 OH_UdsPixelMap::OH_UdsPixelMap() : UdsObject(NdkStructId::UDS_PIXEL_MAP_STRUCT_ID) {}
77
OH_UdsArrayBuffer()78 OH_UdsArrayBuffer::OH_UdsArrayBuffer() : UdsObject(NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID) {}
79
80 template<typename T>
HasObjectKey(const char * paramName)81 bool UdsObject::HasObjectKey(const char* paramName)
82 {
83 auto it = obj->value_.find(paramName);
84 if (it == obj->value_.end() || !std::holds_alternative<T>(it->second)) {
85 LOG_ERROR(UDMF_CAPI, "Don't have property %{public}s.", paramName);
86 return false;
87 }
88 return true;
89 }
90
91 template<typename T>
GetUdsValue(const char * paramName)92 T* UdsObject::GetUdsValue(const char* paramName)
93 {
94 if (!HasObjectKey<T>(paramName)) {
95 return nullptr;
96 }
97 return std::get_if<T>(&(obj->value_[paramName]));
98 }
99
100 template<typename T>
SetUdsValue(const char * paramName,const T pramValue)101 int UdsObject::SetUdsValue(const char* paramName, const T pramValue)
102 {
103 if (!HasObjectKey<T>(paramName)) {
104 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
105 }
106 std::lock_guard<std::mutex> lock(mutex);
107 obj->value_[paramName] = pramValue;
108 return Udmf_ErrCode::UDMF_E_OK;
109 }
110
OH_UdsPlainText_Create()111 OH_UdsPlainText* OH_UdsPlainText_Create()
112 {
113 OH_UdsPlainText* plainText = new (std::nothrow) OH_UdsPlainText();
114 if (plainText == nullptr) {
115 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
116 return nullptr;
117 }
118 plainText->obj = std::make_shared<Object>();
119 plainText->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_PLAIN_TEXT;
120 plainText->obj->value_[CONTENT] = "";
121 plainText->obj->value_[ABSTRACT] = "";
122 return plainText;
123 }
124
OH_UdsPlainText_Destroy(OH_UdsPlainText * pThis)125 void OH_UdsPlainText_Destroy(OH_UdsPlainText* pThis)
126 {
127 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID) {
128 delete pThis;
129 }
130 }
131
OH_UdsPlainText_GetType(OH_UdsPlainText * pThis)132 const char* OH_UdsPlainText_GetType(OH_UdsPlainText* pThis)
133 {
134 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, UNIFORM_DATA_TYPE);
135 }
136
OH_UdsPlainText_GetContent(OH_UdsPlainText * pThis)137 const char* OH_UdsPlainText_GetContent(OH_UdsPlainText* pThis)
138 {
139 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, CONTENT);
140 }
141
OH_UdsPlainText_GetAbstract(OH_UdsPlainText * pThis)142 const char* OH_UdsPlainText_GetAbstract(OH_UdsPlainText* pThis)
143 {
144 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, ABSTRACT);
145 }
146
OH_UdsPlainText_SetContent(OH_UdsPlainText * pThis,const char * content)147 int OH_UdsPlainText_SetContent(OH_UdsPlainText* pThis, const char* content)
148 {
149 if (content == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID)) {
150 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
151 }
152 return pThis->SetUdsValue<std::string>(CONTENT, content);
153 }
154
OH_UdsPlainText_SetAbstract(OH_UdsPlainText * pThis,const char * abstract)155 int OH_UdsPlainText_SetAbstract(OH_UdsPlainText* pThis, const char* abstract)
156 {
157 if (abstract == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID)) {
158 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
159 }
160 return pThis->SetUdsValue<std::string>(ABSTRACT, abstract);
161 }
162
OH_UdsHyperlink_Create()163 OH_UdsHyperlink* OH_UdsHyperlink_Create()
164 {
165 OH_UdsHyperlink* hyperlink = new (std::nothrow) OH_UdsHyperlink();
166 if (hyperlink == nullptr) {
167 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
168 return nullptr;
169 }
170 hyperlink->obj = std::make_shared<Object>();
171 hyperlink->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_HYPERLINK;
172 hyperlink->obj->value_[URL] = "";
173 hyperlink->obj->value_[DESCRIPTION] = "";
174 return hyperlink;
175 }
176
OH_UdsHyperlink_Destroy(OH_UdsHyperlink * pThis)177 void OH_UdsHyperlink_Destroy(OH_UdsHyperlink* pThis)
178 {
179 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_HYPERLINK_STRUCT_ID) {
180 delete pThis;
181 }
182 }
183
OH_UdsHyperlink_GetType(OH_UdsHyperlink * pThis)184 const char* OH_UdsHyperlink_GetType(OH_UdsHyperlink* pThis)
185 {
186 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, UNIFORM_DATA_TYPE);
187 }
188
OH_UdsHyperlink_GetUrl(OH_UdsHyperlink * pThis)189 const char* OH_UdsHyperlink_GetUrl(OH_UdsHyperlink* pThis)
190 {
191 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, URL);
192 }
193
OH_UdsHyperlink_GetDescription(OH_UdsHyperlink * pThis)194 const char* OH_UdsHyperlink_GetDescription(OH_UdsHyperlink* pThis)
195 {
196 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, DESCRIPTION);
197 }
198
OH_UdsHyperlink_SetUrl(OH_UdsHyperlink * pThis,const char * url)199 int OH_UdsHyperlink_SetUrl(OH_UdsHyperlink* pThis, const char* url)
200 {
201 if (url == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID)) {
202 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
203 }
204 return pThis->SetUdsValue<std::string>(URL, url);
205 }
206
OH_UdsHyperlink_SetDescription(OH_UdsHyperlink * pThis,const char * description)207 int OH_UdsHyperlink_SetDescription(OH_UdsHyperlink* pThis, const char* description)
208 {
209 if (description == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID)) {
210 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
211 }
212 return pThis->SetUdsValue<std::string>(DESCRIPTION, description);
213 }
214
OH_UdsHtml_Create()215 OH_UdsHtml* OH_UdsHtml_Create()
216 {
217 OH_UdsHtml* html = new (std::nothrow) OH_UdsHtml();
218 if (html == nullptr) {
219 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
220 return nullptr;
221 }
222 html->obj = std::make_shared<Object>();
223 html->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_HTML;
224 html->obj->value_[HTML_CONTENT] = "";
225 html->obj->value_[PLAIN_CONTENT] = "";
226 return html;
227 }
228
OH_UdsHtml_Destroy(OH_UdsHtml * pThis)229 void OH_UdsHtml_Destroy(OH_UdsHtml* pThis)
230 {
231 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_HTML_STRUCT_ID) {
232 delete pThis;
233 }
234 }
235
OH_UdsHtml_GetType(OH_UdsHtml * pThis)236 const char* OH_UdsHtml_GetType(OH_UdsHtml* pThis)
237 {
238 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, UNIFORM_DATA_TYPE);
239 }
240
OH_UdsHtml_GetContent(OH_UdsHtml * pThis)241 const char* OH_UdsHtml_GetContent(OH_UdsHtml* pThis)
242 {
243 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, HTML_CONTENT);
244 }
245
OH_UdsHtml_GetPlainContent(OH_UdsHtml * pThis)246 const char* OH_UdsHtml_GetPlainContent(OH_UdsHtml* pThis)
247 {
248 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, PLAIN_CONTENT);
249 }
250
OH_UdsHtml_SetContent(OH_UdsHtml * pThis,const char * content)251 int OH_UdsHtml_SetContent(OH_UdsHtml* pThis, const char* content)
252 {
253 if (content == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HTML_STRUCT_ID)) {
254 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
255 }
256 return pThis->SetUdsValue<std::string>(HTML_CONTENT, content);
257 }
258
OH_UdsHtml_SetPlainContent(OH_UdsHtml * pThis,const char * plainContent)259 int OH_UdsHtml_SetPlainContent(OH_UdsHtml* pThis, const char* plainContent)
260 {
261 if (plainContent == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HTML_STRUCT_ID)) {
262 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
263 }
264 return pThis->SetUdsValue<std::string>(PLAIN_CONTENT, plainContent);
265 }
266
OH_UdsAppItem_Create()267 OH_UdsAppItem* OH_UdsAppItem_Create()
268 {
269 OH_UdsAppItem* appItem = new (std::nothrow) OH_UdsAppItem();
270 if (appItem == nullptr) {
271 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
272 return nullptr;
273 }
274 appItem->obj = std::make_shared<Object>();
275 appItem->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_OPENHARMONY_APP_ITEM;
276 appItem->obj->value_[APP_ID] = "";
277 appItem->obj->value_[APP_NAME] = "";
278 appItem->obj->value_[APP_ICON_ID] = "";
279 appItem->obj->value_[APP_LABEL_ID] = "";
280 appItem->obj->value_[BUNDLE_NAME] = "";
281 appItem->obj->value_[ABILITY_NAME] = "";
282 return appItem;
283 }
284
OH_UdsAppItem_Destroy(OH_UdsAppItem * pThis)285 void OH_UdsAppItem_Destroy(OH_UdsAppItem* pThis)
286 {
287 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_APP_ITEM_STRUCT_ID) {
288 delete pThis;
289 }
290 }
291
OH_UdsAppItem_GetType(OH_UdsAppItem * pThis)292 const char* OH_UdsAppItem_GetType(OH_UdsAppItem* pThis)
293 {
294 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, UNIFORM_DATA_TYPE);
295 }
296
OH_UdsAppItem_GetId(OH_UdsAppItem * pThis)297 const char* OH_UdsAppItem_GetId(OH_UdsAppItem* pThis)
298 {
299 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_ID);
300 }
301
OH_UdsAppItem_GetName(OH_UdsAppItem * pThis)302 const char* OH_UdsAppItem_GetName(OH_UdsAppItem* pThis)
303 {
304 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_NAME);
305 }
306
OH_UdsAppItem_GetIconId(OH_UdsAppItem * pThis)307 const char* OH_UdsAppItem_GetIconId(OH_UdsAppItem* pThis)
308 {
309 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_ICON_ID);
310 }
311
OH_UdsAppItem_GetLabelId(OH_UdsAppItem * pThis)312 const char* OH_UdsAppItem_GetLabelId(OH_UdsAppItem* pThis)
313 {
314 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_LABEL_ID);
315 }
316
OH_UdsAppItem_GetBundleName(OH_UdsAppItem * pThis)317 const char* OH_UdsAppItem_GetBundleName(OH_UdsAppItem* pThis)
318 {
319 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, BUNDLE_NAME);
320 }
321
OH_UdsAppItem_GetAbilityName(OH_UdsAppItem * pThis)322 const char* OH_UdsAppItem_GetAbilityName(OH_UdsAppItem* pThis)
323 {
324 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, ABILITY_NAME);
325 }
326
OH_UdsAppItem_SetId(OH_UdsAppItem * pThis,const char * appId)327 int OH_UdsAppItem_SetId(OH_UdsAppItem* pThis, const char* appId)
328 {
329 if (appId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
330 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
331 }
332 return pThis->SetUdsValue<std::string>(APP_ID, appId);
333 }
334
OH_UdsAppItem_SetName(OH_UdsAppItem * pThis,const char * appName)335 int OH_UdsAppItem_SetName(OH_UdsAppItem* pThis, const char* appName)
336 {
337 if (appName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
338 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
339 }
340 return pThis->SetUdsValue<std::string>(APP_NAME, appName);
341 }
342
OH_UdsAppItem_SetIconId(OH_UdsAppItem * pThis,const char * appIconId)343 int OH_UdsAppItem_SetIconId(OH_UdsAppItem* pThis, const char* appIconId)
344 {
345 if (appIconId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
346 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
347 }
348 return pThis->SetUdsValue<std::string>(APP_ICON_ID, appIconId);
349 }
350
OH_UdsAppItem_SetLabelId(OH_UdsAppItem * pThis,const char * appLabelId)351 int OH_UdsAppItem_SetLabelId(OH_UdsAppItem* pThis, const char* appLabelId)
352 {
353 if (appLabelId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
354 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
355 }
356 return pThis->SetUdsValue<std::string>(APP_LABEL_ID, appLabelId);
357 }
358
OH_UdsAppItem_SetBundleName(OH_UdsAppItem * pThis,const char * bundleName)359 int OH_UdsAppItem_SetBundleName(OH_UdsAppItem* pThis, const char* bundleName)
360 {
361 if (bundleName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
362 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
363 }
364 return pThis->SetUdsValue<std::string>(BUNDLE_NAME, bundleName);
365 }
366
OH_UdsAppItem_SetAbilityName(OH_UdsAppItem * pThis,const char * abilityName)367 int OH_UdsAppItem_SetAbilityName(OH_UdsAppItem* pThis, const char* abilityName)
368 {
369 if (abilityName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
370 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
371 }
372 return pThis->SetUdsValue<std::string>(ABILITY_NAME, abilityName);
373 }
374
OH_UdsFileUri_Create()375 OH_UdsFileUri* OH_UdsFileUri_Create()
376 {
377 OH_UdsFileUri* fileUri = new (std::nothrow) OH_UdsFileUri();
378 if (fileUri == nullptr) {
379 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
380 return nullptr;
381 }
382 fileUri->obj = std::make_shared<Object>();
383 fileUri->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_GENERAL_FILE_URI;
384 fileUri->obj->value_[FILE_URI_PARAM] = "";
385 fileUri->obj->value_[FILE_TYPE] = "";
386 return fileUri;
387 }
388
OH_UdsFileUri_Destroy(OH_UdsFileUri * pThis)389 void OH_UdsFileUri_Destroy(OH_UdsFileUri* pThis)
390 {
391 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_FILE_URI_STRUCT_ID) {
392 delete pThis;
393 }
394 }
395
OH_UdsFileUri_GetType(OH_UdsFileUri * pThis)396 const char* OH_UdsFileUri_GetType(OH_UdsFileUri* pThis)
397 {
398 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, UNIFORM_DATA_TYPE);
399 }
400
OH_UdsFileUri_GetFileUri(OH_UdsFileUri * pThis)401 const char* OH_UdsFileUri_GetFileUri(OH_UdsFileUri* pThis)
402 {
403 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, FILE_URI_PARAM);
404 }
405
OH_UdsFileUri_GetFileType(OH_UdsFileUri * pThis)406 const char* OH_UdsFileUri_GetFileType(OH_UdsFileUri* pThis)
407 {
408 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, FILE_TYPE);
409 }
410
OH_UdsFileUri_SetFileUri(OH_UdsFileUri * pThis,const char * fileUri)411 int OH_UdsFileUri_SetFileUri(OH_UdsFileUri* pThis, const char* fileUri)
412 {
413 if (fileUri == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID)) {
414 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
415 }
416 return pThis->SetUdsValue<std::string>(FILE_URI_PARAM, fileUri);
417 }
418
OH_UdsFileUri_SetFileType(OH_UdsFileUri * pThis,const char * fileType)419 int OH_UdsFileUri_SetFileType(OH_UdsFileUri* pThis, const char* fileType)
420 {
421 if (fileType == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID)) {
422 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
423 }
424 return pThis->SetUdsValue<std::string>(FILE_TYPE, fileType);
425 }
426
OH_UdsPixelMap_Create()427 OH_UdsPixelMap* OH_UdsPixelMap_Create()
428 {
429 OH_UdsPixelMap* pixelMap = new (std::nothrow) OH_UdsPixelMap();
430 if (pixelMap == nullptr) {
431 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
432 return nullptr;
433 }
434 pixelMap->obj = std::make_shared<Object>();
435 pixelMap->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_OPENHARMONY_PIXEL_MAP;
436 pixelMap->obj->value_[PIXEL_MAP] = std::make_shared<OHOS::Media::PixelMap>();
437 return pixelMap;
438 }
439
OH_UdsPixelMap_Destroy(OH_UdsPixelMap * pThis)440 void OH_UdsPixelMap_Destroy(OH_UdsPixelMap* pThis)
441 {
442 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_PIXEL_MAP_STRUCT_ID) {
443 delete pThis;
444 }
445 }
446
OH_UdsPixelMap_GetType(OH_UdsPixelMap * pThis)447 const char* OH_UdsPixelMap_GetType(OH_UdsPixelMap* pThis)
448 {
449 return GetUdsStrValue(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID, UNIFORM_DATA_TYPE);
450 }
451
OH_UdsPixelMap_GetPixelMap(OH_UdsPixelMap * pThis,OH_PixelmapNative * pixelmapNative)452 void OH_UdsPixelMap_GetPixelMap(OH_UdsPixelMap* pThis, OH_PixelmapNative* pixelmapNative)
453 {
454 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID)) {
455 return;
456 }
457 auto pixelMap = pThis->GetUdsValue<std::shared_ptr<OHOS::Media::PixelMap>>(PIXEL_MAP);
458 if (pixelMap != nullptr) {
459 *pixelmapNative = OH_PixelmapNative(*pixelMap);
460 }
461 }
462
OH_UdsPixelMap_SetPixelMap(OH_UdsPixelMap * pThis,OH_PixelmapNative * pixelmapNative)463 int OH_UdsPixelMap_SetPixelMap(OH_UdsPixelMap* pThis, OH_PixelmapNative* pixelmapNative)
464 {
465 if (pixelmapNative == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID)) {
466 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
467 }
468 return pThis->SetUdsValue<std::shared_ptr<OHOS::Media::PixelMap>>(PIXEL_MAP, pixelmapNative->GetInnerPixelmap());
469 }
470
OH_UdsArrayBuffer_Create()471 OH_UdsArrayBuffer* OH_UdsArrayBuffer_Create()
472 {
473 auto *buffer = new (std::nothrow) OH_UdsArrayBuffer();
474 if (buffer == nullptr) {
475 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
476 return nullptr;
477 }
478 buffer->obj = std::make_shared<Object>();
479 buffer->obj->value_[UNIFORM_DATA_TYPE] = "";
480 buffer->obj->value_[ARRAY_BUFFER] = std::vector<uint8_t>();
481 buffer->obj->value_[ARRAY_BUFFER_LENGTH] = 0;
482 return buffer;
483 }
484
OH_UdsArrayBuffer_Destroy(OH_UdsArrayBuffer * buffer)485 int OH_UdsArrayBuffer_Destroy(OH_UdsArrayBuffer* buffer)
486 {
487 if (IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID)) {
488 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
489 return UDMF_E_INVALID_PARAM;
490 }
491 delete buffer;
492 return UDMF_E_OK;
493 }
494
OH_UdsArrayBuffer_SetData(OH_UdsArrayBuffer * buffer,unsigned char * data,unsigned int len)495 int OH_UdsArrayBuffer_SetData(OH_UdsArrayBuffer* buffer, unsigned char* data, unsigned int len)
496 {
497 if (data == nullptr || len == 0 || IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID) ||
498 len > MAX_GENERAL_ENTRY_SIZE) {
499 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
500 return UDMF_E_INVALID_PARAM;
501 }
502 std::vector<uint8_t> arrayBuffer(data, data + len);
503 int ret = buffer->SetUdsValue<std::vector<uint8_t>>(ARRAY_BUFFER, arrayBuffer);
504 if (ret != UDMF_E_OK) {
505 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
506 return ret;
507 }
508 ret = buffer->SetUdsValue<int>(ARRAY_BUFFER_LENGTH, static_cast<int>(len));
509 return ret;
510 }
511
OH_UdsArrayBuffer_GetData(OH_UdsArrayBuffer * buffer,unsigned char ** data,unsigned int * len)512 int OH_UdsArrayBuffer_GetData(OH_UdsArrayBuffer* buffer, unsigned char** data, unsigned int* len)
513 {
514 if (buffer == nullptr || IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID)) {
515 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
516 return UDMF_E_INVALID_PARAM;
517 }
518 const auto arrayBuffer = buffer->GetUdsValue<std::vector<uint8_t>>(ARRAY_BUFFER);
519 if (arrayBuffer == nullptr || arrayBuffer->empty()) {
520 return UDMF_ERR;
521 }
522 const auto length = buffer->GetUdsValue<int>(ARRAY_BUFFER_LENGTH);
523 if (length == nullptr || *length <= 0 || *length > MAX_GENERAL_ENTRY_SIZE) {
524 return UDMF_ERR;
525 }
526
527 *data = arrayBuffer->data();
528 *len = *length;
529 return UDMF_E_OK;
530 }