2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Contact: Suresh Kumar N (suresh.n@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <zblib_service_interface.h>
22 #include <zblib_request.h>
23 #include <zblib_service.h>
25 /**< ZigBee Service interface object */
26 struct zblib_service_interface_type {
27 gchar *object_name; /**< ZigBee Service interface object name */
29 gpointer user_data; /**< ZigBee servcie interface data */
31 ZigBeeService *service; /**< ZigBee Service */
34 ZigBeeServiceInterface *zblib_service_interface_new(ZigBeeService *service,
35 const gchar *object_name)
37 ZigBeeServiceInterface *service_interface = NULL;
39 zblib_check_null_ret_error("service", service, NULL);
40 zblib_check_null_ret_error("object_name", object_name, NULL);
43 service_interface = g_malloc0(sizeof(ZigBeeServiceInterface));
46 service_interface->object_name = g_strdup(object_name);
47 service_interface->service = service;
49 Z_LOGI("Service interface created - Name [%s]",
50 service_interface->object_name);
52 return service_interface;
55 void zblib_service_interface_free(ZigBeeService *service,
56 ZigBeeServiceInterface *service_interface)
58 zblib_check_null_ret("service", service);
59 zblib_check_null_ret("service_interface", service_interface);
61 Z_LOGI("Freeing Service interface - Name [%p]",
62 service_interface->object_name);
65 g_free(service_interface->object_name);
66 g_free(service_interface);
69 char *zblib_service_interface_get_name(ZigBeeServiceInterface *service_interface)
71 zblib_check_null_ret_error("service_interface", service_interface, NULL);
73 return g_strdup(service_interface->object_name);
76 gboolean zblib_service_interface_link_user_data(ZigBeeServiceInterface *service_interface,
79 zblib_check_null_ret_error("service_interface", service_interface, FALSE);
81 service_interface->user_data = user_data;
86 gpointer zblib_service_interface_ref_user_data(ZigBeeServiceInterface *service_interface)
88 zblib_check_null_ret_error("service_interface", service_interface, NULL);
90 return service_interface->user_data;
93 ZigBeeService *zblib_service_interface_ref_service(ZigBeeServiceInterface *service_interface)
95 zblib_check_null_ret_error("service_interface", service_interface, NULL);
97 return service_interface->service;
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)
109 zblib_check_null_ret_error("service_interface",
110 service_interface, ZIGBEE_SERVICE_INTERFACE_INVALID_REQUEST_ID);
112 /* Create request type */
113 request_type = zblib_request_generate_request_type(driver_type, ops_id);
115 /* Create new request */
116 request_id = zblib_request_new(service_interface,
118 request_data, request_data_len);
120 /* Set response callback */
121 ret = zblib_request_set_response_cb(service_interface,
123 (gpointer)resp_cb, (gpointer)resp_cb_data);
125 Z_LOGE("zblib_request_set_response_cb failed!");
127 /* Free request_id */
128 zblib_request_free(service_interface, request_id);
129 request_id = ZIGBEE_SERVICE_INTERFACE_INVALID_REQUEST_ID;
135 void zblib_service_interface_free_request(ZigBeeServiceInterface *service_interface,
138 zblib_check_null_ret("service_interface", service_interface);
140 /* Free request_id */
141 zblib_request_free(service_interface, request_id);
144 gboolean zblib_service_interface_dispatch_request(ZigBeeServiceInterface *service_interface,
147 ZigBeeService *service = NULL;
149 zblib_check_null_ret_error("service_interface", service_interface, FALSE);
152 service = service_interface->service;
153 zblib_check_null_ret_error("service", service, FALSE);
155 /* Dispatch request to service */
156 return zblib_service_dispatch_request(service, request_id);
159 void zblib_service_interface_send_response(ZigBeeServiceInterface *service_interface,
160 guint request_id, gpointer resp_data, guint resp_data_len)
162 ZblibServiceInterfaceResponseCb_t resp_cb;
163 gpointer resp_cb_data;
165 zblib_check_null_ret("service_interface", service_interface);
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);
172 /* Invoke send response function */
173 resp_cb(service_interface,
174 request_id, resp_data, resp_data_len, resp_cb_data);
177 zblib_request_free(service_interface, request_id);