21 #include "../../gsmstate.h" 23 #ifdef GSM_ENABLE_BLUETOOTHDEVICE 31 #include <sys/socket.h> 35 #include <bluetooth/rfcomm.h> 36 #include <bluetooth/sdp.h> 37 #include <bluetooth/sdp_lib.h> 38 #include <bluetooth/hci_lib.h> 40 #include "../../gsmcomon.h" 41 #include "../devfunc.h" 49 for (tries = 0; tries < 5; tries++) {
51 struct sockaddr_rc laddr, raddr;
59 memset(&laddr, 0,
sizeof(laddr));
60 memset(&raddr, 0,
sizeof(raddr));
62 smprintf(s,
"Connecting to RF channel %i\n", port);
64 fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
66 smprintf(s,
"Can't create socket\n");
70 bacpy(&laddr.rc_bdaddr, BDADDR_ANY);
71 laddr.rc_family = AF_BLUETOOTH;
74 if (bind(fd, (
struct sockaddr *)&laddr,
sizeof(laddr)) < 0) {
75 smprintf(s,
"Can't bind socket (%d, %s)\n", errno, strerror(errno));
80 str2ba(device, &bdaddr);
81 bacpy(&raddr.rc_bdaddr, &bdaddr);
82 raddr.rc_family = AF_BLUETOOTH;
83 raddr.rc_channel = port;
85 if (connect(fd, (
struct sockaddr *)&raddr,
sizeof(raddr)) < 0) {
86 smprintf(s,
"Can't connect (%d, %s)\n", errno, strerror(errno));
98 #ifdef BLUETOOTH_RF_SEARCHING 102 sdp_session_t *sess = 0;
104 sdp_list_t *attrid, *search, *seq, *next, *proto;
107 uint32_t range = 0x0000ffff;
111 int score, bestscore = 0;
117 smprintf(s,
"Looking for suitable channel for PHONET\n");
119 smprintf(s,
"Looking for suitable channel for OBEX\n");
121 smprintf(s,
"Looking for suitable channel for AT\n");
124 bacpy(&interface,BDADDR_ANY);
130 dd = hci_open_dev(0);
132 memset(name, 0,
sizeof(name));
133 if (hci_read_remote_name(dd, bdaddr,
sizeof(name), name, 100000) >= 0) {
145 sess = sdp_connect(&interface, bdaddr, SDP_RETRY_IF_BUSY);
148 if (errno == EALREADY && retries < 5) {
149 smprintf(s,
"Operation already in progress, retrying.\n");
159 smprintf(s,
"Failed to connect to SDP server on %s: %s\n", str, strerror(errno));
164 attrid = sdp_list_append(0, &range);
165 search = sdp_list_append(0, group);
166 if (sdp_service_search_attr_req(sess, search, SDP_ATTR_REQ_RANGE, attrid, &seq)) {
167 smprintf(s,
"Service Search failed: %s\n", strerror(errno));
171 sdp_list_free(attrid, 0);
172 sdp_list_free(search, 0);
174 for (; seq; seq = next) {
175 rec = (sdp_record_t *) seq->data;
178 if (sdp_get_access_protos(rec, &proto) == 0) {
179 channel = sdp_get_proto_port(proto, RFCOMM_UUID);
180 sdp_list_foreach(proto, (sdp_list_func_t)sdp_list_free, 0);
181 sdp_list_free(proto, 0);
185 smprintf(s,
" Channel %i", channel);
188 d = sdp_data_get(rec, SDP_ATTR_SVCNAME_PRIMARY);
190 score = bluetooth_checkservicename(s, d->val.str);
191 smprintf(s,
" - \"%s\" (score=%d)\n", d->val.str, score);
192 if (score > bestscore) {
201 memset(&subgroup, 0,
sizeof(subgroup));
202 if (sdp_get_group_id(rec, &subgroup) != -1) {
203 if (subgroup.value.uuid16 != group->value.uuid16) {
204 bluetooth_checkdevice(s, bdaddr, &subgroup);
212 sdp_record_free(rec);
220 smprintf(s,
"No suitable bluetooth channel found!\n");
230 struct hci_dev_info hci_info;
233 memset(&group, 0,
sizeof(group));
235 sdp_uuid16_create(&group, RFCOMM_UUID);
240 smprintf(s,
"Searching for devices\n");
241 if (sdp_general_inquiry(ii, 20, 8, &count) < 0) {
248 for (i=0;i<count;i++) {
249 error = bluetooth_checkdevice(s,&ii[i].bdaddr,&group);
GSM_Config * CurrentConfig
union GSM_Device::@0 Data
GSM_ConnectionType ConnectionType
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,...)