net: For Bluetooth sockets use the Linux sockaddr types

Signed-off-by: Brennan Ashton <bashton@brennanashton.com>
This commit is contained in:
Brennan Ashton 2020-08-29 12:28:52 -07:00 committed by patacongo
parent 1025456ffa
commit 797bf447d1
4 changed files with 87 additions and 106 deletions

View File

@ -1,35 +1,20 @@
/**************************************************************************** /****************************************************************************
* include/netpacket/bluetooth.h * include/netpacket/bluetooth.h
* *
* Copyright (C) 2018 Gregory Nutt. All rights reserved. * Licensed to the Apache Software Foundation (ASF) under one or more
* Author: Gregory Nutt <gnutt@nuttx.org> * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* *
* Redistribution and use in source and binary forms, with or without * http://www.apache.org/licenses/LICENSE-2.0
* modification, are permitted provided that the following conditions
* are met:
* *
* 1. Redistributions of source code must retain the above copyright * Unless required by applicable law or agreed to in writing, software
* notice, this list of conditions and the following disclaimer. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* 2. Redistributions in binary form must reproduce the above copyright * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* notice, this list of conditions and the following disclaimer in * License for the specific language governing permissions and limitations
* the documentation and/or other materials provided with the * under the License.
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
* *
****************************************************************************/ ****************************************************************************/
@ -75,7 +60,7 @@
* should be used. * should be used.
* BTPROTO_L2CAP * BTPROTO_L2CAP
* L2CAP sockets give sequential packet access over channels to other * L2CAP sockets give sequential packet access over channels to other
* Bluetooth devices and make use of the bt_psm field in the sockaddr_bt_s * Bluetooth devices and make use of the bt_psm field in the sockaddr_l2
* structure to select the Protocol/Service Multiplexer to specify when * structure to select the Protocol/Service Multiplexer to specify when
* making connections. If the special value of L2CAP_PSM_ANY is bound * making connections. If the special value of L2CAP_PSM_ANY is bound
* when the listen() call is made, the next available PSM from the * when the listen() call is made, the next available PSM from the
@ -83,16 +68,12 @@
* using the getsockname() call. * using the getsockname() call.
* BTPROTO_RFCOMM * BTPROTO_RFCOMM
* RFCOMM sockets provide streamed data over Bluetooth connection and * RFCOMM sockets provide streamed data over Bluetooth connection and
* make use of the bt_psm, and bt_channel fields in the sockaddr_bt_s * make use of the l2_cid field in the sockaddr_rc structure.
* structure. The channel number must be between 1 and 30 inclusive * The channel number must be between 1 and 30 inclusive except that if
* except that if the special value RFCOMM_CHANNEL_ANY is bound, when * the special value RFCOMM_CHANNEL_ANY is bound, when the listen() call
* the listen() call is made, the first unused channel for the relevant * is made, the first unused channel for the relevant bdaddr will be
* bdaddr will be allocated and may be discovered using the * allocated and may be discovered using the getsockname(2) call.
* getsockname(2) call. If no PSM is specified, a default value of
* L2CAP_PSM_RFCOMM (0x0003) will be used.
* *
* NOTE: All protocol values currently ignored. Only BTPROTO_L2CAP is
* supported by default.
*/ */
#define BTPROTO_L2CAP 0 #define BTPROTO_L2CAP 0
@ -217,13 +198,13 @@
* use */ * use */
#define BT_LE_CID_ATT 0x0004 /* Attribute Protocol */ #define BT_LE_CID_ATT 0x0004 /* Attribute Protocol */
#define BT_LE_CID_L2CAP 0x0005 /* Low Energy L2CAP Signaling channel */ #define BT_LE_CID_L2CAP 0x0005 /* Low Energy L2CAP Signaling channel */
#define BT_LE_CID_SMP 0x0006 /* Security Manager Protocol #define BT_LE_CID_SMP 0x0006 /* Security Manager Protocol */
/* 0x0007-0x001f Reserved for future /* 0x0007-0x001f Reserved for future */
/* 0x0020-0x003e Assigned Numbers /* 0x0020-0x003e Assigned Numbers */
/* 0x003f Reserved for future use */ /* 0x003f Reserved for future use */
/* 0x0040-0x007f Dynamically allocated /* 0x0040-0x007f Dynamically allocated
* using the L2CAP LE credit based * using the L2CAP LE credit based
* connection mechanism * connection mechanism */
/* Others reserved for future use */ /* Others reserved for future use */
/* Protocol and Service Multiplexers (PSMs) */ /* Protocol and Service Multiplexers (PSMs) */
@ -276,14 +257,29 @@
* sendto() - Send to specified remote address * sendto() - Send to specified remote address
* recvfrom()- Receive from indicated remote address. * recvfrom()- Receive from indicated remote address.
* *
* REVISIT: Some protocols would require a bt_psm field as well.
*/ */
struct sockaddr_bt_s struct sockaddr_l2
{ {
sa_family_t bt_family; /* Must be AF_BLUETOOTH */ sa_family_t l2_family; /* Must be AF_BLUETOOTH */
bt_addr_t bt_bdaddr; /* 6-byte Bluetooth address */ uint16_t l2_psm; /* Protocol Service Multiplexer (PSM) */
uint8_t bt_channel; /* Channel identifier (CID) */ bt_addr_t l2_bdaddr; /* 6-byte Bluetooth address */
uint16_t l2_cid; /* Channel identifier (CID) */
uint8_t l2_bdaddr_type; /* Bluetooth address type */
};
struct sockaddr_hci
{
sa_family_t hci_family; /* Must be AF_BLUETOOTH */
uint16_t hci_dev; /* Network Device ID */
uint16_t hci_channel; /* Channel: USER, MONITOR, CONTROL, etc... */
};
struct sockaddr_rc
{
sa_family_t rc_family; /* Must be AF_BLUETOOTH */
bt_addr_t rc_bdaddr; /* 6-byte Bluetooth address */
uint8_t rc_channel; /* Channel number */
}; };
/**************************************************************************** /****************************************************************************

View File

@ -110,7 +110,7 @@ static int bluetooth_count_frames(FAR struct bluetooth_conn_s *conn)
* Returned Value: * Returned Value:
* *
* Assumptions: * Assumptions:
* The network is lockec * The network is locked
* *
****************************************************************************/ ****************************************************************************/
@ -119,7 +119,7 @@ static ssize_t
FAR struct bluetooth_recvfrom_s *pstate) FAR struct bluetooth_recvfrom_s *pstate)
{ {
FAR struct bluetooth_container_s *container; FAR struct bluetooth_container_s *container;
FAR struct sockaddr_bt_s *iaddr; FAR struct sockaddr_l2 *iaddr;
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
FAR struct iob_s *iob; FAR struct iob_s *iob;
size_t copylen; size_t copylen;
@ -175,10 +175,10 @@ static ssize_t
if (pstate->ir_from != NULL) if (pstate->ir_from != NULL)
{ {
iaddr = (FAR struct sockaddr_bt_s *)pstate->ir_from; iaddr = (FAR struct sockaddr_l2 *)pstate->ir_from;
iaddr->bt_family = AF_BLUETOOTH; iaddr->l2_family = AF_BLUETOOTH;
BLUETOOTH_ADDRCOPY(&iaddr->bt_bdaddr, &container->bn_raddr); BLUETOOTH_ADDRCOPY(&iaddr->l2_bdaddr, &container->bn_raddr);
iaddr->bt_channel = container->bn_channel; iaddr->l2_cid = container->bn_channel;
} }
/* Free both the IOB and the container */ /* Free both the IOB and the container */
@ -319,7 +319,7 @@ ssize_t bluetooth_recvfrom(FAR struct socket *psock, FAR void *buf,
* enough to hold this address family. * enough to hold this address family.
*/ */
if (from != NULL && *fromlen < sizeof(struct sockaddr_bt_s)) if (from != NULL && *fromlen < sizeof(struct sockaddr_l2))
{ {
return -EINVAL; return -EINVAL;
} }

View File

@ -235,7 +235,7 @@ ssize_t psock_bluetooth_sendto(FAR struct socket *psock, FAR const void *buf,
FAR const struct sockaddr *to, FAR const struct sockaddr *to,
socklen_t tolen) socklen_t tolen)
{ {
FAR struct sockaddr_bt_s *destaddr; FAR struct sockaddr_l2 *destaddr;
FAR struct radio_driver_s *radio; FAR struct radio_driver_s *radio;
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
struct bluetooth_sendto_s state; struct bluetooth_sendto_s state;
@ -290,8 +290,8 @@ ssize_t psock_bluetooth_sendto(FAR struct socket *psock, FAR const void *buf,
/* Copy the destination address */ /* Copy the destination address */
destaddr = (FAR struct sockaddr_bt_s *)to; destaddr = (FAR struct sockaddr_l2 *)to;
memcpy(&state.is_destaddr, &destaddr->bt_bdaddr, memcpy(&state.is_destaddr, &destaddr->l2_bdaddr,
sizeof(bt_addr_t)); sizeof(bt_addr_t));
if (len > 0) if (len > 0)

View File

@ -1,35 +1,20 @@
/**************************************************************************** /****************************************************************************
* net/socket/bluetooth_sockif.c * net/socket/bluetooth_sockif.c
* *
* Copyright (C) 2018 Gregory Nutt. All rights reserved. * Licensed to the Apache Software Foundation (ASF) under one or more
* Author: Gregory Nutt <gnutt@nuttx.org> * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* *
* Redistribution and use in source and binary forms, with or without * http://www.apache.org/licenses/LICENSE-2.0
* modification, are permitted provided that the following conditions
* are met:
* *
* 1. Redistributions of source code must retain the above copyright * Unless required by applicable law or agreed to in writing, software
* notice, this list of conditions and the following disclaimer. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* 2. Redistributions in binary form must reproduce the above copyright * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* notice, this list of conditions and the following disclaimer in * License for the specific language governing permissions and limitations
* the documentation and/or other materials provided with the * under the License.
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
* *
****************************************************************************/ ****************************************************************************/
@ -274,7 +259,7 @@ static int bluetooth_connect(FAR struct socket *psock,
socklen_t addrlen) socklen_t addrlen)
{ {
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
FAR struct sockaddr_bt_s *btaddr; FAR struct sockaddr_l2 *btaddr;
int ret = OK; int ret = OK;
DEBUGASSERT(psock != NULL || addr != NULL); DEBUGASSERT(psock != NULL || addr != NULL);
@ -287,9 +272,9 @@ static int bluetooth_connect(FAR struct socket *psock,
{ {
/* Save the "connection" address */ /* Save the "connection" address */
btaddr = (FAR struct sockaddr_bt_s *)addr; btaddr = (FAR struct sockaddr_l2 *)addr;
memcpy(&conn->bc_raddr, &btaddr->bt_bdaddr, sizeof(bt_addr_t)); memcpy(&conn->bc_raddr, &btaddr->l2_bdaddr, sizeof(bt_addr_t));
conn->bc_channel = btaddr->bt_channel; conn->bc_channel = btaddr->l2_cid;
} }
else else
{ {
@ -379,7 +364,7 @@ static int bluetooth_accept(FAR struct socket *psock,
static int bluetooth_bind(FAR struct socket *psock, static int bluetooth_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen) FAR const struct sockaddr *addr, socklen_t addrlen)
{ {
FAR const struct sockaddr_bt_s *iaddr; FAR const struct sockaddr_l2 *iaddr;
FAR struct radio_driver_s *radio; FAR struct radio_driver_s *radio;
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
@ -388,10 +373,10 @@ static int bluetooth_bind(FAR struct socket *psock,
/* Verify that a valid address has been provided */ /* Verify that a valid address has been provided */
if (addr->sa_family != AF_BLUETOOTH || if (addr->sa_family != AF_BLUETOOTH ||
addrlen < sizeof(struct sockaddr_bt_s)) addrlen < sizeof(struct sockaddr_l2))
{ {
nerr("ERROR: Invalid family: %u or address length: %d < %d\n", nerr("ERROR: Invalid family: %u or address length: %d < %d\n",
addr->sa_family, addrlen, sizeof(struct sockaddr_bt_s)); addr->sa_family, addrlen, sizeof(struct sockaddr_l2));
return -EBADF; return -EBADF;
} }
@ -414,7 +399,7 @@ static int bluetooth_bind(FAR struct socket *psock,
return -EINVAL; return -EINVAL;
} }
iaddr = (FAR const struct sockaddr_bt_s *)addr; iaddr = (FAR const struct sockaddr_l2 *)addr;
/* Very that some address was provided. /* Very that some address was provided.
* *
@ -426,7 +411,7 @@ static int bluetooth_bind(FAR struct socket *psock,
/* Find the device associated with the requested address */ /* Find the device associated with the requested address */
radio = bluetooth_find_device(conn, &iaddr->bt_bdaddr); radio = bluetooth_find_device(conn, &iaddr->l2_bdaddr);
if (radio == NULL) if (radio == NULL)
{ {
nerr("ERROR: No radio at this address\n"); nerr("ERROR: No radio at this address\n");
@ -435,7 +420,7 @@ static int bluetooth_bind(FAR struct socket *psock,
/* Save the address as the socket's local address */ /* Save the address as the socket's local address */
memcpy(&conn->bc_laddr, &iaddr->bt_bdaddr, sizeof(bt_addr_t)); memcpy(&conn->bc_laddr, &iaddr->l2_bdaddr, sizeof(bt_addr_t));
return OK; return OK;
} }
@ -473,7 +458,7 @@ static int bluetooth_getsockname(FAR struct socket *psock,
socklen_t *addrlen) socklen_t *addrlen)
{ {
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
FAR struct sockaddr_bt_s tmp; FAR struct sockaddr_l2 tmp;
socklen_t copylen; socklen_t copylen;
DEBUGASSERT(psock != NULL && addr != NULL && addrlen != NULL); DEBUGASSERT(psock != NULL && addr != NULL && addrlen != NULL);
@ -483,12 +468,12 @@ static int bluetooth_getsockname(FAR struct socket *psock,
/* Create a copy of the full address on the stack */ /* Create a copy of the full address on the stack */
tmp.bt_family = AF_BLUETOOTH; tmp.l2_family = AF_BLUETOOTH;
memcpy(&tmp.bt_bdaddr, &conn->bc_laddr, sizeof(bt_addr_t)); memcpy(&tmp.l2_bdaddr, &conn->bc_laddr, sizeof(bt_addr_t));
/* Copy to the user buffer, truncating if necessary */ /* Copy to the user buffer, truncating if necessary */
copylen = sizeof(struct sockaddr_bt_s); copylen = sizeof(struct sockaddr_l2);
if (copylen > *addrlen) if (copylen > *addrlen)
{ {
copylen = *addrlen; copylen = *addrlen;
@ -535,7 +520,7 @@ static int bluetooth_getpeername(FAR struct socket *psock,
FAR socklen_t *addrlen) FAR socklen_t *addrlen)
{ {
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
FAR struct sockaddr_bt_s tmp; FAR struct sockaddr_l2 tmp;
socklen_t copylen; socklen_t copylen;
DEBUGASSERT(psock != NULL && addr != NULL && addrlen != NULL); DEBUGASSERT(psock != NULL && addr != NULL && addrlen != NULL);
@ -545,12 +530,12 @@ static int bluetooth_getpeername(FAR struct socket *psock,
/* Create a copy of the full address on the stack */ /* Create a copy of the full address on the stack */
tmp.bt_family = AF_BLUETOOTH; tmp.l2_family = AF_BLUETOOTH;
memcpy(&tmp.bt_bdaddr, &conn->bc_raddr, sizeof(bt_addr_t)); memcpy(&tmp.l2_bdaddr, &conn->bc_raddr, sizeof(bt_addr_t));
/* Copy to the user buffer, truncating if necessary */ /* Copy to the user buffer, truncating if necessary */
copylen = sizeof(struct sockaddr_bt_s); copylen = sizeof(struct sockaddr_l2);
if (copylen > *addrlen) if (copylen > *addrlen)
{ {
copylen = *addrlen; copylen = *addrlen;
@ -645,7 +630,7 @@ static int bluetooth_poll_local(FAR struct socket *psock,
static ssize_t bluetooth_send(FAR struct socket *psock, FAR const void *buf, static ssize_t bluetooth_send(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags) size_t len, int flags)
{ {
struct sockaddr_bt_s to; struct sockaddr_l2 to;
FAR struct bluetooth_conn_s *conn; FAR struct bluetooth_conn_s *conn;
ssize_t ret; ssize_t ret;
@ -665,15 +650,15 @@ static ssize_t bluetooth_send(FAR struct socket *psock, FAR const void *buf,
} }
else else
{ {
to.bt_family = AF_BLUETOOTH; to.l2_family = AF_BLUETOOTH;
memcpy(&to.bt_bdaddr, &conn->bc_raddr, sizeof(bt_addr_t)); memcpy(&to.l2_bdaddr, &conn->bc_raddr, sizeof(bt_addr_t));
to.bt_channel = conn->bc_channel; to.l2_cid = conn->bc_channel;
/* Then perform the send() as sendto() */ /* Then perform the send() as sendto() */
ret = psock_bluetooth_sendto(psock, buf, len, flags, ret = psock_bluetooth_sendto(psock, buf, len, flags,
(FAR const struct sockaddr *)&to, (FAR const struct sockaddr *)&to,
sizeof(struct sockaddr_bt_s)); sizeof(struct sockaddr_l2));
} }
} }
else else