summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/USBHost/USBHost3GModule/WANDongle.cpp
blob: 49cafb7cc3d021f24c0be3b2fe61f26949d236f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Copyright (c) 2010-2012 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.
*/

#include "USBHostConf.h"

#ifdef USBHOST_3GMODULE

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

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

WANDongle::WANDongle() : m_pInitializer(NULL), m_serialCount(0), m_totalInitializers(0)
{
    host = USBHost::getHostInst();
    init();
}


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

bool WANDongle::tryConnect()
{
  //FIXME should run on USB thread

  USB_DBG("Trying to connect device");

  if (dev_connected) {
      USB_DBG("Device is already connected!");
      return true;
  }

  m_pInitializer = NULL;

  //Protect from concurrent access from USB thread
  USBHost::Lock lock(host);

  for (int i = 0; i < MAX_DEVICE_CONNECTED; i++)
  {
      if ((dev = host->getDevice(i)) != NULL)
      {
          m_pInitializer = NULL; //Will be set in setVidPid callback

          USB_DBG("Enumerate");
          int ret = host->enumerate(dev, this);
          if(ret)
          {
            return false;
          }

          USB_DBG("Device has VID:%04x PID:%04x", dev->getVid(), dev->getPid());

          if(m_pInitializer) //If an initializer has been found
          {
            USB_DBG("m_pInitializer=%p", m_pInitializer);
            USB_DBG("m_pInitializer->getSerialVid()=%04x", m_pInitializer->getSerialVid());
            USB_DBG("m_pInitializer->getSerialPid()=%04x", m_pInitializer->getSerialPid());
            if ((dev->getVid() == m_pInitializer->getSerialVid()) && (dev->getPid() == m_pInitializer->getSerialPid()))
            {
              USB_DBG("The dongle is in virtual serial mode");
              host->registerDriver(dev, 0, this, &WANDongle::init);
              m_serialCount = m_pInitializer->getSerialPortCount();
              if( m_serialCount > WANDONGLE_MAX_SERIAL_PORTS )
              {
                m_serialCount = WANDONGLE_MAX_SERIAL_PORTS;
              }
              for(int j = 0; j < m_serialCount; j++)
              {
                USB_DBG("Connecting serial port #%d", j+1);
                USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, false));
                USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, true));
                m_serial[j].connect( dev, m_pInitializer->getEp(dev, j, false), m_pInitializer->getEp(dev, j, true) );
              }

              USB_DBG("Device connected");

              dev_connected = true;


              return true;
            }
            else if ((dev->getVid() == m_pInitializer->getMSDVid()) && (dev->getPid() == m_pInitializer->getMSDPid()))
            {
              USB_DBG("Vodafone K3370 dongle detected in MSD mode");
              //Try to switch
              if( m_pInitializer->switchMode(dev) )
              {
                USB_DBG("Switched OK");
                return false; //Will be connected on a next iteration
              }
              else
              {
                USB_ERR("Could not switch mode");
                return false;
              }
            }
          } //if()
      } //if()
  } //for()
  return false;
}

bool WANDongle::disconnect()
{
  dev_connected = false;
  for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
  {
    m_serial[i].disconnect();
  }
  return true;
}

int WANDongle::getDongleType()
{
  if( m_pInitializer != NULL )
  {
    return m_pInitializer->getType();
  }
  else
  {
    return WAN_DONGLE_TYPE_UNKNOWN;
  }
}

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

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

//Private methods
void WANDongle::init()
{
  m_pInitializer = NULL;
  dev_connected = false;
  for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
  {
    m_serial[i].init(host);
  }
}


/*virtual*/ void WANDongle::setVidPid(uint16_t vid, uint16_t pid)
{
  WANDongleInitializer* initializer;

  for(int i = 0; i < m_totalInitializers; i++)
  {
    initializer = m_Initializers[i];
    USB_DBG("initializer=%p", initializer);
    USB_DBG("initializer->getSerialVid()=%04x", initializer->getSerialVid());
    USB_DBG("initializer->getSerialPid()=%04x", initializer->getSerialPid());
    if ((dev->getVid() == initializer->getSerialVid()) && (dev->getPid() == initializer->getSerialPid()))
    {
      USB_DBG("The dongle is in virtual serial mode");
      m_pInitializer = initializer;
      break;
    }
    else if ((dev->getVid() == initializer->getMSDVid()) && (dev->getPid() == initializer->getMSDPid()))
    {
      USB_DBG("Dongle detected in MSD mode");
      m_pInitializer = initializer;
      break;
    }
    initializer++;
  } //for
  if(m_pInitializer)
  {
    m_pInitializer->setVidPid(vid, pid);
  }
}

/*virtual*/ bool WANDongle::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
{
  if(m_pInitializer)
  {
    return m_pInitializer->parseInterface(intf_nb, intf_class, intf_subclass, intf_protocol);
  }
  else
  {
    return false;
  }
}

/*virtual*/ bool WANDongle::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
{
  if(m_pInitializer)
  {
    return m_pInitializer->useEndpoint(intf_nb, type, dir);
  }
  else
  {
    return false;
  }
}


bool WANDongle::addInitializer(WANDongleInitializer* pInitializer)
{
  if (m_totalInitializers >= WANDONGLE_MAX_INITIALIZERS)
    return false;
  m_Initializers[m_totalInitializers++] = pInitializer;
  return true;
}

WANDongle::~WANDongle()
{
  for(int i = 0; i < m_totalInitializers; i++)
    delete m_Initializers[i];
}

#endif /* USBHOST_3GMODULE */