Add module APIs in zigbee-lib
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_scene.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
22 #include "zblib_driver_zcl_scene.h"
23
24 /**< Private data */
25 typedef struct {
26         ZblibDriverZclSceneOps_t *ops; /**< Operations */
27 } ZblibDriverZclScenePrivData_t;
28
29 static gboolean __zblib_driver_zcl_scene_dispatcher(ZigBeeDriver *driver)
30 {
31         if (NULL == driver) {
32                 Z_LOGE("driver is NULL");
33                 return FALSE;
34         }
35
36         return TRUE;
37 }
38
39 static void __zblib_driver_zcl_scene_free_hook(ZigBeeDriver *driver)
40 {
41         ZblibDriverZclScenePrivData_t *priv_data = NULL;
42
43         if (NULL == driver) {
44                 Z_LOGE("driver is NULL");
45                 return;
46         }
47
48         /* Fetch private data */
49         priv_data = zblib_driver_ref_object(driver);
50         if (NULL == priv_data) {
51                 Z_LOGE("priv_data is NULL");
52                 return;
53         }
54
55         /* Free resources */
56         g_free(priv_data);
57 }
58
59 ZigBeeDriver *zblib_driver_zcl_scene_new(ZigBeePlugin *plugin,
60         const gchar *driver_name,
61         ZblibDriverZclSceneOps_t *ops)
62 {
63         ZigBeeDriver *driver = NULL;
64         ZblibDriverZclScenePrivData_t *priv_data = NULL;
65         gboolean ret;
66
67         if (NULL == plugin) {
68                 Z_LOGE("plugin is NULL");
69                 return NULL;
70         }
71
72         /* Create new driver */
73         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_SCENE);
74         if (NULL == driver) {
75                 Z_LOGE("driver is NULL");
76                 return NULL;
77         }
78
79         /* Allocate memory for private data */
80         priv_data = g_malloc0(sizeof(ZblibDriverZclScenePrivData_t));
81
82         /* Update private data */
83         priv_data->ops = ops;
84
85         /* Link private data to driver */
86         ret = zblib_driver_link_object(driver, priv_data);
87         if (FALSE == ret) {
88                 Z_LOGE("zblib_driver_link_object failed!");
89
90                 /* Free allocated resources */
91                 g_free(priv_data);
92                 g_free(driver);
93
94                 return NULL;
95         }
96
97         /* Set operations dispatcher function */
98         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_scene_dispatcher);
99         if (FALSE == ret) {
100                 Z_LOGE("zblib_driver_set_dispatcher failed!");
101
102                 /* Free allocated resources */
103                 g_free(priv_data);
104                 g_free(driver);
105
106                 return NULL;
107         }
108
109         /* Set free hook function */
110         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_scene_free_hook);
111         if (FALSE == ret) {
112                 Z_LOGE("zblib_driver_set_free_hook failed!");
113
114                 /* Free allocated resources */
115                 g_free(priv_data);
116                 g_free(driver);
117
118                 return NULL;
119         }
120
121         return driver;
122 }
123
124 void zblib_driver_zcl_scene_free(ZigBeeDriver *driver)
125 {
126         if (NULL == driver) {
127                 Z_LOGE("driver is NULL");
128                 return;
129         }
130
131         /* Free driver */
132         zblib_driver_free(driver);
133 }