Peter Barrett氏のBlueUSBにMIDI USB HOST機能を加えたサンプルプログラムです。KORG nanoKEYなどのUSB MIDIストリームをシリアルMIDI(Serial TX p9)にブリッジします。動作確認はKORG nanoKEY、AKAI LPK-25、EDIROL PC-50のみです。

Dependencies:   mbed

Committer:
radiojunkbox
Date:
Fri May 11 10:05:40 2012 +0000
Revision:
0:79620c558b0c
Rev. 1.0

Who changed what in which revision?

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