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