Adding service interface layer logic for request processing
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_custom.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_custom.h>
22
23 static void on_custom_aps_send_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_custom_aps_send(ZigbeeCustom *custom_object,
37         GDBusMethodInvocation *invocation,
38         gshort node_id,
39         gchar aps_frame_ctrl,
40         gchar src_ep,
41         gchar dest_ep,
42         gshort cluster_id,
43         gshort profile_id,
44         gchar zcl_frame_ctrl,
45         gshort mfg_code,
46         GVariant *payload,
47         gpointer user_data)
48 {
49         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
50         ZigbeeCustomApsSend_t req;
51         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
52
53         GVariantIter *iter = NULL;
54         guint i = 0;
55
56         gboolean ret;
57
58         memset(&req, 0x0, sizeof(ZigbeeCustomApsSend_t));
59
60         /* Update request structure */
61         req.node_id = node_id;
62         req.aps_frame_ctrl = aps_frame_ctrl;
63         req.src_ep = src_ep;
64         req.dest_ep = dest_ep;
65         req.cluster_id = cluster_id;
66         req.profile_id = profile_id;
67         req.zcl_frame_ctrl = zcl_frame_ctrl;
68         req.mfg_code = mfg_code;
69         g_variant_get(payload, "ay", &iter);
70         while (g_variant_iter_loop(iter, "y", req.payload[i])) {
71                 i++;
72                 if (i >= ZIGBEE_CUSTOM_SEND_PAYLOAD_LEN)
73                         break;
74         }
75
76         /* Allocate response callback data */
77         resp_cb_data =
78                 zigbee_service_dbus_interface_create_resp_cb_data(custom_object,
79                         invocation, NULL, 0);
80         if (NULL == resp_cb_data) {
81                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
82
83                 /* Send failure response */
84                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
85
86                 return TRUE;
87         }
88
89         /* Dispatch request */
90         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
91                 ZBLIB_DRIVER_TYPE_CUSTOM,
92                 ZBLIB_CUSTOM_OPS_APS_SEND,
93                 &req, sizeof(req),
94                 on_custom_aps_send_resp, resp_cb_data);
95         if (FALSE == ret) {
96                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
97
98                 /* Free response callback data */
99                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
100
101                 /* Send failure response */
102                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
103
104                 return TRUE;
105         }
106
107         return TRUE;
108 }
109
110 static void on_custom_zcl_send_resp(ZigBeeServiceInterface *service_interface,
111         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
112 {
113         ZigbeeServiceInterfaceRespCbData_t *cb_data =
114                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
115
116         NOT_USED(cb_data);
117         NOT_USED(service_interface);
118         NOT_USED(request_id);
119         NOT_USED(resp_data);
120         NOT_USED(resp_data_len);
121 }
122
123 static gboolean on_custom_zcl_send(ZigbeeCustom *custom_object,
124         GDBusMethodInvocation *invocation,
125         gshort node_id,
126         gchar src_ep,
127         gchar dest_ep,
128         gshort cluster_id,
129         gchar zcl_frame_ctrl,
130         gchar cmd_id,
131         GVariant *payload,
132         gpointer user_data)
133 {
134         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
135         ZigbeeCustomZclSend_t req;
136         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
137
138         GVariantIter *iter = NULL;
139         guint i = 0;
140
141         gboolean ret;
142
143         memset(&req, 0x0, sizeof(ZigbeeCustomZclSend_t));
144
145         /* Update request structure */
146         req.node_id = node_id;
147         req.src_ep = src_ep;
148         req.dest_ep = dest_ep;
149         req.cluster_id = cluster_id;
150         req.zcl_frame_ctrl = zcl_frame_ctrl;
151         req.cmd_id = cmd_id;
152         g_variant_get(payload, "ay", &iter);
153         while (g_variant_iter_loop(iter, "y", req.payload[i])) {
154                 i++;
155                 if (i >= ZIGBEE_CUSTOM_SEND_PAYLOAD_LEN)
156                         break;
157         }
158
159         /* Allocate response callback data */
160         resp_cb_data =
161                 zigbee_service_dbus_interface_create_resp_cb_data(custom_object,
162                         invocation, NULL, 0);
163         if (NULL == resp_cb_data) {
164                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
165
166                 /* Send failure response */
167                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
168
169                 return TRUE;
170         }
171
172         /* Dispatch request */
173         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
174                 ZBLIB_DRIVER_TYPE_CUSTOM,
175                 ZBLIB_CUSTOM_OPS_ZCL_SEND,
176                 &req, sizeof(req),
177                 on_custom_zcl_send_resp, resp_cb_data);
178         if (FALSE == ret) {
179                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
180
181                 /* Free response callback data */
182                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
183
184                 /* Send failure response */
185                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
186
187                 return TRUE;
188         }
189
190         return TRUE;
191 }
192
193 static void on_custom_send_to_local_resp(ZigBeeServiceInterface *service_interface,
194         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
195 {
196         ZigbeeServiceInterfaceRespCbData_t *cb_data =
197                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
198
199         NOT_USED(cb_data);
200         NOT_USED(service_interface);
201         NOT_USED(request_id);
202         NOT_USED(resp_data);
203         NOT_USED(resp_data_len);
204 }
205
206 static gboolean on_custom_send_to_local(ZigbeeCustom *custom_object,
207         GDBusMethodInvocation *invocation,
208         GVariant *data,
209         gpointer user_data)
210 {
211         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
212         ZigbeeCustomSendToLocal_t req;
213         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
214
215         GVariantIter *iter = NULL;
216         guint i = 0;
217
218         gboolean ret;
219
220         memset(&req, 0x0, sizeof(ZigbeeCustomSendToLocal_t));
221
222         /* Update request structure */
223         g_variant_get(data, "ay", &iter);
224         while (g_variant_iter_loop(iter, "y", req.data[i])) {
225                 i++;
226                 if (i >= ZIGBEE_CUSTOM_SEND_PAYLOAD_LEN)
227                         break;
228         }
229
230         /* Allocate response callback data */
231         resp_cb_data =
232                 zigbee_service_dbus_interface_create_resp_cb_data(custom_object,
233                         invocation, NULL, 0);
234         if (NULL == resp_cb_data) {
235                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
236
237                 /* Send failure response */
238                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
239
240                 return TRUE;
241         }
242
243         /* Dispatch request */
244         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
245                 ZBLIB_DRIVER_TYPE_CUSTOM,
246                 ZBLIB_CUSTOM_OPS_SEND_TO_LOCAL,
247                 &req, sizeof(req),
248                 on_custom_send_to_local_resp, resp_cb_data);
249         if (FALSE == ret) {
250                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
251
252                 /* Free response callback data */
253                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
254
255                 /* Send failure response */
256                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
257
258                 return TRUE;
259         }
260
261         return TRUE;
262 }
263
264 gboolean zigbee_service_dbus_interface_custom_init(ZigBeeServiceInterface *service_interface,
265         ZigbeeObjectSkeleton *zigbee_object)
266 {
267         ZigbeeCustom *custom_object;
268
269         if (NULL == service_interface) {
270                 Z_LOGE("service_interface is NULL");
271                 return FALSE;
272         }
273
274         custom_object = zigbee_custom_skeleton_new();
275         zigbee_object_skeleton_set_custom(zigbee_object, custom_object);
276         g_object_unref(custom_object);
277
278         Z_LOGI("custom_object: [%p]", custom_object);
279
280         /*
281          * Register signal handlers for 'custom' interface
282          */
283         g_signal_connect(custom_object,
284                 "handle-aps-send",
285                 G_CALLBACK(on_custom_aps_send), service_interface);
286
287         g_signal_connect(custom_object,
288                 "handle-zcl-send",
289                 G_CALLBACK(on_custom_zcl_send), service_interface);
290
291         g_signal_connect(custom_object,
292                 "handle-send-to-local",
293                 G_CALLBACK(on_custom_send_to_local), service_interface);
294
295         return TRUE;
296 }