#include <cp_config.h>
-class CBus {
+class CBaseBus {
+private:
+ struct SBaseBus* m;
+
+private:
+ /**
+ * Invoked function to register callbacks in interface table if bus is acquired.
+ *
+ * @param connection The GDBusConnection to a message bus
+ * @param name The name tha is requested to be owned
+ * @param data The bus object
+ */
+ static void sm_CbHandlerBusAcquired(GDBusConnection *conn, const gchar *name,
+ gpointer data);
+ /**
+ * Invoked when bus name is acquired and calls bus ready callback function
+ * to emit the SIGNAL_REFRESH signal to daemons.
+ *
+ * @param connection The GDBusConnection on which to acquired the name
+ * @param name The name being owned
+ * @param data The bus object
+ */
+ static void sm_CbHandlerNameAcquired(GDBusConnection *conn, const char *name,
+ gpointer data);
+ /**
+ * Invoked when bus name lost and calls bus end callback function.
+ *
+ * @param conn The GDBusConnection on which to acquire the name or NULL
+ * if the connection was disconected
+ * @param name The name being owned
+ * @param data The bus object
+ */
+ static void sm_CbHandlerNameLost(GDBusConnection *conn, const char *name,
+ gpointer data);
+
+ /**
+ * Registers method and handler callback function.
+ *
+ * @param conn The GDBusConnection
+ * @param sender The unique bus name
+ * @param obj_path The object path that the method was invoked on
+ * @param if_name The D-Bus interface name the method was invoked on
+ * @param method The name of the method that was invoked
+ * @param param The message the remote caller would send
+ * @param iv The GDBusMethodInvocation object that can be used to return a value
+ * @param data The bus object
+ */
+ static void sm_CbMethodCall(GDBusConnection *conn,
+ const gchar *sender, const gchar *obj_path,
+ const gchar *if_name, const gchar *method,
+ GVariant *param, GDBusMethodInvocation *iv,
+ gpointer data);
+
+protected:
+ void t_OnBusAcquired(GDBusConnection *conn, const gchar *name);
+ void t_OnNameAcquired(GDBusConnection *conn, const char *name);
+ void t_OnNameLost(GDBusConnection *conn, const char *name);
+
+ virtual void t_OnReady(void);
+ virtual void t_OnEnd(void);
+
+public:
+ struct SCallback {
+ void *cookie;
+ void(*cbReady)(CBaseBus *bus, void *cookie);
+ void(*cbEnd)(CBaseBus *bus, void *cookie);
+ };
+
+protected:
+ virtual bool t_OnEvent(const char *method, GVariant *param) { return false; }
+
+private:
+ /**
+ * Initialize bus for sending the dynamic bar contents.
+ *
+ * Parses BUS_INTROSPECTION_XML data and creates bus.
+ *
+ * @param bus The bus object
+ * @return This function returns zero on success, or negative value
+ */
+ bool m_Create(void);
+ void m_Destroy(void);
+
+public:
+ CBaseBus() : m(0) {}
+ virtual ~CBaseBus() {}
+ bool FlagCreate(void) { return !!m; }
+
+ bool Create(const SCallback *callback);
+ virtual void Destroy(void);
+
+ bool SendSignal(const char *sig);
+};
+
+
+class CBus : public CBaseBus {
public:
enum EActionOp {
ACTION_OP_LAUNCH_NONE,
const char *args;
};
- struct SCallback {
- void (*ready)(CBus *bus, void *data);
-
+ struct SCallback : public CBaseBus::SCallback {
bool (*cbAddLabel)(SLabel *opdata, void *data);
bool (*cbAddAlbum)(SAlbum *opdata, SAction *act, void *data);
bool (*cbAddContent)(SContent *opdata, SAction *act, void *data);
bool (*cbAddPin)(SPin *opdata, SAction *act, void *data);
bool (*cbDelContent)(const char *svcid, int ctnt_id, void *data);
bool (*cbDelAll)(const char *svcid, bool reload, void *data);
-
- void (*end)(CBus *bus, void *data);
};
private:
struct SBus *m;
-private:
- /**
- * Invoked function to register callbacks in interface table if bus is acquired.
- *
- * @param connection The GDBusConnection to a message bus
- * @param name The name tha is requested to be owned
- * @param data The bus object
- */
- static void sm_CbHandlerBusAcquired(GDBusConnection *conn, const gchar *name,
- gpointer data);
- /**
- * Invoked when bus name is acquired and calls bus ready callback function
- * to emit the SIGNAL_REFRESH signal to daemons.
- *
- * @param connection The GDBusConnection on which to acquired the name
- * @param name The name being owned
- * @param data The bus object
- */
- static void sm_CbHandlerNameAcquired(GDBusConnection *conn, const char *name,
- gpointer data);
- /**
- * Invoked when bus name lost and calls bus end callback function.
- *
- * @param conn The GDBusConnection on which to acquire the name or NULL
- * if the connection was disconected
- * @param name The name being owned
- * @param data The bus object
- */
- static void sm_CbHandlerNameLost(GDBusConnection *conn, const char *name,
- gpointer data);
-
- /**
- * Registers method and handler callback function.
- *
- * @param conn The GDBusConnection
- * @param sender The unique bus name
- * @param obj_path The object path that the method was invoked on
- * @param if_name The D-Bus interface name the method was invoked on
- * @param method The name of the method that was invoked
- * @param param The message the remote caller would send
- * @param iv The GDBusMethodInvocation object that can be used to return a value
- * @param data The bus object
- */
-public:
- static void sm_CbMethodCall(GDBusConnection *conn,
- const gchar *sender, const gchar *obj_path,
- const gchar *if_name, const gchar *method,
- GVariant *param, GDBusMethodInvocation *iv,
- gpointer data);
-
protected:
- void t_OnBusAcquired(GDBusConnection *conn, const gchar *name);
- void t_OnNameAcquired(GDBusConnection *conn, const char *name);
- void t_OnNameLost(GDBusConnection *conn, const char *name);
-
+ virtual bool t_OnEvent(const char *method, GVariant *param);
bool t_EventAddLabel(GVariant *param);
bool t_EventAddAlbum(GVariant *param);
bool t_EventDelContent(GVariant *param);
bool t_EventDelAllContent(GVariant *param);
-private:
- bool _init_bus(void);
public:
//! Constructor
CBus() : m(0) {}
bool FlagCreate(void) { return !!m; }
//! Initializes bus.
- bool Create(const SCallback *callback, void *data);
+ bool Create(const SCallback *callback);
//! Finalizes bus.
virtual void Destroy(void);
-
- //! Invoked for sending the signal to bus.
- bool SendSignal(const char *sig);
};
#include "dbg.h"
-#define ASSERT(exp) if (!(exp)) { int *a = 0; *a = 5; }
+
+#define ASSERT(exp) if (!(exp)) { _DBG(#exp); int* a = 0; *a = 5; }
+
+#define _CREATE_BEGIN int ___fail = 0;
+#define _CHECK(exp) do { if (!(exp)) { ___fail = 1; _DBG("_CHECK FAIL"); break; }
+#define _CHECK_FAIL } while (0); if (___fail != 0) ___fail++; if (___fail > 2)
+#define _CREATE_END_AND_CATCH if (___fail != 0)
+#define _WHEN_SUCCESS if (___fail == 0)
+#define _COMMAND if (___fail == 0)
+
+
#define RESEND_TIMEOUT 5
#define ADDRESS_BUF_MAX 128
#define TLM_SEAT_ID "seat0"
#define TLM_DBUS_ROOT_SOCKET_PATH "/var/run/tlm/dbus-sock"
-struct SBus {
- guint owner_id;
- GDBusNodeInfo *node;
+
+// CBaseBus Implement Code
+
+
+struct SBaseBus {
+ CBaseBus::SCallback cb;
+
+ guint owner_id;
+ GDBusNodeInfo *node;
GDBusConnection *conn;
- CBus::SCallback cbs;
- void *cb_data;
};
-void CBus::sm_CbHandlerBusAcquired(GDBusConnection *conn, const gchar *name,
- gpointer data)
+
+void CBaseBus::sm_CbHandlerBusAcquired(GDBusConnection *conn, const gchar *name,
+ gpointer data)
{
- CBus *root = (CBus*)data;
+ CBaseBus *root = (CBaseBus*)data;
if (root)
root->t_OnBusAcquired(conn, name);
}
-void CBus::sm_CbHandlerNameAcquired(GDBusConnection *conn, const char *name,
- gpointer data)
+void CBaseBus::sm_CbHandlerNameAcquired(GDBusConnection *conn, const char *name,
+ gpointer data)
{
- CBus *root = (CBus*)data;
+ CBaseBus *root = (CBaseBus*)data;
if (root)
root->t_OnNameAcquired(conn, name);
}
-void CBus::sm_CbHandlerNameLost(GDBusConnection *conn, const char *name,
- gpointer data)
+void CBaseBus::sm_CbHandlerNameLost(GDBusConnection *conn, const char *name,
+ gpointer data)
{
- CBus *root = (CBus*)data;
+ CBaseBus *root = (CBaseBus*)data;
if (root)
root->t_OnNameLost(conn, name);
}
-static const GDBusInterfaceVTable _bus_ifaces = {
- CBus::sm_CbMethodCall,
- NULL,
- NULL
-};
-void CBus::t_OnBusAcquired(GDBusConnection *conn, const gchar *name)
+void CBaseBus::sm_CbMethodCall(GDBusConnection *conn,
+ const gchar *sender, const gchar *obj_path,
+ const gchar *if_name, const gchar *method,
+ GVariant *param, GDBusMethodInvocation *iv,
+ gpointer data)
+{
+ CBaseBus *bus = (CBaseBus*)data;
+ int r;
+
+ if (!data || !param || !iv) {
+ _ERR("Invalid arguments");
+ return;
+ }
+
+ r = bus->t_OnEvent(method, param);
+
+ g_dbus_method_invocation_return_value(iv, g_variant_new("(i)", r));
+}
+
+
+void CBaseBus::t_OnBusAcquired(GDBusConnection *conn, const gchar *name)
{
GError *err;
guint regid;
+ static const GDBusInterfaceVTable _bus_ifaces = {
+ CBaseBus::sm_CbMethodCall,
+ NULL,
+ NULL
+ };
if (!conn) {
_ERR("Invalid argument");
err = NULL;
regid = g_dbus_connection_register_object(conn,
- BUS_OBJECT_PATH,
- m->node->interfaces[0],
- &_bus_ifaces,
- this, NULL, &err);
+ BUS_OBJECT_PATH,
+ m->node->interfaces[0],
+ &_bus_ifaces,
+ this, NULL, &err);
if (!regid) {
_ERR("register object failed: %s", err ? err->message : "");
g_error_free(err);
- if (!m->cbs.end)
- return;
- m->cbs.end(this, m->cb_data);
+
+ t_OnEnd();
return;
}
m->conn = conn;
}
-void CBus::t_OnNameAcquired(GDBusConnection *conn, const char *name)
+void CBaseBus::t_OnNameAcquired(GDBusConnection *conn, const char *name)
{
if (!conn) {
_ERR("Invalid argument");
}
_DBG("Name acquired: %s", name);
-
- if (!m->cbs.ready)
- return;
- m->cbs.ready(this, m->cb_data);
+
+ t_OnReady();
}
-void CBus::t_OnNameLost(GDBusConnection *conn, const char *name)
+
+void CBaseBus::t_OnNameLost(GDBusConnection *conn, const char *name)
{
if (!conn) {
_ERR("Invalid argument");
}
_DBG("Name lost: %s", name);
- if (!m->cbs.end)
- return;
+ t_OnEnd();
+}
+
- m->cbs.end(this, m->cb_data);
+void CBaseBus::t_OnReady(void)
+{
+ if (m->cb.cbReady)
+ m->cb.cbReady(this, m->cb.cookie);
}
+void CBaseBus::t_OnEnd(void)
+{
+ if (m->cb.cbEnd) {
+ m->cb.cbEnd(this, m->cb.cookie);
+ }
+}
+bool CBaseBus::m_Create(void)
+{
+ guint owner_id;
+ GDBusNodeInfo *node;
+ GError *err;
+ err = NULL;
+ node = g_dbus_node_info_new_for_xml(BUS_INTROSPECTION_XML, &err);
+ if (!node) {
+ _ERR("get node failed: %s", err->message);
+ g_error_free(err);
+ return false;
+ }
+
+ _CREATE_BEGIN{
+ _CHECK(node = g_dbus_node_info_new_for_xml(BUS_INTROSPECTION_XML, &err))
+ _CHECK(node->interfaces)
+ _CHECK(owner_id = g_bus_own_name(BUS_TYPE, BUS_NAME,
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ sm_CbHandlerBusAcquired,
+ sm_CbHandlerNameAcquired,
+ sm_CbHandlerNameLost, this, NULL))
+
+ _CHECK_FAIL{ /* */ }
+ _CHECK_FAIL{ /* */ }
+ _CHECK_FAIL{ g_object_unref(node); }
+ } _CREATE_END_AND_CATCH{ return false; }
+
+ m->node = node;
+ m->owner_id = owner_id;
+
+ return true;
+}
+
+
+void CBaseBus::m_Destroy(void)
+{
+ g_dbus_node_info_unref(m->node);
+ g_bus_unown_name(m->owner_id);
+}
+
+
+bool CBaseBus::Create(const SCallback *callback)
+{
+ ASSERT(!m);
+
+ m = new SBaseBus;
+ if (!m)
+ return false;
+ if (!m_Create()) {
+ delete m;
+ m = NULL;
+ return false;
+ }
+ m->cb = *callback;
+ return true;
+}
+void CBaseBus::Destroy(void)
+{
+ ASSERT(m);
+ m_Destroy();
+ delete m;
+ m = NULL;
+}
+bool CBaseBus::SendSignal(const char *sig)
+{
+ ASSERT(m);
+ ASSERT(sig);
+ GError *err = NULL;
+ gboolean r;
+
+ r = g_dbus_connection_emit_signal(m->conn, NULL,
+ BUS_OBJECT_PATH, BUS_INTERFACE, sig, NULL, &err);
+ if (r == FALSE) {
+ _ERR("emit %s signal failed: %s\n",
+ sig, err ? err->message : NULL);
+ g_error_free(err);
+ return false;
+ }
+
+ return true;
+}
+
+
+// CBus Implement Code
+
+struct SBus {
+ CBus::SCallback cb;
+};
/**
}
-
-void CBus::sm_CbMethodCall(GDBusConnection *conn,
- const gchar *sender, const gchar *obj_path,
- const gchar *if_name, const gchar *method,
- GVariant *param, GDBusMethodInvocation *iv,
- gpointer data)
+bool CBus::t_OnEvent(const char *method, GVariant *param)
{
- CBus *bus = (CBus*)data;
- int r;
-
- if (!data || !param || !iv) {
+ if (!param) {
_ERR("Invalid arguments");
- return;
+ return false;
}
- r = -1;
if (strcmp(BUS_METHOD_ADD_ALBUM, method) == 0) {
- r = bus->t_EventAddAlbum(param);
+ t_EventAddAlbum(param);
}
else if (strcmp(BUS_METHOD_ADD_LABEL, method) == 0) {
- r = bus->t_EventAddLabel(param);
+ t_EventAddLabel(param);
}
else if (strcmp(BUS_METHOD_ADD_CONTENT, method) == 0) {
- r = bus->t_EventAddContent(param);
+ t_EventAddContent(param);
}
else if (strcmp(BUS_METHOD_ADD_PIN, method) == 0) {
- r = bus->t_EventAddPin(param);
+ t_EventAddPin(param);
}
else if (strcmp(BUS_METHOD_DEL_CONTENT, method) == 0) {
- r = bus->t_EventDelAllContent(param);
+ t_EventDelAllContent(param);
}
else if (strcmp(BUS_METHOD_DEL_ALL, method) == 0) {
- r = bus->t_EventDelAllContent(param);
+ t_EventDelAllContent(param);
}
- g_dbus_method_invocation_return_value(iv, g_variant_new("(i)", r));
-}
-
-/**
- * Initialize bus for sending the dynamic bar contents.
- *
- * Parses BUS_INTROSPECTION_XML data and creates bus.
- *
- * @param bus The bus object
- * @return This function returns zero on success, or negative value
- */
-bool CBus::_init_bus(void)
-{
- guint owner_id;
- GDBusNodeInfo *node;
- GError *err;
-
- err = NULL;
- node = g_dbus_node_info_new_for_xml(BUS_INTROSPECTION_XML, &err);
- if (!node) {
- _ERR("get node failed: %s", err->message);
- g_error_free(err);
- return false;
- }
-
- if (!node->interfaces) {
- _ERR("no interface received");
- goto unref_node;
- }
-
- owner_id = g_bus_own_name(BUS_TYPE, BUS_NAME,
- G_BUS_NAME_OWNER_FLAGS_NONE,
- sm_CbHandlerBusAcquired,
- sm_CbHandlerNameAcquired,
- sm_CbHandlerNameLost, this, NULL);
- if (!owner_id) {
- _ERR("bus own name failed");
- goto unref_node;
- }
-
- m->node = node;
- m->owner_id = owner_id;
-
return true;
-
-unref_node:
- g_object_unref(node);
-
- return false;
}
SLabel data;
GVariantIter iter;
- if (!m->cbs.cbAddLabel) {
+ if (!m->cb.cbAddLabel) {
_ERR("Invalid argument");
return false;
}
return false;
}
- return m->cbs.cbAddLabel(&data, m->cb_data);
+ return m->cb.cbAddLabel(&data, m->cb.cookie);
}
GVariantIter iter;
int r;
- if (!m->cbs.cbAddAlbum || !param) {
+ if (!m->cb.cbAddAlbum || !param) {
_ERR("Invalid argument");
return false;
}
act.actor = _get_iter_string(&iter);
act.args = _get_iter_string(&iter);
- r = m->cbs.cbAddAlbum(&data, &act, m->cb_data);
+ r = m->cb.cbAddAlbum(&data, &act, m->cb.cookie);
_free_content_array(data.contents, BUS_DATA_ALBUM_CONTENT_MAX);
return r;
struct SAction act;
GVariantIter iter;
- if (!m->cbs.cbAddContent || !param) {
+ if (!m->cb.cbAddContent || !param) {
_ERR("Invalid argument");
return false;
}
act.actor = _get_iter_string(&iter);
act.args = _get_iter_string(&iter);
- return m->cbs.cbAddContent(&data, &act, m->cb_data);
+ return m->cb.cbAddContent(&data, &act, m->cb.cookie);
}
struct SAction act;
GVariantIter iter;
- if (!m->cbs.cbAddPin || !param) {
+ if (!m->cb.cbAddPin || !param) {
_ERR("Invalid argument");
return false;
}
act.actor = _get_iter_string(&iter);
act.args = _get_iter_string(&iter);
- return m->cbs.cbAddPin(&data, &act, m->cb_data);
+ return m->cb.cbAddPin(&data, &act, m->cb.cookie);
}
const gchar *svcid;
int ctnt_id;
- if (!m->cbs.cbDelContent || !param) {
+ if (!m->cb.cbDelContent || !param) {
_ERR("Invalid argument");
return false;
}
return false;
}
- return m->cbs.cbDelContent(svcid, ctnt_id, m->cb_data);
+ return m->cb.cbDelContent(svcid, ctnt_id, m->cb.cookie);
}
const gchar *svcid;
bool reload;
- if (!m->cbs.cbDelAll || !param) {
+ if (!m->cb.cbDelAll || !param) {
_ERR("Invalid argument");
return false;
}
reload = _get_iter_boolean(&iter);
- return m->cbs.cbDelAll(svcid, reload, m->cb_data);
+ return m->cb.cbDelAll(svcid, reload, m->cb.cookie);
}
-bool CBus::Create(const SCallback *cbs, void *data)
+bool CBus::Create(const SCallback *cb)
{
ASSERT(!FlagCreate());
return false;
}
- if (!_init_bus()) {
+ if (!CBaseBus::Create(cb)) {
delete m;
m = NULL;
return false;
}
- m->cbs = *cbs;
- m->cb_data = data;
+ m->cb = *cb;
return true;
}
+
void CBus::Destroy(void)
{
ASSERT(FlagCreate());
- g_dbus_node_info_unref(m->node);
- g_bus_unown_name(m->owner_id);
+ CBaseBus::Destroy();
delete m;
m = NULL;
}
-bool CBus::SendSignal(const char *sig)
-{
- ASSERT(FlagCreate());
-
- GError *err;
- gboolean r;
-
- if (!sig) {
- _ERR("Invalid argument\n");
- return false;
- }
-
- err = NULL;
- r = g_dbus_connection_emit_signal(m->conn, NULL,
- BUS_OBJECT_PATH, BUS_INTERFACE, sig, NULL, &err);
- if (r == FALSE) {
- _ERR("emit %s signal failed: %s\n",
- sig, err ? err->message : NULL);
- g_error_free(err);
- return false;
- }
-
- return true;
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-
-
-
-
-
-
+// CClientBus Implemente Code
struct SClientBus {