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