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 static rfcomm_info_t *__find_rfcomm_info_with_uuid(const char *uuid)
100 for (l = rfcomm_nodes; l != NULL; l = l->next) {
101 rfcomm_info_t *info = l->data;
103 if (g_strcmp0(info->uuid, uuid) == 0)
110 gboolean _check_uuid_path(char *path, char *uuid)
112 rfcomm_info_t *info = NULL;
113 info = __find_rfcomm_info_with_path(path);
117 if (strcmp(info->uuid, uuid) == 0)
123 static void __connected_cb(rfcomm_info_t *info, bt_event_info_t *event_info)
125 bluetooth_rfcomm_connection_t conn_info;
127 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
128 conn_info.device_role = RFCOMM_ROLE_SERVER;
129 g_strlcpy(conn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
130 conn_info.socket_fd = info->fd;
131 conn_info.device_addr = info->addr;
132 conn_info.server_id = info->id;
134 BT_INFO_C("Connected [RFCOMM Server]");
135 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
136 BLUETOOTH_ERROR_NONE, &conn_info,
137 event_info->cb, event_info->user_data);
140 static gboolean __rfcomm_server_disconnect(rfcomm_info_t *info)
142 bluetooth_rfcomm_disconnection_t disconn_info;
144 bt_event_info_t *event_info;
146 BT_INFO_C("Disconnected [RFCOMM Server]");
148 if (info->data_id > 0) {
149 g_source_remove(info->data_id);
159 g_io_channel_shutdown(info->data_io, TRUE, NULL);
160 g_io_channel_unref(info->data_io);
161 info->data_io = NULL;
163 info->disconnect_idle_id = 0;
164 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
165 if (event_info == NULL)
168 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
169 disconn_info.device_role = RFCOMM_ROLE_SERVER;
170 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
171 disconn_info.socket_fd = fd;
172 disconn_info.device_addr = info->addr;
174 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
175 BLUETOOTH_ERROR_NONE, &disconn_info,
176 event_info->cb, event_info->user_data);
182 static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
187 int result = BLUETOOTH_ERROR_NONE;
188 rfcomm_info_t *info = data;
189 bt_event_info_t *event_info;
190 bluetooth_rfcomm_received_data_t data_r;
191 GIOStatus status = G_IO_STATUS_NORMAL;
194 retv_if(info == NULL, FALSE);
196 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
198 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
199 BT_ERR_C("RFComm Server disconnected: %d", info->fd);
201 if (info->disconnect_idle_id > 0) {
202 BT_INFO("Disconnect idle still not process remove source");
203 g_source_remove(info->disconnect_idle_id);
204 info->disconnect_idle_id = 0;
207 __rfcomm_server_disconnect(info);
211 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
213 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
215 if (status != G_IO_STATUS_NORMAL) {
216 BT_ERR("IO Channel read is failed with %d", status);
220 BT_ERR("IO Channel read error [%s]", err->message);
221 if (status == G_IO_STATUS_ERROR &&
222 !g_strcmp0(err->message, "Connection reset by peer")) {
223 BT_ERR("cond : %d", cond);
225 if (info->disconnect_idle_id > 0) {
226 BT_INFO("Disconnect idle still not process remove source");
227 g_source_remove(info->disconnect_idle_id);
228 info->disconnect_idle_id = 0;
230 __rfcomm_server_disconnect(info);
239 BT_ERR("Length is zero");
241 if (event_info == NULL) {
246 data_r.socket_fd = info->fd;
247 data_r.buffer_size = len;
248 data_r.buffer = buffer;
250 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
252 event_info->cb, event_info->user_data);
259 int new_server_connection(const char *path, int fd, bluetooth_device_address_t *addr)
262 bt_event_info_t *event_info;
264 BT_DBG("%s %d", path, fd);
266 info = __find_rfcomm_info_with_path(path);
271 memcpy(&info->addr, addr, sizeof(bluetooth_device_address_t));
273 info->data_io = g_io_channel_unix_new(info->fd);
275 g_io_channel_set_encoding(info->data_io, NULL, NULL);
276 g_io_channel_set_flags(info->data_io, G_IO_FLAG_NONBLOCK, NULL);
278 info->data_id = g_io_add_watch(info->data_io,
279 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
280 __data_received_cb, info);
282 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
284 __connected_cb(info, event_info);
290 static rfcomm_info_t *__register_method()
297 id = __rfcomm_assign_id();
301 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
303 object_id = _bt_register_new_conn(path, new_server_connection);
305 __rfcomm_delete_id(id);
308 info = g_new(rfcomm_info_t, 1);
309 info->object_id = (guint)object_id;
314 rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
319 static rfcomm_info_t *__register_method_2(const char *path,const char *bus_name)
324 object_id = _bt_register_new_conn_ex(path, bus_name, new_server_connection);
328 info = g_new(rfcomm_info_t, 1);
329 info->object_id = (guint)object_id;
330 info->path = g_strdup(path);
334 rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
339 void free_rfcomm_info(rfcomm_info_t *info)
341 bt_event_info_t *event_info;
344 if (info->disconnect_idle_id > 0) {
345 BT_INFO("Disconnect idle still not process remove source");
346 g_source_remove(info->disconnect_idle_id);
347 info->disconnect_idle_id = 0;
350 __rfcomm_delete_id(info->id);
351 _bt_unregister_gdbus(info->object_id);
354 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
356 BT_DBG("event type %d", event_info->event_type);
357 __rfcomm_server_disconnect(info);
365 void _bt_rfcomm_server_free_all()
367 BT_DBG("Free all the servers");
369 g_slist_free_full(rfcomm_nodes, (GDestroyNotify)free_rfcomm_info);
374 BT_EXPORT_API int bluetooth_rfcomm_create_socket(const char *uuid)
381 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
384 BT_CHECK_ENABLED(return);
385 BT_CHECK_PARAMETER(uuid, return);
386 BT_INFO("UUID Provided %s", uuid);
388 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CREATE_SOCKET)
389 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
390 BT_ERR("Don't have a privilege to use this API");
391 return BLUETOOTH_ERROR_PERMISSION_DEINED;
395 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
396 info = __register_method();
400 info->uuid = g_strdup(uuid);
401 info->disconnect_idle_id = 0;
406 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
408 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
409 g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
411 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CREATE_SOCKET,
412 in_param1, in_param2, in_param3, in_param4, &out_param);
414 BT_DBG("result: %x", result);
416 if (result == BLUETOOTH_ERROR_NONE) {
417 socket_fd = g_array_index(out_param, int, 0);
419 BT_ERR("Fail to send request");
422 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
428 BT_EXPORT_API int bluetooth_rfcomm_create_socket_ex(const char *uuid, const char *bus_name, const char *path)
433 BT_CHECK_ENABLED(return);
434 BT_CHECK_PARAMETER(path, return);
435 BT_INFO("PATH Provided %s", path);
437 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CREATE_SOCKET_EX)
438 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
439 BT_ERR("Don't have a privilege to use this API");
440 return BLUETOOTH_ERROR_PERMISSION_DEINED;
443 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
444 info = __register_method_2(path, bus_name);
446 return BLUETOOTH_ERROR_IN_PROGRESS;
447 info->uuid = g_strdup(uuid);
448 info->disconnect_idle_id = 0;
450 return BLUETOOTH_ERROR_NONE;
452 return BLUETOOTH_ERROR_NOT_SUPPORT;
457 BT_EXPORT_API int bluetooth_rfcomm_remove_socket(int socket_fd)
465 BT_CHECK_ENABLED(return);
467 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_REMOVE_SOCKET)
468 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
469 BT_ERR("Don't have a privilege to use this API");
470 return BLUETOOTH_ERROR_PERMISSION_DEINED;
474 BT_INFO("<<<<<<<<< RFCOMM Remove socket request from app, fd=[%d] >>>>>>>>>>>", socket_fd);
476 info = __find_rfcomm_info_with_id(socket_fd);
478 return BLUETOOTH_ERROR_INVALID_PARAM;
480 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER,info->uuid);
481 _bt_unregister_profile(info->path);
483 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
484 free_rfcomm_info(info);
486 return BLUETOOTH_ERROR_NONE;
489 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
491 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
493 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REMOVE_SOCKET,
494 in_param1, in_param2, in_param3, in_param4, &out_param);
496 BT_DBG("result: %x", result);
498 if (result == BLUETOOTH_ERROR_NONE) {
499 _bt_remove_server(socket_fd);
502 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
508 BT_EXPORT_API int bluetooth_rfcomm_remove_socket_ex(const char *uuid)
513 BT_CHECK_ENABLED(return);
515 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_REMOVE_SOCKET)
516 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
517 BT_ERR("Don't have a privilege to use this API");
518 return BLUETOOTH_ERROR_PERMISSION_DEINED;
521 BT_INFO("<<<<<<<<< RFCOMM Remove socket request from app, uuid=[%s] >>>>>>>>>>>", uuid);
523 info = __find_rfcomm_info_with_uuid(uuid);
525 return BLUETOOTH_ERROR_INVALID_PARAM;
527 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
528 _bt_unregister_profile(info->path);
530 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
531 free_rfcomm_info(info);
533 return BLUETOOTH_ERROR_NONE;
535 return BLUETOOTH_ERROR_NOT_SUPPORT;
539 BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
546 BT_INFO(">>>>>>>>RFCOMM server disconnect request from APP>>>>>>>>>");
548 info = __find_rfcomm_info_with_fd(socket_fd);
550 return BLUETOOTH_ERROR_INVALID_PARAM;
552 if (info->data_io == NULL)
553 return BLUETOOTH_ERROR_NOT_CONNECTED;
555 g_io_channel_shutdown(info->data_io, TRUE, NULL);
556 g_io_channel_unref(info->data_io);
557 info->data_io = NULL;
559 _bt_convert_addr_type_to_string(address, info->addr.addr);
560 BT_DBG("Address %s", address);
561 _bt_disconnect_profile(address, info->uuid, NULL,NULL);
563 info->disconnect_idle_id = g_idle_add((GSourceFunc)
564 __rfcomm_server_disconnect, info);
567 return BLUETOOTH_ERROR_NONE;
571 BT_CHECK_ENABLED(return);
574 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
576 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
578 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT,
579 in_param1, in_param2, in_param3, in_param4, &out_param);
581 BT_DBG("result: %x", result);
583 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
589 BT_EXPORT_API gboolean bluetooth_rfcomm_is_server_uuid_available(const char *uuid)
592 gboolean available = TRUE;
593 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
595 retv_if(uuid == NULL, FALSE);
596 retv_if(bluetooth_check_adapter() ==
597 BLUETOOTH_ADAPTER_DISABLED, FALSE);
600 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
602 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
603 g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
605 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_IS_UUID_AVAILABLE,
606 in_param1, in_param2, in_param3, in_param4, &out_param);
608 BT_DBG("result: %x", result);
610 if (result == BLUETOOTH_ERROR_NONE) {
611 available = g_array_index(out_param, gboolean, 0);
614 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
616 BT_DBG("available: %d", available);
621 BT_EXPORT_API int bluetooth_rfcomm_server_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
625 char connected_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
626 char input_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
628 BT_CHECK_PARAMETER(device_address, return);
629 BT_CHECK_PARAMETER(connected, return);
631 _bt_convert_addr_type_to_string(input_addr, (unsigned char *)device_address->addr);
635 for (l = rfcomm_nodes; l != NULL; l = l->next) {
640 _bt_convert_addr_type_to_string(connected_addr, info->addr.addr);
642 if (g_strcmp0(connected_addr, input_addr) == 0) {
644 return BLUETOOTH_ERROR_NONE;
648 return BLUETOOTH_ERROR_NONE;
651 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int socket_fd, int max_pending_connection)
657 gboolean native_service = TRUE;
660 BT_CHECK_ENABLED(return);
663 BT_INFO("<<<<<<<<< RFCOMM Listen & accept from app >>>>>>>>>>>");
665 info = __find_rfcomm_info_with_id(socket_fd);
667 return BLUETOOTH_ERROR_INVALID_PARAM;
669 bt_register_profile_info_t profile_info;
672 profile_info.authentication = TRUE;
673 profile_info.authorization = TRUE;
674 profile_info.obj_path = info->path;
675 profile_info.role = NULL;
676 profile_info.service = info->uuid;
677 profile_info.uuid = info->uuid;
679 BT_INFO("uuid %s", profile_info.uuid);
680 result = _bt_register_profile(&profile_info, TRUE);
685 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
687 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
688 g_array_append_vals(in_param2, &max_pending_connection, sizeof(int));
689 g_array_append_vals(in_param3, &native_service, sizeof(gboolean));
691 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
692 in_param1, in_param2, in_param3, in_param4, &out_param);
694 BT_DBG("result: %x", result);
696 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
702 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept_ex(const char *uuid, int max_pending_connection, const char *bus_name, const char *path)
707 BT_CHECK_ENABLED(return);
709 BT_INFO("<<<<<<<<< RFCOMM Listen & accept from app >>>>>>>>>>>");
711 info = __find_rfcomm_info_with_uuid(uuid);
713 return BLUETOOTH_ERROR_INVALID_PARAM;
715 bt_register_profile_info_t profile_info;
718 profile_info.authentication = TRUE;
719 profile_info.authorization = TRUE;
720 profile_info.obj_path = info->path;
721 profile_info.role = NULL;
722 profile_info.service = info->uuid;
723 profile_info.uuid = info->uuid;
725 BT_INFO("uuid %s", profile_info.uuid);
726 result = _bt_register_profile_ex(&profile_info, TRUE, bus_name, path);
730 return BLUETOOTH_ERROR_NOT_SUPPORT;
734 BT_EXPORT_API int bluetooth_rfcomm_listen(int socket_fd, int max_pending_connection)
740 gboolean native_service = FALSE;
743 BT_CHECK_ENABLED(return);
746 BT_INFO("<<<<<<<<< RFCOMM Listen >>>>>>>>>>>");
748 info = __find_rfcomm_info_with_id(socket_fd);
750 return BLUETOOTH_ERROR_INVALID_PARAM;
752 bt_register_profile_info_t profile_info;
755 profile_info.authentication = TRUE;
756 profile_info.authorization = TRUE;
757 profile_info.obj_path = info->path;
758 profile_info.role = NULL;
759 profile_info.service = info->uuid;
760 profile_info.uuid = info->uuid;
761 BT_INFO("UUID %s", info->uuid);
762 BT_INFO("PATH %s", info->path);
763 result = _bt_register_profile_platform(&profile_info, TRUE);
764 if (result != BLUETOOTH_ERROR_NONE)
767 return _bt_register_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid,
768 info->path, socket_fd);
772 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
774 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
775 g_array_append_vals(in_param2, &max_pending_connection, sizeof(int));
776 g_array_append_vals(in_param3, &native_service, sizeof(gboolean));
778 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
779 in_param1, in_param2, in_param3, in_param4, &out_param);
781 BT_DBG("result: %x", result);
783 if (result == BLUETOOTH_ERROR_NONE) {
784 _bt_add_server(socket_fd);
787 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
793 BT_EXPORT_API int bluetooth_rfcomm_accept_connection(int server_fd)
797 BT_CHECK_ENABLED(return);
800 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
802 g_array_append_vals(in_param1, &server_fd, sizeof(int));
804 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_ACCEPT_CONNECTION,
805 in_param1, in_param2, in_param3, in_param4, &out_param);
807 BT_DBG("result: %x", result);
809 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
814 BT_EXPORT_API int bluetooth_rfcomm_reject_connection(int server_fd)
818 BT_CHECK_ENABLED(return);
823 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
825 g_array_append_vals(in_param1, &server_fd, sizeof(int));
827 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REJECT_CONNECTION,
828 in_param1, in_param2, in_param3, in_param4, &out_param);
830 BT_DBG("result: %x", result);
832 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);