summaryrefslogtreecommitdiffstats
path: root/hal/linux/UPowerClient.cpp
blob: d5f5e1ca52688a785dec28958d7e3d803736a351 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Hal.h"
#include "HalLog.h"
#include <mozilla/Attributes.h>
#include <mozilla/dom/battery/Constants.h>
#include "mozilla/GRefPtr.h"
#include "mozilla/GUniquePtr.h"
#include <cmath>
#include <gio/gio.h>
#include "mozilla/widget/AsyncDBus.h"

using namespace mozilla::widget;
using namespace mozilla::dom::battery;

namespace mozilla::hal_impl {

/**
 * This is the declaration of UPowerClient class. This class is listening and
 * communicating to upower daemon through DBus.
 * There is no header file because this class shouldn't be public.
 */
class UPowerClient {
 public:
  static UPowerClient* GetInstance();

  void BeginListening();
  void StopListening();

  double GetLevel();
  bool IsCharging();
  double GetRemainingTime();

  ~UPowerClient();

 private:
  UPowerClient();

  enum States {
    eState_Unknown = 0,
    eState_Charging,
    eState_Discharging,
    eState_Empty,
    eState_FullyCharged,
    eState_PendingCharge,
    eState_PendingDischarge
  };

  /**
   * Update the currently tracked device.
   */
  void UpdateTrackedDevices();

  /**
   * Update the battery info.
   */
  bool GetBatteryInfo();

  /**
   * Watch battery device for status
   */
  bool AddTrackedDevice(const char* devicePath);

  /**
   * Callback used by 'DeviceChanged' signal.
   */
  static void DeviceChanged(GDBusProxy* aProxy, gchar* aSenderName,
                            gchar* aSignalName, GVariant* aParameters,
                            UPowerClient* aListener);

  /**
   * Callback used by 'PropertiesChanged' signal.
   * This method is called when the the battery level changes.
   * (Only with upower >= 0.99)
   */
  static void DevicePropertiesChanged(GDBusProxy* aProxy, gchar* aSenderName,
                                      gchar* aSignalName, GVariant* aParameters,
                                      UPowerClient* aListener);

  RefPtr<GCancellable> mCancellable;

  // The DBus proxy object to upower.
  RefPtr<GDBusProxy> mUPowerProxy;

  // The path of the tracked device.
  GUniquePtr<gchar> mTrackedDevice;

  // The DBusGProxy for the tracked device.
  RefPtr<GDBusProxy> mTrackedDeviceProxy;

  double mLevel;
  bool mCharging;
  double mRemainingTime;

  static UPowerClient* sInstance;

  static const guint sDeviceTypeBattery = 2;
  static const guint64 kUPowerUnknownRemainingTime = 0;
};

/*
 * Implementation of mozilla::hal_impl::EnableBatteryNotifications,
 *                   mozilla::hal_impl::DisableBatteryNotifications,
 *               and mozilla::hal_impl::GetCurrentBatteryInformation.
 */

void EnableBatteryNotifications() {
  UPowerClient::GetInstance()->BeginListening();
}

void DisableBatteryNotifications() {
  UPowerClient::GetInstance()->StopListening();
}

void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) {
  UPowerClient* upowerClient = UPowerClient::GetInstance();

  aBatteryInfo->level() = upowerClient->GetLevel();
  aBatteryInfo->charging() = upowerClient->IsCharging();
  aBatteryInfo->remainingTime() = upowerClient->GetRemainingTime();
}

/*
 * Following is the implementation of UPowerClient.
 */

UPowerClient* UPowerClient::sInstance = nullptr;

/* static */
UPowerClient* UPowerClient::GetInstance() {
  if (!sInstance) {
    sInstance = new UPowerClient();
  }

  return sInstance;
}

UPowerClient::UPowerClient()
    : mLevel(kDefaultLevel),
      mCharging(kDefaultCharging),
      mRemainingTime(kDefaultRemainingTime) {}

UPowerClient::~UPowerClient() {
  NS_ASSERTION(
      !mUPowerProxy && !mTrackedDevice && !mTrackedDeviceProxy && !mCancellable,
      "The observers have not been correctly removed! "
      "(StopListening should have been called)");
}

