Add logic to handle notification and response
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_service.c
index af23c3f..879ba23 100644 (file)
 
 #include <zblib_driver_service.h>
 
+static void *_service_interface_ref_zigbee_service(ZigBeeServiceInterface *service_interface)
+{
+       ZigbeeObjectSkeleton *zigbee_object;
+       ZigbeeCustomData_t *custom_data = NULL;
+       ZigbeeService *service = NULL;
+
+       custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
+       if (NULL == custom_data) {
+               Z_LOGE("D-BUS service interface custom_data is NULL!");
+               return NULL;
+       }
+
+       /* Get zigbee object */
+       zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
+       if (zigbee_object) {
+               Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
+               return NULL;
+       }
+
+       service = zigbee_object_get_service(ZIGBEE_OBJECT(zigbee_object));
+       return service;
+}
+
 static void on_service_enable_resp(ZigBeeServiceInterface *service_interface,
        guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
 {
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_enable(service_object, invocation, TRUE);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_enable(ZigbeeService *service_object,
@@ -84,11 +119,23 @@ static void on_service_disable_resp(ZigBeeServiceInterface *service_interface,
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_disable(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_disable(ZigbeeService *service_object,
@@ -140,11 +187,23 @@ static void on_service_zb_hw_reset_resp(ZigBeeServiceInterface *service_interfac
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_zb_hw_reset(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_zb_hw_reset(ZigbeeService *service_object,
@@ -196,11 +255,23 @@ static void on_service_form_network_resp(ZigBeeServiceInterface *service_interfa
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_form_network(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_form_network(ZigbeeService *service_object,
@@ -252,11 +323,23 @@ static void on_service_coex_start_resp(ZigBeeServiceInterface *service_interface
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_coex_start(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_coex_start(ZigbeeService *service_object,
@@ -315,11 +398,23 @@ static void on_service_coex_stop_resp(ZigBeeServiceInterface *service_interface,
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_coex_stop(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_coex_stop(ZigbeeService *service_object,
@@ -371,11 +466,23 @@ static void on_service_leave_network_resp(ZigBeeServiceInterface *service_interf
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_leave_network(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_leave_network(ZigbeeService *service_object,
@@ -427,11 +534,28 @@ static void on_service_get_network_info_resp(ZigBeeServiceInterface *service_int
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
+       ZigbeeServiceGetNetworkInfo_t payload;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       memcpy(&payload, resp_data, resp_data_len);
+       zigbee_service_complete_get_network_info(service_object, invocation,
+               payload.eui64, payload.node_id, payload.pan_id, payload.channel,
+               payload.radio_tx_power);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_get_network_info(ZigbeeService *service_object,
@@ -483,11 +607,23 @@ static void on_service_permit_join_resp(ZigBeeServiceInterface *service_interfac
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_permit_join(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_permit_join(ZigbeeService *service_object,
@@ -548,11 +684,23 @@ static void on_service_leave_request_resp(ZigBeeServiceInterface *service_interf
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
        NOT_USED(resp_data);
        NOT_USED(resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_leave_request(service_object, invocation);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_leave_request(ZigbeeService *service_object,
@@ -623,11 +771,27 @@ static void on_service_get_device_list_resp(ZigBeeServiceInterface *service_inte
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
-       NOT_USED(cb_data);
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(service_interface);
        NOT_USED(request_id);
-       NOT_USED(resp_data);
-       NOT_USED(resp_data_len);
+
+       if (NULL == resp_data || 0 == resp_data_len) {
+               Z_LOGE("resp_data is null");
+               g_free(cb_data);
+               return;
+       }
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_get_device_list(service_object, invocation,     resp_data);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_get_device_list(ZigbeeService *service_object,
@@ -679,11 +843,30 @@ static void on_service_get_mac_resp(ZigBeeServiceInterface *service_interface,
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(cb_data);
        NOT_USED(service_interface);
        NOT_USED(request_id);
-       NOT_USED(resp_data);
-       NOT_USED(resp_data_len);
+
+       if (NULL == resp_data || 0 == resp_data_len) {
+               Z_LOGE("resp_data is null");
+               g_free(cb_data);
+               return;
+       }
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       /* Todo : plugin must load resp_data as char pointer */
+       zigbee_service_complete_get_mac(service_object, invocation, resp_data);
+
+       g_free(cb_data);
+
 }
 
 static gboolean on_service_get_mac(ZigbeeService *service_object,
@@ -735,11 +918,28 @@ static void on_service_get_device_info_resp(ZigBeeServiceInterface *service_inte
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(cb_data);
        NOT_USED(service_interface);
        NOT_USED(request_id);
-       NOT_USED(resp_data);
-       NOT_USED(resp_data_len);
+
+       if (NULL == resp_data || 0 == resp_data_len) {
+               Z_LOGE("resp_data is null");
+               g_free(cb_data);
+               return;
+       }
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_get_device_info(service_object, invocation, resp_data);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_get_device_info(ZigbeeService *service_object,
@@ -791,11 +991,29 @@ static void on_service_get_endpoint_list_resp(ZigBeeServiceInterface *service_in
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(cb_data);
        NOT_USED(service_interface);
        NOT_USED(request_id);
-       NOT_USED(resp_data);
-       NOT_USED(resp_data_len);
+
+       if (NULL == resp_data || 0 == resp_data_len) {
+               Z_LOGE("resp_data is null");
+               g_free(cb_data);
+               return;
+       }
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       /* To fix : end-points must be changed gchar* -> GVariant ay */
+       zigbee_service_complete_get_endpoint_list(service_object, invocation, resp_data);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_get_endpoint_list(ZigbeeService *service_object,
@@ -862,11 +1080,28 @@ static void on_service_get_cluster_list_resp(ZigBeeServiceInterface *service_int
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+
        NOT_USED(cb_data);
        NOT_USED(service_interface);
        NOT_USED(request_id);
-       NOT_USED(resp_data);
-       NOT_USED(resp_data_len);
+
+       if (NULL == resp_data || 0 == resp_data_len) {
+               Z_LOGE("resp_data is null");
+               g_free(cb_data);
+               return;
+       }
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_get_cluster_list(service_object, invocation, resp_data);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_get_cluster_list(ZigbeeService *service_object,
@@ -935,11 +1170,31 @@ static void on_service_get_node_type_resp(ZigBeeServiceInterface *service_interf
        ZigbeeServiceInterfaceRespCbData_t *cb_data =
                (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
 
+       ZigbeeService *service_object;
+       GDBusMethodInvocation *invocation;
+       ZigbeeServiceServiceNodeType_t payload;
+
        NOT_USED(cb_data);
        NOT_USED(service_interface);
        NOT_USED(request_id);
-       NOT_USED(resp_data);
-       NOT_USED(resp_data_len);
+
+       if (NULL == resp_data || 0 == resp_data_len) {
+               Z_LOGE("resp_data is null");
+               g_free(cb_data);
+               return;
+       }
+
+       memcpy(&payload, resp_data, resp_data_len);
+
+       service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
+       zblib_check_null_ret("service_object", service_object);
+
+       invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
+       zblib_check_null_ret("invocation", invocation);
+
+       zigbee_service_complete_get_node_type(service_object, invocation, payload.node_type);
+
+       g_free(cb_data);
 }
 
 static gboolean on_service_get_node_type(ZigbeeService *service_object,
@@ -1000,6 +1255,61 @@ static gboolean on_service_get_node_type(ZigbeeService *service_object,
        return TRUE;
 }
 
+void zigbee_service_dbus_interface_service_notification(ZigBeeServiceInterface *service_interface,
+       guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
+{
+       ZigbeeService *service_object;
+
+       zblib_check_null_ret("service_interface", service_interface);
+
+       if (NULL == noti_data || 0 == noti_data_len) {
+               Z_LOGE("noti_data is NULL");
+               return;
+       }
+
+       service_object = _service_interface_ref_zigbee_service(service_interface);
+       zblib_check_null_ret("service_object", service_object);
+
+       NOT_USED(noti_cb_data);
+
+       switch(noti_id) {
+       case ZBLIB_SERVICE_NOTI_FORM_NETWORK_DONE: {
+               ZigbeeServiceServiceFormNetworkDone_t panid_t;
+               memcpy(&panid_t, noti_data, noti_data_len);
+               zigbee_service_emit_form_network_done(service_object, panid_t.pan_id);
+       }
+       break;
+       case ZBLIB_SERVICE_NOTI_CHILD_JOINED: {
+               zigbee_service_emit_child_joined(service_object, (GVariant *)noti_data);
+       }
+       break;
+       case ZBLIB_SERVICE_NOTI_CHILD_REJOINED: {
+               ZigbeeServiceServiceChildRejoined_t ieee_addr_t;
+               memcpy(&ieee_addr_t, noti_data, noti_data_len);
+               zigbee_service_emit_child_rejoined(service_object, ieee_addr_t.eui64);
+       }
+       break;
+       case ZBLIB_SERVICE_NOTI_CHILD_LEFT: {
+               ZigbeeServiceServiceChildLeft_t child_left_t;
+               memcpy(&child_left_t, noti_data, noti_data_len);
+               zigbee_service_emit_child_left(service_object, child_left_t.eui64, child_left_t.status);
+       }
+       break;
+       case ZBLIB_SERVICE_NOTI_LEAVE_NETWORK_DONE: {
+               ZigbeeServiceServiceLeaveNetworkDone_t leave_net_t;
+               memcpy(&leave_net_t, noti_data, noti_data_len);
+               zigbee_service_emit_leave_network_done(service_object, leave_net_t.pan_id);
+       }
+       break;
+       default:
+               Z_LOGE("Unexpected notification [%x]", noti_id);
+       break;
+       }
+
+       /* ZigbeeService should be dereferenced */
+       g_object_unref(service_object);
+}
+
 gboolean zigbee_service_dbus_interface_service_init(ZigBeeServiceInterface *service_interface,
        ZigbeeObjectSkeleton *zigbee_object)
 {