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];
228 fd = g_io_channel_unix_get_fd(io);
229 conn_info = info->conn_info;
231 if (cond & G_IO_HUP) {
232 ERR("Socket %d hang up", fd);
236 if (cond & (G_IO_ERR | G_IO_NVAL)) {
237 ERR("Socket %d error", fd);
242 ERR("conn_info is NULL");
246 /* Read data from application */
247 if (g_io_channel_read_chars(io, (gchar *)buff,
248 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
249 &len, &err) == G_IO_STATUS_ERROR) {
251 ERR("IO Channel read error: %s", err->message);
253 ERR("IO Channel read error client");
259 ERR("Other end of socket is closed");
263 /* Send data to remote device */
264 sent = write_all(conn_info->stack_fd, buff, len);
266 ERR("write(): %s", strerror(errno));
273 __rfcomm_cb_data_remove(info);
277 static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
281 rfcomm_cb_data_t *info = data;
282 rfcomm_conn_info_t *conn_info;
283 unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
289 fd = g_io_channel_unix_get_fd(io);
290 conn_info = info->conn_info;
292 if (cond & G_IO_HUP) {
293 ERR("Socket %d hang up", fd);
297 if (cond & (G_IO_ERR | G_IO_NVAL)) {
298 ERR("Socket %d error", fd);
303 ERR("conn_info is NULL");
307 /* Read data from remote device */
308 if (g_io_channel_read_chars(io, (gchar *)buff,
309 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
310 &len, &err) == G_IO_STATUS_ERROR) {
312 ERR("IO Channel read error: %s", err->message);
314 ERR("IO Channel read error client");
320 ERR("Other end of socket is closed");
324 /* Send data to application */
325 sent = write_all(conn_info->hal_fd, buff, len);
327 ERR("write(): %s", strerror(errno));
334 __rfcomm_cb_data_remove(info);
338 static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
340 char address[BT_HAL_ADDRESS_STRING_SIZE];
341 rfcomm_cb_data_t *info;
342 rfcomm_conn_info_t *conn_info;
343 struct hal_ev_sock_connect ev;
348 /* TODO: Temperary, later need to fill correct channel form correct place */
351 if (NULL == path || NULL == addr) {
352 ERR("NULL == path || NULL = addr");
356 info = __find_rfcomm_info_from_path(path);
360 conn_info = info->conn_info;
361 _bt_hal_convert_addr_type_to_string(address, addr->address);
362 if (conn_info == NULL) {
363 ERR("conn_info is NULL for dev:[%s]", address);
367 if (write(conn_info->hal_fd, &chan, sizeof(chan)) != sizeof(chan)) {
368 ERR("Error sending RFCOMM channel");
372 conn_info->stack_fd = fd;
373 DBG("Remote address: %s, RFCOMM fd: %d", address, conn_info->stack_fd);
375 /* Send rfcomm connected event */
376 memset(&ev, 0, sizeof(ev));
377 ev.size = sizeof(ev);
378 memcpy(ev.bdaddr, addr->address, 6);
379 ev.status = BT_STATUS_SUCCESS;
380 len = write_all(conn_info->hal_fd, (unsigned char *)&ev, sizeof(ev));
382 ERR("%s", strerror(errno));
386 if (len != sizeof(ev)) {
387 ERR("Error sending connect event");
391 /* Handle events from App */
392 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
393 io = g_io_channel_unix_new(conn_info->hal_fd);
394 conn_info->hal_watch = g_io_add_watch(io, cond, app_event_cb, info);
395 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
396 g_io_channel_unref(io);
398 /* Handle rfcomm events from bluez */
399 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
400 io = g_io_channel_unix_new(conn_info->stack_fd);
401 conn_info->bt_watch = g_io_add_watch(io, cond, stack_event_cb, info);
402 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
403 g_io_channel_unref(io);
407 __rfcomm_cb_data_remove(info);
411 static void __bt_connect_response_cb(GDBusProxy *proxy,
412 GAsyncResult *res, gpointer user_data)
414 GError *error = NULL;
415 rfcomm_cb_data_t *cb_data;
420 if (cb_data == NULL) {
421 ERR("cb_data == NULL");
425 if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
426 ERR("Error : %s \n", error->message);
427 __rfcomm_cb_data_remove(cb_data);
432 g_object_unref(proxy);
437 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
438 GAsyncResult *res, gpointer user_data)
440 rfcomm_cb_data_t *cb_data;
443 bt_hal_register_profile_info_t info = {0};
444 char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
451 ERR("cb_data == NULL");
455 path = g_dbus_proxy_get_object_path(proxy);
456 _bt_hal_convert_device_path_to_address(path, dev_address);
457 DBG("Device Adress [%s]", dev_address);
459 g_dbus_proxy_call_finish(proxy, res, &err);
461 g_object_unref(proxy);
463 ERR("Error occured in Proxy call [%s]\n", err->message);
464 __rfcomm_cb_data_remove(cb_data);
467 INFO("Services are Updated checking required uuid is there");
468 /* Check here for uuid present */
469 ret = _bt_hal_discover_service_uuids(dev_address, cb_data->uuid);
470 if (ret == BT_STATUS_SUCCESS) {
471 info.uuid = (char *)cb_data->uuid;
472 info.obj_path = cb_data->obj_path;
473 info.role = "client";
475 ret = _bt_hal_register_profile(&info, FALSE);
477 DBG("Error: register profile");
478 ret = _bt_hal_connect_profile(dev_address, cb_data->uuid,
479 __bt_connect_response_cb, cb_data);
480 if (ret != BT_STATUS_SUCCESS) {
481 ERR("ConnectProfile failed");
482 __rfcomm_cb_data_remove(cb_data);
486 ERR("remote uuid not found");
487 __rfcomm_cb_data_remove(cb_data);
495 static rfcomm_cb_data_t *__get_rfcomm_cb_data(char *remote_uuid)
500 rfcomm_cb_data_t *cb_data;
504 id = __rfcomm_assign_id();
506 ERR("__rfcomm_assign_id failed");
510 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
511 object_id = _bt_hal_register_new_gdbus_object(path, __new_connection);
513 ERR("_bt_hal_register_new_gdbus_object failed");
515 __rfcomm_delete_id(id);
519 cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
520 g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
521 cb_data->obj_path = path;
522 cb_data->object_id = object_id;
529 static rfcomm_conn_info_t *__rfcomm_create_conn_info(char *addr, int *sock)
531 int fds[2] = {-1, -1};
532 rfcomm_conn_info_t *conn;
534 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
535 ERR("socketpair(): %s", strerror(errno));
540 conn = g_malloc0(sizeof(rfcomm_conn_info_t));
541 g_strlcpy(conn->remote_addr, addr, BT_HAL_ADDRESS_STRING_SIZE);
542 conn->hal_fd = fds[0];
546 DBG("hal_fd: %d, sock: %d", conn->hal_fd, *sock);
551 int _bt_hal_dbus_handler_rfcomm_connect(unsigned char *addr, unsigned char *uuid, int *sock)
554 rfcomm_cb_data_t *cb_data;
555 rfcomm_conn_info_t *conn;
556 char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
557 char remote_uuid[BT_HAL_UUID_STRING_LEN];
560 ERR("remote_addr is NULL");
561 return BT_STATUS_PARM_INVALID;
565 ERR("remote_uuid is NULL");
566 return BT_STATUS_PARM_INVALID;
571 return BT_STATUS_PARM_INVALID;
574 _bt_hal_convert_uuid_type_to_string(remote_uuid, uuid);
575 cb_data = __get_rfcomm_cb_data(remote_uuid);
577 return BT_STATUS_FAIL;
579 _bt_hal_convert_addr_type_to_string(remote_addr, addr);
580 DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
581 conn = __rfcomm_create_conn_info(remote_addr, sock);
583 __bt_free_cb_data(cb_data);
584 return BT_STATUS_FAIL;
587 cb_data->conn_info = conn;
588 ret = _bt_hal_discover_services(remote_addr, (char *)remote_uuid,
589 __bt_discover_service_response_cb, cb_data);
590 if (ret != BT_STATUS_SUCCESS) {
591 ERR("Error returned while service discovery");
592 __bt_free_conn(conn);
593 __bt_free_cb_data(cb_data);
594 return BT_STATUS_FAIL;
597 INFO("Adding callback information to rfcomm_clients");
598 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
600 return BT_STATUS_SUCCESS;
603 /*************************** RFCOMM Server Implementation ***************************/
604 static rfcomm_server_data_t *__find_rfcomm_server_info_with_path(const gchar *path)
608 for (l = rfcomm_servers; l != NULL; l = l->next) {
609 rfcomm_server_data_t *info = l->data;
611 if (g_strcmp0(info->obj_path, path) == 0)
618 static rfcomm_server_data_t *__find_rfcomm_server_info_from_uuid(const char *uuid)
622 for (l = rfcomm_servers; l != NULL; l = l->next) {
623 rfcomm_server_data_t *info = l->data;
625 if (g_strcmp0(info->uuid, uuid) == 0)
632 static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
636 struct cmsghdr *cmsg;
638 char cmsg_buf[CMSG_SPACE(sizeof(int))];
640 if (sock_fd == -1 || send_fd == -1)
643 memset(&msg, 0, sizeof(msg));
644 memset(cmsg_buf, 0, sizeof(cmsg_buf));
646 msg.msg_control = cmsg_buf;
647 msg.msg_controllen = sizeof(cmsg_buf);
649 cmsg = CMSG_FIRSTHDR(&msg);
650 cmsg->cmsg_level = SOL_SOCKET;
651 cmsg->cmsg_type = SCM_RIGHTS;
652 cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
654 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
656 iov.iov_base = (unsigned char *) buf;
662 ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
664 ERR("sendmsg(): sock_fd %d send_fd %d: %s",
665 sock_fd, send_fd, strerror(errno));
670 int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
673 rfcomm_server_data_t *info;
674 struct hal_ev_sock_connect ev;
676 DBG("%s %d", path, fd);
679 ERR("Invalid socket fd received");
683 info = __find_rfcomm_server_info_with_path(path);
685 ERR("rfcomm server info not found");
689 /* Send rfcomm client connected event */
690 memset(&ev, 0, sizeof(ev));
691 ev.size = sizeof(ev);
692 memcpy(ev.bdaddr, addr->address, 6);
693 ev.status = BT_STATUS_SUCCESS;
694 ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
696 ERR("Error sending connect event");
701 /* Remove local reference to client socket fd */
706 static void __free_rfcomm_server_data(rfcomm_server_data_t *data)
711 ERR("server data is NULL");
716 __rfcomm_delete_id(data->id);
718 if (data->object_id > 0)
719 _bt_hal_unregister_gdbus_object(data->object_id);
721 if (data->obj_path) {
722 INFO("Unregister profile");
723 _bt_hal_unregister_profile(data->obj_path);
726 if (0 < data->server_fd)
727 close(data->server_fd);
729 if (data->server_watch > 0) {
730 g_source_remove(data->server_watch);
731 data->server_watch = 0;
734 g_free(data->obj_path);
735 g_free(data->svc_name);
741 static void __remove_rfcomm_server(rfcomm_server_data_t *data)
746 rfcomm_servers = g_slist_remove(rfcomm_servers, data);
747 __free_rfcomm_server_data(data);
753 static int __register_rfcomm_server(rfcomm_server_data_t *server_data)
756 bt_hal_register_profile_info_t profile_info;
758 memset(&profile_info, 0x00, sizeof(bt_hal_register_profile_info_t));
760 profile_info.authentication = TRUE;
761 profile_info.authorization = TRUE;
762 profile_info.obj_path = server_data->obj_path;
763 profile_info.role = "server";
764 profile_info.service = server_data->uuid;
765 profile_info.uuid = server_data->uuid;
767 INFO("uuid %s, svc: %s, role: %s",
768 profile_info.uuid, profile_info.service, profile_info.role);
769 ret = _bt_hal_register_profile(&profile_info, TRUE);
771 ERR("Error: register profile");
772 return BT_STATUS_FAIL;
775 return BT_STATUS_SUCCESS;
778 static rfcomm_server_data_t *__create_rfcomm_server(char *uuid, const char *svc_name, int *sock)
784 int fds[2] = {-1, -1};
785 rfcomm_server_data_t *data;
789 data = __find_rfcomm_server_info_from_uuid(uuid);
791 ERR("RFCOMM Server exists with UUID: %s, sv_name: %s", uuid, data->svc_name);
795 id = __rfcomm_assign_id();
797 ERR("__rfcomm_assign_id failed");
801 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
802 object_id = _bt_hal_register_new_gdbus_object(path, __new_server_connection);
804 ERR("_bt_hal_register_new_gdbus_object failed");
806 __rfcomm_delete_id(id);
810 data = g_malloc0(sizeof(rfcomm_server_data_t));
811 g_strlcpy(data->uuid, uuid, BT_HAL_UUID_STRING_LEN);
812 data->svc_name = g_strdup(svc_name);
813 data->obj_path = path;
814 data->object_id = object_id;
817 ret = __register_rfcomm_server(data);
818 if (ret != BT_STATUS_SUCCESS) {
819 ERR("Error returned while registering service");
820 __free_rfcomm_server_data(data);
824 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
825 ERR("socketpair(): %s", strerror(errno));
826 __free_rfcomm_server_data(data);
831 data->server_fd = fds[0];
834 DBG("server_fd: %d, sock: %d", data->server_fd, *sock);
838 static gboolean __server_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
841 rfcomm_server_data_t *info = data;
842 unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
847 if (cond & G_IO_HUP) {
848 ERR("Socket %d hang up", info->server_fd);
852 if (cond & (G_IO_ERR | G_IO_NVAL)) {
853 ERR("Socket %d error", info->server_fd);
857 /* Read data from application */
858 if (g_io_channel_read_chars(io, (gchar *)buff,
859 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
860 &len, &err) == G_IO_STATUS_ERROR) {
862 ERR("IO Channel read error: %s", err->message);
864 ERR("IO Channel read error client");
870 ERR("Other end of socket is closed");
877 __remove_rfcomm_server(info);
881 int _bt_hal_dbus_handler_rfcomm_listen(const char *svc_name, unsigned char *uuid, int *sock)
884 rfcomm_server_data_t *server_data;
885 char server_uuid[BT_HAL_UUID_STRING_LEN];
890 ERR("svc_name is NULL");
891 return BT_STATUS_PARM_INVALID;
896 return BT_STATUS_PARM_INVALID;
901 return BT_STATUS_PARM_INVALID;
904 _bt_hal_convert_uuid_type_to_string(server_uuid, uuid);
905 server_data = __create_rfcomm_server(server_uuid, svc_name, sock);
907 return BT_STATUS_BUSY;
909 /* TODO: Temperary, later need to fill correct channel form correct place */
910 if (write(server_data->server_fd, &chan, sizeof(chan)) != sizeof(chan)) {
911 ERR("Error sending RFCOMM channel");
912 __free_rfcomm_server_data(server_data);
914 return BT_STATUS_FAIL;
917 /* Rfcomm server: Handle events from Application */
918 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
919 io = g_io_channel_unix_new(server_data->server_fd);
920 server_data->server_watch = g_io_add_watch(io, cond, __server_event_cb, server_data);
921 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
922 g_io_channel_unref(io);
924 INFO("Adding server information to rfcomm_servers list");
925 rfcomm_servers = g_slist_append(rfcomm_servers, server_data);
928 return BT_STATUS_SUCCESS;
931 gboolean _is_rfcomm_server_uuid(const char *uuid)
938 if (NULL == __find_rfcomm_server_info_from_uuid(uuid))