主導機 mbed用のプログラムです 改良しました

Dependencies:   mbed

Fork of F3RC_syudou_master by 日記

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AutoEvents.cpp Source File

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 
00255 //  Implemented in TestShell.cpp
00256 int OnBluetoothInsert(int device);
00257 void convert(u8 *mac){
00258     LocalFileSystem local("local"); 
00259     FILE *fp;
00260     fp = fopen("/local/data.p3b", "r");
00261     if(fp==NULL){
00262         fp = fopen("/local/data.p3b", "w");
00263         fprintf(fp,"00:1B:DC:06:C7:C7");
00264         fclose(fp);
00265         fp = fopen("/local/data.p3b", "r");
00266     }
00267     char a[20];
00268     fgets(a,sizeof(a),fp);
00269     char b[6][5];
00270     char *end;
00271     for(int i=0;i<6;i++){
00272         b[i][0]='0';
00273         b[i][1]='x';
00274         b[i][2]=a[i*3];
00275         b[i][3]=a[1+i*3];
00276         b[i][4]='\n';
00277         mac[i]=(u8)strtol(b[i],&end,0);
00278     }
00279     fclose(fp);
00280 }
00281     
00282 
00283 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
00284 {
00285     printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
00286     char s[128];
00287     //u8 my_mac[6] = {0x00, 0x02, 0x72, 0xD0, 0x82, 0xF7}; // mac address of my Bluetooth device
00288     u8 my_mac[6];
00289     convert(my_mac);
00290     
00291     u8 buf2[6];
00292     
00293     buf2[0] = 0x00;
00294     buf2[1] = 0x02;
00295     buf2[2] = 0x72;
00296     buf2[3] = 0xAD;
00297     buf2[4] = 0xF3;
00298     buf2[5] = 0x5B;
00299     
00300     
00301     for (int i = 1; i < 3; i++)
00302     {
00303         if (GetString(device,i,s,sizeof(s)) < 0)
00304             break;
00305         printf("%d: %s\r\n",i,s);
00306     }
00307     
00308     switch (interfaceDesc->bInterfaceClass)
00309     {
00310         case CLASS_MASS_STORAGE:
00311             if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
00312                 OnDiskInsert(device);    // it's SCSI!
00313             break;
00314         case CLASS_WIRELESS_CONTROLLER:
00315             if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
00316                 OnBluetoothInsert(device);    // it's bluetooth!
00317             break;
00318         case CLASS_HID:
00319             //追加部分
00320             /*if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
00321                 OnBluetoothInsert(device);    // it's bluetooth!
00322             break;
00323             *///追加部分ここまで
00324             printf("idVendor = %04X idProduct = %04X \r\n",deviceDesc->idVendor,deviceDesc->idProduct);
00325             //printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
00326             //if (deviceDesc->idVendor == 0x054C &&  deviceDesc->idProduct == 0x0268) StartPS3Event(device,1,0);
00327             if (deviceDesc->idVendor == 0x054C &&  deviceDesc->idProduct == 0x0268) {
00328                 Ps3USB _Ps3USB(device,1,0);
00329                      
00330                 _Ps3USB.SetPair(my_mac);
00331                 _Ps3USB.Enable();
00332                 _Ps3USB.Led(1);
00333                 _Ps3USB.Rumble(0x20,0xff,0x20,0xff);
00334                 _Ps3USB.ShowPair();
00335                 
00336             }
00337             else StartAutoEvent(device,1,0);
00338             break;
00339         default:
00340             printf("Not yet supported \r\n");
00341             //StartAutoEvent(device,1,0);
00342             break;
00343     }
00344 }