ORTP-L_WiiRemoteTest

Dependencies:   Motordriver mbed FatFileSystem

Fork of WallbotTypeN by Junichi Katsu

Committer:
passionvirus
Date:
Fri Mar 29 09:05:28 2013 +0000
Revision:
1:df4118878dc4
Parent:
0:425791fe4b42
init

Who changed what in which revision?

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