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 "thermal_observer.h"
17
18 #include <datetime_ex.h>
19
20 #include "ithermal_temp_callback.h"
21 #include "constants.h"
22 #include "string_operation.h"
23 #include "thermal_config_base_info.h"
24 #include "thermal_common.h"
25 #include "thermal_service.h"
26
27 namespace OHOS {
28 namespace PowerMgr {
29 namespace {
30 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
31 const std::string TASK_UNREG_SENSOR_TEMP_CALLBACK = "SensorTemp_UnRegSensorTempCB";
32 }
ThermalObserver(const wptr<ThermalService> & tms)33 ThermalObserver::ThermalObserver(const wptr<ThermalService>& tms) : tms_(tms) {};
~ThermalObserver()34 ThermalObserver::~ThermalObserver() {};
35
Init()36 bool ThermalObserver::Init()
37 {
38 THERMAL_HILOGD(COMP_SVC, "Enter");
39 if (sensorTempCBDeathRecipient_ == nullptr) {
40 sensorTempCBDeathRecipient_ = new SensorTempCallbackDeathRecipient();
41 }
42
43 InitSensorTypeMap();
44 THERMAL_HILOGD(COMP_SVC, "Exit");
45 return true;
46 }
47
InitSensorTypeMap()48 void ThermalObserver::InitSensorTypeMap()
49 {
50 THERMAL_HILOGD(COMP_SVC, "Enter");
51 std::vector<std::string> sensorType(TYPE_MAX_SIZE);
52 auto baseInfo = g_service->GetBaseinfoObj();
53 if (baseInfo == nullptr) return;
54 auto typeList = baseInfo->GetSensorsType();
55
56 THERMAL_HILOGD(COMP_SVC, "sensorType size = %{public}zu", typeList.size());
57 if (typeList.size() <= TYPE_MAX_SIZE) {
58 typeList.resize(TYPE_MAX_SIZE);
59 } else {
60 return;
61 }
62
63 if (!typeList.empty()) {
64 for (uint32_t i = 0; i < typeList.size(); i++) {
65 THERMAL_HILOGD(COMP_SVC, "sensorType = %{public}s", typeList[i].c_str());
66 sensorType[i] = typeList[i];
67 }
68 }
69 typeMap_.insert(std::make_pair(SensorType::SOC, sensorType[ARG_0]));
70 typeMap_.insert(std::make_pair(SensorType::BATTERY, sensorType[ARG_1]));
71 typeMap_.insert(std::make_pair(SensorType::SHELL, sensorType[ARG_2]));
72 typeMap_.insert(std::make_pair(SensorType::SENSOR1, sensorType[ARG_3]));
73 typeMap_.insert(std::make_pair(SensorType::SENSOR2, sensorType[ARG_4]));
74 typeMap_.insert(std::make_pair(SensorType::SENSOR3, sensorType[ARG_5]));
75 typeMap_.insert(std::make_pair(SensorType::SENSOR4, sensorType[ARG_6]));
76 typeMap_.insert(std::make_pair(SensorType::SENSOR5, sensorType[ARG_7]));
77 typeMap_.insert(std::make_pair(SensorType::SENSOR6, sensorType[ARG_8]));
78 typeMap_.insert(std::make_pair(SensorType::SENSOR7, sensorType[ARG_9]));
79 THERMAL_HILOGD(COMP_SVC, "Exit");
80 }
81
SetRegisterCallback(Callback & callback)82 void ThermalObserver::SetRegisterCallback(Callback &callback)
83 {
84 callback_ = callback;
85 }
86
SubscribeThermalTempCallback(const std::vector<std::string> & typeList,const sptr<IThermalTempCallback> & callback)87 void ThermalObserver::SubscribeThermalTempCallback(const std::vector<std::string> &typeList,
88 const sptr<IThermalTempCallback>& callback)
89 {
90 THERMAL_HILOGD(COMP_SVC, "Enter");
91 std::lock_guard<std::mutex> lock(mutexTempCallback_);
92 THERMAL_RETURN_IF(callback == nullptr);
93 auto object = callback->AsObject();
94 THERMAL_RETURN_IF(object == nullptr);
95 auto retIt = sensorTempListeners_.insert(callback);
96 if (retIt.second) {
97 object->AddDeathRecipient(sensorTempCBDeathRecipient_);
98 }
99 callbackTypeMap_.insert(std::make_pair(callback, typeList));
100 THERMAL_HILOGD(COMP_SVC,
101 "object=%{public}p, callback=%{public}p, listeners.size=%{public}d, insertOk=%{public}d",
102 object.GetRefPtr(), callback.GetRefPtr(),
103 static_cast<unsigned int>(sensorTempListeners_.size()), retIt.second);
104 }
105
UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback> & callback)106 void ThermalObserver::UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback)
107 {
108 THERMAL_HILOGD(COMP_SVC, "Enter");
109 std::lock_guard lock(mutexTempCallback_);
110 THERMAL_RETURN_IF(callback == nullptr);
111 auto object = callback->AsObject();
112 THERMAL_RETURN_IF(object == nullptr);
113 auto callbackIter = callbackTypeMap_.find(callback);
114 if (callbackIter != callbackTypeMap_.end()) {
115 callbackTypeMap_.erase(callbackIter);
116 }
117 size_t eraseNum = sensorTempListeners_.erase(callback);
118 if (eraseNum != 0) {
119 object->RemoveDeathRecipient(sensorTempCBDeathRecipient_);
120 }
121 THERMAL_HILOGD(COMP_SVC,
122 "object=%{public}p, callback=%{public}p, listeners.size=%{public}d, eraseNum=%{public}zu",
123 object.GetRefPtr(), callback.GetRefPtr(), static_cast<unsigned int>(sensorTempListeners_.size()), eraseNum);
124 }
125
NotifySensorTempChanged(IThermalTempCallback::TempCallbackMap & tempCbMap)126 void ThermalObserver::NotifySensorTempChanged(IThermalTempCallback::TempCallbackMap &tempCbMap)
127 {
128 std::lock_guard lockTempCallback(mutexTempCallback_);
129 static std::map<std::string, int32_t> preSensor;
130 IThermalTempCallback::TempCallbackMap newTempCbMap;
131 if (sensorTempListeners_.empty()) return;
132 for (auto& listener : sensorTempListeners_) {
133 auto callbackIter = callbackTypeMap_.find(listener);
134 if (callbackIter != callbackTypeMap_.end()) {
135 THERMAL_HILOGD(COMP_SVC, "find callback");
136 for (auto type : callbackIter->second) {
137 std::lock_guard lockCallbackInfo(mutexCallbackInfo_);
138 if (preSensor[type] != tempCbMap[type]) {
139 newTempCbMap.insert(std::make_pair(type, tempCbMap[type]));
140 preSensor[type] = tempCbMap[type];
141 }
142 }
143 }
144 listener->OnThermalTempChanged(newTempCbMap);
145 }
146 }
147
OnReceivedSensorInfo(const TypeTempMap & info)148 void ThermalObserver::OnReceivedSensorInfo(const TypeTempMap &info)
149 {
150 {
151 std::lock_guard lock(mutexCallbackInfo_);
152 callbackinfo_ = info;
153 }
154
155 if (callback_ != nullptr) {
156 callback_(callbackinfo_);
157 }
158
159 NotifySensorTempChanged(callbackinfo_);
160 }
161
GetThermalSrvSensorInfo(const SensorType & type,ThermalSrvSensorInfo & sensorInfo)162 bool ThermalObserver::GetThermalSrvSensorInfo(const SensorType &type, ThermalSrvSensorInfo& sensorInfo)
163 {
164 std::lock_guard lock(mutexCallbackInfo_);
165 auto iter = callbackinfo_.find(typeMap_[type]);
166 if (iter != callbackinfo_.end()) {
167 THERMAL_HILOGD(COMP_SVC, "set temp for sensor");
168 sensorInfo.SetType(typeMap_[type]);
169 if (iter->second == INVALID_TEMP) {
170 return false;
171 } else {
172 sensorInfo.SetTemp(iter->second);
173 }
174 return true;
175 } else {
176 THERMAL_HILOGD(COMP_SVC, "set invalid temp for sensor");
177 sensorInfo.SetType(typeMap_[type]);
178 sensorInfo.SetTemp(INVALID_TEMP);
179 }
180 return false;
181 }
182
GetTemp(const SensorType & type)183 int32_t ThermalObserver::GetTemp(const SensorType &type)
184 {
185 ThermalSrvSensorInfo info;
186 GetThermalSrvSensorInfo(type, info);
187 return info.GetTemp();
188 }
189
OnRemoteDied(const wptr<IRemoteObject> & remote)190 void ThermalObserver::SensorTempCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
191 {
192 THERMAL_HILOGD(COMP_SVC, "Enter");
193 if (remote == nullptr || remote.promote() == nullptr) {
194 return;
195 }
196 THERMAL_HILOGD(COMP_SVC, "ThermalSensorTemp::OnRemoteDied remote = %{public}p", remote.promote().GetRefPtr());
197 auto pms = DelayedSpSingleton<ThermalService>::GetInstance();
198 if (pms == nullptr) {
199 return;
200 }
201
202 auto handler = pms->GetHandler();
203 if (handler == nullptr) {
204 return;
205 }
206 sptr<IThermalTempCallback> callback = iface_cast<IThermalTempCallback>(remote.promote());
207 std::function<void ()> unRegFunc = std::bind(&ThermalService::UnSubscribeThermalTempCallback, pms, callback);
208 handler->PostTask(unRegFunc, TASK_UNREG_SENSOR_TEMP_CALLBACK);
209 }
210 } // namespace PowerMgr
211 } // namespace OHOS
212