Skip to content

Commit a0d9973

Browse files
committed
Initial import of USB3GModem in the USBHost library
In the future, USBHostConf.h should really reside in the project that imports USBHost, not inside the USBHost library itself. Doing that now though might break compatibility with projects that currently import USBHost, so we need to figure out a better solution.
1 parent 6b23e0f commit a0d9973

File tree

9 files changed

+1493
-9
lines changed

9 files changed

+1493
-9
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* IUSBHostSerial.h */
2+
/* Copyright (c) 2010-2012 mbed.org, MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5+
* and associated documentation files (the "Software"), to deal in the Software without
6+
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
7+
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
8+
* Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
20+
#ifndef IUSBHOSTSERIAL_H_
21+
#define IUSBHOSTSERIAL_H_
22+
23+
/**
24+
* Generic interface to abstract 3G dongles' impl
25+
*/
26+
27+
#include "USBHostConf.h"
28+
29+
#ifdef USBHOST_3GMODULE
30+
31+
#include "IUSBHostSerialListener.h"
32+
33+
class IUSBHostSerial {
34+
public:
35+
36+
enum IrqType {
37+
RxIrq,
38+
TxIrq
39+
};
40+
41+
/*
42+
* Get a char from the dongle's serial interface
43+
*/
44+
virtual int getc() = 0;
45+
46+
/*
47+
* Put a char to the dongle's serial interface
48+
*/
49+
virtual int putc(int c) = 0;
50+
51+
/*
52+
* Read a packet from the dongle's serial interface, to be called after multiple getc() calls
53+
*/
54+
virtual int readPacket() = 0;
55+
56+
/*
57+
* Write a packet to the dongle's serial interface, to be called after multiple putc() calls
58+
*/
59+
virtual int writePacket() = 0;
60+
61+
/**
62+
* Check the number of bytes available.
63+
*
64+
* @returns the number of bytes available
65+
*/
66+
virtual int readable() = 0;
67+
68+
/**
69+
* Check the free space in output.
70+
*
71+
* @returns the number of bytes available
72+
*/
73+
virtual int writeable() = 0;
74+
75+
/**
76+
* Attach a handler to call when a packet is received / when a packet has been transmitted.
77+
*
78+
* @param pListener instance of the listener deriving from the IUSBHostSerialListener
79+
*/
80+
virtual void attach(IUSBHostSerialListener* pListener) = 0;
81+
82+
/**
83+
* Enable or disable readable/writeable callbacks
84+
*/
85+
virtual void setupIrq(bool en, IrqType irq = RxIrq) = 0;
86+
87+
};
88+
89+
#endif /* USBHOST_3GMODULE */
90+
91+
#endif /* IUSBHOSTSERIAL_H_ */
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* IUSBHostSerialListener.h */
2+
/* Copyright (c) 2010-2012 mbed.org, MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5+
* and associated documentation files (the "Software"), to deal in the Software without
6+
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
7+
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
8+
* Software is furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all copies or
11+
* substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
20+
21+
#ifndef IUSBHOSTSERIALLISTENER_H_
22+
#define IUSBHOSTSERIALLISTENER_H_
23+
24+
#include "USBHostConf.h"
25+
26+
#ifdef USBHOST_3GMODULE
27+
28+
class IUSBHostSerialListener
29+
{
30+
public:
31+
virtual void readable() = 0; //Called when new data is available
32+
virtual void writeable() = 0; //Called when new space is available
33+
};
34+
35+
#endif /* USBHOST_3GMODULE */
36+
37+
#endif /* IUSBHOSTSERIALLISTENER_H_ */
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/* Copyright (c) 2010-2012 mbed.org, MIT License
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4+
* and associated documentation files (the "Software"), to deal in the Software without
5+
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
6+
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7+
* Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or
10+
* substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
19+
#include "USBHostConf.h"
20+
21+
#ifdef USBHOST_3GMODULE
22+
23+
#define __DEBUG__ 0
24+
#ifndef __MODULE__
25+
#define __MODULE__ "WANDongle.cpp"
26+
#endif
27+
28+
#include "core/dbg.h"
29+
#include <cstdint>
30+
#include "rtos.h"
31+
32+
#include "WANDongle.h"
33+
#include "WANDongleInitializer.h"
34+
35+
WANDongle::WANDongle() : m_pInitializer(NULL), m_serialCount(0)
36+
{
37+
host = USBHost::getHostInst();
38+
init();
39+
}
40+
41+
42+
bool WANDongle::connected() {
43+
return dev_connected;
44+
}
45+
46+
bool WANDongle::tryConnect()
47+
{
48+
//FIXME should run on USB thread
49+
50+
DBG("Trying to connect device");
51+
52+
if (dev_connected) {
53+
return true;
54+
}
55+
56+
m_pInitializer = NULL;
57+
58+
for (int i = 0; i < MAX_DEVICE_CONNECTED; i++)
59+
{
60+
if ((dev = host->getDevice(i)) != NULL)
61+
{
62+
m_pInitializer = NULL; //Will be set in setVidPid callback
63+
64+
DBG("Enumerate");
65+
int ret = host->enumerate(dev, this);
66+
if(ret)
67+
{
68+
return false;
69+
}
70+
71+
DBG("Device has VID:%04x PID:%04x", dev->getVid(), dev->getPid());
72+
73+
if(m_pInitializer) //If an initializer has been found
74+
{
75+
DBG("m_pInitializer=%p", m_pInitializer);
76+
DBG("m_pInitializer->getSerialVid()=%04x", m_pInitializer->getSerialVid());
77+
DBG("m_pInitializer->getSerialPid()=%04x", m_pInitializer->getSerialPid());
78+
if ((dev->getVid() == m_pInitializer->getSerialVid()) && (dev->getPid() == m_pInitializer->getSerialPid()))
79+
{
80+
DBG("The dongle is in virtual serial mode");
81+
host->registerDriver(dev, 0, this, &WANDongle::init);
82+
m_serialCount = m_pInitializer->getSerialPortCount();
83+
if( m_serialCount > WANDONGLE_MAX_SERIAL_PORTS )
84+
{
85+
m_serialCount = WANDONGLE_MAX_SERIAL_PORTS;
86+
}
87+
for(int j = 0; j < m_serialCount; j++)
88+
{
89+
DBG("Connecting serial port #%d", j+1);
90+
DBG("Ep %p", m_pInitializer->getEp(dev, j, false));
91+
DBG("Ep %p", m_pInitializer->getEp(dev, j, true));
92+
m_serial[j].connect( dev, m_pInitializer->getEp(dev, j, false), m_pInitializer->getEp(dev, j, true) );
93+
}
94+
95+
DBG("Device connected");
96+
97+
dev_connected = true;
98+
99+
100+
return true;
101+
}
102+
else if ((dev->getVid() == m_pInitializer->getMSDVid()) && (dev->getPid() == m_pInitializer->getMSDPid()))
103+
{
104+
DBG("Vodafone K3370 dongle detected in MSD mode");
105+
//Try to switch
106+
if( m_pInitializer->switchMode(dev) )
107+
{
108+
DBG("Switched OK");
109+
return false; //Will be connected on a next iteration
110+
}
111+
else
112+
{
113+
ERR("Could not switch mode");
114+
return false;
115+
}
116+
}
117+
} //if()
118+
} //if()
119+
} //for()
120+
return false;
121+
}
122+
123+
bool WANDongle::disconnect()
124+
{
125+
dev_connected = false;
126+
for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
127+
{
128+
m_serial[i].disconnect();
129+
}
130+
return true;
131+
}
132+
133+
WAN_DONGLE_TYPE WANDongle::getDongleType()
134+
{
135+
if( m_pInitializer != NULL )
136+
{
137+
return m_pInitializer->getType();
138+
}
139+
else
140+
{
141+
return WAN_DONGLE_TYPE_UNKNOWN;
142+
}
143+
}
144+
145+
IUSBHostSerial& WANDongle::getSerial(int index)
146+
{
147+
return m_serial[index];
148+
}
149+
150+
int WANDongle::getSerialCount()
151+
{
152+
return m_serialCount;
153+
}
154+
155+
//Private methods
156+
void WANDongle::init()
157+
{
158+
m_pInitializer = NULL;
159+
dev_connected = false;
160+
for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++)
161+
{
162+
m_serial[i].init(host);
163+
}
164+
}
165+
166+
167+
/*virtual*/ void WANDongle::setVidPid(uint16_t vid, uint16_t pid)
168+
{
169+
//Load right initializer
170+
WANDongleInitializer** initializer = WANDongleInitializer::getInitializers(host);
171+
172+
while(*initializer)
173+
{
174+
DBG("*initializer=%p", *initializer);
175+
DBG("(*initializer)->getSerialVid()=%04x", (*initializer)->getSerialVid());
176+
DBG("(*initializer)->getSerialPid()=%04x", (*initializer)->getSerialPid());
177+
if ((dev->getVid() == (*initializer)->getSerialVid()) && (dev->getPid() == (*initializer)->getSerialPid()))
178+
{
179+
DBG("The dongle is in virtual serial mode");
180+
m_pInitializer = *initializer;
181+
break;
182+
}
183+
else if ((dev->getVid() == (*initializer)->getMSDVid()) && (dev->getPid() == (*initializer)->getMSDPid()))
184+
{
185+
DBG("Vodafone K3370 dongle detected in MSD mode");
186+
m_pInitializer = *initializer;
187+
break;
188+
}
189+
initializer++;
190+
} //while()
191+
if(m_pInitializer)
192+
{
193+
m_pInitializer->setVidPid(vid, pid);
194+
}
195+
}
196+
197+
/*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
198+
{
199+
if(m_pInitializer)
200+
{
201+
return m_pInitializer->parseInterface(intf_nb, intf_class, intf_subclass, intf_protocol);
202+
}
203+
else
204+
{
205+
return false;
206+
}
207+
}
208+
209+
/*virtual*/ bool WANDongle::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
210+
{
211+
if(m_pInitializer)
212+
{
213+
return m_pInitializer->useEndpoint(intf_nb, type, dir);
214+
}
215+
else
216+
{
217+
return false;
218+
}
219+
}
220+
221+
#endif /* USBHOST_3GMODULE */

0 commit comments

Comments
 (0)