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