raiden00pl commented on code in PR #8794: URL: https://github.com/apache/nuttx/pull/8794#discussion_r1134444603
########## 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: done for all bt driver ops -- 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