Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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