adk init() cleanup

This commit is contained in:
Oleg Mazurov 2011-07-02 12:43:38 -06:00
parent 7d177118e5
commit 55b60c59bc
2 changed files with 93 additions and 108 deletions

134
adk.cpp
View file

@ -13,28 +13,27 @@ ADK::ADK(USB *p, const char* manufacturer,
const char* serial) : const char* serial) :
pUsb(p), //pointer to USB class instance - mandatory pUsb(p), //pointer to USB class instance - mandatory
bAddress(0), //device address - mandatory
bNumEP(1), //if config descriptor needs to be parsed
/* ADK ID Strings */ /* ADK ID Strings */
manufacturer(manufacturer), manufacturer(manufacturer),
model(model), model(model),
description(description), description(description),
version(version), version(version),
uri(uri), uri(uri),
serial(serial), serial(serial)
bAddress(0), //device address - mandatory
bNumEP(1) //if config descriptor needs to be parsed
{ {
/* initialize endpoint data structures */ // initialize endpoint data structures
for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++) for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++) {
{
epInfo[i].epAddr = 0; epInfo[i].epAddr = 0;
epInfo[i].maxPktSize = (i) ? 0 : 8; epInfo[i].maxPktSize = (i) ? 0 : 8;
epInfo[i].epAttribs = 0; epInfo[i].epAttribs = ( 0xfc & ( USB_NAK_MAX_POWER<<2 ));
}//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++...
if (!i) { // register in USB subsystem
epInfo[i].bmNakPower = USB_NAK_MAX_POWER;
}
}
if (pUsb) { if (pUsb) {
pUsb->RegisterDeviceClass(this); //set devConfig[] entry pUsb->RegisterDeviceClass(this); //set devConfig[] entry
} }
@ -43,9 +42,8 @@ ADK::ADK(USB *p, const char* manufacturer,
/* Connection initialization of an Android phone */ /* Connection initialization of an Android phone */
uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
{ {
const uint8_t constBufSize = sizeof(USB_DEVICE_DESCRIPTOR);
uint8_t buf[constBufSize]; uint8_t buf[sizeof(USB_DEVICE_DESCRIPTOR)];
uint8_t rcode; uint8_t rcode;
UsbDevice *p = NULL; UsbDevice *p = NULL;
EpInfo *oldep_ptr = NULL; EpInfo *oldep_ptr = NULL;
@ -62,9 +60,6 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE; return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
} }
//USBTRACE("\r\nHere");
// Get pointer to pseudo device with address 0 assigned // Get pointer to pseudo device with address 0 assigned
p = addrPool.GetUsbDevicePtr(0); p = addrPool.GetUsbDevicePtr(0);
@ -87,7 +82,7 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
p->lowspeed = lowspeed; p->lowspeed = lowspeed;
// Get device descriptor // Get device descriptor
rcode = pUsb->getDevDescr( 0, 0, constBufSize, (uint8_t*)buf ); rcode = pUsb->getDevDescr( 0, 0, sizeof(USB_DEVICE_DESCRIPTOR), (uint8_t*)buf );
// Restore p->epinfo // Restore p->epinfo
p->epinfo = oldep_ptr; p->epinfo = oldep_ptr;
@ -104,15 +99,13 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
// Assign new address to the device // Assign new address to the device
rcode = pUsb->setAddr( 0, 0, bAddress ); rcode = pUsb->setAddr( 0, 0, bAddress );
if (rcode) {
if (rcode)
{
p->lowspeed = false; p->lowspeed = false;
addrPool.FreeAddress(bAddress); addrPool.FreeAddress(bAddress);
bAddress = 0; bAddress = 0;
USBTRACE2("setAddr:",rcode); USBTRACE2("setAddr:",rcode);
return rcode; return rcode;
} }//if (rcode...
USBTRACE2("\r\nAddr:", bAddress); USBTRACE2("\r\nAddr:", bAddress);
@ -128,37 +121,30 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
// Assign epInfo to epinfo pointer // Assign epInfo to epinfo pointer
rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
if (rcode) {
if (rcode)
goto FailSetDevTblEntry; goto FailSetDevTblEntry;
}
//check if ADK device is already in accessory mode; if yes, configure and exit
//check if ADK device is already in accessory mode
if(((USB_DEVICE_DESCRIPTOR*)buf)->idVendor == ADK_VID && if(((USB_DEVICE_DESCRIPTOR*)buf)->idVendor == ADK_VID &&
(((USB_DEVICE_DESCRIPTOR*)buf)->idProduct == ADK_PID || ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct == ADB_PID)) { (((USB_DEVICE_DESCRIPTOR*)buf)->idProduct == ADK_PID || ((USB_DEVICE_DESCRIPTOR*)buf)->idProduct == ADB_PID)) {
USBTRACE("\r\nAcc.mode device detected"); USBTRACE("\r\nAcc.mode device detected");
/* debug code start */ /* go through configurations, find first bulk-IN, bulk-OUT EP, fill epInfo and quit */
num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations; num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
USBTRACE2("\r\nNC:",num_of_conf); USBTRACE2("\r\nNC:",num_of_conf);
USBTRACE2("\r\nNP:",epInfo[0].bmNakPower);
for (uint8_t i=0; i<num_of_conf; i++) { for (uint8_t i=0; i<num_of_conf; i++) {
//USBTRACE("\r\nHexdumper: ");
//HexDumper<USBReadParser, uint16_t, uint16_t> HexDump;
ConfigDescParser<0, 0, 0, 0> confDescrParser(this); ConfigDescParser<0, 0, 0, 0> confDescrParser(this);
//rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump);
//extracting endpoint information. See EndpointXtract()
rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser);
if( rcode ) {
goto FailGetConfDescr;
}
if( bNumEP > 2 ) { if( bNumEP > 2 ) {
break; break;
} }
} // for (uint8_t i=0; i<num_of_conf; i++... } // for (uint8_t i=0; i<num_of_conf; i++...
/* debug code end */
// Set Configuration Value // Set Configuration Value
rcode = pUsb->setConf(bAddress, 0, bConfNum); rcode = pUsb->setConf(bAddress, 0, bConfNum);
if( rcode ){ if( rcode ){
@ -202,7 +188,7 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
sendStr( ACCESSORY_STRING_SERIAL, serial); sendStr( ACCESSORY_STRING_SERIAL, serial);
//switch to accessory mode //switch to accessory mode
//the phone will reset //the Android phone will reset
rcode = switchAcc(); rcode = switchAcc();
if( rcode ) { if( rcode ) {
goto FailSwAcc; //init fails goto FailSwAcc; //init fails
@ -210,7 +196,7 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
rcode = -1; rcode = -1;
goto SwAttempt; //switch to accessory mode attempted goto SwAttempt; //switch to accessory mode attempted
/* diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
USBTRACE("\r\ngetDevDescr:"); USBTRACE("\r\ngetDevDescr:");
goto Fail; goto Fail;
@ -235,9 +221,9 @@ SwAttempt:
// USBTRACE("setDevTblEn:"); // USBTRACE("setDevTblEn:");
// goto Fail; // goto Fail;
// //
//FailGetConfDescr: FailGetConfDescr:
// USBTRACE("getConf:"); // USBTRACE("getConf:");
// goto Fail; goto Fail;
// //
FailSetConf: FailSetConf:
// USBTRACE("setConf:"); // USBTRACE("setConf:");
@ -271,8 +257,6 @@ void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto
// Fill in the endpoint info structure // Fill in the endpoint info structure
epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F); epInfo[index].epAddr = (pep->bEndpointAddress & 0x0F);
epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize; epInfo[index].maxPktSize = (uint8_t)pep->wMaxPacketSize;
epInfo[index].epAttribs = ( 0x3f & USB_NAK_MAX_POWER );
//epInfo[index].bmbmNakPower = USB_NAK_MAX_POWER;
bNumEP ++; bNumEP ++;
@ -294,42 +278,42 @@ uint8_t ADK::Release()
return 0; return 0;
} }
uint8_t ADK::Poll() //uint8_t ADK::Poll()
{ //{
uint8_t rcode = 0; // uint8_t rcode = 0;
//
if (!bPollEnable) // if (!bPollEnable)
return 0; // return 0;
//
uint32_t time_now = millis(); // uint32_t time_now = millis();
//
if (qNextPollTime <= time_now) // if (qNextPollTime <= time_now)
{
qNextPollTime = time_now + 100;
uint8_t rcode;
const uint8_t constBufSize = 16;
uint8_t buf[constBufSize];
for (uint8_t i=0; i<constBufSize; i++)
buf[i] = 0;
// uint16_t read = (constBufSize > epInfo[epInterruptInIndex].maxPktSize)
// ? epInfo[epInterruptInIndex].maxPktSize : constBufSize;
// rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex].epAddr, &read, buf);
if (rcode)
return rcode;
// for (uint8_t i=0; i<read; i++)
// { // {
// PrintHex<uint8_t>(buf[i]); // qNextPollTime = time_now + 100;
// Serial.print(" "); //
// uint8_t rcode;
// const uint8_t constBufSize = 16;
// uint8_t buf[constBufSize];
//
// for (uint8_t i=0; i<constBufSize; i++)
// buf[i] = 0;
//
//// uint16_t read = (constBufSize > epInfo[epInterruptInIndex].maxPktSize)
//// ? epInfo[epInterruptInIndex].maxPktSize : constBufSize;
//// rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex].epAddr, &read, buf);
//
// if (rcode)
// return rcode;
//
//// for (uint8_t i=0; i<read; i++)
//// {
//// PrintHex<uint8_t>(buf[i]);
//// Serial.print(" ");
//// }
//// USBTRACE("\r\n");
// } // }
// USBTRACE("\r\n"); // return rcode;
} //}
return rcode;
}
uint8_t ADK::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) uint8_t ADK::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr)
{ {

11
adk.h
View file

@ -66,11 +66,11 @@ protected:
USB *pUsb; USB *pUsb;
uint8_t bAddress; uint8_t bAddress;
uint8_t bConfNum; // configuration number uint8_t bConfNum; // configuration number
uint8_t bControlIface; // Control interface value // uint8_t bControlIface; // Control interface value
uint8_t bDataIface; // Data interface value // uint8_t bDataIface; // Data interface value
uint8_t bNumEP; // total number of EP in the configuration uint8_t bNumEP; // total number of EP in the configuration
uint32_t qNextPollTime; // next poll time // uint32_t qNextPollTime; // next poll time
bool bPollEnable; // poll enable flag // bool bPollEnable; // poll enable flag
//uint8_t bInitState; //initialization state machine state //uint8_t bInitState; //initialization state machine state
/* Endpoint data structure */ /* Endpoint data structure */
@ -94,12 +94,13 @@ public:
// USBDeviceConfig implementation // USBDeviceConfig implementation
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
virtual uint8_t Release(); virtual uint8_t Release();
virtual uint8_t Poll(); virtual uint8_t Poll(){}; //not implemented
virtual uint8_t GetAddress() { return bAddress; }; virtual uint8_t GetAddress() { return bAddress; };
//UsbConfigXtracter implementation //UsbConfigXtracter implementation
virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep); virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
}; //class ADK : public USBDeviceConfig ... }; //class ADK : public USBDeviceConfig ...
/* get ADK protocol version */ /* get ADK protocol version */
/* returns 2 bytes in *adkproto */ /* returns 2 bytes in *adkproto */
inline uint8_t ADK::getProto( uint8_t* adkproto ) inline uint8_t ADK::getProto( uint8_t* adkproto )