• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "stationary_client.h"
17 
18 #include "devicestatus_define.h"
19 #include "stationary_params.h"
20 #include "intention_client.h"
21 
22 #undef LOG_TAG
23 #define LOG_TAG "StationaryClient"
24 
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 
SubscribeCallback(Type type,ActivityEvent event,ReportLatencyNs latency,sptr<IRemoteDevStaCallback> callback)29 int32_t StationaryClient::SubscribeCallback(Type type, ActivityEvent event,
30     ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback)
31 {
32     int32_t ret = INTENTION_CLIENT->SubscribeStationaryCallback(type, event, latency, callback);
33     if (ret != RET_OK) {
34         FI_HILOGE("SubscribeStationary fail, ret:%{public}d", ret);
35     }
36     std::lock_guard lockGrd(mtx_);
37     auto iter = std::find_if(subParams_.begin(), subParams_.end(),
38         [type, event, latency, callback](const SubscribeStationaryParam &param) {
39             return param.type_ == type && param.event_ == event && param.latency_ == latency &&
40                 param.callback_ == callback;
41         });
42     if (iter == subParams_.end()) {
43         subParams_.emplace_back(type, event, latency, callback);
44         FI_HILOGI("insert callback in subParams, type = %{public}d, size = %{public}zu", type, subParams_.size());
45     }
46     return ret;
47 }
48 
UnsubscribeCallback(Type type,ActivityEvent event,sptr<IRemoteDevStaCallback> callback)49 int32_t StationaryClient::UnsubscribeCallback(Type type, ActivityEvent event,
50     sptr<IRemoteDevStaCallback> callback)
51 {
52     int32_t ret =
53         INTENTION_CLIENT->UnsubscribeStationaryCallback(type, event, callback);
54     if (ret != RET_OK) {
55         FI_HILOGE("UnsubscribeStationary fail, ret:%{public}d", ret);
56     }
57     std::lock_guard lockGrd(mtx_);
58     auto iter = std::find_if(subParams_.begin(), subParams_.end(),
59         [type, event, callback](const SubscribeStationaryParam &param) {
60             return param.type_ == type && param.event_ == event && param.callback_ == callback;
61         });
62     if (iter != subParams_.end()) {
63         subParams_.erase(iter);
64         FI_HILOGI("delete callback in subParams, type = %{public}d, size = %{public}zu", type, subParams_.size());
65     }
66     return ret;
67 }
68 
GetDeviceStatusData(Type type)69 Data StationaryClient::GetDeviceStatusData(Type type)
70 {
71     Data reply;
72     int32_t replyType = -1;
73     int32_t replyValue = -1;
74     int32_t ret = INTENTION_CLIENT->GetDeviceStatusData(type, replyType, replyValue);
75     if (ret != RET_OK) {
76         FI_HILOGE("GetDeviceStatusData fail, ret:%{public}d", ret);
77     }
78     reply.type = static_cast<Type>(replyType);
79     reply.value = static_cast<OnChangedValue>(replyValue);
80     return reply;
81 }
82 
GetDevicePostureDataSync(DevicePostureData & data)83 int32_t StationaryClient::GetDevicePostureDataSync(DevicePostureData &data)
84 {
85     int32_t ret = INTENTION_CLIENT->GetDevicePostureDataSync(data);
86     if (ret != RET_OK) {
87         FI_HILOGE("GetDevicePostureData failed, ret=%{public}d", ret);
88         return ret;
89     }
90     return ret;
91 }
92 
OnConnected()93 void StationaryClient::OnConnected()
94 {
95     std::lock_guard lockGrd(mtx_);
96     if (subParams_.empty()) {
97         FI_HILOGI("subParams is empty");
98         return;
99     }
100     int32_t ret = RET_OK;
101     FI_HILOGI("subParams size = %{public}zu", subParams_.size());
102     for (const auto &param : subParams_) {
103         ret = INTENTION_CLIENT->SubscribeStationaryCallback(param.type_, param.event_, param.latency_,
104             param.callback_);
105         if (ret != RET_OK) {
106             FI_HILOGE("ResubscribeStationary fail, ret = %{public}d", ret);
107         }
108     }
109 }
110 } // namespace DeviceStatus
111 } // namespace Msdp
112 } // namespace OHOS
113