• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "fusion_device_profile.h"
17 
18 #include <sstream>
19 
20 #include "distributed_device_profile_client.h"
21 
22 #include "devicestatus_define.h"
23 
24 using namespace OHOS::DeviceProfile;
25 
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, OHOS::Msdp::MSDP_DOMAIN_ID, "FusionDeviceProfile" };
28 const std::string SERVICE_ID { "deviceStatus" };
29 } // namespace
30 
31 class ProfileEventCallbackImpl final : public IProfileEventCallback {
32 public:
33     explicit ProfileEventCallbackImpl(CIProfileEventCb* eventCb);
34     ~ProfileEventCallbackImpl();
35 
36     void OnSyncCompleted(const SyncResult &syncResults) override;
37     void OnProfileChanged(const ProfileChangeNotification &changeNotification) override;
38 
39 private:
40     CIProfileEventCb* eventCb_ { nullptr };
41 };
42 
ProfileEventCallbackImpl(CIProfileEventCb * eventCb)43 ProfileEventCallbackImpl::ProfileEventCallbackImpl(CIProfileEventCb* eventCb)
44 {
45     if ((eventCb != nullptr) && (eventCb->clone != nullptr)) {
46         eventCb_ = eventCb->clone(eventCb);
47     }
48 }
49 
~ProfileEventCallbackImpl()50 ProfileEventCallbackImpl::~ProfileEventCallbackImpl()
51 {
52     if (eventCb_ != nullptr && eventCb_->destruct != nullptr) {
53         eventCb_->destruct(eventCb_);
54     }
55 }
56 
OnSyncCompleted(const SyncResult & syncResults)57 void ProfileEventCallbackImpl::OnSyncCompleted(const SyncResult &syncResults)
58 {
59     std::for_each(syncResults.begin(), syncResults.end(), [](const auto &syncResult) {
60         FI_HILOGD("Sync result:%{public}d", syncResult.second);
61     });
62 }
63 
OnProfileChanged(const ProfileChangeNotification & changeNotification)64 void ProfileEventCallbackImpl::OnProfileChanged(const ProfileChangeNotification &changeNotification)
65 {
66     CALL_INFO_TRACE;
67 }
68 
Destruct(CIProfileEvents * target)69 static void Destruct(CIProfileEvents* target)
70 {
71     CHKPV(target);
72     if (target->profileEvents == nullptr) {
73         delete target;
74     } else {
75         delete [] target->profileEvents;
76         delete target;
77     }
78 }
79 
PutDeviceProfile(const CServiceCharacteristicProfile * profile)80 int32_t PutDeviceProfile(const CServiceCharacteristicProfile* profile)
81 {
82     CALL_DEBUG_ENTER;
83     return RET_ERR;
84 }
85 
GetDeviceProfile(const char * udId,const char * serviceId,CServiceCharacteristicProfile * profile)86 int32_t GetDeviceProfile(const char* udId, const char* serviceId, CServiceCharacteristicProfile* profile)
87 {
88     CALL_DEBUG_ENTER;
89     return RET_ERR;
90 }
91 
SubscribeProfileEvents(const CISubscribeInfos * subscribeInfos,CIProfileEventCb * eventCb,CIProfileEvents ** failedEvents)92 int32_t SubscribeProfileEvents(const CISubscribeInfos* subscribeInfos,
93                                CIProfileEventCb* eventCb,
94                                CIProfileEvents** failedEvents)
95 {
96     CALL_DEBUG_ENTER;
97     CHKPR(subscribeInfos, RET_ERR);
98     CHKPR(subscribeInfos->subscribeInfos, RET_ERR);
99     std::list<SubscribeInfo> subscriptions;
100 
101     for (size_t index = 0; index < subscribeInfos->nSubscribeInfos; ++index) {
102         const CSubscribeInfo &cSub = subscribeInfos->subscribeInfos[index];
103 
104         if ((cSub.profileEvent >= ProfileEvent::EVENT_UNKNOWN) &&
105             (cSub.profileEvent < ProfileEvent::EVENT_PROFILE_END)) {
106             CHKPC(cSub.extraInfo);
107             SubscribeInfo subscription;
108             subscription.profileEvent = static_cast<ProfileEvent>(cSub.profileEvent);
109             subscription.extraInfo = nlohmann::json::parse(cSub.extraInfo, nullptr, false);
110             subscriptions.push_back(subscription);
111         }
112     }
113 
114     auto callback = std::make_shared<ProfileEventCallbackImpl>(eventCb);
115     std::list<ProfileEvent> fails;
116 
117     int32_t ret = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents(
118         subscriptions, callback, fails);
119 
120     if (!fails.empty()) {
121         CIProfileEvents* events = new (std::nothrow) CIProfileEvents;
122         CHKPR(events, RET_ERR);
123         events->numOfProfileEvents = fails.size();
124         events->profileEvents = new (std::nothrow) uint32_t[fails.size()];
125         if (events->profileEvents == nullptr) {
126             delete events;
127             FI_HILOGE("Failed to allocate memory for profileEvents");
128             return RET_ERR;
129         }
130         events->clone = nullptr;
131         events->destruct = &Destruct;
132 
133         size_t index = 0;
134         for (const auto &profile_event: fails) {
135             events->profileEvents[index++] = profile_event;
136         }
137         *failedEvents = events;
138         events->destruct(events);
139     } else {
140         *failedEvents = nullptr;
141     }
142     return ret;
143 }
144 
UnsubscribeProfileEvents(const CIProfileEvents * profileEvents,CIProfileEventCb * eventCb,CIProfileEvents ** failedEvents)145 int32_t UnsubscribeProfileEvents(const CIProfileEvents* profileEvents,
146                                  CIProfileEventCb* eventCb,
147                                  CIProfileEvents** failedEvents)
148 {
149     CALL_DEBUG_ENTER;
150     CHKPR(profileEvents, RET_ERR);
151     std::list<ProfileEvent> profiles;
152 
153     for (size_t index = 0; index < profileEvents->numOfProfileEvents; ++index) {
154         uint32_t cPro = profileEvents->profileEvents[index];
155         if ((cPro >= static_cast<uint32_t>(ProfileEvent::EVENT_UNKNOWN)) &&
156             (cPro < static_cast<uint32_t>(ProfileEvent::EVENT_PROFILE_END))) {
157             ProfileEvent profile = static_cast<ProfileEvent>(cPro);
158             profiles.push_back(profile);
159         }
160     }
161 
162     auto callback = std::make_shared<ProfileEventCallbackImpl>(eventCb);
163     std::list<ProfileEvent> fails;
164 
165     int32_t ret = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents(
166         profiles, callback, fails);
167 
168     if (!fails.empty()) {
169         CIProfileEvents* events = new (std::nothrow) CIProfileEvents;
170         CHKPR(events, RET_ERR);
171         events->numOfProfileEvents = fails.size();
172         events->profileEvents = new (std::nothrow) uint32_t[fails.size()];
173         if (events->profileEvents == nullptr) {
174             delete events;
175             FI_HILOGE("Failed to allocate memory for profileEvents");
176             return RET_ERR;
177         }
178         events->clone = nullptr;
179         events->destruct = &Destruct;
180 
181         size_t index = 0;
182         for (const auto &profile_event: fails) {
183             events->profileEvents[index++] = profile_event;
184         }
185         *failedEvents = events;
186         events->destruct(events);
187     } else {
188         *failedEvents = nullptr;
189     }
190     return ret;
191 }
192 
SyncDeviceProfile(const CSyncOptions * syncOptions,CIProfileEventCb * syncCb)193 int32_t SyncDeviceProfile(const CSyncOptions* syncOptions, CIProfileEventCb* syncCb)
194 {
195     CALL_DEBUG_ENTER;
196     return RET_ERR;
197 }
198