f3261829111fdefe0bb6e67252038d6bfa619893
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / zblib_service_interface.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
21 #include <zblib_service_interface.h>
22
23 /**< ZigBee Service interface object */
24 struct zblib_service_interface_type {
25         gchar *object_name; /**< ZigBee Service interface object name */
26         struct zblib_service_interface_methods *methods;  /**< ZigBee Service interface object methods */
27
28         gpointer user_data; /**< ZigBee servcie interface data */
29
30         ZigBeeService *service; /**< ZigBee Service */
31 };
32
33 ZigBeeServiceInterface *zblib_service_interface_new(ZigBeeService *service,
34         const gchar *object_name,
35         struct zblib_service_interface_methods *methods)
36 {
37         ZigBeeServiceInterface *service_interface = NULL;
38
39         if ((NULL == service) || (NULL == object_name)) {
40                 Z_LOGE("service [%p] or object_name [%p] is NULL",
41                         service, object_name);
42                 return NULL;
43         }
44
45         /* Allocate memory */
46         service_interface = g_malloc0(sizeof(ZigBeeServiceInterface));
47
48         /* Update fields */
49         service_interface->object_name = g_strdup(object_name);
50         service_interface->methods = methods;
51         service_interface->service = service;
52
53         Z_LOGI("Service interface created - Name [%s]",
54                 service_interface->object_name);
55
56         return service_interface;
57 }
58
59 void zblib_service_interface_free(ZigBeeService *service,
60         ZigBeeServiceInterface *service_interface)
61 {
62         if ((NULL == service) || (NULL == service_interface)) {
63                 Z_LOGE("service [%p] or service_interface [%p] is NULL",
64                         service, service_interface);
65                 return;
66         }
67
68         Z_LOGI("Freeing Service interface - Name [%p]",
69                         service_interface->object_name);
70
71         /* Free resources */
72         g_free(service_interface->object_name);
73         g_free(service_interface);
74 }
75
76 char *zblib_service_interface_get_name(ZigBeeServiceInterface *service_interface)
77 {
78         if (NULL == service_interface) {
79                 Z_LOGE("service_interface is NULL");
80                 return NULL;
81         }
82
83         return g_strdup(service_interface->object_name);
84 }
85
86 gboolean zblib_service_interface_link_user_data(ZigBeeServiceInterface *service_interface,
87         gpointer user_data)
88 {
89         if (NULL == service_interface) {
90                 Z_LOGE("service_interface is NULL");
91                 return FALSE;
92         }
93
94         service_interface->user_data = user_data;
95
96         return TRUE;
97 }
98
99 gpointer zblib_service_interface_ref_user_data(ZigBeeServiceInterface *service_interface)
100 {
101         if (NULL == service_interface) {
102                 Z_LOGE("service_interface is NULL");
103                 return NULL;
104         }
105
106         return service_interface->user_data;
107 }
108
109 ZigBeeService *zblib_service_interface_ref_service(ZigBeeServiceInterface *service_interface)
110 {
111         if (NULL == service_interface) {
112                 Z_LOGE("service_interface is NULL");
113                 return NULL;
114         }
115
116         return service_interface->service;
117 }