8 #include "../../gsmstate.h" 10 #ifdef GSM_ENABLE_BLUETOOTHDEVICE 13 #include <sys/socket.h> 25 #include "../../gsmcomon.h" 26 #include "../devfunc.h" 32 #ifndef BTPROTO_RFCOMM 33 #define BTPROTO_RFCOMM BLUETOOTH_PROTO_RFCOMM 34 #define BDADDR_ANY NG_HCI_BDADDR_ANY 35 #define sockaddr_bt sockaddr_rfcomm 36 #define bt_len rfcomm_len 37 #define bt_family rfcomm_family 38 #define bt_channel rfcomm_channel 39 #define bt_bdaddr rfcomm_bdaddr 40 #define bdaddr_copy(d, s) memcpy((d), (s), sizeof(bdaddr_t)) 46 struct sockaddr_bt sa;
49 memset(&sa, 0,
sizeof(sa));
50 sa.bt_len =
sizeof(sa);
51 sa.bt_family = AF_BLUETOOTH;
53 smprintf(s,
"Connecting to RF channel %i\n", channel);
55 fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
57 smprintf(s,
"Can't create socket\n");
61 bdaddr_copy(&sa.bt_bdaddr, BDADDR_ANY);
63 if (bind(fd, (
struct sockaddr *)&sa,
sizeof(sa)) < 0) {
64 smprintf(s,
"Can't bind socket: %s\n", strerror(errno));
69 sa.bt_channel = channel;
70 bdaddr_copy(&sa.bt_bdaddr, bdaddr);
72 if (connect(fd, (
struct sockaddr *)&sa,
sizeof(sa)) < 0) {
73 smprintf(s,
"Can't connect to %s: %s\n", bt_ntoa(bdaddr, NULL), strerror(errno));
85 struct hostent *he = NULL;
87 if (!bt_aton(device, &bdaddr)) {
88 if ((he = bt_gethostbyname(device)) == NULL) {
89 smprintf(s,
"%s: %s\n", device, hstrerror(h_errno));
93 bdaddr_copy(&bdaddr, (bdaddr_t *)he->h_addr);
96 return bluetooth_open(s, &bdaddr, port);
99 #ifdef BLUETOOTH_RF_SEARCHING 101 static int bluetooth_channel(sdp_data_t *value)
106 sdp_get_alt(value, value);
108 while (sdp_get_seq(value, &pdl)) {
109 if (sdp_get_seq(&pdl, &seq)
110 && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
111 && sdp_get_seq(&pdl, &seq)
112 && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_RFCOMM)
113 && sdp_get_uint(&seq, &channel)
114 && channel >= 1 && channel <= 30)
121 static char *bluetooth_service(sdp_data_t *value)
126 if (!sdp_get_str(value, &str, &len))
129 return strndup(str, len);
134 sdp_data_t rec, rsp, ssp, value;
138 int ch, channel, sc, score;
141 smprintf(s,
"Searching for services on %s\n", bt_ntoa(bdaddr, NULL));
143 ss = sdp_open(NULL, bdaddr);
145 smprintf(s,
"SDP Connection failed: %s\n", strerror(errno));
150 ssp.end = buf +
sizeof(buf);
151 sdp_put_uuid16(&ssp, SDP_UUID_PROTOCOL_RFCOMM);
155 if (!sdp_service_search_attribute(ss, &ssp, NULL, &rsp)) {
156 smprintf(s,
"SDP Service Search Attribute failed: %s\n", strerror(errno));
164 while (sdp_get_seq(&rsp, &rec)) {
168 while (sdp_get_attr(&rec, &attr, &value)) {
170 case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
171 ch = bluetooth_channel(&value);
174 case SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET:
175 sv = bluetooth_service(&value);
186 sc = bluetooth_checkservicename(s, sv);
187 smprintf(s,
" - \"%s\" (score=%d)", sv, sc);
202 smprintf(s,
"No suitable service found!\n");
206 return bluetooth_open(s, bdaddr, channel);
214 struct bt_devinquiry *ii;
217 if (bt_aton(device, &bdaddr))
218 return bluetooth_search(s, &bdaddr);
220 if ((he = bt_gethostbyname(device)) != NULL)
221 return bluetooth_search(s, (bdaddr_t *)he->h_addr);
223 smprintf(s,
"Device \"%s\" unknown. Starting inquiry..\n", device);
225 if ((count = bt_devinquiry(NULL, 10, 20, &ii)) == -1) {
226 smprintf(s,
"Inquiry failed: %s\n", strerror(errno));
230 smprintf(s,
"Found %d device%s.\n", count, (count == 1 ?
"" :
"s"));
232 for (n = 0; n < count; n++) {
233 if (bluetooth_search(s, &ii[n].bdaddr) ==
ERR_NONE) {
GSM_Config * CurrentConfig
union GSM_Device::@0 Data
GSM_Error bluetooth_connect(GSM_StateMachine *s, int port, char *device)
GSM_Error bluetooth_findchannel(GSM_StateMachine *s)
int smprintf(GSM_StateMachine *s, const char *format,...)