Implement ZCL OnOff Cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_on_off.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_on_off.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverZclOnOffOps_t *ops; /**< Operations */
29 } ZblibDriverZclOnOffPrivData_t;
30
31 static gboolean __zblib_driver_zcl_on_off_dispatcher(ZigBeeDriver *driver, guint request_id)
32 {
33         ZigBeeService *service = NULL;
34         ZblibDriverZclOnOffPrivData_t *priv_data = NULL;
35         ZblibDriverZclOnOffOps_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_ON_OFF_OPS_SET_ON_OFF: {
59                 zblib_check_null_ret_error("ops->set_on_off",
60                         ops->set_on_off, FALSE);
61
62                 ret = ops->set_on_off(driver, request_id);
63         }
64         break;
65
66         case ZBLIB_ZCL_ON_OFF_OPS_GET_ON_OFF_STATE: {
67                 zblib_check_null_ret_error("ops->get_on_off_state",
68                         ops->get_on_off_state, FALSE);
69
70                 ret = ops->get_on_off_state(driver, request_id);
71         }
72         break;
73
74         default:
75         break;
76         }
77
78         Z_LOGD("ret: [%d]", ret);
79
80         return ret;
81 }
82
83 static void __zblib_driver_zcl_on_off_free_hook(ZigBeeDriver *driver)
84 {
85         ZblibDriverZclOnOffPrivData_t *priv_data = NULL;
86
87         zblib_check_null_ret("driver", driver);
88
89         /* Fetch private data */
90         priv_data = zblib_driver_ref_object(driver);
91         zblib_check_null_ret("priv_data", priv_data);
92
93         /* Free resources */
94         g_free(priv_data);
95 }
96
97 ZigBeeDriver *zblib_driver_zcl_on_off_new(ZigBeePlugin *plugin,
98         const gchar *driver_name,
99         ZblibDriverZclOnOffOps_t *ops)
100 {
101         ZigBeeService *service = NULL;
102         ZigBeeDriver *driver = NULL;
103         ZblibDriverZclOnOffPrivData_t *priv_data = NULL;
104         gboolean ret;
105
106         zblib_check_null_ret_error("plugin", plugin, NULL);
107
108         /* Create new driver */
109         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_ON_OFF);
110         zblib_check_null_ret_error("driver", driver, NULL);
111
112         /* Allocate memory for private data */
113         priv_data = g_malloc0(sizeof(ZblibDriverZclOnOffPrivData_t));
114
115         /* Update private data */
116         priv_data->ops = ops;
117
118         /* Link service to driver */
119         service = zblib_plugin_ref_service(plugin);
120         if (NULL == service) {
121                 Z_LOGE("zblib_plugin_ref_service failed!");
122
123                 /* Free allocated resources */
124                 g_free(priv_data);
125                 g_free(driver);
126
127                 return NULL;
128         }
129         zblib_driver_link_service(driver, service);
130
131         /* Link private data to driver */
132         ret = zblib_driver_link_object(driver, priv_data);
133         if (FALSE == ret) {
134                 Z_LOGE("zblib_driver_link_object failed!");
135
136                 /* Free allocated resources */
137                 g_free(priv_data);
138                 g_free(driver);
139
140                 return NULL;
141         }
142
143         /* Set operations dispatcher function */
144         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_on_off_dispatcher);
145         if (FALSE == ret) {
146                 Z_LOGE("zblib_driver_set_dispatcher failed!");
147
148                 /* Free allocated resources */
149                 g_free(priv_data);
150                 g_free(driver);
151
152                 return NULL;
153         }
154
155         /* Set free hook function */
156         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_on_off_free_hook);
157         if (FALSE == ret) {
158                 Z_LOGE("zblib_driver_set_free_hook failed!");
159
160                 /* Free allocated resources */
161                 g_free(priv_data);
162                 g_free(driver);
163
164                 return NULL;
165         }
166
167         return driver;
168 }
169
170 void zblib_driver_zcl_on_off_free(ZigBeeDriver *driver)
171 {
172         zblib_check_null_ret("driver", driver);
173
174         /* Free driver */
175         zblib_driver_free(driver);
176 }