1 /*
2 * Copyright (c) 2023 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 "UdmfServiceStub"
16
17 #include "udmf_service_stub.h"
18
19 #include <vector>
20 #include <string_ex.h>
21
22 #include "accesstoken_kit.h"
23 #include "ipc_skeleton.h"
24 #include "log_print.h"
25 #include "udmf_types_util.h"
26 #include "unified_data.h"
27 #include "unified_meta.h"
28
29 namespace OHOS {
30 namespace UDMF {
31 constexpr UdmfServiceStub::Handler
32 UdmfServiceStub::HANDLERS[static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_BUTT)];
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)33 int UdmfServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
34 {
35 ZLOGI("start##code = %{public}u callingPid:%{public}u callingUid:%{public}u.", code, IPCSkeleton::GetCallingPid(),
36 IPCSkeleton::GetCallingUid());
37 std::u16string myDescripter = UdmfServiceStub::GetDescriptor();
38 std::u16string remoteDescripter = data.ReadInterfaceToken();
39 if (myDescripter != remoteDescripter) {
40 ZLOGE("end##descriptor checked fail,myDescripter = %{public}s,remoteDescripter = %{public}s.",
41 Str16ToStr8(myDescripter).c_str(), Str16ToStr8(remoteDescripter).c_str());
42 return -1;
43 }
44 if (static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_HEAD) > code ||
45 code >= static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_BUTT)) {
46 return -1;
47 }
48 return (this->*HANDLERS[code])(data, reply);
49 }
50
OnSetData(MessageParcel & data,MessageParcel & reply)51 int32_t UdmfServiceStub::OnSetData(MessageParcel &data, MessageParcel &reply)
52 {
53 ZLOGI("start");
54 CustomOption customOption;
55 UnifiedData unifiedData;
56 if (!ITypesUtil::Unmarshal(data, customOption, unifiedData)) {
57 ZLOGE("Unmarshal customOption or unifiedData failed!");
58 return E_READ_PARCEL_ERROR;
59 }
60 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
61 customOption.tokenId = token;
62 std::string key;
63 int32_t status = SetData(customOption, unifiedData, key);
64 if (!ITypesUtil::Marshal(reply, status, key)) {
65 ZLOGE("Marshal status or key failed, status: %{public}d, key: %{public}s", status, key.c_str());
66 return E_WRITE_PARCEL_ERROR;
67 }
68 return E_OK;
69 }
70
OnGetData(MessageParcel & data,MessageParcel & reply)71 int32_t UdmfServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply)
72 {
73 ZLOGI("start");
74 QueryOption query;
75 if (!ITypesUtil::Unmarshal(data, query)) {
76 ZLOGE("Unmarshal queryOption failed!");
77 return E_READ_PARCEL_ERROR;
78 }
79 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
80 query.tokenId = token;
81 UnifiedData unifiedData;
82 int32_t status = GetData(query, unifiedData);
83 if (!ITypesUtil::Marshal(reply, status, unifiedData)) {
84 ZLOGE("Marshal status or unifiedData failed, status: %{public}d", status);
85 return E_WRITE_PARCEL_ERROR;
86 }
87 return E_OK;
88 }
89
OnGetBatchData(MessageParcel & data,MessageParcel & reply)90 int32_t UdmfServiceStub::OnGetBatchData(MessageParcel &data, MessageParcel &reply)
91 {
92 ZLOGI("start");
93 QueryOption query;
94 if (!ITypesUtil::Unmarshal(data, query)) {
95 ZLOGE("Unmarshal queryOption failed!");
96 return E_READ_PARCEL_ERROR;
97 }
98 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
99 query.tokenId = token;
100 std::vector<UnifiedData> unifiedDataSet;
101 int32_t status = GetBatchData(query, unifiedDataSet);
102 if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
103 ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
104 return E_WRITE_PARCEL_ERROR;
105 }
106 return E_OK;
107 }
108
OnUpdateData(MessageParcel & data,MessageParcel & reply)109 int32_t UdmfServiceStub::OnUpdateData(MessageParcel &data, MessageParcel &reply)
110 {
111 ZLOGI("start");
112 QueryOption query;
113 UnifiedData unifiedData;
114 if (!ITypesUtil::Unmarshal(data, query, unifiedData)) {
115 ZLOGE("Unmarshal queryOption or unifiedData failed!");
116 return E_READ_PARCEL_ERROR;
117 }
118 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
119 query.tokenId = token;
120 int32_t status = UpdateData(query, unifiedData);
121 if (!ITypesUtil::Marshal(reply, status)) {
122 ZLOGE("Marshal status failed, status: %{public}d", status);
123 return E_WRITE_PARCEL_ERROR;
124 }
125 return E_OK;
126 }
127
OnDeleteData(MessageParcel & data,MessageParcel & reply)128 int32_t UdmfServiceStub::OnDeleteData(MessageParcel &data, MessageParcel &reply)
129 {
130 ZLOGI("start");
131 QueryOption query;
132 if (!ITypesUtil::Unmarshal(data, query)) {
133 ZLOGE("Unmarshal queryOption failed!");
134 return E_READ_PARCEL_ERROR;
135 }
136 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
137 query.tokenId = token;
138 std::vector<UnifiedData> unifiedDataSet;
139 int32_t status = DeleteData(query, unifiedDataSet);
140 if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
141 ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
142 return E_WRITE_PARCEL_ERROR;
143 }
144 return E_OK;
145 }
146
OnGetSummary(MessageParcel & data,MessageParcel & reply)147 int32_t UdmfServiceStub::OnGetSummary(MessageParcel &data, MessageParcel &reply)
148 {
149 ZLOGI("start");
150 QueryOption query;
151 if (!ITypesUtil::Unmarshal(data, query)) {
152 ZLOGE("Unmarshal query failed");
153 return E_READ_PARCEL_ERROR;
154 }
155 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
156 query.tokenId = token;
157 Summary summary;
158 int32_t status = GetSummary(query, summary);
159 if (!ITypesUtil::Marshal(reply, status, summary)) {
160 ZLOGE("Marshal summary failed, key: %{public}s", query.key.c_str());
161 return E_WRITE_PARCEL_ERROR;
162 }
163 return E_OK;
164 }
165
OnAddPrivilege(MessageParcel & data,MessageParcel & reply)166 int32_t UdmfServiceStub::OnAddPrivilege(MessageParcel &data, MessageParcel &reply)
167 {
168 ZLOGI("start");
169 QueryOption query;
170 Privilege privilege;
171 if (!ITypesUtil::Unmarshal(data, query, privilege)) {
172 ZLOGE("Unmarshal query and privilege failed");
173 return E_READ_PARCEL_ERROR;
174 }
175 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
176 query.tokenId = token;
177 int32_t status = AddPrivilege(query, privilege);
178 if (!ITypesUtil::Marshal(reply, status)) {
179 ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
180 return E_WRITE_PARCEL_ERROR;
181 }
182 return E_OK;
183 }
184
OnSync(MessageParcel & data,MessageParcel & reply)185 int32_t UdmfServiceStub::OnSync(MessageParcel &data, MessageParcel &reply)
186 {
187 ZLOGI("start");
188 QueryOption query;
189 std::vector<std::string> devices;
190 if (!ITypesUtil::Unmarshal(data, query, devices)) {
191 ZLOGE("Unmarshal query and devices failed");
192 return E_READ_PARCEL_ERROR;
193 }
194 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
195 query.tokenId = token;
196 int32_t status = Sync(query, devices);
197 if (!ITypesUtil::Marshal(reply, status)) {
198 ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
199 return E_WRITE_PARCEL_ERROR;
200 }
201 return E_OK;
202 }
203 } // namespace UDMF
204 } // namespace OHOS