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: mbed
Fork of PS3conOut 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 #include <mbed.h> 00032 00033 #include "USBHost.h" 00034 #include "Utils.h" 00035 #include "ps3.h" 00036 00037 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) 00038 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1) 00039 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2) 00040 //#define AUTO_PS3 AUTOEVT(CLASS_HID,0,0) 00041 00042 u8 auto_mouse[4]; // buttons,dx,dy,scroll 00043 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6 00044 u8 auto_joystick[4]; // x,y,buttons,throttle 00045 //u8 auto_ps3[48]; 00046 00047 00048 00049 00050 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) 00051 { 00052 int evt = (int)userData; 00053 switch (evt) 00054 { 00055 case AUTO_KEYBOARD: 00056 printf("AUTO_KEYBOARD "); 00057 break; 00058 case AUTO_MOUSE: 00059 printf("AUTO_MOUSE "); 00060 break; 00061 // case AUTO_PS3: 00062 // printf("AUTO_PS3 "); 00063 // ParsePs3Report(data,len); 00064 // break; 00065 default: 00066 printf("HUH "); 00067 } 00068 //printfBytes("data",data,len); 00069 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData); 00070 } 00071 00072 // Establish transfers for interrupt events 00073 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed) 00074 { 00075 printf("message from endpoint %02X\r\n",ed->bEndpointAddress); 00076 printf("Class Sub Proto: %02X %02X %02X\r\n",id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); 00077 //if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80)) 00078 // return; 00079 00080 // Make automatic interrupt enpoints for known devices 00081 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol); 00082 printf("Evt: %08X \r\n",evt); 00083 u8* dst = 0; 00084 int len; 00085 switch (evt) 00086 { 00087 case AUTO_MOUSE: 00088 dst = auto_mouse; 00089 len = sizeof(auto_mouse); 00090 break; 00091 case AUTO_KEYBOARD: 00092 dst = auto_keyboard; 00093 len = sizeof(auto_keyboard); 00094 break; 00095 // case AUTO_PS3: 00096 // printf("PS3 event ? \r\n"); 00097 // dst = auto_ps3; 00098 // len = sizeof(auto_ps3); 00099 default: 00100 printf("Interrupt endpoint %02X %08X\r\n",ed->bEndpointAddress,evt); 00101 break; 00102 } 00103 if (dst) 00104 { 00105 printf("Auto Event for %02X %08X\r\n",ed->bEndpointAddress,evt); 00106 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt); 00107 } 00108 } 00109 00110 void PrintString(int device, int i) 00111 { 00112 u8 buffer[256]; 00113 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255); 00114 if (le < 0) 00115 return; 00116 char* dst = (char*)buffer; 00117 for (int j = 2; j < le; j += 2) 00118 *dst++ = buffer[j]; 00119 *dst = 0; 00120 printf("%d:%s\r\n",i,(const char*)buffer); 00121 } 00122 00123 // Walk descriptors and create endpoints for a given device 00124 int StartAutoEvent(int device, int configuration, int interfaceNumber) 00125 { 00126 00127 printf("StartAutoEvent \r\n"); 00128 00129 u8 buffer[255]; 00130 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); 00131 if (err < 0) 00132 return err; 00133 00134 int len = buffer[2] | (buffer[3] << 8); 00135 u8* d = buffer; 00136 u8* end = d + len; 00137 while (d < end) 00138 { 00139 if (d[1] == DESCRIPTOR_TYPE_INTERFACE) 00140 { 00141 InterfaceDescriptor* id = (InterfaceDescriptor*)d; 00142 if (id->bInterfaceNumber == interfaceNumber) 00143 { 00144 d += d[0]; 00145 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) 00146 { 00147 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) 00148 AddAutoEvent(device,id,(EndpointDescriptor*)d); 00149 d += d[0]; 00150 } 00151 } 00152 } 00153 d += d[0]; 00154 } 00155 return 0; 00156 } 00157 00158 /* 00159 int StartPS3Event(int device, int configuration, int interfaceNumber) 00160 { 00161 00162 printf("StartPS3Event \r\n"); 00163 00164 EndpointDescriptor* ep; 00165 00166 u8 buf[4]; 00167 buf[0] = 0x42; 00168 buf[1] = 0x0c; 00169 buf[2] = 0x00; 00170 buf[3] = 0x00; 00171 00172 u8 buf2[8]; 00173 u8 buf3[8]; 00174 00175 buf2[0] = 0x01; 00176 buf2[1] = 0x00; 00177 buf2[2] = 0x00; 00178 buf2[3] = 0x02; 00179 buf2[4] = 0x72; 00180 buf2[5] = 0xAD; 00181 buf2[6] = 0xF3; 00182 buf2[7] = 0x5B; 00183 00184 00185 00186 00187 int result; 00188 int err; 00189 00190 u8 buffer[255]; 00191 err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); 00192 if (err < 0) 00193 return err; 00194 00195 00196 00197 //configure the device 00198 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_STANDARD|RECIPIENT_DEVICE, SET_CONFIGURATION, 1, 0, 0, 0, 0, 0 ); 00199 err = SetConfiguration(device,1); 00200 printf("set config result = %d\r\n", err); 00201 00202 // get Mac address 00203 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_DEVICE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf3, sizeof(buf3), 0, 0 ); 00204 //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); 00205 00206 // set Mac address 00207 err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf2, sizeof(buf2), 0, 0 ); 00208 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); 00209 00210 // get Mac address 00211 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_DEVICE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf3, sizeof(buf3), 0, 0 ); 00212 //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); 00213 00214 err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, buf, sizeof(buf), 0, 0 ); 00215 printf("set report result = %d\r\n", err); 00216 //USBTransfer(device,0,DEVICE_TO_HOST,buf,sizeof(buf),0,0); 00217 00218 int len = buffer[2] | (buffer[3] << 8); 00219 u8* d = buffer; 00220 u8* end = d + len; 00221 while (d < end) 00222 { 00223 if (d[1] == DESCRIPTOR_TYPE_INTERFACE) 00224 { 00225 InterfaceDescriptor* id = (InterfaceDescriptor*)d; 00226 if (id->bInterfaceNumber == interfaceNumber) 00227 { 00228 d += d[0]; 00229 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) 00230 { 00231 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) 00232 ep = (EndpointDescriptor*)d; 00233 00234 if (ep->bEndpointAddress == 0x02) { 00235 printf("PS3 input endpoint (0x02) found\r\n"); 00236 00237 } 00238 if (ep->bEndpointAddress == 0x81) { 00239 printf("PS3 output endpoint (0x81) found\r\n"); 00240 AddAutoEvent(device,id,(EndpointDescriptor*)d); 00241 } 00242 d += d[0]; 00243 } 00244 } 00245 } 00246 d += d[0]; 00247 } 00248 return 0; 00249 } 00250 */ 00251 00252 // Implemented in main.cpp 00253 int OnDiskInsert(int device); 00254 void convert(u8 *mac){ 00255 LocalFileSystem local("local"); 00256 FILE *fp; 00257 fp = fopen("/local/data.p3b", "r"); 00258 if(fp==NULL){ 00259 fp = fopen("/local/data.p3b", "w"); 00260 fprintf(fp,"00:02:72:D0:82:F7"); 00261 fclose(fp); 00262 fp = fopen("/local/data.p3b", "r"); 00263 } 00264 char a[20]; 00265 fgets(a,sizeof(a),fp); 00266 char b[6][5]; 00267 char *end; 00268 for(int i=0;i<6;i++){ 00269 b[i][0]='0'; 00270 b[i][1]='x'; 00271 b[i][2]=a[i*3]; 00272 b[i][3]=a[1+i*3]; 00273 b[i][4]='\n'; 00274 mac[i]=(u8)strtol(b[i],&end,0); 00275 } 00276 fclose(fp); 00277 } 00278 00279 // Implemented in TestShell.cpp 00280 int OnBluetoothInsert(int device); 00281 00282 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) 00283 { 00284 printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); 00285 char s[128]; 00286 //u8 my_mac[6] = {0x00, 0x02, 0x72, 0xD0, 0x82, 0xF7}; // mac address of my Bluetooth device 00287 u8 my_mac[6]; 00288 convert(my_mac); 00289 00290 u8 buf2[6]; 00291 00292 buf2[0] = 0x00; 00293 buf2[1] = 0x02; 00294 buf2[2] = 0x72; 00295 buf2[3] = 0xAD; 00296 buf2[4] = 0xF3; 00297 buf2[5] = 0x5B; 00298 00299 00300 for (int i = 1; i < 3; i++) 00301 { 00302 if (GetString(device,i,s,sizeof(s)) < 0) 00303 break; 00304 printf("%d: %s\r\n",i,s); 00305 } 00306 00307 switch (interfaceDesc->bInterfaceClass) 00308 { 00309 case CLASS_MASS_STORAGE: 00310 if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50) 00311 OnDiskInsert(device); // it's SCSI! 00312 break; 00313 case CLASS_WIRELESS_CONTROLLER: 00314 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01) 00315 OnBluetoothInsert(device); // it's bluetooth! 00316 break; 00317 case CLASS_HID: 00318 //追加部分 00319 /*if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01) 00320 OnBluetoothInsert(device); // it's bluetooth! 00321 break; 00322 *///追加部分ここまで 00323 printf("idVendor = %04X idProduct = %04X \r\n",deviceDesc->idVendor,deviceDesc->idProduct); 00324 //printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); 00325 //if (deviceDesc->idVendor == 0x054C && deviceDesc->idProduct == 0x0268) StartPS3Event(device,1,0); 00326 if (deviceDesc->idVendor == 0x054C && deviceDesc->idProduct == 0x0268) { 00327 Ps3USB _Ps3USB(device,1,0); 00328 00329 _Ps3USB.SetPair(my_mac); 00330 _Ps3USB.Enable(); 00331 _Ps3USB.Led(1); 00332 _Ps3USB.Rumble(0x20,0xff,0x20,0xff); 00333 _Ps3USB.ShowPair(); 00334 00335 } 00336 else StartAutoEvent(device,1,0); 00337 break; 00338 default: 00339 printf("Not yet supported \r\n"); 00340 //StartAutoEvent(device,1,0); 00341 break; 00342 } 00343 }
Generated on Sat Jul 16 2022 01:37:19 by
1.7.2
