|
| 1 | +/* Copyright (c) 2010-2012 mbed.org, MIT License |
| 2 | +* |
| 3 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| 4 | +* and associated documentation files (the "Software"), to deal in the Software without |
| 5 | +* restriction, including without limitation the rights to use, copy, modify, merge, publish, |
| 6 | +* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
| 7 | +* Software is furnished to do so, subject to the following conditions: |
| 8 | +* |
| 9 | +* The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +* substantial portions of the Software. |
| 11 | +* |
| 12 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 13 | +* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 14 | +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 15 | +* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | +*/ |
| 18 | + |
| 19 | +#include "USBHostConf.h" |
| 20 | + |
| 21 | +#ifdef USBHOST_3GMODULE |
| 22 | + |
| 23 | +#define __DEBUG__ 0 |
| 24 | +#ifndef __MODULE__ |
| 25 | +#define __MODULE__ "WANDongle.cpp" |
| 26 | +#endif |
| 27 | + |
| 28 | +#include "core/dbg.h" |
| 29 | +#include <cstdint> |
| 30 | +#include "rtos.h" |
| 31 | + |
| 32 | +#include "WANDongle.h" |
| 33 | +#include "WANDongleInitializer.h" |
| 34 | + |
| 35 | +WANDongle::WANDongle() : m_pInitializer(NULL), m_serialCount(0) |
| 36 | +{ |
| 37 | + host = USBHost::getHostInst(); |
| 38 | + init(); |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +bool WANDongle::connected() { |
| 43 | + return dev_connected; |
| 44 | +} |
| 45 | + |
| 46 | +bool WANDongle::tryConnect() |
| 47 | +{ |
| 48 | + //FIXME should run on USB thread |
| 49 | + |
| 50 | + DBG("Trying to connect device"); |
| 51 | + |
| 52 | + if (dev_connected) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + m_pInitializer = NULL; |
| 57 | + |
| 58 | + for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) |
| 59 | + { |
| 60 | + if ((dev = host->getDevice(i)) != NULL) |
| 61 | + { |
| 62 | + m_pInitializer = NULL; //Will be set in setVidPid callback |
| 63 | + |
| 64 | + DBG("Enumerate"); |
| 65 | + int ret = host->enumerate(dev, this); |
| 66 | + if(ret) |
| 67 | + { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + DBG("Device has VID:%04x PID:%04x", dev->getVid(), dev->getPid()); |
| 72 | + |
| 73 | + if(m_pInitializer) //If an initializer has been found |
| 74 | + { |
| 75 | + DBG("m_pInitializer=%p", m_pInitializer); |
| 76 | + DBG("m_pInitializer->getSerialVid()=%04x", m_pInitializer->getSerialVid()); |
| 77 | + DBG("m_pInitializer->getSerialPid()=%04x", m_pInitializer->getSerialPid()); |
| 78 | + if ((dev->getVid() == m_pInitializer->getSerialVid()) && (dev->getPid() == m_pInitializer->getSerialPid())) |
| 79 | + { |
| 80 | + DBG("The dongle is in virtual serial mode"); |
| 81 | + host->registerDriver(dev, 0, this, &WANDongle::init); |
| 82 | + m_serialCount = m_pInitializer->getSerialPortCount(); |
| 83 | + if( m_serialCount > WANDONGLE_MAX_SERIAL_PORTS ) |
| 84 | + { |
| 85 | + m_serialCount = WANDONGLE_MAX_SERIAL_PORTS; |
| 86 | + } |
| 87 | + for(int j = 0; j < m_serialCount; j++) |
| 88 | + { |
| 89 | + DBG("Connecting serial port #%d", j+1); |
| 90 | + DBG("Ep %p", m_pInitializer->getEp(dev, j, false)); |
| 91 | + DBG("Ep %p", m_pInitializer->getEp(dev, j, true)); |
| 92 | + m_serial[j].connect( dev, m_pInitializer->getEp(dev, j, false), m_pInitializer->getEp(dev, j, true) ); |
| 93 | + } |
| 94 | + |
| 95 | + DBG("Device connected"); |
| 96 | + |
| 97 | + dev_connected = true; |
| 98 | + |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + else if ((dev->getVid() == m_pInitializer->getMSDVid()) && (dev->getPid() == m_pInitializer->getMSDPid())) |
| 103 | + { |
| 104 | + DBG("Vodafone K3370 dongle detected in MSD mode"); |
| 105 | + //Try to switch |
| 106 | + if( m_pInitializer->switchMode(dev) ) |
| 107 | + { |
| 108 | + DBG("Switched OK"); |
| 109 | + return false; //Will be connected on a next iteration |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + ERR("Could not switch mode"); |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + } //if() |
| 118 | + } //if() |
| 119 | + } //for() |
| 120 | + return false; |
| 121 | +} |
| 122 | + |
| 123 | +bool WANDongle::disconnect() |
| 124 | +{ |
| 125 | + dev_connected = false; |
| 126 | + for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++) |
| 127 | + { |
| 128 | + m_serial[i].disconnect(); |
| 129 | + } |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +WAN_DONGLE_TYPE WANDongle::getDongleType() |
| 134 | +{ |
| 135 | + if( m_pInitializer != NULL ) |
| 136 | + { |
| 137 | + return m_pInitializer->getType(); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + return WAN_DONGLE_TYPE_UNKNOWN; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +IUSBHostSerial& WANDongle::getSerial(int index) |
| 146 | +{ |
| 147 | + return m_serial[index]; |
| 148 | +} |
| 149 | + |
| 150 | +int WANDongle::getSerialCount() |
| 151 | +{ |
| 152 | + return m_serialCount; |
| 153 | +} |
| 154 | + |
| 155 | +//Private methods |
| 156 | +void WANDongle::init() |
| 157 | +{ |
| 158 | + m_pInitializer = NULL; |
| 159 | + dev_connected = false; |
| 160 | + for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++) |
| 161 | + { |
| 162 | + m_serial[i].init(host); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +/*virtual*/ void WANDongle::setVidPid(uint16_t vid, uint16_t pid) |
| 168 | +{ |
| 169 | + //Load right initializer |
| 170 | + WANDongleInitializer** initializer = WANDongleInitializer::getInitializers(host); |
| 171 | + |
| 172 | + while(*initializer) |
| 173 | + { |
| 174 | + DBG("*initializer=%p", *initializer); |
| 175 | + DBG("(*initializer)->getSerialVid()=%04x", (*initializer)->getSerialVid()); |
| 176 | + DBG("(*initializer)->getSerialPid()=%04x", (*initializer)->getSerialPid()); |
| 177 | + if ((dev->getVid() == (*initializer)->getSerialVid()) && (dev->getPid() == (*initializer)->getSerialPid())) |
| 178 | + { |
| 179 | + DBG("The dongle is in virtual serial mode"); |
| 180 | + m_pInitializer = *initializer; |
| 181 | + break; |
| 182 | + } |
| 183 | + else if ((dev->getVid() == (*initializer)->getMSDVid()) && (dev->getPid() == (*initializer)->getMSDPid())) |
| 184 | + { |
| 185 | + DBG("Vodafone K3370 dongle detected in MSD mode"); |
| 186 | + m_pInitializer = *initializer; |
| 187 | + break; |
| 188 | + } |
| 189 | + initializer++; |
| 190 | + } //while() |
| 191 | + if(m_pInitializer) |
| 192 | + { |
| 193 | + m_pInitializer->setVidPid(vid, pid); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +/*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 |
| 198 | +{ |
| 199 | + if(m_pInitializer) |
| 200 | + { |
| 201 | + return m_pInitializer->parseInterface(intf_nb, intf_class, intf_subclass, intf_protocol); |
| 202 | + } |
| 203 | + else |
| 204 | + { |
| 205 | + return false; |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +/*virtual*/ bool WANDongle::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
| 210 | +{ |
| 211 | + if(m_pInitializer) |
| 212 | + { |
| 213 | + return m_pInitializer->useEndpoint(intf_nb, type, dir); |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + return false; |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +#endif /* USBHOST_3GMODULE */ |
0 commit comments