| // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef THERMALD_TEMPERATURE_SENSOR_MONITOR_H_ |
| #define THERMALD_TEMPERATURE_SENSOR_MONITOR_H_ |
| |
| #include <map> |
| #include <memory> |
| #include <string> |
| |
| #include "base/callback.h" |
| #include "base/callback_list.h" |
| #include "base/macros.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/time/time.h" |
| #include "base/timer/timer.h" |
| |
| #include "thermald/temperature_monitor_interface.h" |
| #include "thermald/temperature_sensor_interface.h" |
| |
| namespace thermald { |
| |
| // Periodically reads a temperature sensor at a configurable rate and |
| // provides an interface to subscribe for notification upon temperature |
| // changes. |
| // |
| // Subscribers are only notified about changes in the temperature, not every |
| // time the sensor is read. The monitoring of the sensors is only performed |
| // when at least one subscription is active. |
| // |
| // The TemperatureSensorMonitor is based on the libbase event model. A |
| // TemperatureSensorMonitor instance must not be used in multiple threads |
| // of execution. |
| class TemperatureSensorMonitor : public TemperatureMonitorInterface { |
| public: |
| // Does not take ownership of the sensor. |
| TemperatureSensorMonitor(TemperatureSensorInterface *sensor, |
| base::TimeDelta sample_period); |
| virtual ~TemperatureSensorMonitor(); |
| |
| std::string name() const; |
| |
| #if BASE_VER < 860220 |
| std::unique_ptr<TemperatureMonitorInterface::Subscription> |
| #else |
| base::CallbackListSubscription |
| #endif |
| Subscribe(const TemperatureMonitorCallback &cb); |
| |
| private: |
| friend class TemperatureSensorMonitorTest; |
| |
| void StartSampling(); |
| void StopSampling(); |
| |
| // Invoked whenever a subscriber is removed. |
| void OnSubscriberRemoved(); |
| |
| // Read the temperature sensor and reports changes to all subscribers. |
| void ReadAndReportTemperature(); |
| |
| // Report the current/latest temperature to a new subscriber. |
| void ReportInitialTemperature(const TemperatureMonitorCallback &cb, |
| int temperature); |
| |
| TemperatureSensorInterface *sensor_; |
| const base::TimeDelta sample_period_; |
| |
| base::WeakPtrFactory<TemperatureSensorMonitor> weak_ptr_factory_; |
| |
| base::CallbackList<void(int)> subscribers_; |
| int prev_temperature_; |
| bool sensor_was_operational_; |
| |
| // Timer for periodic sampling. |
| // |
| // The Timer class is used instead of RepeatingTimer to facilitate testing |
| // through injection of a MockTimer. |
| std::unique_ptr<base::RepeatingTimer> timer_; |
| |
| DISALLOW_COPY_AND_ASSIGN(TemperatureSensorMonitor); |
| }; |
| |
| } // namespace thermald |
| |
| #endif // THERMALD_TEMPERATURE_SENSOR_MONITOR_H_ |