Implementing request & response logic
authorSuresh Kumar N <suresh.n@samsung.com>
Thu, 29 Dec 2016 12:10:00 +0000 (17:40 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Thu, 11 May 2017 09:10:25 +0000 (18:10 +0900)
Change-Id: I5062e291324fc02aa1fb7385f6ca263b9223bf10
Signed-off-by: Suresh Kumar N <suresh.n@samsung.com>
zigbee-daemon/zigbee-interface/src/zigbee_service_dbus_interface.c
zigbee-daemon/zigbee-lib/include/zblib_driver.h
zigbee-daemon/zigbee-lib/include/zblib_plugin.h
zigbee-daemon/zigbee-lib/include/zblib_request.h
zigbee-daemon/zigbee-lib/include/zblib_service.h
zigbee-daemon/zigbee-lib/include/zblib_service_interface.h
zigbee-daemon/zigbee-lib/src/zblib_driver.c
zigbee-daemon/zigbee-lib/src/zblib_plugin.c
zigbee-daemon/zigbee-lib/src/zblib_request.c
zigbee-daemon/zigbee-lib/src/zblib_service.c
zigbee-daemon/zigbee-lib/src/zblib_service_interface.c

index 16e47a3..943d7b2 100644 (file)
@@ -234,17 +234,11 @@ static void zigbee_on_bus_acquired(GDBusConnection *connection,
        if (TRUE == custom_data->name_acquired) {
                /* TODO - Emit zigbee_state signal */
        }
+
 OUT:
        g_free(path);
 }
 
-/**< ZigBee D-BUS Service interface methods */
-struct zblib_service_interface_methods dbus_service_interface_methods = {
-       .send_response = NULL,
-       .send_notification = NULL,
-};
-
-
 /**< Zigbee service dbus interface initialization */
 gboolean zigbee_service_dbus_interface_init(ZigBeeService *service)
 {
@@ -261,8 +255,7 @@ gboolean zigbee_service_dbus_interface_init(ZigBeeService *service)
         * Create ZigBee service interface object
         */
        service_interface = zblib_service_interface_new(service,
-               ZIGBEE_DBUS_SERVICE_INTERFACE_NAME,
-               &dbus_service_interface_methods);
+               ZIGBEE_DBUS_SERVICE_INTERFACE_NAME);
        if (NULL == service_interface) {
                Z_LOGE("Create D-BUS service interface failed!");
 
index 715c7d4..ab73308 100644 (file)
@@ -44,4 +44,7 @@ ZblibDriverType_e zblib_driver_ref_driver_type(ZigBeeDriver *driver);
 
 gboolean zblib_driver_dispatch_request(ZigBeeDriver *driver, guint request_id);
 
+void zblib_driver_send_response(ZigBeeDriver *driver,
+       guint request_id, gpointer resp_data, guint resp_data_len);
+
 #endif /* __ZIGBEE_LIB_DRIVER_H__ */
index 6fd6c40..885c7bf 100644 (file)
 #define __ZIGBEE_LIB_PLUGIN_H__
 
 /**< ZigBee plug-in descriptor */
-struct zblib_plugin_descriptor {
+typedef struct {
        const gchar *name;
        int version;
        gboolean (*is_implemented)(); /**< Is plug-in implemented */
        gboolean (*load)(); /**< Load plugin */
        gboolean (*init)(ZigBeePlugin *); /**< Initialize plugin */
        void (*unload)(ZigBeePlugin *); /**< Unload plugin */
-};
+} ZblibPluginDescriptor_t;
 
 /* ZigBee vendor plug-in APIs */
 ZigBeePlugin *zblib_plugin_new(ZigBeeService *service,
        const gchar *plugin_name,
-       const struct zblib_plugin_descriptor *descriptor,
+       const ZblibPluginDescriptor_t *descriptor,
        void *plugin_handle);
 void zblib_plugin_free(ZigBeePlugin *plugin);
 
-const struct zblib_plugin_descriptor *zblib_plugin_get_descriptor(ZigBeePlugin *plugin);
+const ZblibPluginDescriptor_t *zblib_plugin_get_descriptor(ZigBeePlugin *plugin);
 char *zblib_plugin_get_plugin_name(ZigBeePlugin *plugin);
 
 ZigBeeDriver *zblib_plugin_ref_driver(ZigBeePlugin *plugin, ZblibDriverType_e driver_type);
 
+gboolean zblib_plugin_dispatch_request(ZigBeePlugin *plugin,
+       guint request_id);
+void zblib_plugin_send_response(ZigBeePlugin *plugin,
+       guint request_id, gpointer resp_data, guint resp_data_len);
+
 #endif /* __ZIGBEE_LIB_PLUGIN_H__ */
index 8bf56ca..605f664 100644 (file)
@@ -27,11 +27,18 @@ gint zblib_request_new(ZigBeeServiceInterface *service_interface,
 void zblib_request_free(ZigBeeServiceInterface *service_interface,
        guint request_id);
 
+gboolean zblib_request_set_response_cb(ZigBeeServiceInterface *service_interface,
+       guint request_id,
+       gpointer resp_cb, gpointer resp_cb_data);
+gpointer zblib_request_ref_response_cb(ZigBeeServiceInterface *service_interface,
+       guint request_id, gpointer *resp_cb_data);
+
 ZigBeeRequest *zblib_request_ref(ZigBeeRequest *request);
 void zblib_request_unref(ZigBeeRequest *request);
 
 guint zblib_request_ref_request_type(ZigBeeRequest *request);
-ZigBeeServiceInterface *zblib_request_ref_service_interface(ZigBeeRequest *request);
+ZigBeeServiceInterface *zblib_request_ref_service_interface(ZigBeeService *service,
+       guint request_id);
 gpointer zblib_request_ref_request_data(ZigBeeRequest *request);
 
 guint zblib_request_generate_request_type(ZblibDriverType_e driver_type,
index 8d21a27..2457d26 100644 (file)
@@ -45,4 +45,9 @@ ZigBeeServiceInterface *zblib_service_ref_service_interface(ZigBeeService *servi
 GHashTable *zblib_service_ref_request_hash_table(ZigBeeService *service);
 gint zblib_service_generate_request_id(ZigBeeService *service);
 
+gboolean zblib_service_dispatch_request(ZigBeeService *service,
+       guint request_id);
+void zblib_service_send_response(ZigBeeService *service,
+       guint request_id, gpointer resp_data, guint resp_data_len);
+
 #endif /* __ZIGBEE_LIB_SERVICE_H__ */
index 9eb0d9a..41e0e61 100644 (file)
 #ifndef __ZIGBEE_LIB_SERVICE_INTERFACE_H__
 #define __ZIGBEE_LIB_SERVICE_INTERFACE_H__
 
-/**< Service interface methods */
-struct zblib_service_interface_methods {
-       gboolean (*send_response)();
-       gboolean (*send_notification)();
-};
+/**< ZigBee Service interface response callback function pointer */
+typedef gboolean (*ZblibServiceInterfaceResponseCb_t)(ZigBeeServiceInterface *service_interface,
+       guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data);
 
 /**< Service interface APIs */
 ZigBeeServiceInterface *zblib_service_interface_new(ZigBeeService *service,
-       const gchar *object_name,
-       struct zblib_service_interface_methods *methods);
+       const gchar *object_name);
 void zblib_service_interface_free(ZigBeeService *service,
        ZigBeeServiceInterface *service_interface);
 
@@ -40,4 +37,14 @@ gpointer zblib_service_interface_ref_user_data(ZigBeeServiceInterface *service_i
 
 ZigBeeService *zblib_service_interface_ref_service(ZigBeeServiceInterface *service_interface);
 
+gint zblib_service_interface_new_request(ZigBeeServiceInterface *service_interface,
+       guint request_type,
+       gpointer request_data, guint request_data_len,
+       ZblibServiceInterfaceResponseCb_t resp_cb, gpointer resp_cb_data);
+
+gboolean zblib_service_interface_dispatch_request(ZigBeeServiceInterface *service_interface,
+       guint request_id);
+void zblib_service_interface_send_response(ZigBeeServiceInterface *service_interface,
+       guint request_id, gpointer resp_data, guint resp_data_len);
+
 #endif /* __ZIGBEE_LIB_SERVICE_INTERFACE_H__ */
index b6d1538..c4ccda6 100644 (file)
@@ -18,6 +18,8 @@
 
 #include <zblib.h>
 #include <zblib_driver.h>
+#include <zblib_plugin.h>
+#include <zblib_service.h>
 
 /**< ZigBee driver object */
 struct zblib_driver_type {
@@ -135,3 +137,18 @@ gboolean zblib_driver_dispatch_request(ZigBeeDriver *driver,
        /* Dispatch request to driver dispatcher function */
        return driver->dispatcher_fn(driver, request_id);
 }
+
+void zblib_driver_send_response(ZigBeeDriver *driver,
+       guint request_id, gpointer resp_data, guint resp_data_len)
+{
+       ZigBeePlugin *plugin = NULL;
+
+       zblib_check_null_ret("driver", driver);
+
+       plugin = driver->plugin;
+       zblib_check_null_ret("plugin", plugin);
+
+       /* Send response to plugin */
+       zblib_plugin_send_response(plugin,
+               request_id, resp_data, resp_data_len);
+}
index 539e9df..6afef3e 100644 (file)
 #include <zblib.h>
 #include <zblib_plugin.h>
 #include <zblib_driver.h>
+#include <zblib_service.h>
+#include <zblib_request.h>
 
 /**< ZigBee plug-in object */
 struct zblib_plugin_type {
        gchar *plugin_name; /**< ZigBee plug-in name */
-       const struct zblib_plugin_descriptor *descriptor; /**< ZigBee plug-in descriptor */
+       const ZblibPluginDescriptor_t *descriptor; /**< ZigBee plug-in descriptor */
        void *plugin_handle; /**< ZigBee Plug-in .so handle */
 
        GSList *driver_list; /**< List of ZigBee drivers */
@@ -35,7 +37,7 @@ struct zblib_plugin_type {
 
 ZigBeePlugin *zblib_plugin_new(ZigBeeService *service,
        const gchar *plugin_name,
-       const struct zblib_plugin_descriptor *descriptor,
+       const ZblibPluginDescriptor_t *descriptor,
        void *plugin_handle)
 {
        ZigBeePlugin *plugin = NULL;
@@ -56,10 +58,7 @@ ZigBeePlugin *zblib_plugin_new(ZigBeeService *service,
 
 void zblib_plugin_free(ZigBeePlugin *plugin)
 {
-       if (NULL == plugin) {
-               Z_LOGE("ZigBee vendor plug-in is NULL");
-               return;
-       }
+       zblib_check_null_ret("plugin", plugin);
 
        Z_LOGI("Freeing Vendor plug-in - Name [%s]", plugin->descriptor->name);
 
@@ -95,22 +94,16 @@ void zblib_plugin_free(ZigBeePlugin *plugin)
        g_free(plugin);
 }
 
-const struct zblib_plugin_descriptor *zblib_plugin_get_descriptor(ZigBeePlugin *plugin)
+const ZblibPluginDescriptor_t *zblib_plugin_get_descriptor(ZigBeePlugin *plugin)
 {
-       if (NULL == plugin) {
-               Z_LOGE("ZigBee vendor plug-in is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("plugin", plugin, NULL);
 
        return plugin->descriptor;
 }
 
 char *zblib_plugin_get_plugin_name(ZigBeePlugin *plugin)
 {
-       if (NULL == plugin) {
-               Z_LOGE("ZigBee vendor plug-in is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("plugin", plugin, NULL);
 
        return g_strdup(plugin->plugin_name);
 }
@@ -121,10 +114,7 @@ ZigBeeDriver *zblib_plugin_ref_driver(ZigBeePlugin *plugin, ZblibDriverType_e dr
        ZblibDriverType_e _driver_type;
        GSList *list = NULL;
 
-       if (NULL == plugin) {
-               Z_LOGE("ZigBee vendor plug-in is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("plugin", plugin, NULL);
 
        list = plugin->driver_list;
        while (list) {
@@ -142,3 +132,42 @@ ZigBeeDriver *zblib_plugin_ref_driver(ZigBeePlugin *plugin, ZblibDriverType_e dr
 
        return driver;
 }
+
+gboolean zblib_plugin_dispatch_request(ZigBeePlugin *plugin,
+       guint request_id)
+{
+       ZigBeeService *service = NULL;
+       ZigBeeDriver *driver = NULL;
+       ZblibDriverType_e driver_type;
+
+       zblib_check_null_ret_error("plugin", plugin, FALSE);
+
+       /* Fetch service */
+       service = plugin->service;
+       zblib_check_null_ret_error("service", service, FALSE);
+
+       /* Fetch driver type */
+       driver_type = (ZblibDriverType_e)zblib_request_ref_request_type_by_request_id(service, request_id);
+
+       /* Fetch driver */
+       driver = zblib_plugin_ref_driver(plugin, driver_type);
+       zblib_check_null_ret_error("driver", driver, FALSE);
+
+       /* Dispatch request to driver */
+       return zblib_driver_dispatch_request(driver, request_id);
+}
+
+void zblib_plugin_send_response(ZigBeePlugin *plugin,
+       guint request_id, gpointer resp_data, guint resp_data_len)
+{
+       ZigBeeService *service = NULL;
+
+       zblib_check_null_ret("plugin", plugin);
+
+       service = plugin->service;
+       zblib_check_null_ret("service", service);
+
+       /* Send response to service */
+       zblib_service_send_response(service,
+               request_id, resp_data, resp_data_len);
+}
index be1039a..06e6592 100644 (file)
@@ -49,6 +49,8 @@ struct zblib_request_type {
        guint ref_count; /* Reference count */
 
        gpointer request_data; /**< Request data */
+       gpointer resp_cb; /**< Response callback */
+       gpointer resp_cb_data; /**< Response callback data */
 
        ZigBeeServiceInterface *service_interface; /* Service interface */
 };
@@ -71,7 +73,7 @@ static ZblibDriverType_e __zblib_request_get_ops_id(guint request_type)
        return ops_id;
 }
 
-static gint __zblib_request_ref_request_type_by_request_id(ZigBeeService *service,
+static ZigBeeRequest *__zblib_request_ref_request_by_by_request_id(ZigBeeService *service,
        guint request_id)
 {
        ZigBeeRequest *request = NULL;
@@ -81,13 +83,28 @@ static gint __zblib_request_ref_request_type_by_request_id(ZigBeeService *servic
        request_table = zblib_service_ref_request_hash_table(service);
        if (NULL == request_table) {
                Z_LOGE("zblib_service_ref_request_hash_table failed!");
-               return -1;
+               return NULL;
        }
 
        /* Look-up requets_id in request hash table */
        request = (ZigBeeRequest *)g_hash_table_lookup(request_table, (gconstpointer)request_id);
-       if (NULL == request_table) {
-               Z_LOGE("No request availabel for request_id: [%d]", request_id);
+       if (NULL == request) {
+               Z_LOGE("No request available for request_id: [%d]", request_id);
+               return NULL;
+       }
+
+       return request;
+}
+
+static gint __zblib_request_ref_request_type_by_request_id(ZigBeeService *service,
+       guint request_id)
+{
+       ZigBeeRequest *request = NULL;
+
+       /* Fetch request based on request_id */
+       request = __zblib_request_ref_request_by_by_request_id(service, request_id);
+       if (NULL == request) {
+               Z_LOGE("No request available for request_id: [%d]", request_id);
                return -1;
        }
 
@@ -4686,9 +4703,11 @@ void zblib_request_free(ZigBeeServiceInterface *service_interface,
 
        zblib_check_null_ret("service_interface", service_interface);
 
+       /* Fetch service */
        service = zblib_service_interface_ref_service(service_interface);
        zblib_check_null_ret("service", service);
 
+       /* Fetch request based on request_id */
        request = __zblib_request_ref_request_by_request_id(service, request_id);
        zblib_check_null_ret("request", request);
 
@@ -4697,6 +4716,59 @@ void zblib_request_free(ZigBeeServiceInterface *service_interface,
        g_free(request);
 }
 
+gboolean zblib_request_set_response_cb(ZigBeeServiceInterface *service_interface,
+       guint request_id,
+       gpointer resp_cb, gpointer resp_cb_data)
+{
+       ZigBeeService *service = NULL;
+       ZigBeeRequest *request = NULL;
+
+       zblib_check_null_ret_error("request", request, FALSE);
+
+       /* Fetch service */
+       service = zblib_service_interface_ref_service(service_interface);
+       zblib_check_null_ret_error("service", service, FALSE);
+
+       /* Fetch request based on request_id */
+       request = __zblib_request_ref_request_by_by_request_id(service, request_id);
+       if (NULL == request) {
+               Z_LOGE("No request available for request_id: [%d]", request_id);
+               return FALSE;
+       }
+
+       /* Update response callback and response callback data */
+       request->resp_cb = resp_cb;
+       request->resp_cb_data = resp_cb_data;
+
+       return TRUE;
+}
+
+gpointer zblib_request_ref_response_cb(ZigBeeServiceInterface *service_interface,
+       guint request_id, gpointer *resp_cb_data)
+{
+       ZigBeeService *service = NULL;
+       ZigBeeRequest *request = NULL;
+
+       zblib_check_null_ret_error("request", request, NULL);
+
+       /* Fetch service */
+       service = zblib_service_interface_ref_service(service_interface);
+       zblib_check_null_ret_error("service", service, NULL);
+
+       /* Fetch request based on request_id */
+       request = __zblib_request_ref_request_by_by_request_id(service, request_id);
+       if (NULL == request) {
+               Z_LOGE("No request available for request_id: [%d]", request_id);
+               return NULL;
+       }
+
+       /* Response callback data */
+       *resp_cb_data = request->resp_cb_data;
+
+       /* Return response callback */
+       return request->resp_cb;
+}
+
 ZigBeeRequest *zblib_request_ref(ZigBeeRequest *request)
 {
        zblib_check_null_ret_error("request", request, NULL);
@@ -4727,19 +4799,26 @@ guint zblib_request_ref_request_type(ZigBeeRequest *request)
        return request->request_type;
 }
 
-ZigBeeServiceInterface *zblib_request_ref_service_interface(ZigBeeRequest *request)
+ZigBeeServiceInterface *zblib_request_ref_service_interface(ZigBeeService *service,
+       guint request_id)
 {
-       zblib_check_null_ret_error("request", request, NULL);
+       ZigBeeRequest *request = NULL;
+
+       zblib_check_null_ret_error("service", service, NULL);
+
+       /* Fetch request based on request_id */
+       request = __zblib_request_ref_request_by_by_request_id(service, request_id);
+       if (NULL == request) {
+               Z_LOGE("No request available for request_id: [%d]", request_id);
+               return NULL;
+       }
 
        return request->service_interface;
 }
 
 gpointer zblib_request_ref_request_data(ZigBeeRequest *request)
 {
-       if (NULL == request) {
-               Z_LOGE("request is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("request", request, NULL);
 
        return request->request_data;
 }
index 135a6f6..657bc57 100644 (file)
@@ -23,6 +23,7 @@
 #include <zblib_service.h>
 #include <zblib_plugin.h>
 #include <zblib_service_interface.h>
+#include <zblib_request.h>
 
 /**< ZigBee Service object */
 struct zblib_service_type {
@@ -36,17 +37,14 @@ struct zblib_service_type {
 };
 
 static void *__zblib_service_load_plugin(gchar *filename,
-       struct zblib_plugin_descriptor **descriptor_out)
+       ZblibPluginDescriptor_t **descriptor_out)
 {
-       struct zblib_plugin_descriptor *descriptor = NULL;
+       ZblibPluginDescriptor_t *descriptor = NULL;
        void *handle = NULL;
        struct stat stat_buf;
        char file_date[27];
 
-       if (G_UNLIKELY(NULL == descriptor_out)) {
-               Z_LOGE("descriptor_out is NULL!!!");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("descriptor_out", descriptor_out, NULL);
 
        /* Open .so */
        handle = dlopen(filename, RTLD_LAZY);
@@ -96,12 +94,10 @@ static void *__zblib_service_load_plugin(gchar *filename,
 
 static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
 {
-       const struct zblib_plugin_descriptor *descriptor = zblib_plugin_get_descriptor(plugin);
+       const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
 
-       if ((NULL == descriptor) || (NULL == descriptor->init)) {
-               Z_LOGE("descriptor OR init function is NULL!!!");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("descriptor", descriptor, FALSE);
+       zblib_check_null_ret_error("descriptor->init", descriptor->init, FALSE);
 
        if (G_UNLIKELY(FALSE == descriptor->init(plugin))) {
                char *plugin_name = zblib_plugin_get_plugin_name(plugin);
@@ -117,14 +113,11 @@ static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
 
 static gboolean __zblib_service_unload_plugin(ZigBeePlugin *plugin)
 {
-       const struct zblib_plugin_descriptor *descriptor = zblib_plugin_get_descriptor(plugin);
+       const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
        char *plugin_name = zblib_plugin_get_plugin_name(plugin);
 
-       if ((NULL == descriptor) || (NULL == descriptor->unload)) {
-               Z_LOGE("descriptor OR unload function is NULL!!!");
-               return FALSE;
-       }
-
+       zblib_check_null_ret_error("descriptor", descriptor, FALSE);
+       zblib_check_null_ret_error("descriptor->unload", descriptor->unload, FALSE);
 
        descriptor->unload(plugin);
        Z_LOGI("plugin(%s) unloaded!", plugin_name);
@@ -155,10 +148,7 @@ ZigBeeService *zblib_service_new()
 
 void zblib_service_free(ZigBeeService *service)
 {
-       if (NULL == service) {
-               Z_LOGE("service is NULL");
-               return;
-       }
+       zblib_check_null_ret("service", service);
 
        /* Free plug-ins */
        if (service->plugins) {
@@ -175,10 +165,8 @@ void zblib_service_free(ZigBeeService *service)
 
 gboolean zblib_service_run(ZigBeeService *service)
 {
-       if ((NULL == service) || (NULL == service->main_loop)) {
-               Z_LOGE("service or mainloop is NULL");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
 
        g_main_loop_run(service->main_loop);
 
@@ -187,10 +175,8 @@ gboolean zblib_service_run(ZigBeeService *service)
 
 gboolean zblib_service_exit(ZigBeeService *service)
 {
-       if ((NULL == service) || (NULL == service->main_loop)) {
-               Z_LOGE("service or mainloop is NULL");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
 
        g_main_loop_quit(service->main_loop);
 
@@ -201,10 +187,8 @@ gboolean zblib_service_add_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
 {
        gchar *plugin_name;
 
-       if ((NULL == service) || (plugin == NULL)) {
-               Z_LOGE("service: [%p] plugin: [%p]", service, plugin);
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("plugin", plugin, FALSE);
 
        plugin_name = zblib_plugin_get_plugin_name(plugin);
 
@@ -218,10 +202,8 @@ gboolean zblib_service_add_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
 
 gboolean zblib_service_remove_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
 {
-       if ((NULL == service) || (plugin == NULL)) {
-               Z_LOGE("service: [%p] plugin: [%p]", service, plugin);
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("plugin", plugin, FALSE);
 
        /* Specific vendor plug-in would be removed */
        service->plugins = g_slist_remove(service->plugins, plugin);
@@ -235,14 +217,12 @@ gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_p
        gchar *filename = NULL;
        GDir *dir = NULL;
        void *handle = NULL;
-       struct zblib_plugin_descriptor *descriptor = NULL;
+       ZblibPluginDescriptor_t *descriptor = NULL;
        ZigBeePlugin *plugin = NULL;
        gboolean ret;
 
-       if ((NULL == service) || (NULL == plugin_path)) {
-               Z_LOGE("service: [%p] plugin_path: [%p]", service, plugin_path);
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("plugin_path", plugin_path, FALSE);
 
        /* Open plug-in directory */
        dir = g_dir_open(plugin_path, 0, NULL);
@@ -290,10 +270,7 @@ gboolean zblib_service_initialize_plugins(ZigBeeService *service)
 {
        GSList *list;
 
-       if (NULL == service) {
-               Z_LOGE("service is NULL");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
 
        /* Refer plug-in list */
        list = zblib_service_ref_plugins(service);
@@ -313,10 +290,7 @@ gboolean zblib_service_unload_plugins(ZigBeeService *service)
 {
        GSList *list;
 
-       if (NULL == service) {
-               Z_LOGE("service is NULL");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
 
        list = zblib_service_ref_plugins(service);
        while (list != NULL) {
@@ -337,10 +311,7 @@ gboolean zblib_service_unload_plugins(ZigBeeService *service)
 
 GSList *zblib_service_ref_plugins(ZigBeeService *service)
 {
-       if (NULL == service) {
-               Z_LOGE("service is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("service", service, NULL);
 
        return service->plugins;
 }
@@ -350,10 +321,8 @@ gboolean zblib_service_add_service_interface(ZigBeeService *service,
 {
        gchar *object_name;
 
-       if ((NULL == service) || (NULL == service_interface)) {
-               Z_LOGE("service: [%p] service_interface: [%p]", service, service_interface);
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
 
        object_name = zblib_service_interface_get_name(service_interface);
 
@@ -370,10 +339,8 @@ gboolean zblib_service_add_service_interface(ZigBeeService *service,
 gboolean zblib_service_remove_service_interface(ZigBeeService *service,
        ZigBeeServiceInterface *service_interface)
 {
-       if ((NULL == service) || (NULL == service_interface)) {
-               Z_LOGE("service: [%p] service_interface: [%p]", service, service_interface);
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service", service, FALSE);
+       zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
 
        /*
         * service interface object would be removed
@@ -392,10 +359,8 @@ ZigBeeServiceInterface *zblib_service_ref_service_interface(ZigBeeService *servi
 
        GSList *list;
 
-       if ((NULL == service) || (NULL == service_interface_name)) {
-               Z_LOGE("service: [%p] service_interface_name: [%p]", service, service_interface_name);
-               return NULL;
-       }
+       zblib_check_null_ret_error("service", service, NULL);
+       zblib_check_null_ret_error("service_interface_name", service_interface_name, NULL);
 
        list = service->interface_objs;
        while (list) {
@@ -426,23 +391,56 @@ ZigBeeServiceInterface *zblib_service_ref_service_interface(ZigBeeService *servi
 
 GHashTable *zblib_service_ref_request_hash_table(ZigBeeService *service)
 {
-       if (NULL == service) {
-               Z_LOGE("service is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("service", service, NULL);
 
        return service->request_table;
 }
 
 gint zblib_service_generate_request_id(ZigBeeService *service)
 {
-       if (NULL == service) {
-               Z_LOGE("service is NULL");
-               return -1;
-       }
+       zblib_check_null_ret_error("service", service, -1);
 
        /* Increment request ID */
        service->request_id++;
 
        return (gint)service->request_id;
-}
\ No newline at end of file
+}
+
+gboolean zblib_service_dispatch_request(ZigBeeService *service,
+       guint request_id)
+{
+       ZigBeePlugin *plugin = NULL;
+       GSList *plugin_list = NULL;
+       gboolean ret = FALSE;
+
+       zblib_check_null_ret_error("service", service, FALSE);
+
+       /* Fetch plugin_list */
+       plugin_list = service->plugins;
+       zblib_check_null_ret_error("plugin_list", plugin_list, FALSE);
+
+       while (plugin_list) {
+               plugin = (ZigBeePlugin *)plugin_list->data;
+
+               /* Dispatch request to plugin */
+               ret = zblib_plugin_dispatch_request(plugin, request_id);
+
+               /* Move to next plugin */
+               plugin_list = g_slist_next(plugin_list);
+       }
+
+       return ret;
+}
+
+void zblib_service_send_response(ZigBeeService *service,
+       guint request_id, gpointer resp_data, guint resp_data_len)
+{
+       ZigBeeServiceInterface *service_interface = NULL;
+
+       service_interface = zblib_request_ref_service_interface(service, request_id);
+       zblib_check_null_ret("service_interface", service_interface);
+
+       /* Send response to service interface */
+       zblib_service_interface_send_response(service_interface,
+               request_id, resp_data, resp_data_len);
+}
index f326182..a8cffdf 100644 (file)
 #include <zblib.h>
 
 #include <zblib_service_interface.h>
+#include <zblib_request.h>
+#include <zblib_service.h>
 
 /**< ZigBee Service interface object */
 struct zblib_service_interface_type {
        gchar *object_name; /**< ZigBee Service interface object name */
-       struct zblib_service_interface_methods *methods;  /**< ZigBee Service interface object methods */
 
        gpointer user_data; /**< ZigBee servcie interface data */
 
@@ -31,23 +32,18 @@ struct zblib_service_interface_type {
 };
 
 ZigBeeServiceInterface *zblib_service_interface_new(ZigBeeService *service,
-       const gchar *object_name,
-       struct zblib_service_interface_methods *methods)
+       const gchar *object_name)
 {
        ZigBeeServiceInterface *service_interface = NULL;
 
-       if ((NULL == service) || (NULL == object_name)) {
-               Z_LOGE("service [%p] or object_name [%p] is NULL",
-                       service, object_name);
-               return NULL;
-       }
+       zblib_check_null_ret_error("service", service, NULL);
+       zblib_check_null_ret_error("object_name", object_name, NULL);
 
        /* Allocate memory */
        service_interface = g_malloc0(sizeof(ZigBeeServiceInterface));
 
        /* Update fields */
        service_interface->object_name = g_strdup(object_name);
-       service_interface->methods = methods;
        service_interface->service = service;
 
        Z_LOGI("Service interface created - Name [%s]",
@@ -59,11 +55,8 @@ ZigBeeServiceInterface *zblib_service_interface_new(ZigBeeService *service,
 void zblib_service_interface_free(ZigBeeService *service,
        ZigBeeServiceInterface *service_interface)
 {
-       if ((NULL == service) || (NULL == service_interface)) {
-               Z_LOGE("service [%p] or service_interface [%p] is NULL",
-                       service, service_interface);
-               return;
-       }
+       zblib_check_null_ret("service", service);
+       zblib_check_null_ret("service_interface", service_interface);
 
        Z_LOGI("Freeing Service interface - Name [%p]",
                        service_interface->object_name);
@@ -75,10 +68,7 @@ void zblib_service_interface_free(ZigBeeService *service,
 
 char *zblib_service_interface_get_name(ZigBeeServiceInterface *service_interface)
 {
-       if (NULL == service_interface) {
-               Z_LOGE("service_interface is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("service_interface", service_interface, NULL);
 
        return g_strdup(service_interface->object_name);
 }
@@ -86,10 +76,7 @@ char *zblib_service_interface_get_name(ZigBeeServiceInterface *service_interface
 gboolean zblib_service_interface_link_user_data(ZigBeeServiceInterface *service_interface,
        gpointer user_data)
 {
-       if (NULL == service_interface) {
-               Z_LOGE("service_interface is NULL");
-               return FALSE;
-       }
+       zblib_check_null_ret_error("service_interface", service_interface, FALSE);
 
        service_interface->user_data = user_data;
 
@@ -98,20 +85,80 @@ gboolean zblib_service_interface_link_user_data(ZigBeeServiceInterface *service_
 
 gpointer zblib_service_interface_ref_user_data(ZigBeeServiceInterface *service_interface)
 {
-       if (NULL == service_interface) {
-               Z_LOGE("service_interface is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("service_interface", service_interface, NULL);
 
        return service_interface->user_data;
 }
 
 ZigBeeService *zblib_service_interface_ref_service(ZigBeeServiceInterface *service_interface)
 {
-       if (NULL == service_interface) {
-               Z_LOGE("service_interface is NULL");
-               return NULL;
-       }
+       zblib_check_null_ret_error("service_interface", service_interface, NULL);
 
        return service_interface->service;
 }
+
+gint zblib_service_interface_new_request(ZigBeeServiceInterface *service_interface,
+       guint request_type,
+       gpointer request_data, guint request_data_len,
+       ZblibServiceInterfaceResponseCb_t resp_cb, gpointer resp_cb_data)
+{
+       gint request_id = 0;
+       gboolean ret;
+
+       zblib_check_null_ret_error("service_interface", service_interface, -1);
+
+       /* Create new request */
+       request_id = zblib_request_new(service_interface,
+               request_type,
+               request_data, request_data_len);
+
+       /* Set response callback */
+       ret = zblib_request_set_response_cb(service_interface,
+               request_id,
+               (gpointer)resp_cb, (gpointer)resp_cb_data);
+       if (FALSE == ret) {
+               Z_LOGE("zblib_request_set_response_cb failed!");
+
+               /* Free request_id */
+               zblib_request_free(service_interface, request_id);
+               request_id = -1;
+       }
+
+       return request_id;
+}
+
+gboolean zblib_service_interface_dispatch_request(ZigBeeServiceInterface *service_interface,
+       guint request_id)
+{
+       ZigBeeService *service = NULL;
+
+       zblib_check_null_ret_error("service_interface", service_interface, FALSE);
+
+       /* Fetch service */
+       service = service_interface->service;
+       zblib_check_null_ret_error("service", service, FALSE);
+
+       /* Dispatch request to service */
+       return zblib_service_dispatch_request(service, request_id);
+}
+
+void zblib_service_interface_send_response(ZigBeeServiceInterface *service_interface,
+       guint request_id, gpointer resp_data, guint resp_data_len)
+{
+       ZblibServiceInterfaceResponseCb_t resp_cb;
+       gpointer resp_cb_data;
+
+       zblib_check_null_ret("service_interface", service_interface);
+
+       /* Fetch response callback and response callback data from request_id  */
+       resp_cb = zblib_request_ref_response_cb(service_interface,
+               request_id, &resp_cb_data);
+       zblib_check_null_ret("resp_cb", resp_cb);
+
+       /* Invoke send response function */
+       resp_cb(service_interface,
+               request_id, resp_data, resp_data_len, resp_cb_data);
+
+       /* Free request */
+       zblib_request_free(service_interface, request_id);
+}