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