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