Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
gert_lauritsen
Date:
Fri Jul 05 12:55:53 2019 +0000
Branch:
MbedBMAGThrRev
Revision:
61:52a6d0ffd8e5
dato rettel og ID i file lagt ind i version 5

Who changed what in which revision?

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