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