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