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 "screen_sensor_connector.h"
17
18 #include <chrono>
19 #include <securec.h>
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace {
24 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
25 constexpr int32_t MOTION_ACTION_PORTRAIT = 0;
26 constexpr int32_t MOTION_ACTION_LEFT_LANDSCAPE = 1;
27 constexpr int32_t MOTION_ACTION_PORTRAIT_INVERTED = 2;
28 constexpr int32_t MOTION_ACTION_RIGHT_LANDSCAPE = 3;
29 constexpr int32_t MOTION_ACTION_TENT_MODE_OFF = 0;
30 constexpr int32_t MOTION_ACTION_TENT_MODE_ON = 1;
31 constexpr int32_t MOTION_ACTION_TENT_MODE_HOVER = 2;
32 const int32_t MOTION_TYPE_ROTATION = 700;
33 const int32_t MOTION_TYPE_TENT = 2800;
34 #endif
35 }
36
37 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
38 bool MotionSubscriber::isMotionSensorSubscribed_ = false;
39 bool MotionTentSubscriber::isMotionSensorSubscribed_ = false;
40 static void RotationMotionEventCallback(const MotionSensorEvent& motionData);
41 static void TentMotionEventCallback(const MotionSensorEvent& motionData);
42 #endif
43
SubscribeRotationSensor()44 void ScreenSensorConnector::SubscribeRotationSensor()
45 {
46 TLOGD(WmsLogTag::DMS, "subscribe rotation-related sensor");
47 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
48 MotionSubscriber::SubscribeMotionSensor();
49 #endif
50 }
51
UnsubscribeRotationSensor()52 void ScreenSensorConnector::UnsubscribeRotationSensor()
53 {
54 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
55 MotionSubscriber::UnsubscribeMotionSensor();
56 #endif
57 }
58
SubscribeTentSensor()59 void ScreenSensorConnector::SubscribeTentSensor()
60 {
61 TLOGD(WmsLogTag::DMS, "start");
62 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
63 MotionTentSubscriber::SubscribeMotionSensor();
64 #endif
65 }
66
UnsubscribeTentSensor()67 void ScreenSensorConnector::UnsubscribeTentSensor()
68 {
69 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
70 MotionTentSubscriber::UnsubscribeMotionSensor();
71 #endif
72 }
73
74 // Motion
75 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
SubscribeMotionSensor()76 void MotionSubscriber::SubscribeMotionSensor()
77 {
78 TLOGI(WmsLogTag::DMS, "start");
79 if (isMotionSensorSubscribed_) {
80 TLOGE(WmsLogTag::DMS, "motion sensor's already subscribed");
81 return;
82 }
83
84 if (!SubscribeCallback(MOTION_TYPE_ROTATION, RotationMotionEventCallback)) {
85 TLOGE(WmsLogTag::DMS, "dms: motion sensor subscribe failed");
86 return;
87 }
88 isMotionSensorSubscribed_ = true;
89 }
90
UnsubscribeMotionSensor()91 void MotionSubscriber::UnsubscribeMotionSensor()
92 {
93 if (!isMotionSensorSubscribed_) {
94 TLOGI(WmsLogTag::DMS, "start");
95 return;
96 }
97
98 if (!UnsubscribeCallback(MOTION_TYPE_ROTATION, RotationMotionEventCallback)) {
99 TLOGE(WmsLogTag::DMS, "dms: motion sensor unsubscribe failed");
100 return;
101 }
102 isMotionSensorSubscribed_ = false;
103 }
104
RotationMotionEventCallback(const MotionSensorEvent & motionData)105 void RotationMotionEventCallback(const MotionSensorEvent& motionData)
106 {
107 DeviceRotation motionRotation = DeviceRotation::INVALID;
108 switch (motionData.status) {
109 case MOTION_ACTION_PORTRAIT: {
110 motionRotation = DeviceRotation::ROTATION_PORTRAIT;
111 break;
112 }
113 case MOTION_ACTION_LEFT_LANDSCAPE: {
114 motionRotation = DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
115 break;
116 }
117 case MOTION_ACTION_PORTRAIT_INVERTED: {
118 motionRotation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
119 break;
120 }
121 case MOTION_ACTION_RIGHT_LANDSCAPE: {
122 motionRotation = DeviceRotation::ROTATION_LANDSCAPE;
123 break;
124 }
125 default: {
126 break;
127 }
128 }
129 ScreenRotationProperty::HandleSensorEventInput(motionRotation);
130 }
131
SubscribeMotionSensor()132 void MotionTentSubscriber::SubscribeMotionSensor()
133 {
134 TLOGI(WmsLogTag::DMS, "Subscribe tent motion Sensor");
135 if (isMotionSensorSubscribed_) {
136 TLOGE(WmsLogTag::DMS, "tent motion sensor's already subscribed");
137 return;
138 }
139
140 if (!SubscribeCallback(MOTION_TYPE_TENT, TentMotionEventCallback)) {
141 TLOGE(WmsLogTag::DMS, "dms: motion sensor subscribe failed");
142 return;
143 }
144 isMotionSensorSubscribed_ = true;
145 }
146
UnsubscribeMotionSensor()147 void MotionTentSubscriber::UnsubscribeMotionSensor()
148 {
149 if (!isMotionSensorSubscribed_) {
150 TLOGI(WmsLogTag::DMS, "start");
151 return;
152 }
153
154 if (!UnsubscribeCallback(MOTION_TYPE_TENT, TentMotionEventCallback)) {
155 TLOGE(WmsLogTag::DMS, "dms: motion sensor unsubscribe failed");
156 return;
157 }
158 isMotionSensorSubscribed_ = false;
159 }
160
TentMotionEventCallback(const MotionSensorEvent & motionData)161 void TentMotionEventCallback(const MotionSensorEvent& motionData)
162 {
163 TLOGI(WmsLogTag::DMS, "tent mode status %{public}d", motionData.status);
164 if (motionData.dataLen < 1 || motionData.data == nullptr) {
165 TLOGE(WmsLogTag::DMS, "tent mode datalen %{public}d", motionData.dataLen);
166 return;
167 }
168 int32_t tentData = motionData.data[0];
169 int realHall = static_cast<int>(tentData & 0xFF);
170 if (realHall != 0 && realHall != 1) {
171 TLOGW(WmsLogTag::DMS, "tent mode invalid hall : %{public}d", realHall);
172 realHall = -1;
173 }
174
175 if (motionData.status == MOTION_ACTION_TENT_MODE_OFF || motionData.status == MOTION_ACTION_TENT_MODE_ON ||
176 motionData.status == MOTION_ACTION_TENT_MODE_HOVER) {
177 ScreenTentProperty::HandleSensorEventInput(motionData.status, realHall);
178 } else {
179 TLOGE(WmsLogTag::DMS, "tent motion:%{public}d invalid", motionData.status);
180 }
181 }
182 #endif
183 } // Rosen
184 } // OHOS
185