Skip to content

Commit

Permalink
First version of usb_moded_util to control the DBUS api without havin…
Browse files Browse the repository at this point in the history
…g to remember it all

Signed-off-by: Philippe De Swert <philippe.deswert@jollamobile.com>
  • Loading branch information
philippedeswert committed May 16, 2013
1 parent da73a71 commit 2fac124
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 1 deletion.
3 changes: 3 additions & 0 deletions debian/changelog
@@ -1,6 +1,9 @@
usb-moded (0.60) unstable; urgency=low

* Add dedicated charger tracking
* Config file merging support
* Add basic version of usb_moded_util to not having to remember dbus-send commands
* Fix small bug in the android mode setting

-- Philippe De Swert <philippe.deswert@jollamobile.com> Fri, 10 May 2013 15:56:03 +0300

Expand Down
14 changes: 13 additions & 1 deletion src/Makefile.am
@@ -1,4 +1,5 @@
sbin_PROGRAMS = usb_moded
sbin_PROGRAMS = usb_moded \
usb_moded_util

usb_moded_CPPFLAGS = \
$(USB_MODED_CFLAGS)
Expand Down Expand Up @@ -86,4 +87,15 @@ usb_moded_SOURCES += \
usb_moded-dyn-config.c \
usb_moded-dyn-config.h
endif

usb_moded_util_CPPFLAGS = \
$(USB_MODED_CFLAGS)

usb_moded_util_LDFLAGS = \
-Wl,--as-needed

usb_moded_util_LDADD = \
$(USB_MODED_LIBS)

usb_moded_util_SOURCES = \
usb_moded-util.c
186 changes: 186 additions & 0 deletions src/usb_moded-util.c
@@ -0,0 +1,186 @@
/**
@file usb_moded-util.c
Copyright (C) 2013 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.deswert@jollamobile.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/


#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <dbus/dbus.h>

#include "usb_moded-dbus.h"

DBusError error;
DBusConnection *conn = 0;

static int query_mode (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;

if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_STATE_REQUEST)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}

if(ret)
{
printf("mode = %s\n", ret);
return 0;
}

/* not everything went as planned, return error */
return 1;
}

static int get_modelist (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;

if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_LIST)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}

if(ret)
{
printf("modes supported are = %s\n", ret);
return 0;
}

/* not everything went as planned, return error */
return 1;
}

static int get_mode_configured (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;

if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_GET)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}

if(ret)
{
printf("On USB connection usb_moded will set the following mode based on the configuration = %s\n", ret);
return 0;
}

/* not everything went as planned, return error */
return 1;
}

int main (int argc, char *argv[])
{
int query = 0, network = 0, setmode = 0, config = 0;
int modelist = 0, mode_configured = 0;
int res = 1, opt;

if(argc == 1)
{
fprintf(stderr, "No options given, try -h for more information\n");
exit(1);
}

while ((opt = getopt(argc, argv, "cdhmnqs")) != -1)
{
switch (opt) {
case 'c':
config = 1;
break;
case 'd':
mode_configured = 1;
break;
case 'm':
modelist = 1;
break;
case 'n':
network = 1;
break;
case 'q':
query = 1;
break;
case 's':
setmode = 1;
break;
case 'h':
default:
fprintf(stderr, "\nUsage: %s -<option> <args>\n\n \
Options are: \n \
\t-c to set a mode in the config file,\n \
\t-d to get the default mode set in the configuration, \n \
\t-h to get this help, \n \
\t-n to set network configuration in the config,\n \
\t-m to get the list of supported modes, \n \
\t-q to query the current mode,\n \
\t-s to set/activate a mode\n",
argv[0]);
exit(1);
}
}

/* init dbus */
dbus_error_init(&error);

conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
if (!conn)
{
if (dbus_error_is_set(&error))
return 1;
}

/* check which sub-routine to call */
if(query)
res = query_mode();
else if (modelist)
res = get_modelist();
else if (mode_configured)
res = get_mode_configured();

/* subfunctions will return 1 if an error occured, print message */
if(res)
printf("Sorry an error occured, your request was not processed.\n");

/* clean-up and exit */
dbus_connection_close(conn);
dbus_connection_unref(conn);
return 0;
}

0 comments on commit 2fac124

Please sign in to comment.