From baad63f125b51bf6230cd4752bfef1d45caf0ecb Mon Sep 17 00:00:00 2001 From: "sh.pi" Date: Thu, 4 Apr 2013 11:30:31 +0900 Subject: [PATCH] Add dbus interface for method call. Change-Id: I87e8937de041e19fcc6c5dddf069c47757be6046 Signed-off-by: Krzysztof Sasiak --- CMakeLists.txt | 1 + display/core.c | 6 +++ display/core.h | 3 +- display/display-dbus.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++ src/core/edbus-handler.c | 61 +++++++++++++++++++++ src/core/edbus-handler.h | 18 +++++++ 6 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 display/display-dbus.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 21b4ad1..e600905 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ SET(SRCS ${SRCS} display/setting.c display/poll.c display/core.c + display/display-dbus.c display/lsensor.c display/key-filter.c display/battery.c) diff --git a/display/core.c b/display/core.c index da2b79a..ea1512b 100644 --- a/display/core.c +++ b/display/core.c @@ -1360,6 +1360,7 @@ enum { INIT_INTERFACE, INIT_POLL, INIT_FIFO, + INIT_DBUS, INIT_END }; @@ -1368,6 +1369,7 @@ static char *errMSG[INIT_END] = { [INIT_INTERFACE] = "lowlevel interface(sysfs or others) init error", [INIT_POLL] = "input devices poll init error", [INIT_FIFO] = "FIFO poll init error", + [INIT_DBUS] = "d-bus init error", }; /* logging indev_list for debug */ @@ -1580,6 +1582,10 @@ void start_pm_main(void) LOGINFO("poll init"); ret = init_pm_poll(poll_callback); break; + case INIT_DBUS: + LOGINFO("dbus init"); + ret = init_pm_dbus(); + break; } if (ret != 0) { LOGERR("%s", errMSG[i]); diff --git a/display/core.h b/display/core.h index 7488bc9..db4bcd2 100644 --- a/display/core.h +++ b/display/core.h @@ -122,7 +122,8 @@ int check_processes(enum state_t prohibit_state); * @param[in] flags If the first bit of this is set, start managing without Start notification. * If the second bit of ths is set, use unified device manager functions. */ -void start_pm_main(); +void start_pm_main(void); +void end_pm_main(void); /** * @} diff --git a/display/display-dbus.c b/display/display-dbus.c new file mode 100644 index 0000000..aa09dec --- /dev/null +++ b/display/display-dbus.c @@ -0,0 +1,134 @@ +/* + * deviced + * + * Copyright (c) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + + +/** + * @file display-dbus.c + * @brief dbus interface + * + */ + +#include + +#include "util.h" +#include "core.h" +#include "device-node.h" +#include "core/common.h" + +#define DISP_INDEX_BIT 4 +#define COMBINE_DISP_CMD(cmd, prop, index) (cmd = (prop | (index << DISP_INDEX_BIT))) + +static DBusMessage* e_dbus_start_cb(E_DBus_Object *obj, DBusMessage* msg) +{ + start_pm_main(); + return dbus_message_new_method_return(msg); +} + +static DBusMessage* e_dbus_stop_cb(E_DBus_Object *obj, DBusMessage* msg) +{ + end_pm_main(); + return dbus_message_new_method_return(msg); +} + +static DBusMessage* e_dbus_getbrightness_cb(E_DBus_Object *obj, DBusMessage* msg) +{ + DBusMessageIter iter; + DBusMessage *reply; + int ret; + int cmd; + int brightness = -1; + + COMBINE_DISP_CMD(cmd, PROP_DISPLAY_BRIGHTNESS, DEFAULT_DISPLAY); + ret = device_get_property(DEVICE_TYPE_DISPLAY, cmd, &brightness); + + LOGINFO("get brightness %d, %d", brightness, ret); + + reply = dbus_message_new_method_return(msg); + dbus_message_iter_init_append(reply, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &brightness); + + return reply; +} + +static DBusMessage* e_dbus_setbrightness_cb(E_DBus_Object *obj, DBusMessage* msg) +{ + DBusMessageIter iter; + DBusMessage *reply; + int ret; + int cmd; + int brightness; + + dbus_message_iter_init(msg, &iter); + dbus_message_iter_get_basic(&iter, &brightness); + + COMBINE_DISP_CMD(cmd, PROP_DISPLAY_BRIGHTNESS, DEFAULT_DISPLAY); + ret = device_set_property(DEVICE_TYPE_DISPLAY, cmd, brightness); + + LOGINFO("set brightness %d, %d", brightness, ret); + + reply = dbus_message_new_method_return(msg); + dbus_message_iter_init_append(reply, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret); + + return reply; +} + +static struct edbus_method { + const char *member; + const char *signature; + const char *reply_signature; + E_DBus_Method_Cb func; +} edbus_methods[] = { + { "start", NULL, NULL, e_dbus_start_cb }, + { "stop", NULL, NULL, e_dbus_stop_cb }, + { "getbrightness", NULL, "i", e_dbus_getbrightness_cb }, + { "setbrightness", "i", "i", e_dbus_setbrightness_cb }, + /* Add methods here */ +}; + +int init_pm_dbus(void) +{ + E_DBus_Interface *iface; + int ret; + int i; + + iface = get_edbus_interface(DEVICED_PATH_DISPLAY); + + LOGINFO("%s, %x", DEVICED_PATH_DISPLAY, iface); + + if (!iface) { + LOGERR("fail to get edbus interface!"); + return -1; + } + + for (i = 0; i < ARRAY_SIZE(edbus_methods); i++) { + ret = e_dbus_interface_method_add(iface, + edbus_methods[i].member, + edbus_methods[i].signature, + edbus_methods[i].reply_signature, + edbus_methods[i].func); + if (!ret) { + LOGERR("fail to add method %s!", edbus_methods[i].member); + return -1; + } + } + + return 0; +} + diff --git a/src/core/edbus-handler.c b/src/core/edbus-handler.c index be0177e..b8eacd0 100644 --- a/src/core/edbus-handler.c +++ b/src/core/edbus-handler.c @@ -21,6 +21,7 @@ #include "core/log.h" #include "core/data.h" #include "core/edbus-handler.h" +#include "core/common.h" #define EDBUS_INIT_RETRY_COUNT 5 @@ -29,10 +30,59 @@ struct edbus_list{ E_DBus_Signal_Handler *handler; }; +static struct edbus_object { + const char *path; + const char *interface; + E_DBus_Object *obj; + E_DBus_Interface *iface; +} edbus_objects[] = { + { DEVICED_PATH_CORE , DEVICED_INTERFACE_CORE , NULL, NULL }, + { DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY, NULL, NULL }, + /* Add new object & interface here*/ +}; + static Eina_List *edbus_handler_list; static int edbus_init_val; static E_DBus_Connection *edbus_conn; static DBusPendingCall *edbus_request_name; + +static int register_edbus_interface(struct edbus_object *object) +{ + int ret; + + if (!object) { + PRT_TRACE_ERR("object is invalid value!"); + return -1; + } + + object->obj = e_dbus_object_add(edbus_conn, object->path, NULL); + if (!object->obj) { + PRT_TRACE_ERR("fail to add edbus obj"); + return -1; + } + + object->iface = e_dbus_interface_new(object->interface); + if (!object->iface) { + PRT_TRACE_ERR("fail to add edbus interface"); + return -1; + } + + e_dbus_object_interface_attach(object->obj, object->iface); + + return 0; +} + +E_DBus_Interface *get_edbus_interface(const char *path) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) + if (!strcmp(path, edbus_objects[i].path)) + return edbus_objects[i].iface; + + return NULL; +} + static void unregister_edbus_signal_handle(void) { Eina_List *tmp; @@ -107,6 +157,7 @@ void edbus_fini(void) void edbus_init(void) { int retry = EDBUS_INIT_RETRY_COUNT; + int i, r; while (--retry) { edbus_init_val = e_dbus_init(); @@ -139,6 +190,16 @@ void edbus_init(void) goto err_dbus_close; } } + + for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) { + r = register_edbus_interface(&edbus_objects[i]); + if (r < 0) + _D("fail to add obj & interface for %s", + edbus_objects[i].interface); + + _D("add new obj for %s", edbus_objects[i].interface); + } + _D("start edbus service"); return; diff --git a/src/core/edbus-handler.h b/src/core/edbus-handler.h index ea40f0f..302ac8f 100644 --- a/src/core/edbus-handler.h +++ b/src/core/edbus-handler.h @@ -27,8 +27,26 @@ #define OBJECT_PATH "/Org/Tizen/System/DeviceD" #define INTERFACE_NAME BUS_NAME +/* + * Core service + * get/set device status + * operations about device + */ +#define DEVICED_PATH_CORE OBJECT_PATH"/Core" +#define DEVICED_INTERFACE_CORE INTERFACE_NAME".core" + +/* + * Display service + * start/stop display(pm) + * get/set brightness + * operations about display + */ +#define DEVICED_PATH_DISPLAY OBJECT_PATH"/Display" +#define DEVICED_INTERFACE_DISPLAY INTERFACE_NAME".display" + void edbus_init(void); void edbus_fini(void); int register_edbus_signal_handler(char *signal_name, E_DBus_Signal_Cb cb); +E_DBus_Interface *get_edbus_interface(const char *path); #endif /* __SS_EDBUS_HANDLE_H__ */ -- 2.7.4