Tizen 2.0 Release
[framework/connectivity/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 #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 GIOChannel *gatt_connect(const gchar *src, const gchar *dst,
40                                 const gchar *dst_type, const gchar *sec_level,
41                                 int psm, int mtu, BtIOConnect connect_cb)
42 {
43         GIOChannel *chan;
44         bdaddr_t sba, dba;
45         uint8_t dest_type;
46         GError *err = NULL;
47         BtIOSecLevel sec;
48
49         /* Remote device */
50         if (dst == NULL) {
51                 g_printerr("Remote Bluetooth address required\n");
52                 return NULL;
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(BT_IO_L2CAP, connect_cb, NULL, NULL, &err,
80                                 BT_IO_OPT_SOURCE_BDADDR, &sba,
81                                 BT_IO_OPT_DEST_BDADDR, &dba,
82                                 BT_IO_OPT_DEST_TYPE, dest_type,
83                                 BT_IO_OPT_CID, ATT_CID,
84                                 BT_IO_OPT_SEC_LEVEL, sec,
85                                 BT_IO_OPT_INVALID);
86         else
87                 chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err,
88                                 BT_IO_OPT_SOURCE_BDADDR, &sba,
89                                 BT_IO_OPT_DEST_BDADDR, &dba,
90                                 BT_IO_OPT_PSM, psm,
91                                 BT_IO_OPT_IMTU, mtu,
92                                 BT_IO_OPT_SEC_LEVEL, sec,
93                                 BT_IO_OPT_INVALID);
94
95         if (err) {
96                 g_printerr("%s\n", err->message);
97                 g_error_free(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 }