4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <sys/types.h>
25 #include <sys/socket.h>
31 #include "bt-hal-l2cap-le-dbus-handler.h"
32 #include "bt-hal-dbus-common-utils.h"
33 #include "bt-hal-socket-common.h"
35 #define BT_HAL_L2CAP_LE_ID_MAX 245
36 #define BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE 0xFFFF
37 #define L2CAP_LE_DEFAULT_CHANNEL 0
38 #define L2CAP_LE_PSM_MAX 0xFFFF
39 #define L2CAP_LE_UUID_SUBSTR "FFFFFFFF-FFFF-FFFF-FFFF-"
47 unsigned int server_watch;
49 } l2cap_le_server_data_t;
51 static GSList *l2cap_le_clients;
52 static GSList *l2cap_le_servers;
53 static int latest_id = -1;
54 static gboolean id_used[BT_HAL_L2CAP_LE_ID_MAX];
56 static int __l2cap_le_assign_id(void)
60 DBG("latest_id: %d", latest_id);
62 index = latest_id + 1;
63 if (index >= BT_HAL_L2CAP_LE_ID_MAX)
66 DBG("index: %d", index);
68 while (id_used[index] == TRUE) {
69 if (index == latest_id) {
70 ERR("All request ID is used");
75 if (index >= BT_HAL_L2CAP_LE_ID_MAX)
80 id_used[index] = TRUE;
81 INFO("Assigned Id: %d", latest_id);
86 static void __l2cap_le_delete_id(int id)
88 if (id >= BT_HAL_L2CAP_LE_ID_MAX || id < 0) {
89 ERR("Invalid id %d", id);
95 DBG("id: %d, latest_id: %d", id, latest_id);
98 /*************************** L2CAP_LE Client Implementation ***************************/
99 l2cap_le_cb_data_t *__find_l2cap_le_client_info_from_path(const char *path)
103 for (l = l2cap_le_clients; l != NULL; l = l->next) {
104 l2cap_le_cb_data_t *info = l->data;
107 if (g_strcmp0(info->obj_path, path) == 0)
114 static void __bt_free_conn(l2cap_le_conn_info_t *conn)
119 if (0 < conn->hal_fd) {
120 shutdown(conn->hal_fd, SHUT_RDWR);
124 if (conn->hal_watch > 0) {
125 g_source_remove(conn->hal_watch);
129 if (0 < conn->stack_fd) {
130 shutdown(conn->stack_fd, SHUT_RDWR);
131 close(conn->stack_fd);
134 if (conn->bt_watch > 0) {
135 g_source_remove(conn->bt_watch);
142 static void __bt_free_cb_data(l2cap_le_cb_data_t *cb_data)
146 if (cb_data->id >= 0)
147 __l2cap_le_delete_id(cb_data->id);
149 if (cb_data->object_id > 0)
150 _bt_hal_unregister_gdbus_object(cb_data->object_id);
152 if (cb_data->obj_path) {
153 INFO("remove l2cap_le socket");
154 _bt_hal_remove_l2cap_le_socket(cb_data->obj_path);
157 g_free(cb_data->obj_path);
158 g_free(cb_data->device_path);
164 void __l2cap_le_cb_data_remove(l2cap_le_cb_data_t *info)
171 l2cap_le_clients = g_slist_remove(l2cap_le_clients, info);
172 __bt_free_conn(info->conn_info);
173 __bt_free_cb_data(info);
176 static int write_all(int fd, unsigned char *buf, int len)
184 written = write(fd, buf, len);
186 if (errno == EINTR || errno == EAGAIN)
194 len -= written; buf += written; sent += written;
200 static gboolean app_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
204 l2cap_le_cb_data_t *info = data;
205 l2cap_le_conn_info_t *conn_info;
206 unsigned char buff[BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE];
209 char err_msg[256] = {0, };
212 fd = g_io_channel_unix_get_fd(io);
213 conn_info = info->conn_info;
215 if (cond & G_IO_HUP) {
216 ERR("Socket %d hang up", fd);
220 if (cond & (G_IO_ERR | G_IO_NVAL)) {
221 ERR("Socket %d error", fd);
226 ERR("conn_info is NULL");
230 /* Read data from application */
231 if (g_io_channel_read_chars(io, (gchar *)buff,
232 BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE,
233 &len, &err) == G_IO_STATUS_ERROR) {
235 ERR("IO Channel read error: %s", err->message);
237 ERR("IO Channel read error client");
241 INFO("len: %zu", len);
243 ERR("Other end of socket is closed");
247 /* Send data to remote device */
248 sent = write_all(conn_info->stack_fd, buff, len);
250 strerror_r(errno, err_msg, sizeof(err_msg));
251 ERR("write(): %s", err_msg);
258 __l2cap_le_cb_data_remove(info);
262 static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
266 l2cap_le_cb_data_t *info = data;
267 l2cap_le_conn_info_t *conn_info;
268 unsigned char buff[BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE];
271 char err_msg[256] = {0, };
275 fd = g_io_channel_unix_get_fd(io);
276 conn_info = info->conn_info;
278 if (cond & G_IO_HUP) {
279 ERR("Socket %d hang up", fd);
283 if (cond & (G_IO_ERR | G_IO_NVAL)) {
284 ERR("Socket %d error", fd);
289 ERR("conn_info is NULL");
293 /* Read data from remote device */
294 if (g_io_channel_read_chars(io, (gchar *)buff,
295 BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE,
296 (gsize *)&len, &err) == G_IO_STATUS_ERROR) {
298 ERR("IO Channel read error: %s", err->message);
300 ERR("IO Channel read error client");
304 INFO("len: %d", len);
306 ERR("Other end of socket is closed");
310 /* Send data to application */
311 sent = write_all(conn_info->hal_fd, buff, len);
313 strerror_r(errno, err_msg, sizeof(err_msg));
314 ERR("write(): %s", err_msg);
321 __l2cap_le_cb_data_remove(info);
325 static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
327 char address[BT_HAL_ADDRESS_STRING_SIZE];
328 l2cap_le_cb_data_t *info;
329 l2cap_le_conn_info_t *conn_info;
330 struct hal_ev_sock_connect ev;
334 char err_msg[256] = {0, };
336 /* TODO: Temperary, later need to fill correct psm */
339 if (NULL == path || NULL == addr) {
340 ERR("NULL == path || NULL = addr");
344 info = __find_l2cap_le_client_info_from_path(path);
350 conn_info = info->conn_info;
351 _bt_hal_convert_addr_type_to_string(address, addr->address);
352 if (conn_info == NULL) {
353 ERR("conn_info is NULL for dev:[%s]", address);
357 if (write(conn_info->hal_fd, &psm, sizeof(psm)) != sizeof(psm)) {
358 ERR("Error sending l2cap_le channel");
362 conn_info->stack_fd = fd;
363 DBG("Remote address: %s, l2cap_le stack fd: %d, hal_fd %d",
364 address, conn_info->stack_fd, conn_info->hal_fd);
366 /* Send l2cap_le connected event */
367 memset(&ev, 0, sizeof(ev));
368 ev.size = sizeof(ev);
369 memcpy(ev.bdaddr, addr->address, 6);
370 ev.status = BT_STATUS_SUCCESS;
372 // TODO: Need to fill corrrect psm later on
375 len = write_all(conn_info->hal_fd, (unsigned char *)&ev, sizeof(ev));
377 strerror_r(errno, err_msg, sizeof(err_msg));
382 if (len != sizeof(ev)) {
383 ERR("Error sending connect event");
387 /* Handle events from App */
388 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
389 io = g_io_channel_unix_new(conn_info->hal_fd);
390 conn_info->hal_watch = g_io_add_watch(io, cond, app_event_cb, info);
391 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
392 g_io_channel_set_encoding(io, NULL, NULL);
393 g_io_channel_unref(io);
395 /* Handle l2cap_le events from bluez */
396 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
397 io = g_io_channel_unix_new(conn_info->stack_fd);
398 conn_info->bt_watch = g_io_add_watch(io, cond, stack_event_cb, info);
399 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
400 g_io_channel_set_encoding(io, NULL, NULL);
401 g_io_channel_unref(io);
405 __l2cap_le_cb_data_remove(info);
409 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
412 GError *error = NULL;
413 l2cap_le_cb_data_t *cb_data;
419 if (cb_data == NULL) {
420 ERR("cb_data == NULL");
424 result = g_dbus_proxy_call_finish(proxy, res, &error);
426 ERR("Error : %s \n", error->message);
427 __l2cap_le_cb_data_remove(cb_data);
430 g_variant_unref(result);
434 g_object_unref(proxy);
439 static l2cap_le_cb_data_t *__get_l2cap_le_cb_data(int psm)
444 l2cap_le_cb_data_t *cb_data;
448 id = __l2cap_le_assign_id();
450 ERR("__l2cap_le_assign_id failed");
454 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
455 object_id = _bt_hal_register_new_l2cap_le_gdbus_object(path, __new_connection);
457 ERR("_bt_hal_register_new_l2cap_le_gdbus_object failed");
459 __l2cap_le_delete_id(id);
463 cb_data = g_malloc0(sizeof(l2cap_le_cb_data_t));
465 cb_data->obj_path = path;
466 cb_data->object_id = object_id;
473 static l2cap_le_conn_info_t *__l2cap_le_create_conn_info(char *addr, int *sock)
475 int fds[2] = {-1, -1};
476 l2cap_le_conn_info_t *conn;
477 char err_msg[256] = {0, };
479 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
480 strerror_r(errno, err_msg, sizeof(err_msg));
481 ERR("socketpair(): %s", err_msg);
486 conn = g_malloc0(sizeof(l2cap_le_conn_info_t));
487 g_strlcpy(conn->remote_addr, addr, BT_HAL_ADDRESS_STRING_SIZE);
488 conn->hal_fd = fds[0];
492 INFO("hal_fd: %d, sock: %d", conn->hal_fd, *sock);
497 int _bt_hal_dbus_handler_l2cap_le_connect(unsigned char *addr, int psm, int *sock)
500 l2cap_le_cb_data_t *cb_data;
501 l2cap_le_conn_info_t *conn;
502 char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
503 bt_hal_l2cap_le_profile_info_t profile_info;
506 ERR("remote_addr is NULL");
507 return BT_STATUS_PARM_INVALID;
512 return BT_STATUS_PARM_INVALID;
515 cb_data = __get_l2cap_le_cb_data(psm);
517 ERR("cb_Data is NULL");
518 return BT_STATUS_FAIL;
521 _bt_hal_convert_addr_type_to_string(remote_addr, addr);
522 INFO("Connecting to %s, psm %d", remote_addr, psm);
523 conn = __l2cap_le_create_conn_info(remote_addr, sock);
525 INFO("connection is NULL");
526 __bt_free_cb_data(cb_data);
527 return BT_STATUS_FAIL;
530 cb_data->conn_info = conn;
532 memset(&profile_info, 0x00, sizeof(bt_hal_l2cap_le_profile_info_t));
534 profile_info.authentication = FALSE;
535 profile_info.authorization = FALSE;
536 profile_info.obj_path = cb_data->obj_path;
537 profile_info.psm = psm;
539 INFO("l2cap_le connect psm: %d", profile_info.psm);
541 ret = _bt_hal_connect_l2cap_le(remote_addr, &profile_info,
542 __bt_connect_response_cb, cb_data);
544 if (ret != BT_STATUS_SUCCESS) {
545 ERR("Error returned while connecting l2cap_le");
546 __bt_free_conn(conn);
547 __bt_free_cb_data(cb_data);
548 return BT_STATUS_FAIL;
551 INFO("Adding callback information to l2cap_le_clients");
552 l2cap_le_clients = g_slist_append(l2cap_le_clients, cb_data);
554 return BT_STATUS_SUCCESS;
557 /*************************** L2CAP_LE Server Implementation ***************************/
558 static l2cap_le_server_data_t *__find_l2cap_le_server_info_from_path(const gchar *path)
562 for (l = l2cap_le_servers; l != NULL; l = l->next) {
563 l2cap_le_server_data_t *info = l->data;
565 if (g_strcmp0(info->obj_path, path) == 0)
572 static l2cap_le_server_data_t *__find_l2cap_le_server_info_from_psm(int psm)
576 for (l = l2cap_le_servers; l != NULL; l = l->next) {
577 l2cap_le_server_data_t *info = l->data;
579 if (info->psm == psm)
587 static int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
590 l2cap_le_server_data_t *info;
591 struct hal_ev_sock_connect ev;
593 INFO("%s %d", path, fd);
596 ERR("Invalid socket fd received");
600 info = __find_l2cap_le_server_info_from_path(path);
602 ERR("l2cap_le server info not found");
606 /* Send l2cap_le client connected event */
607 memset(&ev, 0, sizeof(ev));
608 ev.size = sizeof(ev);
609 memcpy(ev.bdaddr, addr->address, 6);
610 ev.status = BT_STATUS_SUCCESS;
611 ret = _bt_hal_send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
613 ERR("Error sending connect event");
618 /* Remove local reference to client socket fd */
623 static void __free_l2cap_le_server_data(l2cap_le_server_data_t *data)
628 ERR("server data is NULL");
633 __l2cap_le_delete_id(data->id);
635 if (data->object_id > 0)
636 _bt_hal_unregister_l2cap_le_gdbus_object(data->object_id);
638 if (data->obj_path) {
639 INFO("Remove socket");
640 _bt_hal_remove_l2cap_le_socket(data->obj_path);
643 if (0 < data->server_fd)
644 close(data->server_fd);
646 if (data->server_watch > 0) {
647 g_source_remove(data->server_watch);
648 data->server_watch = 0;
651 g_free(data->obj_path);
657 static void __remove_l2cap_le_server(l2cap_le_server_data_t *data)
661 l2cap_le_servers = g_slist_remove(l2cap_le_servers, data);
662 __free_l2cap_le_server_data(data);
667 static int __listen_l2cap_le_server(l2cap_le_server_data_t *server_data)
671 bt_hal_l2cap_le_profile_info_t profile_info;
675 memset(&profile_info, 0x00, sizeof(bt_hal_l2cap_le_profile_info_t));
677 profile_info.authentication = FALSE;
678 profile_info.authorization = FALSE;
679 profile_info.obj_path = server_data->obj_path;
680 profile_info.psm = server_data->psm;
682 INFO("Listen psm: %d", profile_info.psm);
684 ret = _bt_hal_listen_l2cap_le(&profile_info);
686 ERR("Error: Listen L2CAP_LE server failed");
687 return BT_STATUS_FAIL;
690 if (server_data->psm == L2CAP_LE_DEFAULT_CHANNEL) {
691 ret = _bt_hal_get_psm_l2cap_le(&profile_info, &psm);
693 ERR("Error: get_psm failed");
694 return BT_STATUS_FAIL;
696 INFO("l2cap_le server, listening to psm %d", psm);
697 server_data->psm = (int)psm;
701 return BT_STATUS_SUCCESS;
704 static l2cap_le_server_data_t *__create_l2cap_le_server(int psm, int *sock)
710 int fds[2] = {-1, -1};
711 l2cap_le_server_data_t *data;
712 char err_msg[256] = {0, };
716 data = __find_l2cap_le_server_info_from_psm(psm);
718 ERR("L2CAP_LE Server exists with psm: %d", psm);
722 id = __l2cap_le_assign_id();
724 ERR("__l2cap_le_assign_id failed");
728 path = g_strdup_printf("/org/socket/server/%d/%d", getpid(), id);
729 object_id = _bt_hal_register_new_l2cap_le_gdbus_object(path, __new_server_connection);
731 ERR("_bt_hal_register_new_l2cap_le_gdbus_object failed");
733 __l2cap_le_delete_id(id);
737 data = g_malloc0(sizeof(l2cap_le_server_data_t));
738 data->obj_path = path;
739 data->object_id = object_id;
743 ret = __listen_l2cap_le_server(data);
744 if (ret != BT_STATUS_SUCCESS) {
745 ERR("Error returned while registering service");
746 __free_l2cap_le_server_data(data);
750 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
751 strerror_r(errno, err_msg, sizeof(err_msg));
752 ERR("socketpair(): %s", err_msg);
753 __free_l2cap_le_server_data(data);
758 data->server_fd = fds[0];
761 INFO("server_fd: %d, sock: %d", data->server_fd, *sock);
765 static gboolean __server_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
768 l2cap_le_server_data_t *info = data;
769 unsigned char buff[BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE];
774 if (cond & G_IO_HUP) {
775 ERR("Socket %d hang up", info->server_fd);
779 if (cond & (G_IO_ERR | G_IO_NVAL)) {
780 ERR("Socket %d error", info->server_fd);
784 /* Read data from application */
785 if (g_io_channel_read_chars(io, (gchar *)buff,
786 BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE,
787 &len, &err) == G_IO_STATUS_ERROR) {
789 ERR("IO Channel read error: %s", err->message);
791 ERR("IO Channel read error client");
795 INFO("len: %zu", len);
797 ERR("Other end of socket is closed");
804 __remove_l2cap_le_server(info);
808 int _bt_hal_dbus_handler_l2cap_le_listen(int psm, int *sock)
811 l2cap_le_server_data_t *server_data;
817 return BT_STATUS_PARM_INVALID;
820 if (psm < 0 || psm > L2CAP_LE_PSM_MAX) {
821 ERR("Invalid PSM %d", psm);
822 return BT_STATUS_PARM_INVALID;
825 server_data = __create_l2cap_le_server(psm, sock);
827 return BT_STATUS_BUSY;
829 new_psm = server_data->psm;
830 INFO("sending l2cap_le psm %d", new_psm);
832 if (write(server_data->server_fd, &new_psm, sizeof(new_psm)) != sizeof(new_psm)) {
833 ERR("Error sending l2cap_le psm");
834 __free_l2cap_le_server_data(server_data);
836 return BT_STATUS_FAIL;
839 /* L2CAP_LE server: Handle events from Application */
840 cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
841 io = g_io_channel_unix_new(server_data->server_fd);
842 server_data->server_watch = g_io_add_watch(io, cond, __server_event_cb, server_data);
843 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
844 g_io_channel_set_encoding(io, NULL, NULL);
845 g_io_channel_unref(io);
847 INFO("Adding server information to l2cap_le_servers list");
848 l2cap_le_servers = g_slist_append(l2cap_le_servers, server_data);
850 return BT_STATUS_SUCCESS;
853 gboolean _is_l2cap_le_server_uuid(const char *uuid)
860 strncmp(uuid, L2CAP_LE_UUID_SUBSTR, strlen(L2CAP_LE_UUID_SUBSTR)))
863 value = uuid + strlen(L2CAP_LE_UUID_SUBSTR);
866 if (NULL == __find_l2cap_le_server_info_from_psm(psm))