devicectl: Add dbusname and devicelist function() 36/44736/1
authorJiyoung Yun <jy910.yun@samsung.com>
Mon, 27 Jul 2015 08:08:46 +0000 (17:08 +0900)
committerJiyoung Yun <jy910.yun@samsung.com>
Mon, 27 Jul 2015 08:08:46 +0000 (17:08 +0900)
These functions will support to get whole dbus name and device list in deviced.
And do not use sync call to request save log and dump mode events.

Change-Id: I51966c0717750b29ce9523ee3f9c95ff57a9bafc
Signed-off-by: Jiyoung Yun <jy910.yun@samsung.com>
packaging/deviced.spec
src/core/devices.c
src/devicectl/CMakeLists.txt
src/devicectl/devicectl.c
src/shared/dbus.h

index a40a2ab..88089ec 100644 (file)
@@ -70,6 +70,7 @@ BuildRequires:  pkgconfig(sensor)
 BuildRequires:  pkgconfig(tapi)
 %endif
 
+Requires: %{name}-tools = %{version}-%{release}
 %{?systemd_requires}
 Requires(post): /usr/bin/vconftool
 
@@ -86,7 +87,6 @@ deviced daemon.
 %package tools
 Summary:  Deviced tools
 Group:    System/Utilities
-Requires: %{name} = %{version}-%{release}
 
 %description tools
 Deviced helper programs
index 942b7e0..3ff5a8d 100644 (file)
@@ -23,6 +23,7 @@
 #include "list.h"
 #include "common.h"
 #include "devices.h"
+#include "edbus-handler.h"
 
 static const struct device_ops default_ops = {
        .name = "default-ops",
@@ -62,10 +63,27 @@ int check_default(const struct device_ops *dev)
        return (dev == &default_ops);
 }
 
