OSDN Git Service

attrib: Fix PDU length check for Read by Group Type Request
[android-x86/external-bluetooth-bluez.git] / attrib / utils.c
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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <glib.h>
30
31 #include <bluetooth/bluetooth.h>
32 #include <bluetooth/hci.h>
33 #include <bluetooth/hci_lib.h>
34 #include <bluetooth/sdp.h>
35
36 #include "lib/uuid.h"
37 #include <btio/btio.h>
38 #include "att.h"
39 #include "gattrib.h"
40 #include "gatt.h"
41 #include "gatttool.h"
42
43 GIOChannel *gatt_connect(const char *src, const char *dst,
44                                 const char *dst_type, const char *sec_level,
45                                 int psm, int mtu, BtIOConnect connect_cb,
46                                 GError **gerr)
47 {
48         GIOChannel *chan;
49         bdaddr_t sba, dba;
50         uint8_t dest_type;
51         GError *tmp_err = NULL;
52         BtIOSecLevel sec;
53
54         str2ba(dst, &dba);
55
56         /* Local adapter */
57         if (src != NULL) {
58                 if (!strncmp(src, "hci", 3))
59                         hci_devba(atoi(src + 3), &sba);
60                 else
61                         str2ba(src, &sba);
62         } else
63                 bacpy(&sba, BDADDR_ANY);
64
65         /* Not used for BR/EDR */
66         if (strcmp(dst_type, "random") == 0)
67                 dest_type = BDADDR_LE_RANDOM;
68         else
69                 dest_type = BDADDR_LE_PUBLIC;
70
71         if (strcmp(sec_level, "medium") == 0)
72                 sec = BT_IO_SEC_MEDIUM;
73         else if (strcmp(sec_level, "high") == 0)
74                 sec = BT_IO_SEC_HIGH;
75         else
76                 sec = BT_IO_SEC_LOW;
77
78         if (psm == 0)
79                 chan = bt_io_connect(connect_cb, NULL, NULL, &tmp_err,
80                                 BT_IO_OPT_SOURCE_BDADDR, &sba,
81                                 BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
82                                 BT_IO_OPT_DEST_BDADDR, &dba,
83                                 BT_IO_OPT_DEST_TYPE, dest_type,
84                                 BT_IO_OPT_CID, ATT_CID,
85                                 BT_IO_OPT_SEC_LEVEL, sec,
86                                 BT_IO_OPT_INVALID);
87         else
88                 chan = bt_io_connect(connect_cb, NULL, NULL, &tmp_err,
89                                 BT_IO_OPT_SOURCE_BDADDR, &sba,
90                                 BT_IO_OPT_DEST_BDADDR, &dba,
91                                 BT_IO_OPT_PSM, psm,
92                                 BT_IO_OPT_IMTU, mtu,
93                                 BT_IO_OPT_SEC_LEVEL, sec,
94                                 BT_IO_OPT_INVALID);
95
96         if (tmp_err) {
97                 g_propagate_error(gerr, tmp_err);
98                 return NULL;
99         }
100
101         return chan;
102 }
103
104 size_t gatt_attr_data_from_string(const char *str, uint8_t **data)
105 {
106         char tmp[3];
107         size_t size, i;
108
109         size = strlen(str) / 2;
110         *data = g_try_malloc0(size);
111         if (*data == NULL)
112                 return 0;
113
114         tmp[2] = '\0';
115         for (i = 0; i < size; i++) {
116                 memcpy(tmp, str + (i * 2), 2);
117                 (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
118         }
119
120         return size;
121 }