From 300b055419daa9cde89887833ceb75728f6c6209 Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Wed, 11 May 2016 15:37:53 +0300 Subject: [PATCH] [ssu] Get device manufacturer and product name from ssu. Fixes JB#35156 Maintaining usb manufacturer and product name configurations for multiple devices is cumbersome and time consuming. It also duplicates effort that is needed for ssu configuration anyway. When possible, override usb-moded manufacturer and product name configuration by using data provided by ssu D-Bus service. The feature is optional and needs to be enabled at usb-moded build time via "--enable-mer-ssu" configure option. Signed-off-by: Simo Piiroinen --- configure.ac | 8 ++ rpm/usb-moded.spec | 2 +- src/Makefile.am | 6 ++ src/usb_moded-config.c | 28 ++++++- src/usb_moded-ssu.c | 168 +++++++++++++++++++++++++++++++++++++++++ src/usb_moded-ssu.h | 29 +++++++ 6 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 src/usb_moded-ssu.c create mode 100644 src/usb_moded-ssu.h diff --git a/configure.ac b/configure.ac index 9ae2405..6876dcd 100644 --- a/configure.ac +++ b/configure.ac @@ -40,6 +40,14 @@ AC_ARG_ENABLE([meegodevlock], AS_HELP_STRING([--enable-meegodevlock], [Enable Me esac],[meegodevlock=false]) AM_CONDITIONAL([MEEGOLOCK], [test x$meegodevlock = xtrue]) +AC_ARG_ENABLE([mer_ssu], AS_HELP_STRING([--enable-mer-ssu], [Enable MER SSU @<:@default=false@:>@]), + [case "${enableval}" in + yes) mer_ssu=true ; CFLAGS="-DUSE_MER_SSU $CFLAGS" ;; + no) mer_ssu=false ;; + *) AC_MSG_ERROR([bad value ${enableval} for --enable-mer-ssu]) ;; + esac],[mer_ssu=false]) +AM_CONDITIONAL([USE_MER_SSU], [test x$mer_ssu = xtrue]) + AC_ARG_ENABLE([app_sync], AS_HELP_STRING([--enable-app-sync], [Enable application syncing @<:@default=true@:>@]), [case "${enableval}" in yes) app_sync=true ; CFLAGS="-DAPP_SYNC $CFLAGS" ;; diff --git a/rpm/usb-moded.spec b/rpm/usb-moded.spec index 09b7a98..9f5df40 100644 --- a/rpm/usb-moded.spec +++ b/rpm/usb-moded.spec @@ -308,7 +308,7 @@ when the UI fails. %build %autogen -%configure --enable-app-sync --enable-meegodevlock --enable-debug --enable-connman --enable-systemd +%configure --enable-app-sync --enable-meegodevlock --enable-debug --enable-connman --enable-systemd --enable-mer-ssu make all doc %{?_smp_mflags} %install diff --git a/src/Makefile.am b/src/Makefile.am index 637a113..440ed09 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -38,6 +38,12 @@ usb_moded_SOURCES = \ usb_moded-android.h \ usb_moded-android.c +if USE_MER_SSU +usb_moded_SOURCES += \ + usb_moded-ssu.h \ + usb_moded-ssu.c +endif + if MEEGOLOCK usb_moded_SOURCES += \ usb_moded-devicelock.h \ diff --git a/src/usb_moded-config.c b/src/usb_moded-config.c index e0457ed..635745c 100644 --- a/src/usb_moded-config.c +++ b/src/usb_moded-config.c @@ -42,6 +42,10 @@ #include "usb_moded-log.h" #include "usb_moded-modes.h" +#ifdef USE_MER_SSU +# include "usb_moded-ssu.h" +#endif + static int get_conf_int(const gchar *entry, const gchar *key); static char * get_conf_string(const gchar *entry, const gchar *key); static char * get_kcmdline_string(const char *entry); @@ -743,7 +747,17 @@ int conf_file_merge(void) char * get_android_manufacturer(void) { - return(get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY)); +#ifdef USE_MER_SSU + /* If SSU can provide manufacturer name, use it. Otherwise fall + * back to using the name specified in configuration files. */ + char *ssu_name = ssu_get_manufacturer_name(); + if( ssu_name ) + { + return ssu_name; + } +#endif + + return get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY); } char * get_android_vendor_id(void) @@ -753,7 +767,17 @@ char * get_android_vendor_id(void) char * get_android_product(void) { - return(get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY)); +#ifdef USE_MER_SSU + /* If SSU can provide device model name, use it. Otherwise fall + * back to using the name specified in configuration files. */ + char *ssu_name = ssu_get_product_name(); + if( ssu_name ) + { + return ssu_name; + } +#endif + + return get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY); } char * get_android_product_id(void) diff --git a/src/usb_moded-ssu.c b/src/usb_moded-ssu.c new file mode 100644 index 0000000..52d40bc --- /dev/null +++ b/src/usb_moded-ssu.c @@ -0,0 +1,168 @@ +/** + @file usb_moded-ssu.c + + Copyright (C) 2016 Jolla. All rights reserved. + + @author: Simo Piiroinen + + 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 + +#include + +#include "usb_moded-ssu.h" +#include "usb_moded-log.h" + +/** SSU D-Bus service name */ +#define SSU_DBUS_SERVICE "org.nemo.ssu" + +/** Default SSU D-Bus object path */ +#define SSU_DBUS_OBJECT "/org/nemo/ssu" + +/** SSU D-Bus interface */ +#define SSU_DBUS_INTERFACE "org.nemo.ssu" + +/** SSU displayName D-Bus method call */ +#define SSU_DBUS_GET_DISPLAY_NAME "displayName" + +/** SsuDisplayType enum adapted from ssu c++ headers */ +typedef enum +{ + /** Manufacturer, like ACME Corp. + * Board mappings key "deviceManufacturer" */ + SsuDeviceManufacturer = 0, + + /** Marketed device name, like Pogoblaster 3000. + * Board mappings key "prettyModel" */ + SsuDeviceModel = 1, + + /** Type designation, like NCC-1701. + * Beard mappings key "deviceDesignation" */ + SsuDeviceDesignation = 2, +} SsuDisplayType; + +/** Wrapper for making synchronous org.nemo.ssu.displayName() method calls + * + * Caller must release non-null return value with g_free(). + * + * @param type_id display name type to query + * + * @return human readable string, or NULL in case of errors + */ +static gchar * +usb_moded_get_ssu_display_name(SsuDisplayType type_id) +{ + gchar *res = 0; + DBusConnection *con = 0; + DBusMessage *req = 0; + DBusMessage *rsp = 0; + dbus_int32_t arg = type_id; + const char *val = 0; + DBusError err = DBUS_ERROR_INIT; + + if( !(con = dbus_bus_get(DBUS_BUS_SYSTEM, &err)) ) { + log_err("could not connect to system bus: %s: %s", + err.name, err.message); + goto EXIT; + } + + req = dbus_message_new_method_call(SSU_DBUS_SERVICE, + SSU_DBUS_OBJECT, + SSU_DBUS_INTERFACE, + SSU_DBUS_GET_DISPLAY_NAME); + + if( !req ) { + log_err("could not create method call message"); + goto EXIT; + } + + if( !dbus_message_append_args(req, + DBUS_TYPE_INT32, &arg, + DBUS_TYPE_INVALID) ) { + log_err("could not add method call parameters"); + goto EXIT; + } + + rsp = dbus_connection_send_with_reply_and_block(con, req, -1, &err); + if( !rsp ) { + /* Be less verbose with failures that are expected + * if ssu service is not installed on the device. */ + if( !strcmp(err.name, DBUS_ERROR_SERVICE_UNKNOWN) ) { + log_debug("%s D-Bus service is not available", + SSU_DBUS_SERVICE); + } + else { + log_err("did not get reply: %s: %s", + err.name, err.message); + } + goto EXIT; + } + + if( dbus_set_error_from_message(&err, rsp) ) { + log_err("got error reply: %s: %s", err.name, err.message); + goto EXIT; + } + + if( !dbus_message_get_args(rsp, &err, + DBUS_TYPE_STRING, &val, + DBUS_TYPE_INVALID) ) { + log_err("could not parse reply: %s: %s", + err.name, err.message); + goto EXIT; + } + + res = g_strdup(val); + +EXIT: + + dbus_error_free(&err); + + if( rsp ) + dbus_message_unref(rsp); + if( req ) + dbus_message_unref(req); + if( con ) + dbus_connection_unref(con); + + log_debug("ssu displayName(%d) -> %s", type_id, res ?: "N/A"); + + return res; +} + +/** Query device manufacturer name from the SSU D-Bus service + * + * Caller must release non-null return value with g_free(). + * + * @return human readable string, or NULL in case of errors + */ +gchar * +ssu_get_manufacturer_name(void) +{ + return usb_moded_get_ssu_display_name(SsuDeviceManufacturer); +} + +/** Query device model name from the SSU D-Bus service + * + * Caller must release non-null return value with g_free(). + * + * @return human readable string, or NULL in case of errors + */ +gchar * +ssu_get_product_name(void) +{ + return usb_moded_get_ssu_display_name(SsuDeviceModel); +} diff --git a/src/usb_moded-ssu.h b/src/usb_moded-ssu.h new file mode 100644 index 0000000..0eef2c2 --- /dev/null +++ b/src/usb_moded-ssu.h @@ -0,0 +1,29 @@ +/** + @file usb_moded-ssu.h + + Copyright (C) 2016 Jolla. All rights reserved. + + @author: Simo Piiroinen + + 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 +*/ + +#ifndef USB_MODED_SSU_H_ +# define USB_MODED_SSU_H_ + +gchar * ssu_get_manufacturer_name(void); +gchar * ssu_get_product_name(void); + +#endif // USB_MODED_SSU_H_