1 /* 2 * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #ifndef _BTCOREDATA_H 6 #define _BTCOREDATA_H 7 8 #include <module.h> 9 #include <lock.h> 10 #include <util/DoublyLinkedList.h> 11 #include <util/DoublyLinkedQueue.h> 12 13 #include <net_buffer.h> 14 #include <net_device.h> 15 16 #include <bluetooth/bluetooth.h> 17 #include <bluetooth/HCI/btHCI.h> 18 #include <l2cap.h> 19 20 #define BT_CORE_DATA_MODULE_NAME "bluetooth/btCoreData/v1" 21 22 struct L2capChannel; 23 struct L2capFrame; 24 struct L2capEndpoint; 25 26 typedef enum _connection_status { 27 HCI_CONN_CLOSED, 28 HCI_CONN_OPEN, 29 } connection_status; 30 31 #ifdef __cplusplus 32 33 struct HciConnection : DoublyLinkedListLinkImpl<HciConnection> { 34 HciConnection(); 35 virtual ~HciConnection(); 36 37 hci_id Hid; 38 struct net_device* ndevice; 39 net_buffer* currentRxPacket; 40 ssize_t currentRxExpectedLength; 41 bdaddr_t destination; 42 uint16 handle; 43 int type; 44 uint16 mtu; 45 connection_status status; /* ACL connection state */ 46 uint16 lastCid; 47 uint8 lastIdent; 48 DoublyLinkedList<L2capChannel> ChannelList; 49 DoublyLinkedList<L2capFrame> ExpectedResponses; 50 DoublyLinkedList<L2capFrame> OutGoingFrames; 51 mutex fLock; 52 mutex fLockExpected; 53 }; 54 55 #else 56 57 struct HciConnection; 58 59 #endif 60 61 typedef enum _channel_status { 62 L2CAP_CHAN_CLOSED, /* channel closed */ 63 L2CAP_CHAN_W4_L2CAP_CON_RSP, /* wait for L2CAP resp. */ 64 L2CAP_CHAN_W4_L2CA_CON_RSP, /* wait for upper resp. */ 65 L2CAP_CHAN_CONFIG, /* L2CAP configuration */ 66 L2CAP_CHAN_OPEN, /* channel open */ 67 L2CAP_CHAN_W4_L2CAP_DISCON_RSP, /* wait for L2CAP discon. */ 68 L2CAP_CHAN_W4_L2CA_DISCON_RSP /* wait for upper discon. */ 69 } channel_status; 70 71 72 #ifdef __cplusplus 73 74 typedef struct _ChannelConfiguration { 75 76 uint16 imtu; /* incoming channel MTU */ 77 l2cap_flow_t iflow; /* incoming flow control */ 78 uint16 omtu; /* outgoing channel MTU */ 79 l2cap_flow_t oflow; /* outgoing flow control */ 80 81 uint16 flush_timo; /* flush timeout */ 82 uint16 link_timo; /* link timeout */ 83 84 } ChannelConfiguration; 85 86 struct L2capChannel : DoublyLinkedListLinkImpl<L2capChannel> { 87 HciConnection* conn; 88 uint16 scid; 89 uint16 dcid; 90 uint16 psm; 91 uint8 ident; 92 uint8 cfgState; 93 94 channel_status state; 95 ChannelConfiguration* configuration; 96 L2capEndpoint* endpoint; 97 }; 98 99 #endif 100 101 102 typedef enum _frametype { 103 L2CAP_C_FRAME, // signals 104 L2CAP_G_FRAME, // CL packets 105 L2CAP_B_FRAME, // CO packets 106 107 L2CAP_I_FRAME, 108 L2CAP_S_FRAME 109 } frame_type; 110 111 #ifdef __cplusplus 112 113 struct L2capFrame : DoublyLinkedListLinkImpl<L2capFrame> { 114 115 HciConnection* conn; 116 L2capChannel* channel; 117 118 uint16 flags; /* command flags */ 119 #define L2CAP_CMD_PENDING (1 << 0) /* command is pending */ 120 121 uint8 code; /* L2CAP command opcode */ 122 uint8 ident; /* L2CAP command ident */ 123 124 frame_type type; 125 126 net_buffer* buffer; // contains 1 l2cap / mutliple acls 127 128 //TODO :struct callout timo; /* RTX/ERTX timeout */ 129 }; 130 131 #endif 132 133 134 struct bluetooth_core_data_module_info { 135 module_info info; 136 137 status_t (*PostEvent)(struct net_device* ndev, void* event, size_t size); 138 struct HciConnection* (*AddConnection)(uint16 handle, int type, bdaddr_t* dst, hci_id hid); 139 140 /*status_t (*RemoveConnection)(bdaddr_t destination, hci_id hid);*/ 141 status_t (*RemoveConnection)(uint16 handle, hci_id hid); 142 143 hci_id (*RouteConnection)(bdaddr_t* destination); 144 145 void (*SetAclBuffer)(struct HciConnection* conn, net_buffer* nbuf); 146 void (*SetAclExpectedSize)(struct HciConnection* conn, size_t size); 147 void (*AclPutting)(struct HciConnection* conn, size_t size); 148 bool (*AclComplete)(struct HciConnection* conn); 149 bool (*AclOverFlowed)(struct HciConnection* conn); 150 151 struct HciConnection* (*ConnectionByHandle)(uint16 handle, hci_id hid); 152 struct HciConnection* (*ConnectionByDestination)(bdaddr_t* destination, hci_id hid); 153 154 struct L2capChannel* (*AddChannel)(struct HciConnection* conn, uint16 psm); 155 void (*RemoveChannel)(struct HciConnection* conn, uint16 scid); 156 struct L2capChannel* (*ChannelBySourceID)(struct HciConnection* conn, uint16 sid); 157 uint16 (*ChannelAllocateCid)(struct HciConnection* conn); 158 uint16 (*ChannelAllocateIdent)(struct HciConnection* conn); 159 160 struct L2capFrame* (*SignalByIdent)(struct HciConnection* conn, uint8 ident); 161 status_t (*TimeoutSignal)(struct L2capFrame* frame, uint32 timeo); 162 status_t (*UnTimeoutSignal)(struct L2capFrame* frame); 163 struct L2capFrame* (*SpawnFrame)(struct HciConnection* conn, struct L2capChannel* channel, net_buffer* buffer, frame_type frame); 164 struct L2capFrame* (*SpawnSignal)(struct HciConnection* conn, struct L2capChannel* channel, net_buffer* buffer, uint8 ident, uint8 code); 165 status_t (*AcknowledgeSignal)(struct L2capFrame* frame); 166 status_t (*QueueSignal)(struct L2capFrame* frame); 167 168 }; 169 170 inline bool ExistConnectionByDestination(bdaddr_t* destination, hci_id hid); 171 inline bool ExistConnectionByHandle(uint16 handle, hci_id hid); 172 173 #endif // _BTCOREDATA_H 174