4cdf672952a9635a90faeafce78acecd96dc325a
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_interface_common.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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <glib.h>
24 #include <dlog.h>
25
26 #include <zblib.h>
27 #include <zblib_service.h>
28 #include <zblib_service_interface.h>
29
30 #include "zigbee_service_interface.h"
31 #include "zigbee_service_interface_common.h"
32 #include "zigbee_service_dbus_interface.h"
33
34 /**< ZigBee D-BUS response callback data */
35 struct zigbee_service_interface_resp_cb_type {
36         void *interface_object; /**< D-BUS interface object */
37         GDBusMethodInvocation *invocation; /**< D-BUS invocation */
38         char user_data[0]; /* Additional user data base pointer */
39 };
40
41 ZigbeeServiceInterfaceRespCbData_t *
42 zigbee_service_dbus_interface_create_resp_cb_data(void *interface_object,
43         GDBusMethodInvocation *invocation,
44         void *user_data, unsigned int user_data_len)
45 {
46         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
47
48         /* Allocate memory for response callback data */
49         resp_cb_data = g_malloc0(sizeof(ZigbeeServiceInterfaceRespCbData_t) + user_data_len);
50
51         /* Update interface object and invocation */
52         resp_cb_data->interface_object = interface_object;
53         resp_cb_data->invocation = invocation;
54
55         /* Update user data */
56         if ((user_data != NULL) && (user_data_len > 0))
57                 memcpy(resp_cb_data->user_data, user_data, user_data_len);
58
59         return resp_cb_data;
60 }
61
62 void zigbee_service_dbus_interface_destroy_resp_cb_data(ZigbeeServiceInterfaceRespCbData_t *resp_cb_data)
63 {
64         if (NULL == resp_cb_data) {
65                 Z_LOGE("resp_cb_data is NULL");
66                 return;
67         }
68
69         /* Free response callback data */
70         g_free(resp_cb_data);
71 }
72
73 gboolean zigbee_service_dbus_interface_dispatch_request(ZigBeeServiceInterface *service_interface,
74         ZblibDriverType_e driver_type, guint ops_id,
75         gpointer request_data, guint request_data_len,
76         ZblibServiceInterfaceResponseCb_t resp_cb, gpointer resp_cb_data)
77 {
78         gint request_id;
79         gboolean ret;
80
81         /* Create request_id */
82         request_id = zblib_service_interface_new_request(service_interface,
83                 driver_type,
84                 ops_id,
85                 &request_data, request_data_len,
86                 resp_cb, resp_cb_data);
87         if (ZIGBEE_SERVICE_INTERFACE_INVALID_REQUEST_ID == request_id) {
88                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
89
90                 return FALSE;
91         }
92
93         /* Dispatch request */
94         ret = zblib_service_interface_dispatch_request(service_interface, request_id);
95         if (FALSE == ret) {
96                 Z_LOGE("zblib_service_interface_dispatch_request failed!");
97
98                 /* Free request_id */
99                 zblib_service_interface_free_request(service_interface, request_id);
100
101                 return FALSE;
102         }
103
104         return TRUE;
105 }