Jason Engelman
/
BlueUSB
BlueUSB with USB-> SERIAL (CP210x) GPIO pins working.
hci.h@0:a14eaa2e1445, 2010-04-23 (annotated)
- Committer:
- tecnosys
- Date:
- Fri Apr 23 05:04:28 2010 +0000
- Revision:
- 0:a14eaa2e1445
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tecnosys | 0:a14eaa2e1445 | 1 | /* |
tecnosys | 0:a14eaa2e1445 | 2 | Copyright (c) 2010 Peter Barrett |
tecnosys | 0:a14eaa2e1445 | 3 | |
tecnosys | 0:a14eaa2e1445 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
tecnosys | 0:a14eaa2e1445 | 5 | of this software and associated documentation files (the "Software"), to deal |
tecnosys | 0:a14eaa2e1445 | 6 | in the Software without restriction, including without limitation the rights |
tecnosys | 0:a14eaa2e1445 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
tecnosys | 0:a14eaa2e1445 | 8 | copies of the Software, and to permit persons to whom the Software is |
tecnosys | 0:a14eaa2e1445 | 9 | furnished to do so, subject to the following conditions: |
tecnosys | 0:a14eaa2e1445 | 10 | |
tecnosys | 0:a14eaa2e1445 | 11 | The above copyright notice and this permission notice shall be included in |
tecnosys | 0:a14eaa2e1445 | 12 | all copies or substantial portions of the Software. |
tecnosys | 0:a14eaa2e1445 | 13 | |
tecnosys | 0:a14eaa2e1445 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
tecnosys | 0:a14eaa2e1445 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
tecnosys | 0:a14eaa2e1445 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
tecnosys | 0:a14eaa2e1445 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
tecnosys | 0:a14eaa2e1445 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
tecnosys | 0:a14eaa2e1445 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
tecnosys | 0:a14eaa2e1445 | 20 | THE SOFTWARE. |
tecnosys | 0:a14eaa2e1445 | 21 | */ |
tecnosys | 0:a14eaa2e1445 | 22 | |
tecnosys | 0:a14eaa2e1445 | 23 | #ifndef HCI_H_INCLUDED |
tecnosys | 0:a14eaa2e1445 | 24 | #define HCI_H_INCLUDED |
tecnosys | 0:a14eaa2e1445 | 25 | |
tecnosys | 0:a14eaa2e1445 | 26 | #include "Socket.h" |
tecnosys | 0:a14eaa2e1445 | 27 | |
tecnosys | 0:a14eaa2e1445 | 28 | #pragma pack(1) |
tecnosys | 0:a14eaa2e1445 | 29 | |
tecnosys | 0:a14eaa2e1445 | 30 | #define ERR_HCI_DEVICE_NOT_FOUND -300 |
tecnosys | 0:a14eaa2e1445 | 31 | |
tecnosys | 0:a14eaa2e1445 | 32 | class HCI; |
tecnosys | 0:a14eaa2e1445 | 33 | class HCITransport; |
tecnosys | 0:a14eaa2e1445 | 34 | class BTDevice; |
tecnosys | 0:a14eaa2e1445 | 35 | |
tecnosys | 0:a14eaa2e1445 | 36 | typedef struct |
tecnosys | 0:a14eaa2e1445 | 37 | { |
tecnosys | 0:a14eaa2e1445 | 38 | u8 addr[6]; |
tecnosys | 0:a14eaa2e1445 | 39 | } BD_ADDR; |
tecnosys | 0:a14eaa2e1445 | 40 | |
tecnosys | 0:a14eaa2e1445 | 41 | typedef struct |
tecnosys | 0:a14eaa2e1445 | 42 | { |
tecnosys | 0:a14eaa2e1445 | 43 | BD_ADDR bdaddr; |
tecnosys | 0:a14eaa2e1445 | 44 | u8 pscan_rep_mode; |
tecnosys | 0:a14eaa2e1445 | 45 | u8 pscan_period_mode; |
tecnosys | 0:a14eaa2e1445 | 46 | u8 pscan_mode; |
tecnosys | 0:a14eaa2e1445 | 47 | u8 dev_class[3]; |
tecnosys | 0:a14eaa2e1445 | 48 | u16 clock_offset; |
tecnosys | 0:a14eaa2e1445 | 49 | } inquiry_info; |
tecnosys | 0:a14eaa2e1445 | 50 | |
tecnosys | 0:a14eaa2e1445 | 51 | typedef struct |
tecnosys | 0:a14eaa2e1445 | 52 | { |
tecnosys | 0:a14eaa2e1445 | 53 | u8 status; |
tecnosys | 0:a14eaa2e1445 | 54 | u16 handle; |
tecnosys | 0:a14eaa2e1445 | 55 | BD_ADDR bdaddr; |
tecnosys | 0:a14eaa2e1445 | 56 | u8 link_type; |
tecnosys | 0:a14eaa2e1445 | 57 | u8 encr_mode; |
tecnosys | 0:a14eaa2e1445 | 58 | } connection_info; |
tecnosys | 0:a14eaa2e1445 | 59 | |
tecnosys | 0:a14eaa2e1445 | 60 | // Address struct for creating L2CAP sockets |
tecnosys | 0:a14eaa2e1445 | 61 | typedef struct { |
tecnosys | 0:a14eaa2e1445 | 62 | SocketAddrHdr hdr; |
tecnosys | 0:a14eaa2e1445 | 63 | BD_ADDR bdaddr; |
tecnosys | 0:a14eaa2e1445 | 64 | u16 psm; |
tecnosys | 0:a14eaa2e1445 | 65 | } L2CAPAddr; |
tecnosys | 0:a14eaa2e1445 | 66 | |
tecnosys | 0:a14eaa2e1445 | 67 | #pragma pack(4) |
tecnosys | 0:a14eaa2e1445 | 68 | |
tecnosys | 0:a14eaa2e1445 | 69 | class BTDevice; |
tecnosys | 0:a14eaa2e1445 | 70 | typedef struct |
tecnosys | 0:a14eaa2e1445 | 71 | { |
tecnosys | 0:a14eaa2e1445 | 72 | public: |
tecnosys | 0:a14eaa2e1445 | 73 | SocketInternal si; |
tecnosys | 0:a14eaa2e1445 | 74 | BTDevice* btdevice; |
tecnosys | 0:a14eaa2e1445 | 75 | u16 scid; |
tecnosys | 0:a14eaa2e1445 | 76 | u16 dcid; |
tecnosys | 0:a14eaa2e1445 | 77 | } L2CAPSocket; |
tecnosys | 0:a14eaa2e1445 | 78 | |
tecnosys | 0:a14eaa2e1445 | 79 | #define MAX_HCL_NAME_LENGTH 20 // TODO - BTDevice wants to be a multiple of 4 |
tecnosys | 0:a14eaa2e1445 | 80 | |
tecnosys | 0:a14eaa2e1445 | 81 | // BTDevice encapsulates individual device state |
tecnosys | 0:a14eaa2e1445 | 82 | // It provides L2CAP layer sockets |
tecnosys | 0:a14eaa2e1445 | 83 | |
tecnosys | 0:a14eaa2e1445 | 84 | class BTDevice : public SocketHandler |
tecnosys | 0:a14eaa2e1445 | 85 | { |
tecnosys | 0:a14eaa2e1445 | 86 | public: |
tecnosys | 0:a14eaa2e1445 | 87 | HCITransport* _transport; |
tecnosys | 0:a14eaa2e1445 | 88 | inquiry_info _info; |
tecnosys | 0:a14eaa2e1445 | 89 | u16 _handle; // acl connection handle |
tecnosys | 0:a14eaa2e1445 | 90 | u8 _state; // connection state |
tecnosys | 0:a14eaa2e1445 | 91 | u8 _txid; |
tecnosys | 0:a14eaa2e1445 | 92 | char _name[MAX_HCL_NAME_LENGTH]; |
tecnosys | 0:a14eaa2e1445 | 93 | |
tecnosys | 0:a14eaa2e1445 | 94 | void Init(); |
tecnosys | 0:a14eaa2e1445 | 95 | |
tecnosys | 0:a14eaa2e1445 | 96 | BD_ADDR* GetAddress() { return &_info.bdaddr; } |
tecnosys | 0:a14eaa2e1445 | 97 | |
tecnosys | 0:a14eaa2e1445 | 98 | // Called from HCI |
tecnosys | 0:a14eaa2e1445 | 99 | void ACLRecv(const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 100 | |
tecnosys | 0:a14eaa2e1445 | 101 | // SocketHandler |
tecnosys | 0:a14eaa2e1445 | 102 | virtual int Open(SocketInternal* sock, SocketAddrHdr* addr); |
tecnosys | 0:a14eaa2e1445 | 103 | virtual int Send(SocketInternal* sock, const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 104 | virtual int Close(SocketInternal* sock); |
tecnosys | 0:a14eaa2e1445 | 105 | |
tecnosys | 0:a14eaa2e1445 | 106 | private: |
tecnosys | 0:a14eaa2e1445 | 107 | L2CAPSocket* SCIDToSocket(int scid); |
tecnosys | 0:a14eaa2e1445 | 108 | int Send(const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 109 | int Send(u8 c, u8 id, u16* params, int count); |
tecnosys | 0:a14eaa2e1445 | 110 | int Connect(int scid, int psm); |
tecnosys | 0:a14eaa2e1445 | 111 | int Disconnect(int scid, int dcid); |
tecnosys | 0:a14eaa2e1445 | 112 | int ConfigureRequest(int dcid); |
tecnosys | 0:a14eaa2e1445 | 113 | int ConfigureResponse(u8 rxid, int dcid); |
tecnosys | 0:a14eaa2e1445 | 114 | int DisconnectResponse(u8 rxid, int scid, int dcid); |
tecnosys | 0:a14eaa2e1445 | 115 | void Control(const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 116 | }; |
tecnosys | 0:a14eaa2e1445 | 117 | |
tecnosys | 0:a14eaa2e1445 | 118 | enum HCI_CALLBACK_EVENT |
tecnosys | 0:a14eaa2e1445 | 119 | { |
tecnosys | 0:a14eaa2e1445 | 120 | CALLBACK_NONE, |
tecnosys | 0:a14eaa2e1445 | 121 | CALLBACK_READY, |
tecnosys | 0:a14eaa2e1445 | 122 | CALLBACK_INQUIRY_RESULT, |
tecnosys | 0:a14eaa2e1445 | 123 | CALLBACK_INQUIRY_DONE, |
tecnosys | 0:a14eaa2e1445 | 124 | CALLBACK_REMOTE_NAME, |
tecnosys | 0:a14eaa2e1445 | 125 | CALLBACK_CONNECTION_COMPLETE, |
tecnosys | 0:a14eaa2e1445 | 126 | CALLBACK_CONNECTION_FAILED |
tecnosys | 0:a14eaa2e1445 | 127 | }; |
tecnosys | 0:a14eaa2e1445 | 128 | |
tecnosys | 0:a14eaa2e1445 | 129 | // L2CAP Protocol/Service Multiplexor (PSM) values |
tecnosys | 0:a14eaa2e1445 | 130 | |
tecnosys | 0:a14eaa2e1445 | 131 | #define L2CAP_PSM_ANY 0x0000 /* Any/Invalid PSM */ |
tecnosys | 0:a14eaa2e1445 | 132 | #define L2CAP_PSM_SDP 0x0001 /* Service Discovery Protocol */ |
tecnosys | 0:a14eaa2e1445 | 133 | #define L2CAP_PSM_RFCOMM 0x0003 /* RFCOMM protocol */ |
tecnosys | 0:a14eaa2e1445 | 134 | #define L2CAP_PSM_TCP 0x0005 /* Telephony Control Protocol */ |
tecnosys | 0:a14eaa2e1445 | 135 | #define L2CAP_PSM_TCS 0x0007 /* TCS cordless */ |
tecnosys | 0:a14eaa2e1445 | 136 | #define L2CAP_PSM_BNEP 0x000f /* Bluetooth Network Encapsulation Protocol*/ |
tecnosys | 0:a14eaa2e1445 | 137 | #define L2CAP_PSM_HID_CNTL 0x0011 /* HID Control */ |
tecnosys | 0:a14eaa2e1445 | 138 | #define L2CAP_PSM_HID_INTR 0x0013 /* HID Interrupt */ |
tecnosys | 0:a14eaa2e1445 | 139 | #define L2CAP_PSM_ESDP 0x0015 /* Extended Service Discovery Profile */ |
tecnosys | 0:a14eaa2e1445 | 140 | #define L2CAP_PSM_AVCTP 0x0017 /* Audio/Visual Control Transport Protocol */ |
tecnosys | 0:a14eaa2e1445 | 141 | #define L2CAP_PSM_AVDTP 0x0019 /* Audio/Visual Distribution */ |
tecnosys | 0:a14eaa2e1445 | 142 | |
tecnosys | 0:a14eaa2e1445 | 143 | // Callback from inquiry |
tecnosys | 0:a14eaa2e1445 | 144 | typedef int (*HCICallback)(HCI* hci, HCI_CALLBACK_EVENT evt, const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 145 | |
tecnosys | 0:a14eaa2e1445 | 146 | #define MAX_BTDEVICES 8 |
tecnosys | 0:a14eaa2e1445 | 147 | |
tecnosys | 0:a14eaa2e1445 | 148 | class HCITransport; |
tecnosys | 0:a14eaa2e1445 | 149 | class HCI : public SocketHandler |
tecnosys | 0:a14eaa2e1445 | 150 | { |
tecnosys | 0:a14eaa2e1445 | 151 | HCITransport* _transport; |
tecnosys | 0:a14eaa2e1445 | 152 | HCICallback _callback; |
tecnosys | 0:a14eaa2e1445 | 153 | BD_ADDR _localAddr; |
tecnosys | 0:a14eaa2e1445 | 154 | |
tecnosys | 0:a14eaa2e1445 | 155 | BTDevice _devices[MAX_BTDEVICES]; |
tecnosys | 0:a14eaa2e1445 | 156 | int _deviceCount; |
tecnosys | 0:a14eaa2e1445 | 157 | |
tecnosys | 0:a14eaa2e1445 | 158 | int _acl_mtu; |
tecnosys | 0:a14eaa2e1445 | 159 | int _acl_max_pkt; |
tecnosys | 0:a14eaa2e1445 | 160 | int _sco_mtu; |
tecnosys | 0:a14eaa2e1445 | 161 | int _sco_max_pkt; |
tecnosys | 0:a14eaa2e1445 | 162 | |
tecnosys | 0:a14eaa2e1445 | 163 | int _state; |
tecnosys | 0:a14eaa2e1445 | 164 | |
tecnosys | 0:a14eaa2e1445 | 165 | public: |
tecnosys | 0:a14eaa2e1445 | 166 | |
tecnosys | 0:a14eaa2e1445 | 167 | // Open a local adapter |
tecnosys | 0:a14eaa2e1445 | 168 | int Open(HCITransport* transport, HCICallback callback); |
tecnosys | 0:a14eaa2e1445 | 169 | |
tecnosys | 0:a14eaa2e1445 | 170 | // Return list of discovered addreses |
tecnosys | 0:a14eaa2e1445 | 171 | int GetDevices(BTDevice** devices, int maxDevices); |
tecnosys | 0:a14eaa2e1445 | 172 | |
tecnosys | 0:a14eaa2e1445 | 173 | // Lookup a device by address or handle |
tecnosys | 0:a14eaa2e1445 | 174 | BTDevice* Find(const BD_ADDR* addr); |
tecnosys | 0:a14eaa2e1445 | 175 | BTDevice* Find(int handle); |
tecnosys | 0:a14eaa2e1445 | 176 | |
tecnosys | 0:a14eaa2e1445 | 177 | // Disconnect from a remote device |
tecnosys | 0:a14eaa2e1445 | 178 | int Disconnect(const BD_ADDR* addr); |
tecnosys | 0:a14eaa2e1445 | 179 | int DisconnectAll(); |
tecnosys | 0:a14eaa2e1445 | 180 | |
tecnosys | 0:a14eaa2e1445 | 181 | // see what devies are in the system |
tecnosys | 0:a14eaa2e1445 | 182 | int Inquiry(int duration = 10); |
tecnosys | 0:a14eaa2e1445 | 183 | |
tecnosys | 0:a14eaa2e1445 | 184 | // get a name, delivered in callback |
tecnosys | 0:a14eaa2e1445 | 185 | int RemoteNameRequest(const BD_ADDR* addr); |
tecnosys | 0:a14eaa2e1445 | 186 | |
tecnosys | 0:a14eaa2e1445 | 187 | // Connect to a remote device |
tecnosys | 0:a14eaa2e1445 | 188 | int CreateConnection(const BD_ADDR* remoteAddr); |
tecnosys | 0:a14eaa2e1445 | 189 | |
tecnosys | 0:a14eaa2e1445 | 190 | bool Busy(); |
tecnosys | 0:a14eaa2e1445 | 191 | |
tecnosys | 0:a14eaa2e1445 | 192 | // called from transport |
tecnosys | 0:a14eaa2e1445 | 193 | void HCIRecv(const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 194 | |
tecnosys | 0:a14eaa2e1445 | 195 | // called from transport |
tecnosys | 0:a14eaa2e1445 | 196 | void ACLRecv(const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 197 | |
tecnosys | 0:a14eaa2e1445 | 198 | // SocketHandler methods for maintaining L2CAP sockets |
tecnosys | 0:a14eaa2e1445 | 199 | virtual int Open(SocketInternal* sock, SocketAddrHdr* addr); |
tecnosys | 0:a14eaa2e1445 | 200 | virtual int Send(SocketInternal* sock, const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 201 | virtual int Close(SocketInternal* sock); |
tecnosys | 0:a14eaa2e1445 | 202 | |
tecnosys | 0:a14eaa2e1445 | 203 | private: |
tecnosys | 0:a14eaa2e1445 | 204 | void InquiryResult(const inquiry_info* info); |
tecnosys | 0:a14eaa2e1445 | 205 | void RemoteName(const BD_ADDR* addr, const char* name); |
tecnosys | 0:a14eaa2e1445 | 206 | void ConnectComplete(const connection_info* info); |
tecnosys | 0:a14eaa2e1445 | 207 | void DisconnectComplete(int handle); |
tecnosys | 0:a14eaa2e1445 | 208 | int SendCmd(int cmd, const u8* params = 0, int len = 0); |
tecnosys | 0:a14eaa2e1445 | 209 | void OnCommandComplete(int cmd, const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 210 | void Callback(HCI_CALLBACK_EVENT c, const u8* data, int len); |
tecnosys | 0:a14eaa2e1445 | 211 | int PinCodeReply(const u8* data); |
tecnosys | 0:a14eaa2e1445 | 212 | }; |
tecnosys | 0:a14eaa2e1445 | 213 | |
tecnosys | 0:a14eaa2e1445 | 214 | class HCITransport |
tecnosys | 0:a14eaa2e1445 | 215 | { |
tecnosys | 0:a14eaa2e1445 | 216 | protected: |
tecnosys | 0:a14eaa2e1445 | 217 | HCI* _target; |
tecnosys | 0:a14eaa2e1445 | 218 | public: |
tecnosys | 0:a14eaa2e1445 | 219 | void Set(HCI* target) { _target = target; }; |
tecnosys | 0:a14eaa2e1445 | 220 | virtual void HCISend(const u8* data, int len) = 0; |
tecnosys | 0:a14eaa2e1445 | 221 | virtual void ACLSend(const u8* data, int len) = 0; |
tecnosys | 0:a14eaa2e1445 | 222 | }; |
tecnosys | 0:a14eaa2e1445 | 223 | |
tecnosys | 0:a14eaa2e1445 | 224 | #endif |