4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
27 #include <gio/gunixfdlist.h>
28 #include <sys/socket.h>
31 #include "bluetooth-api.h"
32 #include "bt-internal-types.h"
34 #include "bt-common.h"
35 #include "bt-request-sender.h"
36 #include "bt-event-handler.h"
40 static GSList *rfcomm_nodes;
50 bluetooth_device_address_t addr;
51 guint disconnect_idle_id;
54 static rfcomm_info_t *__find_rfcomm_info_with_id(int id)
58 for (l = rfcomm_nodes; l != NULL; l = l->next) {
59 rfcomm_info_t *info = l->data;
68 static rfcomm_info_t *__find_rfcomm_info_with_fd(int fd)
72 for (l = rfcomm_nodes; l != NULL; l = l->next) {
73 rfcomm_info_t *info = l->data;
82 static rfcomm_info_t *__find_rfcomm_info_with_path(const gchar *path)
86 for (l = rfcomm_nodes; l != NULL; l = l->next) {
87 rfcomm_info_t *info = l->data;
89 if (g_strcmp0(info->path, path) == 0)
96 gboolean _check_uuid_path(char *path, char *uuid)
98 rfcomm_info_t *info = NULL;
99 info = __find_rfcomm_info_with_path(path);
103 if (strcmp(info->uuid, uuid) == 0)
109 static void __connected_cb(rfcomm_info_t *info, bt_event_info_t *event_info)
111 bluetooth_rfcomm_connection_t conn_info;
113 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
114 conn_info.device_role = RFCOMM_ROLE_SERVER;
115 g_strlcpy(conn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
116 conn_info.socket_fd = info->fd;
117 conn_info.device_addr = info->addr;
118 conn_info.server_id = info->id;
120 BT_INFO_C("Connected [RFCOMM Server]");
121 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
122 BLUETOOTH_ERROR_NONE, &conn_info,
123 event_info->cb, event_info->user_data);
126 static gboolean __rfcomm_server_disconnect(rfcomm_info_t *info)
128 bluetooth_rfcomm_disconnection_t disconn_info;
130 bt_event_info_t *event_info;
132 BT_INFO_C("Disconnected [RFCOMM Server]");
134 if (info->data_id > 0) {
135 g_source_remove(info->data_id);
145 g_io_channel_shutdown(info->data_io, TRUE, NULL);
146 g_io_channel_unref(info->data_io);
147 info->data_io = NULL;
149 info->disconnect_idle_id = 0;
150 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
151 if (event_info == NULL)
154 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
155 disconn_info.device_role = RFCOMM_ROLE_SERVER;
156 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
157 disconn_info.socket_fd = fd;
158 disconn_info.device_addr = info->addr;
160 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
161 BLUETOOTH_ERROR_NONE, &disconn_info,
162 event_info->cb, event_info->user_data);
168 static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
173 int result = BLUETOOTH_ERROR_NONE;
174 rfcomm_info_t *info = data;
175 bt_event_info_t *event_info;
176 bluetooth_rfcomm_received_data_t data_r;
177 GIOStatus status = G_IO_STATUS_NORMAL;
180 retv_if(info == NULL, FALSE);
182 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
184 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
185 BT_ERR_C("RFComm Server disconnected: %d", info->fd);
187 if (info->disconnect_idle_id > 0) {
188 BT_INFO("Disconnect idle still not process remove source");
189 g_source_remove(info->disconnect_idle_id);
190 info->disconnect_idle_id = 0;
193 __rfcomm_server_disconnect(info);
197 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
199 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
201 if (status != G_IO_STATUS_NORMAL) {
202 BT_ERR("IO Channel read is failed with %d", status);
206 BT_ERR("IO Channel read error [%s]", err->message);
207 if (status == G_IO_STATUS_ERROR &&
208 !g_strcmp0(err->message, "Connection reset by peer")) {
209 BT_ERR("cond : %d", cond);
211 if (info->disconnect_idle_id > 0) {
212 BT_INFO("Disconnect idle still not process remove source");
213 g_source_remove(info->disconnect_idle_id);
214 info->disconnect_idle_id = 0;
216 __rfcomm_server_disconnect(info);
225 BT_ERR("Length is zero");
227 if (event_info == NULL) {
232 data_r.socket_fd = info->fd;
233 data_r.buffer_size = len;
234 data_r.buffer = buffer;
236 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
238 event_info->cb, event_info->user_data);
245 int new_server_connection(const char *path, int fd, bluetooth_device_address_t *addr)
248 bt_event_info_t *event_info;
250 BT_DBG("%s %d", path, fd);
252 info = __find_rfcomm_info_with_path(path);
257 memcpy(&info->addr, addr, sizeof(bluetooth_device_address_t));
259 info->data_io = g_io_channel_unix_new(info->fd);
261 g_io_channel_set_encoding(info->data_io, NULL, NULL);
262 g_io_channel_set_flags(info->data_io, G_IO_FLAG_NONBLOCK, NULL);
264 info->data_id = g_io_add_watch(info->data_io,
265 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
266 __data_received_cb, info);
268 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
270 __connected_cb(info, event_info);
276 static rfcomm_info_t *__register_method()
283 id = __rfcomm_assign_id();
287 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
289 object_id = _bt_register_new_conn(path, new_server_connection);
291 __rfcomm_delete_id(id);
294 info = g_new(rfcomm_info_t, 1);
295 info->object_id = (guint)object_id;
300 rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
305 void free_rfcomm_info(rfcomm_info_t *info)
307 bt_event_info_t *event_info;
310 if (info->disconnect_idle_id > 0) {
311 BT_INFO("Disconnect idle still not process remove source");
312 g_source_remove(info->disconnect_idle_id);
313 info->disconnect_idle_id = 0;
316 __rfcomm_delete_id(info->id);
317 _bt_unregister_gdbus(info->object_id);
320 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
322 BT_DBG("event type %d", event_info->event_type);
323 __rfcomm_server_disconnect(info);
331 void _bt_rfcomm_server_free_all()
333 BT_DBG("Free all the servers");
335 g_slist_free_full(rfcomm_nodes, (GDestroyNotify)free_rfcomm_info);
340 BT_EXPORT_API int bluetooth_rfcomm_create_socket(const char *uuid)
347 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
350 BT_CHECK_ENABLED(return);
351 BT_CHECK_PARAMETER(uuid, return);
352 BT_INFO("UUID Provided %s", uuid);
354 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CREATE_SOCKET)
355 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
356 BT_ERR("Don't have a privilege to use this API");
357 return BLUETOOTH_ERROR_PERMISSION_DEINED;
361 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
362 info = __register_method();
366 info->uuid = g_strdup(uuid);
367 info->disconnect_idle_id = 0;
372 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
374 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
375 g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
377 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CREATE_SOCKET,
378 in_param1, in_param2, in_param3, in_param4, &out_param);
380 BT_DBG("result: %x", result);
382 if (result == BLUETOOTH_ERROR_NONE) {
383 socket_fd = g_array_index(out_param, int, 0);
385 BT_ERR("Fail to send request");
388 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
394 BT_EXPORT_API int bluetooth_rfcomm_remove_socket(int socket_fd)
402 BT_CHECK_ENABLED(return);
404 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_REMOVE_SOCKET)
405 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
406 BT_ERR("Don't have a privilege to use this API");
407 return BLUETOOTH_ERROR_PERMISSION_DEINED;
411 BT_INFO("<<<<<<<<< RFCOMM Remove socket request from app, fd=[%d] >>>>>>>>>>>", socket_fd);
413 info = __find_rfcomm_info_with_id(socket_fd);
415 return BLUETOOTH_ERROR_INVALID_PARAM;
417 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER,info->uuid);
418 _bt_unregister_profile(info->path);
420 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
421 free_rfcomm_info(info);
423 return BLUETOOTH_ERROR_NONE;
426 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
428 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
430 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REMOVE_SOCKET,
431 in_param1, in_param2, in_param3, in_param4, &out_param);
433 BT_DBG("result: %x", result);
435 if (result == BLUETOOTH_ERROR_NONE) {
436 _bt_remove_server(socket_fd);
439 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
445 BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
452 BT_INFO(">>>>>>>>RFCOMM server disconnect request from APP>>>>>>>>>");
454 info = __find_rfcomm_info_with_fd(socket_fd);
456 return BLUETOOTH_ERROR_INVALID_PARAM;
458 if (info->data_io == NULL)
459 return BLUETOOTH_ERROR_NOT_CONNECTED;
461 g_io_channel_shutdown(info->data_io, TRUE, NULL);
462 g_io_channel_unref(info->data_io);
463 info->data_io = NULL;
465 _bt_convert_addr_type_to_string(address, info->addr.addr);
466 BT_DBG("Address %s", address);
467 _bt_disconnect_profile(address, info->uuid, NULL,NULL);
469 info->disconnect_idle_id = g_idle_add(__rfcomm_server_disconnect, info);
472 return BLUETOOTH_ERROR_NONE;
476 BT_CHECK_ENABLED(return);
479 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
481 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
483 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT,
484 in_param1, in_param2, in_param3, in_param4, &out_param);
486 BT_DBG("result: %x", result);
488 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
494 BT_EXPORT_API gboolean bluetooth_rfcomm_is_server_uuid_available(const char *uuid)
497 gboolean available = TRUE;
498 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
500 retv_if(uuid == NULL, FALSE);
501 retv_if(bluetooth_check_adapter() ==
502 BLUETOOTH_ADAPTER_DISABLED, FALSE);
505 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
507 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
508 g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
510 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_IS_UUID_AVAILABLE,
511 in_param1, in_param2, in_param3, in_param4, &out_param);
513 BT_DBG("result: %x", result);
515 if (result == BLUETOOTH_ERROR_NONE) {
516 available = g_array_index(out_param, gboolean, 0);
519 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
521 BT_DBG("available: %d", available);
526 BT_EXPORT_API int bluetooth_rfcomm_server_is_connected(bluetooth_device_address_t *device_address, gboolean *connected)
530 char connected_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
531 char input_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
533 BT_CHECK_PARAMETER(device_address, return);
534 BT_CHECK_PARAMETER(connected, return);
536 _bt_convert_addr_type_to_string(input_addr, device_address->addr);
540 for (l = rfcomm_nodes; l != NULL; l = l->next) {
545 _bt_convert_addr_type_to_string(connected_addr, info->addr.addr);
547 if (g_strcmp0(connected_addr, input_addr) == 0) {
549 return BLUETOOTH_ERROR_NONE;
553 return BLUETOOTH_ERROR_NONE;
556 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int socket_fd, int max_pending_connection)
562 gboolean native_service = TRUE;
565 BT_CHECK_ENABLED(return);
568 BT_INFO("<<<<<<<<< RFCOMM Listen & accept from app >>>>>>>>>>>");
570 info = __find_rfcomm_info_with_id(socket_fd);
572 return BLUETOOTH_ERROR_INVALID_PARAM;
574 bt_register_profile_info_t profile_info;
577 profile_info.authentication = TRUE;
578 profile_info.authorization = TRUE;
579 profile_info.obj_path = info->path;
580 profile_info.role = NULL;
581 profile_info.service = info->uuid;
582 profile_info.uuid = info->uuid;
583 BT_INFO("uuid %s", profile_info.uuid);
584 result = _bt_register_profile(&profile_info, TRUE);
589 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
591 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
592 g_array_append_vals(in_param2, &max_pending_connection, sizeof(int));
593 g_array_append_vals(in_param3, &native_service, sizeof(gboolean));
595 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
596 in_param1, in_param2, in_param3, in_param4, &out_param);
598 BT_DBG("result: %x", result);
600 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
606 BT_EXPORT_API int bluetooth_rfcomm_listen(int socket_fd, int max_pending_connection)
612 gboolean native_service = FALSE;
615 BT_CHECK_ENABLED(return);
618 BT_INFO("<<<<<<<<< RFCOMM Listen >>>>>>>>>>>");
620 info = __find_rfcomm_info_with_id(socket_fd);
622 return BLUETOOTH_ERROR_INVALID_PARAM;
624 bt_register_profile_info_t profile_info;
627 profile_info.authentication = TRUE;
628 profile_info.authorization = TRUE;
629 profile_info.obj_path = info->path;
630 profile_info.role = NULL;
631 profile_info.service = info->uuid;
632 profile_info.uuid = info->uuid;
633 BT_INFO("UUID %s", info->uuid);
634 BT_INFO("PATH %s", info->path);
635 result = _bt_register_profile_platform(&profile_info, TRUE);
636 if (result != BLUETOOTH_ERROR_NONE)
639 return _bt_register_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid,
640 info->path, socket_fd);
644 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
646 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
647 g_array_append_vals(in_param2, &max_pending_connection, sizeof(int));
648 g_array_append_vals(in_param3, &native_service, sizeof(gboolean));
650 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
651 in_param1, in_param2, in_param3, in_param4, &out_param);
653 BT_DBG("result: %x", result);
655 if (result == BLUETOOTH_ERROR_NONE) {
656 _bt_add_server(socket_fd);
659 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
665 BT_EXPORT_API int bluetooth_rfcomm_accept_connection(int server_fd)
669 BT_CHECK_ENABLED(return);
672 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
674 g_array_append_vals(in_param1, &server_fd, sizeof(int));
676 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_ACCEPT_CONNECTION,
677 in_param1, in_param2, in_param3, in_param4, &out_param);
679 BT_DBG("result: %x", result);
681 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
686 BT_EXPORT_API int bluetooth_rfcomm_reject_connection(int server_fd)
690 BT_CHECK_ENABLED(return);
695 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
697 g_array_append_vals(in_param1, &server_fd, sizeof(int));
699 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REJECT_CONNECTION,
700 in_param1, in_param2, in_param3, in_param4, &out_param);
702 BT_DBG("result: %x", result);
704 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);