+static DBusMessage *edbus_device_list(E_DBus_Object *obj, DBusMessage *msg)
+{
+       dd_list *elem;
+       const struct device_ops *dev;
+
+       _I("device list!");
+       DD_LIST_FOREACH(dev_head, elem, dev)
+               _I("%s", dev->name);
+
+       return dbus_message_new_method_return(msg);
+}
+
+static const struct edbus_method edbus_methods[] = {
+       { "DeviceList",          NULL,        NULL,        edbus_device_list },
+};
+
 void devices_init(void *data)
 {
        dd_list *elem, *elem_n;
        const struct device_ops *dev;
+       int ret;
 
        DD_LIST_FOREACH_SAFE(dev_head, elem, elem_n, dev) {
                if (dev->probe && dev->probe(data) != 0) {
@@ -78,6 +96,11 @@ void devices_init(void *data)
                if (dev->init)
                        dev->init(data);
        }
+
+       ret = register_edbus_method(DEVICED_PATH_CORE,
+                   edbus_methods, ARRAY_SIZE(edbus_methods));
+       if (ret < 0)
+               _E("Failed to register edbus method! %d", ret);
 }
 
 void devices_exit(void *data)
index b5e2e3c..7078254 100755 (executable)
@@ -18,14 +18,8 @@ FOREACH(flag ${pkgs_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
-IF(USE_ENGINEER_MODE)
-    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -fno-omit-frame-pointer -finstrument-functions")
-ELSE()
-    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -fno-omit-frame-pointer")
-ENDIF()
-
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -fno-omit-frame-pointer -finstrument-functions")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
-MESSAGE("FLAGS: ${CMAKE_C_FLAGS}")
 
 ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
 ADD_DEFINITIONS("-DFACTORYFS=\"$ENV{FACTORYFS}\"")
index e59724b..c98425a 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <dbus/dbus.h>
 #include <shared/dbus.h>
 #include <core/common.h>
@@ -99,9 +100,7 @@ static int stop_device(char **args)
 
 static int dump_mode(char **args)
 {
-       DBusError err;
-       DBusMessage *msg;
-       int ret, val;
+       int ret;
        char *arr[1];
 
        if (!args[1] || !args[2] || !args[3])
@@ -110,37 +109,120 @@ static int dump_mode(char **args)
        printf("%s (%s %s)!\n", args[1], args[2], args[3]);
 
        arr[0] = args[3];
-       msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
+       ret = dbus_method_async(DEVICED_BUS_NAME,
                    devices[arg_id].path, devices[arg_id].iface,
                    "Dumpmode", "s", arr);
-       if (!msg)
-               return -EBADMSG;
+       if (ret < 0)
+               printf("failed to set dump mode (%d)", ret);
 
-       dbus_error_init(&err);
+       return ret;
+}
 
-       ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
-       if (!ret) {
-               printf("no message : [%s:%s]", err.name, err.message);
-               dbus_error_free(&err);
-               val = -ENOMSG;
+static int save_log(char **args)
+{
+       int ret;
+
+       if (!args[1])
+               return -EINVAL;
+
+       printf("save log %s device!\n", args[1]);
+
+       ret = dbus_method_async(DEVICED_BUS_NAME,
+                   devices[arg_id].path, devices[arg_id].iface,
+                   "SaveLog", NULL, NULL);
+       if (ret < 0)
+               printf("failed to save log (%d)", ret);
+
+       return ret;
+}
+
+static void get_pname(pid_t pid, char *pname)
+{
+       char buf[PATH_MAX];
+       int cmdline, r;
+
+       snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
+       cmdline = open(buf, O_RDONLY);
+       if (cmdline < 0) {
+               pname[0] = '\0';
+               return;
+       }
+
+       r = read(cmdline, pname, PATH_MAX);
+       if ((r >= 0) && (r < PATH_MAX))
+               pname[r] = '\0';
+       else
+               pname[0] = '\0';
+
+       close(cmdline);
+}
+
+static int save_dbus_name(char **args)
+{
+       DBusMessage *msg;
+       unsigned char **list;
+       int ret, size, i;
+       pid_t pid;
+       char *arr[1];
+       char pname[PATH_MAX];
+
+       if (!args[1])
+               return -EINVAL;
+
+       printf("save dbus name!\n");
+
+       msg = dbus_method_sync_with_reply(DBUS_BUS_NAME,
+           DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME,
+           "ListNames", NULL, NULL);
+       if (!msg) {
+               printf("failed to get list names");
+               return -EBADMSG;
        }
 
+       ret = dbus_message_get_args(msg, NULL, DBUS_TYPE_ARRAY,
+           DBUS_TYPE_STRING, &list, &size, DBUS_TYPE_INVALID);
        dbus_message_unref(msg);
-       return val;
+       if (!ret) {
+               printf("invalid list name arguments!");
+               return -EINVAL;
+       }
+
+       printf("%d connections\n", size);
+
+       for (i = 0; i < size; i++) {
+               arr[0] = list[i];
+               msg = dbus_method_sync_with_reply(DBUS_BUS_NAME,
+                   DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME,
+                   "GetConnectionUnixProcessID", "s", arr);
+               if (!msg)
+                       continue;
+
+               ret = dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32,
+                   &pid, DBUS_TYPE_INVALID);
+               dbus_message_unref(msg);
+               if (!ret)
+                       continue;
+
+               get_pname(pid, pname);
+               printf("%6d  %6s  %s\n", pid, list[i], pname);
+
+       }
+
+       return 0;
 }
 
-static int save_log(char **args)
+static int device_list(char **args)
 {
        DBusMessage *msg;
 
        if (!args[1])
                return -EINVAL;
 
-       printf("save log %s device!\n", args[1]);
+       printf("print %s to dlog!\n", args[1]);
 
        msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
                    devices[arg_id].path, devices[arg_id].iface,
-                   "SaveLog", NULL, NULL);
+                   "DeviceList", NULL, NULL);
        if (!msg)
                return -EBADMSG;
 
@@ -219,6 +301,8 @@ static const struct action {
        { DEVICE_DISPLAY,   "savelog",         3, save_log,          ""            },
        { DEVICE_USB,       "set",             4, set_usb_mode,      "[sdb|ssh]"   },
        { DEVICE_USB,       "unset",           4, unset_usb_mode,    "[sdb|ssh]"   },
+       { DEVICE_CORE,      "dbusname",        3, save_dbus_name,    ""            },
+       { DEVICE_CORE,      "devicelist",      3, device_list,       ""            },
        { DEVICE_EXTCON,    "enable",          4, enable_device,     "[USB|HEADPHONE|HDMI|DOCK]" },
        { DEVICE_EXTCON,    "disable",         4, disable_device,    "[USB|HEADPHONE|HDMI|DOCK]" },
 };
index e00a939..ece871a 100644 (file)
  */
 
 /*
+ * DBus daemon
+ */
+#define DBUS_BUS_NAME                       "org.freedesktop.DBus"
+#define DBUS_OBJECT_PATH                    "/org/freedesktop/DBus"
+#define DBUS_INTERFACE_NAME                 DBUS_BUS_NAME
+
+/*
  * Device daemon
  */
 #define DEVICED_BUS_NAME                    "org.tizen.system.deviced"