File: thermal.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (304 lines) | stat: -rw-r--r-- 9,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "thermal"

#include <cerrno>
#include <thread>
#include <limits>

#include <android/thermal.h>
#include <android/os/BnThermalStatusListener.h>
#include <android/os/IThermalService.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>

using android::sp;

using namespace android;
using namespace android::os;

struct ThermalServiceListener : public BnThermalStatusListener {
    public:
        virtual binder::Status onStatusChange(int32_t status) override;
        ThermalServiceListener(AThermalManager *manager) {mMgr = manager;}
    private:
        AThermalManager *mMgr;
};

struct ListenerCallback {
    AThermal_StatusCallback callback;
    void* data;
};

struct AThermalManager {
   public:
        static AThermalManager* createAThermalManager();
        AThermalManager() = delete;
        ~AThermalManager();
        status_t notifyStateChange(int32_t status);
        status_t getCurrentThermalStatus(int32_t *status);
        status_t addListener(AThermal_StatusCallback, void *data);
        status_t removeListener(AThermal_StatusCallback, void *data);
        status_t getThermalHeadroom(int32_t forecastSeconds, float *result);
   private:
       AThermalManager(sp<IThermalService> service);
       sp<IThermalService> mThermalSvc;
       sp<ThermalServiceListener> mServiceListener;
       std::vector<ListenerCallback> mListeners;
       std::mutex mMutex;
};

binder::Status ThermalServiceListener::onStatusChange(int32_t status) {
    if (mMgr != nullptr) {
        mMgr->notifyStateChange(status);
    }
    return binder::Status::ok();
}

AThermalManager* AThermalManager::createAThermalManager() {
    sp<IBinder> binder =
            defaultServiceManager()->checkService(String16("thermalservice"));

    if (binder == nullptr) {
        ALOGE("%s: Thermal service is not ready ", __FUNCTION__);
        return nullptr;
    }
    return new AThermalManager(interface_cast<IThermalService>(binder));
}

AThermalManager::AThermalManager(sp<IThermalService> service)
    : mThermalSvc(service),
      mServiceListener(nullptr) {
}

AThermalManager::~AThermalManager() {
    std::unique_lock<std::mutex> lock(mMutex);

    mListeners.clear();
    if (mServiceListener != nullptr) {
        bool success = false;
        mThermalSvc->unregisterThermalStatusListener(mServiceListener, &success);
        mServiceListener = nullptr;
    }
}

status_t AThermalManager::notifyStateChange(int32_t status) {
    std::unique_lock<std::mutex> lock(mMutex);
    AThermalStatus thermalStatus = static_cast<AThermalStatus>(status);

    for (auto listener : mListeners) {
        listener.callback(listener.data, thermalStatus);
    }
    return OK;
}

status_t AThermalManager::addListener(AThermal_StatusCallback callback, void *data) {
    std::unique_lock<std::mutex> lock(mMutex);

    if (callback == nullptr) {
        // Callback can not be nullptr
        return EINVAL;
    }
    for (const auto& cb : mListeners) {
        // Don't re-add callbacks.
        if (callback == cb.callback && data == cb.data) {
            return EINVAL;
        }
    }
    mListeners.emplace_back(ListenerCallback{callback, data});

    if (mServiceListener != nullptr) {
        return OK;
    }
    bool success = false;
    mServiceListener = new ThermalServiceListener(this);
    if (mServiceListener == nullptr) {
        return ENOMEM;
    }
    auto ret = mThermalSvc->registerThermalStatusListener(mServiceListener, &success);
    if (!success || !ret.isOk()) {
        ALOGE("Failed in registerThermalStatusListener %d", success);
        if (ret.exceptionCode() == binder::Status::EX_SECURITY) {
            return EPERM;
        }
        return EPIPE;
    }
    return OK;
}

status_t AThermalManager::removeListener(AThermal_StatusCallback callback, void *data) {
    std::unique_lock<std::mutex> lock(mMutex);

    auto it = std::remove_if(mListeners.begin(),
                             mListeners.end(),
                             [&](const ListenerCallback& cb) {
                                    return callback == cb.callback &&
                                           data == cb.data;
                             });
    if (it == mListeners.end()) {
        // If the listener and data pointer were not previously added.
        return EINVAL;
    }
    mListeners.erase(it, mListeners.end());

    if (!mListeners.empty()) {
        return OK;
    }
    if (mServiceListener == nullptr) {
        return OK;
    }
    bool success = false;
    auto ret = mThermalSvc->unregisterThermalStatusListener(mServiceListener, &success);
    if (!success || !ret.isOk()) {
        ALOGE("Failed in unregisterThermalStatusListener %d", success);
        if (ret.exceptionCode() == binder::Status::EX_SECURITY) {
            return EPERM;
        }
        return EPIPE;
    }
    mServiceListener = nullptr;
    return OK;
}

