Dependencies:   mbed

Committer:
abe00makoto
Date:
Thu May 26 19:39:37 2011 +0000
Revision:
1:237cfff63ef8
Parent:
0:e939856c1939

        

Who changed what in which revision?

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