うおーるぼっと用プログラム Wiiリモコンからのダイレクト操作モードのみ BlueUSBをベースに使用しています。

Dependencies:   BD6211F mbed SimpleFilter

Committer:
jksoft
Date:
Fri Apr 29 15:50:23 2011 +0000
Revision:
0:4f749f62c6d7

        

Who changed what in which revision?

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