status_t AThermalManager::getCurrentThermalStatus(int32_t *status) {
    binder::Status ret = mThermalSvc->getCurrentThermalStatus(status);

    if (!ret.isOk()) {
        if (ret.exceptionCode() == binder::Status::EX_SECURITY) {
            return EPERM;
        }
        return EPIPE;
    }
    return OK;
}

status_t AThermalManager::getThermalHeadroom(int32_t forecastSeconds, float *result) {
    binder::Status ret = mThermalSvc->getThermalHeadroom(forecastSeconds, result);

    if (!ret.isOk()) {
        if (ret.exceptionCode() == binder::Status::EX_SECURITY) {
            return EPERM;
        }
        return EPIPE;
    }
    return OK;
}

/**
  * Acquire an instance of the thermal manager. This must be freed using
  * {@link AThermal_releaseManager}.
  *
  * @return manager instance on success, nullptr on failure.
 */
AThermalManager* AThermal_acquireManager() {
    auto manager = AThermalManager::createAThermalManager();

    return manager;
}

/**
 * Release the thermal manager pointer acquired by
 * {@link AThermal_acquireManager}.
 *
 * @param manager The manager to be released.
 *
 */
void AThermal_releaseManager(AThermalManager *manager) {
    delete manager;
}

/**
  * Gets the current thermal status.
  *
  * @param manager The manager instance to use to query the thermal status,
  * acquired by {@link AThermal_acquireManager}.
  *
  * @return current thermal status, ATHERMAL_STATUS_ERROR on failure.
*/
AThermalStatus AThermal_getCurrentThermalStatus(AThermalManager *manager) {
    int32_t status = 0;
    status_t ret = manager->getCurrentThermalStatus(&status);
    if (ret != OK) {
        return AThermalStatus::ATHERMAL_STATUS_ERROR;
    }
    return static_cast<AThermalStatus>(status);
}

/**
 * Register the thermal status listener for thermal status change.
 *
 * @param manager The manager instance to use to register.
 * acquired by {@link AThermal_acquireManager}.
 * @param callback The callback function to be called when thermal status updated.
 * @param data The data pointer to be passed when callback is called.
 *
 * @return 0 on success
 *         EINVAL if the listener and data pointer were previously added and not removed.
 *         EPERM if the required permission is not held.
 *         EPIPE if communication with the system service has failed.
 */
int AThermal_registerThermalStatusListener(AThermalManager *manager,
        AThermal_StatusCallback callback, void *data) {
    return manager->addListener(callback, data);
}

/**
 * Unregister the thermal status listener previously resgistered.
 *
 * @param manager The manager instance to use to unregister.
 * acquired by {@link AThermal_acquireManager}.
 * @param callback The callback function to be called when thermal status updated.
 * @param data The data pointer to be passed when callback is called.
 *
 * @return 0 on success
 *         EINVAL if the listener and data pointer were not previously added.
 *         EPERM if the required permission is not held.
 *         EPIPE if communication with the system service has failed.
 */
int AThermal_unregisterThermalStatusListener(AThermalManager *manager,
        AThermal_StatusCallback callback, void *data) {
    return manager->removeListener(callback, data);
}

/**
 * Provides an estimate of how much thermal headroom the device currently has
 * before hitting severe throttling.
 *
 * Note that this only attempts to track the headroom of slow-moving sensors,
 * such as the skin temperature sensor. This means that there is no benefit to
 * calling this function more frequently than about once per second, and attempts
 * to call significantly more frequently may result in the function returning {@code NaN}.
 *
 * See also PowerManager#getThermalHeadroom.
 *
 * @param manager The manager instance to use
 * @param forecastSeconds how many seconds in the future to forecast
 * @return a value greater than or equal to 0.0 where 1.0 indicates the SEVERE throttling
 *  	   threshold. Returns NaN if the device does not support this functionality or if
 * 	       this function is called significantly faster than once per second.
 */
float AThermal_getThermalHeadroom(AThermalManager *manager,
        int forecastSeconds) {
    float result = 0.0f;
    status_t ret = manager->getThermalHeadroom(forecastSeconds, &result);

    if (ret != OK) {
        result = std::numeric_limits<float>::quiet_NaN();
    }

    return result;
}