Implement module dispatcher functions
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_door_lock.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_door_lock.h"
24
25 /**< Private data */
26 typedef struct {
27         ZblibDriverDoorLockOps_t *ops; /**< Operations */
28 } ZblibDriverDoorLockPrivData_t;
29
30 static gboolean __zblib_driver_door_lock_dispatcher(ZigBeeDriver *driver, guint request_id)
31 {
32         ZigBeeService *service = NULL;
33         ZblibDriverDoorLockPrivData_t *priv_data = NULL;
34         ZblibDriverDoorLockOps_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_DOOR_LOCK_OPS_SUBSCRIBE_LOCK_EVENT: {
58                 zblib_check_null_ret_error("ops->subscribe_lock_event",
59                         ops->subscribe_lock_event, FALSE);
60
61                 ret = ops->subscribe_lock_event(driver, request_id);
62         }
63         break;
64
65         case ZBLIB_DOOR_LOCK_OPS_SET_DOOR_LOCK_PIN: {
66                 zblib_check_null_ret_error("ops->set_door_lock_pin",
67                         ops->set_door_lock_pin, FALSE);
68
69                 ret = ops->set_door_lock_pin(driver, request_id);
70         }
71         break;
72
73         case ZBLIB_DOOR_LOCK_OPS_SET_DOOR_LOCK: {
74                 zblib_check_null_ret_error("ops->set_door_lock",
75                         ops->set_door_lock, FALSE);
76
77                 ret = ops->set_door_lock(driver, request_id);
78         }
79         break;
80
81         case ZBLIB_DOOR_LOCK_OPS_GET_LOCK_STATE: {
82                 zblib_check_null_ret_error("ops->get_lock_state", ops->get_lock_state, FALSE);
83
84                 ret = ops->get_lock_state(driver, request_id);
85         }
86         break;
87
88         default:
89         break;
90         }
91
92         Z_LOGD("ret: [%d]", ret);
93
94         return ret;
95 }
96
97 static void __zblib_driver_door_lock_free_hook(ZigBeeDriver *driver)
98 {
99         ZblibDriverDoorLockPrivData_t *priv_data = NULL;
100
101         zblib_check_null_ret("driver", driver);
102
103         /* Fetch private data */
104         priv_data = zblib_driver_ref_object(driver);
105         zblib_check_null_ret("priv_data", priv_data);
106
107         /* Free resources */
108         g_free(priv_data);
109 }
110
111 ZigBeeDriver *zblib_driver_door_lock_new(ZigBeePlugin *plugin,
112         const gchar *driver_name,
113         ZblibDriverDoorLockOps_t *ops)
114 {
115         ZigBeeDriver *driver = NULL;
116         ZblibDriverDoorLockPrivData_t *priv_data = NULL;
117         gboolean ret;
118
119         zblib_check_null_ret_error("plugin", plugin, NULL);
120
121         /* Create new driver */
122         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_DOOR_LOCK);
123         zblib_check_null_ret_error("driver", driver, NULL);
124
125         /* Allocate memory for private data */
126         priv_data = g_malloc0(sizeof(ZblibDriverDoorLockPrivData_t));
127
128         /* Update private data */
129         priv_data->ops = ops;
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_door_lock_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_door_lock_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_door_lock_free(ZigBeeDriver *driver)
171 {
172         zblib_check_null_ret("driver", driver);
173
174         /* Free driver */
175         zblib_driver_free(driver);
176 }