2018-04-01 04:55:03 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* net/bluetooth/bluetooth_sockif.c
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
2024-09-11 20:39:39 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-08-30 03:28:52 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <socket/socket.h>
|
|
|
|
#include <netpacket/bluetooth.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/radiodev.h>
|
|
|
|
#include <nuttx/net/bluetooth.h>
|
2018-04-30 02:43:09 +08:00
|
|
|
#include <nuttx/wireless/bluetooth/bt_hci.h>
|
2018-04-01 04:55:03 +08:00
|
|
|
|
|
|
|
#include "bluetooth/bluetooth.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_BLUETOOTH
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-07 01:55:44 +08:00
|
|
|
static int bluetooth_setup(FAR struct socket *psock);
|
2018-04-01 04:55:03 +08:00
|
|
|
static sockcaps_t bluetooth_sockcaps(FAR struct socket *psock);
|
|
|
|
static void bluetooth_addref(FAR struct socket *psock);
|
|
|
|
static int bluetooth_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen);
|
|
|
|
static int bluetooth_getsockname(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr, FAR socklen_t *addrlen);
|
2018-07-20 00:30:52 +08:00
|
|
|
static int bluetooth_getpeername(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr, FAR socklen_t *addrlen);
|
2018-04-01 04:55:03 +08:00
|
|
|
static int bluetooth_connect(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen);
|
|
|
|
static int bluetooth_close(FAR struct socket *psock);
|
|
|
|
|
2020-08-29 05:51:26 +08:00
|
|
|
/* Protocol Specific Interfaces */
|
|
|
|
|
|
|
|
static int bluetooth_l2cap_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr_l2 *addr, socklen_t addrlen);
|
|
|
|
static int bluetooth_hci_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr_hci *addr,
|
|
|
|
socklen_t addrlen);
|
|
|
|
|
2018-04-01 04:55:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const struct sock_intf_s g_bluetooth_sockif =
|
|
|
|
{
|
|
|
|
bluetooth_setup, /* si_setup */
|
|
|
|
bluetooth_sockcaps, /* si_sockcaps */
|
|
|
|
bluetooth_addref, /* si_addref */
|
|
|
|
bluetooth_bind, /* si_bind */
|
|
|
|
bluetooth_getsockname, /* si_getsockname */
|
2018-07-19 21:16:30 +08:00
|
|
|
bluetooth_getpeername, /* si_getpeername */
|
2023-03-06 13:08:24 +08:00
|
|
|
NULL, /* si_listen */
|
2018-04-01 04:55:03 +08:00
|
|
|
bluetooth_connect, /* si_connect */
|
2023-03-06 13:16:26 +08:00
|
|
|
NULL, /* si_accept */
|
2023-03-06 13:24:00 +08:00
|
|
|
NULL, /* si_poll */
|
2020-12-01 14:55:16 +08:00
|
|
|
bluetooth_sendmsg, /* si_sendmsg */
|
|
|
|
bluetooth_recvmsg, /* si_recvmsg */
|
2018-04-01 04:55:03 +08:00
|
|
|
bluetooth_close /* si_close */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_sockif_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate and attach a PF_BLUETOOTH connection structure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_sockif_alloc(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
/* Allocate the packet socket connection structure and save in the new
|
|
|
|
* socket instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct bluetooth_conn_s *conn = bluetooth_conn_alloc();
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to reserve a connection structure */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the reference count on the connection structure. This reference
|
|
|
|
* count will be incremented only if the socket is dup'ed
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->bc_crefs == 0);
|
|
|
|
conn->bc_crefs = 1;
|
|
|
|
|
|
|
|
/* Save the pre-allocated connection in the socket structure */
|
|
|
|
|
|
|
|
psock->s_conn = conn;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called for socket() to verify that the provided socket type and
|
|
|
|
* protocol are usable by this address family. Perform any family-
|
|
|
|
* specific socket fields.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock A pointer to a user allocated socket structure to be
|
|
|
|
* initialized.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-14 20:55:45 +08:00
|
|
|
* Zero (OK) is returned on success. Otherwise, a negated errno value is
|
2018-04-01 04:55:03 +08:00
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-07 01:55:44 +08:00
|
|
|
static int bluetooth_setup(FAR struct socket *psock)
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
|
|
|
/* Allocate the appropriate connection structure. This reserves the
|
2021-01-15 17:01:00 +08:00
|
|
|
* connection structure, it is unallocated at this point. It will not
|
|
|
|
* actually be initialized until the socket is connected.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
2023-01-18 13:42:42 +08:00
|
|
|
* SOCK_RAW and SOCK_CTRL are supported
|
2018-04-01 04:55:03 +08:00
|
|
|
*/
|
|
|
|
|
2023-01-18 13:42:42 +08:00
|
|
|
if (psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL)
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
|
|
|
return bluetooth_sockif_alloc(psock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_sockcaps
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the bit encoded capabilities of this socket.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - Socket structure of the socket whose capabilities are being
|
|
|
|
* queried.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2023-02-17 19:26:18 +08:00
|
|
|
* The set of socket capabilities is returned.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static sockcaps_t bluetooth_sockcaps(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_addref
|
|
|
|
*
|
|
|
|
* Description:
|
2020-02-23 02:31:14 +08:00
|
|
|
* Increment the reference count on the underlying connection structure.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void bluetooth_addref(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
|
|
|
|
2023-08-30 14:46:41 +08:00
|
|
|
DEBUGASSERT(psock->s_type == SOCK_RAW || psock->s_type == SOCK_CTRL);
|
2018-04-01 04:55:03 +08:00
|
|
|
|
2023-05-08 22:11:04 +08:00
|
|
|
conn = psock->s_conn;
|
2018-04-01 04:55:03 +08:00
|
|
|
DEBUGASSERT(conn->bc_crefs > 0 && conn->bc_crefs < 255);
|
|
|
|
conn->bc_crefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_connect
|
|
|
|
*
|
|
|
|
* Description:
|
2020-06-15 16:23:25 +08:00
|
|
|
* bluetooth_connect() connects the local socket referred to by the
|
|
|
|
* structure 'psock' to the address specified by 'addr'. The addrlen
|
|
|
|
* argument specifies the size of 'addr'. The format of the address in
|
|
|
|
* 'addr' is determined by the address space of the socket 'psock'.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
|
|
|
* Generally, connection-based protocol sockets may successfully
|
|
|
|
* bluetooth_connect() only once; connectionless protocol sockets may use
|
|
|
|
* bluetooth_connect() multiple times to change their association.
|
|
|
|
* Connectionless sockets may dissolve the association by connecting to
|
2018-04-01 06:31:51 +08:00
|
|
|
* an address with the bt_family member of sockaddr set to AF_UNSPEC.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Pointer to a socket structure initialized by psock_socket()
|
|
|
|
* addr Server address (form depends on type of socket)
|
|
|
|
* addrlen Length of actual 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-02-23 16:50:23 +08:00
|
|
|
* 0 on success; a negated errno value on failure. See connect() for the
|
2018-04-01 04:55:03 +08:00
|
|
|
* list of appropriate errno values to be returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_connect(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
2020-08-30 03:28:52 +08:00
|
|
|
FAR struct sockaddr_l2 *btaddr;
|
2020-01-31 17:34:28 +08:00
|
|
|
int ret = OK;
|
2018-04-01 04:55:03 +08:00
|
|
|
|
2023-05-08 22:11:04 +08:00
|
|
|
conn = psock->s_conn;
|
2018-04-01 04:55:03 +08:00
|
|
|
|
|
|
|
/* Verify the address family */
|
|
|
|
|
|
|
|
if (addr->sa_family == AF_BLUETOOTH)
|
|
|
|
{
|
2020-08-29 05:51:26 +08:00
|
|
|
/* Verify the Protocol */
|
|
|
|
|
|
|
|
if (psock->s_proto != BTPROTO_L2CAP)
|
|
|
|
{
|
|
|
|
return -EPFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2018-04-01 04:55:03 +08:00
|
|
|
/* Save the "connection" address */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
btaddr = (FAR struct sockaddr_l2 *)addr;
|
|
|
|
memcpy(&conn->bc_raddr, &btaddr->l2_bdaddr, sizeof(bt_addr_t));
|
|
|
|
conn->bc_channel = btaddr->l2_cid;
|
2020-09-21 03:45:09 +08:00
|
|
|
conn->bc_proto = psock->s_proto;
|
2018-04-01 04:55:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The specified address is not a valid address for the address family
|
|
|
|
* of the specified socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* bluetooth_bind() gives the socket 'psock' the local address 'addr'.
|
|
|
|
* 'addr' is 'addrlen' bytes long. Traditionally, this is called
|
|
|
|
* "assigning a name to a socket." When a socket is created with
|
|
|
|
* socket(), it exists in a name space (address family) but has no name
|
|
|
|
* assigned.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of the socket to bind
|
|
|
|
* addr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; A negated errno value is returned on failure. See
|
|
|
|
* bind() for a list a appropriate error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_bind(FAR struct socket *psock,
|
2020-06-15 16:23:25 +08:00
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen)
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
2020-08-29 05:51:26 +08:00
|
|
|
if (addr->sa_family != AF_BLUETOOTH)
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
2020-08-29 05:51:26 +08:00
|
|
|
nerr("ERROR: Invalid family: %u\n", addr->sa_family);
|
2018-04-01 04:55:03 +08:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2020-08-29 05:51:26 +08:00
|
|
|
switch (psock->s_proto)
|
|
|
|
{
|
|
|
|
case BTPROTO_L2CAP:
|
|
|
|
{
|
|
|
|
FAR const struct sockaddr_l2 *iaddr;
|
|
|
|
if (addrlen < sizeof(struct sockaddr_l2))
|
|
|
|
{
|
2020-11-23 15:46:47 +08:00
|
|
|
nerr("ERROR: Invalid address length: %zu < %zu\n",
|
|
|
|
(size_t)addrlen, sizeof(struct sockaddr_l2));
|
2020-08-29 05:51:26 +08:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
iaddr = (FAR const struct sockaddr_l2 *)addr;
|
|
|
|
return bluetooth_l2cap_bind(psock, iaddr, addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
case BTPROTO_HCI:
|
|
|
|
{
|
|
|
|
FAR const struct sockaddr_hci *hciaddr;
|
|
|
|
if (addrlen < sizeof(struct sockaddr_hci))
|
|
|
|
{
|
2020-11-23 15:46:47 +08:00
|
|
|
nerr("ERROR: Invalid address length: %zu < %zu\n",
|
|
|
|
(size_t)addrlen, sizeof(struct sockaddr_hci));
|
2020-08-29 05:51:26 +08:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
hciaddr = (FAR const struct sockaddr_hci *)addr;
|
|
|
|
return bluetooth_hci_bind(psock, hciaddr, addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EPFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_l2cap_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* bluetooth_bind() gives the socket 'psock' the local address 'iaddr'.
|
|
|
|
* 'iaddr' is 'addrlen' bytes long. Traditionally, this is called
|
|
|
|
* "assigning a name to a socket." When a socket is created with
|
|
|
|
* socket(), it exists in a name space (address family) but has no name
|
|
|
|
* assigned.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of the socket to bind
|
|
|
|
* iaddr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; A negated errno value is returned on failure. See
|
|
|
|
* bind() for a list a appropriate error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_l2cap_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr_l2 *iaddr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
FAR struct radio_driver_s *radio;
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
|
|
|
|
2018-04-01 06:31:51 +08:00
|
|
|
/* Bind a PF_BLUETOOTH socket to an network device.
|
|
|
|
*
|
2023-01-18 13:42:42 +08:00
|
|
|
* SOCK_RAW and SOCK_CTRL are supported
|
2018-04-01 06:31:51 +08:00
|
|
|
*/
|
2018-04-01 04:55:03 +08:00
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
if (psock == NULL || psock->s_conn == NULL ||
|
2023-01-18 13:42:42 +08:00
|
|
|
(psock->s_type != SOCK_RAW && psock->s_type != SOCK_CTRL))
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket type: %u\n", psock->s_type);
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:11:04 +08:00
|
|
|
conn = psock->s_conn;
|
2022-02-08 13:18:01 +08:00
|
|
|
|
2018-04-01 04:55:03 +08:00
|
|
|
/* Verify that the socket is not already bound. */
|
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
if (_SS_ISBOUND(conn->bc_conn.s_flags))
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
|
|
|
nerr("ERROR: Already bound\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-03-12 02:48:17 +08:00
|
|
|
/* Very that some address was provided.
|
|
|
|
*
|
2020-02-23 16:50:23 +08:00
|
|
|
* REVISIT: Currently and explicit address must be assigned. Should we
|
2018-04-01 04:55:03 +08:00
|
|
|
* support some moral equivalent to INADDR_ANY?
|
|
|
|
*/
|
|
|
|
|
2020-09-21 03:45:09 +08:00
|
|
|
conn->bc_proto = psock->s_proto;
|
|
|
|
|
2018-04-01 04:55:03 +08:00
|
|
|
/* Find the device associated with the requested address */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
radio = bluetooth_find_device(conn, &iaddr->l2_bdaddr);
|
2018-04-01 04:55:03 +08:00
|
|
|
if (radio == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: No radio at this address\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the address as the socket's local address */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
memcpy(&conn->bc_laddr, &iaddr->l2_bdaddr, sizeof(bt_addr_t));
|
2018-04-01 04:55:03 +08:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-08-29 05:51:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_l2cap_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* bluetooth_bind() gives the socket 'psock' the local address 'hciaddr'.
|
|
|
|
* 'hciaddr' is 'addrlen' bytes long. Traditionally, this is called
|
|
|
|
* "assigning a name to a socket." When a socket is created with
|
|
|
|
* socket(), it exists in a name space (address family) but has no name
|
|
|
|
* assigned.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of the socket to bind
|
|
|
|
* hciaddr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; A negated errno value is returned on failure. See
|
|
|
|
* bind() for a list a appropriate error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_hci_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr_hci *hciaddr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
|
|
|
|
|
|
|
/* Bind a PF_BLUETOOTH socket to an network device.
|
|
|
|
*
|
2023-01-18 13:42:42 +08:00
|
|
|
* SOCK_RAW and SOCK_CTRL are supported
|
2020-08-29 05:51:26 +08:00
|
|
|
*/
|
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
if (psock == NULL || psock->s_conn == NULL ||
|
2023-01-18 13:42:42 +08:00
|
|
|
(psock->s_type != SOCK_RAW && psock->s_type != SOCK_CTRL))
|
2020-08-29 05:51:26 +08:00
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket type: %u\n", psock->s_type);
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:11:04 +08:00
|
|
|
conn = psock->s_conn;
|
2022-02-08 13:18:01 +08:00
|
|
|
|
2020-08-29 05:51:26 +08:00
|
|
|
/* Verify that the socket is not already bound. */
|
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
if (_SS_ISBOUND(conn->bc_conn.s_flags))
|
2020-08-29 05:51:26 +08:00
|
|
|
{
|
|
|
|
nerr("ERROR: Already bound\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-21 03:45:09 +08:00
|
|
|
conn->bc_proto = psock->s_proto;
|
2020-08-29 05:51:26 +08:00
|
|
|
conn->bc_channel = hciaddr->hci_channel;
|
|
|
|
conn->bc_ldev = hciaddr->hci_dev;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-04-01 04:55:03 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_getsockname
|
|
|
|
*
|
|
|
|
* Description:
|
2020-06-15 16:23:25 +08:00
|
|
|
* The bluetooth_getsockname() function retrieves the locally-bound name of
|
|
|
|
* the specified packet socket, stores this address in the sockaddr
|
|
|
|
* structure pointed to by the 'addr' argument, and stores the length of
|
|
|
|
* this address in the object pointed to by the 'addrlen' argument.
|
2018-04-01 04:55:03 +08:00
|
|
|
*
|
|
|
|
* If the actual length of the address is greater than the length of the
|
|
|
|
* supplied sockaddr structure, the stored address will be truncated.
|
|
|
|
*
|
|
|
|
* If the socket has not been bound to a local name, the value stored in
|
|
|
|
* the object pointed to by address is unspecified.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of the socket to be queried
|
|
|
|
* addr sockaddr structure to receive data [out]
|
|
|
|
* addrlen Length of sockaddr structure [in/out]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, 0 is returned, the 'addr' argument points to the address
|
|
|
|
* of the socket, and the 'addrlen' argument points to the length of the
|
|
|
|
* address. Otherwise, a negated errno value is returned. See
|
|
|
|
* getsockname() for the list of appropriate error numbers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_getsockname(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr, FAR
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
2020-08-30 03:28:52 +08:00
|
|
|
FAR struct sockaddr_l2 tmp;
|
2018-04-01 04:55:03 +08:00
|
|
|
socklen_t copylen;
|
|
|
|
|
2023-05-08 22:11:04 +08:00
|
|
|
conn = psock->s_conn;
|
2018-04-01 04:55:03 +08:00
|
|
|
|
|
|
|
/* Create a copy of the full address on the stack */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
tmp.l2_family = AF_BLUETOOTH;
|
|
|
|
memcpy(&tmp.l2_bdaddr, &conn->bc_laddr, sizeof(bt_addr_t));
|
2018-04-01 04:55:03 +08:00
|
|
|
|
|
|
|
/* Copy to the user buffer, truncating if necessary */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
copylen = sizeof(struct sockaddr_l2);
|
2018-04-01 04:55:03 +08:00
|
|
|
if (copylen > *addrlen)
|
|
|
|
{
|
|
|
|
copylen = *addrlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, &tmp, copylen);
|
|
|
|
|
|
|
|
/* Return the actual size transferred */
|
2018-07-19 21:16:30 +08:00
|
|
|
|
|
|
|
*addrlen = copylen;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_getpeername
|
|
|
|
*
|
|
|
|
* Description:
|
2020-06-15 16:23:25 +08:00
|
|
|
* The bluetooth_getpeername() function retrieves the remote-connected name
|
|
|
|
* of the specified local socket, stores this address in the sockaddr
|
2018-07-19 21:16:30 +08:00
|
|
|
* structure pointed to by the 'addr' argument, and stores the length of
|
|
|
|
* this address in the object pointed to by the 'addrlen' argument.
|
|
|
|
*
|
|
|
|
* If the actual length of the address is greater than the length of the
|
|
|
|
* supplied sockaddr structure, the stored address will be truncated.
|
|
|
|
*
|
|
|
|
* If the socket has not been bound to a local name, the value stored in
|
|
|
|
* the object pointed to by address is unspecified.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock Socket structure of the socket to be queried
|
|
|
|
* addr sockaddr structure to receive data [out]
|
|
|
|
* addrlen Length of sockaddr structure [in/out]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, 0 is returned, the 'addr' argument points to the address
|
|
|
|
* of the socket, and the 'addrlen' argument points to the length of the
|
|
|
|
* address. Otherwise, a negated errno value is returned. See
|
|
|
|
* getpeername() for the list of appropriate error numbers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_getpeername(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
2020-08-30 03:28:52 +08:00
|
|
|
FAR struct sockaddr_l2 tmp;
|
2018-07-19 21:16:30 +08:00
|
|
|
socklen_t copylen;
|
|
|
|
|
2020-08-29 05:51:26 +08:00
|
|
|
if (psock->s_proto != BTPROTO_L2CAP)
|
|
|
|
{
|
|
|
|
return -EPFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:11:04 +08:00
|
|
|
conn = psock->s_conn;
|
2018-07-19 21:16:30 +08:00
|
|
|
|
|
|
|
/* Create a copy of the full address on the stack */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
tmp.l2_family = AF_BLUETOOTH;
|
|
|
|
memcpy(&tmp.l2_bdaddr, &conn->bc_raddr, sizeof(bt_addr_t));
|
2018-07-19 21:16:30 +08:00
|
|
|
|
|
|
|
/* Copy to the user buffer, truncating if necessary */
|
|
|
|
|
2020-08-30 03:28:52 +08:00
|
|
|
copylen = sizeof(struct sockaddr_l2);
|
2018-07-19 21:16:30 +08:00
|
|
|
if (copylen > *addrlen)
|
|
|
|
{
|
|
|
|
copylen = *addrlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, &tmp, copylen);
|
|
|
|
|
|
|
|
/* Return the actual size transferred */
|
2018-04-01 04:55:03 +08:00
|
|
|
|
|
|
|
*addrlen = copylen;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on a PF_BLUETOOTH socket instance
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
/* Perform some pre-close operations for the PF_BLUETOOTH address type */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
2023-01-18 13:42:42 +08:00
|
|
|
/* SOCK_RAW and SOCK_CTRL are supported */
|
2018-04-01 06:31:51 +08:00
|
|
|
|
|
|
|
case SOCK_RAW:
|
2023-01-18 13:42:42 +08:00
|
|
|
case SOCK_CTRL:
|
2018-04-01 04:55:03 +08:00
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure (there
|
|
|
|
* could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->bc_crefs <= 1)
|
|
|
|
{
|
|
|
|
/* Yes... free the connection structure */
|
|
|
|
|
|
|
|
conn->bc_crefs = 0; /* No more references on the connection */
|
|
|
|
bluetooth_conn_free(psock->s_conn); /* Free network resources */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
|
|
|
conn->bc_crefs--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_BLUETOOTH */
|