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 "unified_meta.h"
20 #include "udmf_capi_common.h"
21 #include "udmf_meta.h"
22 #include "udmf_err_code.h"
23 #include "pixel_map.h"
24 #include "pixelmap_native_impl.h"
25
26 using namespace OHOS::UDMF;
27
28 constexpr const int32_t MAX_PARAM_NAME = 1024;
29
GetUdsStrValue(UdsObject * pThis,NdkStructId ndkStructId,const char * paramName)30 static const char* GetUdsStrValue(UdsObject* pThis, NdkStructId ndkStructId, const char* paramName)
31 {
32 if (IsInvalidUdsObjectPtr(pThis, ndkStructId)) {
33 return nullptr;
34 }
35 auto value = pThis->GetUdsValue<std::string>(paramName);
36 return value == nullptr ? nullptr : value->c_str();
37 }
38
GetUdsUint8Value(UdsObject * pThis,const char * paramName,const char * paramNameLen,unsigned char ** data,unsigned int * len)39 static int GetUdsUint8Value(UdsObject* pThis, const char* paramName, const char* paramNameLen, unsigned char** data,
40 unsigned int* len)
41 {
42 auto value = pThis->GetUdsValue<std::vector<uint8_t>>(paramName);
43 if (value == nullptr) {
44 return UDMF_ERR;
45 }
46 auto lengthPtr = pThis->GetUdsValue<int>(paramNameLen);
47 int length;
48 if (lengthPtr == nullptr) {
49 if (value->size() > MAX_GENERAL_ENTRY_SIZE) {
50 LOG_ERROR(UDMF_CAPI, "length invalid. value'size = %{public}zu", value->size());
51 return UDMF_ERR;
52 }
53 length = static_cast<int>(value->size());
54 } else {
55 length = *lengthPtr;
56 }
57 if (length < 0 || length > MAX_GENERAL_ENTRY_SIZE) {
58 LOG_ERROR(UDMF_CAPI, "length invalid! length'size = %{public}d", length);
59 return UDMF_ERR;
60 }
61 *data = value->data();
62 *len = length;
63 return UDMF_E_OK;
64 }
65
IsInvalidUdsObjectPtr(const UdsObject * pThis,int cid)66 bool IsInvalidUdsObjectPtr(const UdsObject *pThis, int cid)
67 {
68 return pThis == nullptr || pThis->cid != cid || pThis->obj == nullptr;
69 }
70
IsInvalidUdsObjectByType(const UdsObject * pThis,const UDType & type)71 bool IsInvalidUdsObjectByType(const UdsObject* pThis, const UDType& type)
72 {
73 switch (type) {
74 case PLAIN_TEXT:
75 return IsInvalidUdsObjectPtr(pThis, UDS_PLAIN_TEXT_STRUCT_ID);
76 case HYPERLINK:
77 return IsInvalidUdsObjectPtr(pThis, UDS_HYPERLINK_STRUCT_ID);
78 case HTML:
79 return IsInvalidUdsObjectPtr(pThis, UDS_HTML_STRUCT_ID);
80 case SYSTEM_DEFINED_APP_ITEM:
81 return IsInvalidUdsObjectPtr(pThis, UDS_APP_ITEM_STRUCT_ID);
82 case FILE_URI:
83 return IsInvalidUdsObjectPtr(pThis, UDS_FILE_URI_STRUCT_ID);
84 case SYSTEM_DEFINED_PIXEL_MAP:
85 return IsInvalidUdsObjectPtr(pThis, UDS_PIXEL_MAP_STRUCT_ID);
86 default:
87 return false;
88 }
89 }
90
UdsObject(const int cid)91 UdsObject::UdsObject(const int cid) : cid(cid) {}
92
OH_UdsPlainText()93 OH_UdsPlainText::OH_UdsPlainText() : UdsObject(NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID) {}
94
OH_UdsHyperlink()95 OH_UdsHyperlink::OH_UdsHyperlink() : UdsObject(NdkStructId::UDS_HYPERLINK_STRUCT_ID) {}
96
OH_UdsHtml()97 OH_UdsHtml::OH_UdsHtml() : UdsObject(NdkStructId::UDS_HTML_STRUCT_ID) {}
98
OH_UdsAppItem()99 OH_UdsAppItem::OH_UdsAppItem() : UdsObject(NdkStructId::UDS_APP_ITEM_STRUCT_ID) {}
100
OH_UdsFileUri()101 OH_UdsFileUri::OH_UdsFileUri() : UdsObject(NdkStructId::UDS_FILE_URI_STRUCT_ID) {}
102
OH_UdsPixelMap()103 OH_UdsPixelMap::OH_UdsPixelMap() : UdsObject(NdkStructId::UDS_PIXEL_MAP_STRUCT_ID) {}
104
OH_UdsArrayBuffer()105 OH_UdsArrayBuffer::OH_UdsArrayBuffer() : UdsObject(NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID) {}
106
OH_UdsContentForm()107 OH_UdsContentForm::OH_UdsContentForm() : UdsObject(NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) {}
108
HasObjectKey(const char * paramName)109 template <typename T> bool UdsObject::HasObjectKey(const char* paramName)
110 {
111 if (strlen(paramName) > MAX_PARAM_NAME) {
112 LOG_ERROR(UDMF_CAPI, "paramName too long! paramName size = %{public}zu.", strlen(paramName));
113 return false;
114 }
115 auto it = obj->value_.find(paramName);
116 if (it == obj->value_.end() || !std::holds_alternative<T>(it->second)) {
117 LOG_ERROR(UDMF_CAPI, "Don't have property paramName");
118 return false;
119 }
120 return true;
121 }
122
123 template<typename T>
GetUdsValue(const char * paramName)124 T* UdsObject::GetUdsValue(const char* paramName)
125 {
126 if (!HasObjectKey<T>(paramName)) {
127 return nullptr;
128 }
129 return std::get_if<T>(&(obj->value_[paramName]));
130 }
131
132 template<typename T>
SetUdsValue(const char * paramName,const T & pramValue)133 int UdsObject::SetUdsValue(const char* paramName, const T &pramValue)
134 {
135 if (!HasObjectKey<T>(paramName)) {
136 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
137 }
138 std::lock_guard<std::mutex> lock(mutex);
139 obj->value_[paramName] = pramValue;
140 return Udmf_ErrCode::UDMF_E_OK;
141 }
142
OH_UdsPlainText_Create()143 OH_UdsPlainText* OH_UdsPlainText_Create()
144 {
145 OH_UdsPlainText* plainText = new (std::nothrow) OH_UdsPlainText();
146 if (plainText == nullptr) {
147 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
148 return nullptr;
149 }
150 plainText->obj = std::make_shared<Object>();
151 plainText->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_PLAIN_TEXT;
152 plainText->obj->value_[CONTENT] = "";
153 plainText->obj->value_[ABSTRACT] = "";
154 return plainText;
155 }
156
OH_UdsPlainText_Destroy(OH_UdsPlainText * pThis)157 void OH_UdsPlainText_Destroy(OH_UdsPlainText* pThis)
158 {
159 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID) {
160 delete pThis;
161 }
162 }
163
OH_UdsPlainText_GetType(OH_UdsPlainText * pThis)164 const char* OH_UdsPlainText_GetType(OH_UdsPlainText* pThis)
165 {
166 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, UNIFORM_DATA_TYPE);
167 }
168
OH_UdsPlainText_GetContent(OH_UdsPlainText * pThis)169 const char* OH_UdsPlainText_GetContent(OH_UdsPlainText* pThis)
170 {
171 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, CONTENT);
172 }
173
OH_UdsPlainText_GetAbstract(OH_UdsPlainText * pThis)174 const char* OH_UdsPlainText_GetAbstract(OH_UdsPlainText* pThis)
175 {
176 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, ABSTRACT);
177 }
178
OH_UdsPlainText_SetContent(OH_UdsPlainText * pThis,const char * content)179 int OH_UdsPlainText_SetContent(OH_UdsPlainText* pThis, const char* content)
180 {
181 if (content == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID)) {
182 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
183 }
184 return pThis->SetUdsValue<std::string>(CONTENT, content);
185 }
186
OH_UdsPlainText_SetAbstract(OH_UdsPlainText * pThis,const char * abstract)187 int OH_UdsPlainText_SetAbstract(OH_UdsPlainText* pThis, const char* abstract)
188 {
189 if (abstract == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID)) {
190 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
191 }
192 return pThis->SetUdsValue<std::string>(ABSTRACT, abstract);
193 }
194
OH_UdsHyperlink_Create()195 OH_UdsHyperlink* OH_UdsHyperlink_Create()
196 {
197 OH_UdsHyperlink* hyperlink = new (std::nothrow) OH_UdsHyperlink();
198 if (hyperlink == nullptr) {
199 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
200 return nullptr;
201 }
202 hyperlink->obj = std::make_shared<Object>();
203 hyperlink->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_HYPERLINK;
204 hyperlink->obj->value_[URL] = "";
205 hyperlink->obj->value_[DESCRIPTION] = "";
206 return hyperlink;
207 }
208
OH_UdsHyperlink_Destroy(OH_UdsHyperlink * pThis)209 void OH_UdsHyperlink_Destroy(OH_UdsHyperlink* pThis)
210 {
211 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_HYPERLINK_STRUCT_ID) {
212 delete pThis;
213 }
214 }
215
OH_UdsHyperlink_GetType(OH_UdsHyperlink * pThis)216 const char* OH_UdsHyperlink_GetType(OH_UdsHyperlink* pThis)
217 {
218 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, UNIFORM_DATA_TYPE);
219 }
220
OH_UdsHyperlink_GetUrl(OH_UdsHyperlink * pThis)221 const char* OH_UdsHyperlink_GetUrl(OH_UdsHyperlink* pThis)
222 {
223 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, URL);
224 }
225
OH_UdsHyperlink_GetDescription(OH_UdsHyperlink * pThis)226 const char* OH_UdsHyperlink_GetDescription(OH_UdsHyperlink* pThis)
227 {
228 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, DESCRIPTION);
229 }
230
OH_UdsHyperlink_SetUrl(OH_UdsHyperlink * pThis,const char * url)231 int OH_UdsHyperlink_SetUrl(OH_UdsHyperlink* pThis, const char* url)
232 {
233 if (url == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID)) {
234 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
235 }
236 return pThis->SetUdsValue<std::string>(URL, url);
237 }
238
OH_UdsHyperlink_SetDescription(OH_UdsHyperlink * pThis,const char * description)239 int OH_UdsHyperlink_SetDescription(OH_UdsHyperlink* pThis, const char* description)
240 {
241 if (description == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID)) {
242 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
243 }
244 return pThis->SetUdsValue<std::string>(DESCRIPTION, description);
245 }
246
OH_UdsHtml_Create()247 OH_UdsHtml* OH_UdsHtml_Create()
248 {
249 OH_UdsHtml* html = new (std::nothrow) OH_UdsHtml();
250 if (html == nullptr) {
251 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
252 return nullptr;
253 }
254 html->obj = std::make_shared<Object>();
255 html->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_HTML;
256 html->obj->value_[HTML_CONTENT] = "";
257 html->obj->value_[PLAIN_CONTENT] = "";
258 return html;
259 }
260
OH_UdsHtml_Destroy(OH_UdsHtml * pThis)261 void OH_UdsHtml_Destroy(OH_UdsHtml* pThis)
262 {
263 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_HTML_STRUCT_ID) {
264 delete pThis;
265 }
266 }
267
OH_UdsHtml_GetType(OH_UdsHtml * pThis)268 const char* OH_UdsHtml_GetType(OH_UdsHtml* pThis)
269 {
270 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, UNIFORM_DATA_TYPE);
271 }
272
OH_UdsHtml_GetContent(OH_UdsHtml * pThis)273 const char* OH_UdsHtml_GetContent(OH_UdsHtml* pThis)
274 {
275 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, HTML_CONTENT);
276 }
277
OH_UdsHtml_GetPlainContent(OH_UdsHtml * pThis)278 const char* OH_UdsHtml_GetPlainContent(OH_UdsHtml* pThis)
279 {
280 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, PLAIN_CONTENT);
281 }
282
OH_UdsHtml_SetContent(OH_UdsHtml * pThis,const char * content)283 int OH_UdsHtml_SetContent(OH_UdsHtml* pThis, const char* content)
284 {
285 if (content == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HTML_STRUCT_ID)) {
286 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
287 }
288 return pThis->SetUdsValue<std::string>(HTML_CONTENT, content);
289 }
290
OH_UdsHtml_SetPlainContent(OH_UdsHtml * pThis,const char * plainContent)291 int OH_UdsHtml_SetPlainContent(OH_UdsHtml* pThis, const char* plainContent)
292 {
293 if (plainContent == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HTML_STRUCT_ID)) {
294 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
295 }
296 return pThis->SetUdsValue<std::string>(PLAIN_CONTENT, plainContent);
297 }
298
OH_UdsAppItem_Create()299 OH_UdsAppItem* OH_UdsAppItem_Create()
300 {
301 OH_UdsAppItem* appItem = new (std::nothrow) OH_UdsAppItem();
302 if (appItem == nullptr) {
303 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
304 return nullptr;
305 }
306 appItem->obj = std::make_shared<Object>();
307 appItem->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_OPENHARMONY_APP_ITEM;
308 appItem->obj->value_[APP_ID] = "";
309 appItem->obj->value_[APP_NAME] = "";
310 appItem->obj->value_[APP_ICON_ID] = "";
311 appItem->obj->value_[APP_LABEL_ID] = "";
312 appItem->obj->value_[BUNDLE_NAME] = "";
313 appItem->obj->value_[ABILITY_NAME] = "";
314 return appItem;
315 }
316
OH_UdsAppItem_Destroy(OH_UdsAppItem * pThis)317 void OH_UdsAppItem_Destroy(OH_UdsAppItem* pThis)
318 {
319 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_APP_ITEM_STRUCT_ID) {
320 delete pThis;
321 }
322 }
323
OH_UdsAppItem_GetType(OH_UdsAppItem * pThis)324 const char* OH_UdsAppItem_GetType(OH_UdsAppItem* pThis)
325 {
326 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, UNIFORM_DATA_TYPE);
327 }
328
OH_UdsAppItem_GetId(OH_UdsAppItem * pThis)329 const char* OH_UdsAppItem_GetId(OH_UdsAppItem* pThis)
330 {
331 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_ID);
332 }
333
OH_UdsAppItem_GetName(OH_UdsAppItem * pThis)334 const char* OH_UdsAppItem_GetName(OH_UdsAppItem* pThis)
335 {
336 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_NAME);
337 }
338
OH_UdsAppItem_GetIconId(OH_UdsAppItem * pThis)339 const char* OH_UdsAppItem_GetIconId(OH_UdsAppItem* pThis)
340 {
341 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_ICON_ID);
342 }
343
OH_UdsAppItem_GetLabelId(OH_UdsAppItem * pThis)344 const char* OH_UdsAppItem_GetLabelId(OH_UdsAppItem* pThis)
345 {
346 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_LABEL_ID);
347 }
348
OH_UdsAppItem_GetBundleName(OH_UdsAppItem * pThis)349 const char* OH_UdsAppItem_GetBundleName(OH_UdsAppItem* pThis)
350 {
351 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, BUNDLE_NAME);
352 }
353
OH_UdsAppItem_GetAbilityName(OH_UdsAppItem * pThis)354 const char* OH_UdsAppItem_GetAbilityName(OH_UdsAppItem* pThis)
355 {
356 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, ABILITY_NAME);
357 }
358
OH_UdsAppItem_SetId(OH_UdsAppItem * pThis,const char * appId)359 int OH_UdsAppItem_SetId(OH_UdsAppItem* pThis, const char* appId)
360 {
361 if (appId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
362 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
363 }
364 return pThis->SetUdsValue<std::string>(APP_ID, appId);
365 }
366
OH_UdsAppItem_SetName(OH_UdsAppItem * pThis,const char * appName)367 int OH_UdsAppItem_SetName(OH_UdsAppItem* pThis, const char* appName)
368 {
369 if (appName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
370 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
371 }
372 return pThis->SetUdsValue<std::string>(APP_NAME, appName);
373 }
374
OH_UdsAppItem_SetIconId(OH_UdsAppItem * pThis,const char * appIconId)375 int OH_UdsAppItem_SetIconId(OH_UdsAppItem* pThis, const char* appIconId)
376 {
377 if (appIconId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
378 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
379 }
380 return pThis->SetUdsValue<std::string>(APP_ICON_ID, appIconId);
381 }
382
OH_UdsAppItem_SetLabelId(OH_UdsAppItem * pThis,const char * appLabelId)383 int OH_UdsAppItem_SetLabelId(OH_UdsAppItem* pThis, const char* appLabelId)
384 {
385 if (appLabelId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
386 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
387 }
388 return pThis->SetUdsValue<std::string>(APP_LABEL_ID, appLabelId);
389 }
390
OH_UdsAppItem_SetBundleName(OH_UdsAppItem * pThis,const char * bundleName)391 int OH_UdsAppItem_SetBundleName(OH_UdsAppItem* pThis, const char* bundleName)
392 {
393 if (bundleName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
394 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
395 }
396 return pThis->SetUdsValue<std::string>(BUNDLE_NAME, bundleName);
397 }
398
OH_UdsAppItem_SetAbilityName(OH_UdsAppItem * pThis,const char * abilityName)399 int OH_UdsAppItem_SetAbilityName(OH_UdsAppItem* pThis, const char* abilityName)
400 {
401 if (abilityName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
402 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
403 }
404 return pThis->SetUdsValue<std::string>(ABILITY_NAME, abilityName);
405 }
406
OH_UdsFileUri_Create()407 OH_UdsFileUri* OH_UdsFileUri_Create()
408 {
409 OH_UdsFileUri* fileUri = new (std::nothrow) OH_UdsFileUri();
410 if (fileUri == nullptr) {
411 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
412 return nullptr;
413 }
414 fileUri->obj = std::make_shared<Object>();
415 fileUri->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_GENERAL_FILE_URI;
416 fileUri->obj->value_[FILE_URI_PARAM] = "";
417 fileUri->obj->value_[FILE_TYPE] = "";
418 return fileUri;
419 }
420
OH_UdsFileUri_Destroy(OH_UdsFileUri * pThis)421 void OH_UdsFileUri_Destroy(OH_UdsFileUri* pThis)
422 {
423 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_FILE_URI_STRUCT_ID) {
424 delete pThis;
425 }
426 }
427
OH_UdsFileUri_GetType(OH_UdsFileUri * pThis)428 const char* OH_UdsFileUri_GetType(OH_UdsFileUri* pThis)
429 {
430 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, UNIFORM_DATA_TYPE);
431 }
432
OH_UdsFileUri_GetFileUri(OH_UdsFileUri * pThis)433 const char* OH_UdsFileUri_GetFileUri(OH_UdsFileUri* pThis)
434 {
435 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, FILE_URI_PARAM);
436 }
437
OH_UdsFileUri_GetFileType(OH_UdsFileUri * pThis)438 const char* OH_UdsFileUri_GetFileType(OH_UdsFileUri* pThis)
439 {
440 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, FILE_TYPE);
441 }
442
OH_UdsFileUri_SetFileUri(OH_UdsFileUri * pThis,const char * fileUri)443 int OH_UdsFileUri_SetFileUri(OH_UdsFileUri* pThis, const char* fileUri)
444 {
445 if (fileUri == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID)) {
446 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
447 }
448 return pThis->SetUdsValue<std::string>(FILE_URI_PARAM, fileUri);
449 }
450
OH_UdsFileUri_SetFileType(OH_UdsFileUri * pThis,const char * fileType)451 int OH_UdsFileUri_SetFileType(OH_UdsFileUri* pThis, const char* fileType)
452 {
453 if (fileType == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID)) {
454 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
455 }
456 return pThis->SetUdsValue<std::string>(FILE_TYPE, fileType);
457 }
458
OH_UdsPixelMap_Create()459 OH_UdsPixelMap* OH_UdsPixelMap_Create()
460 {
461 OH_UdsPixelMap* pixelMap = new (std::nothrow) OH_UdsPixelMap();
462 if (pixelMap == nullptr) {
463 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
464 return nullptr;
465 }
466 pixelMap->obj = std::make_shared<Object>();
467 pixelMap->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_OPENHARMONY_PIXEL_MAP;
468 pixelMap->obj->value_[PIXEL_MAP] = std::make_shared<OHOS::Media::PixelMap>();
469 return pixelMap;
470 }
471
OH_UdsPixelMap_Destroy(OH_UdsPixelMap * pThis)472 void OH_UdsPixelMap_Destroy(OH_UdsPixelMap* pThis)
473 {
474 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_PIXEL_MAP_STRUCT_ID) {
475 delete pThis;
476 }
477 }
478
OH_UdsPixelMap_GetType(OH_UdsPixelMap * pThis)479 const char* OH_UdsPixelMap_GetType(OH_UdsPixelMap* pThis)
480 {
481 return GetUdsStrValue(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID, UNIFORM_DATA_TYPE);
482 }
483
OH_UdsPixelMap_GetPixelMap(OH_UdsPixelMap * pThis,OH_PixelmapNative * pixelmapNative)484 void OH_UdsPixelMap_GetPixelMap(OH_UdsPixelMap* pThis, OH_PixelmapNative* pixelmapNative)
485 {
486 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID)) {
487 return;
488 }
489 if (pixelmapNative == nullptr) {
490 LOG_ERROR(UDMF_CAPI, "pixelmapNative is nullptr.");
491 return;
492 }
493 auto pixelMap = pThis->GetUdsValue<std::shared_ptr<OHOS::Media::PixelMap>>(PIXEL_MAP);
494 if (pixelMap == nullptr) {
495 LOG_ERROR(UDMF_CAPI, "Get pixelMap value is null.");
496 return;
497 }
498 *pixelmapNative = OH_PixelmapNative(*pixelMap);
499 }
500
OH_UdsPixelMap_SetPixelMap(OH_UdsPixelMap * pThis,OH_PixelmapNative * pixelmapNative)501 int OH_UdsPixelMap_SetPixelMap(OH_UdsPixelMap* pThis, OH_PixelmapNative* pixelmapNative)
502 {
503 if (pixelmapNative == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID)) {
504 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
505 }
506 return pThis->SetUdsValue<std::shared_ptr<OHOS::Media::PixelMap>>(PIXEL_MAP, pixelmapNative->GetInnerPixelmap());
507 }
508
OH_UdsArrayBuffer_Create()509 OH_UdsArrayBuffer* OH_UdsArrayBuffer_Create()
510 {
511 auto *buffer = new (std::nothrow) OH_UdsArrayBuffer();
512 if (buffer == nullptr) {
513 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
514 return nullptr;
515 }
516 buffer->obj = std::make_shared<Object>();
517 buffer->obj->value_[UNIFORM_DATA_TYPE] = "";
518 buffer->obj->value_[ARRAY_BUFFER] = std::vector<uint8_t>();
519 buffer->obj->value_[ARRAY_BUFFER_LENGTH] = 0;
520 return buffer;
521 }
522
OH_UdsArrayBuffer_Destroy(OH_UdsArrayBuffer * buffer)523 int OH_UdsArrayBuffer_Destroy(OH_UdsArrayBuffer* buffer)
524 {
525 if (IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID)) {
526 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
527 return UDMF_E_INVALID_PARAM;
528 }
529 delete buffer;
530 return UDMF_E_OK;
531 }
532
OH_UdsArrayBuffer_SetData(OH_UdsArrayBuffer * buffer,unsigned char * data,unsigned int len)533 int OH_UdsArrayBuffer_SetData(OH_UdsArrayBuffer* buffer, unsigned char* data, unsigned int len)
534 {
535 if (data == nullptr || len == 0 || IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID) ||
536 len > MAX_GENERAL_ENTRY_SIZE) {
537 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
538 return UDMF_E_INVALID_PARAM;
539 }
540 std::vector<uint8_t> arrayBuffer(data, data + len);
541 int ret = buffer->SetUdsValue<std::vector<uint8_t>>(ARRAY_BUFFER, arrayBuffer);
542 if (ret != UDMF_E_OK) {
543 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
544 return ret;
545 }
546 ret = buffer->SetUdsValue<int>(ARRAY_BUFFER_LENGTH, static_cast<int>(len));
547 return ret;
548 }
549
OH_UdsArrayBuffer_GetData(OH_UdsArrayBuffer * buffer,unsigned char ** data,unsigned int * len)550 int OH_UdsArrayBuffer_GetData(OH_UdsArrayBuffer* buffer, unsigned char** data, unsigned int* len)
551 {
552 if (IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID)) {
553 return UDMF_E_INVALID_PARAM;
554 }
555 return GetUdsUint8Value(buffer, ARRAY_BUFFER, ARRAY_BUFFER_LENGTH, data, len);
556 }
557
OH_UdsContentForm_Create()558 OH_UdsContentForm* OH_UdsContentForm_Create()
559 {
560 auto contentForm = new (std::nothrow) OH_UdsContentForm();
561 if (contentForm == nullptr) {
562 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
563 return nullptr;
564 }
565 contentForm->obj = std::make_shared<Object>();
566 contentForm->obj->value_[UNIFORM_DATA_TYPE] = UDMF_METE_GENERAL_CONTENT_FORM;
567 contentForm->obj->value_[THUMB_DATA] = std::vector<uint8_t>();
568 contentForm->obj->value_[THUMB_DATA_LENGTH] = 0;
569 contentForm->obj->value_[DESCRIPTION] = "";
570 contentForm->obj->value_[TITLE] = "";
571 contentForm->obj->value_[APP_ICON] = std::vector<uint8_t>();
572 contentForm->obj->value_[APP_ICON_LENGTH] = 0;
573 contentForm->obj->value_[APP_NAME] = "";
574 contentForm->obj->value_[LINK_URL] = "";
575 return contentForm;
576 }
577
OH_UdsContentForm_Destroy(OH_UdsContentForm * pThis)578 void OH_UdsContentForm_Destroy(OH_UdsContentForm* pThis)
579 {
580 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) {
581 delete pThis;
582 }
583 }
584
OH_UdsContentForm_GetType(OH_UdsContentForm * pThis)585 const char* OH_UdsContentForm_GetType(OH_UdsContentForm* pThis)
586 {
587 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, UNIFORM_DATA_TYPE);
588 }
589
OH_UdsContentForm_GetThumbData(OH_UdsContentForm * pThis,unsigned char ** thumbData,unsigned int * len)590 int OH_UdsContentForm_GetThumbData(OH_UdsContentForm* pThis, unsigned char** thumbData, unsigned int* len)
591 {
592 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
593 return UDMF_E_INVALID_PARAM;
594 }
595 return GetUdsUint8Value(pThis, THUMB_DATA, THUMB_DATA_LENGTH, thumbData, len);
596 }
597
OH_UdsContentForm_GetDescription(OH_UdsContentForm * pThis)598 const char* OH_UdsContentForm_GetDescription(OH_UdsContentForm* pThis)
599 {
600 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, DESCRIPTION);
601 }
602
OH_UdsContentForm_GetTitle(OH_UdsContentForm * pThis)603 const char* OH_UdsContentForm_GetTitle(OH_UdsContentForm* pThis)
604 {
605 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, TITLE);
606 }
607
OH_UdsContentForm_GetAppIcon(OH_UdsContentForm * pThis,unsigned char ** appIcon,unsigned int * len)608 int OH_UdsContentForm_GetAppIcon(OH_UdsContentForm* pThis, unsigned char** appIcon, unsigned int* len)
609 {
610 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
611 return UDMF_E_INVALID_PARAM;
612 }
613 return GetUdsUint8Value(pThis, APP_ICON, APP_ICON_LENGTH, appIcon, len);
614 }
615
OH_UdsContentForm_GetAppName(OH_UdsContentForm * pThis)616 const char* OH_UdsContentForm_GetAppName(OH_UdsContentForm* pThis)
617 {
618 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, APP_NAME);
619 }
620
OH_UdsContentForm_GetLinkUri(OH_UdsContentForm * pThis)621 const char* OH_UdsContentForm_GetLinkUri(OH_UdsContentForm* pThis)
622 {
623 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, LINK_URL);
624 }
625
OH_UdsContentForm_SetThumbData(OH_UdsContentForm * pThis,const unsigned char * thumbData,unsigned int len)626 int OH_UdsContentForm_SetThumbData(OH_UdsContentForm* pThis, const unsigned char* thumbData, unsigned int len)
627 {
628 if (thumbData == nullptr || len == 0 || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) ||
629 len > MAX_GENERAL_ENTRY_SIZE) {
630 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
631 return UDMF_E_INVALID_PARAM;
632 }
633 std::vector<uint8_t> data(thumbData, thumbData + len);
634 int ret = pThis->SetUdsValue<std::vector<uint8_t>>(THUMB_DATA, data);
635 if (ret != UDMF_E_OK) {
636 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
637 return ret;
638 }
639 ret = pThis->SetUdsValue<int>(THUMB_DATA_LENGTH, static_cast<int>(len));
640 return ret;
641 }
642
OH_UdsContentForm_SetDescription(OH_UdsContentForm * pThis,const char * description)643 int OH_UdsContentForm_SetDescription(OH_UdsContentForm* pThis, const char* description)
644 {
645 if (description == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
646 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
647 }
648 return pThis->SetUdsValue<std::string>(DESCRIPTION, description);
649 }
650
OH_UdsContentForm_SetTitle(OH_UdsContentForm * pThis,const char * title)651 int OH_UdsContentForm_SetTitle(OH_UdsContentForm* pThis, const char* title)
652 {
653 if (title == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
654 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
655 }
656 return pThis->SetUdsValue<std::string>(TITLE, title);
657 }
658
OH_UdsContentForm_SetAppIcon(OH_UdsContentForm * pThis,const unsigned char * appIcon,unsigned int len)659 int OH_UdsContentForm_SetAppIcon(OH_UdsContentForm* pThis, const unsigned char* appIcon, unsigned int len)
660 {
661 if (appIcon == nullptr || len == 0 || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) ||
662 len > MAX_GENERAL_ENTRY_SIZE) {
663 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
664 return UDMF_E_INVALID_PARAM;
665 }
666 std::vector<uint8_t> data(appIcon, appIcon + len);
667 int ret = pThis->SetUdsValue<std::vector<uint8_t>>(APP_ICON, data);
668 if (ret != UDMF_E_OK) {
669 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
670 return ret;
671 }
672 ret = pThis->SetUdsValue<int>(APP_ICON_LENGTH, static_cast<int>(len));
673 return ret;
674 }
675
OH_UdsContentForm_SetAppName(OH_UdsContentForm * pThis,const char * appName)676 int OH_UdsContentForm_SetAppName(OH_UdsContentForm* pThis, const char* appName)
677 {
678 if (appName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
679 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
680 }
681 return pThis->SetUdsValue<std::string>(APP_NAME, appName);
682 }
683
OH_UdsContentForm_SetLinkUri(OH_UdsContentForm * pThis,const char * linkUri)684 int OH_UdsContentForm_SetLinkUri(OH_UdsContentForm* pThis, const char* linkUri)
685 {
686 if (linkUri == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
687 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
688 }
689 return pThis->SetUdsValue<std::string>(LINK_URL, linkUri);
690 }