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