Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FatFileSystem mbed
Fork of PS3_BlueUSB_Shibutsu by
AutoEvents.cpp
00001 /* 00002 Copyright (c) 2010 Peter Barrett 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 /* 00024 Tue Apr 26 2011 Bart Janssens: added PS3 USB support 00025 */ 00026 00027 #include <stdio.h> 00028 #include <stdlib.h> 00029 #include <stdio.h> 00030 #include <string.h> 00031 00032 #include "USBHost.h" 00033 #include "Utils.h" 00034 #include "ps3.h" 00035 00036 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) 00037 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1) 00038 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2) 00039 //#define AUTO_PS3 AUTOEVT(CLASS_HID,0,0) 00040 00041 u8 auto_mouse[4]; // buttons,dx,dy,scroll 00042 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6 00043 u8 auto_joystick[4]; // x,y,buttons,throttle 00044 //u8 auto_ps3[48]; 00045 00046 00047 00048 00049 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) 00050 { 00051 int evt = (int)userData; 00052 switch (evt) 00053 { 00054 case AUTO_KEYBOARD: 00055 printf("AUTO_KEYBOARD "); 00056 break; 00057 case AUTO_MOUSE: 00058 printf("AUTO_MOUSE "); 00059 break; 00060 // case AUTO_PS3: 00061 // printf("AUTO_PS3 "); 00062 // ParsePs3Report(data,len); 00063 // break; 00064 default: 00065 printf("HUH "); 00066 } 00067 //printfBytes("data",data,len); 00068 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData); 00069 } 00070 00071 // Establish transfers for interrupt events 00072 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed) 00073 { 00074 printf("message from endpoint %02X\r\n",ed->bEndpointAddress); 00075 printf("Class Sub Proto: %02X %02X %02X\r\n",id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); 00076 //if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) 00077 // return; 00078 00079 // Make automatic interrupt enpoints for known devices 00080 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); 00081 printf("Evt: %08X \r\n",evt); 00082 u8* dst = 0; 00083 int len; 00084 switch (evt) 00085 { 00086 case AUTO_MOUSE: 00087 dst = auto_mouse; 00088 len = sizeof(auto_mouse); 00089 break; 00090 case AUTO_KEYBOARD: 00091 dst = auto_keyboard; 00092 len = sizeof(auto_keyboard); 00093 break; 00094 // case AUTO_PS3: 00095 // printf("PS3 event ? \r\n"); 00096 // dst = auto_ps3; 00097 // len = sizeof(auto_ps3); 00098 default: 00099 printf("Interrupt endpoint %02X %08X\r\n",ed->bEndpointAddress,evt); 00100 break; 00101 } 00102 if (dst) 00103 { 00104 printf("Auto Event for %02X %08X\r\n",ed->bEndpointAddress,evt); 00105 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt); 00106 } 00107 } 00108 00109 void PrintString(int device, int i) 00110 { 00111 u8 buffer[256]; 00112 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255); 00113 if (le < 0) 00114 return; 00115 char* dst = (char*)buffer; 00116 for (int j = 2; j < le; j += 2) 00117 *dst++ = buffer[j]; 00118 *dst = 0; 00119 printf("%d:%s\r\n",i,(const char*)buffer); 00120 } 00121 00122 // Walk descriptors and create endpoints for a given device 00123 int StartAutoEvent(int device, int configuration, int interfaceNumber) 00124 { 00125 00126 printf("StartAutoEvent \r\n"); 00127 00128 u8 buffer[255]; 00129 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); 00130 if (err < 0) 00131 return err; 00132 00133 int len = buffer[2] | (buffer[3] << 8); 00134 u8* d = buffer; 00135 u8* end = d + len; 00136 while (d < end) 00137 { 00138 if (d[1] == DESCRIPTOR_TYPE_INTERFACE) 00139 { 00140 InterfaceDescriptor* id = (InterfaceDescriptor*)d; 00141 if (id->bInterfaceNumber == interfaceNumber) 00142 { 00143 d += d[0]; 00144 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) 00145 { 00146 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) 00147 AddAutoEvent(device,id,(EndpointDescriptor*)d); 00148 d += d[0]; 00149 } 00150 } 00151 } 00152 d += d[0]; 00153 } 00154 return 0; 00155 } 00156 00157 /* 00158 int StartPS3Event(int device, int configuration, int interfaceNumber) 00159 { 00160 00161 printf("StartPS3Event \r\n"); 00162 00163 EndpointDescriptor* ep; 00164 00165 u8 buf[4]; 00166 buf[0] = 0x42; 00167 buf[1] = 0x0c; 00168 buf[2] = 0x00; 00169 buf[3] = 0x00; 00170 00171 u8 buf2[8]; 00172 u8 buf3[8]; 00173 00174 buf2[0] = 0x01; 00175 buf2[1] = 0x00; 00176 buf2[2] = 0x00; 00177 buf2[3] = 0x02; 00178 buf2[4] = 0x72; 00179 buf2[5] = 0xAD; 00180 buf2[6] = 0xF3; 00181 buf2[7] = 0x5B; 00182 00183 00184 00185 00186 int result; 00187 int err; 00188 00189 u8 buffer[255]; 00190 err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); 00191 if (err < 0) 00192 return err; 00193 00194 00195 00196 //configure the device 00197 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_STANDARD|RECIPIENT_DEVICE, SET_CONFIGURATION, 1, 0, 0, 0, 0, 0 ); 00198 err = SetConfiguration(device,1); 00199 printf("set config result = %d\r\n", err); 00200 00201 // get Mac address 00202 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_DEVICE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf3, sizeof(buf3), 0, 0 ); 00203 //printf("get Mac to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf3[2], buf3[3], buf3[4], buf3[5], buf3[6], buf3[7], err); 00204 00205 // set Mac address 00206 err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf2, sizeof(buf2), 0, 0 ); 00207 printf("set Mac to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf2[2], buf2[3], buf2[4], buf2[5], buf2[6], buf2[7], err); 00208 00209 // get Mac address 00210 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_DEVICE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf3, sizeof(buf3), 0, 0 ); 00211 //printf("get Mac to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf3[2], buf3[3], buf3[4], buf3[5], buf3[6], buf3[7], err); 00212 00213 err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, buf, sizeof(buf), 0, 0 ); 00214 printf("set report result = %d\r\n", err); 00215 //USBTransfer(device,0,DEVICE_TO_HOST,buf,sizeof(buf),0,0); 00216 00217 int len = buffer[2] | (buffer[3] << 8); 00218 u8* d = buffer; 00219 u8* end = d + len; 00220 while (d < end) 00221 { 00222 if (d[1] == DESCRIPTOR_TYPE_INTERFACE) 00223 { 00224 InterfaceDescriptor* id = (InterfaceDescriptor*)d; 00225 if (id->bInterfaceNumber == interfaceNumber) 00226 { 00227 d += d[0]; 00228 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) 00229 { 00230 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) 00231 ep = (EndpointDescriptor*)d; 00232 00233 if (ep->bEndpointAddress == 0x02) { 00234 printf("PS3 input endpoint (0x02) found\r\n"); 00235 00236 } 00237 if (ep->bEndpointAddress == 0x81) { 00238 printf("PS3 output endpoint (0x81) found\r\n"); 00239 AddAutoEvent(device,id,(EndpointDescriptor*)d); 00240 } 00241 d += d[0]; 00242 } 00243 } 00244 } 00245 d += d[0]; 00246 } 00247 return 0; 00248 } 00249 */ 00250 00251 // Implemented in main.cpp 00252 int OnDiskInsert(int device); 00253 00254 // Implemented in TestShell.cpp 00255 int OnBluetoothInsert(int device); 00256 00257 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) 00258 { 00259 printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); 00260 char s[128]; 00261 //u8 my_mac[6] = {0x00,0x1b,0xdc,0x06,0x67,0x4e}; // mac address of my Bluetooth device(部活の) 00262 u8 my_mac[6]={0x00,0x1B,0xDC,0x06,0xD3,0x56};//(部活の2) 00263 //u8 my_mac[6]={0x00,0x1b,0xdc,0x0f,0x3f,0x43};//(私物) 00264 //00:1B:DC:06:67:4E 00265 00266 u8 buf2[6]; 00267 00268 buf2[0] = 0x00; 00269 buf2[1] = 0x02; 00270 buf2[2] = 0x72; 00271 buf2[3] = 0xAD; 00272 buf2[4] = 0xF3; 00273 buf2[5] = 0x5B; 00274 00275 00276 for (int i = 1; i < 3; i++) 00277 { 00278 if (GetString(device,i,s,sizeof(s)) < 0) 00279 break; 00280 printf("%d: %s\r\n",i,s); 00281 } 00282 00283 switch (interfaceDesc->bInterfaceClass) 00284 { 00285 case CLASS_MASS_STORAGE: 00286 if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50) 00287 OnDiskInsert(device); // it's SCSI! 00288 break; 00289 case CLASS_WIRELESS_CONTROLLER: 00290 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01) 00291 OnBluetoothInsert(device); // it's bluetooth! 00292 break; 00293 case CLASS_HID: 00294 printf("idVendor = %04X idProduct = %04X \r\n",deviceDesc->idVendor,deviceDesc->idProduct); 00295 //printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); 00296 //if (deviceDesc->idVendor == 0x054C && deviceDesc->idProduct == 0x0268) StartPS3Event(device,1,0); 00297 if (deviceDesc->idVendor == 0x054C && deviceDesc->idProduct == 0x0268) { 00298 Ps3USB _Ps3USB(device,1,0); 00299 00300 _Ps3USB.SetPair(my_mac); 00301 _Ps3USB.Enable(); 00302 _Ps3USB.Led(1); 00303 _Ps3USB.Rumble(0x20,0xff,0x20,0xff); 00304 _Ps3USB.ShowPair(); 00305 00306 } 00307 else StartAutoEvent(device,1,0); 00308 break; 00309 default: 00310 printf("Not yet supported \r\n"); 00311 //StartAutoEvent(device,1,0); 00312 break; 00313 } 00314 }
Generated on Thu Jul 14 2022 02:37:21 by
