1 /*
2  * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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 "medical_sensor_data_channel.h"
17 
18 #include <cerrno>
19 #include <unistd.h>
20 
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 
24 #include "my_file_descriptor_listener.h"
25 #include "medical_errors.h"
26 #include "medical_log_domain.h"
27 #include "string_ex.h"
28 
29 #ifndef O_NONBLOCK
30 # define O_NONBLOCK	  04000
31 #endif
32 
33 namespace OHOS {
34 namespace Sensors {
35 using namespace OHOS::HiviewDFX;
36 using namespace OHOS::AppExecFwk;
37 std::shared_ptr<MyEventHandler> MedicalSensorDataChannel::eventHandler_;
38 std::shared_ptr<AppExecFwk::EventRunner> MedicalSensorDataChannel::eventRunner_;
39 
40 namespace {
41 constexpr HiLogLabel LABEL = { LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_FRAMEWORK, "MedicalSensorDataChannel" };
42 // max 100 data in cache buffer
43 constexpr int32_t SENSOR_READ_DATA_SIZE = sizeof(struct SensorEvent) * 100;
44 const uint32_t STOP_EVENT_ID = 0;
45 }  // namespace
46 
MedicalSensorDataChannel()47 MedicalSensorDataChannel::MedicalSensorDataChannel()
48     : dataCB_(nullptr),
49       privateData_(nullptr)
50 {}
51 
CreateSensorDataChannel(DataChannelCB callBack,void * data)52 int32_t MedicalSensorDataChannel::CreateSensorDataChannel(DataChannelCB callBack, void *data)
53 {
54     if (callBack == nullptr) {
55         HiLog::Error(LABEL, "%{public}s callBack cannot be null", __func__);
56         return SENSOR_NATIVE_REGSITER_CB_ERR;
57     }
58     dataCB_ = callBack;
59     privateData_ = data;
60     return InnerSensorDataChannel();
61 }
62 
RestoreSensorDataChannel()63 int32_t MedicalSensorDataChannel::RestoreSensorDataChannel()
64 {
65     if (dataCB_ == nullptr) {
66         HiLog::Error(LABEL, "%{public}s dataCB_ cannot be null", __func__);
67         return SENSOR_CHANNEL_RESTORE_CB_ERR;
68     }
69     if (GetReceiveDataFd() != INVALID_FD) {
70         HiLog::Error(LABEL, "%{public}s fd not close", __func__);
71         return SENSOR_CHANNEL_RESTORE_FD_ERR;
72     }
73     return InnerSensorDataChannel();
74 }
75 
InnerSensorDataChannel()76 int32_t MedicalSensorDataChannel::InnerSensorDataChannel()
77 {
78     std::lock_guard<std::mutex> eventRunnerLock(eventRunnerMutex_);
79     // create basic data channel
80     int32_t ret = CreateSensorBasicChannel(SENSOR_READ_DATA_SIZE, SENSOR_READ_DATA_SIZE);
81     if (ret != ERR_OK) {
82         HiLog::Error(LABEL, "%{public}s create basic channel failed, ret : %{public}d", __func__, ret);
83         return ret;
84     }
85     auto listener = std::make_shared<MyFileDescriptorListener>();
86     listener->SetChannel(this);
87     auto myRunner = AppExecFwk::EventRunner::Create(true);
88     if (myRunner == nullptr) {
89         HiLog::Error(LABEL, "%{public}s myRunner is null", __func__);
90         return -1;
91     }
92     auto handler = std::make_shared<MyEventHandler>(myRunner);
93     if (handler == nullptr) {
94         HiLog::Error(LABEL, "%{public}s handler is null", __func__);
95         return -1;
96     }
97 
98     int32_t receiveFd = GetReceiveDataFd();
99     auto inResult = handler->AddFileDescriptorListener(receiveFd, AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener,
100         "MedicalSensorTask");
101     if (inResult != 0) {
102         HiLog::Error(LABEL, "%{public}s AddFileDescriptorListener fail", __func__);
103         return -1;
104     }
105     eventHandler_ = handler;
106     eventRunner_ = myRunner;
107     int64_t delayTime = 100;
108     int64_t param = 0;
109     bool sendEventResult = eventHandler_->SendEvent(STOP_EVENT_ID, param, delayTime);
110     if (!sendEventResult) {
111         HiLog::Error(LABEL, "%{public}s EventHandler SendEvent fail", __func__);
112         return -1;
113     }
114     int32_t runResult = eventRunner_->Run();
115     if (!runResult) {
116         HiLog::Error(LABEL, "%{public}s EventRunner run fail", __func__);
117         return -1;
118     }
119     return ERR_OK;
120 }
121 
DestroySensorDataChannel()122 int32_t MedicalSensorDataChannel::DestroySensorDataChannel()
123 {
124     std::lock_guard<std::mutex> eventRunnerLock(eventRunnerMutex_);
125     if (eventHandler_ == nullptr || eventRunner_ == nullptr) {
126         HiLog::Error(LABEL, "%{public}s handler or eventRunner is null", __func__);
127         return -1;
128     }
129     int32_t receiveFd = GetReceiveDataFd();
130     eventHandler_->RemoveFileDescriptorListener(receiveFd);
131     eventHandler_ = nullptr;
132     eventRunner_->Stop();
133     eventRunner_ = nullptr;
134     // destroy sensor basic channelx
135     return DestroySensorBasicChannel();
136 }
137 
~MedicalSensorDataChannel()138 MedicalSensorDataChannel::~MedicalSensorDataChannel()
139 {
140     DestroySensorDataChannel();
141 }
142 }  // namespace Sensors
143 }  // namespace OHOS
144