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 if (inResult != 0) {
101 HiLog::Error(LABEL, "%{public}s AddFileDescriptorListener fail", __func__);
102 return -1;
103 }
104 eventHandler_ = handler;
105 eventRunner_ = myRunner;
106 int64_t delayTime = 100;
107 int64_t param = 0;
108 bool sendEventResult = eventHandler_->SendEvent(STOP_EVENT_ID, param, delayTime);
109 if (!sendEventResult) {
110 HiLog::Error(LABEL, "%{public}s EventHandler SendEvent fail", __func__);
111 return -1;
112 }
113 int32_t runResult = eventRunner_->Run();
114 if (!runResult) {
115 HiLog::Error(LABEL, "%{public}s EventRunner run fail", __func__);
116 return -1;
117 }
118 return ERR_OK;
119 }
120
DestroySensorDataChannel()121 int32_t MedicalSensorDataChannel::DestroySensorDataChannel()
122 {
123 std::lock_guard<std::mutex> eventRunnerLock(eventRunnerMutex_);
124 if (eventHandler_ == nullptr || eventRunner_ == nullptr) {
125 HiLog::Error(LABEL, "%{public}s handler or eventRunner is null", __func__);
126 return -1;
127 }
128 int32_t receiveFd = GetReceiveDataFd();
129 eventHandler_->RemoveFileDescriptorListener(receiveFd);
130 eventHandler_ = nullptr;
131 eventRunner_->Stop();
132 eventRunner_ = nullptr;
133 // destroy sensor basic channelx
134 return DestroySensorBasicChannel();
135 }
136
~MedicalSensorDataChannel()137 MedicalSensorDataChannel::~MedicalSensorDataChannel()
138 {
139 DestroySensorDataChannel();
140 }
141 } // namespace Sensors
142 } // namespace OHOS
143