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
16 #include "cj_element_name_ffi.h"
17
18 #include "cj_utils_ffi.h"
19 #include "element_name.h"
20 #include "hilog_tag_wrapper.h"
21
22 using OHOS::AppExecFwk::ElementName;
23
24 extern "C" {
FFICJElementNameCreateWithContent(char * deviceId,char * bundleName,char * abilityName,char * moduleName)25 ElementNameHandle FFICJElementNameCreateWithContent(
26 char* deviceId, char* bundleName, char* abilityName, char* moduleName)
27 {
28 return new ElementName(deviceId, bundleName, abilityName, moduleName);
29 }
30
FFICJElementNameDelete(ElementNameHandle elementNameHandle)31 void FFICJElementNameDelete(ElementNameHandle elementNameHandle)
32 {
33 if (elementNameHandle == nullptr) {
34 TAG_LOGE(AAFwkTag::CONTEXT, "null elementNameHandle");
35 return;
36 }
37 auto actualElementName = reinterpret_cast<ElementName*>(elementNameHandle);
38 delete actualElementName;
39 actualElementName = nullptr;
40 }
41
42 // The return variable needs to delete in Cangjie by `FFICJElementNameParamsDelete`.
FFICJElementNameGetElementNameInfo(ElementNameHandle elementNameHandle)43 ElementNameParams* FFICJElementNameGetElementNameInfo(ElementNameHandle elementNameHandle)
44 {
45 ElementNameParams* buffer = static_cast<ElementNameParams*>(malloc(sizeof(ElementNameParams)));
46 if (buffer == nullptr) {
47 TAG_LOGE(AAFwkTag::CONTEXT, "null buffer");
48 return nullptr;
49 }
50
51 auto actualElementName = reinterpret_cast<ElementName*>(elementNameHandle);
52 buffer->deviceId = CreateCStringFromString(actualElementName->GetDeviceID());
53 buffer->bundleName = CreateCStringFromString(actualElementName->GetBundleName());
54 buffer->abilityName = CreateCStringFromString(actualElementName->GetAbilityName());
55 buffer->moduleName = CreateCStringFromString(actualElementName->GetModuleName());
56 return buffer;
57 }
58
FFICJElementNameParamsDelete(ElementNameParams * elementNameParams)59 void FFICJElementNameParamsDelete(ElementNameParams* elementNameParams)
60 {
61 free(static_cast<void*>(elementNameParams->deviceId));
62 free(static_cast<void*>(elementNameParams->bundleName));
63 free(static_cast<void*>(elementNameParams->abilityName));
64 free(static_cast<void*>(elementNameParams->moduleName));
65 free(static_cast<void*>(elementNameParams));
66 }
67 }