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