takayuki ikai / Mbed 2 deprecated BlueWii

Dependencies:   mbed

Committer:
ikaii
Date:
Mon Dec 06 17:02:12 2010 +0000
Revision:
0:010465683d59

        

Who changed what in which revision?

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