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