/*-
* Copyright (c) 2008-2009 Iain Hibbert
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM,
&l2cap_mode, sizeof(l2cap_mode)) == -1) {
log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
exit(EXIT_FAILURE);
}
mru = BNEP_MTU_MIN;
if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU,
&mru, sizeof(mru)) == -1) {
log_err("Could not set L2CAP IMTU (%d): %m", mru);
exit(EXIT_FAILURE);
}
log_info("Opening connection to service 0x%4.4x at %s",
service_class, bt_ntoa(&remote_bdaddr, NULL));
sa.bt_psm = l2cap_psm;
bdaddr_copy(&sa.bt_bdaddr, &remote_bdaddr);
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
log_err("Could not connect: %m");
exit(EXIT_FAILURE);
}
len = sizeof(mru);
if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
log_err("Could not get IMTU: %m");
exit(EXIT_FAILURE);
}
if (mru < BNEP_MTU_MIN) {
log_err("L2CAP IMTU too small (%d)", mru);
exit(EXIT_FAILURE);
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
log_err("Could not read SO_RCVBUF");
exit(EXIT_FAILURE);
}
if (n < 10 * mru) {
n = 10 * mru;
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
log_info("Could not increase SO_RCVBUF (to %d)", n);
}
len = sizeof(mtu);
if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
log_err("Could not get L2CAP OMTU: %m");
exit(EXIT_FAILURE);
}
if (mtu < BNEP_MTU_MIN) {
log_err("L2CAP OMTU too small (%d)", mtu);
exit(EXIT_FAILURE);
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
log_err("Could not get socket send buffer size: %m");
close(fd);
return;
}
if (n < (mtu * 2)) {
n = mtu * 2;
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
log_err("Could not set socket send buffer size (%d): %m", n);
close(fd);
return;
}
}
n = mtu;
if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
log_err("Could not set socket low water mark (%d): %m", n);
close(fd);
return;
}
chan = channel_alloc();
if (chan == NULL)
exit(EXIT_FAILURE);
/*
* we expect the response to contain a list of records
* containing a ProtocolDescriptorList. Find the first
* one containing L2CAP and BNEP protocols and extract
* the PSM.
*/
rv = false;
while (!rv && sdp_get_seq(&rsp, &rec)) {
if (!sdp_get_attr(&rec, &attr, &value)
|| attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
continue;
sdp_get_alt(&value, &value); /* drop any alt header */
while (!rv && sdp_get_seq(&value, &pdl)) {
if (sdp_get_seq(&pdl, &seq)
&& sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
&& sdp_get_uint(&seq, &psm)
&& sdp_get_seq(&pdl, &seq)
&& sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP))
rv = true;
}
}
sdp_close(ss);
if (!rv) {
log_err("%s query failed", service_type);
exit(EXIT_FAILURE);
}
l2cap_psm = (uint16_t)psm;
log_info("Found PSM %u for service %s", l2cap_psm, service_type);
}