Adding service interface layer logic for request processing
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_on_off.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Suresh Kumar N (suresh.n@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "zigbee_service_interface_common.h"
20
21 #include <zblib_driver_on_off.h>
22
23 static void on_on_off_set_on_off_resp(ZigBeeServiceInterface *service_interface,
24         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
25 {
26         ZigbeeServiceInterfaceRespCbData_t *cb_data =
27                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
28
29         NOT_USED(cb_data);
30         NOT_USED(service_interface);
31         NOT_USED(request_id);
32         NOT_USED(resp_data);
33         NOT_USED(resp_data_len);
34 }
35
36 static gboolean on_on_off_set_on_off(ZigbeeOn_off *on_off_object,
37         GDBusMethodInvocation *invocation,
38         gshort node_id,
39         gchar endpoint,
40         gchar on_off_type,
41         gpointer user_data)
42 {
43         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
44         ZigbeeOnOffSetOnOff_t req;
45         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
46
47         gboolean ret;
48
49         memset(&req, 0x0, sizeof(ZigbeeOnOffSetOnOff_t));
50
51         /* Update request structure */
52         req.node_id = node_id;
53         req.endpoint = endpoint;
54         req.on_off_type = on_off_type;
55
56         /* Allocate response callback data */
57         resp_cb_data =
58                 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
59                         invocation, NULL, 0);
60         if (NULL == resp_cb_data) {
61                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
62
63                 /* Send failure response */
64                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
65
66                 return TRUE;
67         }
68
69         /* Dispatch request */
70         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
71                 ZBLIB_DRIVER_TYPE_ON_OFF,
72                 ZBLIB_ON_OFF_OPS_SET_ON_OFF,
73                 &req, sizeof(req),
74                 on_on_off_set_on_off_resp, resp_cb_data);
75         if (FALSE == ret) {
76                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
77
78                 /* Free response callback data */
79                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
80
81                 /* Send failure response */
82                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
83
84                 return TRUE;
85         }
86
87         return TRUE;
88 }
89
90 static void on_on_off_get_on_off_state_resp(ZigBeeServiceInterface *service_interface,
91         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
92 {
93         ZigbeeServiceInterfaceRespCbData_t *cb_data =
94                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
95
96         NOT_USED(cb_data);
97         NOT_USED(service_interface);
98         NOT_USED(request_id);
99         NOT_USED(resp_data);
100         NOT_USED(resp_data_len);
101 }
102
103 static gboolean on_on_off_get_on_off_state(ZigbeeOn_off *on_off_object,
104         GDBusMethodInvocation *invocation,
105         gshort node_id,
106         gchar endpoint,
107         gpointer user_data)
108 {
109         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
110         ZigbeeOnOffGetOnOffState_t req;
111         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
112
113         gboolean ret;
114
115         memset(&req, 0x0, sizeof(ZigbeeOnOffGetOnOffState_t));
116
117         /* Update request structure */
118         req.node_id = node_id;
119         req.endpoint = endpoint;
120
121         /* Allocate response callback data */
122         resp_cb_data =
123                 zigbee_service_dbus_interface_create_resp_cb_data(on_off_object,
124                         invocation, NULL, 0);
125         if (NULL == resp_cb_data) {
126                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
127
128                 /* Send failure response */
129                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
130
131                 return TRUE;
132         }
133
134         /* Dispatch request */
135         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
136                 ZBLIB_DRIVER_TYPE_ON_OFF,
137                 ZBLIB_ON_OFF_OPS_GET_ON_OFF_STATE,
138                 &req, sizeof(req),
139                 on_on_off_get_on_off_state_resp, resp_cb_data);
140         if (FALSE == ret) {
141                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
142
143                 /* Free response callback data */
144                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
145
146                 /* Send failure response */
147                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
148
149                 return TRUE;
150         }
151
152         return TRUE;
153 }
154
155 gboolean zigbee_service_dbus_interface_on_off_init(ZigBeeServiceInterface *service_interface,
156         ZigbeeObjectSkeleton *zigbee_object)
157 {
158         ZigbeeOn_off *on_off_object;
159
160         if (NULL == service_interface) {
161                 Z_LOGE("service_interface is NULL");
162                 return FALSE;
163         }
164
165         on_off_object = zigbee_on_off_skeleton_new();
166         zigbee_object_skeleton_set_on_off(zigbee_object, on_off_object);
167         g_object_unref(on_off_object);
168
169         Z_LOGI("on_off_object: [%p]", on_off_object);
170
171         /*
172          * Register signal handlers for 'on_off' interface
173          */
174         g_signal_connect(on_off_object,
175                 "handle-set-on-off",
176                 G_CALLBACK(on_on_off_set_on_off), service_interface);
177
178         g_signal_connect(on_off_object,
179                 "handle-get-on-off-state",
180                 G_CALLBACK(on_on_off_get_on_off_state), service_interface);
181
182         return TRUE;
183 }