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
radiojunkbox 0:79620c558b0c 24 #include <stdio.h>
radiojunkbox 0:79620c558b0c 25 #include <stdlib.h>
radiojunkbox 0:79620c558b0c 26 #include <stdio.h>
radiojunkbox 0:79620c558b0c 27 #include <string.h>
radiojunkbox 0:79620c558b0c 28
radiojunkbox 0:79620c558b0c 29 #include "Utils.h"
radiojunkbox 0:79620c558b0c 30 #include "hci.h"
radiojunkbox 0:79620c558b0c 31
radiojunkbox 0:79620c558b0c 32 #define L2CAP_COMMAND_REJ 0x01
radiojunkbox 0:79620c558b0c 33 #define L2CAP_CONN_REQ 0x02
radiojunkbox 0:79620c558b0c 34 #define L2CAP_CONN_RSP 0x03
radiojunkbox 0:79620c558b0c 35 #define L2CAP_CONF_REQ 0x04
radiojunkbox 0:79620c558b0c 36 #define L2CAP_CONF_RSP 0x05
radiojunkbox 0:79620c558b0c 37 #define L2CAP_DISCONN_REQ 0x06
radiojunkbox 0:79620c558b0c 38 #define L2CAP_DISCONN_RSP 0x07
radiojunkbox 0:79620c558b0c 39 #define L2CAP_ECHO_REQ 0x08
radiojunkbox 0:79620c558b0c 40 #define L2CAP_ECHO_RSP 0x09
radiojunkbox 0:79620c558b0c 41 #define L2CAP_INFO_REQ 0x0a
radiojunkbox 0:79620c558b0c 42 #define L2CAP_INFO_RSP 0x0b
radiojunkbox 0:79620c558b0c 43
radiojunkbox 0:79620c558b0c 44
radiojunkbox 0:79620c558b0c 45 /* L2CAP command codes */
radiojunkbox 0:79620c558b0c 46 const char* L2CAP_ComandCodeStr(int c)
radiojunkbox 0:79620c558b0c 47 {
radiojunkbox 0:79620c558b0c 48 switch (c)
radiojunkbox 0:79620c558b0c 49 {
radiojunkbox 0:79620c558b0c 50 case L2CAP_COMMAND_REJ: return "L2CAP_COMMAND_REJ";
radiojunkbox 0:79620c558b0c 51 case L2CAP_CONN_REQ: return "L2CAP_CONN_REQ";
radiojunkbox 0:79620c558b0c 52 case L2CAP_CONN_RSP: return "L2CAP_CONN_RSP";
radiojunkbox 0:79620c558b0c 53 case L2CAP_CONF_REQ: return "L2CAP_CONF_REQ";
radiojunkbox 0:79620c558b0c 54 case L2CAP_CONF_RSP: return "L2CAP_CONF_RSP";
radiojunkbox 0:79620c558b0c 55 case L2CAP_DISCONN_REQ: return "L2CAP_DISCONN_REQ";
radiojunkbox 0:79620c558b0c 56 case L2CAP_DISCONN_RSP: return "L2CAP_DISCONN_RSP";
radiojunkbox 0:79620c558b0c 57 case L2CAP_ECHO_REQ: return "L2CAP_ECHO_REQ";
radiojunkbox 0:79620c558b0c 58 case L2CAP_ECHO_RSP: return "L2CAP_ECHO_RSP";
radiojunkbox 0:79620c558b0c 59 case L2CAP_INFO_REQ: return "L2CAP_INFO_REQ";
radiojunkbox 0:79620c558b0c 60 case L2CAP_INFO_RSP: return "L2CAP_INFO_RSP";
radiojunkbox 0:79620c558b0c 61 }
radiojunkbox 0:79620c558b0c 62 return "unknown";
radiojunkbox 0:79620c558b0c 63 }
radiojunkbox 0:79620c558b0c 64
radiojunkbox 0:79620c558b0c 65 typedef struct
radiojunkbox 0:79620c558b0c 66 {
radiojunkbox 0:79620c558b0c 67 u16 handle;
radiojunkbox 0:79620c558b0c 68 u16 length; // total
radiojunkbox 0:79620c558b0c 69 u16 l2capLength; // length -4
radiojunkbox 0:79620c558b0c 70 u16 cid; // Signaling packet CID = 1
radiojunkbox 0:79620c558b0c 71 u8 data[64]; // Largest thing to send!!! todo
radiojunkbox 0:79620c558b0c 72 } L2CAPData;
radiojunkbox 0:79620c558b0c 73
radiojunkbox 0:79620c558b0c 74 typedef struct
radiojunkbox 0:79620c558b0c 75 {
radiojunkbox 0:79620c558b0c 76 u16 handle;
radiojunkbox 0:79620c558b0c 77 u16 length; // total
radiojunkbox 0:79620c558b0c 78 u16 l2capLength; // length -4
radiojunkbox 0:79620c558b0c 79 u16 cid; // Signaling packet CID = 1
radiojunkbox 0:79620c558b0c 80
radiojunkbox 0:79620c558b0c 81 // Payload
radiojunkbox 0:79620c558b0c 82 u8 cmd; //
radiojunkbox 0:79620c558b0c 83 u8 id;
radiojunkbox 0:79620c558b0c 84 u16 cmdLength; // total-8
radiojunkbox 0:79620c558b0c 85 u16 params[4]; // Params
radiojunkbox 0:79620c558b0c 86 } L2CAPCmd;
radiojunkbox 0:79620c558b0c 87
radiojunkbox 0:79620c558b0c 88 //
radiojunkbox 0:79620c558b0c 89 void BTDevice::Init()
radiojunkbox 0:79620c558b0c 90 {
radiojunkbox 0:79620c558b0c 91 memset(&_info,0,sizeof(inquiry_info));
radiojunkbox 0:79620c558b0c 92 _handle = 0;
radiojunkbox 0:79620c558b0c 93 _name[0] = 0;
radiojunkbox 0:79620c558b0c 94 _state = 0;
radiojunkbox 0:79620c558b0c 95 }
radiojunkbox 0:79620c558b0c 96
radiojunkbox 0:79620c558b0c 97 // virtual SocketHandler
radiojunkbox 0:79620c558b0c 98 int BTDevice::Open(SocketInternal* sock, SocketAddrHdr* addr)
radiojunkbox 0:79620c558b0c 99 {
radiojunkbox 0:79620c558b0c 100 L2CAPSocket* s = (L2CAPSocket*)sock;
radiojunkbox 0:79620c558b0c 101 L2CAPAddr* a = (L2CAPAddr*)addr;
radiojunkbox 0:79620c558b0c 102 s->scid = 0x40 + sock->ID-1; // are these reserved?
radiojunkbox 0:79620c558b0c 103 s->dcid = 0;
radiojunkbox 0:79620c558b0c 104 Connect(s->scid,a->psm);
radiojunkbox 0:79620c558b0c 105 return sock->ID;
radiojunkbox 0:79620c558b0c 106 }
radiojunkbox 0:79620c558b0c 107
radiojunkbox 0:79620c558b0c 108 // virtual SocketHandler
radiojunkbox 0:79620c558b0c 109 int BTDevice::Send(SocketInternal* sock, const u8* data, int len)
radiojunkbox 0:79620c558b0c 110 {
radiojunkbox 0:79620c558b0c 111 L2CAPData d;
radiojunkbox 0:79620c558b0c 112 L2CAPSocket* s = (L2CAPSocket*)sock;
radiojunkbox 0:79620c558b0c 113
radiojunkbox 0:79620c558b0c 114 d.handle = _handle | 0x2000;
radiojunkbox 0:79620c558b0c 115 d.length = 4 + len;
radiojunkbox 0:79620c558b0c 116 d.l2capLength = len;
radiojunkbox 0:79620c558b0c 117 d.cid = s->dcid;
radiojunkbox 0:79620c558b0c 118
radiojunkbox 0:79620c558b0c 119 if (len > 64)
radiojunkbox 0:79620c558b0c 120 return -1;
radiojunkbox 0:79620c558b0c 121 memcpy(d.data,data,len);
radiojunkbox 0:79620c558b0c 122 return Send((u8*)&d,len+8);
radiojunkbox 0:79620c558b0c 123 }
radiojunkbox 0:79620c558b0c 124
radiojunkbox 0:79620c558b0c 125 // virtual SocketHandler
radiojunkbox 0:79620c558b0c 126 int BTDevice::Close(SocketInternal* sock)
radiojunkbox 0:79620c558b0c 127 {
radiojunkbox 0:79620c558b0c 128 printf("L2CAP close %d\n",sock->ID);
radiojunkbox 0:79620c558b0c 129 L2CAPSocket* s = (L2CAPSocket*)sock;
radiojunkbox 0:79620c558b0c 130 return Disconnect(s->scid,s->dcid);
radiojunkbox 0:79620c558b0c 131 }
radiojunkbox 0:79620c558b0c 132
radiojunkbox 0:79620c558b0c 133 L2CAPSocket* BTDevice::SCIDToSocket(int scid)
radiojunkbox 0:79620c558b0c 134 {
radiojunkbox 0:79620c558b0c 135 return (L2CAPSocket*)GetSocketInternal(scid-0x40+1);
radiojunkbox 0:79620c558b0c 136 }
radiojunkbox 0:79620c558b0c 137
radiojunkbox 0:79620c558b0c 138 int BTDevice::Send(const u8* data, int len)
radiojunkbox 0:79620c558b0c 139 {
radiojunkbox 0:79620c558b0c 140 _transport->ACLSend(data,len);
radiojunkbox 0:79620c558b0c 141 return 0;
radiojunkbox 0:79620c558b0c 142 }
radiojunkbox 0:79620c558b0c 143
radiojunkbox 0:79620c558b0c 144 int BTDevice::Send(u8 c, u8 id, u16* params, int count)
radiojunkbox 0:79620c558b0c 145 {
radiojunkbox 0:79620c558b0c 146 L2CAPCmd cmd;
radiojunkbox 0:79620c558b0c 147 cmd.handle = _handle | 0x2000;
radiojunkbox 0:79620c558b0c 148 cmd.length = 8 + count*2;
radiojunkbox 0:79620c558b0c 149
radiojunkbox 0:79620c558b0c 150 cmd.l2capLength = cmd.length-4;
radiojunkbox 0:79620c558b0c 151 cmd.cid = 1; // Signaling packet
radiojunkbox 0:79620c558b0c 152
radiojunkbox 0:79620c558b0c 153 cmd.cmd = c;
radiojunkbox 0:79620c558b0c 154 cmd.id = id;
radiojunkbox 0:79620c558b0c 155 cmd.cmdLength = count*2;
radiojunkbox 0:79620c558b0c 156 for (int i = 0; i < count; i++)
radiojunkbox 0:79620c558b0c 157 cmd.params[i] = params[i];
radiojunkbox 0:79620c558b0c 158 return Send((u8*)&cmd,cmd.length+4);
radiojunkbox 0:79620c558b0c 159 }
radiojunkbox 0:79620c558b0c 160
radiojunkbox 0:79620c558b0c 161 int BTDevice::Connect(int scid, int psm)
radiojunkbox 0:79620c558b0c 162 {
radiojunkbox 0:79620c558b0c 163 u16 p[2];
radiojunkbox 0:79620c558b0c 164 p[0] = psm;
radiojunkbox 0:79620c558b0c 165 p[1] = scid;
radiojunkbox 0:79620c558b0c 166 return Send(L2CAP_CONN_REQ,_txid++,p,2);
radiojunkbox 0:79620c558b0c 167 }
radiojunkbox 0:79620c558b0c 168
radiojunkbox 0:79620c558b0c 169 int BTDevice::Disconnect(int scid, int dcid)
radiojunkbox 0:79620c558b0c 170 {
radiojunkbox 0:79620c558b0c 171 u16 p[2];
radiojunkbox 0:79620c558b0c 172 p[0] = dcid;
radiojunkbox 0:79620c558b0c 173 p[1] = scid;
radiojunkbox 0:79620c558b0c 174 return Send(L2CAP_DISCONN_REQ,_txid++,p,2);
radiojunkbox 0:79620c558b0c 175 }
radiojunkbox 0:79620c558b0c 176
radiojunkbox 0:79620c558b0c 177 int BTDevice::ConfigureRequest(int dcid)
radiojunkbox 0:79620c558b0c 178 {
radiojunkbox 0:79620c558b0c 179 u16 p[4];
radiojunkbox 0:79620c558b0c 180 p[0] = dcid;
radiojunkbox 0:79620c558b0c 181 p[1] = 0;
radiojunkbox 0:79620c558b0c 182 p[2] = 0x0201; // Options
radiojunkbox 0:79620c558b0c 183 p[3] = 0x02A0; // 672
radiojunkbox 0:79620c558b0c 184 return Send(L2CAP_CONF_REQ,_txid++,p,4);
radiojunkbox 0:79620c558b0c 185 }
radiojunkbox 0:79620c558b0c 186
radiojunkbox 0:79620c558b0c 187 int BTDevice::ConfigureResponse(u8 rxid, int dcid)
radiojunkbox 0:79620c558b0c 188 {
radiojunkbox 0:79620c558b0c 189 u16 p[3];
radiojunkbox 0:79620c558b0c 190 p[0] = dcid;
radiojunkbox 0:79620c558b0c 191 p[1] = 0;
radiojunkbox 0:79620c558b0c 192 p[2] = 0;
radiojunkbox 0:79620c558b0c 193 return Send(L2CAP_CONF_RSP,rxid,p,3);
radiojunkbox 0:79620c558b0c 194 }
radiojunkbox 0:79620c558b0c 195
radiojunkbox 0:79620c558b0c 196 int BTDevice::DisconnectResponse(u8 rxid, int scid, int dcid)
radiojunkbox 0:79620c558b0c 197 {
radiojunkbox 0:79620c558b0c 198 u16 p[2];
radiojunkbox 0:79620c558b0c 199 p[0] = dcid;
radiojunkbox 0:79620c558b0c 200 p[1] = scid;
radiojunkbox 0:79620c558b0c 201 return Send(L2CAP_DISCONN_RSP,rxid,p,2);
radiojunkbox 0:79620c558b0c 202 }
radiojunkbox 0:79620c558b0c 203
radiojunkbox 0:79620c558b0c 204 void BTDevice::Control(const u8* data, int len)
radiojunkbox 0:79620c558b0c 205 {
radiojunkbox 0:79620c558b0c 206 int cc = data[8];
radiojunkbox 0:79620c558b0c 207 printf(L2CAP_ComandCodeStr(cc));
radiojunkbox 0:79620c558b0c 208 int result = LE16(data+16);
radiojunkbox 0:79620c558b0c 209 printf(" Result %d\n",result);
radiojunkbox 0:79620c558b0c 210 switch (cc)
radiojunkbox 0:79620c558b0c 211 {
radiojunkbox 0:79620c558b0c 212 case L2CAP_COMMAND_REJ:
radiojunkbox 0:79620c558b0c 213 break;
radiojunkbox 0:79620c558b0c 214 case L2CAP_CONN_REQ:
radiojunkbox 0:79620c558b0c 215 break;
radiojunkbox 0:79620c558b0c 216
radiojunkbox 0:79620c558b0c 217 // Response to our initial connect from Remote
radiojunkbox 0:79620c558b0c 218 case L2CAP_CONN_RSP:
radiojunkbox 0:79620c558b0c 219 {
radiojunkbox 0:79620c558b0c 220 if (result == 0)
radiojunkbox 0:79620c558b0c 221 {
radiojunkbox 0:79620c558b0c 222 int dcid = LE16(data+12);
radiojunkbox 0:79620c558b0c 223 int scid = LE16(data+14);
radiojunkbox 0:79620c558b0c 224 L2CAPSocket* s = SCIDToSocket(scid);
radiojunkbox 0:79620c558b0c 225 if (s)
radiojunkbox 0:79620c558b0c 226 {
radiojunkbox 0:79620c558b0c 227 s->dcid = dcid;
radiojunkbox 0:79620c558b0c 228 ConfigureRequest(dcid);
radiojunkbox 0:79620c558b0c 229 }
radiojunkbox 0:79620c558b0c 230 } else
radiojunkbox 0:79620c558b0c 231 printf("Connect failed?\n");
radiojunkbox 0:79620c558b0c 232 }
radiojunkbox 0:79620c558b0c 233 break;
radiojunkbox 0:79620c558b0c 234
radiojunkbox 0:79620c558b0c 235 case L2CAP_CONF_RSP:
radiojunkbox 0:79620c558b0c 236 {
radiojunkbox 0:79620c558b0c 237 int scid = LE16(data+12);
radiojunkbox 0:79620c558b0c 238 SocketInternal* s = (SocketInternal*)SCIDToSocket(scid);
radiojunkbox 0:79620c558b0c 239 if (s)
radiojunkbox 0:79620c558b0c 240 s->SetState(SocketState_Open);
radiojunkbox 0:79620c558b0c 241 }
radiojunkbox 0:79620c558b0c 242 break;
radiojunkbox 0:79620c558b0c 243
radiojunkbox 0:79620c558b0c 244 case L2CAP_CONF_REQ:
radiojunkbox 0:79620c558b0c 245 {
radiojunkbox 0:79620c558b0c 246 int scid = LE16(data+12);
radiojunkbox 0:79620c558b0c 247 L2CAPSocket* s = SCIDToSocket(scid);
radiojunkbox 0:79620c558b0c 248 if (s)
radiojunkbox 0:79620c558b0c 249 ConfigureResponse(data[9],s->dcid);
radiojunkbox 0:79620c558b0c 250 }
radiojunkbox 0:79620c558b0c 251 break;
radiojunkbox 0:79620c558b0c 252 }
radiojunkbox 0:79620c558b0c 253 }
radiojunkbox 0:79620c558b0c 254
radiojunkbox 0:79620c558b0c 255 void BTDevice::ACLRecv(const u8* data, int len)
radiojunkbox 0:79620c558b0c 256 {
radiojunkbox 0:79620c558b0c 257 // printfBytes("L2CP",data,16);
radiojunkbox 0:79620c558b0c 258 int handle = LE16(data);
radiojunkbox 0:79620c558b0c 259 if (handle != (0x2000 | _handle))
radiojunkbox 0:79620c558b0c 260 return;
radiojunkbox 0:79620c558b0c 261
radiojunkbox 0:79620c558b0c 262 int cid = LE16(data+6);
radiojunkbox 0:79620c558b0c 263 if (cid == 1)
radiojunkbox 0:79620c558b0c 264 {
radiojunkbox 0:79620c558b0c 265 Control(data,len);
radiojunkbox 0:79620c558b0c 266 return;
radiojunkbox 0:79620c558b0c 267 }
radiojunkbox 0:79620c558b0c 268
radiojunkbox 0:79620c558b0c 269 SocketInternal* s = (SocketInternal*)SCIDToSocket(cid);
radiojunkbox 0:79620c558b0c 270 if (s)
radiojunkbox 0:79620c558b0c 271 s->Recv(data+8,LE16(data+2)-4);
radiojunkbox 0:79620c558b0c 272 else
radiojunkbox 0:79620c558b0c 273 printf("Bad event cid %d\n",cid);
radiojunkbox 0:79620c558b0c 274 }