Shinji Tadenuma / Mbed 2 deprecated ADK_DEMO

Dependencies:   TextLCD mbed

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