l2cap_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /**************************************************************************************************
  29. **************************************************************************************************/
  30. #ifndef L2CAP_INTERNAL_H
  31. #define L2CAP_INTERNAL_H
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. /*********************************************************************
  37. * INCLUDES
  38. */
  39. #include "hci.h"
  40. #include "l2cap.h"
  41. /*********************************************************************
  42. * MACROS
  43. */
  44. // Macro to see if a given channel is a fixed channel
  45. #define FIX_CHANNEL( CID ) ( (CID) == L2CAP_CID_GENERIC ||\
  46. (CID) == L2CAP_CID_SIG ||\
  47. (CID) == L2CAP_CID_ATT ||\
  48. (CID) == L2CAP_CID_SMP )
  49. // Marco to convert a channel ID to an index into L2CAP Channel table
  50. #define CID_TO_INDEX( CID ) ( (CID) - BASE_DYNAMIC_CID )
  51. // Marco to convert a fixed channel ID to an index into L2CAP Fixed Channel table
  52. #define FCID_TO_INDEX( CID ) ( (CID) - L2CAP_CID_ATT )
  53. // Macro to return the record maintained a given fix channel
  54. #define FIX_CHANNEL_REC( CID ) ( l2capFixedChannels[FCID_TO_INDEX( CID )] )
  55. /*********************************************************************
  56. * CONSTANTS
  57. */
  58. // Signaling command header: Code (1 byte) + Identifier (1 byte) + Length (2 bytes)
  59. #define SIGNAL_HDR_SIZE 4
  60. // Maximum size of data field of Signaling commands
  61. #define SIGNAL_DATA_SIZE ( L2CAP_SIG_MTU_SIZE - SIGNAL_HDR_SIZE )
  62. /*********************************************************************
  63. * L2CAP Channel States: states used for l2capChannel 'state' field
  64. */
  65. // Closed - no channel associated with this CID
  66. #define L2CAP_CLOSED 0x00
  67. // Waiting for Echo Response
  68. #define L2CAP_W4_ECHO_RSP 0x01
  69. // Waiting for Info Response
  70. #define L2CAP_W4_INFO_RSP 0x02
  71. // Waiting for Connection Parameter Update Response
  72. #define L2CAP_W4_PARAM_UPDATE_RSP 0x03
  73. /*********************************************************************
  74. * TYPEDEFS
  75. */
  76. // L2CAP Channel structure. Allocated one per application connection
  77. // between two devices. CID assignment is relative to a particular device
  78. // and a device can assign CIDs independently from other devices (except
  79. // for the reserved CIDs). The CIDs are dynamically allocated in the range
  80. // from 0x0040 to 0xFFFF.
  81. typedef struct
  82. {
  83. // Channel info
  84. uint8 state; // Channel connection state
  85. uint16 CID; // Local channel id
  86. uint8 id; // Local identifier - matches responses with requests
  87. // Link info
  88. uint16 connHandle; // link connection handle
  89. // Application info
  90. uint8 taskId; // task that channel belongs to
  91. // Timer id
  92. uint8 timerId;
  93. } l2capChannel_t;
  94. // L2CAP Fixed Channel structure. Allocated one for each fixed channel.
  95. typedef struct
  96. {
  97. uint16 CID; // channel id
  98. uint8 taskId; // task registered with channel
  99. } l2capFixedChannel_t;
  100. // Signaling packet header format
  101. typedef struct
  102. {
  103. uint8 opcode; // type of command
  104. uint8 id; // identifier - matches responses with requests
  105. uint16 len; // length of data field (doesn't cover Code, Identifier and Length fields)
  106. } l2capSignalHdr_t;
  107. /**
  108. * @brief Callback function prototype for building a Signaling command.
  109. *
  110. * @param pBuf - pointer to buffer to hold command data
  111. * @param pData - pointer to command data
  112. *
  113. * @return length of the command data
  114. */
  115. typedef uint16 (*pfnL2CAPBuildCmd_t)( uint8 *pBuf, uint8 *pData );
  116. /*********************************************************************
  117. * GLOBAL VARIABLES
  118. */
  119. extern uint8 l2capTaskID;
  120. extern l2capChannel_t l2capChannels[];
  121. extern l2capFixedChannel_t l2capFixedChannels[];
  122. /*********************************************************************
  123. * FUNCTIONS - API
  124. */
  125. /*
  126. * Send L2CAP Command.
  127. */
  128. extern bStatus_t l2capSendCmd( uint16 connHandle, uint8 opcode, uint8 id,
  129. uint8 *pCmd, pfnL2CAPBuildCmd_t pfnBuildCmd );
  130. /*
  131. * Send L2CAP Request.
  132. */
  133. extern bStatus_t l2capSendReq( uint16 connHandle, uint8 opcode, uint8 *pReq,
  134. pfnL2CAPBuildCmd_t pfnBuildCmd, uint8 state, uint8 taskId );
  135. /*
  136. * Build Echo Request.
  137. */
  138. extern uint16 l2capBuildEchoReq( uint8 *pBuf, uint8 *pCmd );
  139. /*
  140. * Build Info Request.
  141. */
  142. extern uint16 l2capBuildInfoReq( uint8 *pBuf, uint8 *pCmd );
  143. /*
  144. * Build Parameter Update Request.
  145. */
  146. extern uint16 l2capBuildParamUpdateReq( uint8 *pBuf, uint8 *pData );
  147. /*
  148. * Encapsulate and send L2CAP packet.
  149. */
  150. extern bStatus_t l2capEncapSendData( uint16 connHandle, l2capPacket_t *pPkt );
  151. /*
  152. * Parse L2CAP packet.
  153. */
  154. extern uint8 l2capParsePacket( l2capPacket_t *pPkt, hciDataEvent_t *pHciMsg );
  155. /*
  156. * Parse L2CAP Signaling header.
  157. */
  158. extern void l2capParseSignalHdr( l2capSignalHdr_t *pHdr, uint8 *pData );
  159. /*
  160. * Build Echo Response.
  161. */
  162. extern uint16 l2capBuildEchoRsp( uint8 *pBuf, uint8 *pCmd );
  163. /*
  164. * Parse Command Reject.
  165. */
  166. extern bStatus_t l2capParseCmdReject( l2capSignalCmd_t *pCmd, uint8 *pData, uint16 len );
  167. /*
  168. * Parse Echo Response.
  169. */
  170. extern bStatus_t l2capParseEchoRsp( l2capSignalCmd_t *pCmd, uint8 *pData, uint16 len );
  171. /*
  172. * Parse Information Response.
  173. */
  174. extern bStatus_t l2capParseInfoRsp( l2capSignalCmd_t *pCmd, uint8 *pData, uint16 len );
  175. /*
  176. * Parse Connection Parameter Update Response.
  177. */
  178. extern bStatus_t l2capParseParamUpdateRsp( l2capSignalCmd_t *pCmd, uint8 *pData, uint16 len );
  179. /*
  180. * Find a channel using the local identifier.
  181. */
  182. extern l2capChannel_t *l2capFindLocalId( uint8 id );
  183. /*
  184. * Free a channel.
  185. */
  186. extern void l2capFreeChannel( l2capChannel_t *pChannel );
  187. /*
  188. * Stop an active timer for a given channel.
  189. */
  190. extern void l2capStopTimer( l2capChannel_t *pChannel );
  191. /*
  192. * Handle an incoming packet error.
  193. */
  194. extern void l2capHandleRxError( uint16 connHandle );
  195. /*
  196. * Forward a data message to upper layer application.
  197. */
  198. extern bStatus_t l2capNotifyData( uint8 taskId, uint16 connHandle, l2capPacket_t *pPkt );
  199. /*
  200. * Send a Signaling command to upper layer application.
  201. */
  202. extern void l2capNotifySignal( uint8 taskId, uint16 connHandle, uint8 status,
  203. uint8 opcode, uint8 id, l2capSignalCmd_t *pCmd );
  204. extern void *L2CAP_Fragment_bm_alloc( uint16 size );
  205. extern uint8 L2CAP_Fragment_SendDataPkt( uint16 connHandle, uint8 fragFlg,uint16 pktLen, uint8 *pBuf );
  206. /*********************************************************************
  207. * @fn l2capInfoRsp
  208. *
  209. * @brief Send Info Response.
  210. *
  211. * Use like: l2capInfoRsp( uint16 connHandle, uint8 id, l2capInfoRsp_t *pInfoRsp );
  212. *
  213. * @param connHandle - connection to use
  214. * @param id - identifier received in request
  215. * @param pInfoRsp - pointer to Info Response to be sent
  216. *
  217. * @return SUCCESS: Request was sent successfully.
  218. * INVALIDPARAMETER: Data can not fit into one packet.
  219. * MSG_BUFFER_NOT_AVAIL: No HCI buffer is available.
  220. * bleNotConnected: Connection is down.
  221. * bleMemAllocError: Memory allocation error occurred.
  222. */
  223. #define l2capInfoRsp( connHandle, id, pInfoRsp ) l2capSendCmd( (connHandle), L2CAP_INFO_RSP, (id),\
  224. (uint8 *)(pInfoRsp), L2CAP_BuildInfoRsp )
  225. /*********************************************************************
  226. * @fn l2capEchoRsp
  227. *
  228. * @brief Send Ehco Response.
  229. *
  230. * Use like: l2capEchoRsp( uint16 connHandle, uint8 id, l2capEchoRsp_t *pEchoRsp );
  231. *
  232. * @param connHandle - connection to use
  233. * @param id - identifier received in request
  234. * @param pEchoRsp - pinter to Echo Response to be sent
  235. *
  236. * @return SUCCESS: Request was sent successfully.
  237. * INVALIDPARAMETER: Data can not fit into one packet.
  238. * MSG_BUFFER_NOT_AVAIL: No HCI buffer is available.
  239. * bleNotConnected: Connection is down.
  240. * bleMemAllocError: Memory allocation error occurred.
  241. */
  242. #define l2capEchoRsp( connHandle, id, pEchoRsp ) l2capSendCmd( (connHandle), L2CAP_ECHO_RSP, (id),\
  243. (uint8 *)(pEchoRsp), l2capBuildEchoRsp )
  244. /*********************************************************************
  245. *********************************************************************/
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #endif /* L2CAP_INTERNAL_H */