void UPowerClient::BeginListening() {
  GUniquePtr<GError> error;

  mCancellable = dont_AddRef(g_cancellable_new());
  CreateDBusProxyForBus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
                        /* aInterfaceInfo = */ nullptr,
                        "org.freedesktop.UPower", "/org/freedesktop/UPower",
                        "org.freedesktop.UPower", mCancellable)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          // It's safe to capture this as we use mCancellable to stop
          // listening.
          [this](RefPtr<GDBusProxy>&& aProxy) {
            mUPowerProxy = std::move(aProxy);
            UpdateTrackedDevices();
          },
          [](GUniquePtr<GError>&& aError) {
            if (!g_error_matches(aError.get(), G_IO_ERROR,
                                 G_IO_ERROR_CANCELLED)) {
              g_warning(
                  "Failed to create DBus proxy for org.freedesktop.UPower: "
                  "%s\n",
                  aError->message);
            }
          });
}

void UPowerClient::StopListening() {
  if (mUPowerProxy) {
    g_signal_handlers_disconnect_by_func(mUPowerProxy, (void*)DeviceChanged,
                                         this);
  }
  if (mCancellable) {
    g_cancellable_cancel(mCancellable);
    mCancellable = nullptr;
  }

  mTrackedDeviceProxy = nullptr;
  mTrackedDevice = nullptr;
  mUPowerProxy = nullptr;

  // We should now show the default values, not the latest we got.
  mLevel = kDefaultLevel;
  mCharging = kDefaultCharging;
  mRemainingTime = kDefaultRemainingTime;
}

bool UPowerClient::AddTrackedDevice(const char* aDevicePath) {
  RefPtr<GDBusProxy> proxy = dont_AddRef(g_dbus_proxy_new_for_bus_sync(
      G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, nullptr,
      "org.freedesktop.UPower", aDevicePath, "org.freedesktop.UPower.Device",
      mCancellable, nullptr));
  if (!proxy) {
    return false;
  }

  RefPtr<GVariant> deviceType =
      dont_AddRef(g_dbus_proxy_get_cached_property(proxy, "Type"));
  if (NS_WARN_IF(!deviceType ||
                 !g_variant_is_of_type(deviceType, G_VARIANT_TYPE_UINT32))) {
    return false;
  }

  if (g_variant_get_uint32(deviceType) != sDeviceTypeBattery) {
    return false;
  }

  GUniquePtr<gchar> device(g_strdup(aDevicePath));
  mTrackedDevice = std::move(device);
  mTrackedDeviceProxy = std::move(proxy);

  if (!GetBatteryInfo()) {
    return false;
  }
  hal::NotifyBatteryChange(
      hal::BatteryInformation(mLevel, mCharging, mRemainingTime));

  g_signal_connect(mTrackedDeviceProxy, "g-signal",
                   G_CALLBACK(DevicePropertiesChanged), this);
  return true;
}

void UPowerClient::UpdateTrackedDevices() {
  // Reset the current tracked device:
  g_signal_handlers_disconnect_by_func(mUPowerProxy, (void*)DeviceChanged,
                                       this);

  mTrackedDevice = nullptr;
  mTrackedDeviceProxy = nullptr;

  DBusProxyCall(mUPowerProxy, "EnumerateDevices", nullptr,
                G_DBUS_CALL_FLAGS_NONE, -1, mCancellable)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          // It's safe to capture this as we use mCancellable to stop
          // listening.
          [this](RefPtr<GVariant>&& aResult) {
            RefPtr<GVariant> variant =
                dont_AddRef(g_variant_get_child_value(aResult.get(), 0));
            if (!variant || !g_variant_is_of_type(
                                variant, G_VARIANT_TYPE_OBJECT_PATH_ARRAY)) {
              g_warning(
                  "Failed to enumerate devices of org.freedesktop.UPower: "
                  "wrong param %s\n",
                  g_variant_get_type_string(aResult.get()));
              return;
            }
            gsize num = g_variant_n_children(variant);
            for (gsize i = 0; i < num; i++) {
              const char* devicePath = g_variant_get_string(
                  g_variant_get_child_value(variant, i), nullptr);
              if (!devicePath) {
                g_warning(
                    "Failed to enumerate devices of org.freedesktop.UPower: "
                    "missing device?\n");
                return;
              }
              /*
               * We are looking for the first device that is a battery.
               * TODO: we could try to combine more than one battery.
               */
              if (AddTrackedDevice(devicePath)) {
                break;
              }
            }
            g_signal_connect(mUPowerProxy, "g-signal",
                             G_CALLBACK(DeviceChanged), this);
          },
          [this](GUniquePtr<GError>&& aError) {
            if (!g_error_matches(aError.get(), G_IO_ERROR,
                                 G_IO_ERROR_CANCELLED)) {
              g_warning(
                  "Failed to enumerate devices of org.freedesktop.UPower: %s\n",
                  aError->message);
            }
            g_signal_connect(mUPowerProxy, "g-signal",
                             G_CALLBACK(DeviceChanged), this);
          });
}

