Test Ver

Dependencies:   mbed FatFileSystem

Committer:
jksoft
Date:
Sat Nov 17 13:22:00 2012 +0000
Revision:
0:269589d8d2c2
Test Program

Who changed what in which revision?

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