diff options
author | Sheldon Demario <sheldon.demario@openbossa.org> | 2011-02-17 11:18:40 -0300 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@nokia.com> | 2011-02-17 15:48:43 -0300 |
commit | eb4e1d6d4b8405a324bb68e9dc28d108746837ef (patch) | |
tree | 7f840f3b45fe43d3bf8cbb731bcbb0baed91d956 /attrib/utils.c | |
parent | b03aa22d2d2aca385840645cc32d57dc4bb90ebe (diff) | |
download | bluez-eb4e1d6d4b8405a324bb68e9dc28d108746837ef.tar.gz |
Move do_connect() to a common file between interactive.c and gatttool.c
Since do_connect() is used in both interactive.c and gatttool.c
files, it was moved to a new file that should hold all common functions
Diffstat (limited to 'attrib/utils.c')
-rw-r--r-- | attrib/utils.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/attrib/utils.c b/attrib/utils.c new file mode 100644 index 000000000..4710d2d55 --- /dev/null +++ b/attrib/utils.c @@ -0,0 +1,105 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2011 Nokia Corporation + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include <stdlib.h> +#include <glib.h> + +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> +#include <bluetooth/sdp.h> + +#include "gattrib.h" +#include "gatt.h" +#include "btio.h" +#include "gatttool.h" + +/* Minimum MTU for ATT connections */ +#define ATT_MIN_MTU_LE 23 +#define ATT_MIN_MTU_L2CAP 48 + +GIOChannel *gatt_connect(const gchar *src, const gchar *dst, + const gchar *sec_level, int psm, int mtu, gboolean le, + BtIOConnect connect_cb) +{ + GIOChannel *chan; + bdaddr_t sba, dba; + GError *err = NULL; + BtIOSecLevel sec; + + /* This check is required because currently setsockopt() returns no + * errors for MTU values smaller than the allowed minimum. */ + if (mtu != 0 && mtu < (le ? ATT_MIN_MTU_LE : ATT_MIN_MTU_L2CAP)) { + g_printerr("MTU cannot be smaller than %d\n", + (le ? ATT_MIN_MTU_LE : ATT_MIN_MTU_L2CAP)); + return NULL; + } + + /* Remote device */ + if (dst == NULL) { + g_printerr("Remote Bluetooth address required\n"); + return NULL; + } + str2ba(dst, &dba); + + /* Local adapter */ + if (src != NULL) { + if (!strncmp(src, "hci", 3)) + hci_devba(atoi(src + 3), &sba); + else + str2ba(src, &sba); + } else + bacpy(&sba, BDADDR_ANY); + + if (strcmp(sec_level, "medium") == 0) + sec = BT_IO_SEC_MEDIUM; + else if (strcmp(sec_level, "high") == 0) + sec = BT_IO_SEC_HIGH; + else + sec = BT_IO_SEC_LOW; + + if (le) + chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err, + BT_IO_OPT_SOURCE_BDADDR, &sba, + BT_IO_OPT_DEST_BDADDR, &dba, + BT_IO_OPT_CID, GATT_CID, + BT_IO_OPT_OMTU, mtu, + BT_IO_OPT_SEC_LEVEL, sec, + BT_IO_OPT_INVALID); + else + chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err, + BT_IO_OPT_SOURCE_BDADDR, &sba, + BT_IO_OPT_DEST_BDADDR, &dba, + BT_IO_OPT_PSM, psm, + BT_IO_OPT_OMTU, mtu, + BT_IO_OPT_SEC_LEVEL, sec, + BT_IO_OPT_INVALID); + + if (err) { + g_printerr("%s\n", err->message); + g_error_free(err); + return NULL; + } + + return chan; +} |