bt_queue.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /****************************************************************************
  2. * wireless/bluetooth/bt_queue.c
  3. * Inter-thread buffer queue management
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one or more
  6. * contributor license agreements. See the NOTICE file distributed with
  7. * this work for additional information regarding copyright ownership. The
  8. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance with the
  10. * License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. * License for the specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. ****************************************************************************/
  21. /****************************************************************************
  22. * Included Files
  23. ****************************************************************************/
  24. #include <nuttx/config.h>
  25. #include <nuttx/compiler.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <debug.h>
  29. #include <nuttx/mqueue.h>
  30. #include <nuttx/mm/iob.h>
  31. #include <nuttx/wireless/bluetooth/bt_buf.h>
  32. #include "bt_queue.h"
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. /* Common essage queue attributes */
  37. #define BT_MSGSIZE sizeof(struct bt_bufmsg_s)
  38. #define BT_MSGFLAGS 0
  39. /****************************************************************************
  40. * Private Types
  41. ****************************************************************************/
  42. /* A message is just a buffer structure */
  43. struct bt_bufmsg_s
  44. {
  45. FAR struct bt_buf_s *buf;
  46. };
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: bt_queue_open
  52. *
  53. * Description:
  54. * Open a message queue for read or write access.
  55. *
  56. * Input Parameters:
  57. * name - The name of the message queue to open
  58. * oflags - Open flags with access mode
  59. * nmsgs - Max number of messages in queue before bt_queue_send() blocks.
  60. * mqd - The location in which to return the message queue descriptor
  61. *
  62. * Returned Value:
  63. * Zero is returned on success; a negated errno value is returned on any
  64. * failure;
  65. *
  66. ****************************************************************************/
  67. int bt_queue_open(FAR const char *name, int oflags, int nmsgs,
  68. FAR struct file *mqd)
  69. {
  70. struct mq_attr attr;
  71. int ret = OK;
  72. /* Initialize the message queue attributes */
  73. attr.mq_maxmsg = nmsgs;
  74. attr.mq_msgsize = BT_MSGSIZE;
  75. attr.mq_flags = BT_MSGFLAGS;
  76. ret = file_mq_open(mqd, name, oflags, 0666, &attr);
  77. if (ret < 0)
  78. {
  79. gerr("ERROR: file_mq_open(%s) failed: %d\n", name, ret);
  80. }
  81. return ret;
  82. }
  83. /****************************************************************************
  84. * Name: bt_queue_receive
  85. *
  86. * Description:
  87. * Block until the next buffer is received on the queue.
  88. *
  89. * Input Parameters:
  90. * mqd - The message queue descriptor previously returned by
  91. * bt_open_*queue.
  92. * buf - The location in which to return the received buffer.
  93. *
  94. * Returned Value:
  95. * Zero is returned on success; a negated errno value is returned on any
  96. * failure;
  97. *
  98. ****************************************************************************/
  99. int bt_queue_receive(struct file *mqd, FAR struct bt_buf_s **buf)
  100. {
  101. union
  102. {
  103. struct bt_bufmsg_s msg;
  104. char msgbuf[BT_MSGSIZE];
  105. } u;
  106. ssize_t msgsize;
  107. unsigned int priority;
  108. DEBUGASSERT(mqd != NULL && buf != NULL);
  109. /* Wait for the next message */
  110. u.msg.buf = NULL;
  111. msgsize = file_mq_receive(mqd, u.msgbuf, BT_MSGSIZE, &priority);
  112. if (msgsize < 0)
  113. {
  114. wlerr("ERROR: file_mq_receive() failed: %ld\n", (long)msgsize);
  115. return (int)msgsize;
  116. }
  117. /* Only buffers are expected as messages and all messages should have an
  118. * attached IOB frame.
  119. */
  120. DEBUGASSERT(msgsize == sizeof(struct bt_bufmsg_s));
  121. DEBUGASSERT(u.msg.buf != NULL && u.msg.buf->frame != NULL);
  122. /* Return the buffer */
  123. *buf = u.msg.buf;
  124. return OK;
  125. }
  126. /****************************************************************************
  127. * Name: bt_queue_send
  128. *
  129. * Description:
  130. * Send the buffer to the specified message queue
  131. *
  132. * Input Parameters:
  133. * mqd - The message queue descriptor previously returned by
  134. * bt_open_*queue.
  135. * buf - A reference to the buffer to be sent
  136. * priority - Either BT_NORMAL_PRIO or BT_NORMAL_HIGH. NOTE:
  137. * BT_NORMAL_HIGHis only for use within the stack. Drivers
  138. * should always use BT_NORMAL_PRIO.
  139. *
  140. * Returned Value:
  141. * Zero is returned on success; a negated errno value is returned on any
  142. * failure;
  143. *
  144. ****************************************************************************/
  145. int bt_queue_send(struct file *mqd,
  146. FAR struct bt_buf_s *buf,
  147. unsigned int priority)
  148. {
  149. struct bt_bufmsg_s msg;
  150. int ret;
  151. DEBUGASSERT(mqd != NULL && buf != NULL && buf->frame != NULL);
  152. /* Format and send the buffer message */
  153. msg.buf = buf;
  154. ret = file_mq_send(mqd, (FAR const char *)&msg,
  155. sizeof(struct bt_bufmsg_s), priority);
  156. if (ret < 0)
  157. {
  158. wlerr("ERROR: file_mq_send() failed: %d\n", ret);
  159. }
  160. return ret;
  161. }