Merge branch 'battery' into 'master'
Add MceBattery and MceCharger objects See merge request mer-core/libmce-glib!4
This commit is contained in:
2
Makefile
2
Makefile
@@ -38,6 +38,8 @@ LIB = $(LIB_SONAME).$(VERSION_MINOR).$(VERSION_RELEASE)
|
||||
#
|
||||
|
||||
SRC = \
|
||||
mce_battery.c \
|
||||
mce_charger.c \
|
||||
mce_display.c \
|
||||
mce_proxy.c \
|
||||
mce_tklock.c
|
||||
|
||||
119
include/mce_battery.h
Normal file
119
include/mce_battery.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Jolla Ltd.
|
||||
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
|
||||
*
|
||||
* You may use this file under the terms of BSD license as follows:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the names of the copyright holders nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef MCE_BATTERY_H
|
||||
#define MCE_BATTERY_H
|
||||
|
||||
#include "mce_types.h"
|
||||
|
||||
/* Since 1.0.6 */
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef enum mce_battery_status {
|
||||
MCE_BATTERY_UNKNOWN,
|
||||
MCE_BATTERY_EMPTY,
|
||||
MCE_BATTERY_LOW,
|
||||
MCE_BATTERY_OK,
|
||||
MCE_BATTERY_FULL
|
||||
} MCE_BATTERY_STATUS;
|
||||
|
||||
typedef struct mce_battery_priv MceBatteryPriv;
|
||||
|
||||
typedef struct mce_battery {
|
||||
GObject object;
|
||||
MceBatteryPriv* priv;
|
||||
gboolean valid;
|
||||
guint level;
|
||||
MCE_BATTERY_STATUS status;
|
||||
} MceBattery;
|
||||
|
||||
typedef void
|
||||
(*MceBatteryFunc)(
|
||||
MceBattery* battery,
|
||||
void* arg);
|
||||
|
||||
MceBattery*
|
||||
mce_battery_new(
|
||||
void);
|
||||
|
||||
MceBattery*
|
||||
mce_battery_ref(
|
||||
MceBattery* battery);
|
||||
|
||||
void
|
||||
mce_battery_unref(
|
||||
MceBattery* battery);
|
||||
|
||||
gulong
|
||||
mce_battery_add_valid_changed_handler(
|
||||
MceBattery* battery,
|
||||
MceBatteryFunc fn,
|
||||
void* arg);
|
||||
|
||||
gulong
|
||||
mce_battery_add_level_changed_handler(
|
||||
MceBattery* battery,
|
||||
MceBatteryFunc fn,
|
||||
void* arg);
|
||||
|
||||
gulong
|
||||
mce_battery_add_status_changed_handler(
|
||||
MceBattery* battery,
|
||||
MceBatteryFunc fn,
|
||||
void* arg);
|
||||
|
||||
void
|
||||
mce_battery_remove_handler(
|
||||
MceBattery* battery,
|
||||
gulong id);
|
||||
|
||||
void
|
||||
mce_battery_remove_handlers(
|
||||
MceBattery* battery,
|
||||
gulong *ids,
|
||||
guint count);
|
||||
|
||||
#define mce_battery_remove_all_handlers(d, ids) \
|
||||
mce_battery_remove_handlers(d, ids, G_N_ELEMENTS(ids))
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* MCE_BATTERY_H */
|
||||
|
||||
/*
|
||||
* Local Variables:
|
||||
* mode: C
|
||||
* c-basic-offset: 4
|
||||
* indent-tabs-mode: nil
|
||||
* End:
|
||||
*/
|
||||
110
include/mce_charger.h
Normal file
110
include/mce_charger.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Jolla Ltd.
|
||||
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
|
||||
*
|
||||
* You may use this file under the terms of BSD license as follows:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the names of the copyright holders nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef MCE_CHARGER_H
|
||||
#define MCE_CHARGER_H
|
||||
|
||||
#include "mce_types.h"
|
||||
|
||||
/* Since 1.0.6 */
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef enum mce_charger_state {
|
||||
MCE_CHARGER_UNKNOWN,
|
||||
MCE_CHARGER_ON,
|
||||
MCE_CHARGER_OFF
|
||||
} MCE_CHARGER_STATE;
|
||||
|
||||
typedef struct mce_charger_priv MceChargerPriv;
|
||||
|
||||
typedef struct mce_charger {
|
||||
GObject object;
|
||||
MceChargerPriv* priv;
|
||||
gboolean valid;
|
||||
MCE_CHARGER_STATE state;
|
||||
} MceCharger;
|
||||
|
||||
typedef void
|
||||
(*MceChargerFunc)(
|
||||
MceCharger* charger,
|
||||
void* arg);
|
||||
|
||||
MceCharger*
|
||||
mce_charger_new(
|
||||
void);
|
||||
|
||||
MceCharger*
|
||||
mce_charger_ref(
|
||||
MceCharger* charger);
|
||||
|
||||
void
|
||||
mce_charger_unref(
|
||||
MceCharger* charger);
|
||||
|
||||
gulong
|
||||
mce_charger_add_valid_changed_handler(
|
||||
MceCharger* charger,
|
||||
MceChargerFunc fn,
|
||||
void* arg);
|
||||
|
||||
gulong
|
||||
mce_charger_add_state_changed_handler(
|
||||
MceCharger* charger,
|
||||
MceChargerFunc fn,
|
||||
void* arg);
|
||||
|
||||
void
|
||||
mce_charger_remove_handler(
|
||||
MceCharger* charger,
|
||||
gulong id);
|
||||
|
||||
void
|
||||
mce_charger_remove_handlers(
|
||||
MceCharger* charger,
|
||||
gulong *ids,
|
||||
guint count);
|
||||
|
||||
#define mce_charger_remove_all_handlers(d, ids) \
|
||||
mce_charger_remove_handlers(d, ids, G_N_ELEMENTS(ids))
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* MCE_CHARGER_H */
|
||||
|
||||
/*
|
||||
* Local Variables:
|
||||
* mode: C
|
||||
* c-basic-offset: 4
|
||||
* indent-tabs-mode: nil
|
||||
* End:
|
||||
*/
|
||||
@@ -10,5 +10,14 @@
|
||||
<method name="get_tklock_mode">
|
||||
<arg direction="out" name="mode_name" type="s"/>
|
||||
</method>
|
||||
<method name="get_battery_level">
|
||||
<arg direction="out" name="battery_level" type="i"/>
|
||||
</method>
|
||||
<method name="get_battery_status">
|
||||
<arg direction="out" name="battery_status" type="s"/>
|
||||
</method>
|
||||
<method name="get_charger_state">
|
||||
<arg direction="out" name="charger_state" type="s"/>
|
||||
</method>
|
||||
</interface>
|
||||
</node>
|
||||
|
||||
@@ -10,5 +10,14 @@
|
||||
<signal name="tklock_mode_ind">
|
||||
<arg name="tklock_mode" type="s"/>
|
||||
</signal>
|
||||
<signal name="battery_level_ind">
|
||||
<arg name="battery_level" type="i"/>
|
||||
</signal>
|
||||
<signal name="battery_status_ind">
|
||||
<arg name="battery_status" type="s"/>
|
||||
</signal>
|
||||
<signal name="charger_state_ind">
|
||||
<arg name="charger_state" type="s"/>
|
||||
</signal>
|
||||
</interface>
|
||||
</node>
|
||||
|
||||
424
src/mce_battery.c
Normal file
424
src/mce_battery.c
Normal file
@@ -0,0 +1,424 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Jolla Ltd.
|
||||
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
|
||||
*
|
||||
* You may use this file under the terms of BSD license as follows:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the names of the copyright holders nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "mce_battery.h"
|
||||
#include "mce_proxy.h"
|
||||
#include "mce_log_p.h"
|
||||
|
||||
#include <mce/dbus-names.h>
|
||||
#include <mce/mode-names.h>
|
||||
|
||||
#include <gutil_misc.h>
|
||||
|
||||
/* Generated headers */
|
||||
#include "com.nokia.mce.request.h"
|
||||
#include "com.nokia.mce.signal.h"
|
||||
|
||||
enum mce_battery_ind {
|
||||
BATTERY_IND_LEVEL,
|
||||
BATTERY_IND_STATUS,
|
||||
BATTERY_IND_COUNT
|
||||
};
|
||||
|
||||
typedef enum mce_battery_flags {
|
||||
BATTERY_HAVE_NONE = 0x00,
|
||||
BATTERY_HAVE_LEVEL = 0x01,
|
||||
BATTERY_HAVE_STATUS = 0x02
|
||||
} BATTERY_FLAGS;
|
||||
|
||||
#define BATTERY_HAVE_ALL (BATTERY_HAVE_LEVEL | BATTERY_HAVE_STATUS)
|
||||
|
||||
struct mce_battery_priv {
|
||||
MceProxy* proxy;
|
||||
BATTERY_FLAGS flags;
|
||||
gulong proxy_valid_id;
|
||||
gulong battery_ind_id[BATTERY_IND_COUNT];
|
||||
};
|
||||
|
||||
enum mce_battery_signal {
|
||||
SIGNAL_VALID_CHANGED,
|
||||
SIGNAL_LEVEL_CHANGED,
|
||||
SIGNAL_STATUS_CHANGED,
|
||||
SIGNAL_COUNT
|
||||
};
|
||||
|
||||
#define SIGNAL_VALID_CHANGED_NAME "mce-battery-valid-changed"
|
||||
#define SIGNAL_LEVEL_CHANGED_NAME "mce-battery-level-changed"
|
||||
#define SIGNAL_STATUS_CHANGED_NAME "mce-battery-status-changed"
|
||||
|
||||
static guint mce_battery_signals[SIGNAL_COUNT] = { 0 };
|
||||
|
||||
typedef GObjectClass MceBatteryClass;
|
||||
G_DEFINE_TYPE(MceBattery, mce_battery, G_TYPE_OBJECT)
|
||||
#define PARENT_CLASS mce_battery_parent_class
|
||||
#define MCE_BATTERY_TYPE (mce_battery_get_type())
|
||||
#define MCE_BATTERY(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj,\
|
||||
MCE_BATTERY_TYPE,MceBattery))
|
||||
|
||||
/*==========================================================================*
|
||||
* Implementation
|
||||
*==========================================================================*/
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_check_valid(
|
||||
MceBattery* self)
|
||||
{
|
||||
MceBatteryPriv* priv = self->priv;
|
||||
const gboolean valid = priv->proxy->valid &&
|
||||
(priv->flags & BATTERY_HAVE_ALL) == BATTERY_HAVE_ALL;
|
||||
|
||||
if (valid != self->valid) {
|
||||
self->valid = valid;
|
||||
g_signal_emit(self, mce_battery_signals[SIGNAL_VALID_CHANGED], 0);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_level_update(
|
||||
MceBattery* self,
|
||||
gint level)
|
||||
{
|
||||
MceBatteryPriv* priv = self->priv;
|
||||
const guint new_level = (level < 0) ? 0 : (level > 100) ? 100 : level;
|
||||
|
||||
if (self->level != new_level) {
|
||||
self->level = new_level;
|
||||
g_signal_emit(self, mce_battery_signals[SIGNAL_LEVEL_CHANGED], 0);
|
||||
}
|
||||
priv->flags |= BATTERY_HAVE_LEVEL;
|
||||
mce_battery_check_valid(self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_status_update(
|
||||
MceBattery* self,
|
||||
const char* status)
|
||||
{
|
||||
MceBatteryPriv* priv = self->priv;
|
||||
MCE_BATTERY_STATUS new_status;
|
||||
|
||||
if (!g_strcmp0(status, MCE_BATTERY_STATUS_FULL)) {
|
||||
new_status = MCE_BATTERY_FULL;
|
||||
} else if (!g_strcmp0(status, MCE_BATTERY_STATUS_OK)) {
|
||||
new_status = MCE_BATTERY_OK;
|
||||
} else if (!g_strcmp0(status, MCE_BATTERY_STATUS_LOW)) {
|
||||
new_status = MCE_BATTERY_LOW;
|
||||
} else if (!g_strcmp0(status, MCE_BATTERY_STATUS_EMPTY)) {
|
||||
new_status = MCE_BATTERY_EMPTY;
|
||||
} else {
|
||||
GASSERT(!g_strcmp0(status, MCE_BATTERY_STATUS_UNKNOWN));
|
||||
new_status = MCE_BATTERY_UNKNOWN;
|
||||
}
|
||||
if (self->status != new_status) {
|
||||
self->status = new_status;
|
||||
g_signal_emit(self, mce_battery_signals[SIGNAL_STATUS_CHANGED], 0);
|
||||
}
|
||||
priv->flags |= BATTERY_HAVE_STATUS;
|
||||
mce_battery_check_valid(self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_level_query_done(
|
||||
GObject* proxy,
|
||||
GAsyncResult* result,
|
||||
gpointer arg)
|
||||
{
|
||||
MceBattery* self = MCE_BATTERY(arg);
|
||||
GError* error = NULL;
|
||||
gint level;
|
||||
|
||||
if (com_nokia_mce_request_call_get_battery_level_finish(
|
||||
COM_NOKIA_MCE_REQUEST(proxy), &level, result, &error)) {
|
||||
GDEBUG("Battery level is currently %d", level);
|
||||
mce_battery_level_update(self, level);
|
||||
} else {
|
||||
/* Should retry? */
|
||||
GWARN("Failed to query battery level %s", GERRMSG(error));
|
||||
g_error_free(error);
|
||||
}
|
||||
mce_battery_unref(self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_status_query_done(
|
||||
GObject* proxy,
|
||||
GAsyncResult* result,
|
||||
gpointer arg)
|
||||
{
|
||||
MceBattery* self = MCE_BATTERY(arg);
|
||||
GError* error = NULL;
|
||||
char* status = NULL;
|
||||
|
||||
if (com_nokia_mce_request_call_get_battery_status_finish(
|
||||
COM_NOKIA_MCE_REQUEST(proxy), &status, result, &error)) {
|
||||
GDEBUG("Battery is currently %s", status);
|
||||
mce_battery_status_update(self, status);
|
||||
g_free(status);
|
||||
} else {
|
||||
/* Should retry? */
|
||||
GWARN("Failed to query battery status %s", GERRMSG(error));
|
||||
g_error_free(error);
|
||||
}
|
||||
mce_battery_unref(self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_level_ind(
|
||||
ComNokiaMceSignal* proxy,
|
||||
gint level,
|
||||
gpointer arg)
|
||||
{
|
||||
GDEBUG("Battery level is %d", level);
|
||||
mce_battery_level_update(MCE_BATTERY(arg), level);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_status_ind(
|
||||
ComNokiaMceSignal* proxy,
|
||||
const char* status,
|
||||
gpointer arg)
|
||||
{
|
||||
GDEBUG("Battery is %s", status);
|
||||
mce_battery_status_update(MCE_BATTERY(arg), status);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_query(
|
||||
MceBattery* self)
|
||||
{
|
||||
MceBatteryPriv* priv = self->priv;
|
||||
MceProxy* proxy = priv->proxy;
|
||||
|
||||
/*
|
||||
* proxy->signal and proxy->request may not be available at the
|
||||
* time when MceBattery is created. In that case we have to wait
|
||||
* for the valid signal before we can connect the battery state
|
||||
* signal and submit the initial query.
|
||||
*/
|
||||
if (proxy->signal) {
|
||||
if (!priv->battery_ind_id[BATTERY_IND_LEVEL]) {
|
||||
priv->battery_ind_id[BATTERY_IND_LEVEL] =
|
||||
g_signal_connect(proxy->signal, "battery-level-ind",
|
||||
G_CALLBACK(mce_battery_level_ind), self);
|
||||
}
|
||||
|
||||
if (!priv->battery_ind_id[BATTERY_IND_STATUS]) {
|
||||
priv->battery_ind_id[BATTERY_IND_STATUS] =
|
||||
g_signal_connect(proxy->signal, "battery-status-ind",
|
||||
G_CALLBACK(mce_battery_status_ind), self);
|
||||
}
|
||||
}
|
||||
if (proxy->request && proxy->valid) {
|
||||
com_nokia_mce_request_call_get_battery_level(proxy->request, NULL,
|
||||
mce_battery_level_query_done, mce_battery_ref(self));
|
||||
com_nokia_mce_request_call_get_battery_status(proxy->request, NULL,
|
||||
mce_battery_status_query_done, mce_battery_ref(self));
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_valid_changed(
|
||||
MceProxy* proxy,
|
||||
void* arg)
|
||||
{
|
||||
MceBattery* self = MCE_BATTERY(arg);
|
||||
MceBatteryPriv* priv = self->priv;
|
||||
|
||||
if (proxy->valid) {
|
||||
mce_battery_query(self);
|
||||
} else {
|
||||
priv->flags = BATTERY_HAVE_NONE;
|
||||
}
|
||||
mce_battery_check_valid(self);
|
||||
}
|
||||
|
||||
/*==========================================================================*
|
||||
* API
|
||||
*==========================================================================*/
|
||||
|
||||
MceBattery*
|
||||
mce_battery_new()
|
||||
{
|
||||
/* MCE assumes one battery */
|
||||
static MceBattery* mce_battery_instance = NULL;
|
||||
|
||||
if (mce_battery_instance) {
|
||||
mce_battery_ref(mce_battery_instance);
|
||||
} else {
|
||||
mce_battery_instance = g_object_new(MCE_BATTERY_TYPE, NULL);
|
||||
mce_battery_query(mce_battery_instance);
|
||||
g_object_add_weak_pointer(G_OBJECT(mce_battery_instance),
|
||||
(gpointer*)(&mce_battery_instance));
|
||||
}
|
||||
return mce_battery_instance;
|
||||
}
|
||||
|
||||
MceBattery*
|
||||
mce_battery_ref(
|
||||
MceBattery* self)
|
||||
{
|
||||
if (G_LIKELY(self)) {
|
||||
g_object_ref(MCE_BATTERY(self));
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
void
|
||||
mce_battery_unref(
|
||||
MceBattery* self)
|
||||
{
|
||||
if (G_LIKELY(self)) {
|
||||
g_object_unref(MCE_BATTERY(self));
|
||||
}
|
||||
}
|
||||
|
||||
gulong
|
||||
mce_battery_add_valid_changed_handler(
|
||||
MceBattery* self,
|
||||
MceBatteryFunc fn,
|
||||
void* arg)
|
||||
{
|
||||
return (G_LIKELY(self) && G_LIKELY(fn)) ? g_signal_connect(self,
|
||||
SIGNAL_VALID_CHANGED_NAME, G_CALLBACK(fn), arg) : 0;
|
||||
}
|
||||
|
||||
gulong
|
||||
mce_battery_add_level_changed_handler(
|
||||
MceBattery* self,
|
||||
MceBatteryFunc fn,
|
||||
void* arg)
|
||||
{
|
||||
return (G_LIKELY(self) && G_LIKELY(fn)) ? g_signal_connect(self,
|
||||
SIGNAL_LEVEL_CHANGED_NAME, G_CALLBACK(fn), arg) : 0;
|
||||
}
|
||||
|
||||
gulong
|
||||
mce_battery_add_status_changed_handler(
|
||||
MceBattery* self,
|
||||
MceBatteryFunc fn,
|
||||
void* arg)
|
||||
{
|
||||
return (G_LIKELY(self) && G_LIKELY(fn)) ? g_signal_connect(self,
|
||||
SIGNAL_STATUS_CHANGED_NAME, G_CALLBACK(fn), arg) : 0;
|
||||
}
|
||||
|
||||
void
|
||||
mce_battery_remove_handler(
|
||||
MceBattery* self,
|
||||
gulong id)
|
||||
{
|
||||
if (G_LIKELY(self) && G_LIKELY(id)) {
|
||||
g_signal_handler_disconnect(self, id);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
mce_battery_remove_handlers(
|
||||
MceBattery* self,
|
||||
gulong *ids,
|
||||
guint count)
|
||||
{
|
||||
gutil_disconnect_handlers(self, ids, count);
|
||||
}
|
||||
|
||||
/*==========================================================================*
|
||||
* Internals
|
||||
*==========================================================================*/
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_init(
|
||||
MceBattery* self)
|
||||
{
|
||||
MceBatteryPriv* priv = G_TYPE_INSTANCE_GET_PRIVATE(self, MCE_BATTERY_TYPE,
|
||||
MceBatteryPriv);
|
||||
|
||||
self->priv = priv;
|
||||
priv->proxy = mce_proxy_new();
|
||||
priv->proxy_valid_id = mce_proxy_add_valid_changed_handler(priv->proxy,
|
||||
mce_battery_valid_changed, self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_finalize(
|
||||
GObject* object)
|
||||
{
|
||||
MceBattery* self = MCE_BATTERY(object);
|
||||
MceBatteryPriv* priv = self->priv;
|
||||
|
||||
gutil_disconnect_handlers(priv->proxy->signal, priv->battery_ind_id,
|
||||
G_N_ELEMENTS(priv->battery_ind_id));
|
||||
mce_proxy_remove_handler(priv->proxy, priv->proxy_valid_id);
|
||||
mce_proxy_unref(priv->proxy);
|
||||
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_battery_class_init(
|
||||
MceBatteryClass* klass)
|
||||
{
|
||||
GObjectClass* object_class = G_OBJECT_CLASS(klass);
|
||||
|
||||
object_class->finalize = mce_battery_finalize;
|
||||
g_type_class_add_private(klass, sizeof(MceBatteryPriv));
|
||||
mce_battery_signals[SIGNAL_VALID_CHANGED] =
|
||||
g_signal_new(SIGNAL_VALID_CHANGED_NAME,
|
||||
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
|
||||
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
||||
mce_battery_signals[SIGNAL_LEVEL_CHANGED] =
|
||||
g_signal_new(SIGNAL_LEVEL_CHANGED_NAME,
|
||||
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
|
||||
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
||||
mce_battery_signals[SIGNAL_STATUS_CHANGED] =
|
||||
g_signal_new(SIGNAL_STATUS_CHANGED_NAME,
|
||||
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
|
||||
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Local Variables:
|
||||
* mode: C
|
||||
* c-basic-offset: 4
|
||||
* indent-tabs-mode: nil
|
||||
* End:
|
||||
*/
|
||||
322
src/mce_charger.c
Normal file
322
src/mce_charger.c
Normal file
@@ -0,0 +1,322 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Jolla Ltd.
|
||||
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
|
||||
*
|
||||
* You may use this file under the terms of BSD license as follows:
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the names of the copyright holders nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "mce_charger.h"
|
||||
#include "mce_proxy.h"
|
||||
#include "mce_log_p.h"
|
||||
|
||||
#include <mce/dbus-names.h>
|
||||
#include <mce/mode-names.h>
|
||||
|
||||
#include <gutil_misc.h>
|
||||
|
||||
/* Generated headers */
|
||||
#include "com.nokia.mce.request.h"
|
||||
#include "com.nokia.mce.signal.h"
|
||||
|
||||
struct mce_charger_priv {
|
||||
MceProxy* proxy;
|
||||
gulong proxy_valid_id;
|
||||
gulong charger_state_ind_id;
|
||||
};
|
||||
|
||||
enum mce_charger_signal {
|
||||
SIGNAL_VALID_CHANGED,
|
||||
SIGNAL_STATE_CHANGED,
|
||||
SIGNAL_COUNT
|
||||
};
|
||||
|
||||
#define SIGNAL_VALID_CHANGED_NAME "mce-charger-valid-changed"
|
||||
#define SIGNAL_STATE_CHANGED_NAME "mce-charger-state-changed"
|
||||
|
||||
static guint mce_charger_signals[SIGNAL_COUNT] = { 0 };
|
||||
|
||||
typedef GObjectClass MceChargerClass;
|
||||
G_DEFINE_TYPE(MceCharger, mce_charger, G_TYPE_OBJECT)
|
||||
#define PARENT_CLASS mce_charger_parent_class
|
||||
#define MCE_CHARGER_TYPE (mce_charger_get_type())
|
||||
#define MCE_CHARGER(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj,\
|
||||
MCE_CHARGER_TYPE,MceCharger))
|
||||
|
||||
/*==========================================================================*
|
||||
* Implementation
|
||||
*==========================================================================*/
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_state_update(
|
||||
MceCharger* self,
|
||||
const char* value)
|
||||
{
|
||||
MCE_CHARGER_STATE state;
|
||||
MceChargerPriv* priv = self->priv;
|
||||
|
||||
if (!g_strcmp0(value, MCE_CHARGER_STATE_ON)) {
|
||||
state = MCE_CHARGER_ON;
|
||||
} else if (!g_strcmp0(value, MCE_CHARGER_STATE_OFF)) {
|
||||
state = MCE_CHARGER_OFF;
|
||||
} else {
|
||||
GASSERT(!g_strcmp0(value, MCE_CHARGER_STATE_UNKNOWN));
|
||||
state = MCE_CHARGER_UNKNOWN;
|
||||
}
|
||||
if (self->state != state) {
|
||||
self->state = state;
|
||||
g_signal_emit(self, mce_charger_signals[SIGNAL_STATE_CHANGED], 0);
|
||||
}
|
||||
if (priv->proxy->valid && !self->valid) {
|
||||
self->valid = TRUE;
|
||||
g_signal_emit(self, mce_charger_signals[SIGNAL_VALID_CHANGED], 0);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_state_query_done(
|
||||
GObject* proxy,
|
||||
GAsyncResult* result,
|
||||
gpointer arg)
|
||||
{
|
||||
GError* error = NULL;
|
||||
char* state = NULL;
|
||||
MceCharger* self = MCE_CHARGER(arg);
|
||||
|
||||
if (com_nokia_mce_request_call_get_charger_state_finish(
|
||||
COM_NOKIA_MCE_REQUEST(proxy), &state, result, &error)) {
|
||||
GDEBUG("Charger is currently %s", state);
|
||||
mce_charger_state_update(self, state);
|
||||
g_free(state);
|
||||
} else {
|
||||
/*
|
||||
* We could retry but it's probably not worth the trouble
|
||||
* because the next time charger state changes we receive
|
||||
* charger_state_ind signal and sync our state with mce.
|
||||
* Until then, this object stays invalid.
|
||||
*/
|
||||
GWARN("Failed to query charger state %s", GERRMSG(error));
|
||||
g_error_free(error);
|
||||
}
|
||||
mce_charger_unref(self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_state_ind(
|
||||
ComNokiaMceSignal* proxy,
|
||||
const char* state,
|
||||
gpointer arg)
|
||||
{
|
||||
GDEBUG("Charger is %s", state);
|
||||
mce_charger_state_update(MCE_CHARGER(arg), state);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_state_query(
|
||||
MceCharger* self)
|
||||
{
|
||||
MceChargerPriv* priv = self->priv;
|
||||
MceProxy* proxy = priv->proxy;
|
||||
|
||||
/*
|
||||
* proxy->signal and proxy->request may not be available at the
|
||||
* time when MceCharger is created. In that case we have to wait
|
||||
* for the valid signal before we can connect the charger state
|
||||
* signal and submit the initial query.
|
||||
*/
|
||||
if (proxy->signal && !priv->charger_state_ind_id) {
|
||||
priv->charger_state_ind_id = g_signal_connect(proxy->signal,
|
||||
MCE_CHARGER_STATE_SIG, G_CALLBACK(mce_charger_state_ind), self);
|
||||
}
|
||||
if (proxy->request && proxy->valid) {
|
||||
com_nokia_mce_request_call_get_charger_state(proxy->request, NULL,
|
||||
mce_charger_state_query_done, mce_charger_ref(self));
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_valid_changed(
|
||||
MceProxy* proxy,
|
||||
void* arg)
|
||||
{
|
||||
MceCharger* self = MCE_CHARGER(arg);
|
||||
|
||||
if (proxy->valid) {
|
||||
mce_charger_state_query(self);
|
||||
} else {
|
||||
if (self->valid) {
|
||||
self->valid = FALSE;
|
||||
g_signal_emit(self, mce_charger_signals[SIGNAL_VALID_CHANGED], 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*==========================================================================*
|
||||
* API
|
||||
*==========================================================================*/
|
||||
|
||||
MceCharger*
|
||||
mce_charger_new()
|
||||
{
|
||||
/* MCE assumes one charger */
|
||||
static MceCharger* mce_charger_instance = NULL;
|
||||
|
||||
if (mce_charger_instance) {
|
||||
mce_charger_ref(mce_charger_instance);
|
||||
} else {
|
||||
mce_charger_instance = g_object_new(MCE_CHARGER_TYPE, NULL);
|
||||
mce_charger_state_query(mce_charger_instance);
|
||||
g_object_add_weak_pointer(G_OBJECT(mce_charger_instance),
|
||||
(gpointer*)(&mce_charger_instance));
|
||||
}
|
||||
return mce_charger_instance;
|
||||
}
|
||||
|
||||
MceCharger*
|
||||
mce_charger_ref(
|
||||
MceCharger* self)
|
||||
{
|
||||
if (G_LIKELY(self)) {
|
||||
g_object_ref(MCE_CHARGER(self));
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
void
|
||||
mce_charger_unref(
|
||||
MceCharger* self)
|
||||
{
|
||||
if (G_LIKELY(self)) {
|
||||
g_object_unref(MCE_CHARGER(self));
|
||||
}
|
||||
}
|
||||
|
||||
gulong
|
||||
mce_charger_add_valid_changed_handler(
|
||||
MceCharger* self,
|
||||
MceChargerFunc fn,
|
||||
void* arg)
|
||||
{
|
||||
return (G_LIKELY(self) && G_LIKELY(fn)) ? g_signal_connect(self,
|
||||
SIGNAL_VALID_CHANGED_NAME, G_CALLBACK(fn), arg) : 0;
|
||||
}
|
||||
|
||||
gulong
|
||||
mce_charger_add_state_changed_handler(
|
||||
MceCharger* self,
|
||||
MceChargerFunc fn,
|
||||
void* arg)
|
||||
{
|
||||
return (G_LIKELY(self) && G_LIKELY(fn)) ? g_signal_connect(self,
|
||||
SIGNAL_STATE_CHANGED_NAME, G_CALLBACK(fn), arg) : 0;
|
||||
}
|
||||
|
||||
void
|
||||
mce_charger_remove_handler(
|
||||
MceCharger* self,
|
||||
gulong id)
|
||||
{
|
||||
if (G_LIKELY(self) && G_LIKELY(id)) {
|
||||
g_signal_handler_disconnect(self, id);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
mce_charger_remove_handlers(
|
||||
MceCharger* self,
|
||||
gulong *ids,
|
||||
guint count)
|
||||
{
|
||||
gutil_disconnect_handlers(self, ids, count);
|
||||
}
|
||||
|
||||
/*==========================================================================*
|
||||
* Internals
|
||||
*==========================================================================*/
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_init(
|
||||
MceCharger* self)
|
||||
{
|
||||
MceChargerPriv* priv = G_TYPE_INSTANCE_GET_PRIVATE(self, MCE_CHARGER_TYPE,
|
||||
MceChargerPriv);
|
||||
|
||||
self->priv = priv;
|
||||
priv->proxy = mce_proxy_new();
|
||||
priv->proxy_valid_id = mce_proxy_add_valid_changed_handler(priv->proxy,
|
||||
mce_charger_valid_changed, self);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_finalize(
|
||||
GObject* object)
|
||||
{
|
||||
MceCharger* self = MCE_CHARGER(object);
|
||||
MceChargerPriv* priv = self->priv;
|
||||
|
||||
if (priv->charger_state_ind_id) {
|
||||
g_signal_handler_disconnect(priv->proxy->signal,
|
||||
priv->charger_state_ind_id);
|
||||
}
|
||||
mce_proxy_remove_handler(priv->proxy, priv->proxy_valid_id);
|
||||
mce_proxy_unref(priv->proxy);
|
||||
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
mce_charger_class_init(
|
||||
MceChargerClass* klass)
|
||||
{
|
||||
GObjectClass* object_class = G_OBJECT_CLASS(klass);
|
||||
|
||||
object_class->finalize = mce_charger_finalize;
|
||||
g_type_class_add_private(klass, sizeof(MceChargerPriv));
|
||||
mce_charger_signals[SIGNAL_VALID_CHANGED] =
|
||||
g_signal_new(SIGNAL_VALID_CHANGED_NAME,
|
||||
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
|
||||
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
||||
mce_charger_signals[SIGNAL_STATE_CHANGED] =
|
||||
g_signal_new(SIGNAL_STATE_CHANGED_NAME,
|
||||
G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
|
||||
0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Local Variables:
|
||||
* mode: C
|
||||
* c-basic-offset: 4
|
||||
* indent-tabs-mode: nil
|
||||
* End:
|
||||
*/
|
||||
Reference in New Issue
Block a user