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