1 /*
2 * Copyright (c) 2024-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 <securec.h>
17 #include "proximity_controller_base.h"
18 #include "ffrt_utils.h"
19 #include "power_log.h"
20 #include "errors.h"
21 #include "setting_helper.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
ProximityControllerBase(const std::string & name,SensorCallbackFunc callback)25 ProximityControllerBase::ProximityControllerBase(const std::string& name, SensorCallbackFunc callback)
26 {
27 #ifdef HAS_SENSORS_SENSOR_PART
28 POWER_HILOGD(FEATURE_INPUT, "Instance enter");
29 callback_ = callback;
30 name_ = name;
31 InitProximitySensorUser();
32 #endif
33 }
34
~ProximityControllerBase()35 ProximityControllerBase::~ProximityControllerBase()
36 {
37 #ifdef HAS_SENSORS_SENSOR_PART
38 if (IsSupported()) {
39 UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
40 }
41 #endif
42 }
43
44 #ifdef HAS_SENSORS_SENSOR_PART
InitProximitySensorUser()45 bool ProximityControllerBase::InitProximitySensorUser()
46 {
47 SensorInfo* sensorInfo = nullptr;
48 int32_t count = 0;
49 const constexpr int32_t PARAM_ZERO = 0;
50 int ret = GetAllSensors(&sensorInfo, &count);
51 if (ret != PARAM_ZERO || sensorInfo == nullptr) {
52 POWER_HILOGE(FEATURE_INPUT, "Get sensors fail, ret=%{public}d", ret);
53 return false;
54 }
55 for (int32_t i = PARAM_ZERO; i < count; i++) {
56 if (sensorInfo[i].sensorTypeId == SENSOR_TYPE_ID_PROXIMITY) {
57 POWER_HILOGI(FEATURE_INPUT, "Support PROXIMITY sensor");
58 SetSupported(true);
59 break;
60 }
61 }
62 if (!IsSupported()) {
63 POWER_HILOGE(FEATURE_INPUT, "PROXIMITY sensor not support");
64 return false;
65 }
66 if (strcpy_s(user_.name, sizeof(user_.name), name_.c_str()) != EOK) {
67 POWER_HILOGE(FEATURE_INPUT, "strcpy_s user_.name=%{public}s error", name_.c_str());
68 return true;
69 }
70 user_.userData = nullptr;
71 user_.callback = callback_;
72 return true;
73 }
74
Enable()75 void ProximityControllerBase::Enable()
76 {
77 POWER_HILOGD(FEATURE_INPUT, "Enter");
78 SetEnabled(true);
79 if (!IsSupported() && !InitProximitySensorUser()) {
80 POWER_HILOGE(FEATURE_INPUT, "Enable PROXIMITY sensor not support");
81 return;
82 }
83 int32_t errorCode = SubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
84 if (errorCode != ERR_OK) {
85 POWER_HILOGW(FEATURE_INPUT, "SubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
86 return;
87 }
88 SetBatch(SENSOR_TYPE_ID_PROXIMITY, &user_, SAMPLING_RATE, SAMPLING_RATE);
89 errorCode = ActivateSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
90 if (errorCode != ERR_OK) {
91 POWER_HILOGW(FEATURE_INPUT, "ActivateSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
92 return;
93 }
94 SetMode(SENSOR_TYPE_ID_PROXIMITY, &user_, SENSOR_ON_CHANGE);
95 POWER_HILOGI(FEATURE_INPUT, "ActivateSensor PROXIMITY success");
96 }
97
Disable()98 void ProximityControllerBase::Disable()
99 {
100 POWER_HILOGD(FEATURE_INPUT, "Enter");
101 SetEnabled(false);
102 if (!IsSupported()) {
103 POWER_HILOGE(FEATURE_INPUT, "Disable PROXIMITY sensor not support");
104 return;
105 }
106
107 DeactivateSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
108 int32_t errorCode = UnsubscribeSensor(SENSOR_TYPE_ID_PROXIMITY, &user_);
109 if (errorCode != ERR_OK) {
110 POWER_HILOGW(FEATURE_INPUT, "UnsubscribeSensor PROXIMITY failed, errorCode=%{public}d", errorCode);
111 return;
112 }
113 POWER_HILOGI(FEATURE_INPUT, "UnsubscribeSensor PROXIMITY success");
114 }
115 #endif
116 } // namespace PowerMgr
117 } // namespace OHOS