Home | History | Annotate | Download | only in attrib
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2011  Nokia Corporation
      6  *
      7  *
      8  *  This program is free software; you can redistribute it and/or modify
      9  *  it under the terms of the GNU General Public License as published by
     10  *  the Free Software Foundation; either version 2 of the License, or
     11  *  (at your option) any later version.
     12  *
     13  *  This program is distributed in the hope that it will be useful,
     14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  *  GNU General Public License for more details.
     17  *
     18  *  You should have received a copy of the GNU General Public License
     19  *  along with this program; if not, write to the Free Software
     20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     21  *
     22  */
     23 
     24 #include <stdlib.h>
     25 #include <glib.h>
     26 
     27 #include <bluetooth/bluetooth.h>
     28 #include <bluetooth/hci.h>
     29 #include <bluetooth/hci_lib.h>
     30 #include <bluetooth/uuid.h>
     31 #include <bluetooth/sdp.h>
     32 
     33 #include "att.h"
     34 #include "gattrib.h"
     35 #include "gatt.h"
     36 #include "btio.h"
     37 #include "gatttool.h"
     38 
     39 /* Minimum MTU for ATT connections */
     40 #define ATT_MIN_MTU_LE		23
     41 #define ATT_MIN_MTU_L2CAP	48
     42 
     43 GIOChannel *gatt_connect(const gchar *src, const gchar *dst,
     44 				const gchar *sec_level, int psm, int mtu,
     45 				BtIOConnect connect_cb)
     46 {
     47 	GIOChannel *chan;
     48 	bdaddr_t sba, dba;
     49 	GError *err = NULL;
     50 	BtIOSecLevel sec;
     51 	int minimum_mtu;
     52 
     53 	/* This check is required because currently setsockopt() returns no
     54 	 * errors for MTU values smaller than the allowed minimum. */
     55 	minimum_mtu = psm ? ATT_MIN_MTU_L2CAP : ATT_MIN_MTU_LE;
     56 	if (mtu != 0 && mtu < minimum_mtu) {
     57 		g_printerr("MTU cannot be smaller than %d\n", minimum_mtu);
     58 		return NULL;
     59 	}
     60 
     61 	/* Remote device */
     62 	if (dst == NULL) {
     63 		g_printerr("Remote Bluetooth address required\n");
     64 		return NULL;
     65 	}
     66 	str2ba(dst, &dba);
     67 
     68 	/* Local adapter */
     69 	if (src != NULL) {
     70 		if (!strncmp(src, "hci", 3))
     71 			hci_devba(atoi(src + 3), &sba);
     72 		else
     73 			str2ba(src, &sba);
     74 	} else
     75 		bacpy(&sba, BDADDR_ANY);
     76 
     77 	if (strcmp(sec_level, "medium") == 0)
     78 		sec = BT_IO_SEC_MEDIUM;
     79 	else if (strcmp(sec_level, "high") == 0)
     80 		sec = BT_IO_SEC_HIGH;
     81 	else
     82 		sec = BT_IO_SEC_LOW;
     83 
     84 	if (psm == 0)
     85 		chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err,
     86 				BT_IO_OPT_SOURCE_BDADDR, &sba,
     87 				BT_IO_OPT_DEST_BDADDR, &dba,
     88 				BT_IO_OPT_CID, ATT_CID,
     89 				BT_IO_OPT_OMTU, mtu,
     90 				BT_IO_OPT_SEC_LEVEL, sec,
     91 				BT_IO_OPT_INVALID);
     92 	else
     93 		chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err,
     94 				BT_IO_OPT_SOURCE_BDADDR, &sba,
     95 				BT_IO_OPT_DEST_BDADDR, &dba,
     96 				BT_IO_OPT_PSM, psm,
     97 				BT_IO_OPT_OMTU, mtu,
     98 				BT_IO_OPT_SEC_LEVEL, sec,
     99 				BT_IO_OPT_INVALID);
    100 
    101 	if (err) {
    102 		g_printerr("%s\n", err->message);
    103 		g_error_free(err);
    104 		return NULL;
    105 	}
    106 
    107 	return chan;
    108 }
    109 
    110 size_t gatt_attr_data_from_string(const char *str, uint8_t **data)
    111 {
    112 	char tmp[3];
    113 	size_t size, i;
    114 
    115 	size = strlen(str) / 2;
    116 	*data = g_try_malloc0(size);
    117 	if (*data == NULL)
    118 		return 0;
    119 
    120 	tmp[2] = '\0';
    121 	for (i = 0; i < size; i++) {
    122 		memcpy(tmp, str + (i * 2), 2);
    123 		(*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
    124 	}
    125 
    126 	return size;
    127 }
    128