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