I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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