Changes to support Vodafone K4606

Dependents:   VodafoneUSBModem

Fork of USBHostWANDongle by mbed official

USB3GModule/WANDongle.cpp

Committer:
donatien
Date:
2012-06-26
Revision:
2:a8b2d0cd9bbd
Parent:
1:49df46e3295c
Child:
3:4394986752db

File content as of revision 2:a8b2d0cd9bbd:

/* Copyright (c) 2010-2011 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#define __DEBUG__ 4 //Maximum verbosity
#ifndef __MODULE__
#define __MODULE__ "WANDongle.cpp"
#endif

#include "dbg.h"
#include <cstdint>
#include "rtos.h"

#include "WANDongle.h"
#include "WANDongleInitializer.h"

WANDongle::WANDongle() : cb_tx_en(false), cb_rx_en(false), listener(NULL), m_serialCount(0)
{
    host = USBHost::getHostInst();
    init();
}


bool WANDongle::connected() {
  return dev_connected;
}

bool WANDongle::tryConnect()
{
  bool found = false;

  //FIXME should run on USB thread

  DBG("Trying to connect device");

  if (dev_connected) {
      return true;
  }

  host->lock();

  for (int i = 0; i < MAX_DEVICE_NB; i++)
  {
      if ((dev = host->getDevice(i)) != NULL)
      {
          DBG("Found one device, reset it");
          host->resetDevice(dev);

          DBG("Enumerate");
          host->enumerate(dev);
          
          WANDongleInitializer* initializer = getInitializers();
          
          while(initializer++)
          {
            if ((dev->getVid() == initializer->getSerialVid()) && (dev->getPid() == initializer->getSerialPid()))
            {
              host->registerDriver(dev, 0, this, &WANDongle::init);
              m_serialCount = initializer->getSerialPortCount();
              for(int j = 0; j < m_serialCount; j++)
              {
                m_serial[j].connect( initializer->getEp(dev, j, false), initializer->getEp(dev, j, true) );
              }
              dev_connected = true;
              host->unlock();
              return true;
            }
            else if ((dev->getVid() == initializer->getMSDVid()) && (dev->getPid() == initializer->getMSDPid()))
            {
              DBG("Vodafone K3370 dongle detected in MSD mode");
              //Try to switch   
              if( initializer->switchMode() )
              {
                DBG("Switched OK");
                host->unlock();
                return false; //Will be connected on a next iteration
              }
              else
              {
                ERR("Could not switch mode");
                host->unlock();
                return false;
              }
            }
          } //while()
      } //if()
  } //for()
  host->unlock();
  return false;
}

IUSBHostSerial* WANDongle::getSerial(int index)
{
  return m_serial[index];
}

int WANDongle::getSerialCount()
{
  return m_serialCount;
}

//Private methods
void WANDongle::init()
{
  dev_connected = false;
}