• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "distributed_device_profile_stub.h"
17 
18 #include <string>
19 
20 #include "authority_manager.h"
21 #include "device_profile_errors.h"
22 #include "device_profile_log.h"
23 #include "device_profile_storage.h"
24 #include "device_profile_utils.h"
25 #include "iprofile_event_notifier.h"
26 #include "parcel_helper.h"
27 
28 namespace OHOS {
29 namespace DeviceProfile {
30 namespace {
31 const std::string TAG = "DistributedDeviceProfileStub";
32 constexpr uint32_t MAX_EVENT_LEN = 1000000;
33 }
34 
DistributedDeviceProfileStub()35 DistributedDeviceProfileStub::DistributedDeviceProfileStub()
36 {
37     funcsMap_[PUT_DEVICE_PROFILE] = &DistributedDeviceProfileStub::PutDeviceProfileInner;
38     funcsMap_[DELETE_DEVICE_PROFILE] = &DistributedDeviceProfileStub::DeleteDeviceProfileInner;
39     funcsMap_[GET_DEVICE_PROFILE] = &DistributedDeviceProfileStub::GetDeviceProfileInner;
40     funcsMap_[SUBSCRIBE_PROFILE_EVENT] = &DistributedDeviceProfileStub::SubscribeProfileEventInner;
41     funcsMap_[UNSUBSCRIBE_PROFILE_EVENT] = &DistributedDeviceProfileStub::UnsubscribeProfileEventInner;
42     funcsMap_[SYNC_DEVICE_PROFILE] = &DistributedDeviceProfileStub::SyncDeviceProfileInner;
43 }
44 
EnforceInterfaceToken(MessageParcel & data)45 bool DistributedDeviceProfileStub::EnforceInterfaceToken(MessageParcel& data)
46 {
47     return data.ReadInterfaceToken() == IDistributedDeviceProfile::GetDescriptor();
48 }
49 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t DistributedDeviceProfileStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
51     MessageParcel& reply, MessageOption& option)
52 {
53     HILOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
54 
55     auto iter = funcsMap_.find(code);
56     if (iter != funcsMap_.end()) {
57         auto func = iter->second;
58         if (!EnforceInterfaceToken(data)) {
59             HILOGE("check interface token failed");
60             return ERR_DP_INTERFACE_CHECK_FAILED;
61         }
62         if (!AuthorityManager::GetInstance().CheckCallerTrust()) {
63             HILOGE("caller is not trusted");
64             return ERR_DP_PERMISSION_DENIED;
65         }
66         if (func != nullptr) {
67             return (this->*func)(data, reply);
68         }
69     }
70     HILOGW("unknown request code, please check");
71     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 }
73 
PutDeviceProfileInner(MessageParcel & data,MessageParcel & reply)74 int32_t DistributedDeviceProfileStub::PutDeviceProfileInner(MessageParcel& data, MessageParcel& reply)
75 {
76     HILOGI("called");
77     ServiceCharacteristicProfile profile;
78     if (!profile.Unmarshalling(data)) {
79         return ERR_NULL_OBJECT;
80     }
81     return PutDeviceProfile(profile);
82 }
83 
GetDeviceProfileInner(MessageParcel & data,MessageParcel & reply)84 int32_t DistributedDeviceProfileStub::GetDeviceProfileInner(MessageParcel& data, MessageParcel& reply)
85 {
86     HILOGI("called");
87     std::string udid;
88     std::string serviceId;
89     ServiceCharacteristicProfile profile;
90     PARCEL_READ_HELPER(data, String, udid);
91     PARCEL_READ_HELPER(data, String, serviceId);
92     int32_t ret = GetDeviceProfile(udid, serviceId, profile);
93     if (!(reply.WriteInt32(ret) && profile.Marshalling(reply))) {
94         HILOGE("marshall profile failed");
95         return ERR_FLATTEN_OBJECT;
96     }
97     return ERR_NONE;
98 }
99 
DeleteDeviceProfileInner(MessageParcel & data,MessageParcel & reply)100 int32_t DistributedDeviceProfileStub::DeleteDeviceProfileInner(MessageParcel& data, MessageParcel& reply)
101 {
102     HILOGI("called");
103     return DeleteDeviceProfile(data.ReadString());
104 }
105 
SubscribeProfileEventInner(MessageParcel & data,MessageParcel & reply)106 int32_t DistributedDeviceProfileStub::SubscribeProfileEventInner(MessageParcel& data, MessageParcel& reply)
107 {
108     HILOGI("called");
109     uint32_t numSubscribeInfos = data.ReadUint32();
110     if (numSubscribeInfos == 0 || numSubscribeInfos > MAX_EVENT_LEN) {
111         return ERR_DP_INVALID_PARAMS;
112     }
113 
114     std::list<SubscribeInfo> subscribeInfos;
115     for (uint32_t i = 0; i < numSubscribeInfos; i++) {
116         SubscribeInfo subscribeInfo;
117         if (!subscribeInfo.Unmarshalling(data)) {
118             return ERR_NULL_OBJECT;
119         }
120         HILOGD("profile event = %{public}d", subscribeInfo.profileEvent);
121         subscribeInfos.emplace_back(std::move(subscribeInfo));
122     }
123     sptr<IRemoteObject> eventNotifier = data.ReadRemoteObject();
124     std::list<ProfileEvent> failedEvents;
125 
126     int32_t errCode = SubscribeProfileEvents(subscribeInfos, eventNotifier, failedEvents);
127     HILOGI("errCode = %{public}d", errCode);
128     if (!reply.WriteInt32(errCode)) {
129         return ERR_FLATTEN_OBJECT;
130     }
131     if ((errCode != ERR_OK) && !DeviceProfileUtils::WriteProfileEvents(failedEvents, reply)) {
132         HILOGE("write profile events failed");
133         return ERR_FLATTEN_OBJECT;
134     }
135     return ERR_NONE;
136 }
137 
UnsubscribeProfileEventInner(MessageParcel & data,MessageParcel & reply)138 int32_t DistributedDeviceProfileStub::UnsubscribeProfileEventInner(MessageParcel& data, MessageParcel& reply)
139 {
140     HILOGI("called");
141     uint32_t numEvents = data.ReadUint32();
142     if (numEvents == 0 || numEvents > MAX_EVENT_LEN) {
143         return ERR_DP_INVALID_PARAMS;
144     }
145 
146     std::list<ProfileEvent> profileEvents;
147     for (uint32_t i = 0; i < numEvents; i++) {
148         ProfileEvent profileEvent = static_cast<ProfileEvent>(data.ReadUint32());
149         if (profileEvent >= EVENT_PROFILE_END || profileEvent == EVENT_UNKNOWN) {
150             return ERR_DP_INVALID_PARAMS;
151         }
152         profileEvents.emplace_back(profileEvent);
153     }
154     sptr<IRemoteObject> eventNotifier = data.ReadRemoteObject();
155     std::list<ProfileEvent> failedEvents;
156 
157     int32_t errCode = UnsubscribeProfileEvents(profileEvents, eventNotifier, failedEvents);
158     HILOGI("errCode = %{public}d", errCode);
159     if (!reply.WriteInt32(errCode)) {
160         return ERR_FLATTEN_OBJECT;
161     }
162     if ((errCode != ERR_OK) && !DeviceProfileUtils::WriteProfileEvents(failedEvents, reply)) {
163         HILOGE("write profile events failed");
164         return ERR_FLATTEN_OBJECT;
165     }
166     return ERR_NONE;
167 }
168 
SyncDeviceProfileInner(MessageParcel & data,MessageParcel & reply)169 int32_t DistributedDeviceProfileStub::SyncDeviceProfileInner(MessageParcel& data, MessageParcel& reply)
170 {
171     SyncOptions syncOption;
172     if (!syncOption.Unmarshalling(data)) {
173         HILOGD("unmarshalling failed");
174         return ERR_NULL_OBJECT;
175     }
176 
177     sptr<IRemoteObject> eventNotifier = data.ReadRemoteObject();
178     return SyncDeviceProfile(syncOption, eventNotifier);
179 }
180 } // namespace DeviceProfile
181 } // namespace OHOS