l2cap.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. Filename: l2cap.h
  30. Revised:
  31. Revision:
  32. Description: This file contains the L2CAP definitions.
  33. **************************************************************************************************/
  34. #ifndef L2CAP_H
  35. #define L2CAP_H
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. /*********************************************************************
  41. * INCLUDES
  42. */
  43. #include "bcomdef.h"
  44. #include "OSAL.h"
  45. /*********************************************************************
  46. * CONSTANTS
  47. */
  48. // Minimum supported information payload for the Basic information frame (B-frame)
  49. #define L2CAP_MTU_SIZE 517//247//23
  50. // Minimum supported information payload for the Control frame (C-frame)
  51. #define L2CAP_SIG_MTU_SIZE 23
  52. // Basic L2CAP header: Length (2 bytes) + Channel ID (2 bytes)
  53. #define L2CAP_HDR_SIZE 4
  54. // Minimum size of PDU received from lower layer protocol (incoming
  55. // packet), or delivered to lower layer protocol (outgoing packet).
  56. #define L2CAP_PDU_SIZE ( L2CAP_HDR_SIZE + L2CAP_MTU_SIZE )
  57. // L2CAP Channel Identifiers. Identifiers from 0x0001 to 0x003F are
  58. // reserved for specific L2CAP functions. Identifiers 0x0001-0x0003
  59. // are reserved by BR/EDR.
  60. #define L2CAP_CID_NULL 0x0000 // Illegal Identifier
  61. #define L2CAP_CID_ATT 0x0004 // Attribute Protocol
  62. #define L2CAP_CID_SIG 0x0005 // L2CAP Signaling
  63. #define L2CAP_CID_SMP 0x0006 // Security Management Protocol
  64. #define L2CAP_CID_GENERIC 0x0007 // Generic Fixed Channel
  65. // L2CAP Dynamic Channel Identifiers
  66. #define L2CAP_BASE_DYNAMIC_CID 0x0040
  67. #define L2CAP_LAST_DYNAMIC_CID ( BASE_DYNAMIC_CID + L2CAP_NUM_CHANNELS - 1 )
  68. // Number of Fixed channels: one for each of ATT, Signaling, SMP channels and one Generic Channel
  69. #define L2CAP_NUM_FIXED_CHANNELS 4
  70. // Number of Protocols supported -- for future use
  71. #define L2CAP_NUM_PROTOCOLS 0
  72. // Number of Auxiliary channels: one for each of Echo Request, Information
  73. // Request and Connection Parameter Update Request
  74. #define L2CAP_NUM_AUX_CHANNELS 3
  75. // Number of Dynamic channels: one per each protocol supported on each physical connection
  76. #define L2CAP_NUM_DYNAMIC_CHANNELS ( L2CAP_NUM_PROTOCOLS * MAX_NUM_LL_CONN )
  77. // Total number of L2CAP channels: Dynamic channels plus Auxiliary channels
  78. #define L2CAP_NUM_CHANNELS ( L2CAP_NUM_DYNAMIC_CHANNELS + L2CAP_NUM_AUX_CHANNELS )
  79. // L2CAP Response Timeout expired (RTX) value for Signaling commands (in seconds).
  80. // The RTX timer is used for response timeout or to terminate a dynamic channel
  81. // when the remote device is unresponsive to signaling requests. Its value may
  82. // range from 1 to 60 seconds.
  83. #define L2CAP_RTX_TIMEOUT 30
  84. // L2CAP Signaling Codes (type of commands)
  85. #define L2CAP_CMD_REJECT 0x01
  86. #define L2CAP_ECHO_REQ 0x08 // No longer supported
  87. #define L2CAP_ECHO_RSP 0x09 // No longer supported
  88. #define L2CAP_INFO_REQ 0x0a // No longer supported
  89. #define L2CAP_INFO_RSP 0x0b // No longer supported
  90. #define L2CAP_PARAM_UPDATE_REQ 0x12
  91. #define L2CAP_PARAM_UPDATE_RSP 0x13
  92. /*********************************************************************
  93. * Command Reject: Reason Codes
  94. */
  95. // Command not understood
  96. #define L2CAP_REJECT_CMD_NOT_UNDERSTOOD 0x0000
  97. // Signaling MTU exceeded
  98. #define L2CAP_REJECT_SIGNAL_MTU_EXCEED 0x0001
  99. // Invalid CID in request
  100. #define L2CAP_REJECT_INVALID_CID 0x0002
  101. /*********************************************************************
  102. * Information Request/Response: Info Type
  103. */
  104. // Connectionless MTU
  105. #define L2CAP_INFO_CONNLESS_MTU 0x0001
  106. // Extended features supported
  107. #define L2CAP_INFO_EXTENDED_FEATURES 0x0002
  108. // Fixed channels supported
  109. #define L2CAP_INFO_FIXED_CHANNELS 0x0003
  110. /*********************************************************************
  111. * Information Response: Extended Features Mask Values
  112. */
  113. // Fixed channels are supported
  114. #define L2CAP_FIXED_CHANNELS 0x00000080
  115. // Length of Extended Features bit mask
  116. #define L2CAP_EXTENDED_FEATURES_SIZE 4
  117. /*********************************************************************
  118. * Information Response: Fixed Channels Mask Values
  119. */
  120. // Fixed Channel ATT is supported
  121. #define L2CAP_FIXED_CHANNELS_ATT 0x10
  122. // Fixed Channel L2CAP Signaling is supported
  123. #define L2CAP_FIXED_CHANNELS_SIG 0x20
  124. // Fixed Channel SMP is supported
  125. #define L2CAP_FIXED_CHANNELS_SMP 0x40
  126. // Length of Fixed Channels bit mask
  127. #define L2CAP_FIXED_CHANNELS_SIZE 8
  128. /*********************************************************************
  129. * Information Response: Result Values
  130. */
  131. // Success
  132. #define L2CAP_INFO_SUCCESS 0x0000
  133. // Not supported
  134. #define L2CAP_INFO_NOT_SUPPORTED 0x0001
  135. /*********************************************************************
  136. * Connection Parameter Update Response: Result values
  137. */
  138. // Connection Parameters accepted
  139. #define L2CAP_CONN_PARAMS_ACCEPTED 0x0000
  140. // Connection Parameters rejected
  141. #define L2CAP_CONN_PARAMS_REJECTED 0x0001
  142. /*********************************************************************
  143. * VARIABLES
  144. */
  145. /*********************************************************************
  146. * MACROS
  147. */
  148. /*********************************************************************
  149. * TYPEDEFS
  150. */
  151. // Invalid CID in Request format
  152. typedef struct
  153. {
  154. uint16 localCID; // Destination CID from the rejected command
  155. uint16 remoteCID; // Source CID from the rejected command
  156. } l2capInvalidCID_t;
  157. // Command Reject Reason Data format
  158. typedef union
  159. {
  160. uint16 signalMTU; // Maximum Signaling MTU
  161. l2capInvalidCID_t invalidCID; // Invalid CID in Request
  162. } l2capReasonData_t;
  163. // Command Reject format
  164. typedef struct
  165. {
  166. uint16 reason; // Reason
  167. l2capReasonData_t reasonData; // Reason Data
  168. // Shorthand access for union members
  169. #define maxSignalMTU reasonData.signalMTU
  170. #define invalidLocalCID reasonData.invalidCID.localCID
  171. #define invalidRemoteCID reasonData.invalidCID.remoteCID
  172. } l2capCmdReject_t;
  173. // Echo Request format
  174. typedef struct
  175. {
  176. uint8 *pData; // Optional data field
  177. uint16 len; // Length of data
  178. } l2capEchoReq_t;
  179. // Echo Response format
  180. typedef struct
  181. {
  182. uint8 *pData; // Optional data field -- must be freed by the application
  183. uint16 len; // Length of data
  184. } l2capEchoRsp_t;
  185. // Information Request format
  186. typedef struct
  187. {
  188. uint16 infoType; // Information type
  189. } l2capInfoReq_t;
  190. // Information Response Data field
  191. typedef union
  192. {
  193. uint16 connectionlessMTU; // Connectionless MTU
  194. uint32 extendedFeatures; // Extended features supported
  195. uint8 fixedChannels[L2CAP_FIXED_CHANNELS_SIZE]; // Fixed channels supported
  196. } l2capInfo_t;
  197. // Information Response format
  198. typedef struct
  199. {
  200. uint16 result; // Result
  201. uint16 infoType; // Information type
  202. l2capInfo_t info; // Content of Info field depends on infoType
  203. } l2capInfoRsp_t;
  204. // Connection Parameter Update Request format
  205. typedef struct
  206. {
  207. uint16 intervalMin; // Minimum Interval
  208. uint16 intervalMax; // Maximum Interval
  209. uint16 slaveLatency; // Slave Latency
  210. uint16 timeoutMultiplier; // Timeout Multiplier
  211. } l2capParamUpdateReq_t;
  212. // Connection Parameter Update Response format
  213. typedef struct
  214. {
  215. uint16 result; // Result
  216. } l2capParamUpdateRsp_t;
  217. // Union of all L2CAP Signaling commands
  218. typedef union
  219. {
  220. // Requests
  221. l2capEchoReq_t echoReq;
  222. l2capInfoReq_t infoReq;
  223. l2capParamUpdateReq_t updateReq;
  224. // Responses
  225. l2capCmdReject_t cmdReject;
  226. l2capEchoRsp_t echoRsp;
  227. l2capInfoRsp_t infoRsp;
  228. l2capParamUpdateRsp_t updateRsp;
  229. } l2capSignalCmd_t;
  230. // OSAL L2CAP_SIGNAL_EVENT message format. This message is used to deliver an
  231. // incoming Signaling command up to an upper layer application.
  232. typedef struct
  233. {
  234. osal_event_hdr_t hdr; // L2CAP_SIGNAL_EVENT and status
  235. uint16 connHandle; // connection message was received on
  236. uint8 id; // identifier to match responses with requests
  237. uint8 opcode; // type of command
  238. l2capSignalCmd_t cmd; // command data
  239. } l2capSignalEvent_t;
  240. // L2CAP packet structure
  241. typedef struct
  242. {
  243. uint16 CID; // local channel id
  244. uint8 *pPayload; // pointer to information payload. This contains the payload
  245. // received from the upper layer protocol (outgoing packet),
  246. // or delivered to the upper layer protocol (incoming packet).
  247. uint16 len; // length of information payload
  248. } l2capPacket_t;
  249. // OSAL L2CAP_DATA_EVENT message format. This message is used to forward an
  250. // incoming data packet up to an upper layer application.
  251. typedef struct
  252. {
  253. osal_event_hdr_t hdr; // L2CAP_DATA_EVENT and status
  254. uint16 connHandle; // connection packet was received on
  255. l2capPacket_t pkt; // received packet
  256. } l2capDataEvent_t;
  257. typedef struct
  258. {
  259. uint16 cIdx; // reassemble packet current idx
  260. l2capPacket_t pkt; // received packet
  261. } l2capReassemblePkt_t;
  262. typedef struct
  263. {
  264. uint8 len; // pkt len
  265. uint8* ptr ; // pkt point
  266. } segmentBuff_t;
  267. typedef struct
  268. {
  269. segmentBuff_t pkt[20];//251/27->9.2 , 20200604 10 --> 20 : support for MTU 517
  270. uint8 depth;
  271. uint8 idx;
  272. uint8* pBufScr; //source buffer ptr
  273. uint8 fragment;
  274. } l2capSegmentBuff_t;
  275. typedef struct
  276. {
  277. uint32 reassembleInCnt;
  278. uint32 reassembleOutCnt;
  279. uint32 reassembleErrIdx;
  280. uint32 reassembleErrCID;
  281. uint32 reassembleErrInComp;
  282. uint32 reassembleErrMiss;
  283. uint32 resssambleMemAlocErr;
  284. uint32 segmentInCnt;
  285. uint32 segmentOutCnt;
  286. uint32 segmentErrCnt;
  287. uint32 fragmentSendCounter;
  288. uint32 segmentMemAlocErr;
  289. uint32 segmentSentToLinkLayerErr;
  290. } l2capSARDbugCnt_t;
  291. //typedef enum
  292. //{
  293. // DATA_IN_YBUF_FIRST = 0, // YBUF fisrt bufin fisrt shift out
  294. // DATA_IN_XBUF_FIRST = 1
  295. //} SegmentBuffOrder_t;
  296. //typedef struct
  297. //{
  298. // l2capSegmentBuff_t xBuf;
  299. // l2capSegmentBuff_t yBuf;
  300. // SegmentBuffOrder_t order; //which buffer
  301. //
  302. //}l2capSegmentPkt_t;
  303. /*********************************************************************
  304. * VARIABLES
  305. */
  306. /*********************************************************************
  307. * FUNCTIONS
  308. */
  309. /*
  310. * Initialize L2CAP layer.
  311. */
  312. extern void L2CAP_Init( uint8 taskId );
  313. /*
  314. * L2CAP Task event processing function.
  315. */
  316. extern uint16 L2CAP_ProcessEvent( uint8 taskId, uint16 events );
  317. /*
  318. * Register a protocol/application with an L2CAP channel.
  319. */
  320. extern bStatus_t L2CAP_RegisterApp( uint8 taskId, uint16 CID );
  321. /*
  322. * Send L2CAP Data Packet.
  323. */
  324. extern bStatus_t L2CAP_SendData( uint16 connHandle, l2capPacket_t *pPkt );
  325. /*
  326. * Send Command Reject.
  327. */
  328. extern bStatus_t L2CAP_CmdReject( uint16 connHandle, uint8 id, l2capCmdReject_t *pCmdReject );
  329. /*
  330. * Build Command Reject.
  331. */
  332. extern uint16 L2CAP_BuildCmdReject( uint8 *pBuf, uint8 *pCmd );
  333. /*
  334. * Send L2CAP Echo Request.
  335. */
  336. extern bStatus_t L2CAP_EchoReq( uint16 connHandle, l2capEchoReq_t *pEchoReq, uint8 taskId );
  337. /*
  338. * Send L2CAP Information Request.
  339. */
  340. extern bStatus_t L2CAP_InfoReq( uint16 connHandle, l2capInfoReq_t *pInfoReq, uint8 taskId );
  341. /*
  342. * Build Information Response.
  343. */
  344. extern uint16 L2CAP_BuildInfoRsp( uint8 *pBuf, uint8 *pCmd );
  345. /*
  346. * Parse Information Request.
  347. */
  348. extern bStatus_t L2CAP_ParseInfoReq( l2capSignalCmd_t *pCmd, uint8 *pData, uint16 len );
  349. /*
  350. * Send L2CAP Connection Parameter Update Request.
  351. */
  352. extern bStatus_t L2CAP_ConnParamUpdateReq( uint16 connHandle, l2capParamUpdateReq_t *pUpdateReq, uint8 taskId );
  353. /*
  354. * Parse Connection Parameter Update Request.
  355. */
  356. extern bStatus_t L2CAP_ParseParamUpdateReq( l2capSignalCmd_t *pCmd, uint8 *pData, uint16 len );
  357. /*
  358. * Send L2CAP Connection Parameter Update Response.
  359. */
  360. extern bStatus_t L2CAP_ConnParamUpdateRsp( uint16 connHandle, uint8 id, l2capParamUpdateRsp_t *pUpdateRsp );
  361. /*
  362. * Build Connection Parameter Update Response.
  363. */
  364. extern uint16 L2CAP_BuildParamUpdateRsp( uint8 *pBuf, uint8 *pData );
  365. /*
  366. * Allocate a block of memory at the L2CAP layer.
  367. */
  368. extern void *L2CAP_bm_alloc( uint16 size );
  369. /*
  370. * This API is used by the upper layer to turn flow control on
  371. * or off for data packets sent from the Controller to the Host.
  372. */
  373. extern void L2CAP_SetControllerToHostFlowCtrl( uint16 hostBuffSize, uint8 flowCtrlMode );
  374. /*
  375. * This API is used by the upper layer to notify L2CAP of the
  376. * number of data packets that have been completed for connection
  377. * handle since this API was previously called.
  378. */
  379. extern void L2CAP_HostNumCompletedPkts( uint16 connHandle, uint16 numCompletedPkts );
  380. extern uint8 l2capPktToSegmentBuff(uint16 connHandle,l2capSegmentBuff_t* pSegBuf, uint8 blen,uint8* pBuf);
  381. extern uint8 l2capSegmentBuffToLinkLayer(uint16 connHandle, l2capSegmentBuff_t* pSegBuf);
  382. extern void l2capPocessFragmentTxData(uint16 connHandle);
  383. extern void l2capSarBufReset(void);
  384. extern void L2CAP_ReassemblePkt_Reset(uint16 connHandle);
  385. extern void L2CAP_SegmentPkt_Reset(uint16 connHandle);
  386. extern void L2CAP_ExtendFramgents_Config(uint8 flag);
  387. /*********************************************************************
  388. *********************************************************************/
  389. #ifdef __cplusplus
  390. }
  391. #endif
  392. #endif /* L2CAP_H */