xiaoxiang781216 commented on a change in pull request #1655: URL: https://github.com/apache/incubator-nuttx/pull/1655#discussion_r478474115
########## File path: arch/sim/src/sim/up_hcisocket_host.c ########## @@ -0,0 +1,181 @@ +/**************************************************************************** + * arch/sim/src/sim/up_hcisocket.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#include <sys/uio.h> +#include <sys/socket.h> +#include <sys/ioctl.h> + +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "up_internal.h" +#include "up_hcisocket_host.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BTPROTO_HCI 1 + +#define HCI_CHANNEL_RAW 0 +#define HCI_CHANNEL_USER 1 + +#define HCIDEVDOWN 0x400448ca + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct sockaddr_hci +{ + sa_family_t hci_family; + unsigned short hci_dev; + unsigned short hci_channel; +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: bthcisock_host_send + * + * Description: + * Send a Bluetooth packet out via the host user socket. + * + * Input Parameters: + * fd: Host Bluetooth socket fd + * pkt_type: Packet type as known to the Linux Bluetooth stack + * data: Pointer to the HCI packet + * len: Length of packet + * + * Returned Value: + * Zero is returned on success; a negated errno value is returned on any + * failure. + * + ****************************************************************************/ + +int bthcisock_host_send(int fd, uint8_t pkt_type, uint8_t *data, size_t len) +{ + struct iovec iv[2]; + + iv[0].iov_base = &pkt_type; + iv[0].iov_len = 1; + iv[1].iov_base = data; + iv[1].iov_len = len; + + while (writev(fd, iv, 2) < 0) + { + if (errno == EAGAIN || errno == EINTR) + continue; + return -1; + } + + return 0; +} + +/**************************************************************************** + * Name: bthcisock_host_read + * + * Description: + * Read from the Host HCI socket interface. + * + * Input Parameters: + * fd: Host Bluetooth socket fd + * data: Pointer to store HCI packet + * len: Maximum length of packet + * + * Returned Value: + * Zero is returned on success; a negated errno value is returned on any + * failure. + * + ****************************************************************************/ + +int bthcisock_host_read(int fd, uint8_t *type, void *buf, size_t len) +{ + int err; + while ((err = recv(fd, buf, len, 0)) < 0 && (errno == EINTR)); Review comment: > That's actually a good idea since we don't need to worry about flags anymore. Unfortunately I can't quite avoid the temporary buffer because the allocation function requires knowing the type, but the length will be more sane since it wont have the extra pad. > bt_buf_alloc doesn't really use type execpt saving into bt_buf_s::type field, so we can pass a fixed type and overwrite it after bthcisock_host_read return. Actually, I am thinking to add an enum to bt_buf_type_e(e.g. BT_UNKOWN) to indicate this special case. ########## File path: arch/sim/src/sim/up_hcisocket_host.c ########## @@ -0,0 +1,181 @@ +/**************************************************************************** + * arch/sim/src/sim/up_hcisocket.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <sys/types.h> +#include <sys/uio.h> +#include <sys/socket.h> +#include <sys/ioctl.h> + +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "up_internal.h" +#include "up_hcisocket_host.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define BTPROTO_HCI 1 + +#define HCI_CHANNEL_RAW 0 +#define HCI_CHANNEL_USER 1 + +#define HCIDEVDOWN 0x400448ca + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct sockaddr_hci +{ + sa_family_t hci_family; + unsigned short hci_dev; + unsigned short hci_channel; +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: bthcisock_host_send + * + * Description: + * Send a Bluetooth packet out via the host user socket. + * + * Input Parameters: + * fd: Host Bluetooth socket fd + * pkt_type: Packet type as known to the Linux Bluetooth stack + * data: Pointer to the HCI packet + * len: Length of packet + * + * Returned Value: + * Zero is returned on success; a negated errno value is returned on any + * failure. + * + ****************************************************************************/ + +int bthcisock_host_send(int fd, uint8_t pkt_type, uint8_t *data, size_t len) +{ + struct iovec iv[2]; + + iv[0].iov_base = &pkt_type; + iv[0].iov_len = 1; + iv[1].iov_base = data; + iv[1].iov_len = len; + + while (writev(fd, iv, 2) < 0) + { + if (errno == EAGAIN || errno == EINTR) + continue; + return -1; + } + + return 0; +} + +/**************************************************************************** + * Name: bthcisock_host_read + * + * Description: + * Read from the Host HCI socket interface. + * + * Input Parameters: + * fd: Host Bluetooth socket fd + * data: Pointer to store HCI packet + * len: Maximum length of packet + * + * Returned Value: + * Zero is returned on success; a negated errno value is returned on any + * failure. + * + ****************************************************************************/ + +int bthcisock_host_read(int fd, uint8_t *type, void *buf, size_t len) +{ + int err; + while ((err = recv(fd, buf, len, 0)) < 0 && (errno == EINTR)); Review comment: > That's actually a good idea since we don't need to worry about flags anymore. Unfortunately I can't quite avoid the temporary buffer because the allocation function requires knowing the type, but the length will be more sane since it wont have the extra pad. > bt_buf_alloc doesn't really use type execpt saving into bt_buf_s::type field, so we can pass a fixed type and overwrite it after bthcisock_host_read return. Actually, I am thinking to add an enum to bt_buf_type_e(e.g. BT_UNKOWN) to indicate this special case. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
