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, bool accept)
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 if (accept == false) {
1065 g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
1066 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
1067 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
1069 g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
1070 result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN_AND_ACCEPT,
1071 in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
1074 BT_DBG("result: %x", result);
1075 if (result != BLUETOOTH_ERROR_NONE) {
1076 BT_ERR("Fail to send request");
1078 } else if (NULL == out_fd_list) {
1079 BT_ERR("out_fd_list is NULL");
1080 return BLUETOOTH_ERROR_INTERNAL;
1086 return BLUETOOTH_ERROR_INTERNAL;
1088 fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
1089 BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
1090 server_info->server_fd = fd_list_array[0];
1091 BT_INFO("Socket fd: %d", server_info->server_fd);
1093 g_free(fd_list_array);
1094 g_object_unref(out_fd_list);
1097 server_io = g_io_channel_unix_new(server_info->server_fd);
1098 g_io_channel_set_encoding(server_io, NULL, NULL);
1099 g_io_channel_set_flags(server_io, G_IO_FLAG_NONBLOCK, NULL);
1100 server_info->watch_id = g_io_add_watch(server_io,
1101 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
1102 __new_connection_request_cb, server_info);
1103 g_io_channel_unref(server_io);
1105 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1107 return BLUETOOTH_ERROR_NONE;
1111 BT_EXPORT_API int bluetooth_rfcomm_create_socket(const char *uuid)
1113 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1114 rfcomm_info_t *info;
1116 rfcomm_server_info_t *server_info;
1119 BT_CHECK_ENABLED(return);
1120 BT_CHECK_PARAMETER(uuid, return);
1121 BT_INFO("UUID Provided %s", uuid);
1123 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CREATE_SOCKET)
1124 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1125 BT_ERR("Don't have a privilege to use this API");
1126 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1129 #ifdef TIZEN_FEATURE_BT_DPM
1130 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1131 BT_ERR("Not allow to use SPP profile");
1132 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1136 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1137 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1138 info = __register_method();
1142 info->uuid = g_strdup(uuid);
1143 info->disconnect_idle_id = 0;
1146 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1148 server_info = __get_rfcomm_server_info_with_uuid(uuid);
1150 server_info = g_malloc0(sizeof(rfcomm_server_info_t));
1151 g_strlcpy(server_info->uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
1152 server_info->server_id = __rfcomm_assign_server_id();
1153 server_info->server_fd = -1;
1154 server_info->watch_id = -1;
1155 server_info->auto_accept = FALSE;
1156 rfcomm_servers = g_slist_append(rfcomm_servers, server_info);
1158 return server_info->server_id;
1162 BT_EXPORT_API int bluetooth_rfcomm_create_socket_ex(const char *uuid, const char *bus_name, const char *path)
1164 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1165 rfcomm_info_t *info;
1167 BT_CHECK_ENABLED(return);
1168 BT_CHECK_PARAMETER(path, return);
1169 BT_INFO("PATH Provided %s", path);
1171 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CREATE_SOCKET_EX)
1172 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1173 BT_ERR("Don't have a privilege to use this API");
1174 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1177 #ifdef TIZEN_FEATURE_BT_DPM
1178 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1179 BT_ERR("Not allow to use SPP profile");
1180 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1184 BT_INFO("<<<<<<<<< RFCOMM Create socket from app >>>>>>>>>");
1185 info = __register_method_2(path, bus_name);
1187 return BLUETOOTH_ERROR_IN_PROGRESS;
1188 info->uuid = g_strdup(uuid);
1189 info->disconnect_idle_id = 0;
1191 return BLUETOOTH_ERROR_NONE;
1193 return BLUETOOTH_ERROR_NOT_SUPPORT;
1198 BT_EXPORT_API int bluetooth_rfcomm_remove_socket(int id)
1200 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1201 rfcomm_info_t *info;
1203 rfcomm_server_info_t *server_info;
1206 BT_CHECK_ENABLED(return);
1208 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_REMOVE_SOCKET)
1209 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1210 BT_ERR("Don't have a privilege to use this API");
1211 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1215 BT_ERR("Invalid ID");
1216 return BLUETOOTH_ERROR_INVALID_PARAM;
1219 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1220 BT_INFO("RFCOMM Remove socket request from app, ID [%d]", id);
1222 info = __find_rfcomm_info_with_id(id);
1224 return BLUETOOTH_ERROR_INVALID_PARAM;
1226 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
1227 _bt_unregister_profile(info->path);
1229 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
1230 free_rfcomm_info(info);
1232 return BLUETOOTH_ERROR_NONE;
1234 BT_INFO("<<<<<<<<< RFCOMM Remove socket request from app, fd=[%d] >>>>>>>>>>>", socket_fd);
1236 server_info = __get_rfcomm_server_info_with_id(id);
1238 BT_ERR("server_info not found for socket_fd: %d", id);
1239 return BLUETOOTH_ERROR_INVALID_PARAM;
1242 __remove_rfcomm_server(server_info);
1244 return BLUETOOTH_ERROR_NONE;
1248 BT_EXPORT_API int bluetooth_rfcomm_remove_socket_ex(const char *uuid)
1250 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1251 rfcomm_info_t *info;
1253 BT_CHECK_ENABLED(return);
1255 if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_REMOVE_SOCKET)
1256 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1257 BT_ERR("Don't have a privilege to use this API");
1258 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1261 BT_INFO("RFCOMM Remove socket request from app, uuid=[%s]", uuid);
1263 info = __find_rfcomm_info_with_uuid(uuid);
1265 return BLUETOOTH_ERROR_INVALID_PARAM;
1267 _bt_unregister_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid);
1268 _bt_unregister_profile(info->path);
1270 rfcomm_nodes = g_slist_remove(rfcomm_nodes, info);
1271 free_rfcomm_info(info);
1273 return BLUETOOTH_ERROR_NONE;
1275 return BLUETOOTH_ERROR_NOT_SUPPORT;
1279 BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
1281 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1282 rfcomm_info_t *info;
1283 rfcomm_conn_t *conn;
1287 BT_INFO(" ### Disconnect RFCOMM server");
1288 if (socket_fd < 0) {
1289 BT_ERR("Invalid FD");
1290 return BLUETOOTH_ERROR_INVALID_PARAM;
1293 info = __find_rfcomm_info_with_fd(socket_fd);
1295 return BLUETOOTH_ERROR_INVALID_PARAM;
1297 conn = __find_rfcomm_conn_with_fd(info, socket_fd);
1299 return BLUETOOTH_ERROR_INVALID_PARAM;
1301 if (conn->watch_id == 0 || conn->disconnected)
1302 return BLUETOOTH_ERROR_NOT_CONNECTED;
1305 conn->disconnected = TRUE;
1307 _bt_convert_addr_type_to_string(address, conn->addr.addr);
1309 BT_DBG("Address %s", address);
1310 _bt_disconnect_ext_profile(address, info->path);
1312 if (info->disconnect_idle_id == 0)
1313 info->disconnect_idle_id = g_idle_add(
1314 (GSourceFunc)__rfcomm_server_disconnect, info);
1317 return BLUETOOTH_ERROR_NONE;
1319 rfcomm_remote_client_info_t *client_info;
1321 BT_CHECK_ENABLED(return);
1323 BT_INFO(">>>>>>>>RFCOMM server disconnect request from APP>>>>>>>>>");
1324 if (socket_fd < 0) {
1325 BT_ERR("Invalid FD");
1326 return BLUETOOTH_ERROR_INVALID_PARAM;
1329 client_info = __get_rfcomm_rem_client_info_with_fd(socket_fd);
1331 BT_ERR("client_info not found for socket_fd: %d", socket_fd);
1332 return BLUETOOTH_ERROR_NOT_CONNECTED;
1335 if (client_info->sock_fd) {
1336 shutdown(client_info->sock_fd, SHUT_RDWR);
1337 close(client_info->sock_fd);
1338 client_info->sock_fd = -1;
1341 return BLUETOOTH_ERROR_NONE;
1345 BT_EXPORT_API gboolean bluetooth_rfcomm_is_server_uuid_available(const char *uuid)
1348 gboolean available = TRUE;
1349 char uuid_str[BLUETOOTH_UUID_STRING_MAX];
1351 retv_if(uuid == NULL, FALSE);
1352 retv_if(bluetooth_check_adapter() ==
1353 BLUETOOTH_ADAPTER_DISABLED, FALSE);
1356 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1358 g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1359 g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
1361 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_IS_UUID_AVAILABLE,
1362 in_param1, in_param2, in_param3, in_param4, &out_param);
1364 BT_DBG("result: %x", result);
1366 if (result == BLUETOOTH_ERROR_NONE)
1367 available = g_array_index(out_param, gboolean, 0);
1369 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1371 BT_DBG("available: %d", available);
1376 BT_EXPORT_API int bluetooth_rfcomm_server_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
1378 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1381 rfcomm_info_t *info;
1382 rfcomm_conn_t *conn;
1384 char input_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1385 rfcomm_remote_client_info_t *info;
1388 BT_CHECK_PARAMETER(device_address, return);
1389 BT_CHECK_PARAMETER(connected, return);
1393 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1394 for (l = rfcomm_nodes; l; l = l->next) {
1397 if (info == NULL || info->rfcomm_conns == NULL)
1400 for (ll = info->rfcomm_conns; ll; ll = ll->next) {
1403 if (memcmp(device_address, &conn->addr,
1404 sizeof(bluetooth_device_address_t)))
1408 return BLUETOOTH_ERROR_NONE;
1412 return BLUETOOTH_ERROR_NONE;
1414 _bt_convert_addr_type_to_string(input_addr, (unsigned char *)device_address->addr);
1415 info = __get_rfcomm_rem_client_info_with_addr(input_addr);
1419 return BLUETOOTH_ERROR_NONE;
1423 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int id, int max_pending_connection)
1425 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1426 rfcomm_info_t *info;
1428 rfcomm_server_info_t *server_info;
1431 BT_CHECK_ENABLED(return);
1433 BT_ERR("Invalid ID");
1434 return BLUETOOTH_ERROR_INVALID_PARAM;
1437 #ifdef TIZEN_FEATURE_BT_DPM
1438 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1439 BT_ERR("Not allow to use SPP profile");
1440 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1444 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1445 BT_INFO("RFCOMM Listen & accept from app");
1447 info = __find_rfcomm_info_with_id(id);
1449 return BLUETOOTH_ERROR_INVALID_PARAM;
1451 bt_register_profile_info_t profile_info;
1454 profile_info.authentication = TRUE;
1455 profile_info.authorization = TRUE;
1456 profile_info.obj_path = info->path;
1457 profile_info.role = NULL;
1458 profile_info.service = info->uuid;
1459 profile_info.uuid = info->uuid;
1461 BT_INFO("uuid %s", profile_info.uuid);
1462 result = _bt_register_profile(&profile_info, TRUE);
1466 BT_INFO("<<<<<<<<< RFCOMM Listen & accept from app >>>>>>>>>>>");
1468 server_info = __get_rfcomm_server_info_with_id(id);
1470 BT_ERR("server_info not found for id: %d", id);
1471 return BLUETOOTH_ERROR_INVALID_PARAM;
1474 if (server_info->server_fd >= 0) {
1475 BT_ERR("server already listining");
1476 return BLUETOOTH_ERROR_DEVICE_BUSY;
1479 server_info->max_pending_conn = max_pending_connection;
1480 server_info->auto_accept = TRUE;
1482 return __rfcomm_listen(server_info, true);
1486 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept_ex(const char *uuid,
1487 int max_pending_connection,
1488 const char *bus_name, const char *path)
1490 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1491 rfcomm_info_t *info;
1493 BT_CHECK_ENABLED(return);
1495 #ifdef TIZEN_FEATURE_BT_DPM
1496 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1497 BT_ERR("Not allow to use SPP profile");
1498 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1502 BT_INFO("RFCOMM Listen & accept from app");
1504 info = __find_rfcomm_info_with_uuid(uuid);
1506 return BLUETOOTH_ERROR_INVALID_PARAM;
1508 bt_register_profile_info_t profile_info;
1511 profile_info.authentication = TRUE;
1512 profile_info.authorization = TRUE;
1513 profile_info.obj_path = info->path;
1514 profile_info.role = NULL;
1515 profile_info.service = info->uuid;
1516 profile_info.uuid = info->uuid;
1518 BT_INFO("uuid %s", profile_info.uuid);
1519 result = _bt_register_profile_ex(&profile_info, TRUE, bus_name, path);
1523 return BLUETOOTH_ERROR_NOT_SUPPORT;
1527 BT_EXPORT_API int bluetooth_rfcomm_listen(int id, int max_pending_connection)
1529 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1530 rfcomm_info_t *info;
1532 rfcomm_server_info_t *server_info;
1535 BT_CHECK_ENABLED(return);
1537 BT_ERR("Invalid ID");
1538 return BLUETOOTH_ERROR_INVALID_PARAM;
1541 #ifdef TIZEN_FEATURE_BT_DPM
1542 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1543 BT_ERR("Not allow to use SPP profile");
1544 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1548 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1549 BT_INFO("RFCOMM Listen");
1551 info = __find_rfcomm_info_with_id(id);
1553 return BLUETOOTH_ERROR_INVALID_PARAM;
1555 bt_register_profile_info_t profile_info;
1558 profile_info.authentication = TRUE;
1559 profile_info.authorization = TRUE;
1560 profile_info.obj_path = info->path;
1561 profile_info.role = NULL;
1562 profile_info.service = info->uuid;
1563 profile_info.uuid = info->uuid;
1564 BT_INFO("UUID %s", info->uuid);
1565 BT_INFO("PATH %s", info->path);
1566 result = _bt_register_profile_platform(&profile_info, TRUE);
1567 if (result != BLUETOOTH_ERROR_NONE)
1570 return _bt_register_osp_server_in_agent(BT_RFCOMM_SERVER, info->uuid,
1574 BT_INFO("<<<<<<<<< RFCOMM Listen >>>>>>>>>>>");
1576 server_info = __get_rfcomm_server_info_with_id(id);
1578 BT_ERR("server_info not found for id: %d", id);
1579 return BLUETOOTH_ERROR_INVALID_PARAM;
1582 if (server_info->server_fd >= 0) {
1583 BT_ERR("server already listining");
1584 return BLUETOOTH_ERROR_DEVICE_BUSY;
1587 server_info->max_pending_conn = max_pending_connection;
1588 server_info->auto_accept = FALSE;
1589 return __rfcomm_listen(server_info, false);
1593 BT_EXPORT_API int bluetooth_rfcomm_accept_connection(int server_fd)
1596 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1598 rfcomm_server_info_t *server_info;
1601 BT_CHECK_ENABLED(return);
1603 #ifdef TIZEN_FEATURE_BT_DPM
1604 if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED) {
1605 BT_ERR("Not allow to use SPP profile");
1606 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1610 if (server_fd < 0) {
1611 BT_ERR("Invalid FD");
1612 return BLUETOOTH_ERROR_INVALID_PARAM;
1616 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1618 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1619 g_array_append_vals(in_param1, &server_fd, sizeof(int));
1621 server_info = __get_rfcomm_server_info_with_id(server_fd);
1623 BT_ERR("No server with fd: %d", server_fd);
1624 return BLUETOOTH_ERROR_INVALID_PARAM;
1627 g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
1630 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_ACCEPT_CONNECTION,
1631 in_param1, in_param2, in_param3, in_param4, &out_param);
1633 BT_DBG("result: %x", result);
1635 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1640 BT_EXPORT_API int bluetooth_rfcomm_reject_connection(int server_fd)
1643 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1645 rfcomm_server_info_t *server_info;
1648 BT_CHECK_ENABLED(return);
1650 if (server_fd < 0) {
1651 BT_ERR("Invalid FD");
1652 return BLUETOOTH_ERROR_INVALID_PARAM;
1658 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1660 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
1661 g_array_append_vals(in_param1, &server_fd, sizeof(int));
1663 server_info = __get_rfcomm_server_info_with_id(server_fd);
1665 BT_ERR("No server with fd: %d", server_fd);
1666 return BLUETOOTH_ERROR_INVALID_PARAM;
1669 g_array_append_vals(in_param1, server_info->pending_addr, BT_ADDRESS_STRING_SIZE);
1671 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REJECT_CONNECTION,
1672 in_param1, in_param2, in_param3, in_param4, &out_param);
1674 BT_DBG("result: %x", result);
1676 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);