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

