Skip to content

Commit

Permalink
Merge pull request #16 from plundstr/master
Browse files Browse the repository at this point in the history
Add usb detection
  • Loading branch information
Pekka Lundstrom committed Aug 26, 2013
2 parents 59169ee + fa2062d commit b9bde7e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 11 deletions.
2 changes: 1 addition & 1 deletion configure.ac
@@ -1,5 +1,5 @@
# Package name and version
AC_INIT(dsme, 0.62.15)
AC_INIT(dsme, 0.62.16)

AM_INIT_AUTOMAKE

Expand Down
2 changes: 2 additions & 0 deletions modules/Makefile.am
Expand Up @@ -109,6 +109,8 @@ thermalmanager_la_LIBADD = -lthermalmanager_dbus_if
emergencycalltracker_la_SOURCES = emergencycalltracker.c

usbtracker_la_SOURCES = usbtracker.c
usbtracker_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)
usbtracker_la_LIBADD = $(DBUS_LIBS)

iphb_la_SOURCES = iphb.c
iphb_la_CFLAGS = $(AM_CFLAGS) $(GLIB_CFLAGS) $(DBUS_CFLAGS)
Expand Down
2 changes: 1 addition & 1 deletion modules/state.c
Expand Up @@ -233,7 +233,7 @@ static dsme_state_t select_state(void)
} else if (shutdown_requested || reboot_requested) {
/* favor normal shutdown over reboot over actdead */
if (shutdown_requested &&
(charger_state == CHARGER_DISCONNECTED || charger_state == CHARGER_STATE_UNKNOWN) &&
(charger_state == CHARGER_DISCONNECTED) &&
!alarm_set)
{
dsme_log(LOG_NOTICE, "Normal shutdown");
Expand Down
142 changes: 136 additions & 6 deletions modules/usbtracker.c
Expand Up @@ -37,9 +37,13 @@

#include "dsme/modules.h"
#include "dsme/logging.h"
#include <dsme/state.h>

#include <string.h>
#include <dbus/dbus.h>

static bool mounted_to_pc = false;
static bool charger_connected = false;

static void send_usb_status(bool mounted_to_pc)
{
Expand All @@ -48,23 +52,59 @@ static void send_usb_status(bool mounted_to_pc)
msg.mounted_to_pc = mounted_to_pc;

dsme_log(LOG_DEBUG,
"broadcasting usb state:%s mounted to PC",
"usbtracker: broadcasting usb state:%s mounted to PC",
msg.mounted_to_pc ? "" : " not");
broadcast_internally(&msg);
}

static void send_charger_status(bool charger_state)
{
DSM_MSGTYPE_SET_CHARGER_STATE msg = DSME_MSG_INIT(DSM_MSGTYPE_SET_CHARGER_STATE);

msg.connected = charger_state;

dsme_log(LOG_DEBUG,
"usbtracker: broadcasting usb charger state:%s connected",
msg.connected ? "" : " not");

broadcast_internally(&msg);
}

static void usb_state_ind(const DsmeDbusMessage* ind)
{
bool mounted_to_pc = false;
static bool mounted_to_pc_new = false;
static bool charger_connected_new = false;
const char* state = dsme_dbus_message_get_string(ind);

// dsme_log(LOG_DEBUG, "usbtracker: %s(state = %s)",__FUNCTION__, state);

if (strcmp(state, "mass_storage") == 0 ||
strcmp(state, "data_in_use" ) == 0)
{
mounted_to_pc = true;
mounted_to_pc_new = true;
}
if (strcmp(state, "USB connected") == 0 ||
strcmp(state, "charger_connected") == 0 )
charger_connected_new = true;
else if (strcmp(state, "USB disconnected") == 0 ||
strcmp(state, "charger_disconnected") == 0 )
{
charger_connected_new = false;
mounted_to_pc_new = false;
}

if (mounted_to_pc != mounted_to_pc_new)
{
mounted_to_pc = mounted_to_pc_new;
send_usb_status(mounted_to_pc);
}

if (charger_connected != charger_connected_new)
{
charger_connected = charger_connected_new;
send_charger_status(charger_connected);
}

send_usb_status(mounted_to_pc);
}

static const dsme_dbus_signal_binding_t signals[] = {
Expand All @@ -74,10 +114,100 @@ static const dsme_dbus_signal_binding_t signals[] = {

static bool bound = false;

static bool is_charging(const char *mode)
{
return strcmp(mode, "undefined") ? true : false;
}

static bool is_mounted_pc(const char *mode)
{
bool connected = FALSE;

if ((strcmp(mode, "mass_storage") == 0) ||
(strcmp(mode, "mtp_mode") == 0))
connected = TRUE;

return (connected);
}

static void mode_request_cb(DBusPendingCall *pending,
void *user_data)
{
(void)user_data; // not used

DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
const char *dta = 0;

if( !(rsp = dbus_pending_call_steal_reply(pending)) )
goto cleanup;

if( dbus_set_error_from_message(&err, rsp) ||
!dbus_message_get_args(rsp, &err,
DBUS_TYPE_STRING, &dta,
DBUS_TYPE_INVALID) )
{
dsme_log(LOG_ERR, "usbtracker: mode_request: %s: %s",
err.name, err.message);
goto cleanup;
}

dsme_log(LOG_DEBUG, "usbtracker: mode = '%s'", dta ?: "???");

if( dta )
{
charger_connected = is_charging(dta);
send_charger_status(charger_connected);
mounted_to_pc = is_mounted_pc(dta);
send_usb_status(mounted_to_pc);
}

cleanup:
if( rsp ) dbus_message_unref(rsp);
dbus_error_free(&err);
}

DSME_HANDLER(DSM_MSGTYPE_DBUS_CONNECT, client, msg)
{
dsme_log(LOG_DEBUG, "usbtracker: DBUS_CONNECT");
dsme_dbus_bind_signals(&bound, signals);
dsme_log(LOG_DEBUG, "usbtracker: DBUS_CONNECT");
dsme_dbus_bind_signals(&bound, signals);

/* we are connected on dbus, now we can query
* charger/usb connection details */

DBusError err = DBUS_ERROR_INIT;
DBusPendingCall *pc = 0;
DBusConnection *conn = 0;
DBusMessage *req = NULL;

if( !(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err)) )
{
dsme_log(LOG_ERR, "DBUS_BUS_SYSTEM: %s: %s",
err.name, err.message);
goto cleanup;
}

req = dbus_message_new_method_call("com.meego.usb_moded",
"/com/meego/usb_moded",
"com.meego.usb_moded",
"mode_request");
if( !req )
goto cleanup;

if( !dbus_connection_send_with_reply(conn, req, &pc, -1) )
goto cleanup;

if( !dbus_pending_call_set_notify(pc, mode_request_cb, 0, 0) )
goto cleanup;

dsme_log(LOG_DEBUG, "usbtracker: mode_request sent");

cleanup:

if( pc ) dbus_pending_call_unref(pc);
if( req ) dbus_message_unref(req);
if( conn ) dbus_connection_unref(conn);
dbus_error_free(&err);
}

DSME_HANDLER(DSM_MSGTYPE_DBUS_DISCONNECT, client, msg)
Expand Down
2 changes: 1 addition & 1 deletion rpm/dsme.service
@@ -1,6 +1,6 @@
[Unit]
Description=DSME
After=syslog.target
After=syslog.target usb-moded.service

[Service]
Type=notify
Expand Down
2 changes: 1 addition & 1 deletion rpm/dsme.spec
Expand Up @@ -9,7 +9,7 @@ Name: dsme
# << macros

Summary: Device State Management Entity
Version: 0.62.15
Version: 0.62.16
Release: 0
Group: System/System Control
License: LGPLv2+
Expand Down
2 changes: 1 addition & 1 deletion rpm/dsme.yaml
@@ -1,6 +1,6 @@
Name: dsme
Summary: Device State Management Entity
Version: 0.62.15
Version: 0.62.16
Release: 0
Group: System/System Control
License: LGPLv2+
Expand Down

0 comments on commit b9bde7e

Please sign in to comment.