1e52591702d17b3dec34af2289fea67ff0cde5c2
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_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 <zblib.h>
20 #include <zblib_driver.h>
21 #include <zblib_request.h>
22
23 #include "zblib_driver_custom.h"
24
25 /**< Private data */
26 typedef struct {
27         ZblibDriverCustomOps_t *ops; /**< Operations */
28 } ZblibDriverCustomPrivData_t;
29
30 static gboolean __zblib_driver_custom_dispatcher(ZigBeeDriver *driver, guint request_id)
31 {
32         ZigBeeService *service = NULL;
33         ZblibDriverCustomPrivData_t *priv_data = NULL;
34         ZblibDriverCustomOps_t *ops = NULL;
35         guint request_type;
36         guint ops_id;
37         gboolean ret = FALSE;
38
39         zblib_check_null_ret_error("driver", driver, FALSE);
40
41         service = zblib_driver_ref_service(driver);
42         zblib_check_null_ret_error("service", service, FALSE);
43
44         /* Fetch private data */
45         priv_data = zblib_driver_ref_object(driver);
46         zblib_check_null_ret_error("priv_data", priv_data, FALSE);
47
48         ops = priv_data->ops;
49         zblib_check_null_ret_error("ops", ops, FALSE);
50
51         /* Fetch request_type using request_id */
52         request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
53
54         /* Fetch ops ID */
55         ops_id = zblib_request_get_ops_id(request_type);
56         switch (ops_id) {
57         case ZBLIB_CUSTOM_OPS_APS_SEND: {
58                 zblib_check_null_ret_error("ops->aps_send",
59                         ops->aps_send, FALSE);
60
61                 ret = ops->aps_send(driver, request_id);
62         }
63         break;
64
65         case ZBLIB_CUSTOM_OPS_ZCL_SEND: {
66                 zblib_check_null_ret_error("ops->zcl_send",
67                         ops->zcl_send, FALSE);
68
69                 ret = ops->zcl_send(driver, request_id);
70         }
71         break;
72
73         case ZBLIB_CUSTOM_OPS_SEND_TO_LOCAL: {
74                 zblib_check_null_ret_error("ops->send_to_local",
75                         ops->send_to_local, FALSE);
76
77                 ret = ops->send_to_local(driver, request_id);
78         }
79         break;
80
81         default:
82         break;
83         }
84
85         Z_LOGD("ret: [%d]", ret);
86
87         return ret;
88 }
89
90 static void __zblib_driver_custom_free_hook(ZigBeeDriver *driver)
91 {
92         ZblibDriverCustomPrivData_t *priv_data = NULL;
93
94         zblib_check_null_ret("driver", driver);
95
96         /* Fetch private data */
97         priv_data = zblib_driver_ref_object(driver);
98         zblib_check_null_ret("priv_data", priv_data);
99
100         /* Free resources */
101         g_free(priv_data);
102 }
103
104 ZigBeeDriver *zblib_driver_custom_new(ZigBeePlugin *plugin,
105         const gchar *driver_name,
106         ZblibDriverCustomOps_t *ops)
107 {
108         ZigBeeDriver *driver = NULL;
109         ZblibDriverCustomPrivData_t *priv_data = NULL;
110         gboolean ret;
111
112         zblib_check_null_ret_error("plugin", plugin, NULL);
113
114         /* Create new driver */
115         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_CUSTOM);
116         zblib_check_null_ret_error("driver", driver, NULL);
117
118         /* Allocate memory for private data */
119         priv_data = g_malloc0(sizeof(ZblibDriverCustomPrivData_t));
120
121         /* Update private data */
122         priv_data->ops = ops;
123
124         /* Link private data to driver */
125         ret = zblib_driver_link_object(driver, priv_data);
126         if (FALSE == ret) {
127                 Z_LOGE("zblib_driver_link_object failed!");
128
129                 /* Free allocated resources */
130                 g_free(priv_data);
131                 g_free(driver);
132
133                 return NULL;
134         }
135
136         /* Set operations dispatcher function */
137         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_custom_dispatcher);
138         if (FALSE == ret) {
139                 Z_LOGE("zblib_driver_set_dispatcher failed!");
140
141                 /* Free allocated resources */
142                 g_free(priv_data);
143                 g_free(driver);
144
145                 return NULL;
146         }
147
148         /* Set free hook function */
149         ret = zblib_driver_set_free_hook(driver, __zblib_driver_custom_free_hook);
150         if (FALSE == ret) {
151                 Z_LOGE("zblib_driver_set_free_hook failed!");
152
153                 /* Free allocated resources */
154                 g_free(priv_data);
155                 g_free(driver);
156
157                 return NULL;
158         }
159
160         return driver;
161 }
162
163 void zblib_driver_custom_free(ZigBeeDriver *driver)
164 {
165         zblib_check_null_ret("driver", driver);
166
167         /* Free driver */
168         zblib_driver_free(driver);
169 }