• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_common_ffi.h"
17 #include "udmf_client.h"
18 #include "udmf_log.h"
19 #include "unified_data.h"
20 #include "unified_data_impl.h"
21 #include "unified_meta.h"
22 #include "unified_record_impl.h"
23 #include "utils.h"
24 
25 using namespace OHOS::FFI;
26 using namespace OHOS::UDMF;
27 
28 using ErrCode = int;
29 
30 namespace OHOS {
31 namespace UDMF {
32 extern "C" {
33 
FfiUDMFInsertData(const char * intention,int64_t unifiedDataId,int32_t * errCode)34 FFI_EXPORT char *FfiUDMFInsertData(const char *intention, int64_t unifiedDataId, int32_t *errCode)
35 {
36     *errCode = E_INVALID_PARAMETERS;
37     std::string key;
38     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
39     if (data == nullptr || data->unifiedData_ == nullptr) {
40         LOGE("Get unified data failed!");
41         return nullptr;
42     }
43     Intention it = UnifiedDataUtils::GetIntentionByString(intention);
44     if (it == UD_INTENTION_DRAG) {
45         LOGE("Parameter error: The intention parameter is invalid");
46         return nullptr;
47     }
48     CustomOption option = { .intention = it };
49     auto status = UdmfClient::GetInstance().SetData(option, *(data->unifiedData_), key);
50     *errCode = status;
51     if (status != E_OK) {
52         LOGE("Insert data failed!");
53     }
54     return Utils::MallocCString(key);
55 }
56 
FfiUDMFUpdateData(const char * key,int64_t unifiedDataId)57 FFI_EXPORT int32_t FfiUDMFUpdateData(const char *key, int64_t unifiedDataId)
58 {
59     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
60     if (data == nullptr || data->unifiedData_ == nullptr) {
61         LOGE("Get unified data failed!");
62         return E_INVALID_PARAMETERS;
63     }
64     UnifiedKey verifyKey(key);
65     if (!verifyKey.IsValid()) {
66         LOGE("Parameter error: parameter key is invalid");
67         return E_INVALID_PARAMETERS;
68     }
69     QueryOption option = { .key = key };
70     auto status = UdmfClient::GetInstance().UpdateData(option, *(data->unifiedData_));
71     if (status != E_OK) {
72         LOGE("Update unified data failed!");
73     }
74     return status;
75 }
76 
FfiUDMFQueryData(const char * intention,const char * key,int32_t * errCode)77 FFI_EXPORT CArrI64 FfiUDMFQueryData(const char *intention, const char *key, int32_t *errCode)
78 {
79     *errCode = E_INVALID_PARAMETERS;
80     CArrI64 queryData = { .head = nullptr, .size = 0 };
81     Intention it = UnifiedDataUtils::GetIntentionByString(intention);
82     if (it == UD_INTENTION_DRAG) {
83         LOGE("Parameter error: the intention parameter is invalid");
84         return queryData;
85     }
86     QueryOption option = { .key = key, .intention = it };
87     std::vector<UnifiedData> unifiedDataSet;
88     auto status = UdmfClient::GetInstance().GetBatchData(option, unifiedDataSet);
89     *errCode = status;
90     if (status != E_OK) {
91         LOGE("Query unified data failed!");
92         return queryData;
93     }
94     auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * unifiedDataSet.size()));
95     if (arr == nullptr) {
96         LOGE("malloc error in queryData");
97         return queryData;
98     }
99     for (uint64_t i = 0; i < unifiedDataSet.size(); i++) {
100         std::shared_ptr<UnifiedData> unifiedDataItem = std::make_shared<UnifiedData>();
101         auto cUnifiedData = FFIData::Create<CUnifiedData>();
102         if (unifiedDataItem == nullptr || cUnifiedData == nullptr) {
103             LOGE("Create unifiedData failed. Instance is null.");
104             free(arr);
105             arr = nullptr;
106             return queryData;
107         }
108         unifiedDataItem->SetRecords(unifiedDataSet[i].GetRecords());
109         unifiedDataItem->SetProperties(unifiedDataSet[i].GetProperties());
110         cUnifiedData->unifiedData_ = unifiedDataItem;
111         arr[i] = cUnifiedData->GetID();
112     }
113     queryData.head = arr;
114     queryData.size = static_cast<int64_t>(unifiedDataSet.size());
115     return queryData;
116 }
117 
FfiUDMFDeleteData(const char * intention,const char * key,int32_t * errCode)118 FFI_EXPORT CArrI64 FfiUDMFDeleteData(const char *intention, const char *key, int32_t *errCode)
119 {
120     *errCode = E_INVALID_PARAMETERS;
121     CArrI64 deleteData = { .head = nullptr, .size = 0 };
122     Intention it = UnifiedDataUtils::GetIntentionByString(intention);
123     if (it == UD_INTENTION_DRAG) {
124         LOGE("Parameter error: the intention parameter is invalid");
125         return deleteData;
126     }
127     QueryOption option = { .key = key, .intention = it };
128     std::vector<UnifiedData> unifiedDataSet;
129     auto status = UdmfClient::GetInstance().DeleteData(option, unifiedDataSet);
130     *errCode = status;
131     if (status != E_OK) {
132         LOGE("Query unified data failed!");
133         return deleteData;
134     }
135     auto arr = static_cast<int64_t*>(malloc(sizeof(int64_t) * unifiedDataSet.size()));
136     if (arr == nullptr) {
137         LOGE("malloc error in deleteData");
138         return deleteData;
139     }
140     for (uint64_t i = 0; i < unifiedDataSet.size(); i++) {
141         std::shared_ptr<UnifiedData> unifiedDataItem = std::make_shared<UnifiedData>();
142         auto cUnifiedData = FFIData::Create<CUnifiedData>();
143         if (unifiedDataItem == nullptr || cUnifiedData == nullptr) {
144             LOGE("Create unifiedData failed. Instance is null.");
145             free(arr);
146             arr = nullptr;
147             return deleteData;
148         }
149         unifiedDataItem->SetRecords(unifiedDataSet[i].GetRecords());
150         unifiedDataItem->SetProperties(unifiedDataSet[i].GetProperties());
151         cUnifiedData->unifiedData_ = unifiedDataItem;
152         arr[i] = cUnifiedData->GetID();
153     }
154     deleteData.head = arr;
155     deleteData.size = static_cast<int64_t>(unifiedDataSet.size());
156     return deleteData;
157 }
158 
FfiUDMFFreeStringData(char * str)159 FFI_EXPORT void FfiUDMFFreeStringData(char *str)
160 {
161     free(str);
162 }
163 
FfiUDMFFreeArrI64Data(CArrI64 * ptr)164 FFI_EXPORT void FfiUDMFFreeArrI64Data(CArrI64* ptr)
165 {
166     if (ptr == nullptr || ptr->head == nullptr) {
167         return;
168     }
169     free(ptr->head);
170     ptr->head = nullptr;
171 }
172 }
173 } // namespace UDMF
174 } // namespace OHOS