Android Open Accessory Library化してみました。 バルク転送の使い方が分かっていないため、相変わらず動作は不安定。 I do not understand the usage of the bulk transfer. Please teach someone.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AndroidAccessory.cpp Source File

AndroidAccessory.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #include "USBHost.h"
00006 #include "AndroidAccessory.h"
00007 #include "mbed.h"
00008 
00009 AndroidAccessory* _adk;
00010 
00011 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
00012 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
00013 
00014 
00015 
00016 AndroidAccessory::AndroidAccessory(int rbuffsize,int wbuffsize,
00017                                    const char* manufacturer,
00018                                    const char *model,
00019                                    const char *description,
00020                                    const char *version,
00021                                    const char *uri,
00022                                    const char *serial
00023                                   ) {
00024 
00025     _adk=this;
00026 
00027     this->manufacturer=manufacturer;
00028     this->model=model;
00029     this->description=description;
00030     this->version=version;
00031     this->uri=uri;
00032     this->serial=serial;
00033 
00034     u32 len;
00035     u8* p=USBGetBuffer(&len);
00036     if (len<(rbuffsize+wbuffsize+255)) {
00037         error("buff size too big.please resize max=%d. currentSize=%d\r\n",len,(rbuffsize+wbuffsize+255));
00038     }
00039 
00040     _readbuff=p;
00041     _readbuffsize=rbuffsize;
00042     p+=rbuffsize;
00043     _writebuff=p;
00044     _writebuffsize=wbuffsize;
00045     p+=wbuffsize;
00046     _strbuff=p;
00047     p+=255;
00048 
00049 }
00050 
00051 
00052 
00053 int AndroidAccessory::write(u8 *buff, int len) {
00054     log("AndroidAccessory::write ");
00055    // __disable_irq();
00056     int ret=USBBulkTransfer(_device,output_ep,buff,len/*,AdkwriteCallback,this*/);
00057    // __enable_irq();
00058     log("--ret=%d \r\n",ret);
00059     return ret;
00060 }
00061 
00062 #if 0
00063 int AndroidAccessory::read(u8 *buff, int len) {
00064     if(_initok==false)return 0;
00065     
00066     log("AndroidAccessory::read ");
00067    // __disable_irq();
00068     int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len);
00069    // __enable_irq();
00070     log("--ret=%d \r\n",ret);
00071     return ret;
00072 }
00073 #endif
00074 
00075 void AndroidAccessory::init(int device, int configuration, int interfaceNumber) {
00076 
00077     log("AndroidAccessory::init \r\n");
00078 
00079 //    _initok=false;
00080     _device = device;
00081     _configuration = configuration;
00082     _interfaceNumber = interfaceNumber;
00083     printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
00084     int err;
00085 
00086     u8* buffer=_strbuff;
00087     err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4);
00088 
00089     if (err < 0) {
00090         log("Failed to get descriptor\r\n");
00091         return;
00092     }
00093 
00094 
00095     int len = buffer[2] | (buffer[3] << 8);
00096     if (len > 255) {
00097         log("config descriptor too large\n");
00098         /* might want to truncate here */
00099         return;
00100     }
00101     err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len);
00102     u8* p = buffer;
00103     input_ep=0;
00104     output_ep=0;
00105     EndpointDescriptor *epDesc;
00106     while (p<(buffer+len)) {
00107         u8 descLen  = p[0];
00108         u8 descType = p[1];
00109         log("descLen=%d,descType=%d\r\n",descLen,descType);
00110         switch (descType) {
00111             case DESCRIPTOR_TYPE_CONFIGURATION:
00112                 log("config desc\r\n");
00113                 break;
00114             case DESCRIPTOR_TYPE_INTERFACE:
00115                 log("interface desc\r\n");
00116                 break;
00117             case DESCRIPTOR_TYPE_ENDPOINT:
00118                 epDesc=(EndpointDescriptor*)p;
00119                 if (!input_ep && (epDesc->bEndpointAddress& 0x80)) {
00120                     input_ep=epDesc->bEndpointAddress& 0x7f;
00121                     //PacketSize drop
00122                     log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
00123 
00124                 } else if (!output_ep) {
00125                     output_ep=epDesc->bEndpointAddress& 0x7f;
00126                     //PacketSize drop
00127                     log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
00128                 } else {
00129                     //other
00130                     log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
00131                 }
00132                 break;
00133             default:
00134                 log("unkown desc type(%d) \r\n",descType);
00135         }
00136         p+=descLen;
00137     }
00138 
00139     if (!(input_ep && output_ep)) {
00140         log("can't find accessory endpoints\r\n");
00141         return;
00142     }
00143 
00144     log("SetConfiguration\r\n");
00145     err = SetConfiguration(device,configuration);
00146     if (err < 0) {
00147         log("SetConfiguration error\r\n");
00148         return;
00149     }
00150 
00151 
00152     log("interrupt setup\r\n");
00153     //interrupt setup
00154     if (_readbuff==NULL || _readbuffsize<=0) {
00155         error("_readbuffer error please setup buffer call setReadBuffer function\r\n");
00156     }
00157 
00158     if (IO_PENDING!=USBBulkTransfer(_device,input_ep|0x80,_readbuff,_readbuffsize,AdkreadCallback,this))
00159         return;
00160 
00161 
00162     log("setupDevice\r\n");
00163     this->setupDevice();
00164 //    _initok=true;
00165 }
00166 
00167 
00168 
00169 bool AndroidAccessory::switchDevice(int device) {
00170 
00171     if (1==getProtocol(device)) {
00172         log("device supports protocol 1\r\n");
00173 
00174     } else {
00175         log("could not read device protocol version\r\n");
00176         return false;
00177     }
00178 
00179 
00180     sendString(device,ACCESSORY_STRING_MANUFACTURER,manufacturer);
00181     sendString(device,ACCESSORY_STRING_MODEL,model);
00182     sendString(device,ACCESSORY_STRING_DESCRIPTION,description);
00183     sendString(device,ACCESSORY_STRING_VERSION,version);
00184     sendString(device,ACCESSORY_STRING_URI,uri);
00185     sendString(device,ACCESSORY_STRING_SERIAL,serial);
00186     USBControlTransfer(device,
00187                        HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
00188                        ACCESSORY_START,
00189                        0,//value
00190                        0, //index
00191                        0,
00192                        0,
00193                        0,
00194                        0 );
00195 
00196     wait_ms(4);
00197     //reset usb host
00198     USBInit();
00199 
00200     return true;
00201 
00202 }
00203 
00204 
00205 int AndroidAccessory::getProtocol(int device) {
00206     s16 data=-1;
00207     USBControlTransfer(device,
00208                        DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
00209                        ACCESSORY_GET_PROTOCOL,
00210                        0,//value
00211                        0, //index
00212                        (u8*)&data,
00213                        2,
00214                        0,
00215                        0 );
00216     return data;
00217 
00218 }
00219 
00220 void AndroidAccessory::sendString(int device, int index, const char *str) {
00221 
00222     LOG("send_string start(%d,%d,%s)  %d \r\n",device,index,str,strlen(str)+1);
00223     strcpy((char*)_strbuff,str);
00224     //thankyou curryman san
00225     USBControlTransfer(device,
00226                        HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
00227                        ACCESSORY_SEND_STRING,
00228                        0,//value
00229                        index,
00230                        _strbuff,
00231                        strlen(str)+1
00232                       );
00233 
00234     LOG("send_string end(%d,%d,%s)\r\n",device,index,str);
00235 
00236 }
00237 
00238 
00239 /** from USBHost load function. initialize Android device**/
00240 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) {
00241     printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
00242     char s[128];
00243 
00244     for (int i = 1; i < 3; i++) {
00245         if (GetString(device,i,s,sizeof(s)) < 0)
00246             break;
00247         printf("%d: %s\r\n",i,s);
00248     }
00249 
00250     //for android ADK
00251     if ( ( deviceDesc->idVendor != 0x18D1 ||
00252             ( deviceDesc->idProduct != 0x2D00 && deviceDesc->idProduct != 0x2D01))
00253             &&_adk->switchDevice(device)) {
00254 
00255         printf("  try to change accmode.interfaceDesc->bInterfaceClass=%d\r\n",interfaceDesc->bInterfaceClass);
00256         //1th root
00257         //accmode_support=true;
00258         printf("accessory mode ok.\r\n");
00259         return;
00260     }
00261 
00262     if (deviceDesc->idVendor == 0x18D1 &&
00263             (deviceDesc->idProduct == 0x2D00 || deviceDesc->idProduct == 0x2D01)) {
00264         //2th root
00265         printf("connecting Android.\r\n");
00266         printf("idVender=%x  idProduct=%x  interfaceDesc->bInterfaceClass=%d\r\n",deviceDesc->idVendor,deviceDesc->idProduct,interfaceDesc->bInterfaceClass);
00267         _adk->init(device,1,0);
00268         //_AdkUSB.loop();
00269         return;
00270     }
00271 }
00272 
00273 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
00274     log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
00275         device,endpoint,status,buf,len,userData);
00276 //    __disable_irq();
00277     AndroidAccessory* t = (AndroidAccessory*)userData;
00278     if (status!=0) {
00279         log("adk end.\r\n");
00280         t->adkEnd();
00281 //        __enable_irq();
00282         USBInit();
00283         return;
00284     }
00285 
00286 
00287     //virtual method run
00288     t->callbackRead(buf,len);
00289 
00290     USBBulkTransfer(device, endpoint , buf, len, AdkreadCallback, userData);
00291 
00292 //    wait_ms(4);
00293 //    __enable_irq();
00294 }
00295 
00296 
00297 
00298 #if 0
00299 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
00300 
00301     log("AdkwriteCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
00302         device,endpoint,status,buf,len,userData);
00303 
00304     //AndroidAccessory* t = (AndroidAccessory*)userData;
00305     //wait_ms(4);
00306 
00307 }
00308 #endif