e5586667c17dc919c3ccca6d43cb58c1345fe6cc
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_alarm.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_alarm.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverZclAlarmOps_t *ops; /**< Operations */
29 } ZblibDriverZclAlarmPrivData_t;
30
31 static gboolean __zblib_driver_alarm_dispatcher(ZigBeeDriver *driver, guint request_id)
32 {
33         ZigBeeService *service = NULL;
34         ZblibDriverZclAlarmPrivData_t *priv_data = NULL;
35         ZblibDriverZclAlarmOps_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_ALARM_OPS_GET_ALARM_COUNT: {
59                 zblib_check_null_ret_error("ops->get_alarm_count",
60                         ops->get_alarm_count, FALSE);
61
62                 ret = ops->get_alarm_count(driver, request_id);
63         }
64         break;
65
66         case ZBLIB_ZCL_ALARM_OPS_RESET_ALARM: {
67                 zblib_check_null_ret_error("ops->reset_alarm",
68                         ops->reset_alarm, FALSE);
69
70                 ret = ops->reset_alarm(driver, request_id);
71         }
72         break;
73
74         case ZBLIB_ZCL_ALARM_OPS_RESET_ALL_ALARM: {
75                 zblib_check_null_ret_error("ops->reset_all_alarm",
76                         ops->reset_all_alarm, FALSE);
77
78                 ret = ops->reset_all_alarm(driver, request_id);
79         }
80         break;
81
82         case ZBLIB_ZCL_ALARM_OPS_ALARM: {
83                 zblib_check_null_ret_error("ops->alarm",
84                         ops->alarm, FALSE);
85
86                 ret = ops->alarm(driver, request_id);
87         }
88         break;
89
90         case ZBLIB_ZCL_ALARM_OPS_RESET_ALARM_LOG: {
91                 zblib_check_null_ret_error("ops->reset_alarm_log",
92                         ops->reset_alarm_log, FALSE);
93
94                 ret = ops->reset_alarm_log(driver, request_id);
95         }
96         break;
97
98         case ZBLIB_ZCL_ALARM_OPS_GET_ALARM: {
99                 zblib_check_null_ret_error("ops->get_alarm",
100                         ops->get_alarm, FALSE);
101
102                 ret = ops->get_alarm(driver, request_id);
103         }
104         break;
105
106         default:
107         break;
108         }
109
110         Z_LOGD("ret: [%d]", ret);
111
112         return ret;
113 }
114
115 static void __zblib_driver_alarm_free_hook(ZigBeeDriver *driver)
116 {
117         ZblibDriverZclAlarmPrivData_t *priv_data = NULL;
118
119         zblib_check_null_ret("driver", driver);
120
121         /* Fetch private data */
122         priv_data = zblib_driver_ref_object(driver);
123         zblib_check_null_ret("priv_data", priv_data);
124
125         /* Free resources */
126         g_free(priv_data);
127 }
128
129 ZigBeeDriver *zblib_driver_zcl_alarm_new(ZigBeePlugin *plugin,
130         const gchar *driver_name,
131         ZblibDriverZclAlarmOps_t *ops)
132 {
133         ZigBeeDriver *driver = NULL;
134         ZigBeeService *service = NULL;
135         ZblibDriverZclAlarmPrivData_t *priv_data = NULL;
136         gboolean ret;
137
138         zblib_check_null_ret_error("plugin", plugin, NULL);
139
140         /* Create new driver */
141         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_ALARM);
142         zblib_check_null_ret_error("driver", driver, NULL);
143
144         /* Allocate memory for private data */
145         priv_data = g_malloc0(sizeof(ZblibDriverZclAlarmPrivData_t));
146
147         /* Update private data */
148         priv_data->ops = ops;
149
150         /* Link service to driver */
151         service = zblib_plugin_ref_service(plugin);
152         if (NULL == service) {
153                 Z_LOGE("zblib_plugin_ref_service failed!");
154
155                 /* Free allocated resources */
156                 g_free(priv_data);
157                 g_free(driver);
158
159                 return NULL;
160         }
161         zblib_driver_link_service(driver, service);
162
163         /* Link private data to driver */
164         ret = zblib_driver_link_object(driver, priv_data);
165         if (FALSE == ret) {
166                 Z_LOGE("zblib_driver_link_object failed!");
167
168                 /* Free allocated resources */
169                 g_free(priv_data);
170                 g_free(driver);
171
172                 return NULL;
173         }
174
175         /* Set operations dispatcher function */
176         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_alarm_dispatcher);
177         if (FALSE == ret) {
178                 Z_LOGE("zblib_driver_set_dispatcher failed!");
179
180                 /* Free allocated resources */
181                 g_free(priv_data);
182                 g_free(driver);
183
184                 return NULL;
185         }
186
187         /* Set free hook function */
188         ret = zblib_driver_set_free_hook(driver, __zblib_driver_alarm_free_hook);
189         if (FALSE == ret) {
190                 Z_LOGE("zblib_driver_set_free_hook failed!");
191
192                 /* Free allocated resources */
193                 g_free(priv_data);
194                 g_free(driver);
195
196                 return NULL;
197         }
198
199         return driver;
200 }
201
202 void zblib_driver_zcl_alarm_free(ZigBeeDriver *driver)
203 {
204         zblib_check_null_ret("driver", driver);
205
206         /* Free driver */
207         zblib_driver_free(driver);
208 }