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