Adding service interface layer logic for request processing
[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 #include <zblib_request.h>
23 #include <zblib_service.h>
24
25 /**< ZigBee Service interface object */
26 struct zblib_service_interface_type {
27         gchar *object_name; /**< ZigBee Service interface object name */
28
29         gpointer user_data; /**< ZigBee servcie interface data */
30
31         ZigBeeService *service; /**< ZigBee Service */
32 };
33
34 ZigBeeServiceInterface *zblib_service_interface_new(ZigBeeService *service,
35         const gchar *object_name)
36 {
37         ZigBeeServiceInterface *service_interface = NULL;
38
39         zblib_check_null_ret_error("service", service, NULL);
40         zblib_check_null_ret_error("object_name", object_name, NULL);
41
42         /* Allocate memory */
43         service_interface = g_malloc0(sizeof(ZigBeeServiceInterface));
44
45         /* Update fields */
46         service_interface->object_name = g_strdup(object_name);
47         service_interface->service = service;
48
49         Z_LOGI("Service interface created - Name [%s]",
50                 service_interface->object_name);
51
52         return service_interface;
53 }
54
55 void zblib_service_interface_free(ZigBeeService *service,
56         ZigBeeServiceInterface *service_interface)
57 {
58         zblib_check_null_ret("service", service);
59         zblib_check_null_ret("service_interface", service_interface);
60
61         Z_LOGI("Freeing Service interface - Name [%p]",
62                         service_interface->object_name);
63
64         /* Free resources */
65         g_free(service_interface->object_name);
66         g_free(service_interface);
67 }
68
69 char *zblib_service_interface_get_name(ZigBeeServiceInterface *service_interface)
70 {
71         zblib_check_null_ret_error("service_interface", service_interface, NULL);
72
73         return g_strdup(service_interface->object_name);
74 }
75
76 gboolean zblib_service_interface_link_user_data(ZigBeeServiceInterface *service_interface,
77         gpointer user_data)
78 {
79         zblib_check_null_ret_error("service_interface", service_interface, FALSE);
80
81         service_interface->user_data = user_data;
82
83         return TRUE;
84 }
85
86 gpointer zblib_service_interface_ref_user_data(ZigBeeServiceInterface *service_interface)
87 {
88         zblib_check_null_ret_error("service_interface", service_interface, NULL);
89
90         return service_interface->user_data;
91 }
92
93 ZigBeeService *zblib_service_interface_ref_service(ZigBeeServiceInterface *service_interface)
94 {
95         zblib_check_null_ret_error("service_interface", service_interface, NULL);
96
97         return service_interface->service;
98 }
99
100 gint zblib_service_interface_new_request(ZigBeeServiceInterface *service_interface,
101         ZblibDriverType_e driver_type, guint ops_id,
102         gpointer request_data, guint request_data_len,
103         ZblibServiceInterfaceResponseCb_t resp_cb, gpointer resp_cb_data)
104 {
105         guint request_type;
106         gint request_id = 0;
107         gboolean ret;
108
109         zblib_check_null_ret_error("service_interface",
110                 service_interface, ZIGBEE_SERVICE_INTERFACE_INVALID_REQUEST_ID);
111
112         /* Create request type */
113         request_type = zblib_request_generate_request_type(driver_type, ops_id);
114
115         /* Create new request */
116         request_id = zblib_request_new(service_interface,
117                 request_type,
118                 request_data, request_data_len);
119
120         /* Set response callback */
121         ret = zblib_request_set_response_cb(service_interface,
122                 request_id,
123                 (gpointer)resp_cb, (gpointer)resp_cb_data);
124         if (FALSE == ret) {
125                 Z_LOGE("zblib_request_set_response_cb failed!");
126
127                 /* Free request_id */
128                 zblib_request_free(service_interface, request_id);
129                 request_id = ZIGBEE_SERVICE_INTERFACE_INVALID_REQUEST_ID;
130         }
131
132         return request_id;
133 }
134
135 void zblib_service_interface_free_request(ZigBeeServiceInterface *service_interface,
136         guint request_id)
137 {
138         zblib_check_null_ret("service_interface", service_interface);
139
140         /* Free request_id */
141         zblib_request_free(service_interface, request_id);
142 }
143
144 gboolean zblib_service_interface_dispatch_request(ZigBeeServiceInterface *service_interface,
145         guint request_id)
146 {
147         ZigBeeService *service = NULL;
148
149         zblib_check_null_ret_error("service_interface", service_interface, FALSE);
150
151         /* Fetch service */
152         service = service_interface->service;
153         zblib_check_null_ret_error("service", service, FALSE);
154
155         /* Dispatch request to service */
156         return zblib_service_dispatch_request(service, request_id);
157 }
158
159 void zblib_service_interface_send_response(ZigBeeServiceInterface *service_interface,
160         guint request_id, gpointer resp_data, guint resp_data_len)
161 {
162         ZblibServiceInterfaceResponseCb_t resp_cb;
163         gpointer resp_cb_data;
164
165         zblib_check_null_ret("service_interface", service_interface);
166
167         /* Fetch response callback and response callback data from request_id  */
168         resp_cb = zblib_request_ref_response_cb(service_interface,
169                 request_id, &resp_cb_data);
170         zblib_check_null_ret("resp_cb", resp_cb);
171
172         /* Invoke send response function */
173         resp_cb(service_interface,
174                 request_id, resp_data, resp_data_len, resp_cb_data);
175
176         /* Free request */
177         zblib_request_free(service_interface, request_id);
178 }