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.
Fork of autonomous Robot Android by
Adb.cpp
00001 /* 00002 Copyright 2011 Niels Brouwers 00003 00004 Licensed under the Apache License, Version 2.0 (the "License"); 00005 you may not use this file except in compliance with the License. 00006 You may obtain a copy of the License at 00007 00008 http://www.apache.org/licenses/LICENSE-2.0 00009 00010 Unless required by applicable law or agreed to in writing, software 00011 distributed under the License is distributed on an "AS IS" BASIS, 00012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 See the License for the specific language governing permissions and 00014 limitations under the License. 00015 */ 00016 00017 /* Changed by Junichi Katsu */ 00018 00019 #include <string.h> 00020 #include <Adb.h> 00021 00022 //#define DEBUG 00023 00024 #define MAX_BUF_SIZE 128 00025 00026 #ifdef DEBUG 00027 #define log(...) printf(__VA_ARGS__) 00028 #else 00029 #define log(...) do {} while(0) 00030 #endif 00031 00032 int input_ep; 00033 int output_ep; 00034 int _device; 00035 int _configuration; 00036 int _interfaceNumber; 00037 00038 static Connection * firstConnection; 00039 static boolean connected; 00040 static int connectionLocalId = 1; 00041 unsigned char readbuff[MAX_BUF_SIZE]; 00042 00043 // Event handler callback function. 00044 adb_eventHandler * eventHandler; 00045 00046 ADB* _adb; 00047 PacketBuffer recv_packet_buf(100,MAX_BUF_SIZE); 00048 Ticker timer; 00049 00050 int time_ms = 0; 00051 00052 00053 void attime(void) 00054 { 00055 time_ms++; 00056 } 00057 00058 int millis() 00059 { 00060 return(time_ms); 00061 } 00062 00063 char *strdup(const char *src) 00064 { 00065 char *p; 00066 00067 if(src == NULL){ 00068 return NULL; 00069 } 00070 00071 p = (char *)malloc(strlen(src) + 1); 00072 if(p != NULL) strcpy(p, src); 00073 return p; 00074 00075 } 00076 00077 /** 00078 * Initialises the ADB protocol. This function initialises the USB layer underneath so no further setup is required. 00079 */ 00080 void ADB::init() 00081 { 00082 recv_packet_buf.clear(); 00083 00084 // Signal that we are not connected. 00085 _device = 0; 00086 connected = false; 00087 00088 00089 // Initialise Usb host. 00090 USBInit(); 00091 00092 timer.attach_us(&attime, 1000); 00093 } 00094 00095 /** 00096 * Sets the ADB event handler function. This function will be called by the ADB layer 00097 * when interesting events occur, such as ADB connect/disconnect, connection open/close, and 00098 * connection writes from the ADB device. 00099 * 00100 * @param handler event handler function. 00101 */ 00102 void ADB::setEventHandler(adb_eventHandler * handler) 00103 { 00104 eventHandler = handler; 00105 } 00106 00107 /** 00108 * Fires an ADB event. 00109 * @param connection ADB connection. May be NULL in case of global connect/disconnect events. 00110 * @param type event type. 00111 * @param length payload length or zero if no payload. 00112 * @param data payload data if relevant or NULL otherwise. 00113 */ 00114 void ADB::fireEvent(Connection * connection, adb_eventType type, uint16_t length, uint8_t * data) 00115 { 00116 // Fire the global event handler, if set. 00117 if (eventHandler!=NULL) 00118 eventHandler(connection, type, length, data); 00119 00120 // Fire the event handler of the connection in question, if relevant 00121 if (connection!=NULL && connection->eventHandler!=NULL) 00122 connection->eventHandler(connection, type, length, data); 00123 } 00124 00125 /** 00126 * Adds a new ADB connection. The connection string is per ADB specs, for example "tcp:1234" opens a 00127 * connection to tcp port 1234, and "shell:ls" outputs a listing of the phone root filesystem. Connections 00128 * can be made persistent by setting reconnect to true. Persistent connections will be automatically 00129 * reconnected when the USB cable is re-plugged in. Non-persistent connections will connect only once, 00130 * and should never be used after they are closed. 00131 * 00132 * The connection string is copied into the Connection record and may not exceed ADB_CONNECTIONSTRING_LENGTH-1 00133 * characters. 00134 * 00135 * @param connectionString ADB connectionstring. I.e. "tcp:1234" or "shell:ls". 00136 * @param reconnect true for automatic reconnect (persistent connections). 00137 * @param handler event handler. 00138 * @return an ADB connection record or NULL on failure (not enough slots or connection string too long). 00139 */ 00140 Connection * ADB::addConnection(const char * connectionString, boolean reconnect, adb_eventHandler * handler) 00141 { 00142 00143 // Allocate a new ADB connection object 00144 Connection * connection = (Connection*)malloc(sizeof(Connection)); 00145 if (connection == NULL) return NULL; 00146 00147 // Allocate memory for the connection string 00148 connection->connectionString = (char*)strdup(connectionString); 00149 if (connection->connectionString==NULL) 00150 { 00151 // Free the connection object and return null 00152 free(connection); 00153 return NULL; 00154 } 00155 00156 // Initialise the newly created object. 00157 connection->localID = connectionLocalId ++; 00158 connection->status = ADB_CLOSED; 00159 connection->lastConnectionAttempt = 0; 00160 connection->reconnect = reconnect; 00161 connection->eventHandler = handler; 00162 00163 // Add the connection to the linked list. Note that it's easier to just insert 00164 // at position 0 because you don't have to traverse the list :) 00165 connection->next = firstConnection; 00166 firstConnection = connection; 00167 00168 // Unable to find an empty spot, all connection slots in use. 00169 return connection; 00170 } 00171 00172 /** 00173 * Prints an ADB_message, for debugging purposes. 00174 * @param message ADB message to print. 00175 */ 00176 #ifdef DEBUG 00177 static void adb_printMessage(adb_message * message) 00178 { 00179 switch(message->command) 00180 { 00181 case A_OKAY: 00182 printf("OKAY message [%lx] %ld %ld\r\n", message->command, message->arg0, message->arg1); 00183 break; 00184 case A_CLSE: 00185 printf("CLSE message [%lx] %ld %ld\r\n", message->command, message->arg0, message->arg1); 00186 break; 00187 case A_WRTE: 00188 printf("WRTE message [%lx] %ld %ld, %ld bytes\r\n", message->command, message->arg0, message->arg1, message->data_length); 00189 break; 00190 case A_CNXN: 00191 printf("CNXN message [%lx] %ld %ld\r\n", message->command, message->arg0, message->arg1); 00192 break; 00193 case A_SYNC: 00194 printf("SYNC message [%lx] %ld %ld\r\n", message->command, message->arg0, message->arg1); 00195 break; 00196 case A_OPEN: 00197 printf("OPEN message [%lx] %ld %ld\r\n", message->command, message->arg0, message->arg1); 00198 break; 00199 default: 00200 printf("WTF message [%lx] %ld %ld\r\n", message->command, message->arg0, message->arg1); 00201 break; 00202 } 00203 } 00204 #endif 00205 00206 /** 00207 * Writes an empty message (without payload) to the ADB device. 00208 * 00209 * @param device USB device handle. 00210 * @param command ADB command. 00211 * @param arg0 first ADB argument (command dependent). 00212 * @param arg0 second ADB argument (command dependent). 00213 * @return error code or 0 for success. 00214 */ 00215 int ADB::writeEmptyMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1) 00216 { 00217 adb_message message; 00218 00219 message.command = command; 00220 message.arg0 = arg0; 00221 message.arg1 = arg1; 00222 message.data_length = 0; 00223 message.data_check = 0; 00224 message.magic = command ^ 0xffffffff; 00225 00226 #ifdef DEBUG 00227 printf("OUT << "); adb_printMessage(&message); 00228 #endif 00229 00230 int r = USBBulkTransfer( device , output_ep , (uint8_t*)&message , sizeof(adb_message) ); 00231 00232 #ifdef DEBUG 00233 log("[writeMessage1] size:%d\r\n",r); 00234 00235 int ii,jj; 00236 uint8_t* buf = (uint8_t*)&message; 00237 for(ii = 0 ; ii < r ; ii+=16) 00238 { 00239 for(jj = 0 ; jj < 16 ; jj+=2 ) 00240 { 00241 log("%02X%02X ",buf[ii+jj],buf[ii+jj+1]); 00242 if((ii+jj) > r) break; 00243 } 00244 log(" : "); 00245 for(jj = 0 ; jj < 16 ; jj+=2 ) 00246 { 00247 log("%c%c",buf[ii+jj],buf[ii+jj+1]); 00248 if((ii+jj) > r) break; 00249 } 00250 log("\r\n"); 00251 if((ii+jj) > r) break; 00252 } 00253 #endif 00254 00255 return r; 00256 } 00257 00258 /** 00259 * Writes an ADB message with payload to the ADB device. 00260 * 00261 * @param device USB device handle. 00262 * @param command ADB command. 00263 * @param arg0 first ADB argument (command dependent). 00264 * @param arg0 second ADB argument (command dependent). 00265 * @param length payload length. 00266 * @param data command payload. 00267 * @return error code or 0 for success. 00268 */ 00269 int ADB::writeMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1, uint32_t length, uint8_t * data) 00270 { 00271 adb_message message; 00272 uint8_t msg[256]; 00273 uint32_t count, sum = 0; 00274 uint8_t * x; 00275 00276 // Calculate data checksum 00277 count = length; 00278 x = data; 00279 while(count-- > 0) sum += *x++; 00280 00281 // Fill out the message record. 00282 message.command = command; 00283 message.arg0 = arg0; 00284 message.arg1 = arg1; 00285 message.data_length = length; 00286 message.data_check = (sum); 00287 message.magic = command ^ 0xffffffff; 00288 00289 #ifdef DEBUG 00290 printf("OUT << "); adb_printMessage(&message); 00291 #endif 00292 00293 int r = USBBulkTransfer( device , output_ep , (uint8_t*)&message , sizeof(adb_message) ); 00294 00295 if (r<0) return r; 00296 00297 #ifdef DEBUG 00298 log("[writeMessage1] size:%d\r\n",r); 00299 00300 int ii,jj; 00301 uint8_t* buf = (uint8_t*)&message; 00302 for(ii = 0 ; ii < r ; ii+=16) 00303 { 00304 for(jj = 0 ; jj < 16 ; jj+=2 ) 00305 { 00306 log("%02X%02X ",buf[ii+jj],buf[ii+jj+1]); 00307 if((ii+jj) > r) break; 00308 } 00309 log(" : "); 00310 for(jj = 0 ; jj < 16 ; jj+=2 ) 00311 { 00312 log("%c%c",buf[ii+jj],buf[ii+jj+1]); 00313 if((ii+jj) > r) break; 00314 } 00315 log("\r\n"); 00316 if((ii+jj) > r) break; 00317 } 00318 #endif 00319 00320 memcpy( msg , data , length ); 00321 00322 r = USBBulkTransfer( device , output_ep , msg , length ); 00323 log("USB SEND RET2:%d\r\n",r); 00324 00325 if (r<0) return r; 00326 00327 #ifdef DEBUG 00328 log("[writeMessage2] size:%d\r\n",r); 00329 00330 buf = msg; 00331 for(ii = 0 ; ii < r ; ii+=16) 00332 { 00333 for(jj = 0 ; jj < 16 ; jj+=2 ) 00334 { 00335 log("%02X%02X ",buf[ii+jj],buf[ii+jj+1]); 00336 if((ii+jj) > r) break; 00337 } 00338 log(" : "); 00339 for(jj = 0 ; jj < 16 ; jj+=2 ) 00340 { 00341 log("%c%c",buf[ii+jj],buf[ii+jj+1]); 00342 if((ii+jj) > r) break; 00343 } 00344 log("\r\n"); 00345 if((ii+jj) > r) break; 00346 } 00347 #endif 00348 00349 r = 0; 00350 00351 return r; 00352 } 00353 00354 /** 00355 * Writes an ADB command with a string as payload. 00356 * 00357 * @param device USB device handle. 00358 * @param command ADB command. 00359 * @param arg0 first ADB argument (command dependent). 00360 * @param arg0 second ADB argument (command dependent). 00361 * @param str payload string. 00362 * @return error code or 0 for success. 00363 */ 00364 int ADB::writeStringMessage(int device, uint32_t command, uint32_t arg0, uint32_t arg1, char * str) 00365 { 00366 return ADB::writeMessage(device, command, arg0, arg1, strlen(str) + 1, (uint8_t*)str); 00367 } 00368 00369 /** 00370 * Poll an ADB message. 00371 * @param message on success, the ADB message will be returned in this struct. 00372 * @param poll true to poll for a packet on the input endpoint, false to wait for a packet. Use false here when a packet is expected (i.e. OKAY in response to WRTE) 00373 * @return true iff a packet was successfully received, false otherwise. 00374 */ 00375 boolean ADB::pollMessage(adb_message * message, boolean poll) 00376 { 00377 int bytesRead = 0; 00378 uint8_t buf[ADB_USB_PACKETSIZE]; 00379 00380 // Poll a packet from the USB 00381 bytesRead = recv_packet_buf.GetPacket((char*)buf); 00382 00383 // Check if the USB in transfer was successful. 00384 if (bytesRead<=0) return false; 00385 00386 log("[pollMessage] byteRead size:%d\r\n",bytesRead); 00387 00388 // Check if the buffer contains a valid message 00389 memcpy((void*)message, (void*)buf, sizeof(adb_message)); 00390 00391 // If the message is corrupt, return. 00392 #if 1 00393 if (message->magic != (message->command ^ 0xffffffff)) 00394 { 00395 #ifdef DEBUG 00396 printf("Broken message, magic mismatch, %d bytes\r\n", bytesRead); 00397 return false; 00398 #endif 00399 } 00400 #endif 00401 // Check if the received number of bytes matches our expected 24 bytes of ADB message header. 00402 if (bytesRead != sizeof(adb_message)) return false; 00403 00404 return true; 00405 } 00406 00407 /** 00408 * Sends an ADB OPEN message for any connections that are currently in the CLOSED state. 00409 */ 00410 void ADB::openClosedConnections() 00411 { 00412 uint32_t timeSinceLastConnect; 00413 Connection * connection; 00414 00415 // Iterate over the connection list and send "OPEN" for the ones that are currently closed. 00416 for (connection = firstConnection; connection!=NULL; connection = connection->next) 00417 { 00418 timeSinceLastConnect = millis() - connection->lastConnectionAttempt; 00419 if (connection->status==ADB_CLOSED && timeSinceLastConnect>ADB_CONNECTION_RETRY_TIME) 00420 { 00421 // Issue open command. 00422 ADB::writeStringMessage(_device, A_OPEN, connection->localID, 0, connection->connectionString); 00423 00424 // Record the last attempt time 00425 connection->lastConnectionAttempt = millis(); 00426 connection->status = ADB_OPENING; 00427 00428 } 00429 } 00430 00431 } 00432 00433 /** 00434 * Handles and ADB OKAY message, which represents a transition in the connection state machine. 00435 * 00436 * @param connection ADB connection 00437 * @param message ADB message struct. 00438 */ 00439 void ADB::handleOkay(Connection * connection, adb_message * message) 00440 { 00441 // Check if the OKAY message was a response to a CONNECT message. 00442 if (connection->status==ADB_OPENING) 00443 { 00444 connection->status = ADB_OPEN; 00445 connection->remoteID = message->arg0; 00446 00447 ADB::fireEvent(connection, ADB_CONNECTION_OPEN, 0, NULL); 00448 } 00449 00450 // Check if the OKAY message was a response to a WRITE message. 00451 if (connection->status == ADB_WRITING) 00452 connection->status = ADB_OPEN; 00453 00454 } 00455 00456 /** 00457 * Handles an ADB CLOSE message, and fires an ADB event accordingly. 00458 * 00459 * @param connection ADB connection 00460 */ 00461 void ADB::handleClose(Connection * connection) 00462 { 00463 // Check if the CLOSE message was a response to a CONNECT message. 00464 if (connection->status==ADB_OPENING) 00465 ADB::fireEvent(connection, ADB_CONNECTION_FAILED, 0, NULL); 00466 else 00467 ADB::fireEvent(connection, ADB_CONNECTION_CLOSE, 0, NULL); 00468 00469 // Connection failed 00470 if (connection->reconnect) 00471 connection->status = ADB_CLOSED; 00472 else 00473 connection->status = ADB_UNUSED; 00474 00475 } 00476 00477 /** 00478 * Handles an ADB WRITE message. 00479 * 00480 * @param connection ADB connection 00481 * @param message ADB message struct. 00482 */ 00483 void ADB::handleWrite(Connection * connection, adb_message * message) 00484 { 00485 uint32_t bytesLeft = message->data_length; 00486 uint8_t buf[ADB_USB_PACKETSIZE]; 00487 ConnectionStatus previousStatus; 00488 int bytesRead; 00489 00490 previousStatus = connection->status; 00491 00492 connection->status = ADB_RECEIVING; 00493 connection->dataRead = 0; 00494 connection->dataSize = message->data_length; 00495 00496 while (bytesLeft>0) 00497 { 00498 int len = bytesLeft < ADB_USB_PACKETSIZE ? bytesLeft : ADB_USB_PACKETSIZE; 00499 00500 // Read payload 00501 bytesRead = recv_packet_buf.GetPacket((char*)buf); 00502 00503 00504 // Poll the USB layer. 00505 USBLoop(); 00506 00507 log("[handleWrite] byteRead size:%d\r\n",bytesRead); 00508 00509 // if (len != bytesRead) 00510 // printf("bytes read mismatch: %d expected, %d read, %ld left\r\n", len, bytesRead, bytesLeft); 00511 00512 // Break out of the read loop if there's no data to read :( 00513 if (bytesRead==-1) break; 00514 else if(bytesRead!=0) 00515 { 00516 connection->dataRead += len; 00517 ADB::fireEvent(connection, ADB_CONNECTION_RECEIVE, len, buf); 00518 00519 bytesLeft -= bytesRead; 00520 } 00521 } 00522 00523 // Send OKAY message in reply. 00524 bytesRead = ADB::writeEmptyMessage(_device, A_OKAY, message->arg1, message->arg0); 00525 00526 connection->status = previousStatus; 00527 00528 } 00529 00530 /** 00531 * Close all ADB connections. 00532 * 00533 * @param connection ADB connection 00534 * @param message ADB message struct. 00535 */ 00536 void ADB::closeAll() 00537 { 00538 Connection * connection; 00539 00540 // Iterate over all connections and close the ones that are currently open. 00541 for (connection = firstConnection; connection != NULL; connection = connection->next) 00542 if (!(connection->status==ADB_UNUSED || connection->status==ADB_CLOSED)) 00543 ADB::handleClose(connection); 00544 00545 } 00546 00547 /** 00548 * Handles an ADB connect message. This is a response to a connect message sent from our side. 00549 * @param message ADB message. 00550 */ 00551 void ADB::handleConnect(adb_message * message) 00552 { 00553 unsigned int bytesRead; 00554 uint8_t buf[MAX_BUF_SIZE]; 00555 uint16_t len; 00556 00557 // Read payload (remote ADB device ID) 00558 len = message->data_length < MAX_BUF_SIZE ? message->data_length : MAX_BUF_SIZE; 00559 bytesRead = recv_packet_buf.GetPacket((char*)buf); 00560 00561 log("[handleConnect] byteRead size:%d\r\n",bytesRead); 00562 00563 // Signal that we are now connected to an Android device (yay!) 00564 connected = true; 00565 00566 // Fire event. 00567 ADB::fireEvent(NULL, ADB_CONNECT, len, buf); 00568 00569 } 00570 00571 /** 00572 * This method is called periodically to check for new messages on the USB bus and process them. 00573 */ 00574 void ADB::poll() 00575 { 00576 Connection * connection; 00577 adb_message message; 00578 00579 // Poll the USB layer. 00580 USBLoop(); 00581 00582 // If no USB device, there's no work for us to be done, so just return. 00583 if (_device==0) return; 00584 00585 // If not connected, send a connection string to the device. 00586 if (!connected) 00587 { 00588 ADB::writeStringMessage(_device, A_CNXN, 0x01000000, 4096, (char*)"host::microbridge"); 00589 for(int ii=0;ii<400;ii++) 00590 { 00591 USBLoop(); 00592 wait_ms(1); 00593 } 00594 //wait_ms(500); // Give the device some time to respond. 00595 } 00596 00597 // If we are connected, check if there are connections that need to be opened 00598 if (connected) 00599 ADB::openClosedConnections(); 00600 00601 // Check for an incoming ADB message. 00602 if (!ADB::pollMessage(&message, true)) 00603 return; 00604 00605 // Handle a response from the ADB device to our CONNECT message. 00606 if (message.command == A_CNXN) 00607 ADB::handleConnect(&message); 00608 00609 // Handle messages for specific connections 00610 for (connection = firstConnection; connection != NULL; connection = connection->next) 00611 { 00612 if(connection->status!=ADB_UNUSED && connection->localID==message.arg1) 00613 { 00614 switch(message.command) 00615 { 00616 case A_OKAY: 00617 //printf("HANDLE OKEY\r\n"); 00618 ADB::handleOkay(connection, &message); 00619 break; 00620 case A_CLSE: 00621 printf("HANDLE CLOSE\r\n"); 00622 ADB::handleClose(connection); 00623 break; 00624 case A_WRTE: 00625 //printf("HANDLE WRITE\r\n"); 00626 ADB::handleWrite(connection, &message); 00627 break; 00628 default: 00629 break; 00630 } 00631 } 00632 } 00633 00634 } 00635 00636 void ADB::AdbreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) { 00637 00638 recv_packet_buf.PutPacket((char*)buf,len); 00639 00640 #ifdef DEBUG 00641 log("[AdbreadCallback] size:%d\r\n",len); 00642 00643 int ii,jj; 00644 for(ii = 0 ; ii < len ; ii+=16) 00645 { 00646 for(jj = 0 ; jj < 16 ; jj+=2 ) 00647 { 00648 log("%02X%02X ",buf[ii+jj],buf[ii+jj+1]); 00649 if((ii+jj) > len) break; 00650 } 00651 log(" : "); 00652 for(jj = 0 ; jj < 16 ; jj+=2 ) 00653 { 00654 log("%c%c",buf[ii+jj],buf[ii+jj+1]); 00655 if((ii+jj) > len) break; 00656 } 00657 log("\r\n"); 00658 if((ii+jj) > len) break; 00659 } 00660 #endif 00661 USBBulkTransfer(device, endpoint ,readbuff,sizeof(readbuff), AdbreadCallback, userData); 00662 // wait_ms(4); 00663 } 00664 00665 /** 00666 * Checks whether the a connected USB device is an ADB device and populates a configuration record if it is. 00667 * 00668 * @param device USB device. 00669 * @param handle pointer to a configuration record. The endpoint device address, configuration, and endpoint information will be stored here. 00670 * @return true iff the device is an ADB device. 00671 */ 00672 boolean ADB::isAdbDevice(int device, int configuration, int interfaceNumber) 00673 { 00674 boolean ret = false; 00675 00676 log("connecting Android \r\n"); 00677 00678 _device = device; 00679 _configuration = configuration; 00680 _interfaceNumber = interfaceNumber; 00681 00682 log("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); 00683 00684 int err; 00685 00686 u8 buffer[255]; 00687 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4); 00688 00689 if (err < 0) { 00690 log("Failed to get descriptor\r\n"); 00691 return(ret); 00692 } 00693 00694 int len = buffer[2] | (buffer[3] << 8); 00695 if (len > sizeof(buffer)) { 00696 log("config descriptor too large\r\n"); 00697 /* might want to truncate here */ 00698 return(ret); 00699 } 00700 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len); 00701 u8* p = buffer; 00702 input_ep=0; 00703 output_ep=0; 00704 EndpointDescriptor *epDesc; 00705 00706 log("Descriptor size:%d\r\n",len); 00707 int ii,jj; 00708 for(ii = 0 ; ii < len ; ii+=16) 00709 { 00710 for(jj = 0 ; jj < 16 ; jj+=2 ) 00711 { 00712 log("%02X%02X ",buffer[ii+jj],buffer[ii+jj+1]); 00713 if((ii+jj) > len) break; 00714 } 00715 log("\r\n"); 00716 if((ii+jj) > len) break; 00717 } 00718 u8 interface_num = 0; 00719 00720 while (p<(buffer+len)) { 00721 u8 descLen = p[0]; 00722 u8 descType = p[1]; 00723 log("descLen=%d,descType=%d\r\n",descLen,descType); 00724 switch (descType) { 00725 case DESCRIPTOR_TYPE_CONFIGURATION: 00726 log("config desc\r\n"); 00727 break; 00728 case DESCRIPTOR_TYPE_INTERFACE: 00729 interface_num = p[2]; 00730 log("interface desc num[%d]\r\n",interface_num); 00731 break; 00732 case DESCRIPTOR_TYPE_ENDPOINT: 00733 epDesc=(EndpointDescriptor*)p; 00734 if( interface_num == 1 ) 00735 { 00736 if (!input_ep && (epDesc->bEndpointAddress& 0x80)) { 00737 input_ep=epDesc->bEndpointAddress& 0x7f; 00738 //PacketSize drop 00739 log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); 00740 00741 } else if (!output_ep) { 00742 output_ep=epDesc->bEndpointAddress& 0x7f; 00743 //PacketSize drop 00744 log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",output_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); 00745 } else { 00746 //other 00747 log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); 00748 } 00749 } 00750 break; 00751 default: 00752 log("unkown desc type(%d) \r\n",descType); 00753 } 00754 p+=descLen; 00755 } 00756 00757 if (!(input_ep && output_ep)) { 00758 log("can't find accessory endpoints\r\n"); 00759 return(false); 00760 } 00761 00762 log("SetConfiguration\r\n"); 00763 err = SetConfiguration(device,configuration); 00764 if (err < 0) { 00765 log("SetConfiguration error\r\n"); 00766 return(false); 00767 } 00768 00769 log("interrupt setup\r\n"); 00770 //interrupt setup 00771 if (IO_PENDING!=USBBulkTransfer(_device,input_ep|0x80,readbuff,sizeof(readbuff),AdbreadCallback,NULL)) return(ret); 00772 00773 log("ADB Standby\r\n"); 00774 ret = true; 00775 00776 return(ret); 00777 } 00778 00779 00780 void desconnect(void) 00781 { 00782 ADB::closeAll(); 00783 _device = 0; 00784 connected = false; 00785 } 00786 00787 /** 00788 * Write a set of bytes to an open ADB connection. 00789 * 00790 * @param connection ADB connection to write the data to. 00791 * @param length number of bytes to transmit. 00792 * @param data data to send. 00793 * @return number of transmitted bytes, or -1 on failure. 00794 */ 00795 int ADB::write(Connection * connection, uint16_t length, uint8_t * data) 00796 { 00797 int ret; 00798 00799 // First check if we have a working ADB connection 00800 if (_device==0 || !connected) return -1; 00801 00802 // Check if the connection is open for writing. 00803 if (connection->status != ADB_OPEN) return -2; 00804 00805 // Write payload 00806 ret = ADB::writeMessage(_device, A_WRTE, connection->localID, connection->remoteID, length, data); 00807 if (ret==0) 00808 connection->status = ADB_WRITING; 00809 00810 return ret; 00811 } 00812 00813 /** 00814 * Write a string to an open ADB connection. The trailing zero is not transmitted. 00815 * 00816 * @param connection ADB connection to write the data to. 00817 * @param length number of bytes to transmit. 00818 * @param data data to send. 00819 * @return number of transmitted bytes, or -1 on failure. 00820 */ 00821 int ADB::writeString(Connection * connection, char * str) 00822 { 00823 int ret; 00824 00825 // First check if we have a working ADB connection 00826 if (_device==0 || !connected) return -1; 00827 00828 // Check if the connection is open for writing. 00829 if (connection->status != ADB_OPEN) return -2; 00830 00831 // Write payload 00832 ret = ADB::writeStringMessage(_device, A_WRTE, connection->localID, connection->remoteID, str); 00833 if (ret==0) 00834 connection->status = ADB_WRITING; 00835 00836 return ret; 00837 } 00838 00839 /** 00840 * Write a set of bytes to this ADB connection. 00841 * 00842 * @param length number of bytes to transmit. 00843 * @param data data to send. 00844 * @return number of transmitted bytes, or -1 on failure. 00845 */ 00846 int Connection::write(uint16_t length, uint8_t * data) 00847 { 00848 return ADB::write(this, length, data); 00849 } 00850 00851 /** 00852 * Write a string to this connection. 00853 * 00854 * @param length number of bytes to transmit. 00855 * @param data data to send. 00856 * @return number of transmitted bytes, or -1 on failure. 00857 */ 00858 int Connection::writeString(char * str) 00859 { 00860 return ADB::writeString(this, str); 00861 } 00862 00863 /** 00864 * Checks if the connection is open for writing. 00865 * @return true iff the connection is open and ready to accept write commands. 00866 */ 00867 bool Connection::isOpen() 00868 { 00869 return this->status == ADB_OPEN; 00870 } 00871 00872 00873 /** from USBHost load function. initialize Android device**/ 00874 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) { 00875 char s[128]; 00876 00877 log("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); 00878 00879 for (int i = 1; i < 4; i++) { 00880 if (GetString(device,i,s,sizeof(s)) < 0) 00881 break; 00882 printf("%d: %s\r\n",i,s); 00883 } 00884 00885 // Adb? 00886 if(1) 00887 { 00888 ADB::isAdbDevice(device,1,2); 00889 } 00890 00891 }
Generated on Tue Jul 12 2022 14:15:58 by
1.7.2
