4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Atul Kumar Rai <a.rai@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <sys/types.h>
27 #include <sys/socket.h>
34 #include "bt-hal-rfcomm-dbus-handler.h"
35 #include "bt-hal-dbus-common-utils.h"
37 #define BT_HAL_RFCOMM_ID_MAX 245
38 #define BT_HAL_RFCOMM_MAX_BUFFER_SIZE 1024
41 char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
43 unsigned int hal_watch;
45 unsigned int bt_watch;
49 char uuid[BT_HAL_UUID_STRING_LEN];
54 rfcomm_conn_info_t *conn_info;
58 char uuid[BT_HAL_UUID_STRING_LEN];
64 unsigned int server_watch;
66 } rfcomm_server_data_t;
68 static GSList *rfcomm_clients;
69 static GSList *rfcomm_servers;
70 static int latest_id = -1;
71 static gboolean id_used[BT_HAL_RFCOMM_ID_MAX];
73 int __rfcomm_assign_id(void)
77 DBG("latest_id: %d", latest_id);
79 index = latest_id + 1;
80 if (index >= BT_HAL_RFCOMM_ID_MAX)
83 DBG("index: %d", index);
85 while (id_used[index] == TRUE) {
86 if (index == latest_id) {
88 ERR("All request ID is used");
93 if (index >= BT_HAL_RFCOMM_ID_MAX)
98 id_used[index] = TRUE;
99 DBG("Assigned Id: %d", latest_id);
104 void __rfcomm_delete_id(int id)
106 if (id >= BT_HAL_RFCOMM_ID_MAX || id < 0) {
107 ERR("Invalid id %d", id);
113 DBG("id: %d, latest_id: %d", id, latest_id);
116 /*************************** RFCOMM Client Implementation ***************************/
117 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
121 for (l = rfcomm_clients; l != NULL; l = l->next) {
122 rfcomm_cb_data_t *info = l->data;
125 if (g_strcmp0(info->obj_path, path) == 0)
132 static void __bt_free_conn(rfcomm_conn_info_t *conn)
139 if (0 < conn->hal_fd)
142 if (conn->hal_watch > 0) {
143 g_source_remove(conn->hal_watch);
147 if (0 < conn->stack_fd)
148 close(conn->stack_fd);
150 if (conn->bt_watch > 0) {
151 g_source_remove(conn->bt_watch);
159 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
163 if (cb_data->id >= 0)
164 __rfcomm_delete_id(cb_data->id);
166 if (cb_data->object_id > 0)
167 _bt_hal_unregister_gdbus_object(cb_data->object_id);
169 if (cb_data->obj_path) {
170 INFO("Unregister profile");
171 _bt_hal_unregister_profile(cb_data->obj_path);
174 g_free(cb_data->obj_path);
176 g_free(cb_data->device_path);
182 static void __rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
189 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
190 __bt_free_conn(info->conn_info);
191 __bt_free_cb_data(info);
194 static int write_all(int fd, unsigned char *buf, int len)
201 written = write(fd, buf, len);
203 if (errno == EINTR || errno == EAGAIN)
211 len -= written; buf += written; sent += written;
217 static gboolean app_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
221 rfcomm_cb_data_t *info = data;
222 rfcomm_conn_info_t *conn_info;
223 unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
226 char err_msg[256] = {0, };
229 fd = g_io_channel_unix_get_fd(io);
230 conn_info = info->conn_info;
232 if (cond & G_IO_HUP) {
233 ERR("Socket %d hang up", fd);
237 if (cond & (G_IO_ERR | G_IO_NVAL)) {
238 ERR("Socket %d error", fd);
243 ERR("conn_info is NULL");
247 /* Read data from application */
248 if (g_io_channel_read_chars(io, (gchar *)buff,
249 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
250 &len, &err) == G_IO_STATUS_ERROR) {
252 ERR("IO Channel read error: %s", err->message);
254 ERR("IO Channel read error client");
260 ERR("Other end of socket is closed");
264 /* Send data to remote device */
265 sent = write_all(conn_info->stack_fd, buff, len);
267 strerror_r(errno, err_msg, sizeof(err_msg));
268 ERR("write(): %s", err_msg);
275 __rfcomm_cb_data_remove(info);
279 static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
283 rfcomm_cb_data_t *info = data;
284 rfcomm_conn_info_t *conn_info;
285 unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
288 char err_msg[256] = {0, };
292 fd = g_io_channel_unix_get_fd(io);
293 conn_info = info->conn_info;
295 if (cond & G_IO_HUP) {
296 ERR("Socket %d hang up", fd);
300 if (cond & (G_IO_ERR | G_IO_NVAL)) {
301 ERR("Socket %d error", fd);
306 ERR("conn_info is NULL");
310 /* Read data from remote device */
311 if (g_io_channel_read_chars(io, (gchar *)buff,
312 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
313 &len, &err) == G_IO_STATUS_ERROR) {
315 ERR("IO Channel read error: %s", err->message);
317 ERR("IO Channel read error client");
323 ERR("Other end of socket is closed");
327 /* Send data to application */
328 sent = write_all(conn_info->hal_fd, buff, len);
330 strerror_r(errno, err_msg, sizeof(err_msg));
331 ERR("write(): %s", err_msg);
338 __rfcomm_cb_data_remove(info);
342 static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
344 char address[BT_HAL_ADDRESS_STRING_SIZE];
345 rfcomm_cb_data_t *info;
346 rfcomm_conn_info_t *conn_info;
347 struct hal_ev_sock_connect ev;
351 char err_msg[256] = {0, };
353 /* TODO: Temperary, later need to fill correct channel form correct place */
356 if (NULL == path || NULL == addr) {
357 ERR("NULL == path || NULL = addr");
361 info = __find_rfcomm_info_from_path(path);
365 conn_info = info->conn_info;
366 _bt_hal_convert_addr_type_to_string(address, addr->address);
367 if (conn_info == NULL) {
368 ERR("conn_info is NULL for dev:[%s]", address);
372 if (write(conn_info->hal_fd, &chan, sizeof(chan)) != sizeof(chan)) {
373 ERR("Error sending RFCOMM channel");
377 conn_info->stack_fd = fd;
378 DBG("Remote address: %s, RFCOMM fd: %d", address, conn_info->stack_fd);
380 /* Send rfcomm connected event */
381 memset(&ev, 0, sizeof(ev));
382 ev.size = sizeof(ev);
383 memcpy(ev.bdaddr, addr->address, 6);
384 ev.status = BT_STATUS_SUCCESS;
385 len = write_all(conn_info->hal_fd, (unsigned char *)&ev, sizeof(ev));
387 strerror_r(errno, err_msg, sizeof(err_msg));
392 if (len != sizeof(ev)) {
393 ERR("Error sending connect event");
397 /* Handle events from App */
398 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
399 io = g_io_channel_unix_new(conn_info->hal_fd);
400 conn_info->hal_watch = g_io_add_watch(io, cond, app_event_cb, info);
401 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
402 g_io_channel_unref(io);
404 /* Handle rfcomm events from bluez */
405 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
406 io = g_io_channel_unix_new(conn_info->stack_fd);
407 conn_info->bt_watch = g_io_add_watch(io, cond, stack_event_cb, info);
408 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
409 g_io_channel_unref(io);
413 __rfcomm_cb_data_remove(info);
417 static void __bt_connect_response_cb(GDBusProxy *proxy,
418 GAsyncResult *res, gpointer user_data)
420 GError *error = NULL;
421 rfcomm_cb_data_t *cb_data;
426 if (cb_data == NULL) {
427 ERR("cb_data == NULL");
431 if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
432 ERR("Error : %s \n", error->message);
433 __rfcomm_cb_data_remove(cb_data);
438 g_object_unref(proxy);
443 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
444 GAsyncResult *res, gpointer user_data)
446 rfcomm_cb_data_t *cb_data;
449 bt_hal_register_profile_info_t info = {0};
450 char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
457 ERR("cb_data == NULL");
461 path = g_dbus_proxy_get_object_path(proxy);
462 _bt_hal_convert_device_path_to_address(path, dev_address);
463 DBG("Device Adress [%s]", dev_address);
465 g_dbus_proxy_call_finish(proxy, res, &err);
467 g_object_unref(proxy);
469 ERR("Error occured in Proxy call [%s]\n", err->message);
470 __rfcomm_cb_data_remove(cb_data);
473 INFO("Services are Updated checking required uuid is there");
474 /* Check here for uuid present */
475 ret = _bt_hal_discover_service_uuids(dev_address, cb_data->uuid);
476 if (ret == BT_STATUS_SUCCESS) {
477 info.uuid = (char *)cb_data->uuid;
478 info.obj_path = cb_data->obj_path;
479 info.role = "client";
481 ret = _bt_hal_register_profile(&info, FALSE);
483 DBG("Error: register profile");
484 ret = _bt_hal_connect_profile(dev_address, cb_data->uuid,
485 __bt_connect_response_cb, cb_data);
486 if (ret != BT_STATUS_SUCCESS) {
487 ERR("ConnectProfile failed");
488 __rfcomm_cb_data_remove(cb_data);
492 ERR("remote uuid not found");
493 __rfcomm_cb_data_remove(cb_data);
501 static rfcomm_cb_data_t *__get_rfcomm_cb_data(char *remote_uuid)
506 rfcomm_cb_data_t *cb_data;
510 id = __rfcomm_assign_id();
512 ERR("__rfcomm_assign_id failed");
516 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
517 object_id = _bt_hal_register_new_gdbus_object(path, __new_connection);
519 ERR("_bt_hal_register_new_gdbus_object failed");
521 __rfcomm_delete_id(id);
525 cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
526 g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
527 cb_data->obj_path = path;
528 cb_data->object_id = object_id;
535 static rfcomm_conn_info_t *__rfcomm_create_conn_info(char *addr, int *sock)
537 int fds[2] = {-1, -1};
538 rfcomm_conn_info_t *conn;
539 char err_msg[256] = {0, };
541 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
542 strerror_r(errno, err_msg, sizeof(err_msg));
543 ERR("socketpair(): %s", err_msg);
548 conn = g_malloc0(sizeof(rfcomm_conn_info_t));
549 g_strlcpy(conn->remote_addr, addr, BT_HAL_ADDRESS_STRING_SIZE);
550 conn->hal_fd = fds[0];
554 DBG("hal_fd: %d, sock: %d", conn->hal_fd, *sock);
559 int _bt_hal_dbus_handler_rfcomm_connect(unsigned char *addr, unsigned char *uuid, int *sock)
562 rfcomm_cb_data_t *cb_data;
563 rfcomm_conn_info_t *conn;
564 char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
565 char remote_uuid[BT_HAL_UUID_STRING_LEN];
568 ERR("remote_addr is NULL");
569 return BT_STATUS_PARM_INVALID;
573 ERR("remote_uuid is NULL");
574 return BT_STATUS_PARM_INVALID;
579 return BT_STATUS_PARM_INVALID;
582 _bt_hal_convert_uuid_type_to_string(remote_uuid, uuid);
583 cb_data = __get_rfcomm_cb_data(remote_uuid);
585 return BT_STATUS_FAIL;
587 _bt_hal_convert_addr_type_to_string(remote_addr, addr);
588 DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
589 conn = __rfcomm_create_conn_info(remote_addr, sock);
591 __bt_free_cb_data(cb_data);
592 return BT_STATUS_FAIL;
595 cb_data->conn_info = conn;
596 ret = _bt_hal_discover_services(remote_addr, (char *)remote_uuid,
597 __bt_discover_service_response_cb, cb_data);
598 if (ret != BT_STATUS_SUCCESS) {
599 ERR("Error returned while service discovery");
600 __bt_free_conn(conn);
601 __bt_free_cb_data(cb_data);
602 return BT_STATUS_FAIL;
605 INFO("Adding callback information to rfcomm_clients");
606 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
608 return BT_STATUS_SUCCESS;
611 /*************************** RFCOMM Server Implementation ***************************/
612 static rfcomm_server_data_t *__find_rfcomm_server_info_with_path(const gchar *path)
616 for (l = rfcomm_servers; l != NULL; l = l->next) {
617 rfcomm_server_data_t *info = l->data;
619 if (g_strcmp0(info->obj_path, path) == 0)
626 static rfcomm_server_data_t *__find_rfcomm_server_info_from_uuid(const char *uuid)
630 for (l = rfcomm_servers; l != NULL; l = l->next) {
631 rfcomm_server_data_t *info = l->data;
633 if (g_strcmp0(info->uuid, uuid) == 0)
640 static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
644 struct cmsghdr *cmsg;
646 char cmsg_buf[CMSG_SPACE(sizeof(int))];
647 char err_msg[256] = {0, };
649 if (sock_fd == -1 || send_fd == -1)
652 memset(&msg, 0, sizeof(msg));
653 memset(cmsg_buf, 0, sizeof(cmsg_buf));
655 msg.msg_control = cmsg_buf;
656 msg.msg_controllen = sizeof(cmsg_buf);
658 cmsg = CMSG_FIRSTHDR(&msg);
659 cmsg->cmsg_level = SOL_SOCKET;
660 cmsg->cmsg_type = SCM_RIGHTS;
661 cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
663 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
665 iov.iov_base = (unsigned char *) buf;
671 ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
673 strerror_r(errno, err_msg, sizeof(err_msg));
674 ERR("sendmsg(): sock_fd %d send_fd %d: %s",
675 sock_fd, send_fd, err_msg);
681 int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
684 rfcomm_server_data_t *info;
685 struct hal_ev_sock_connect ev;
687 DBG("%s %d", path, fd);
690 ERR("Invalid socket fd received");
694 info = __find_rfcomm_server_info_with_path(path);
696 ERR("rfcomm server info not found");
700 /* Send rfcomm client connected event */
701 memset(&ev, 0, sizeof(ev));
702 ev.size = sizeof(ev);
703 memcpy(ev.bdaddr, addr->address, 6);
704 ev.status = BT_STATUS_SUCCESS;
705 ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
707 ERR("Error sending connect event");
712 /* Remove local reference to client socket fd */
717 static void __free_rfcomm_server_data(rfcomm_server_data_t *data)
722 ERR("server data is NULL");
727 __rfcomm_delete_id(data->id);
729 if (data->object_id > 0)
730 _bt_hal_unregister_gdbus_object(data->object_id);
732 if (data->obj_path) {
733 INFO("Unregister profile");
734 _bt_hal_unregister_profile(data->obj_path);
737 if (0 < data->server_fd)
738 close(data->server_fd);
740 if (data->server_watch > 0) {
741 g_source_remove(data->server_watch);
742 data->server_watch = 0;
745 g_free(data->obj_path);
746 g_free(data->svc_name);
752 static void __remove_rfcomm_server(rfcomm_server_data_t *data)
757 rfcomm_servers = g_slist_remove(rfcomm_servers, data);
758 __free_rfcomm_server_data(data);
764 static int __register_rfcomm_server(rfcomm_server_data_t *server_data)
767 bt_hal_register_profile_info_t profile_info;
769 memset(&profile_info, 0x00, sizeof(bt_hal_register_profile_info_t));
771 profile_info.authentication = TRUE;
772 profile_info.authorization = TRUE;
773 profile_info.obj_path = server_data->obj_path;
774 profile_info.role = "server";
775 profile_info.service = server_data->uuid;
776 profile_info.uuid = server_data->uuid;
778 INFO("uuid %s, svc: %s, role: %s",
779 profile_info.uuid, profile_info.service, profile_info.role);
780 ret = _bt_hal_register_profile(&profile_info, TRUE);
782 ERR("Error: register profile");
783 return BT_STATUS_FAIL;
786 return BT_STATUS_SUCCESS;
789 static rfcomm_server_data_t *__create_rfcomm_server(char *uuid, const char *svc_name, int *sock)
795 int fds[2] = {-1, -1};
796 rfcomm_server_data_t *data;
797 char err_msg[256] = {0, };
801 data = __find_rfcomm_server_info_from_uuid(uuid);
803 ERR("RFCOMM Server exists with UUID: %s, sv_name: %s", uuid, data->svc_name);
807 id = __rfcomm_assign_id();
809 ERR("__rfcomm_assign_id failed");
813 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
814 object_id = _bt_hal_register_new_gdbus_object(path, __new_server_connection);
816 ERR("_bt_hal_register_new_gdbus_object failed");
818 __rfcomm_delete_id(id);
822 data = g_malloc0(sizeof(rfcomm_server_data_t));
823 g_strlcpy(data->uuid, uuid, BT_HAL_UUID_STRING_LEN);
824 data->svc_name = g_strdup(svc_name);
825 data->obj_path = path;
826 data->object_id = object_id;
829 ret = __register_rfcomm_server(data);
830 if (ret != BT_STATUS_SUCCESS) {
831 ERR("Error returned while registering service");
832 __free_rfcomm_server_data(data);
836 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
837 strerror_r(errno, err_msg, sizeof(err_msg));
838 ERR("socketpair(): %s", err_msg);
839 __free_rfcomm_server_data(data);
844 data->server_fd = fds[0];
847 DBG("server_fd: %d, sock: %d", data->server_fd, *sock);
851 static gboolean __server_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
854 rfcomm_server_data_t *info = data;
855 unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
860 if (cond & G_IO_HUP) {
861 ERR("Socket %d hang up", info->server_fd);
865 if (cond & (G_IO_ERR | G_IO_NVAL)) {
866 ERR("Socket %d error", info->server_fd);
870 /* Read data from application */
871 if (g_io_channel_read_chars(io, (gchar *)buff,
872 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
873 &len, &err) == G_IO_STATUS_ERROR) {
875 ERR("IO Channel read error: %s", err->message);
877 ERR("IO Channel read error client");
883 ERR("Other end of socket is closed");
890 __remove_rfcomm_server(info);
894 int _bt_hal_dbus_handler_rfcomm_listen(const char *svc_name, unsigned char *uuid, int *sock)
897 rfcomm_server_data_t *server_data;
898 char server_uuid[BT_HAL_UUID_STRING_LEN];
903 ERR("svc_name is NULL");
904 return BT_STATUS_PARM_INVALID;
909 return BT_STATUS_PARM_INVALID;
914 return BT_STATUS_PARM_INVALID;
917 _bt_hal_convert_uuid_type_to_string(server_uuid, uuid);
918 server_data = __create_rfcomm_server(server_uuid, svc_name, sock);
920 return BT_STATUS_BUSY;
922 /* TODO: Temperary, later need to fill correct channel form correct place */
923 if (write(server_data->server_fd, &chan, sizeof(chan)) != sizeof(chan)) {
924 ERR("Error sending RFCOMM channel");
925 __free_rfcomm_server_data(server_data);
927 return BT_STATUS_FAIL;
930 /* Rfcomm server: Handle events from Application */
931 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
932 io = g_io_channel_unix_new(server_data->server_fd);
933 server_data->server_watch = g_io_add_watch(io, cond, __server_event_cb, server_data);
934 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
935 g_io_channel_unref(io);
937 INFO("Adding server information to rfcomm_servers list");
938 rfcomm_servers = g_slist_append(rfcomm_servers, server_data);
941 return BT_STATUS_SUCCESS;
944 gboolean _is_rfcomm_server_uuid(const char *uuid)
951 if (NULL == __find_rfcomm_server_info_from_uuid(uuid))