/* static */
void UPowerClient::DeviceChanged(GDBusProxy* aProxy, gchar* aSenderName,
                                 gchar* aSignalName, GVariant* aParameters,
                                 UPowerClient* aListener) {
  // Added new device. Act only if we're missing any tracked device
  if (!g_strcmp0(aSignalName, "DeviceAdded")) {
    if (aListener->mTrackedDevice) {
      return;
    }
  } else if (!g_strcmp0(aSignalName, "DeviceRemoved")) {
    if (g_strcmp0(aSenderName, aListener->mTrackedDevice.get())) {
      return;
    }
  }
  aListener->UpdateTrackedDevices();
}

/* static */
void UPowerClient::DevicePropertiesChanged(GDBusProxy* aProxy,
                                           gchar* aSenderName,
                                           gchar* aSignalName,
                                           GVariant* aParameters,
                                           UPowerClient* aListener) {
  if (aListener->GetBatteryInfo()) {
    hal::NotifyBatteryChange(hal::BatteryInformation(
        sInstance->mLevel, sInstance->mCharging, sInstance->mRemainingTime));
  }
}

bool UPowerClient::GetBatteryInfo() {
  bool isFull = false;

  /*
   * State values are confusing...
   * First of all, after looking at upower sources (0.9.13), it seems that
   * PendingDischarge and PendingCharge are not used.
   * In addition, FullyCharged and Empty states are not clear because we do not
   * know if the battery is actually charging or not. Those values come directly
   * from sysfs (in the Linux kernel) which have four states: "Empty", "Full",
   * "Charging" and "Discharging". In sysfs, "Empty" and "Full" are also only
   * related to the level, not to the charging state.
   * In this code, we are going to assume that Full means charging and Empty
   * means discharging because if that is not the case, the state should not
   * last a long time (actually, it should disappear at the following update).
   * It might be even very hard to see real cases where the state is Empty and
   * the battery is charging or the state is Full and the battery is discharging
   * given that plugging/unplugging the battery should have an impact on the
   * level.
   */

  if (!mTrackedDeviceProxy) {
    return false;
  }

  RefPtr<GVariant> value = dont_AddRef(
      g_dbus_proxy_get_cached_property(mTrackedDeviceProxy, "State"));
  if (NS_WARN_IF(!value ||
                 !g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32))) {
    return false;
  }

  switch (g_variant_get_uint32(value)) {
    case eState_Unknown:
      mCharging = kDefaultCharging;
      break;
    case eState_FullyCharged:
      isFull = true;
      [[fallthrough]];
    case eState_Charging:
    case eState_PendingCharge:
      mCharging = true;
      break;
    case eState_Discharging:
    case eState_Empty:
    case eState_PendingDischarge:
      mCharging = false;
      break;
  }

  /*
   * The battery level might be very close to 100% (like 99%) without
   * increasing. It seems that upower sets the battery state as 'full' in that
   * case so we should trust it and not even try to get the value.
   */
  if (isFull) {
    mLevel = 1.0;
  } else {
    value = dont_AddRef(
        g_dbus_proxy_get_cached_property(mTrackedDeviceProxy, "Percentage"));
    if (NS_WARN_IF(!value ||
                   !g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE))) {
      return false;
    }
    mLevel = round(g_variant_get_double(value)) * 0.01;
  }

  if (isFull) {
    mRemainingTime = 0;
  } else {
    value = dont_AddRef(g_dbus_proxy_get_cached_property(
        mTrackedDeviceProxy, mCharging ? "TimeToFull" : "TimeToEmpty"));
    if (NS_WARN_IF(!value ||
                   !g_variant_is_of_type(value, G_VARIANT_TYPE_INT64))) {
      return false;
    }
    mRemainingTime = g_variant_get_int64(value);
    if (mRemainingTime == kUPowerUnknownRemainingTime) {
      mRemainingTime = kUnknownRemainingTime;
    }
  }
  return true;
}

double UPowerClient::GetLevel() { return mLevel; }

bool UPowerClient::IsCharging() { return mCharging; }

double UPowerClient::GetRemainingTime() { return mRemainingTime; }

}  // namespace mozilla::hal_impl