--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include <signal.h>
+#include <app_manager.h>
+
+#include <types_internal.h>
+#include "server.h"
+#include "client_request.h"
+#include "access_control/peer_creds.h"
+#include "DBusServer.h"
+
+using namespace ctx;
+
+static const gchar __introspection_xml[] =
+ "<node>"
+ " <interface name='" DBUS_IFACE "'>"
+ " <method name='" METHOD_REQUEST "'>"
+ " <arg type='i' name='" ARG_REQTYPE "' direction='in'/>"
+ " <arg type='s' name='" ARG_COOKIE "' direction='in'/>"
+ " <arg type='i' name='" ARG_REQID "' direction='in'/>"
+ " <arg type='s' name='" ARG_SUBJECT "' direction='in'/>"
+ " <arg type='s' name='" ARG_INPUT "' direction='in'/>"
+ " <arg type='i' name='" ARG_RESULT_ERR "' direction='out'/>"
+ " <arg type='s' name='" ARG_RESULT_ADD "' direction='out'/>"
+ " <arg type='s' name='" ARG_OUTPUT "' direction='out'/>"
+ " </method>"
+ " </interface>"
+ "</node>";
+
+DBusServer *DBusServer::__theInstance = NULL;
+
+DBusServer::DBusServer()
+ : __owner(-1)
+ , __connection(NULL)
+ , __nodeInfo(NULL)
+{
+}
+
+DBusServer::~DBusServer()
+{
+ __release();
+}
+
+void DBusServer::__processRequest(const char *sender, GVariant *param, GDBusMethodInvocation *invocation)
+{
+ gint reqType = 0;
+ const gchar *cookie = NULL;
+ gint reqId = 0;
+ const gchar *subject = NULL;
+ const gchar *input = NULL;
+
+ g_variant_get(param, "(i&si&s&s)", &reqType, &cookie, &reqId, &subject, &input);
+ IF_FAIL_VOID_TAG(reqType > 0 && reqId > 0 && cookie && subject && input, _E, "Invalid request");
+
+ _I("[%d] ReqId: %d, Subject: %s", reqType, reqId, subject);
+ _SI("Input: %s", input);
+
+ credentials *creds = NULL;
+
+ if (!peer_creds::get(__connection, sender, &creds)) {
+ _E("Peer credentialing failed");
+ g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
+ return;
+ }
+
+ client_request *request = new(std::nothrow) client_request(reqType, reqId, subject, input, creds, sender, invocation);
+ if (!request) {
+ _E("Memory allocation failed");
+ g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
+ delete creds;
+ return;
+ }
+
+ server::send_request(request);
+}
+
+void DBusServer::__onRequestReceived(GDBusConnection *conn, const gchar *sender,
+ const gchar *path, const gchar *iface, const gchar *name,
+ GVariant *param, GDBusMethodInvocation *invocation, gpointer userData)
+{
+ IF_FAIL_VOID_TAG(STR_EQ(path, DBUS_PATH), _W, "Invalid path: %s", path);
+ IF_FAIL_VOID_TAG(STR_EQ(iface, DBUS_IFACE), _W, "Invalid interface: %s", path);
+
+ if (STR_EQ(name, METHOD_REQUEST)) {
+ __theInstance->__processRequest(sender, param, invocation);
+ } else {
+ _W("Invalid method: %s", name);
+ }
+}
+
+void DBusServer::__onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+ GDBusInterfaceVTable vtable;
+ vtable.method_call = __onRequestReceived;
+ vtable.get_property = NULL;
+ vtable.set_property = NULL;
+
+ guint regId = g_dbus_connection_register_object(conn, DBUS_PATH,
+ __theInstance->__nodeInfo->interfaces[0], &vtable, NULL, NULL, NULL);
+
+ if (regId <= 0) {
+ _E("Failed to acquire dbus");
+ raise(SIGTERM);
+ }
+
+ __theInstance->__connection = conn;
+ _I("Dbus connection acquired");
+}
+
+void DBusServer::__onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+ _SI("Dbus name acquired: %s", name);
+ server::activate();
+}
+
+void DBusServer::__onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+ _E("Dbus name lost");
+ raise(SIGTERM);
+}
+
+void DBusServer::__onCallDone(GObject *source, GAsyncResult *res, gpointer userData)
+{
+ _I("Call %u done", *static_cast<unsigned int*>(userData));
+
+ GDBusConnection *conn = G_DBUS_CONNECTION(source);
+ GError *error = NULL;
+ g_dbus_connection_call_finish(conn, res, &error);
+ HANDLE_GERROR(error);
+}
+
+bool DBusServer::__init()
+{
+ __nodeInfo = g_dbus_node_info_new_for_xml(__introspection_xml, NULL);
+ IF_FAIL_RETURN_TAG(__nodeInfo != NULL, false, _E, "Initialization failed");
+
+ __owner = g_bus_own_name(G_BUS_TYPE_SESSION, DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
+ __onBusAcquired, __onNameAcquired, __onNameLost, NULL, NULL);
+
+ __theInstance = this;
+ return true;
+}
+
+void DBusServer::__release()
+{
+ if (__connection) {
+ g_dbus_connection_flush_sync(__connection, NULL, NULL);
+ }
+
+ if (__owner > 0) {
+ g_bus_unown_name(__owner);
+ __owner = 0;
+ }
+
+ if (__connection) {
+ g_dbus_connection_close_sync(__connection, NULL, NULL);
+ g_object_unref(__connection);
+ __connection = NULL;
+ }
+
+ if (__nodeInfo) {
+ g_dbus_node_info_unref(__nodeInfo);
+ __nodeInfo = NULL;
+ }
+}
+
+void DBusServer::__publish(const char *dest, int reqId, const char *subject, int error, const char *data)
+{
+ _SI("Publish: %s, %d, %s, %#x, %s", dest, reqId, subject, error, data);
+
+ GVariant *param = g_variant_new("(isis)", reqId, subject, error, data);
+ IF_FAIL_VOID_TAG(param, _E, "Memory allocation failed");
+
+ g_dbus_connection_call(__connection, dest, DBUS_PATH, DBUS_IFACE,
+ METHOD_RESPOND, param, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, NULL, NULL);
+}
+
+void DBusServer::__call(const char *dest, const char *obj, const char *iface, const char *method, GVariant *param)
+{
+ static unsigned int callCount = 0;
+ ++callCount;
+
+ _SI("Call %u: %s, %s, %s.%s", callCount, dest, obj, iface, method);
+
+ g_dbus_connection_call(__connection, dest, obj, iface, method, param, NULL,
+ G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, __onCallDone, &callCount);
+}
+
+void DBusServer::publish(std::string dest, int reqId, std::string subject, int error, std::string data)
+{
+ IF_FAIL_VOID_TAG(__theInstance, _E, "Not initialized");
+ __theInstance->__publish(dest.c_str(), reqId, subject.c_str(), error, data.c_str());
+}
+
+void DBusServer::call(std::string dest, std::string obj, std::string iface, std::string method, GVariant *param)
+{
+ IF_FAIL_VOID_TAG(__theInstance, _E, "Not initialized");
+ __theInstance->__call(dest.c_str(), obj.c_str(), iface.c_str(), method.c_str(), param);
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef _CONTEXT_DBUS_SERVER_H_
+#define _CONTEXT_DBUS_SERVER_H_
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <string>
+
+namespace ctx {
+
+ class DBusServer {
+ public:
+ ~DBusServer();
+
+ static void publish(std::string dest, int reqId, std::string subject, int error, std::string data);
+ static void call(std::string dest, std::string obj, std::string iface, std::string method, GVariant *param);
+
+ private:
+ DBusServer();
+
+ static void __onRequestReceived(GDBusConnection *conn, const gchar *sender,
+ const gchar *path, const gchar *iface, const gchar *name,
+ GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data);
+ static void __onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+ static void __onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+ static void __onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData);
+ static void __onCallDone(GObject *source, GAsyncResult *res, gpointer userData);
+
+ bool __init();
+ void __release();
+ void __publish(const char *dest, int reqId, const char *subject, int error, const char *data);
+ void __call(const char *dest, const char *obj, const char *iface, const char *method, GVariant *param);
+
+ void __processRequest(const char *sender, GVariant *param, GDBusMethodInvocation *invocation);
+
+ static DBusServer *__theInstance;
+
+ guint __owner;
+ GDBusConnection *__connection;
+ GDBusNodeInfo *__nodeInfo;
+
+ friend class server;
+
+ }; /* class ctx::DBusServer */
+
+} /* namespace ctx */
+
+#endif /* End of _CONTEXT_DBUS_SERVER_H_ */
#include <glib.h>
#include <app_manager.h>
#include <types_internal.h>
-#include "dbus_server_impl.h"
+#include "DBusServer.h"
#include "access_control/peer_creds.h"
#include "client_request.h"
bool ctx::client_request::publish(int error, ctx::json& data)
{
- char *data_str = data.dup_cstr();
- IF_FAIL_RETURN_TAG(data_str, false, _E, "Memory allocation failed");
-
- dbus_server::publish(__dbus_sender.c_str(), _req_id, _subject.c_str(), error, data_str);
- g_free(data_str);
-
+ DBusServer::publish(__dbus_sender, _req_id, _subject, error, data.str());
return true;
}
#include <system_settings.h>
#include <context_trigger_types_internal.h>
#include <json.h>
-#include "../dbus_server_impl.h"
+#include "../DBusServer.h"
#include "action_manager.h"
static void trigger_action_app_control(ctx::json& action);
action.get(NULL, CT_RULE_ACTION_DBUS_PARAMETER, ¶m);
- ctx::dbus_server::call(bus_name.c_str(), object.c_str(), iface.c_str(), method.c_str(), param);
+ ctx::DBusServer::call(bus_name, object, iface, method, param);
}
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * 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.
- */
-
-#include <signal.h>
-#include <glib.h>
-#include <gio/gio.h>
-#include <app_manager.h>
-
-#include <types_internal.h>
-#include "server.h"
-#include "client_request.h"
-#include "access_control/peer_creds.h"
-#include "dbus_server_impl.h"
-
-static bool conn_acquired = false;
-static bool name_acquired = false;
-static ctx::dbus_server_impl *_instance = NULL;
-static GDBusConnection *dbus_conn_session = NULL;
-static guint dbus_owner_id = 0;
-static GDBusNodeInfo *dbus_node_info = NULL;
-
-static const gchar introspection_xml[] =
- "<node>"
- " <interface name='" DBUS_IFACE "'>"
- " <method name='" METHOD_REQUEST "'>"
- " <arg type='i' name='" ARG_REQTYPE "' direction='in'/>"
- " <arg type='s' name='" ARG_COOKIE "' direction='in'/>"
- " <arg type='i' name='" ARG_REQID "' direction='in'/>"
- " <arg type='s' name='" ARG_SUBJECT "' direction='in'/>"
- " <arg type='s' name='" ARG_INPUT "' direction='in'/>"
- " <arg type='i' name='" ARG_RESULT_ERR "' direction='out'/>"
- " <arg type='s' name='" ARG_RESULT_ADD "' direction='out'/>"
- " <arg type='s' name='" ARG_OUTPUT "' direction='out'/>"
- " </method>"
- " </interface>"
- "</node>";
-
-static const char* req_type_to_str(int req_type)
-{
- switch (req_type) {
- case REQ_SUBSCRIBE:
- return "Subscribe";
- case REQ_UNSUBSCRIBE:
- return "Unsubscribe";
- case REQ_READ:
- return "Read";
- case REQ_READ_SYNC:
- return "Read (Sync)";
- case REQ_WRITE:
- return "Write";
- default:
- return NULL;
- }
-}
-
-static void handle_request(const char *sender, GVariant *param, GDBusMethodInvocation *invocation)
-{
- gint req_type = 0;
- const gchar *cookie = NULL;
- gint req_id = 0;
- const gchar *subject = NULL;
- const gchar *input = NULL;
-
- g_variant_get(param, "(i&si&s&s)", &req_type, &cookie, &req_id, &subject, &input);
- IF_FAIL_VOID_TAG(req_type > 0 && req_id > 0 && cookie && subject && input, _E, "Invalid request");
-
- _I("[%s] ReqId: %d, Subject: %s", req_type_to_str(req_type), req_id, subject);
- _SI("Input: %s", input);
-
- ctx::credentials *creds = NULL;
-
- if (!ctx::peer_creds::get(dbus_conn_session, sender, &creds)) {
- _E("Peer credentialing failed");
- g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
- return;
- }
-
- ctx::client_request *request = new(std::nothrow) ctx::client_request(req_type, req_id, subject, input, creds, sender, invocation);
- if (!request) {
- _E("Memory allocation failed");
- g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
- delete creds;
- return;
- }
-
- ctx::server::send_request(request);
-}
-
-static void handle_method_call(GDBusConnection *conn, const gchar *sender,
- const gchar *obj_path, const gchar *iface, const gchar *method_name,
- GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
-{
- IF_FAIL_VOID_TAG(STR_EQ(obj_path, DBUS_PATH), _W, "Invalid path: %s", obj_path);
- IF_FAIL_VOID_TAG(STR_EQ(iface, DBUS_IFACE), _W, "Invalid interface: %s", obj_path);
-
- if (STR_EQ(method_name, METHOD_REQUEST)) {
- handle_request(sender, param, invocation);
- } else {
- _W("Invalid method: %s", method_name);
- }
-}
-
-static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
-{
- GDBusInterfaceVTable vtable;
- vtable.method_call = handle_method_call;
- vtable.get_property = NULL;
- vtable.set_property = NULL;
-
- guint reg_id = g_dbus_connection_register_object(conn, DBUS_PATH,
- dbus_node_info->interfaces[0], &vtable, NULL, NULL, NULL);
-
- if (reg_id <= 0) {
- _E("Failed to acquire dbus");
- raise(SIGTERM);
- }
-
- conn_acquired = true;
- dbus_conn_session = conn;
-
- _I("Dbus connection acquired");
-
- if (name_acquired)
- ctx::server::activate();
-}
-
-static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
-{
- name_acquired = true;
- _SI("Dbus name acquired: %s", name);
-
- if (conn_acquired)
- ctx::server::activate();
-}
-
-static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data)
-{
- _E("Dbus name lost");
- raise(SIGTERM);
-}
-
-ctx::dbus_server_impl::dbus_server_impl()
-{
-}
-
-ctx::dbus_server_impl::~dbus_server_impl()
-{
- release();
-}
-
-bool ctx::dbus_server_impl::init()
-{
- IF_FAIL_RETURN_TAG(dbus_node_info == NULL, false, _E, "Re-initialization");
-
- dbus_node_info = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
- IF_FAIL_RETURN_TAG(dbus_node_info != NULL, false, _E, "Initialization failed");
-
- dbus_owner_id = g_bus_own_name(G_BUS_TYPE_SESSION, DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
- on_bus_acquired, on_name_acquired, on_name_lost, NULL, NULL);
-
- _instance = this;
- return true;
-}
-
-void ctx::dbus_server_impl::release()
-{
- if (dbus_conn_session) {
- g_dbus_connection_flush_sync(dbus_conn_session, NULL, NULL);
- }
-
- if (dbus_owner_id > 0) {
- g_bus_unown_name(dbus_owner_id);
- dbus_owner_id = 0;
- }
-
- if (dbus_conn_session) {
- g_dbus_connection_close_sync(dbus_conn_session, NULL, NULL);
- g_object_unref(dbus_conn_session);
- dbus_conn_session = NULL;
- }
-
- if (dbus_node_info) {
- g_dbus_node_info_unref(dbus_node_info);
- dbus_node_info = NULL;
- }
-}
-
-void ctx::dbus_server_impl::publish(const char* dest, int req_id, const char* subject, int error, const char* data)
-{
- IF_FAIL_VOID_TAG(dest && subject && data, _E, "Parameter null");
-
- _SI("Publish: %s, %d, %s, %#x, %s", dest, req_id, subject, error, data);
-
- GVariant *param = g_variant_new("(isis)", req_id, subject, error, data);
- IF_FAIL_VOID_TAG(param, _E, "Memory allocation failed");
-
- g_dbus_connection_call(dbus_conn_session, dest, DBUS_PATH, DBUS_IFACE,
- METHOD_RESPOND, param, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, NULL, NULL);
-}
-
-static void handle_call_result(GObject *source, GAsyncResult *res, gpointer user_data)
-{
- _I("Call %u done", *static_cast<unsigned int*>(user_data));
-
- GDBusConnection *conn = G_DBUS_CONNECTION(source);
- GError *error = NULL;
- g_dbus_connection_call_finish(conn, res, &error);
- HANDLE_GERROR(error);
-}
-
-void ctx::dbus_server_impl::call(const char *dest, const char *obj, const char *iface, const char *method, GVariant *param)
-{
- IF_FAIL_VOID_TAG(dest && obj && iface && method, _E, "Parameter null");
-
- static unsigned int call_count = 0;
- ++call_count;
-
- _SI("Call %u: %s, %s, %s.%s", call_count, dest, obj, iface, method);
-
- g_dbus_connection_call(dbus_conn_session, dest, obj, iface, method, param, NULL,
- G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, handle_call_result, &call_count);
-}
-
-void ctx::dbus_server::publish(const char* dest, int req_id, const char* subject, int error, const char* data)
-{
- _instance->publish(dest, req_id, subject, error, data);
-}
-
-void ctx::dbus_server::call(const char *dest, const char *obj, const char *iface, const char *method, GVariant *param)
-{
- _instance->call(dest, obj, iface, method, param);
-}
+++ /dev/null
-/*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * 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.
- */
-
-#ifndef __CONTEXT_DBUS_SERVER_IMPL_H__
-#define __CONTEXT_DBUS_SERVER_IMPL_H__
-
-#include <sys/types.h>
-#include <string>
-
-namespace ctx {
- class dbus_server_impl {
- public:
- dbus_server_impl();
- ~dbus_server_impl();
-
- bool init();
- void release();
-
- void publish(const char *dest, int req_id, const char *subject, int error, const char *data);
- void call(const char *dest, const char *obj, const char *iface, const char *method, GVariant *param);
-
- }; /* class ctx::dbus_server */
-
- namespace dbus_server {
- void publish(const char *dest, int req_id, const char *subject, int error, const char *data);
- void call(const char *dest, const char *obj, const char *iface, const char *method, GVariant *param);
- }
-} /* namespace ctx */
-
-#endif /* End of __CONTEXT_DBUS_SERVER_IMPL_H__ */
#include <glib-object.h>
#include <types_internal.h>
-#include "dbus_server_impl.h"
+#include "DBusServer.h"
#include "db_mgr_impl.h"
#include "timer_mgr_impl.h"
#include "context_mgr_impl.h"
static ctx::context_manager_impl *context_mgr = NULL;
static ctx::timer_manager_impl *timer_mgr = NULL;
static ctx::db_manager_impl *database_mgr = NULL;
-static ctx::dbus_server_impl *dbus_handle = NULL;
+static ctx::DBusServer *dbus_handle = NULL;
static ctx::context_trigger *trigger = NULL;
+/* TODO: re-organize activation & deactivation processes */
void ctx::server::initialize()
{
_I("Init MainLoop");
mainloop = g_main_loop_new(NULL, FALSE);
_I("Init Dbus Connection");
- dbus_handle = new(std::nothrow) ctx::dbus_server_impl();
+ dbus_handle = new(std::nothrow) ctx::DBusServer();
IF_FAIL_VOID_TAG(dbus_handle, _E, "Memory allocation failed");
- IF_FAIL_VOID_TAG(dbus_handle->init(), _E, "Initialization Failed");
+ IF_FAIL_VOID_TAG(dbus_handle->__init(), _E, "Initialization Failed");
// Start the main loop
_I(CYAN("Launching Context-Service"));
_I("Release Dbus Connection");
if (dbus_handle)
- dbus_handle->release();
+ dbus_handle->__release();
_I("Close the Database");
if (database_mgr)
class request_info;
- namespace server {
-
- void initialize();
- void activate();
- void release();
- void send_request(request_info* request);
-
- }; /* namespace ctx::server */
+ class server {
+ public:
+ static void initialize();
+ static void activate();
+ static void release();
+ static void send_request(request_info* request);
+
+ };
} /* namespace ctx */