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