l2cap.h 14 KB

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