• 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 "sensor_data_channel.h"
17 
18 #include "errors.h"
19 #include "sensor_file_descriptor_listener.h"
20 #include "sensors_errors.h"
21 
22 namespace OHOS {
23 namespace Sensors {
24 using namespace OHOS::HiviewDFX;
25 using namespace OHOS::AppExecFwk;
26 std::shared_ptr<SensorEventHandler> SensorDataChannel::eventHandler_;
27 
28 namespace {
29 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorDataChannel" };
30 }  // namespace
31 
CreateSensorDataChannel(DataChannelCB callBack,void * data)32 int32_t SensorDataChannel::CreateSensorDataChannel(DataChannelCB callBack, void *data)
33 {
34     CHKPR(callBack, SENSOR_NATIVE_REGSITER_CB_ERR);
35     dataCB_ = callBack;
36     privateData_ = data;
37     return InnerSensorDataChannel();
38 }
39 
RestoreSensorDataChannel()40 int32_t SensorDataChannel::RestoreSensorDataChannel()
41 {
42     CHKPR(dataCB_, SENSOR_NATIVE_REGSITER_CB_ERR);
43     if (GetReceiveDataFd() != -1) {
44         SEN_HILOGE("fd not close");
45         return SENSOR_CHANNEL_RESTORE_FD_ERR;
46     }
47     return InnerSensorDataChannel();
48 }
49 
InnerSensorDataChannel()50 int32_t SensorDataChannel::InnerSensorDataChannel()
51 {
52     std::lock_guard<std::mutex> eventRunnerLock(eventRunnerMutex_);
53     // create basic data channel
54     int32_t ret = CreateSensorBasicChannel();
55     if (ret != ERR_OK) {
56         SEN_HILOGE("create basic channel failed, ret:%{public}d", ret);
57         return ret;
58     }
59     auto listener = std::make_shared<SensorFileDescriptorListener>();
60     listener->SetChannel(this);
61     auto myRunner = AppExecFwk::EventRunner::Create(true);
62     CHKPR(myRunner, ERROR);
63     eventHandler_ = std::make_shared<SensorEventHandler>(myRunner);
64     CHKPR(eventHandler_, ERROR);
65     int32_t receiveFd = GetReceiveDataFd();
66     auto inResult = eventHandler_->AddFileDescriptorListener(receiveFd,
67         AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener);
68     if (inResult != 0) {
69         SEN_HILOGE("AddFileDescriptorListener fail");
70         return ERROR;
71     }
72     return ERR_OK;
73 }
74 
DestroySensorDataChannel()75 int32_t SensorDataChannel::DestroySensorDataChannel()
76 {
77     std::lock_guard<std::mutex> eventRunnerLock(eventRunnerMutex_);
78     CHKPL(eventHandler_);
79     eventHandler_ = nullptr;
80     // destroy sensor basic channelx
81     return DestroySensorBasicChannel();
82 }
83 
~SensorDataChannel()84 SensorDataChannel::~SensorDataChannel()
85 {
86     DestroySensorDataChannel();
87 }
88 }  // namespace Sensors
89 }  // namespace OHOS
90