bluetooth_sendto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /****************************************************************************
  2. * net/bluetooth/bluetooth_sendto.c
  3. *
  4. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <string.h>
  44. #include <errno.h>
  45. #include <assert.h>
  46. #include <debug.h>
  47. #include <netpacket/bluetooth.h>
  48. #include <arch/irq.h>
  49. #include <nuttx/clock.h>
  50. #include <nuttx/semaphore.h>
  51. #include <nuttx/mm/iob.h>
  52. #include <nuttx/net/radiodev.h>
  53. #include <nuttx/net/bluetooth.h>
  54. #include <nuttx/net/net.h>
  55. #include <nuttx/net/ip.h>
  56. #include <nuttx/wireless/bluetooth/bt_hci.h>
  57. #include "utils/utils.h"
  58. #include "netdev/netdev.h"
  59. #include "devif/devif.h"
  60. #include "socket/socket.h"
  61. #include "bluetooth/bluetooth.h"
  62. #ifdef CONFIG_NET_BLUETOOTH
  63. /****************************************************************************
  64. * Private Types
  65. ****************************************************************************/
  66. /* This structure holds the state of the send operation until it can be
  67. * operated upon from the event handler.
  68. */
  69. struct bluetooth_sendto_s
  70. {
  71. FAR struct socket *is_sock; /* Points to the parent socket structure */
  72. FAR struct devif_callback_s *is_cb; /* Reference to callback instance */
  73. bt_addr_t is_destaddr; /* Frame destination address */
  74. uint8_t is_channel; /* Frame destination channel */
  75. sem_t is_sem; /* Used to wake up the waiting thread */
  76. FAR const uint8_t *is_buffer; /* User buffer of data to send */
  77. size_t is_buflen; /* Number of bytes in the is_buffer */
  78. ssize_t is_sent; /* The number of bytes sent (or error) */
  79. };
  80. /****************************************************************************
  81. * Private Functions
  82. ****************************************************************************/
  83. /****************************************************************************
  84. * Name: bluetooth_sendto_eventhandler
  85. ****************************************************************************/
  86. static uint16_t bluetooth_sendto_eventhandler(FAR struct net_driver_s *dev,
  87. FAR void *pvconn,
  88. FAR void *pvpriv,
  89. uint16_t flags)
  90. {
  91. FAR struct radio_driver_s *radio;
  92. FAR struct bluetooth_sendto_s *pstate;
  93. struct bluetooth_frame_meta_s meta;
  94. FAR struct iob_s *iob;
  95. int hdrlen;
  96. int ret;
  97. DEBUGASSERT(pvpriv != NULL && dev != NULL && pvconn != NULL);
  98. /* Ignore polls from non Bluetooth network drivers */
  99. if (dev->d_lltype != NET_LL_BLUETOOTH)
  100. {
  101. return flags;
  102. }
  103. /* Make sure that this is the driver to which the socket is connected. */
  104. #warning Missing logic
  105. pstate = (FAR struct bluetooth_sendto_s *)pvpriv;
  106. radio = (FAR struct radio_driver_s *)dev;
  107. ninfo("flags: %04x sent: %d\n", flags, pstate->is_sent);
  108. if (pstate != NULL && (flags & BLUETOOTH_POLL) != 0)
  109. {
  110. /* Initialize the meta data */
  111. BLUETOOTH_ADDRCOPY(&meta.bm_raddr, &pstate->is_destaddr);
  112. meta.bm_channel = pstate->is_channel;
  113. /* Get the Bluetooth MAC header length */
  114. hdrlen = radio->r_get_mhrlen(radio, &meta);
  115. if (hdrlen < 0)
  116. {
  117. nerr("ERROR: Failed to get header length: %d\n", hdrlen);
  118. ret = hdrlen;
  119. goto errout;
  120. }
  121. /* Verify that the user buffer can fit within the frame with this
  122. * MAC header.
  123. */
  124. DEBUGASSERT(BLUETOOTH_MAX_FRAMELEN <= CONFIG_IOB_BUFSIZE);
  125. if (pstate->is_buflen + hdrlen > BLUETOOTH_MAX_FRAMELEN)
  126. {
  127. nerr("ERROR: User buffer will not fit into the frame: %u > %u\n",
  128. (unsigned int)(pstate->is_buflen + hdrlen),
  129. (unsigned int)CONFIG_IOB_BUFSIZE);
  130. ret = -E2BIG;
  131. goto errout;
  132. }
  133. /* Allocate an IOB to hold the frame data */
  134. iob = net_ioballoc(false);
  135. if (iob == NULL)
  136. {
  137. nwarn("WARNING: Failed to allocate IOB\n");
  138. return flags;
  139. }
  140. /* Initialize the IOB */
  141. iob->io_offset = hdrlen;
  142. iob->io_len = pstate->is_buflen + hdrlen;
  143. iob->io_pktlen = pstate->is_buflen + hdrlen;
  144. /* Copy the user data into the IOB */
  145. memcpy(&iob->io_data[hdrlen], pstate->is_buffer, pstate->is_buflen);
  146. /* And submit the IOB to the network driver */
  147. ret = radio->r_req_data(radio, &meta, iob);
  148. if (ret < 0)
  149. {
  150. nerr("ERROR: r_req_data() failed: %d\n", ret);
  151. goto errout;
  152. }
  153. /* Save the successful result */
  154. pstate->is_sent = pstate->is_buflen;
  155. /* Don't allow any further call backs. */
  156. pstate->is_cb->flags = 0;
  157. pstate->is_cb->priv = NULL;
  158. pstate->is_cb->event = NULL;
  159. /* Wake up the waiting thread */
  160. nxsem_post(&pstate->is_sem);
  161. }
  162. return flags;
  163. errout:
  164. /* Don't allow any further call backs. */
  165. pstate->is_cb->flags = 0;
  166. pstate->is_cb->priv = NULL;
  167. pstate->is_cb->event = NULL;
  168. pstate->is_sent = ret;
  169. /* Wake up the waiting thread */
  170. nxsem_post(&pstate->is_sem);
  171. return flags;
  172. }
  173. /****************************************************************************
  174. * Public Functions
  175. ****************************************************************************/
  176. /****************************************************************************
  177. * Name: psock_bluetooth_sendto
  178. *
  179. * Description:
  180. * If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
  181. * socket, the parameters to and 'tolen' are ignored (and the error EISCONN
  182. * may be returned when they are not NULL and 0), and the error ENOTCONN is
  183. * returned when the socket was not actually connected.
  184. *
  185. * Input Parameters:
  186. * psock A pointer to a NuttX-specific, internal socket structure
  187. * buf Data to send
  188. * len Length of data to send
  189. * flags Send flags
  190. * to Address of recipient
  191. * tolen The length of the address structure
  192. *
  193. * Returned Value:
  194. * On success, returns the number of characters sent. On error,
  195. * a negated errno value is retruend. See sendto() for the complete list
  196. * of return values.
  197. *
  198. ****************************************************************************/
  199. ssize_t psock_bluetooth_sendto(FAR struct socket *psock, FAR const void *buf,
  200. size_t len, int flags,
  201. FAR const struct sockaddr *to, socklen_t tolen)
  202. {
  203. FAR struct sockaddr_bt_s *destaddr;
  204. FAR struct radio_driver_s *radio;
  205. FAR struct bluetooth_conn_s *conn;
  206. struct bluetooth_sendto_s state;
  207. int ret = OK;
  208. /* Verify that the sockfd corresponds to valid, allocated socket */
  209. if (psock == NULL || psock->s_crefs <= 0)
  210. {
  211. return -EBADF;
  212. }
  213. conn = (FAR struct bluetooth_conn_s *)psock->s_conn;
  214. DEBUGASSERT(conn != NULL);
  215. /* Verify that the address is large enough to be a valid PF_BLUETOOTH
  216. * address.
  217. */
  218. if (tolen < sizeof(bt_addr_t))
  219. {
  220. return -EDESTADDRREQ;
  221. }
  222. /* Get the device driver that will service this transfer */
  223. radio = bluetooth_find_device(conn, &conn->bc_laddr);
  224. if (radio == NULL)
  225. {
  226. return -ENODEV;
  227. }
  228. /* Set the socket state to sending */
  229. psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_SEND);
  230. /* Perform the send operation */
  231. /* Initialize the state structure. This is done with the network locked
  232. * because we don't want anything to happen until we are ready.
  233. */
  234. net_lock();
  235. memset(&state, 0, sizeof(struct bluetooth_sendto_s));
  236. /* This semaphore is used for signaling and, hence, should not have
  237. * priority inheritance enabled.
  238. */
  239. (void)nxsem_init(&state.is_sem, 0, 0); /* Doesn't really fail */
  240. (void)nxsem_setprotocol(&state.is_sem, SEM_PRIO_NONE);
  241. state.is_sock = psock; /* Socket descriptor to use */
  242. state.is_buflen = len; /* Number of bytes to send */
  243. state.is_buffer = buf; /* Buffer to send from */
  244. /* Copy the destination address */
  245. destaddr = (FAR struct sockaddr_bt_s *)to;
  246. memcpy(&state.is_destaddr, &destaddr->bt_bdaddr,
  247. sizeof(bt_addr_t));
  248. if (len > 0)
  249. {
  250. /* Allocate resource to receive a callback */
  251. state.is_cb = bluetooth_callback_alloc(&radio->r_dev, conn);
  252. if (state.is_cb)
  253. {
  254. /* Set up the callback in the connection */
  255. state.is_cb->flags = PKT_POLL;
  256. state.is_cb->priv = (FAR void *)&state;
  257. state.is_cb->event = bluetooth_sendto_eventhandler;
  258. /* Notify the device driver that new TX data is available. */
  259. netdev_txnotify_dev(&radio->r_dev);
  260. /* Wait for the send to complete or an error to occur.
  261. * net_lockedwait will also terminate if a signal is received.
  262. */
  263. ret = net_lockedwait(&state.is_sem);
  264. /* Make sure that no further events are processed */
  265. bluetooth_callback_free(&radio->r_dev, conn, state.is_cb);
  266. }
  267. }
  268. nxsem_destroy(&state.is_sem);
  269. net_unlock();
  270. /* Set the socket state to idle */
  271. psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE);
  272. /* Check for a errors, Errors are signaled by negative errno values
  273. * for the send length
  274. */
  275. if (state.is_sent < 0)
  276. {
  277. return state.is_sent;
  278. }
  279. /* If net_lockedwait failed, then we were probably reawakened by a signal. In
  280. * this case, net_lockedwait will have returned negated errno appropriately.
  281. */
  282. if (ret < 0)
  283. {
  284. return ret;
  285. }
  286. /* Return the number of bytes actually sent */
  287. return state.is_sent;
  288. }
  289. #endif /* CONFIG_NET_BLUETOOTH */