2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
21 #include <gio/gunixfdlist.h>
22 #include <sys/socket.h>
25 #include "bluetooth-api.h"
26 #include "bt-internal-types.h"
28 #include "bt-common.h"
29 #include "bt-request-sender.h"
30 #include "bt-event-handler.h"
32 #ifdef TIZEN_FEATURE_BT_DPM
36 #define BLUETOOTH_SOCK_CONNECT_INFO_LEN 16
38 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
40 static GSList *rfcomm_nodes;
43 bluetooth_device_address_t addr;
46 gboolean disconnected;
55 guint disconnect_idle_id;
58 static rfcomm_info_t *__find_rfcomm_info_with_id(int id)
62 for (l = rfcomm_nodes; l != NULL; l = l->next) {
63 rfcomm_info_t *info = l->data;
72 static rfcomm_info_t *__find_rfcomm_info_with_fd(int fd)
77 for (l = rfcomm_nodes; l != NULL; l = l->next) {
78 rfcomm_info_t *info = l->data;
80 for (ll = info->rfcomm_conns; ll; ll = ll->next) {
81 rfcomm_conn_t *conn = ll->data;
83 if (conn && conn->fd == fd)
91 static rfcomm_info_t *__find_rfcomm_info_with_path(const gchar *path)
95 for (l = rfcomm_nodes; l != NULL; l = l->next) {
96 rfcomm_info_t *info = l->data;
98 if (g_strcmp0(info->path, path) == 0)
105 static rfcomm_info_t *__find_rfcomm_info_with_uuid(const char *uuid)
109 for (l = rfcomm_nodes; l != NULL; l = l->next) {
110 rfcomm_info_t *info = l->data;
112 if (g_strcmp0(info->uuid, uuid) == 0)
119 static rfcomm_conn_t *__find_rfcomm_conn_with_fd(rfcomm_info_t *info,
125 for (l = info->rfcomm_conns; l; l = l->next) {
128 if (conn && conn->fd == fd)
135 static void __rfcomm_remove_conn(rfcomm_info_t *info, int fd)
139 conn = __find_rfcomm_conn_with_fd(info, fd);
143 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn);
145 if (conn->watch_id > 0)
146 g_source_remove(conn->watch_id);
150 gboolean _check_uuid_path(char *path, char *uuid)
152 rfcomm_info_t *info = NULL;
153 info = __find_rfcomm_info_with_path(path);
157 if (strcmp(info->uuid, uuid) == 0)
163 static void __connected_cb(rfcomm_info_t *info, rfcomm_conn_t *conn,
164 bt_event_info_t *event_info)
166 bluetooth_rfcomm_connection_t conn_info;
168 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
170 conn_info.device_role = RFCOMM_ROLE_SERVER;
171 g_strlcpy(conn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
172 conn_info.socket_fd = conn->fd;
173 conn_info.device_addr = conn->addr;
174 conn_info.server_id = info->id;
176 BT_INFO_C("### Connected [RFCOMM Server]");
177 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
178 BLUETOOTH_ERROR_NONE, &conn_info,
179 event_info->cb, event_info->user_data);
182 static void __rfcomm_server_disconnect_conn(rfcomm_conn_t *conn,
185 bluetooth_rfcomm_disconnection_t disconn_info;
186 bt_event_info_t *event_info;
191 if (conn->disconnected == FALSE)
194 if (conn->watch_id > 0) {
195 g_source_remove(conn->watch_id);
199 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
200 if (event_info == NULL) {
201 BT_ERR("event_info is NULL");
202 __rfcomm_remove_conn(info, conn->fd);
206 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
207 disconn_info.device_role = RFCOMM_ROLE_SERVER;
208 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
209 disconn_info.device_addr = conn->addr;
211 BT_INFO("Disconnected FD [%d]", conn->fd);
212 disconn_info.socket_fd = conn->fd;
214 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
215 BLUETOOTH_ERROR_NONE, &disconn_info,
216 event_info->cb, event_info->user_data);
218 __rfcomm_remove_conn(info, conn->fd);
221 static gboolean __rfcomm_server_disconnect(rfcomm_info_t *info)
223 BT_INFO_C("### Disconnected [RFCOMM Server]");
225 if (g_slist_find(rfcomm_nodes, info) == NULL) {
226 BT_INFO("rfcomm resource is already freed");
230 info->disconnect_idle_id = 0;
232 g_slist_foreach(info->rfcomm_conns,
233 (GFunc)__rfcomm_server_disconnect_conn, info);
239 static gboolean __is_error_by_disconnect(GError *err)
241 return !g_strcmp0(err->message, "Connection reset by peer") ||
242 !g_strcmp0(err->message, "Connection timed out") ||
243 !g_strcmp0(err->message, "Software caused connection abort");
246 static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
251 int result = BLUETOOTH_ERROR_NONE;
252 rfcomm_info_t *info = data;
254 bt_event_info_t *event_info;
255 bluetooth_rfcomm_received_data_t data_r;
256 GIOStatus status = G_IO_STATUS_NORMAL;
260 retv_if(info == NULL, FALSE);
262 fd = g_io_channel_unix_get_fd(chan);
263 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
264 BT_ERR_C("RFComm Server disconnected: %d", fd);
266 if (info->disconnect_idle_id > 0) {
267 BT_INFO("Disconnect idle still not process remove source");
268 g_source_remove(info->disconnect_idle_id);
269 info->disconnect_idle_id = 0;
272 conn = __find_rfcomm_conn_with_fd(info, fd);
274 BT_ERR("No Connection info found with FD [%d]", fd);
278 if (conn->disconnected == FALSE) {
280 conn->disconnected = TRUE;
282 __rfcomm_server_disconnect(info);
286 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
288 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
290 if (status != G_IO_STATUS_NORMAL) {
291 BT_ERR("IO Channel read is failed with %d", status);
297 BT_ERR("IO Channel read error [%s]", err->message);
298 if (status == G_IO_STATUS_ERROR &&
299 __is_error_by_disconnect(err)) {
300 BT_ERR("cond : %d", cond);
303 if (info->disconnect_idle_id > 0) {
304 BT_INFO("Disconnect idle still not process remove source");
305 g_source_remove(info->disconnect_idle_id);
306 info->disconnect_idle_id = 0;
309 conn = __find_rfcomm_conn_with_fd(info, fd);
311 BT_ERR("No Connection info found with FD [%d]", fd);
315 if (conn->disconnected == FALSE) {
317 conn->disconnected = TRUE;
319 __rfcomm_server_disconnect(info);
327 BT_ERR("Length is zero");
329 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
330 if (event_info == NULL) {
331 BT_ERR("event_info is NULL. Unable to invoke the callback");
336 data_r.socket_fd = fd;
337 data_r.buffer_size = len;
338 data_r.buffer = buffer;
340 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
342 event_info->cb, event_info->user_data);
349 int new_server_connection(const char *path, int fd, bluetooth_device_address_t *addr)
354 bt_event_info_t *event_info;
356 BT_INFO("%s %d", path, fd);
358 info = __find_rfcomm_info_with_path(path);
360 BT_ERR("rfcomm info is NULL");
364 #ifdef TIZEN_FEATURE_BT_DPM
365 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
368 BT_ERR("Not allow to use SPP profile");
371 _bt_convert_addr_type_to_string(addr_str, addr->addr);
372 _bt_disconnect_ext_profile(addr_str, info->path);
378 conn = g_new0(rfcomm_conn_t, 1);
380 memcpy(&conn->addr, addr, sizeof(bluetooth_device_address_t));
381 info->rfcomm_conns = g_slist_append(info->rfcomm_conns, conn);
383 data_io = g_io_channel_unix_new(conn->fd);
385 g_io_channel_set_encoding(data_io, NULL, NULL);
386 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
388 conn->watch_id = g_io_add_watch(data_io,
389 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
390 __data_received_cb, info);
392 g_io_channel_unref(data_io);
394 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
396 __connected_cb(info, conn, event_info);
401 static rfcomm_info_t *__register_method()
408 id = __rfcomm_assign_id();
412 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
414 object_id = _bt_register_new_conn(path, new_server_connection);
416 __rfcomm_delete_id(id);
419 info = g_new0(rfcomm_info_t, 1);
420 info->object_id = (guint)object_id;
424 rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
429 static rfcomm_info_t *__register_method_2(const char *path, const char *bus_name)
434 object_id = _bt_register_new_conn_ex(path, bus_name, new_server_connection);
438 info = g_new0(rfcomm_info_t, 1);
439 info->object_id = (guint)object_id;
440 info->path = g_strdup(path);
443 rfcomm_nodes = g_slist_append(rfcomm_nodes, info);
448 void free_rfcomm_conn(rfcomm_conn_t *conn, rfcomm_info_t *info)
450 if (conn->disconnected == FALSE) {
452 conn->disconnected = TRUE;
454 __rfcomm_server_disconnect_conn(conn, info);
457 void free_rfcomm_info(rfcomm_info_t *info)
461 if (info->disconnect_idle_id > 0) {
462 BT_INFO("Disconnect idle still not process remove source");
463 g_source_remove(info->disconnect_idle_id);
464 info->disconnect_idle_id = 0;
467 __rfcomm_delete_id(info->id);
468 _bt_unregister_gdbus(info->object_id);
470 g_slist_foreach(info->rfcomm_conns, (GFunc)free_rfcomm_conn, info);
477 void _bt_rfcomm_server_free_all(void)
479 BT_DBG("Free all the servers");
481 g_slist_free_full(rfcomm_nodes, (GDestroyNotify)free_rfcomm_info);
485 void _bt_rfcomm_server_disconnect_all(void)
491 BT_INFO("### Disconnect all RFCOMM server connections");
493 for (server = rfcomm_nodes; server; ) {
494 rfcomm_info_t *info = server->data;
496 for (conn = info->rfcomm_conns; conn; conn = conn->next) {
497 rfcomm_conn_t *conn_info = conn->data;
499 if (conn_info == NULL)
502 if (conn_info->watch_id == 0 || conn_info->disconnected)
505 close(conn_info->fd);
506 conn_info->disconnected = TRUE;
508 _bt_convert_addr_type_to_string(addr,
509 conn_info->addr.addr);
510 _bt_disconnect_ext_profile(addr, info->path);
513 server = server->next;
514 __rfcomm_server_disconnect(info);
521 #define BT_RFCOMM_SERVER_ID_MAX 254
524 char addr[BT_ADDRESS_STRING_SIZE];
528 } rfcomm_remote_client_info_t;
531 char uuid[BLUETOOTH_UUID_STRING_MAX];
535 int max_pending_conn;
536 gboolean auto_accept;
537 char pending_addr[BT_ADDRESS_STRING_SIZE];
539 } rfcomm_server_info_t;
541 static GSList *rfcomm_servers;
542 static gboolean id_used[BT_RFCOMM_SERVER_ID_MAX];
545 int __rfcomm_assign_server_id(void)
549 BT_DBG("latest_id: %d", latest_id);
551 index = latest_id + 1;
553 if (index >= BT_RFCOMM_SERVER_ID_MAX)
556 BT_DBG("index: %d", index);
558 while (id_used[index] == TRUE) {
559 if (index == latest_id) {
560 /* No available ID */
561 BT_ERR("All request ID is used");
567 if (index >= BT_RFCOMM_SERVER_ID_MAX)
572 id_used[index] = TRUE;
574 BT_DBG("Assigned Id: %d", latest_id);
579 void __rfcomm_delete_server_id(int id)
581 ret_if(id >= BT_RFCOMM_SERVER_ID_MAX);
586 /* Next server will use this ID */
590 static rfcomm_server_info_t *__get_rfcomm_server_info_with_uuid(const char *uuid)
597 for (l = rfcomm_servers; l != NULL; l = l->next) {
598 rfcomm_server_info_t *info = l->data;
600 if (!strncasecmp(info->uuid, uuid, strlen(info->uuid)))
607 int _get_rfcomm_server_id(char *uuid, gboolean *auto_accept)
609 rfcomm_server_info_t *server_info;
611 server_info = __get_rfcomm_server_info_with_uuid(uuid);
615 *auto_accept = server_info->auto_accept;
616 return server_info->server_id;
619 static rfcomm_server_info_t *__get_rfcomm_server_info_with_id(int server_id)
623 for (l = rfcomm_servers; l != NULL; l = l->next) {
624 rfcomm_server_info_t *info = l->data;
628 BT_DBG("info->server_fd: %d, sock_fd:%d", info->server_id, server_id);
629 if (info->server_id == server_id)
636 void _bt_rfcomm_server_set_pending_conn(int server_id, char *address)
638 rfcomm_server_info_t *server_info;
644 server_info = __get_rfcomm_server_info_with_id(server_id);
648 g_strlcpy(server_info->pending_addr, address, BT_ADDRESS_STRING_SIZE);
651 static rfcomm_remote_client_info_t *__get_rfcomm_rem_client_info_with_fd(int sock_fd)
656 for (l = rfcomm_servers; l != NULL; l = l->next) {
657 rfcomm_server_info_t *info = l->data;
662 for (l1 = info->conn_list; l1 != NULL; l1 = l1->next) {
663 rfcomm_remote_client_info_t *client_info = l1->data;
667 if (client_info->sock_fd == sock_fd)
675 static rfcomm_remote_client_info_t *__get_rfcomm_rem_client_info_with_addr(char *addr)
680 retv_if(NULL == addr, NULL);
682 for (l = rfcomm_servers; l != NULL; l = l->next) {
683 rfcomm_server_info_t *info = l->data;
688 for (l1 = info->conn_list; l1 != NULL; l1 = l1->next) {
689 rfcomm_remote_client_info_t *client_info = l1->data;
693 if (!strncasecmp(client_info->addr, addr, strlen(client_info->addr)))
701 static void __remove_remote_client_info(rfcomm_remote_client_info_t *rem_client)
705 if (rem_client == NULL)
708 if (0 < rem_client->sock_fd) {
709 shutdown(rem_client->sock_fd, SHUT_RDWR);
710 close(rem_client->sock_fd);
713 if (rem_client->watch_id > 0)
714 g_source_remove(rem_client->watch_id);
721 static void __handle_rfcomm_client_disconnected(rfcomm_server_info_t *server_info,
722 rfcomm_remote_client_info_t *rem_client)
724 bluetooth_rfcomm_disconnection_t disconn_info;
725 bt_event_info_t *event_info;
729 if (rem_client == NULL || server_info == NULL)
732 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
733 if (event_info == NULL)
736 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
737 disconn_info.device_role = RFCOMM_ROLE_SERVER;
738 g_strlcpy(disconn_info.uuid, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
739 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr, rem_client->addr);
740 BT_DBG("Disconnected FD [%d]", rem_client->sock_fd);
741 disconn_info.socket_fd = rem_client->sock_fd;
743 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
744 BLUETOOTH_ERROR_NONE, &disconn_info,
745 event_info->cb, event_info->user_data);
748 static void __remove_rfcomm_server(rfcomm_server_info_t *info)
750 rfcomm_remote_client_info_t *client_info;
757 rfcomm_servers = g_slist_remove(rfcomm_servers, info);
758 if (info->conn_list) {
760 client_info = info->conn_list->data;
764 info->conn_list = g_slist_remove(info->conn_list, client_info);
765 __handle_rfcomm_client_disconnected(info, client_info);
766 __remove_remote_client_info(client_info);
767 } while (info->conn_list);
770 if (info->server_fd) {
771 shutdown(info->server_fd, SHUT_RDWR);
772 close(info->server_fd);
776 g_source_remove(info->watch_id);
778 __rfcomm_delete_server_id(info->server_id);
784 static void __connected_cb(rfcomm_remote_client_info_t *client_info, bt_event_info_t *event_info)
786 bluetooth_rfcomm_connection_t conn_info;
787 rfcomm_server_info_t *server_info;
789 server_info = __get_rfcomm_server_info_with_id(client_info->server_id);
790 ret_if(server_info == NULL);
792 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
793 conn_info.device_role = RFCOMM_ROLE_SERVER;
794 g_strlcpy(conn_info.uuid, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
795 conn_info.socket_fd = client_info->sock_fd;
796 _bt_convert_addr_string_to_type(conn_info.device_addr.addr, client_info->addr);
797 conn_info.server_id = server_info->server_id;
799 BT_INFO_C("Connected [RFCOMM Server]");
800 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
801 BLUETOOTH_ERROR_NONE, &conn_info,
802 event_info->cb, event_info->user_data);
805 static int __process_cmsg(struct msghdr *msg)
808 struct cmsghdr *cmsg_ptr = NULL;
810 for (cmsg_ptr = CMSG_FIRSTHDR(msg); cmsg_ptr != NULL;
811 cmsg_ptr = CMSG_NXTHDR(msg, cmsg_ptr)) {
813 if (cmsg_ptr->cmsg_level != SOL_SOCKET)
816 if (cmsg_ptr->cmsg_type == SCM_RIGHTS) {
817 //int *desc = (int *)CMSG_DATA(cmsg_ptr);
819 = ((cmsg_ptr->cmsg_len - CMSG_LEN(0)) / sizeof(int));
822 BT_ERR("ERROR Invalid count of descriptors");
827 memcpy(&sock_fd, CMSG_DATA(cmsg_ptr), sizeof(sock_fd));
828 BT_DBG("Remote client fd: %d", sock_fd);
835 static int __read_incomming_client_connection(
836 int server_fd, char *buf, unsigned int len, int *client_fd)
841 struct cmsghdr cmsgbuf[2 * sizeof(struct cmsghdr) + 4];
844 retv_if(0 > server_fd, -1);
845 retv_if(NULL == client_fd, -1);
847 BT_INFO("server_fd = %d", server_fd);
849 memset(&msg, 0, sizeof(msg));
850 memset(&iv, 0, sizeof(iv));
856 msg.msg_control = cmsgbuf;
857 msg.msg_controllen = sizeof(cmsgbuf);
859 for (retryCount = 0; retryCount < 5; retryCount++) {
860 ret = recvmsg(server_fd, &msg, 0);
861 BT_DBG("recvmsg ret = %d", ret);
862 if (ret < 0 && errno == EINTR)
868 if (ret < 0 && errno == EPIPE) {
869 /* End of stream, server listining stopped */
870 BT_ERR("EOS errno: %d", errno);
875 BT_ERR("Ret errno: %d", errno);
879 if ((msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) != 0) {
880 BT_ERR("MSG Flags errno: %d", errno);
884 BT_INFO("Connection received");
885 *client_fd = __process_cmsg(&msg);
887 BT_ERR("Invalid client_fd received");
892 static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
897 int result = BLUETOOTH_ERROR_NONE;
898 bt_event_info_t *event_info;
899 bluetooth_rfcomm_received_data_t data_r;
900 GIOStatus status = G_IO_STATUS_NORMAL;
902 rfcomm_remote_client_info_t *client_info = data;
903 rfcomm_server_info_t *server_info;
905 retv_if(client_info == NULL, FALSE);
907 server_info = __get_rfcomm_server_info_with_id(client_info->server_id);
909 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
910 BT_ERR_C("RFComm Server disconnected: %d", client_info->sock_fd);
914 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
915 status = g_io_channel_read_chars(chan, buffer,
916 BT_RFCOMM_BUFFER_LEN, &len, &err);
917 if (status != G_IO_STATUS_NORMAL) {
918 BT_ERR("IO Channel read is failed with %d", status);
921 BT_ERR("IO Channel read error [%s]", err->message);
922 if (status == G_IO_STATUS_ERROR &&
923 !g_strcmp0(err->message, "Connection reset by peer")) {
924 BT_ERR("cond : %d", cond);
934 BT_ERR("Length is zero, remote end hang up");
938 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
939 if (event_info == NULL) {
944 data_r.socket_fd = client_info->sock_fd;
945 data_r.buffer_size = len;
946 data_r.buffer = buffer;
948 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
950 event_info->cb, event_info->user_data);
957 BT_ERR("Failure occured, remove client connection");
958 server_info->conn_list = g_slist_remove(
959 server_info->conn_list, client_info);
960 __handle_rfcomm_client_disconnected(server_info, client_info);
961 __remove_remote_client_info(client_info);
965 static gboolean __new_connection_request_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
972 char buf[BLUETOOTH_SOCK_CONNECT_INFO_LEN];
973 unsigned char addr[BT_ADDRESS_LENGTH_MAX];
975 bt_event_info_t *event_info;
977 rfcomm_remote_client_info_t *rem_client;
978 rfcomm_server_info_t *server_info = data;
981 BT_ERR("Server info is invalid");
985 if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
986 BT_INFO("RFCOMM Server with fd:%d is closed with cond:0x%X",
987 server_info->server_fd, cond);
991 BT_INFO("Server fd: %d", server_info->server_fd);
992 len = __read_incomming_client_connection(
993 server_info->server_fd, buf, BLUETOOTH_SOCK_CONNECT_INFO_LEN, &client_fd);
994 BT_DBG("Socket Read len: %d", len);
996 BT_ERR("Listen stopped");
998 } else if (len != BLUETOOTH_SOCK_CONNECT_INFO_LEN) {
999 BT_ERR("Read length is not same as socket info length");
1004 /* Read size of data */
1005 size = buf[len] | (buf[len + 1] << 8);
1008 /* Read bluetooth address */
1009 memcpy(addr, buf + len, BT_ADDRESS_LENGTH_MAX);
1010 len += BT_ADDRESS_LENGTH_MAX;
1013 channel = buf[len] | (buf[len + 1] << 8) |
1014 (buf[len + 2] << 16) | (buf[len + 3] << 24);
1018 status = buf[len] | (buf[len + 1] << 8) |
1019 (buf[len + 2] << 16) | (buf[len + 3] << 24);
1022 BT_DBG("size: %d, channel: %d, status: %d", size, channel, status);
1024 rem_client = g_malloc0(sizeof(rfcomm_remote_client_info_t));
1025 rem_client->sock_fd = client_fd;
1026 rem_client->server_id = server_info->server_id;
1027 _bt_convert_addr_type_to_string(rem_client->addr, addr);
1029 BT_INFO("New client [%s] connection with socket_fd: %d, server_id: %d",
1030 rem_client->addr, rem_client->sock_fd, rem_client->server_id);
1032 io = g_io_channel_unix_new(rem_client->sock_fd);
1033 g_io_channel_set_encoding(io, NULL, NULL);
1034 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
1035 rem_client->watch_id = g_io_add_watch(io,
1036 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1037 __data_received_cb, rem_client);
1038 g_io_channel_unref(io);
1040 server_info->conn_list = g_slist_append(server_info->conn_list, rem_client);
1041 event_info = _bt_event_get_cb_data(BT_RFCOMM_SERVER_EVENT);
1043 __connected_cb(rem_client, event_info);
1048 /* Error occurred, Remove RFCOMM server*/
1049 __remove_rfcomm_server(server_info);
1053 static int __rfcomm_listen(rfcomm_server_info_t *server_info)
1056 GUnixFDList *out_fd_list = NULL;
1057 GIOChannel *server_io;
1059 retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1062 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1064 g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
1065 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
1066 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
1068 BT_DBG("result: %x", result);
1069 if (result != BLUETOOTH_ERROR_NONE) {
1070 BT_ERR("Fail to send request");
1072 } else if (NULL == out_fd_list) {
1073 BT_ERR("out_fd_list is NULL");
1074 return BLUETOOTH_ERROR_INTERNAL;
1080 return BLUETOOTH_ERROR_INTERNAL;
1082 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
1083 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
1084 server_info->server_fd = fd_list_array[0];
1085 BT_INFO("Socket fd: %d", server_info->server_fd);
1087 g_free(fd_list_array);
1088 g_object_unref(out_fd_list);
1091 server_io = g_io_channel_unix_new(server_info->server_fd);
1092 g_io_channel_set_encoding(server_io, NULL, NULL);
1093 g_io_channel_set_flags(server_io, G_IO_FLAG_NONBLOCK, NULL);
1094 server_info->watch_id = g_io_add_watch(server_io,
1095 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1096 __new_connection_request_cb, server_info);
1097 g_io_channel_unref(server_io);
1099 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1101 return BLUETOOTH_ERROR_NONE;
1105 BT_EXPORT_API int bluetooth_rfcomm_create_socket(const char *uuid)
1107 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1108 rfcomm_info_t *info;
1110 rfcomm_server_info_t *server_info;
1113 BT_CHECK_ENABLED(return);
1114 BT_CHECK_PARAMETER(uuid, return);
1115 BT_INFO("UUID Provided %s", uuid);
1117 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CREATE_SOCKET)
1118 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1119 BT_ERR("Don't have a privilege to use this API");
1120 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1123 #ifdef TIZEN_FEATURE_BT_DPM
1124 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1125 BT_ERR("Not allow to use SPP profile");
1126 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1130 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1131 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1132 info = __register_method();
1136 info->uuid = g_strdup(uuid);
1137 info->disconnect_idle_id = 0;
1140 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1142 server_info = __get_rfcomm_server_info_with_uuid(uuid);
1144 server_info = g_malloc0(sizeof(rfcomm_server_info_t));
1145 g_strlcpy(server_info->uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1146 server_info->server_id = __rfcomm_assign_server_id();
1147 server_info->server_fd = -1;
1148 server_info->watch_id = -1;
1149 server_info->auto_accept = FALSE;
1150 rfcomm_servers = g_slist_append(rfcomm_servers, server_info);
1152 return server_info->server_id;
1156 BT_EXPORT_API int bluetooth_rfcomm_create_socket_ex(const char *uuid, const char *bus_name, const char *path)
1158 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1159 rfcomm_info_t *info;
1161 BT_CHECK_ENABLED(return);
1162 BT_CHECK_PARAMETER(path, return);
1163 BT_INFO("PATH Provided %s", path);
1165 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CREATE_SOCKET_EX)
1166 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1167 BT_ERR("Don't have a privilege to use this API");
1168 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1171 #ifdef TIZEN_FEATURE_BT_DPM
1172 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1173 BT_ERR("Not allow to use SPP profile");
1174 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1178 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1179 info = __register_method_2(path, bus_name);
1181 return BLUETOOTH_ERROR_IN_PROGRESS;
1182 info->uuid = g_strdup(uuid);
1183 info->disconnect_idle_id = 0;
1185 return BLUETOOTH_ERROR_NONE;
1187 return BLUETOOTH_ERROR_NOT_SUPPORT;
1192 BT_EXPORT_API int bluetooth_rfcomm_remove_socket(int id)
1194 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1195 rfcomm_info_t *info;
1197 rfcomm_server_info_t *server_info;
1200 BT_CHECK_ENABLED(return);
1202 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_REMOVE_SOCKET)
1203 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1204 BT_ERR("Don't have a privilege to use this API");
1205 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1209 BT_ERR("Invalid ID");
1210 return BLUETOOTH_ERROR_INVALID_PARAM;
1213 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1214 BT_INFO("RFCOMM Remove socket request from app, ID [%d]", id);
1216 info = __find_rfcomm_info_with_id(id);
1218 return BLUETOOTH_ERROR_INVALID_PARAM;
1220 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
1221 _bt_unregister_profile(info->path);
1223 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
1224 free_rfcomm_info(info);
1226 return BLUETOOTH_ERROR_NONE;
1228 BT_INFO("<<<<<<<<< RFCOMM Remove socket request from app, fd=[%d] >>>>>>>>>>>", socket_fd);
1230 server_info = __get_rfcomm_server_info_with_id(id);
1232 BT_ERR("server_info not found for socket_fd: %d", id);
1233 return BLUETOOTH_ERROR_INVALID_PARAM;
1236 __remove_rfcomm_server(server_info);
1238 return BLUETOOTH_ERROR_NONE;
1242 BT_EXPORT_API int bluetooth_rfcomm_remove_socket_ex(const char *uuid)
1244 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1245 rfcomm_info_t *info;
1247 BT_CHECK_ENABLED(return);
1249 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_REMOVE_SOCKET)
1250 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1251 BT_ERR("Don't have a privilege to use this API");
1252 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1255 BT_INFO("RFCOMM Remove socket request from app, uuid=[%s]", uuid);
1257 info = __find_rfcomm_info_with_uuid(uuid);
1259 return BLUETOOTH_ERROR_INVALID_PARAM;
1261 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
1262 _bt_unregister_profile(info->path);
1264 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
1265 free_rfcomm_info(info);
1267 return BLUETOOTH_ERROR_NONE;
1269 return BLUETOOTH_ERROR_NOT_SUPPORT;
1273 BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
1275 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1276 rfcomm_info_t *info;
1277 rfcomm_conn_t *conn;
1281 BT_INFO("### Disconnect RFCOMM server");
1282 if (socket_fd < 0) {
1283 BT_ERR("Invalid FD");
1284 return BLUETOOTH_ERROR_INVALID_PARAM;
1287 info = __find_rfcomm_info_with_fd(socket_fd);
1289 return BLUETOOTH_ERROR_INVALID_PARAM;
1291 conn = __find_rfcomm_conn_with_fd(info, socket_fd);
1293 return BLUETOOTH_ERROR_INVALID_PARAM;
1295 if (conn->watch_id == 0 || conn->disconnected)
1296 return BLUETOOTH_ERROR_NOT_CONNECTED;
1299 conn->disconnected = TRUE;
1301 _bt_convert_addr_type_to_string(address, conn->addr.addr);
1303 BT_DBG("Address %s", address);
1304 _bt_disconnect_ext_profile(address, info->path);
1306 if (info->disconnect_idle_id == 0)
1307 info->disconnect_idle_id = g_idle_add(
1308 (GSourceFunc)__rfcomm_server_disconnect, info);
1311 return BLUETOOTH_ERROR_NONE;
1313 rfcomm_remote_client_info_t *client_info;
1315 BT_CHECK_ENABLED(return);
1317 BT_INFO(">>>>>>>>RFCOMM server disconnect request from APP>>>>>>>>>");
1318 if (socket_fd < 0) {
1319 BT_ERR("Invalid FD");
1320 return BLUETOOTH_ERROR_INVALID_PARAM;
1323 client_info = __get_rfcomm_rem_client_info_with_fd(socket_fd);
1325 BT_ERR("client_info not found for socket_fd: %d", socket_fd);
1326 return BLUETOOTH_ERROR_NOT_CONNECTED;
1329 if (client_info->sock_fd) {
1330 shutdown(client_info->sock_fd, SHUT_RDWR);
1331 close(client_info->sock_fd);
1332 client_info->sock_fd = -1;
1335 return BLUETOOTH_ERROR_NONE;
1339 BT_EXPORT_API gboolean bluetooth_rfcomm_is_server_uuid_available(const char *uuid)
1342 gboolean available = TRUE;
1343 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
1345 retv_if(uuid == NULL, FALSE);
1346 retv_if(bluetooth_check_adapter() ==
1347 BLUETOOTH_ADAPTER_DISABLED, FALSE);
1350 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1352 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1353 g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
1355 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_IS_UUID_AVAILABLE,
1356 in_param1, in_param2, in_param3, in_param4, &out_param);
1358 BT_DBG("result: %x", result);
1360 if (result == BLUETOOTH_ERROR_NONE)
1361 available = g_array_index(out_param, gboolean, 0);
1363 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1365 BT_DBG("available: %d", available);
1370 BT_EXPORT_API int bluetooth_rfcomm_server_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1372 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1375 rfcomm_info_t *info;
1376 rfcomm_conn_t *conn;
1378 char input_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1379 rfcomm_remote_client_info_t *info;
1382 BT_CHECK_PARAMETER(device_address, return);
1383 BT_CHECK_PARAMETER(connected, return);
1387 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1388 for (l = rfcomm_nodes; l; l = l->next) {
1391 if (info == NULL || info->rfcomm_conns == NULL)
1394 for (ll = info->rfcomm_conns; ll; ll = ll->next) {
1397 if (memcmp(device_address, &conn->addr,
1398 sizeof(bluetooth_device_address_t)))
1402 return BLUETOOTH_ERROR_NONE;
1406 return BLUETOOTH_ERROR_NONE;
1408 _bt_convert_addr_type_to_string(input_addr, (unsigned char *)device_address->addr);
1409 info = __get_rfcomm_rem_client_info_with_addr(input_addr);
1413 return BLUETOOTH_ERROR_NONE;
1417 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int id, int max_pending_connection)
1419 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1420 rfcomm_info_t *info;
1422 rfcomm_server_info_t *server_info;
1425 BT_CHECK_ENABLED(return);
1427 BT_ERR("Invalid ID");
1428 return BLUETOOTH_ERROR_INVALID_PARAM;
1431 #ifdef TIZEN_FEATURE_BT_DPM
1432 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1433 BT_ERR("Not allow to use SPP profile");
1434 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1438 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1439 BT_INFO("RFCOMM Listen & accept from app");
1441 info = __find_rfcomm_info_with_id(id);
1443 return BLUETOOTH_ERROR_INVALID_PARAM;
1445 bt_register_profile_info_t profile_info;
1448 profile_info.authentication = TRUE;
1449 profile_info.authorization = TRUE;
1450 profile_info.obj_path = info->path;
1451 profile_info.role = NULL;
1452 profile_info.service = info->uuid;
1453 profile_info.uuid = info->uuid;
1455 BT_INFO("uuid %s", profile_info.uuid);
1456 result = _bt_register_profile(&profile_info, TRUE);
1460 BT_INFO("<<<<<<<<< RFCOMM Listen & accept from app >>>>>>>>>>>");
1462 server_info = __get_rfcomm_server_info_with_id(id);
1464 BT_ERR("server_info not found for id: %d", id);
1465 return BLUETOOTH_ERROR_INVALID_PARAM;
1468 if (server_info->server_fd >= 0) {
1469 BT_ERR("server already listining");
1470 return BLUETOOTH_ERROR_DEVICE_BUSY;
1473 server_info->max_pending_conn = max_pending_connection;
1474 server_info->auto_accept = TRUE;
1476 return __rfcomm_listen(server_info);
1480 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept_ex(const char *uuid,
1481 int max_pending_connection,
1482 const char *bus_name, const char *path)
1484 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1485 rfcomm_info_t *info;
1487 BT_CHECK_ENABLED(return);
1489 #ifdef TIZEN_FEATURE_BT_DPM
1490 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1491 BT_ERR("Not allow to use SPP profile");
1492 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1496 BT_INFO("RFCOMM Listen & accept from app");
1498 info = __find_rfcomm_info_with_uuid(uuid);
1500 return BLUETOOTH_ERROR_INVALID_PARAM;
1502 bt_register_profile_info_t profile_info;
1505 profile_info.authentication = TRUE;
1506 profile_info.authorization = TRUE;
1507 profile_info.obj_path = info->path;
1508 profile_info.role = NULL;
1509 profile_info.service = info->uuid;
1510 profile_info.uuid = info->uuid;
1512 BT_INFO("uuid %s", profile_info.uuid);
1513 result = _bt_register_profile_ex(&profile_info, TRUE, bus_name, path);
1517 return BLUETOOTH_ERROR_NOT_SUPPORT;
1521 BT_EXPORT_API int bluetooth_rfcomm_listen(int id, int max_pending_connection)
1523 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1524 rfcomm_info_t *info;
1526 rfcomm_server_info_t *server_info;
1529 BT_CHECK_ENABLED(return);
1531 BT_ERR("Invalid ID");
1532 return BLUETOOTH_ERROR_INVALID_PARAM;
1535 #ifdef TIZEN_FEATURE_BT_DPM
1536 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1537 BT_ERR("Not allow to use SPP profile");
1538 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1542 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1543 BT_INFO("RFCOMM Listen");
1545 info = __find_rfcomm_info_with_id(id);
1547 return BLUETOOTH_ERROR_INVALID_PARAM;
1549 bt_register_profile_info_t profile_info;
1552 profile_info.authentication = TRUE;
1553 profile_info.authorization = TRUE;
1554 profile_info.obj_path = info->path;
1555 profile_info.role = NULL;
1556 profile_info.service = info->uuid;
1557 profile_info.uuid = info->uuid;
1558 BT_INFO("UUID %s", info->uuid);
1559 BT_INFO("PATH %s", info->path);
1560 result = _bt_register_profile_platform(&profile_info, TRUE);
1561 if (result != BLUETOOTH_ERROR_NONE)
1564 return _bt_register_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid,
1568 BT_INFO("<<<<<<<<< RFCOMM Listen >>>>>>>>>>>");
1570 server_info = __get_rfcomm_server_info_with_id(id);
1572 BT_ERR("server_info not found for id: %d", id);
1573 return BLUETOOTH_ERROR_INVALID_PARAM;
1576 if (server_info->server_fd >= 0) {
1577 BT_ERR("server already listining");
1578 return BLUETOOTH_ERROR_DEVICE_BUSY;
1581 server_info->max_pending_conn = max_pending_connection;
1582 server_info->auto_accept = FALSE;
1583 return __rfcomm_listen(server_info);
1587 BT_EXPORT_API int bluetooth_rfcomm_accept_connection(int server_fd)
1590 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1592 rfcomm_server_info_t *server_info;
1595 BT_CHECK_ENABLED(return);
1597 #ifdef TIZEN_FEATURE_BT_DPM
1598 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1599 BT_ERR("Not allow to use SPP profile");
1600 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1604 if (server_fd < 0) {
1605 BT_ERR("Invalid FD");
1606 return BLUETOOTH_ERROR_INVALID_PARAM;
1610 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1612 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1613 g_array_append_vals(in_param1, &server_fd, sizeof(int));
1615 server_info = __get_rfcomm_server_info_with_id(server_fd);
1617 BT_ERR("No server with fd: %d", server_fd);
1618 return BLUETOOTH_ERROR_INVALID_PARAM;
1621 g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
1624 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_ACCEPT_CONNECTION,
1625 in_param1, in_param2, in_param3, in_param4, &out_param);
1627 BT_DBG("result: %x", result);
1629 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1634 BT_EXPORT_API int bluetooth_rfcomm_reject_connection(int server_fd)
1637 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1639 rfcomm_server_info_t *server_info;
1642 BT_CHECK_ENABLED(return);
1644 if (server_fd < 0) {
1645 BT_ERR("Invalid FD");
1646 return BLUETOOTH_ERROR_INVALID_PARAM;
1652 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1654 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1655 g_array_append_vals(in_param1, &server_fd, sizeof(int));
1657 server_info = __get_rfcomm_server_info_with_id(server_fd);
1659 BT_ERR("No server with fd: %d", server_fd);
1660 return BLUETOOTH_ERROR_INVALID_PARAM;
1663 g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
1665 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REJECT_CONNECTION,
1666 in_param1, in_param2, in_param3, in_param4, &out_param);
1668 BT_DBG("result: %x", result);
1670 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);