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