This program uses code taken from another program called BlueUSB

Dependencies:   mbed

Committer:
madcowswe
Date:
Sat Dec 10 18:45:31 2011 +0000
Revision:
0:31713f62f35b

        

Who changed what in which revision?

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