Based on myBlueUSB reference ver. http://mbed.org/users/networker/programs/myBlueUSB/lsm1ui

Dependencies:   mbed myUSBHost AvailableMemory rfcomm myBlueUSB sdp

Committer:
kenbumono
Date:
Tue Jul 05 08:25:59 2011 +0000
Revision:
0:8d8481ed6d49

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenbumono 0:8d8481ed6d49 1
kenbumono 0:8d8481ed6d49 2 /*
kenbumono 0:8d8481ed6d49 3 Copyright (c) 2010 Peter Barrett
kenbumono 0:8d8481ed6d49 4
kenbumono 0:8d8481ed6d49 5 Permission is hereby granted, free of charge, to any person obtaining a copy
kenbumono 0:8d8481ed6d49 6 of this software and associated documentation files (the "Software"), to deal
kenbumono 0:8d8481ed6d49 7 in the Software without restriction, including without limitation the rights
kenbumono 0:8d8481ed6d49 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kenbumono 0:8d8481ed6d49 9 copies of the Software, and to permit persons to whom the Software is
kenbumono 0:8d8481ed6d49 10 furnished to do so, subject to the following conditions:
kenbumono 0:8d8481ed6d49 11
kenbumono 0:8d8481ed6d49 12 The above copyright notice and this permission notice shall be included in
kenbumono 0:8d8481ed6d49 13 all copies or substantial portions of the Software.
kenbumono 0:8d8481ed6d49 14
kenbumono 0:8d8481ed6d49 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kenbumono 0:8d8481ed6d49 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kenbumono 0:8d8481ed6d49 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kenbumono 0:8d8481ed6d49 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kenbumono 0:8d8481ed6d49 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenbumono 0:8d8481ed6d49 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
kenbumono 0:8d8481ed6d49 21 THE SOFTWARE.
kenbumono 0:8d8481ed6d49 22 */
kenbumono 0:8d8481ed6d49 23
kenbumono 0:8d8481ed6d49 24 #include "mbed.h"
kenbumono 0:8d8481ed6d49 25 #include "USBHost.h"
kenbumono 0:8d8481ed6d49 26 #include "Utils.h"
kenbumono 0:8d8481ed6d49 27
kenbumono 0:8d8481ed6d49 28 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
kenbumono 0:8d8481ed6d49 29 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1)
kenbumono 0:8d8481ed6d49 30 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2)
kenbumono 0:8d8481ed6d49 31
kenbumono 0:8d8481ed6d49 32 u8 auto_mouse[4]; // buttons,dx,dy,scroll
kenbumono 0:8d8481ed6d49 33 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6
kenbumono 0:8d8481ed6d49 34 u8 auto_joystick[4]; // x,y,buttons,throttle
kenbumono 0:8d8481ed6d49 35
kenbumono 0:8d8481ed6d49 36 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
kenbumono 0:8d8481ed6d49 37 {
kenbumono 0:8d8481ed6d49 38 int evt = (int)userData;
kenbumono 0:8d8481ed6d49 39 switch (evt)
kenbumono 0:8d8481ed6d49 40 {
kenbumono 0:8d8481ed6d49 41 case AUTO_KEYBOARD:
kenbumono 0:8d8481ed6d49 42 printf("AUTO_KEYBOARD ");
kenbumono 0:8d8481ed6d49 43 break;
kenbumono 0:8d8481ed6d49 44 case AUTO_MOUSE:
kenbumono 0:8d8481ed6d49 45 printf("AUTO_MOUSE ");
kenbumono 0:8d8481ed6d49 46 break;
kenbumono 0:8d8481ed6d49 47 default:
kenbumono 0:8d8481ed6d49 48 printf("HUH ");
kenbumono 0:8d8481ed6d49 49 }
kenbumono 0:8d8481ed6d49 50 printfBytes("data",data,len);
kenbumono 0:8d8481ed6d49 51 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
kenbumono 0:8d8481ed6d49 52 }
kenbumono 0:8d8481ed6d49 53
kenbumono 0:8d8481ed6d49 54 // Establish transfers for interrupt events
kenbumono 0:8d8481ed6d49 55 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
kenbumono 0:8d8481ed6d49 56 {
kenbumono 0:8d8481ed6d49 57 if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
kenbumono 0:8d8481ed6d49 58 return;
kenbumono 0:8d8481ed6d49 59
kenbumono 0:8d8481ed6d49 60 // Make automatic interrupt enpoints for known devices
kenbumono 0:8d8481ed6d49 61 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
kenbumono 0:8d8481ed6d49 62 u8* dst = 0;
kenbumono 0:8d8481ed6d49 63 int len;
kenbumono 0:8d8481ed6d49 64 switch (evt)
kenbumono 0:8d8481ed6d49 65 {
kenbumono 0:8d8481ed6d49 66 case AUTO_MOUSE:
kenbumono 0:8d8481ed6d49 67 dst = auto_mouse;
kenbumono 0:8d8481ed6d49 68 len = sizeof(auto_mouse);
kenbumono 0:8d8481ed6d49 69 break;
kenbumono 0:8d8481ed6d49 70 case AUTO_KEYBOARD:
kenbumono 0:8d8481ed6d49 71 dst = auto_keyboard;
kenbumono 0:8d8481ed6d49 72 len = sizeof(auto_keyboard);
kenbumono 0:8d8481ed6d49 73 break;
kenbumono 0:8d8481ed6d49 74 default:
kenbumono 0:8d8481ed6d49 75 printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt);
kenbumono 0:8d8481ed6d49 76 break;
kenbumono 0:8d8481ed6d49 77 }
kenbumono 0:8d8481ed6d49 78 if (dst)
kenbumono 0:8d8481ed6d49 79 {
kenbumono 0:8d8481ed6d49 80 printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt);
kenbumono 0:8d8481ed6d49 81 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
kenbumono 0:8d8481ed6d49 82 }
kenbumono 0:8d8481ed6d49 83 }
kenbumono 0:8d8481ed6d49 84
kenbumono 0:8d8481ed6d49 85 void PrintString(int device, int i)
kenbumono 0:8d8481ed6d49 86 {
kenbumono 0:8d8481ed6d49 87 u8 buffer[256];
kenbumono 0:8d8481ed6d49 88 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
kenbumono 0:8d8481ed6d49 89 if (le < 0)
kenbumono 0:8d8481ed6d49 90 return;
kenbumono 0:8d8481ed6d49 91 char* dst = (char*)buffer;
kenbumono 0:8d8481ed6d49 92 for (int j = 2; j < le; j += 2)
kenbumono 0:8d8481ed6d49 93 *dst++ = buffer[j];
kenbumono 0:8d8481ed6d49 94 *dst = 0;
kenbumono 0:8d8481ed6d49 95 printf("%d:%s\n",i,(const char*)buffer);
kenbumono 0:8d8481ed6d49 96 }
kenbumono 0:8d8481ed6d49 97
kenbumono 0:8d8481ed6d49 98 // Walk descriptors and create endpoints for a given device
kenbumono 0:8d8481ed6d49 99 int StartAutoEvent(int device, int configuration, int interfaceNumber)
kenbumono 0:8d8481ed6d49 100 {
kenbumono 0:8d8481ed6d49 101 u8 buffer[255];
kenbumono 0:8d8481ed6d49 102 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
kenbumono 0:8d8481ed6d49 103 if (err < 0)
kenbumono 0:8d8481ed6d49 104 return err;
kenbumono 0:8d8481ed6d49 105
kenbumono 0:8d8481ed6d49 106 int len = buffer[2] | (buffer[3] << 8);
kenbumono 0:8d8481ed6d49 107 u8* d = buffer;
kenbumono 0:8d8481ed6d49 108 u8* end = d + len;
kenbumono 0:8d8481ed6d49 109 while (d < end)
kenbumono 0:8d8481ed6d49 110 {
kenbumono 0:8d8481ed6d49 111 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:8d8481ed6d49 112 {
kenbumono 0:8d8481ed6d49 113 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
kenbumono 0:8d8481ed6d49 114 if (id->bInterfaceNumber == interfaceNumber)
kenbumono 0:8d8481ed6d49 115 {
kenbumono 0:8d8481ed6d49 116 d += d[0];
kenbumono 0:8d8481ed6d49 117 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:8d8481ed6d49 118 {
kenbumono 0:8d8481ed6d49 119 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
kenbumono 0:8d8481ed6d49 120 AddAutoEvent(device,id,(EndpointDescriptor*)d);
kenbumono 0:8d8481ed6d49 121 d += d[0];
kenbumono 0:8d8481ed6d49 122 }
kenbumono 0:8d8481ed6d49 123 }
kenbumono 0:8d8481ed6d49 124 }
kenbumono 0:8d8481ed6d49 125 d += d[0];
kenbumono 0:8d8481ed6d49 126 }
kenbumono 0:8d8481ed6d49 127 return 0;
kenbumono 0:8d8481ed6d49 128 }
kenbumono 0:8d8481ed6d49 129
kenbumono 0:8d8481ed6d49 130 // Implemented in main.cpp
kenbumono 0:8d8481ed6d49 131 int OnDiskInsert(int device);
kenbumono 0:8d8481ed6d49 132
kenbumono 0:8d8481ed6d49 133 // Implemented in TestShell.cpp
kenbumono 0:8d8481ed6d49 134 int OnBluetoothInsert(int device);
kenbumono 0:8d8481ed6d49 135
kenbumono 0:8d8481ed6d49 136 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
kenbumono 0:8d8481ed6d49 137 {
kenbumono 0:8d8481ed6d49 138 printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
kenbumono 0:8d8481ed6d49 139 char s[128];
kenbumono 0:8d8481ed6d49 140 for (int i = 1; i < 3; i++)
kenbumono 0:8d8481ed6d49 141 {
kenbumono 0:8d8481ed6d49 142 if (GetString(device,i,s,sizeof(s)) < 0)
kenbumono 0:8d8481ed6d49 143 break;
kenbumono 0:8d8481ed6d49 144 printf("%d: %s\n",i,s);
kenbumono 0:8d8481ed6d49 145 }
kenbumono 0:8d8481ed6d49 146
kenbumono 0:8d8481ed6d49 147 switch (interfaceDesc->bInterfaceClass)
kenbumono 0:8d8481ed6d49 148 {
kenbumono 0:8d8481ed6d49 149 case CLASS_MASS_STORAGE:
kenbumono 0:8d8481ed6d49 150 if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
kenbumono 0:8d8481ed6d49 151 OnDiskInsert(device); // it's SCSI!
kenbumono 0:8d8481ed6d49 152 break;
kenbumono 0:8d8481ed6d49 153 case CLASS_WIRELESS_CONTROLLER:
kenbumono 0:8d8481ed6d49 154 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
kenbumono 0:8d8481ed6d49 155 OnBluetoothInsert(device); // it's bluetooth!
kenbumono 0:8d8481ed6d49 156 break;
kenbumono 0:8d8481ed6d49 157 default:
kenbumono 0:8d8481ed6d49 158 StartAutoEvent(device,1,0);
kenbumono 0:8d8481ed6d49 159 break;
kenbumono 0:8d8481ed6d49 160 }
kenbumono 0:8d8481ed6d49 161 }