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