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");
258 DBG("len: %zu", len);
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 (gsize *)&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;
427 if (cb_data == NULL) {
428 ERR("cb_data == NULL");
432 result = g_dbus_proxy_call_finish(proxy, res, &error);
434 ERR("Error : %s \n", error->message);
435 __rfcomm_cb_data_remove(cb_data);
438 g_variant_unref(result);
442 g_object_unref(proxy);
447 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
448 GAsyncResult *res, gpointer user_data)
450 rfcomm_cb_data_t *cb_data;
453 bt_hal_register_profile_info_t info = {0};
454 char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
461 ERR("cb_data == NULL");
465 path = g_dbus_proxy_get_object_path(proxy);
466 _bt_hal_convert_device_path_to_address(path, dev_address);
467 DBG("Device Adress [%s]", dev_address);
469 g_dbus_proxy_call_finish(proxy, res, &err);
471 g_object_unref(proxy);
473 ERR("Error occured in Proxy call [%s]\n", err->message);
474 __rfcomm_cb_data_remove(cb_data);
477 INFO("Services are Updated checking required uuid is there");
478 /* Check here for uuid present */
479 ret = _bt_hal_discover_service_uuids(dev_address, cb_data->uuid);
480 if (ret == BT_STATUS_SUCCESS) {
481 info.uuid = (char *)cb_data->uuid;
482 info.obj_path = cb_data->obj_path;
483 info.role = "client";
485 ret = _bt_hal_register_profile(&info, FALSE);
487 DBG("Error: register profile");
488 ret = _bt_hal_connect_profile(dev_address, cb_data->uuid,
489 __bt_connect_response_cb, cb_data);
490 if (ret != BT_STATUS_SUCCESS) {
491 ERR("ConnectProfile failed");
492 __rfcomm_cb_data_remove(cb_data);
496 ERR("remote uuid not found");
497 __rfcomm_cb_data_remove(cb_data);
505 static rfcomm_cb_data_t *__get_rfcomm_cb_data(char *remote_uuid)
510 rfcomm_cb_data_t *cb_data;
514 id = __rfcomm_assign_id();
516 ERR("__rfcomm_assign_id failed");
520 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
521 object_id = _bt_hal_register_new_gdbus_object(path, __new_connection);
523 ERR("_bt_hal_register_new_gdbus_object failed");
525 __rfcomm_delete_id(id);
529 cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
530 g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
531 cb_data->obj_path = path;
532 cb_data->object_id = object_id;
539 static rfcomm_conn_info_t *__rfcomm_create_conn_info(char *addr, int *sock)
541 int fds[2] = {-1, -1};
542 rfcomm_conn_info_t *conn;
543 char err_msg[256] = {0, };
545 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
546 strerror_r(errno, err_msg, sizeof(err_msg));
547 ERR("socketpair(): %s", err_msg);
552 conn = g_malloc0(sizeof(rfcomm_conn_info_t));
553 g_strlcpy(conn->remote_addr, addr, BT_HAL_ADDRESS_STRING_SIZE);
554 conn->hal_fd = fds[0];
558 DBG("hal_fd: %d, sock: %d", conn->hal_fd, *sock);
563 int _bt_hal_dbus_handler_rfcomm_connect(unsigned char *addr, unsigned char *uuid, int *sock)
566 rfcomm_cb_data_t *cb_data;
567 rfcomm_conn_info_t *conn;
568 char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
569 char remote_uuid[BT_HAL_UUID_STRING_LEN];
572 ERR("remote_addr is NULL");
573 return BT_STATUS_PARM_INVALID;
577 ERR("remote_uuid is NULL");
578 return BT_STATUS_PARM_INVALID;
583 return BT_STATUS_PARM_INVALID;
586 _bt_hal_convert_uuid_type_to_string(remote_uuid, uuid);
587 cb_data = __get_rfcomm_cb_data(remote_uuid);
589 return BT_STATUS_FAIL;
591 _bt_hal_convert_addr_type_to_string(remote_addr, addr);
592 DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
593 conn = __rfcomm_create_conn_info(remote_addr, sock);
595 __bt_free_cb_data(cb_data);
596 return BT_STATUS_FAIL;
599 cb_data->conn_info = conn;
600 ret = _bt_hal_discover_services(remote_addr, (char *)remote_uuid,
601 __bt_discover_service_response_cb, cb_data);
602 if (ret != BT_STATUS_SUCCESS) {
603 ERR("Error returned while service discovery");
604 __bt_free_conn(conn);
605 __bt_free_cb_data(cb_data);
606 return BT_STATUS_FAIL;
609 INFO("Adding callback information to rfcomm_clients");
610 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
612 return BT_STATUS_SUCCESS;
615 /*************************** RFCOMM Server Implementation ***************************/
616 static rfcomm_server_data_t *__find_rfcomm_server_info_with_path(const gchar *path)
620 for (l = rfcomm_servers; l != NULL; l = l->next) {
621 rfcomm_server_data_t *info = l->data;
623 if (g_strcmp0(info->obj_path, path) == 0)
630 static rfcomm_server_data_t *__find_rfcomm_server_info_from_uuid(const char *uuid)
634 for (l = rfcomm_servers; l != NULL; l = l->next) {
635 rfcomm_server_data_t *info = l->data;
637 if (g_strcmp0(info->uuid, uuid) == 0)
644 static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
648 struct cmsghdr *cmsg;
650 char cmsg_buf[CMSG_SPACE(sizeof(int))];
651 char err_msg[256] = {0, };
653 if (sock_fd == -1 || send_fd == -1)
656 memset(&msg, 0, sizeof(msg));
657 memset(cmsg_buf, 0, sizeof(cmsg_buf));
659 msg.msg_control = cmsg_buf;
660 msg.msg_controllen = sizeof(cmsg_buf);
662 cmsg = CMSG_FIRSTHDR(&msg);
663 cmsg->cmsg_level = SOL_SOCKET;
664 cmsg->cmsg_type = SCM_RIGHTS;
665 cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
667 memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
669 iov.iov_base = (unsigned char *) buf;
675 ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
677 strerror_r(errno, err_msg, sizeof(err_msg));
678 ERR("sendmsg(): sock_fd %d send_fd %d: %s",
679 sock_fd, send_fd, err_msg);
685 int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
688 rfcomm_server_data_t *info;
689 struct hal_ev_sock_connect ev;
691 DBG("%s %d", path, fd);
694 ERR("Invalid socket fd received");
698 info = __find_rfcomm_server_info_with_path(path);
700 ERR("rfcomm server info not found");
704 /* Send rfcomm client connected event */
705 memset(&ev, 0, sizeof(ev));
706 ev.size = sizeof(ev);
707 memcpy(ev.bdaddr, addr->address, 6);
708 ev.status = BT_STATUS_SUCCESS;
709 ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
711 ERR("Error sending connect event");
716 /* Remove local reference to client socket fd */
721 static void __free_rfcomm_server_data(rfcomm_server_data_t *data)
726 ERR("server data is NULL");
731 __rfcomm_delete_id(data->id);
733 if (data->object_id > 0)
734 _bt_hal_unregister_gdbus_object(data->object_id);
736 if (data->obj_path) {
737 INFO("Unregister profile");
738 _bt_hal_unregister_profile(data->obj_path);
741 if (0 < data->server_fd)
742 close(data->server_fd);
744 if (data->server_watch > 0) {
745 g_source_remove(data->server_watch);
746 data->server_watch = 0;
749 g_free(data->obj_path);
750 g_free(data->svc_name);
756 static void __remove_rfcomm_server(rfcomm_server_data_t *data)
761 rfcomm_servers = g_slist_remove(rfcomm_servers, data);
762 __free_rfcomm_server_data(data);
768 static int __register_rfcomm_server(rfcomm_server_data_t *server_data)
771 bt_hal_register_profile_info_t profile_info;
773 memset(&profile_info, 0x00, sizeof(bt_hal_register_profile_info_t));
775 profile_info.authentication = TRUE;
776 profile_info.authorization = TRUE;
777 profile_info.obj_path = server_data->obj_path;
778 profile_info.role = "server";
779 profile_info.service = server_data->uuid;
780 profile_info.uuid = server_data->uuid;
782 INFO("uuid %s, svc: %s, role: %s",
783 profile_info.uuid, profile_info.service, profile_info.role);
784 ret = _bt_hal_register_profile(&profile_info, TRUE);
786 ERR("Error: register profile");
787 return BT_STATUS_FAIL;
790 return BT_STATUS_SUCCESS;
793 static rfcomm_server_data_t *__create_rfcomm_server(char *uuid, const char *svc_name, int *sock)
799 int fds[2] = {-1, -1};
800 rfcomm_server_data_t *data;
801 char err_msg[256] = {0, };
805 data = __find_rfcomm_server_info_from_uuid(uuid);
807 ERR("RFCOMM Server exists with UUID: %s, sv_name: %s", uuid, data->svc_name);
811 id = __rfcomm_assign_id();
813 ERR("__rfcomm_assign_id failed");
817 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
818 object_id = _bt_hal_register_new_gdbus_object(path, __new_server_connection);
820 ERR("_bt_hal_register_new_gdbus_object failed");
822 __rfcomm_delete_id(id);
826 data = g_malloc0(sizeof(rfcomm_server_data_t));
827 g_strlcpy(data->uuid, uuid, BT_HAL_UUID_STRING_LEN);
828 data->svc_name = g_strdup(svc_name);
829 data->obj_path = path;
830 data->object_id = object_id;
833 ret = __register_rfcomm_server(data);
834 if (ret != BT_STATUS_SUCCESS) {
835 ERR("Error returned while registering service");
836 __free_rfcomm_server_data(data);
840 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
841 strerror_r(errno, err_msg, sizeof(err_msg));
842 ERR("socketpair(): %s", err_msg);
843 __free_rfcomm_server_data(data);
848 data->server_fd = fds[0];
851 DBG("server_fd: %d, sock: %d", data->server_fd, *sock);
855 static gboolean __server_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
858 rfcomm_server_data_t *info = data;
859 unsigned char buff[BT_HAL_RFCOMM_MAX_BUFFER_SIZE];
864 if (cond & G_IO_HUP) {
865 ERR("Socket %d hang up", info->server_fd);
869 if (cond & (G_IO_ERR | G_IO_NVAL)) {
870 ERR("Socket %d error", info->server_fd);
874 /* Read data from application */
875 if (g_io_channel_read_chars(io, (gchar *)buff,
876 BT_HAL_RFCOMM_MAX_BUFFER_SIZE,
877 &len, &err) == G_IO_STATUS_ERROR) {
879 ERR("IO Channel read error: %s", err->message);
881 ERR("IO Channel read error client");
885 DBG("len: %zu", len);
887 ERR("Other end of socket is closed");
894 __remove_rfcomm_server(info);
898 int _bt_hal_dbus_handler_rfcomm_listen(const char *svc_name, unsigned char *uuid, int *sock)
901 rfcomm_server_data_t *server_data;
902 char server_uuid[BT_HAL_UUID_STRING_LEN];
907 ERR("svc_name is NULL");
908 return BT_STATUS_PARM_INVALID;
913 return BT_STATUS_PARM_INVALID;
918 return BT_STATUS_PARM_INVALID;
921 _bt_hal_convert_uuid_type_to_string(server_uuid, uuid);
922 server_data = __create_rfcomm_server(server_uuid, svc_name, sock);
924 return BT_STATUS_BUSY;
926 /* TODO: Temperary, later need to fill correct channel form correct place */
927 if (write(server_data->server_fd, &chan, sizeof(chan)) != sizeof(chan)) {
928 ERR("Error sending RFCOMM channel");
929 __free_rfcomm_server_data(server_data);
931 return BT_STATUS_FAIL;
934 /* Rfcomm server: Handle events from Application */
935 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
936 io = g_io_channel_unix_new(server_data->server_fd);
937 server_data->server_watch = g_io_add_watch(io, cond, __server_event_cb, server_data);
938 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
939 g_io_channel_unref(io);
941 INFO("Adding server information to rfcomm_servers list");
942 rfcomm_servers = g_slist_append(rfcomm_servers, server_data);
945 return BT_STATUS_SUCCESS;
948 gboolean _is_rfcomm_server_uuid(const char *uuid)
955 if (NULL == __find_rfcomm_server_info_from_uuid(uuid))