xiaoxiang781216 commented on code in PR #8794: URL: https://github.com/apache/nuttx/pull/8794#discussion_r1133159882
########## drivers/wireless/bluetooth/bt_rpmsghci_server.c: ########## @@ -0,0 +1,539 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci_server.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> + +#include <nuttx/rptun/openamp.h> +#include <nuttx/wireless/bluetooth/bt_rpmsghci.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_server_s +{ + struct rpmsg_endpoint ept; + FAR struct bt_driver_s *btdev; + FAR const char *name; + struct work_s work; Review Comment: remove work? ########## drivers/wireless/bluetooth/bt_rpmsghci.c: ########## @@ -0,0 +1,535 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> +#include <stdio.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/net/bluetooth.h> +#include <nuttx/semaphore.h> +#include <nuttx/rptun/openamp.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_s +{ + struct rpmsg_endpoint ept; + struct bt_driver_s btdev; + FAR const char *cpuname; + FAR const char *name; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_recv_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const rpmsg_ept_cb g_rpmsghci_handler[] = +{ + rpmsghci_default_handler, /* RPMSGHCI_OPEN */ + rpmsghci_default_handler, /* RPMSGHCI_CLOSE */ + rpmsghci_default_handler, /* RPMSGHCI_SEND */ + rpmsghci_default_handler, /* RPMSGHCI_IOCTL */ + rpmsghci_recv_handler, /* RPMSGHCI_RECV */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_get_tx_payload_buffer + ****************************************************************************/ + +static FAR void * +rpmsghci_get_tx_payload_buffer(FAR struct rpmsghci_s *priv, + FAR uint32_t *len) +{ + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsghci_send + * + * Description: + * Send the rpmsg data and wait for ACK. + * + * Parameters: + * priv - rpmsg device handle + * command - the command, RPMSGHCI_OPEN, RPMSGHCI_CLOSE, RPMSGHCI_SEND, + * RPMSGHCI_IOCTL + * msg - the message header + * len - length of the payload + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send(FAR struct rpmsghci_s *priv, uint32_t command, + FAR struct rpmsghci_header_s *msg, int len) +{ + struct rpmsghci_cookie_s cookie; + int ret = OK; + + memset(&cookie, 0, sizeof(struct rpmsghci_cookie_s)); + nxsem_init(&cookie.sem, 0, 0); + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)&cookie; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + goto errout; + } + + ret = rpmsg_wait(&priv->ept, &cookie.sem); + if (ret >= 0) + { + ret = msg->result; + } + +errout: + nxsem_destroy(&cookie.sem); + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_open + * + * Description: + * Rpmsg-HCI open operation + * + * Parameters: + * btdev - the bt instance + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_open(struct bt_driver_s *btdev) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_open_s *msg = NULL; + uint32_t space = 0; + int ret = OK; + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + ret = -ENOMEM; + goto errout; + } + + ret = rpmsghci_send(priv, RPMSGHCI_OPEN, &msg->header, + sizeof(struct rpmsghci_open_s)); +errout: + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_close + * + * Description: + * Rpmsg-HCI close operation + * + * Parameters: + * btdev - the bt instance + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static void bt_rpmsghci_close(struct bt_driver_s *btdev) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_close_s *msg = NULL; + uint32_t space = 0; + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + return; + } + + rpmsghci_send(priv, RPMSGHCI_CLOSE, &msg->header, + sizeof(struct rpmsghci_close_s)); +} + +/**************************************************************************** + * Name: bt_rpmsghci_send + * + * Description: + * Rpmsg-HCI send operation + * + * Parameters: + * btdev - the bt instance + * type - bt command type + * data - bt command data + * len - bt command data length + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_send(struct bt_driver_s *btdev, + enum bt_buf_type_e type, + void *data, size_t len) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_data_s *msg = NULL; + uint32_t space = 0; + int ret = OK; + + wlinfo("bt_rpmsghci_send %d\n", type); + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + if (data == NULL) + { + ret = -EINVAL; + goto errout; + } + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + ret = -ENOMEM; + goto errout; + } + + DEBUGASSERT(sizeof(struct rpmsghci_data_s) - 1 + len <= space); + memcpy(msg->data, data, len); + msg->type = type; + ret = rpmsghci_send(priv, RPMSGHCI_SEND, &msg->header, + sizeof(struct rpmsghci_data_s) -1 + len); + +errout: + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_ioctl + * + * Description: + * Rpmsg-HCI ioctl operation + * + * Parameters: + * btdev - the bt instance + * cmd - the ioctl command + * arg - the ioctl arguments + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_ioctl(FAR struct bt_driver_s *btdev, int cmd, + unsigned long arg) +{ + /* TODO */ + + return -ENOTTY; +} + +/**************************************************************************** + * Name: rpmsghci_default_handler + * + * Description: + * Default rpmsg-HCI response handler, this function will be called to + * process the return message of rpmsghci_open(), rpmsghci_close(), + * rpmsghci_send(), rpmsghci_ioctl(). + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * Always OK + * + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_header_s *header = data; + FAR struct rpmsghci_cookie_s *cookie = NULL; + + if (header->cookie != 0) + { + cookie = (FAR struct rpmsghci_cookie_s *)(uintptr_t)header->cookie; + return rpmsg_post(ept, &cookie->sem); + } + + return 0; +} + +/**************************************************************************** + * Name: rpmsghci_recv_handler + * + * Description: + * Rpmsg-HCI receive handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_recv_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_s *client = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + client = priv; + + wlinfo("rpmsghci_recv_handler %d\n", msg->type); + + ret = bt_netdev_receive(&client->btdev, msg->type, msg->data, + len - sizeof(struct rpmsghci_data_s) + 1); + msg->header.result = ret; + + return rpmsg_send(ept, msg, len); Review Comment: Please reference how alarm is handled in rpmsg_rtc: https://github.com/apache/nuttx/blob/master/drivers/timers/rpmsg_rtc.c#L610-L619 Actually, no response is the OpenAMP default behaviour: call rpmsg_send and skip rpmsg_wait. ########## drivers/wireless/bluetooth/bt_rpmsghci_server.c: ########## @@ -0,0 +1,539 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci_server.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> + +#include <nuttx/rptun/openamp.h> +#include <nuttx/wireless/bluetooth/bt_rpmsghci.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_server_s +{ + struct rpmsg_endpoint ept; + FAR struct bt_driver_s *btdev; + FAR const char *name; + struct work_s work; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsghci_open_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_close_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_send_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_ioctl_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const rpmsg_ept_cb g_rpmsghci_handler[] = +{ + rpmsghci_open_handler, /* RPMSGHCI_OPEN */ + rpmsghci_close_handler, /* RPMSGHCI_CLOSE */ + rpmsghci_send_handler, /* RPMSGHCI_SEND */ + rpmsghci_ioctl_handler, /* RPMSGHCI_IOCTL */ + rpmsghci_default_handler, /* RPMSGHCI_RECV */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_get_tx_payload_buffer + ****************************************************************************/ + +static FAR void * +rpmsghci_get_tx_payload_buffer(FAR struct rpmsghci_server_s *priv, + FAR uint32_t *len) +{ + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsghci_send + * + * Description: + * Send the rpmsg data and wait for ACK. + * + * Parameters: + * priv - rpmsg device handle + * command - the command, RPMSGHCI_OPEN, RPMSGHCI_CLOSE, RPMSGHCI_SEND, + * RPMSGHCI_IOCTL + * msg - the message header + * len - length of the payload + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send(FAR struct rpmsghci_server_s *priv, + uint32_t command, FAR struct rpmsghci_header_s *msg, + int len) +{ + struct rpmsghci_cookie_s cookie; + int ret = OK; + + memset(&cookie, 0, sizeof(struct rpmsghci_cookie_s)); + nxsem_init(&cookie.sem, 0, 0); + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)&cookie; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + goto errout; + } + + ret = rpmsg_wait(&priv->ept, &cookie.sem); + if (ret >= 0) + { + ret = msg->result; + } + +errout: + nxsem_destroy(&cookie.sem); + return ret; +} + +/**************************************************************************** + * Name: rpmsghci_open_handler + * + * Description: + * Rpmsg-HCI device open handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_open_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + if (btdev->open) + { + ret = btdev->open(btdev); + } + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_close_handler + * + * Description: + * Rpmsg-HCI close handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_close_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + if (btdev->close) + { + btdev->close(btdev); + } + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_send_handler + * + * Description: + * Rpmsg-HCI send handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + wlinfo("rpmsghci_send_handler %d\n", msg->type); + + ret = btdev->send(btdev, msg->type, msg->data, + len - sizeof(struct rpmsghci_data_s) + 1); + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_ioctl_handler + * + * Description: + * Rpmsg-HCI ioctl handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_ioctl_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_ioctl_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + ret = btdev->ioctl(btdev, msg->request, + msg->arglen > 0 ? (unsigned long)msg->buf : + msg->arg); + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_default_handler + * + * Description: + * Default rpmsg-HCI response handler, this function will be called to + * process the return message of rpmsghci_recv() + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * Always OK + * + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, Review Comment: remove? ########## drivers/wireless/bluetooth/bt_rpmsghci_server.c: ########## @@ -0,0 +1,539 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci_server.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> + +#include <nuttx/rptun/openamp.h> +#include <nuttx/wireless/bluetooth/bt_rpmsghci.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_server_s +{ + struct rpmsg_endpoint ept; + FAR struct bt_driver_s *btdev; + FAR const char *name; + struct work_s work; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsghci_open_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_close_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_send_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_ioctl_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const rpmsg_ept_cb g_rpmsghci_handler[] = +{ + rpmsghci_open_handler, /* RPMSGHCI_OPEN */ + rpmsghci_close_handler, /* RPMSGHCI_CLOSE */ + rpmsghci_send_handler, /* RPMSGHCI_SEND */ + rpmsghci_ioctl_handler, /* RPMSGHCI_IOCTL */ + rpmsghci_default_handler, /* RPMSGHCI_RECV */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_get_tx_payload_buffer + ****************************************************************************/ + +static FAR void * +rpmsghci_get_tx_payload_buffer(FAR struct rpmsghci_server_s *priv, + FAR uint32_t *len) +{ + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsghci_send + * + * Description: + * Send the rpmsg data and wait for ACK. + * + * Parameters: + * priv - rpmsg device handle + * command - the command, RPMSGHCI_OPEN, RPMSGHCI_CLOSE, RPMSGHCI_SEND, + * RPMSGHCI_IOCTL + * msg - the message header + * len - length of the payload + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send(FAR struct rpmsghci_server_s *priv, + uint32_t command, FAR struct rpmsghci_header_s *msg, + int len) +{ + struct rpmsghci_cookie_s cookie; + int ret = OK; + + memset(&cookie, 0, sizeof(struct rpmsghci_cookie_s)); + nxsem_init(&cookie.sem, 0, 0); + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)&cookie; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + goto errout; + } + + ret = rpmsg_wait(&priv->ept, &cookie.sem); + if (ret >= 0) + { + ret = msg->result; + } + +errout: + nxsem_destroy(&cookie.sem); + return ret; +} + +/**************************************************************************** + * Name: rpmsghci_open_handler + * + * Description: + * Rpmsg-HCI device open handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_open_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + if (btdev->open) + { + ret = btdev->open(btdev); + } + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_close_handler + * + * Description: + * Rpmsg-HCI close handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_close_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + if (btdev->close) + { + btdev->close(btdev); + } + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_send_handler + * + * Description: + * Rpmsg-HCI send handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + wlinfo("rpmsghci_send_handler %d\n", msg->type); + + ret = btdev->send(btdev, msg->type, msg->data, + len - sizeof(struct rpmsghci_data_s) + 1); + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_ioctl_handler + * + * Description: + * Rpmsg-HCI ioctl handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_ioctl_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_server_s *server = NULL; + FAR struct bt_driver_s *btdev = NULL; + FAR struct rpmsghci_ioctl_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + server = priv; + DEBUGASSERT(server->btdev); + btdev = server->btdev; + + ret = btdev->ioctl(btdev, msg->request, + msg->arglen > 0 ? (unsigned long)msg->buf : + msg->arg); + + msg->header.result = ret; + + return rpmsg_send(ept, msg, sizeof(struct rpmsghci_header_s)); +} + +/**************************************************************************** + * Name: rpmsghci_default_handler + * + * Description: + * Default rpmsg-HCI response handler, this function will be called to + * process the return message of rpmsghci_recv() + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * Always OK + * + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_header_s *header = data; + FAR struct rpmsghci_cookie_s *cookie = NULL; + + if (header->cookie != 0) + { + cookie = (FAR struct rpmsghci_cookie_s *)(uintptr_t)header->cookie; + return rpmsg_post(ept, &cookie->sem); + } + + return 0; +} + +/**************************************************************************** + * Name: rpmsghci_ns_match + * + * Description: + * Check if we match to the rpmsg name service. + * + ****************************************************************************/ + +static bool rpmsghci_ns_match(FAR struct rpmsg_device *rdev, FAR void *priv, + FAR const char *name, uint32_t dest) +{ + return !strncmp(name, RPMSGHCI_NAME_PREFIX, RPMSGHCI_NAME_PREFIX_LEN); +} + +/**************************************************************************** + * Name: rpmsghci_ept_cb + * + * Description: + * Rpmsg HCI end point callback function, this function will be called + * when receive the client cpu message. + * + * Parameters: + * ept - The rpmsg-HCI end point + * data - The received data + * len - The received data length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_ept_cb(FAR struct rpmsg_endpoint *ept, FAR void *data, + size_t len, uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_header_s *header = data; + uint32_t command = header->command; + + if (command < nitems(g_rpmsghci_handler)) + { + return g_rpmsghci_handler[command](ept, data, len, src, priv); + } + + return -EINVAL; +} + +/**************************************************************************** + * Name: rpmsghci_ns_unbind + * + * Description: + * Unbind from the rpmsg name service. + * + ****************************************************************************/ + +static void rpmsghci_ns_unbind(FAR struct rpmsg_endpoint *ept) +{ + rpmsg_destroy_ept(ept); + kmm_free(ept); +} + +/**************************************************************************** + * Name: rpmsghci_ns_bind + * + * Description: + * Bind to the rpmsg name service. + * + ****************************************************************************/ + +static void rpmsghci_ns_bind(FAR struct rpmsg_device *rdev, FAR void *priv, + FAR const char *name, uint32_t dest) +{ + FAR struct rpmsghci_server_s *server = priv; + + server->ept.priv = priv; + rpmsg_create_ept(&server->ept, rdev, name, + RPMSG_ADDR_ANY, dest, + rpmsghci_ept_cb, rpmsghci_ns_unbind); +} + +/**************************************************************************** + * Name: rpmsghci_bt_receive + * + * Description: + * Called by the Bluetooth low-level driver when new data is received from + * the radio. Transfers data to the RPMSG client. + * + * This callback should not be called from the interrupt context ! + * + * Returned Value: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +int rpmsghci_bt_receive(FAR struct bt_driver_s *btdev, + enum bt_buf_type_e type, FAR void *data, size_t len) +{ + FAR struct rpmsghci_server_s *priv = NULL; + FAR struct rpmsghci_data_s *msg = NULL; + uint32_t space = 0; + int ret = OK; + + wlinfo("rpmsghci_bt_receive %d\n", type); + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->priv; + + if (data == NULL) + { + return -EINVAL; + } + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + return -ENOMEM; + } + + DEBUGASSERT(sizeof(struct rpmsghci_data_s) - 1 + len <= space); + memcpy(msg->data, data, len); + msg->type = type; + return rpmsghci_send(priv, RPMSGHCI_RECV, &msg->header, Review Comment: let's call rpmsg_send without sem_wait directly ########## drivers/wireless/bluetooth/bt_rpmsghci_server.c: ########## @@ -0,0 +1,547 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci_server.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> + +#include <nuttx/rptun/openamp.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_server_s +{ + struct rpmsg_endpoint ept; + FAR struct bt_driver_s *btdev; + FAR const char *name; + struct work_s work; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsghci_open_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_close_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_send_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_ioctl_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const rpmsg_ept_cb g_rpmsghci_handler[] = +{ + rpmsghci_open_handler, /* RPMSGHCI_OPEN */ + rpmsghci_close_handler, /* RPMSGHCI_CLOSE */ + rpmsghci_send_handler, /* RPMSGHCI_SEND */ + rpmsghci_ioctl_handler, /* RPMSGHCI_IOCTL */ + rpmsghci_default_handler, /* RPMSGHCI_RECV */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_get_tx_payload_buffer + ****************************************************************************/ + +static FAR void * +rpmsghci_get_tx_payload_buffer(FAR struct rpmsghci_server_s *priv, + FAR uint32_t *len) +{ + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsghci_send + * + * Description: + * Send the rpmsg data and wait for ACK. + * + * Parameters: + * priv - rpmsg device handle + * command - the command, RPMSGHCI_OPEN, RPMSGHCI_CLOSE, RPMSGHCI_SEND, + * RPMSGHCI_IOCTL + * msg - the message header + * len - length of the payload + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send(FAR struct rpmsghci_server_s *priv, + uint32_t command, FAR struct rpmsghci_header_s *msg, + int len) +{ + struct rpmsghci_cookie_s cookie; + int ret = OK; + + memset(&cookie, 0, sizeof(struct rpmsghci_cookie_s)); + nxsem_init(&cookie.sem, 0, 0); + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)&cookie; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + goto errout; + } + + ret = rpmsg_wait(&priv->ept, &cookie.sem); Review Comment: it's better to call rpmsg_send in rpmsghci_bt_receive directly like rpmsghci_xxx_handler, and remove rpmsghci_default_handler ########## drivers/wireless/bluetooth/bt_rpmsghci.h: ########## @@ -0,0 +1,95 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __DRIVERS_WIRELESS_BLUETOOTH_BT_RPMSGHCI_H +#define __DRIVERS_WIRELESS_BLUETOOTH_BT_RPMSGHCI_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/compiler.h> + +#include <nuttx/wireless/bluetooth/bt_driver.h> Review Comment: it's better to use [u]intxx_t instead of enum in IPC, since the enum may map to the different size when the arch, toolchain or compiler option change. ########## drivers/wireless/bluetooth/bt_rpmsghci.c: ########## @@ -0,0 +1,535 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> +#include <stdio.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/net/bluetooth.h> +#include <nuttx/semaphore.h> +#include <nuttx/rptun/openamp.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_s +{ + struct rpmsg_endpoint ept; + struct bt_driver_s btdev; + FAR const char *cpuname; + FAR const char *name; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_recv_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const rpmsg_ept_cb g_rpmsghci_handler[] = +{ + rpmsghci_default_handler, /* RPMSGHCI_OPEN */ + rpmsghci_default_handler, /* RPMSGHCI_CLOSE */ + rpmsghci_default_handler, /* RPMSGHCI_SEND */ + rpmsghci_default_handler, /* RPMSGHCI_IOCTL */ + rpmsghci_recv_handler, /* RPMSGHCI_RECV */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_get_tx_payload_buffer + ****************************************************************************/ + +static FAR void * +rpmsghci_get_tx_payload_buffer(FAR struct rpmsghci_s *priv, + FAR uint32_t *len) +{ + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsghci_send + * + * Description: + * Send the rpmsg data and wait for ACK. + * + * Parameters: + * priv - rpmsg device handle + * command - the command, RPMSGHCI_OPEN, RPMSGHCI_CLOSE, RPMSGHCI_SEND, + * RPMSGHCI_IOCTL + * msg - the message header + * len - length of the payload + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send(FAR struct rpmsghci_s *priv, uint32_t command, + FAR struct rpmsghci_header_s *msg, int len) +{ + struct rpmsghci_cookie_s cookie; + int ret = OK; + + memset(&cookie, 0, sizeof(struct rpmsghci_cookie_s)); + nxsem_init(&cookie.sem, 0, 0); + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)&cookie; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + goto errout; + } + + ret = rpmsg_wait(&priv->ept, &cookie.sem); + if (ret >= 0) + { + ret = msg->result; + } + +errout: + nxsem_destroy(&cookie.sem); + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_open + * + * Description: + * Rpmsg-HCI open operation + * + * Parameters: + * btdev - the bt instance + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_open(struct bt_driver_s *btdev) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_open_s *msg = NULL; + uint32_t space = 0; + int ret = OK; + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + ret = -ENOMEM; + goto errout; + } + + ret = rpmsghci_send(priv, RPMSGHCI_OPEN, &msg->header, + sizeof(struct rpmsghci_open_s)); +errout: + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_close + * + * Description: + * Rpmsg-HCI close operation + * + * Parameters: + * btdev - the bt instance + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static void bt_rpmsghci_close(struct bt_driver_s *btdev) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_close_s *msg = NULL; + uint32_t space = 0; + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + return; + } + + rpmsghci_send(priv, RPMSGHCI_CLOSE, &msg->header, + sizeof(struct rpmsghci_close_s)); +} + +/**************************************************************************** + * Name: bt_rpmsghci_send + * + * Description: + * Rpmsg-HCI send operation + * + * Parameters: + * btdev - the bt instance + * type - bt command type + * data - bt command data + * len - bt command data length + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_send(struct bt_driver_s *btdev, + enum bt_buf_type_e type, + void *data, size_t len) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_data_s *msg = NULL; + uint32_t space = 0; + int ret = OK; + + wlinfo("bt_rpmsghci_send %d\n", type); + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + if (data == NULL) + { + ret = -EINVAL; + goto errout; + } + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + ret = -ENOMEM; + goto errout; + } + + DEBUGASSERT(sizeof(struct rpmsghci_data_s) - 1 + len <= space); + memcpy(msg->data, data, len); + msg->type = type; + ret = rpmsghci_send(priv, RPMSGHCI_SEND, &msg->header, + sizeof(struct rpmsghci_data_s) -1 + len); + +errout: + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_ioctl + * + * Description: + * Rpmsg-HCI ioctl operation + * + * Parameters: + * btdev - the bt instance + * cmd - the ioctl command + * arg - the ioctl arguments + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_ioctl(FAR struct bt_driver_s *btdev, int cmd, + unsigned long arg) +{ + /* TODO */ + + return -ENOTTY; +} + +/**************************************************************************** + * Name: rpmsghci_default_handler + * + * Description: + * Default rpmsg-HCI response handler, this function will be called to + * process the return message of rpmsghci_open(), rpmsghci_close(), + * rpmsghci_send(), rpmsghci_ioctl(). + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * Always OK + * + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_header_s *header = data; + FAR struct rpmsghci_cookie_s *cookie = NULL; + + if (header->cookie != 0) + { + cookie = (FAR struct rpmsghci_cookie_s *)(uintptr_t)header->cookie; + return rpmsg_post(ept, &cookie->sem); + } + + return 0; +} + +/**************************************************************************** + * Name: rpmsghci_recv_handler + * + * Description: + * Rpmsg-HCI receive handler. + * + * Parameters: + * ept - The rpmsg endpoint + * data - The return message + * len - The return message length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_recv_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_s *client = NULL; + FAR struct rpmsghci_data_s *msg = data; + int ret = OK; + + DEBUGASSERT(priv); + client = priv; + + wlinfo("rpmsghci_recv_handler %d\n", msg->type); + + ret = bt_netdev_receive(&client->btdev, msg->type, msg->data, + len - sizeof(struct rpmsghci_data_s) + 1); + msg->header.result = ret; + + return rpmsg_send(ept, msg, len); +} + +/**************************************************************************** + * Name: bt_rpmsghci_client_ept_cb + * + * Description: + * Rpmsg HCI end point callback function, this function will be called + * when receive the remote cpu message. + * + * Parameters: + * ept - The rpmsg-HCI end point + * data - The received data + * len - The received data length + * src - unknow + * priv - unknow + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_client_ept_cb(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv) +{ + FAR struct rpmsghci_header_s *header = data; + uint32_t command = header->command; + + if (command < nitems(g_rpmsghci_handler)) + { + return g_rpmsghci_handler[command](ept, data, len, src, priv); + } + + return -EINVAL; +} + +/**************************************************************************** + * Name: bt_rpmsghci_client_created + * + * Description: + * Rpmsg HCI create function, this function will be called by rptun to + * create a rpmsg-hci end point. + * + * Parameters: + * rdev - The rpmsg-hci end point + * priv_ - Rpmsg-hci handle + * + * Returned Values: + * None + * + ****************************************************************************/ + +static void bt_rpmsghci_client_created(FAR struct rpmsg_device *rdev, + FAR void *priv_) +{ + FAR struct rpmsghci_s *priv = priv_; + char eptname[RPMSG_NAME_SIZE]; + + if (strcmp(priv->cpuname, rpmsg_get_cpuname(rdev)) == 0) + { + snprintf(eptname, RPMSG_NAME_SIZE, "%s%s", RPMSGHCI_NAME_PREFIX, + priv->name); + priv->ept.priv = priv; + rpmsg_create_ept(&priv->ept, rdev, eptname, + RPMSG_ADDR_ANY, RPMSG_ADDR_ANY, + bt_rpmsghci_client_ept_cb, NULL); + } +} + +/**************************************************************************** + * Name: bt_rpmsghci_client_destroy + * + * Description: + * Rpmsg HCI destroy function, this function will be called by rptun to + * destroy rpmsg-HCI end point. + * + * Parameters: + * rdev - The rpmsg-HCI end point + * priv_ - Rpmsg-HCI handle + * + * Returned Values: + * None + * + ****************************************************************************/ + +static void bt_rpmsghci_client_destroy(FAR struct rpmsg_device *rdev, + FAR void *priv_) +{ + FAR struct rpmsghci_s *priv = priv_; + + if (!strcmp(priv->cpuname, rpmsg_get_cpuname(rdev))) + { + rpmsg_destroy_ept(&priv->ept); + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_register + * + * Description: + * Rpmsg-HCI client initialize function, the client cpu should call + * this function in the board initialize process. + * + * Parameters: + * cpuname - the server cpu name + * name - the HCI device you want to access in the remote cpu + * + * Returned Values: + * A non-NULL handle is returned on success; + * A NULL is returned on any failure. + * + ****************************************************************************/ + +FAR struct bt_driver_s *rpmsghci_register(FAR const char *cpuname, + FAR const char *name) +{ + FAR struct rpmsghci_s *priv = NULL; + + /* Validate input */ + + if (!cpuname || !name) + { + goto errout; + } + + /* Create RPMSG-HCI driver */ + + priv = kmm_zalloc(sizeof(struct rpmsghci_s)); + if (!priv) + { + goto errout; + } + + /* Initialize BT driver */ + + priv->btdev.head_reserve = 0; + priv->btdev.open = bt_rpmsghci_open; + priv->btdev.close = bt_rpmsghci_close; + priv->btdev.send = bt_rpmsghci_send; + priv->btdev.ioctl = bt_rpmsghci_ioctl; + priv->btdev.rpmsg = priv; Review Comment: If so, it's better to move `btdev` field to the first of `rpmsghci_s` and cast `bt_driver_s *` back to `rpmsgchi_s *`, instead adding `void *rpsmsg` to `bt_driver_s`. ########## drivers/wireless/bluetooth/bt_rpmsghci.c: ########## @@ -0,0 +1,535 @@ +/**************************************************************************** + * drivers/wireless/bluetooth/bt_rpmsghci.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/param.h> + +#include <assert.h> +#include <debug.h> +#include <stdlib.h> +#include <stdio.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/net/bluetooth.h> +#include <nuttx/semaphore.h> +#include <nuttx/rptun/openamp.h> + +#include "bt_rpmsghci.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rpmsghci_s +{ + struct rpmsg_endpoint ept; + struct bt_driver_s btdev; + FAR const char *cpuname; + FAR const char *name; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rpmsghci_default_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); +static int rpmsghci_recv_handler(FAR struct rpmsg_endpoint *ept, + FAR void *data, size_t len, + uint32_t src, FAR void *priv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const rpmsg_ept_cb g_rpmsghci_handler[] = +{ + rpmsghci_default_handler, /* RPMSGHCI_OPEN */ + rpmsghci_default_handler, /* RPMSGHCI_CLOSE */ + rpmsghci_default_handler, /* RPMSGHCI_SEND */ + rpmsghci_default_handler, /* RPMSGHCI_IOCTL */ + rpmsghci_recv_handler, /* RPMSGHCI_RECV */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rpmsghci_get_tx_payload_buffer + ****************************************************************************/ + +static FAR void * +rpmsghci_get_tx_payload_buffer(FAR struct rpmsghci_s *priv, + FAR uint32_t *len) +{ + return rpmsg_get_tx_payload_buffer(&priv->ept, len, true); +} + +/**************************************************************************** + * Name: rpmsghci_send + * + * Description: + * Send the rpmsg data and wait for ACK. + * + * Parameters: + * priv - rpmsg device handle + * command - the command, RPMSGHCI_OPEN, RPMSGHCI_CLOSE, RPMSGHCI_SEND, + * RPMSGHCI_IOCTL + * msg - the message header + * len - length of the payload + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int rpmsghci_send(FAR struct rpmsghci_s *priv, uint32_t command, + FAR struct rpmsghci_header_s *msg, int len) +{ + struct rpmsghci_cookie_s cookie; + int ret = OK; + + memset(&cookie, 0, sizeof(struct rpmsghci_cookie_s)); + nxsem_init(&cookie.sem, 0, 0); + + msg->command = command; + msg->result = -ENXIO; + msg->cookie = (uintptr_t)&cookie; + + ret = rpmsg_send_nocopy(&priv->ept, msg, len); + if (ret < 0) + { + goto errout; + } + + ret = rpmsg_wait(&priv->ept, &cookie.sem); + if (ret >= 0) + { + ret = msg->result; + } + +errout: + nxsem_destroy(&cookie.sem); + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_open + * + * Description: + * Rpmsg-HCI open operation + * + * Parameters: + * btdev - the bt instance + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_open(struct bt_driver_s *btdev) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_open_s *msg = NULL; + uint32_t space = 0; + int ret = OK; + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + ret = -ENOMEM; + goto errout; + } + + ret = rpmsghci_send(priv, RPMSGHCI_OPEN, &msg->header, + sizeof(struct rpmsghci_open_s)); +errout: + return ret; +} + +/**************************************************************************** + * Name: bt_rpmsghci_close + * + * Description: + * Rpmsg-HCI close operation + * + * Parameters: + * btdev - the bt instance + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static void bt_rpmsghci_close(struct bt_driver_s *btdev) +{ + FAR struct rpmsghci_s *priv = NULL; + FAR struct rpmsghci_close_s *msg = NULL; + uint32_t space = 0; + + /* Get RPMSG-HCI data */ + + DEBUGASSERT(btdev != NULL); + priv = btdev->rpmsg; + + /* Perform the rpmsg write */ + + msg = rpmsghci_get_tx_payload_buffer(priv, &space); + if (msg == NULL) + { + return; + } + + rpmsghci_send(priv, RPMSGHCI_CLOSE, &msg->header, + sizeof(struct rpmsghci_close_s)); +} + +/**************************************************************************** + * Name: bt_rpmsghci_send + * + * Description: + * Rpmsg-HCI send operation + * + * Parameters: + * btdev - the bt instance + * type - bt command type + * data - bt command data + * len - bt command data length + * + * Returned Values: + * OK on success; A negated errno value is returned on any failure. + * + ****************************************************************************/ + +static int bt_rpmsghci_send(struct bt_driver_s *btdev, Review Comment: if so, let's change to rpmsghci_bt_ -- 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. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org