Fixed version check for AOA 2.0

Fork of AndroidAccessory by Giles Barton-Owen

Committer:
vargham
Date:
Sun Nov 29 15:58:55 2015 +0000
Revision:
3:e7435fada96a
Parent:
0:1151e4446dbc
Fixed version check for AOA 2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p07gbar 0:1151e4446dbc 1 #include <stdio.h>
p07gbar 0:1151e4446dbc 2 #include <stdlib.h>
p07gbar 0:1151e4446dbc 3 #include <string.h>
p07gbar 0:1151e4446dbc 4
p07gbar 0:1151e4446dbc 5 #include "USBHost.h"
p07gbar 0:1151e4446dbc 6 #include "AndroidAccessory.h"
p07gbar 0:1151e4446dbc 7 #include "mbed.h"
p07gbar 0:1151e4446dbc 8
p07gbar 0:1151e4446dbc 9 AndroidAccessory* _adk;
p07gbar 0:1151e4446dbc 10
p07gbar 0:1151e4446dbc 11 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
p07gbar 0:1151e4446dbc 12 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData);
p07gbar 0:1151e4446dbc 13
p07gbar 0:1151e4446dbc 14
p07gbar 0:1151e4446dbc 15
p07gbar 0:1151e4446dbc 16 AndroidAccessory::AndroidAccessory(int rbuffsize,int wbuffsize,
p07gbar 0:1151e4446dbc 17 const char* manufacturer,
p07gbar 0:1151e4446dbc 18 const char *model,
p07gbar 0:1151e4446dbc 19 const char *description,
p07gbar 0:1151e4446dbc 20 const char *version,
p07gbar 0:1151e4446dbc 21 const char *uri,
p07gbar 0:1151e4446dbc 22 const char *serial
p07gbar 0:1151e4446dbc 23 ) {
p07gbar 0:1151e4446dbc 24
p07gbar 0:1151e4446dbc 25 _adk=this;
p07gbar 0:1151e4446dbc 26
p07gbar 0:1151e4446dbc 27 this->manufacturer=manufacturer;
p07gbar 0:1151e4446dbc 28 this->model=model;
p07gbar 0:1151e4446dbc 29 this->description=description;
p07gbar 0:1151e4446dbc 30 this->version=version;
p07gbar 0:1151e4446dbc 31 this->uri=uri;
p07gbar 0:1151e4446dbc 32 this->serial=serial;
p07gbar 0:1151e4446dbc 33
p07gbar 0:1151e4446dbc 34 u32 len;
p07gbar 0:1151e4446dbc 35 u8* p=USBGetBuffer(&len);
p07gbar 0:1151e4446dbc 36 if (len<(rbuffsize+wbuffsize+255)) {
p07gbar 0:1151e4446dbc 37 error("buff size too big.please resize max=%d. currentSize=%d\r\n",len,(rbuffsize+wbuffsize+255));
p07gbar 0:1151e4446dbc 38 }
p07gbar 0:1151e4446dbc 39
p07gbar 0:1151e4446dbc 40 _readbuff=p;
p07gbar 0:1151e4446dbc 41 _readbuffsize=rbuffsize;
p07gbar 0:1151e4446dbc 42 p+=rbuffsize;
p07gbar 0:1151e4446dbc 43 _writebuff=p;
p07gbar 0:1151e4446dbc 44 _writebuffsize=wbuffsize;
p07gbar 0:1151e4446dbc 45 p+=wbuffsize;
p07gbar 0:1151e4446dbc 46 _strbuff=p;
p07gbar 0:1151e4446dbc 47 p+=255;
p07gbar 0:1151e4446dbc 48
p07gbar 0:1151e4446dbc 49 }
p07gbar 0:1151e4446dbc 50
p07gbar 0:1151e4446dbc 51
p07gbar 0:1151e4446dbc 52
p07gbar 0:1151e4446dbc 53 int AndroidAccessory::write(u8 *buff, int len) {
p07gbar 0:1151e4446dbc 54 log("AndroidAccessory::write ");
p07gbar 0:1151e4446dbc 55 // __disable_irq();
p07gbar 0:1151e4446dbc 56 int ret=USBBulkTransfer(_device,output_ep,buff,len,AdkwriteCallback,this);
p07gbar 0:1151e4446dbc 57 // __enable_irq();
p07gbar 0:1151e4446dbc 58 log("--ret=%d \r\n",ret);
p07gbar 0:1151e4446dbc 59 return ret;
p07gbar 0:1151e4446dbc 60 }
p07gbar 0:1151e4446dbc 61 int AndroidAccessory::writeNC(u8 *buff, int len) {
p07gbar 0:1151e4446dbc 62 log("AndroidAccessory::write ");
p07gbar 0:1151e4446dbc 63 // __disable_irq();
p07gbar 0:1151e4446dbc 64 int ret=USBBulkTransfer(_device,output_ep,buff,len);
p07gbar 0:1151e4446dbc 65 // __enable_irq();
p07gbar 0:1151e4446dbc 66 log("--ret=%d \r\n",ret);
p07gbar 0:1151e4446dbc 67 return ret;
p07gbar 0:1151e4446dbc 68 }
p07gbar 0:1151e4446dbc 69
p07gbar 0:1151e4446dbc 70
p07gbar 0:1151e4446dbc 71
p07gbar 0:1151e4446dbc 72 int AndroidAccessory::read(u8 *buff, int len) {
p07gbar 0:1151e4446dbc 73 // if(_initok==false)return 0;
p07gbar 0:1151e4446dbc 74
p07gbar 0:1151e4446dbc 75 log("AndroidAccessory::read ");
p07gbar 0:1151e4446dbc 76 // __disable_irq();
p07gbar 0:1151e4446dbc 77 int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len);
p07gbar 0:1151e4446dbc 78 // __enable_irq();
p07gbar 0:1151e4446dbc 79 log("--ret=%d \r\n",ret);
p07gbar 0:1151e4446dbc 80 return ret;
p07gbar 0:1151e4446dbc 81 }
p07gbar 0:1151e4446dbc 82
p07gbar 0:1151e4446dbc 83
p07gbar 0:1151e4446dbc 84 void AndroidAccessory::init(int device, int configuration, int interfaceNumber) {
p07gbar 0:1151e4446dbc 85
p07gbar 0:1151e4446dbc 86 log("AndroidAccessory::init \r\n");
p07gbar 0:1151e4446dbc 87
p07gbar 0:1151e4446dbc 88 // _initok=false;
p07gbar 0:1151e4446dbc 89 _device = device;
p07gbar 0:1151e4446dbc 90 _configuration = configuration;
p07gbar 0:1151e4446dbc 91 _interfaceNumber = interfaceNumber;
p07gbar 0:1151e4446dbc 92 printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
p07gbar 0:1151e4446dbc 93 int err;
p07gbar 0:1151e4446dbc 94
p07gbar 0:1151e4446dbc 95 u8* buffer=_strbuff;
p07gbar 0:1151e4446dbc 96 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4);
p07gbar 0:1151e4446dbc 97
p07gbar 0:1151e4446dbc 98 if (err < 0) {
p07gbar 0:1151e4446dbc 99 log("Failed to get descriptor\r\n");
p07gbar 0:1151e4446dbc 100 return;
p07gbar 0:1151e4446dbc 101 }
p07gbar 0:1151e4446dbc 102
p07gbar 0:1151e4446dbc 103
p07gbar 0:1151e4446dbc 104 int len = buffer[2] | (buffer[3] << 8);
p07gbar 0:1151e4446dbc 105 if (len > 255) {
p07gbar 0:1151e4446dbc 106 log("config descriptor too large\n");
p07gbar 0:1151e4446dbc 107 /* might want to truncate here */
p07gbar 0:1151e4446dbc 108 return;
p07gbar 0:1151e4446dbc 109 }
p07gbar 0:1151e4446dbc 110 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len);
p07gbar 0:1151e4446dbc 111 u8* p = buffer;
p07gbar 0:1151e4446dbc 112 input_ep=0;
p07gbar 0:1151e4446dbc 113 output_ep=0;
p07gbar 0:1151e4446dbc 114 EndpointDescriptor *epDesc;
p07gbar 0:1151e4446dbc 115 while (p<(buffer+len)) {
p07gbar 0:1151e4446dbc 116 u8 descLen = p[0];
p07gbar 0:1151e4446dbc 117 u8 descType = p[1];
p07gbar 0:1151e4446dbc 118 log("descLen=%d,descType=%d\r\n",descLen,descType);
p07gbar 0:1151e4446dbc 119 switch (descType) {
p07gbar 0:1151e4446dbc 120 case DESCRIPTOR_TYPE_CONFIGURATION:
p07gbar 0:1151e4446dbc 121 log("config desc\r\n");
p07gbar 0:1151e4446dbc 122 break;
p07gbar 0:1151e4446dbc 123 case DESCRIPTOR_TYPE_INTERFACE:
p07gbar 0:1151e4446dbc 124 log("interface desc\r\n");
p07gbar 0:1151e4446dbc 125 break;
p07gbar 0:1151e4446dbc 126 case DESCRIPTOR_TYPE_ENDPOINT:
p07gbar 0:1151e4446dbc 127 epDesc=(EndpointDescriptor*)p;
p07gbar 0:1151e4446dbc 128 if (!input_ep && (epDesc->bEndpointAddress& 0x80)) {
p07gbar 0:1151e4446dbc 129 input_ep=epDesc->bEndpointAddress& 0x7f;
p07gbar 0:1151e4446dbc 130 //PacketSize drop
p07gbar 0:1151e4446dbc 131 log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
p07gbar 0:1151e4446dbc 132
p07gbar 0:1151e4446dbc 133 } else if (!output_ep) {
p07gbar 0:1151e4446dbc 134 output_ep=epDesc->bEndpointAddress& 0x7f;
p07gbar 0:1151e4446dbc 135 //PacketSize drop
p07gbar 0:1151e4446dbc 136 log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
p07gbar 0:1151e4446dbc 137 } else {
p07gbar 0:1151e4446dbc 138 //other
p07gbar 0:1151e4446dbc 139 log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes);
p07gbar 0:1151e4446dbc 140 }
p07gbar 0:1151e4446dbc 141 break;
p07gbar 0:1151e4446dbc 142 default:
p07gbar 0:1151e4446dbc 143 log("unkown desc type(%d) \r\n",descType);
p07gbar 0:1151e4446dbc 144 }
p07gbar 0:1151e4446dbc 145 p+=descLen;
p07gbar 0:1151e4446dbc 146 }
p07gbar 0:1151e4446dbc 147
p07gbar 0:1151e4446dbc 148 if (!(input_ep && output_ep)) {
p07gbar 0:1151e4446dbc 149 log("can't find accessory endpoints\r\n");
p07gbar 0:1151e4446dbc 150 return;
p07gbar 0:1151e4446dbc 151 }
p07gbar 0:1151e4446dbc 152
p07gbar 0:1151e4446dbc 153 log("SetConfiguration\r\n");
p07gbar 0:1151e4446dbc 154 err = SetConfiguration(device,configuration);
p07gbar 0:1151e4446dbc 155 if (err < 0) {
p07gbar 0:1151e4446dbc 156 log("SetConfiguration error\r\n");
p07gbar 0:1151e4446dbc 157 return;
p07gbar 0:1151e4446dbc 158 }
p07gbar 0:1151e4446dbc 159
p07gbar 0:1151e4446dbc 160
p07gbar 0:1151e4446dbc 161 log("interrupt setup\r\n");
p07gbar 0:1151e4446dbc 162 //interrupt setup
p07gbar 0:1151e4446dbc 163 if (_readbuff==NULL || _readbuffsize<=0) {
p07gbar 0:1151e4446dbc 164 error("_readbuffer error please setup buffer call setReadBuffer function\r\n");
p07gbar 0:1151e4446dbc 165 }
p07gbar 0:1151e4446dbc 166
p07gbar 0:1151e4446dbc 167 if (IO_PENDING!=USBBulkTransfer(_device,input_ep|0x80,_readbuff,_readbuffsize,AdkreadCallback,this))
p07gbar 0:1151e4446dbc 168 return;
p07gbar 0:1151e4446dbc 169
p07gbar 0:1151e4446dbc 170
p07gbar 0:1151e4446dbc 171 log("setupDevice\r\n");
p07gbar 0:1151e4446dbc 172 this->setupDevice();
p07gbar 0:1151e4446dbc 173 // _initok=true;
p07gbar 0:1151e4446dbc 174 }
p07gbar 0:1151e4446dbc 175
p07gbar 0:1151e4446dbc 176
p07gbar 0:1151e4446dbc 177
p07gbar 0:1151e4446dbc 178 bool AndroidAccessory::switchDevice(int device) {
p07gbar 0:1151e4446dbc 179
vargham 3:e7435fada96a 180 if (1<=getProtocol(device)) {
p07gbar 0:1151e4446dbc 181 log("device supports protocol 1\r\n");
p07gbar 0:1151e4446dbc 182
p07gbar 0:1151e4446dbc 183 } else {
p07gbar 0:1151e4446dbc 184 log("could not read device protocol version\r\n");
p07gbar 0:1151e4446dbc 185 return false;
p07gbar 0:1151e4446dbc 186 }
p07gbar 0:1151e4446dbc 187
p07gbar 0:1151e4446dbc 188
p07gbar 0:1151e4446dbc 189 sendString(device,ACCESSORY_STRING_MANUFACTURER,manufacturer);
p07gbar 0:1151e4446dbc 190 sendString(device,ACCESSORY_STRING_MODEL,model);
p07gbar 0:1151e4446dbc 191 sendString(device,ACCESSORY_STRING_DESCRIPTION,description);
p07gbar 0:1151e4446dbc 192 sendString(device,ACCESSORY_STRING_VERSION,version);
p07gbar 0:1151e4446dbc 193 sendString(device,ACCESSORY_STRING_URI,uri);
p07gbar 0:1151e4446dbc 194 sendString(device,ACCESSORY_STRING_SERIAL,serial);
p07gbar 0:1151e4446dbc 195 USBControlTransfer(device,
p07gbar 0:1151e4446dbc 196 HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
p07gbar 0:1151e4446dbc 197 ACCESSORY_START,
p07gbar 0:1151e4446dbc 198 0,//value
p07gbar 0:1151e4446dbc 199 0, //index
p07gbar 0:1151e4446dbc 200 0,
p07gbar 0:1151e4446dbc 201 0,
p07gbar 0:1151e4446dbc 202 0,
p07gbar 0:1151e4446dbc 203 0 );
p07gbar 0:1151e4446dbc 204
p07gbar 0:1151e4446dbc 205 wait_ms(4);
p07gbar 0:1151e4446dbc 206 //reset usb host
p07gbar 0:1151e4446dbc 207 USBInit();
p07gbar 0:1151e4446dbc 208
p07gbar 0:1151e4446dbc 209 return true;
p07gbar 0:1151e4446dbc 210
p07gbar 0:1151e4446dbc 211 }
p07gbar 0:1151e4446dbc 212
p07gbar 0:1151e4446dbc 213
p07gbar 0:1151e4446dbc 214 int AndroidAccessory::getProtocol(int device) {
p07gbar 0:1151e4446dbc 215 s16 data=-1;
p07gbar 0:1151e4446dbc 216 USBControlTransfer(device,
p07gbar 0:1151e4446dbc 217 DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
p07gbar 0:1151e4446dbc 218 ACCESSORY_GET_PROTOCOL,
p07gbar 0:1151e4446dbc 219 0,//value
p07gbar 0:1151e4446dbc 220 0, //index
p07gbar 0:1151e4446dbc 221 (u8*)&data,
p07gbar 0:1151e4446dbc 222 2,
p07gbar 0:1151e4446dbc 223 0,
p07gbar 0:1151e4446dbc 224 0 );
p07gbar 0:1151e4446dbc 225 return data;
p07gbar 0:1151e4446dbc 226
p07gbar 0:1151e4446dbc 227 }
p07gbar 0:1151e4446dbc 228
p07gbar 0:1151e4446dbc 229 void AndroidAccessory::sendString(const char *str) {
p07gbar 0:1151e4446dbc 230 sendString(_device,1,str);
p07gbar 0:1151e4446dbc 231
p07gbar 0:1151e4446dbc 232 }
p07gbar 0:1151e4446dbc 233
p07gbar 0:1151e4446dbc 234 void AndroidAccessory::sendString(int device, int index, const char *str) {
p07gbar 0:1151e4446dbc 235
p07gbar 0:1151e4446dbc 236 LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1);
p07gbar 0:1151e4446dbc 237 strcpy((char*)_strbuff,str);
p07gbar 0:1151e4446dbc 238 //thankyou curryman san
p07gbar 0:1151e4446dbc 239 USBControlTransfer(device,
p07gbar 0:1151e4446dbc 240 HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE,
p07gbar 0:1151e4446dbc 241 ACCESSORY_SEND_STRING,
p07gbar 0:1151e4446dbc 242 0,//value
p07gbar 0:1151e4446dbc 243 index,
p07gbar 0:1151e4446dbc 244 _strbuff,
p07gbar 0:1151e4446dbc 245 strlen(str)+1
p07gbar 0:1151e4446dbc 246 );
p07gbar 0:1151e4446dbc 247
p07gbar 0:1151e4446dbc 248 LOG("send_string end(%d,%d,%s)\r\n",device,index,str);
p07gbar 0:1151e4446dbc 249
p07gbar 0:1151e4446dbc 250 }
p07gbar 0:1151e4446dbc 251
p07gbar 0:1151e4446dbc 252
p07gbar 0:1151e4446dbc 253 /** from USBHost load function. initialize Android device**/
p07gbar 0:1151e4446dbc 254 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) {
p07gbar 0:1151e4446dbc 255 printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
p07gbar 0:1151e4446dbc 256 char s[128];
p07gbar 0:1151e4446dbc 257
p07gbar 0:1151e4446dbc 258 for (int i = 1; i < 3; i++) {
p07gbar 0:1151e4446dbc 259 if (GetString(device,i,s,sizeof(s)) < 0)
p07gbar 0:1151e4446dbc 260 break;
p07gbar 0:1151e4446dbc 261 printf("%d: %s\r\n",i,s);
p07gbar 0:1151e4446dbc 262 }
p07gbar 0:1151e4446dbc 263
p07gbar 0:1151e4446dbc 264 //for android ADK
p07gbar 0:1151e4446dbc 265 if ( ( deviceDesc->idVendor != 0x18D1 ||
p07gbar 0:1151e4446dbc 266 ( deviceDesc->idProduct != 0x2D00 && deviceDesc->idProduct != 0x2D01))
p07gbar 0:1151e4446dbc 267 &&_adk->switchDevice(device)) {
p07gbar 0:1151e4446dbc 268
p07gbar 0:1151e4446dbc 269 printf(" try to change accmode.interfaceDesc->bInterfaceClass=%d\r\n",interfaceDesc->bInterfaceClass);
p07gbar 0:1151e4446dbc 270 //1th root
p07gbar 0:1151e4446dbc 271 //accmode_support=true;
p07gbar 0:1151e4446dbc 272 printf("accessory mode ok.\r\n");
p07gbar 0:1151e4446dbc 273 return;
p07gbar 0:1151e4446dbc 274 }
p07gbar 0:1151e4446dbc 275
p07gbar 0:1151e4446dbc 276 if (deviceDesc->idVendor == 0x18D1 &&
p07gbar 0:1151e4446dbc 277 (deviceDesc->idProduct == 0x2D00 || deviceDesc->idProduct == 0x2D01)) {
p07gbar 0:1151e4446dbc 278 //2th root
p07gbar 0:1151e4446dbc 279 printf("connecting Android.\r\n");
p07gbar 0:1151e4446dbc 280 printf("idVender=%x idProduct=%x interfaceDesc->bInterfaceClass=%d\r\n",deviceDesc->idVendor,deviceDesc->idProduct,interfaceDesc->bInterfaceClass);
p07gbar 0:1151e4446dbc 281 _adk->init(device,1,0);
p07gbar 0:1151e4446dbc 282 //_AdkUSB.loop();
p07gbar 0:1151e4446dbc 283 return;
p07gbar 0:1151e4446dbc 284 }
p07gbar 0:1151e4446dbc 285 }
p07gbar 0:1151e4446dbc 286
p07gbar 0:1151e4446dbc 287 void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
p07gbar 0:1151e4446dbc 288 log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
p07gbar 0:1151e4446dbc 289 device,endpoint,status,buf,len,userData);
p07gbar 0:1151e4446dbc 290 // __disable_irq();
p07gbar 0:1151e4446dbc 291 AndroidAccessory* t = (AndroidAccessory*)userData;
p07gbar 0:1151e4446dbc 292 if (status!=0 && status!=8) {
p07gbar 0:1151e4446dbc 293 log("adk end.\r\n");
p07gbar 0:1151e4446dbc 294 t->adkEnd();
p07gbar 0:1151e4446dbc 295 // __enable_irq();
p07gbar 0:1151e4446dbc 296 USBInit();
p07gbar 0:1151e4446dbc 297 return;
p07gbar 0:1151e4446dbc 298 }
p07gbar 0:1151e4446dbc 299
p07gbar 0:1151e4446dbc 300
p07gbar 0:1151e4446dbc 301 //virtual method run
p07gbar 0:1151e4446dbc 302 t->callbackRead(buf,len);
p07gbar 0:1151e4446dbc 303
p07gbar 0:1151e4446dbc 304 USBBulkTransfer(device, endpoint , buf, len, AdkreadCallback, userData);
p07gbar 0:1151e4446dbc 305
p07gbar 0:1151e4446dbc 306 // wait_ms(4);
p07gbar 0:1151e4446dbc 307 // __enable_irq();
p07gbar 0:1151e4446dbc 308 }
p07gbar 0:1151e4446dbc 309
p07gbar 0:1151e4446dbc 310
p07gbar 0:1151e4446dbc 311
p07gbar 0:1151e4446dbc 312
p07gbar 0:1151e4446dbc 313 void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) {
p07gbar 0:1151e4446dbc 314
p07gbar 0:1151e4446dbc 315 log("AdkwriteCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n",
p07gbar 0:1151e4446dbc 316 device,endpoint,status,buf,len,userData);
p07gbar 0:1151e4446dbc 317
p07gbar 0:1151e4446dbc 318 AndroidAccessory* t = (AndroidAccessory*)userData;
p07gbar 0:1151e4446dbc 319 t->callbackWrite();
p07gbar 0:1151e4446dbc 320 //wait_ms(4);
p07gbar 0:1151e4446dbc 321 //USBBulkTransfer(device, endpoint , buf, len, AdkwriteCallback, userData);
p07gbar 0:1151e4446dbc 322 }
p07gbar 0:1151e4446dbc 323