This is a for debugging \\\\\\\"BLUE USB\\\\\\\". You can connect with HCI mode. How to connect White Wizard Board TANK *White Wizard Board - Motor Driver Board * p 21 - IN_R1 * p 22 - IN_R2 * p 23 - IN_L2 * p 24 - IN_L1

Dependencies:   mbed

Committer:
halfpitch
Date:
Wed Aug 31 11:10:18 2011 +0000
Revision:
1:c56059923036
Parent:
0:a6476c138e84
Rev.B

Who changed what in which revision?

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