Birdy YAP / Birdy_BlueWIi

Dependents:   Test_Wiimote

Committer:
bediyap
Date:
Sat Dec 17 02:26:07 2011 +0000
Revision:
1:b9acea6cfa66
Parent:
0:f6f434d9a03a
Child:
2:5c2bfbd63297
Disabled MassStorage R/W

Who changed what in which revision?

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