Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michielber 0:f615d151a72c 1
Michielber 0:f615d151a72c 2 /*
Michielber 0:f615d151a72c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
Michielber 0:f615d151a72c 4
Michielber 0:f615d151a72c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
Michielber 0:f615d151a72c 6 of this software and associated documentation files (the "Software"), to deal
Michielber 0:f615d151a72c 7 in the Software without restriction, including without limitation the rights
Michielber 0:f615d151a72c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michielber 0:f615d151a72c 9 copies of the Software, and to permit persons to whom the Software is
Michielber 0:f615d151a72c 10 furnished to do so, subject to the following conditions:
Michielber 0:f615d151a72c 11
Michielber 0:f615d151a72c 12 The above copyright notice and this permission notice shall be included in
Michielber 0:f615d151a72c 13 all copies or substantial portions of the Software.
Michielber 0:f615d151a72c 14
Michielber 0:f615d151a72c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michielber 0:f615d151a72c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michielber 0:f615d151a72c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michielber 0:f615d151a72c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Michielber 0:f615d151a72c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Michielber 0:f615d151a72c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michielber 0:f615d151a72c 21 THE SOFTWARE.
Michielber 0:f615d151a72c 22 */
Michielber 0:f615d151a72c 23
Michielber 0:f615d151a72c 24 #include "netCfg.h"
Michielber 0:f615d151a72c 25 #if NET_UMTS
Michielber 0:f615d151a72c 26
Michielber 0:f615d151a72c 27 #include "UMTSStick.h"
Michielber 0:f615d151a72c 28
Michielber 0:f615d151a72c 29 #define __DEBUG
Michielber 0:f615d151a72c 30 #include "dbg/dbg.h"
Michielber 0:f615d151a72c 31
Michielber 0:f615d151a72c 32 UMTSStick::UMTSStick() : m_host(), m_pDev(NULL)
Michielber 0:f615d151a72c 33 {
Michielber 0:f615d151a72c 34
Michielber 0:f615d151a72c 35 }
Michielber 0:f615d151a72c 36
Michielber 0:f615d151a72c 37 UMTSStick::~UMTSStick()
Michielber 0:f615d151a72c 38 {
Michielber 0:f615d151a72c 39
Michielber 0:f615d151a72c 40 }
Michielber 0:f615d151a72c 41
Michielber 0:f615d151a72c 42
Michielber 0:f615d151a72c 43 UMTSStickErr UMTSStick::getSerial(UsbSerial** ppUsbSerial)
Michielber 0:f615d151a72c 44 {
Michielber 0:f615d151a72c 45 m_host.init();
Michielber 0:f615d151a72c 46
Michielber 0:f615d151a72c 47 UMTSStickErr rc;
Michielber 0:f615d151a72c 48
Michielber 0:f615d151a72c 49 rc = waitForDevice();
Michielber 0:f615d151a72c 50 if(rc)
Michielber 0:f615d151a72c 51 return rc;
Michielber 0:f615d151a72c 52
Michielber 0:f615d151a72c 53 //Device is now enumerated, read table
Michielber 0:f615d151a72c 54
Michielber 0:f615d151a72c 55 uint16_t vid = m_pDev->getVid();
Michielber 0:f615d151a72c 56 uint16_t pid = m_pDev->getPid();
Michielber 0:f615d151a72c 57
Michielber 0:f615d151a72c 58 DBG("Configuration set: vid:%04x pid:%04x\n", vid, pid);
Michielber 0:f615d151a72c 59
Michielber 0:f615d151a72c 60 bool handled = false;
Michielber 0:f615d151a72c 61 bool cdfs = false;
Michielber 0:f615d151a72c 62 const UMTSSwitchingInfo* pInfo;
Michielber 0:f615d151a72c 63 for(int i = 0; i < UMTS_SWITCHING_COUNT; i++)
Michielber 0:f615d151a72c 64 {
Michielber 0:f615d151a72c 65 pInfo = &UMTSwitchingTable[i];
Michielber 0:f615d151a72c 66 if( !checkDeviceState(pInfo, &cdfs) )
Michielber 0:f615d151a72c 67 {
Michielber 0:f615d151a72c 68 handled = true;
Michielber 0:f615d151a72c 69 break;
Michielber 0:f615d151a72c 70 }
Michielber 0:f615d151a72c 71
Michielber 0:f615d151a72c 72 } //for(int i = 0; i < UMTS_SWITCHING_COUNT; i++)
Michielber 0:f615d151a72c 73
Michielber 0:f615d151a72c 74 if(!handled)
Michielber 0:f615d151a72c 75 {
Michielber 0:f615d151a72c 76 DBG("Don't know this device!\n");
Michielber 0:f615d151a72c 77 return UMTSERR_NOTIMPLEMENTED;
Michielber 0:f615d151a72c 78 }
Michielber 0:f615d151a72c 79
Michielber 0:f615d151a72c 80 //Check if the device is in CDFS mode, in this case switch
Michielber 0:f615d151a72c 81 if(cdfs)
Michielber 0:f615d151a72c 82 {
Michielber 0:f615d151a72c 83 DBG("Switching the device by sending a magic packet\n");
Michielber 0:f615d151a72c 84
Michielber 0:f615d151a72c 85 rc = switchMode(pInfo);
Michielber 0:f615d151a72c 86 if(rc)
Michielber 0:f615d151a72c 87 return rc;
Michielber 0:f615d151a72c 88
Michielber 0:f615d151a72c 89 DBG("Now wait for device to reconnect\n");
Michielber 0:f615d151a72c 90
Michielber 0:f615d151a72c 91 m_host.releaseDevice(m_pDev);
Michielber 0:f615d151a72c 92
Michielber 0:f615d151a72c 93 //Wait for device to reconnect
Michielber 0:f615d151a72c 94 wait(3);
Michielber 0:f615d151a72c 95 rc = waitForDevice();
Michielber 0:f615d151a72c 96 if(rc)
Michielber 0:f615d151a72c 97 return rc;
Michielber 0:f615d151a72c 98 }
Michielber 0:f615d151a72c 99
Michielber 0:f615d151a72c 100 rc = findSerial(ppUsbSerial);
Michielber 0:f615d151a72c 101 if(rc)
Michielber 0:f615d151a72c 102 return rc;
Michielber 0:f615d151a72c 103
Michielber 0:f615d151a72c 104 return UMTSERR_OK;
Michielber 0:f615d151a72c 105 }
Michielber 0:f615d151a72c 106
Michielber 0:f615d151a72c 107 UMTSStickErr UMTSStick::waitForDevice()
Michielber 0:f615d151a72c 108 {
Michielber 0:f615d151a72c 109 bool ready = false;
Michielber 0:f615d151a72c 110 while(!ready)
Michielber 0:f615d151a72c 111 {
Michielber 0:f615d151a72c 112 while(!m_host.devicesCount())
Michielber 0:f615d151a72c 113 {}
Michielber 0:f615d151a72c 114 wait(1);
Michielber 0:f615d151a72c 115 if(m_host.devicesCount())
Michielber 0:f615d151a72c 116 ready = true;
Michielber 0:f615d151a72c 117 }
Michielber 0:f615d151a72c 118
Michielber 0:f615d151a72c 119 wait(2); //Wait for device to be initialized
Michielber 0:f615d151a72c 120
Michielber 0:f615d151a72c 121 if(!m_host.devicesCount())
Michielber 0:f615d151a72c 122 return UMTSERR_DISCONNECTED;
Michielber 0:f615d151a72c 123
Michielber 0:f615d151a72c 124 m_pDev = m_host.getDevice(0);
Michielber 0:f615d151a72c 125
Michielber 0:f615d151a72c 126 while(!m_pDev->enumerated())
Michielber 0:f615d151a72c 127 {
Michielber 0:f615d151a72c 128 m_host.poll();
Michielber 0:f615d151a72c 129 if(!m_host.devicesCount())
Michielber 0:f615d151a72c 130 return UMTSERR_DISCONNECTED;
Michielber 0:f615d151a72c 131 }
Michielber 0:f615d151a72c 132
Michielber 0:f615d151a72c 133 return UMTSERR_OK;
Michielber 0:f615d151a72c 134 }
Michielber 0:f615d151a72c 135
Michielber 0:f615d151a72c 136 UMTSStickErr UMTSStick::checkDeviceState(const UMTSSwitchingInfo* pInfo, bool* pCdfs)
Michielber 0:f615d151a72c 137 {
Michielber 0:f615d151a72c 138 uint16_t vid = m_pDev->getVid();
Michielber 0:f615d151a72c 139 uint16_t pid = m_pDev->getPid();
Michielber 0:f615d151a72c 140 bool handled = false;
Michielber 0:f615d151a72c 141 if( (vid == pInfo->cdfsVid) && (pid == pInfo->cdfsPid) )
Michielber 0:f615d151a72c 142 {
Michielber 0:f615d151a72c 143 DBG("Match on dongles list\n");
Michielber 0:f615d151a72c 144 if( !pInfo->targetClass ) //No specific interface to check, vid/pid couple is specific to CDFS mode
Michielber 0:f615d151a72c 145 {
Michielber 0:f615d151a72c 146 DBG("Found device in CDFS mode\n");
Michielber 0:f615d151a72c 147 handled = true;
Michielber 0:f615d151a72c 148 *pCdfs = true;
Michielber 0:f615d151a72c 149 }
Michielber 0:f615d151a72c 150 else //if( !pInfo->targetClass )
Michielber 0:f615d151a72c 151 {
Michielber 0:f615d151a72c 152 //Has to check if there is an interface of class targetClass
Michielber 0:f615d151a72c 153 byte* desc = NULL;
Michielber 0:f615d151a72c 154 int c = 0;
Michielber 0:f615d151a72c 155
Michielber 0:f615d151a72c 156 while( !m_pDev->getInterfaceDescriptor(1, c++, &desc) )
Michielber 0:f615d151a72c 157 {
Michielber 0:f615d151a72c 158 if( desc[5] == pInfo->targetClass )
Michielber 0:f615d151a72c 159 {
Michielber 0:f615d151a72c 160 DBG("Found device in Serial mode\n");
Michielber 0:f615d151a72c 161 handled = true;
Michielber 0:f615d151a72c 162 *pCdfs = false;
Michielber 0:f615d151a72c 163 break;
Michielber 0:f615d151a72c 164 }
Michielber 0:f615d151a72c 165 }
Michielber 0:f615d151a72c 166
Michielber 0:f615d151a72c 167 if(!handled)
Michielber 0:f615d151a72c 168 {
Michielber 0:f615d151a72c 169 //All interfaces were tried, so we are in CDFS mode
Michielber 0:f615d151a72c 170 DBG("Found device in CDFS mode\n");
Michielber 0:f615d151a72c 171 handled = true;
Michielber 0:f615d151a72c 172 *pCdfs = true;
Michielber 0:f615d151a72c 173 }
Michielber 0:f615d151a72c 174 } //if( !pInfo->targetClass )
Michielber 0:f615d151a72c 175 } //if( (vid == pInfo->cdfsVid) && (pid == pInfo->cdfsPid) )
Michielber 0:f615d151a72c 176 else
Michielber 0:f615d151a72c 177 {
Michielber 0:f615d151a72c 178 //Try every vid/pid couple of the serial list
Michielber 0:f615d151a72c 179 for( int i = 0; i < 16 ; i++)
Michielber 0:f615d151a72c 180 {
Michielber 0:f615d151a72c 181 if(!pInfo->serialPidList[i])
Michielber 0:f615d151a72c 182 break;
Michielber 0:f615d151a72c 183 if( (pInfo->serialVid == vid) && (pInfo->serialPidList[i] == pid) )
Michielber 0:f615d151a72c 184 {
Michielber 0:f615d151a72c 185 DBG("Found device in Serial mode\n");
Michielber 0:f615d151a72c 186 handled = true;
Michielber 0:f615d151a72c 187 *pCdfs = false;
Michielber 0:f615d151a72c 188 break;
Michielber 0:f615d151a72c 189 }
Michielber 0:f615d151a72c 190 }
Michielber 0:f615d151a72c 191 } //if( (vid == pInfo->cdfsVid) && (pid == pInfo->cdfsPid) )
Michielber 0:f615d151a72c 192
Michielber 0:f615d151a72c 193 if(!handled)
Michielber 0:f615d151a72c 194 return UMTSERR_NOTFOUND;
Michielber 0:f615d151a72c 195
Michielber 0:f615d151a72c 196 return UMTSERR_OK;
Michielber 0:f615d151a72c 197 }
Michielber 0:f615d151a72c 198
Michielber 0:f615d151a72c 199 UMTSStickErr UMTSStick::switchMode(const UMTSSwitchingInfo* pInfo)
Michielber 0:f615d151a72c 200 {
Michielber 0:f615d151a72c 201 if(!pInfo->huaweiPacket) //Send SCSI packet on first bulk ep
Michielber 0:f615d151a72c 202 {
Michielber 0:f615d151a72c 203 //Find first bulk ep
Michielber 0:f615d151a72c 204 byte* desc = NULL;
Michielber 0:f615d151a72c 205 int c = 0;
Michielber 0:f615d151a72c 206
Michielber 0:f615d151a72c 207 UsbEndpoint *pEpOut = NULL;
Michielber 0:f615d151a72c 208
Michielber 0:f615d151a72c 209 while( !m_pDev->getInterfaceDescriptor(1, c++, &desc) )
Michielber 0:f615d151a72c 210 {
Michielber 0:f615d151a72c 211 byte* p = desc;
Michielber 0:f615d151a72c 212 int epNum = 0;
Michielber 0:f615d151a72c 213 p = p + p[0]; //Move to next descriptor (which should be an ep descriptor)
Michielber 0:f615d151a72c 214 while (epNum < desc[4]) //Eps count in this if
Michielber 0:f615d151a72c 215 {
Michielber 0:f615d151a72c 216 if (p[1] != USB_DESCRIPTOR_TYPE_ENDPOINT)
Michielber 0:f615d151a72c 217 break;
Michielber 0:f615d151a72c 218
Michielber 0:f615d151a72c 219 if( (p[3] == 0x02) && !(p[2] & 0x80) ) //Bulk endpoint, out
Michielber 0:f615d151a72c 220 {
Michielber 0:f615d151a72c 221 DBG("Found bulk ep %02x\n", p[2]);
Michielber 0:f615d151a72c 222 pEpOut = new UsbEndpoint( m_pDev, p[2], false, USB_BULK, *((uint16_t*)&p[4]) );
Michielber 0:f615d151a72c 223 break;
Michielber 0:f615d151a72c 224 }
Michielber 0:f615d151a72c 225
Michielber 0:f615d151a72c 226 p = p + p[0]; //Move to next ep desc
Michielber 0:f615d151a72c 227 epNum++;
Michielber 0:f615d151a72c 228 }
Michielber 0:f615d151a72c 229 if(pEpOut)
Michielber 0:f615d151a72c 230 break;
Michielber 0:f615d151a72c 231 }
Michielber 0:f615d151a72c 232
Michielber 0:f615d151a72c 233 if(!pEpOut)
Michielber 0:f615d151a72c 234 return UMTSERR_NOTFOUND;
Michielber 0:f615d151a72c 235
Michielber 0:f615d151a72c 236 //Send SCSI packet
Michielber 0:f615d151a72c 237
Michielber 0:f615d151a72c 238 DBG("Sending SCSI Packet to switch\n");
Michielber 0:f615d151a72c 239 byte ramCdfsBuf[31];
Michielber 0:f615d151a72c 240 memcpy(ramCdfsBuf, pInfo->cdfsPacket, 31);
Michielber 0:f615d151a72c 241 pEpOut->transfer((volatile byte*)ramCdfsBuf, 31);
Michielber 0:f615d151a72c 242 while(pEpOut->status() == USBERR_PROCESSING);
Michielber 0:f615d151a72c 243 int ret = pEpOut->status();
Michielber 0:f615d151a72c 244 if((ret < 0) && (ret !=USBERR_DISCONNECTED)) //Packet was not transfered
Michielber 0:f615d151a72c 245 {
Michielber 0:f615d151a72c 246 DBG("Usb error %d\n", ret);
Michielber 0:f615d151a72c 247 delete pEpOut;
Michielber 0:f615d151a72c 248 return UMTSERR_USBERR;
Michielber 0:f615d151a72c 249 }
Michielber 0:f615d151a72c 250
Michielber 0:f615d151a72c 251 delete pEpOut;
Michielber 0:f615d151a72c 252 }
Michielber 0:f615d151a72c 253 else
Michielber 0:f615d151a72c 254 {
Michielber 0:f615d151a72c 255 UsbErr usbErr;
Michielber 0:f615d151a72c 256 //Send the Huawei-specific control packet
Michielber 0:f615d151a72c 257 usbErr = m_pDev->controlSend(0, 0x03, 1, 0, NULL, 0);
Michielber 0:f615d151a72c 258 if(usbErr && (usbErr != USBERR_DISCONNECTED))
Michielber 0:f615d151a72c 259 return UMTSERR_USBERR;
Michielber 0:f615d151a72c 260 }
Michielber 0:f615d151a72c 261
Michielber 0:f615d151a72c 262 DBG("The stick should be switching in serial mode now\n");
Michielber 0:f615d151a72c 263
Michielber 0:f615d151a72c 264 return UMTSERR_OK;
Michielber 0:f615d151a72c 265 }
Michielber 0:f615d151a72c 266
Michielber 0:f615d151a72c 267 UMTSStickErr UMTSStick::findSerial(UsbSerial** ppUsbSerial)
Michielber 0:f615d151a72c 268 {
Michielber 0:f615d151a72c 269 byte* desc = NULL;
Michielber 0:f615d151a72c 270 int c = 0;
Michielber 0:f615d151a72c 271
Michielber 0:f615d151a72c 272 int epOut = 0;
Michielber 0:f615d151a72c 273 int epIn = 0;
Michielber 0:f615d151a72c 274
Michielber 0:f615d151a72c 275 while( !m_pDev->getInterfaceDescriptor(1, c++, &desc) )
Michielber 0:f615d151a72c 276 {
Michielber 0:f615d151a72c 277 byte* p = desc;
Michielber 0:f615d151a72c 278 int epNum = 0;
Michielber 0:f615d151a72c 279
Michielber 0:f615d151a72c 280 DBG("Interface of type %02x\n", desc[5]);
Michielber 0:f615d151a72c 281
Michielber 0:f615d151a72c 282 if(desc[5] != 0xFF) //Not a serial-like if
Michielber 0:f615d151a72c 283 continue;
Michielber 0:f615d151a72c 284
Michielber 0:f615d151a72c 285 p = p + p[0]; //Move to next descriptor (which should be an ep descriptor)
Michielber 0:f615d151a72c 286 while (epNum < desc[4]) //Eps count in this if
Michielber 0:f615d151a72c 287 {
Michielber 0:f615d151a72c 288 if (p[1] == USB_DESCRIPTOR_TYPE_ENDPOINT)
Michielber 0:f615d151a72c 289 {
Michielber 0:f615d151a72c 290 if( (p[3] == 0x02) && !(p[2] & 0x80) && !epOut ) //Bulk endpoint, out
Michielber 0:f615d151a72c 291 {
Michielber 0:f615d151a72c 292 DBG("Found bulk out ep %02x of payload size %04x\n", p[2], *((uint16_t*)&p[4]));
Michielber 0:f615d151a72c 293 epOut = p[2] & 0x7F;
Michielber 0:f615d151a72c 294 }
Michielber 0:f615d151a72c 295
Michielber 0:f615d151a72c 296 if( (p[3] == 0x02) && (p[2] & 0x80) && !epIn ) //Bulk endpoint, in
Michielber 0:f615d151a72c 297 {
Michielber 0:f615d151a72c 298 DBG("Found bulk in ep %02x of payload size %04x\n", p[2], *((uint16_t*)&p[4]));
Michielber 0:f615d151a72c 299 epIn = p[2] & 0x7F;
Michielber 0:f615d151a72c 300 }
Michielber 0:f615d151a72c 301
Michielber 0:f615d151a72c 302 if(epOut && epIn)
Michielber 0:f615d151a72c 303 break;
Michielber 0:f615d151a72c 304 }
Michielber 0:f615d151a72c 305
Michielber 0:f615d151a72c 306 p = p + p[0]; //Move to next ep desc
Michielber 0:f615d151a72c 307 epNum++;
Michielber 0:f615d151a72c 308 }
Michielber 0:f615d151a72c 309
Michielber 0:f615d151a72c 310 if(epOut && epIn)
Michielber 0:f615d151a72c 311 break;
Michielber 0:f615d151a72c 312 }
Michielber 0:f615d151a72c 313
Michielber 0:f615d151a72c 314 if(!epOut || !epIn)
Michielber 0:f615d151a72c 315 return UMTSERR_NOTFOUND;
Michielber 0:f615d151a72c 316
Michielber 0:f615d151a72c 317 DBG("Endpoints found, create serial object\n");
Michielber 0:f615d151a72c 318
Michielber 0:f615d151a72c 319 *ppUsbSerial = new UsbSerial(m_pDev, epIn, epOut);
Michielber 0:f615d151a72c 320
Michielber 0:f615d151a72c 321 DBG("UsbSerial object created\n");
Michielber 0:f615d151a72c 322
Michielber 0:f615d151a72c 323 return UMTSERR_OK;
Michielber 0:f615d151a72c 324 }
Michielber 0:f615d151a72c 325
Michielber 0:f615d151a72c 326 #endif