1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
|
/*===========================================================================
FILE:
GobiQDLCore.cpp
DESCRIPTION:
QUALCOMM Gobi QDL Based API Core
PUBLIC CLASSES AND FUNCTIONS:
cGobiQDLCore
Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Code Aurora Forum nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
==========================================================================*/
//---------------------------------------------------------------------------
// Include Files
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include "GobiQDLCore.h"
#include "QDLBuffers.h"
#include "ProtocolNotification.h"
#include "CoreUtilities.h"
//---------------------------------------------------------------------------
// Definitions
//---------------------------------------------------------------------------
// Default/minimum timeout for QCWWAN QMI requests
const ULONG DEFAULT_GOBI_QDL_TIMEOUT = 4000;
const ULONG MINIMUM_GOBI_QDL_TIMEOUT = 2000;
/*=========================================================================*/
// cGobiQDLCore Methods
/*=========================================================================*/
/*===========================================================================
METHOD:
cGobiQDLCore (Public Method)
DESCRIPTION:
Constructor
RETURN VALUE:
None
===========================================================================*/
cGobiQDLCore::cGobiQDLCore()
: mQDL( 512, 512 ),
mQDLPortNode( "" ),
mQDLTimeout( DEFAULT_GOBI_QDL_TIMEOUT )
{
// Nothing to do
}
/*===========================================================================
METHOD:
~cGobiQDLCore (Public Method)
DESCRIPTION:
Destructor
RETURN VALUE:
None
===========================================================================*/
cGobiQDLCore::~cGobiQDLCore()
{
// Nothing to do
}
/*===========================================================================
METHOD:
Initialize (Public Method)
DESCRIPTION:
Initialize the object
RETURN VALUE:
bool
===========================================================================*/
bool cGobiQDLCore::Initialize()
{
// Nothing to do
return true;
}
/*===========================================================================
METHOD:
Cleanup (Public Method)
DESCRIPTION:
Cleanup the object
RETURN VALUE:
bool
===========================================================================*/
bool cGobiQDLCore::Cleanup()
{
// Just in case
CloseQDLPort( false );
return true;
}
/*===========================================================================
METHOD:
GetAvailableQDLPorts (Public Method)
DESCRIPTION:
Return the set of available Gobi QDL ports
RETURN VALUE:
std::vector <sDeviceID>
===========================================================================*/
std::vector <std::string> cGobiQDLCore::GetAvailableQDLPorts()
{
std::vector <std::string> devices;
std::string path = "/sys/bus/usb/devices/";
std::vector <std::string> files;
DepthSearch( path,
2,
"ttyUSB",
files );
int fileNum = files.size();
for (int i = 0; i < fileNum; i++)
{
// Example "/sys/bus/usb/devices/8-1/8-1:1.1/ttyUSB0"
std::string nodePath = files[i];
int lastSlash = nodePath.find_last_of( "/" );
// This is what we want to return if everything else matches
std::string deviceNode = nodePath.substr( lastSlash + 1 );
// Move down one directory to the interface level
std::string curPath = nodePath.substr( 0, lastSlash );
// Read bInterfaceNumber
int handle = open( (curPath + "/bInterfaceNumber").c_str(),
O_RDONLY );
if (handle == -1)
{
continue;
}
char buff[4];
memset( buff, 0, 4 );
bool bFound = false;
int ret = read( handle, buff, 2 );
if (ret == 2)
{
// Interface 1 or 0
ret = strncmp( buff, "01", 2 );
if (ret == 0)
{
bFound = true;
}
ret = strncmp( buff, "00", 2 );
if (ret == 0)
{
bFound = true;
}
}
close( handle );
if (bFound == false)
{
continue;
}
// Move down one directory to the device level
curPath = curPath.substr( 0, curPath.find_last_of( "/" ) );
// Read idVendor
handle = open( (curPath + "/idVendor").c_str(), O_RDONLY );
if (handle == -1)
{
continue;
}
bFound = false;
ret = read( handle, buff, 4 );
if (ret == 4)
{
ret = strncmp( buff, "05c6", 4 );
if (ret == 0)
{
bFound = true;
}
}
close( handle );
if (bFound == false)
{
continue;
}
// Read idProduct
handle = open( (curPath + "/idProduct").c_str(), O_RDONLY );
if (handle == -1)
{
continue;
}
bFound = false;
ret = read( handle, buff, 4 );
if (ret == 4)
{
ret = strncmp( buff, "920c", 4 );
if (ret == 0)
{
bFound = true;
}
}
close( handle );
if (bFound == false)
{
continue;
}
// Success!
devices.push_back( deviceNode );
}
return devices;
}
/*===========================================================================
METHOD:
SetQDLTimeout (Public Method)
DESCRIPTION:
Set the timeout for all subsequent QDL transactions
PARAMETERS:
to [ I ] - Timeout value (in milliseconds)
RETURN VALUE:
eGobiError
===========================================================================*/
eGobiError cGobiQDLCore::SetQDLTimeout( ULONG to )
{
if (to < MINIMUM_GOBI_QDL_TIMEOUT)
{
return eGOBI_ERR_INVALID_ARG;
}
mQDLTimeout = to;
return eGOBI_ERR_NONE;
}
/*===========================================================================
METHOD:
OpenQDLPort (Public Method)
DESCRIPTION:
This function opens the specified QDL port of the device
PARAMETERS:
portID [ I ] - ID of QDL port to connect to
bBARMode [ I ] - Request boot and recovery mode feature
pMajorVersion [ O ] - Major version of the device boot downloader
pMinorVersion [ O ] - Minor version of the device boot downloader
RETURN VALUE:
eGobiError - Return code
===========================================================================*/
eGobiError cGobiQDLCore::OpenQDLPort(
std::string & portID,
ULONG bBARMode,
ULONG * pMajorVersion,
ULONG * pMinorVersion )
{
if (portID.empty() == true || pMajorVersion == 0 || pMinorVersion == 0)
{
return eGOBI_ERR_INVALID_ARG;
}
// First disconnect from current port (if any)
CloseQDLPort( false );
// Validate port ID
std::string foundDevice;
std::vector <std::string> availPorts = GetAvailableQDLPorts();
for (int index = 0; index < availPorts.size(); index++)
{
if (availPorts[index] == portID)
{
foundDevice = availPorts[index];
break;
}
}
if (foundDevice.empty() == true)
{
return eGOBI_ERR_INVALID_DEVID;
}
// Initialize server (we don't care about the return code
// since the following Connect() call will fail if we are
// unable to initialize the server)
mQDL.Initialize();
// Connect to the port
std::string deviceStr = "/dev/" + foundDevice;
bool bOK = mQDL.Connect( deviceStr.c_str() );
if (bOK == false)
{
return eGOBI_ERR_CONNECT;
}
// Store port ID (we are connected)
mQDLPortNode = foundDevice;
// Build the hello request
bool bBARFeature = bBARMode != 0;
sSharedBuffer * pHelloBuf = sQDLHello::BuildHelloReq( bBARFeature );
if (pHelloBuf == 0)
{
return eGOBI_ERR_MEMORY;
}
// Send the hello request and wait for the response
sProtocolBuffer rsp;
rsp = SendQDL( pHelloBuf );
if (rsp.IsValid() == false)
{
return GetCorrectedLastError();
}
// Extract major and minor boot downloader versions
ULONG majVer;
ULONG minVer;
sQDLHello helloRsp( rsp.GetSharedBuffer() );
if (helloRsp.GetBootVersionInfo( majVer, minVer ) == false)
{
sQDLError errRsp( rsp.GetSharedBuffer() );
if (errRsp.IsValid() == true)
{
eQDLError qdlErr = errRsp.GetErrorCode();
return GetCorrectedQDLError( qdlErr );
}
return eGOBI_ERR_MALFORMED_RSP;
}
// NOTE: in the current firmware implimentation, this cannot happen.
// No hello response will be received in case of feature mismatch.
if (bBARFeature == true)
{
const sQDLRawHelloRsp * pTmpRsp = helloRsp.GetResponse();
if (pTmpRsp == 0)
{
return eGOBI_ERR_MALFORMED_RSP;
}
if ( (pTmpRsp->mFeatures & QDL_FEATURE_BAR_MODE) == 0)
{
return eGOBI_ERR_QDL_BAR_MODE;
}
}
*pMajorVersion = majVer;
*pMinorVersion = minVer;
return eGOBI_ERR_NONE;
}
/*===========================================================================
METHOD:
CloseQDLPort (Public Method)
DESCRIPTION:
This function closes the currently open QDL port of the device
PARAMETERS:
bInformDevice [ I ] - Inform device that QDL port is being closed?
RETURN VALUE:
eGobiError - Return code
===========================================================================*/
eGobiError cGobiQDLCore::CloseQDLPort( bool bInformDevice )
{
// Assume success
eGobiError rc = eGOBI_ERR_NONE;
if (mQDLPortNode.empty() == true)
{
rc = eGOBI_ERR_NO_CONNECTION;
}
else if (bInformDevice == true)
{
BYTE cmd = (BYTE)eQDL_CMD_SESSION_CLOSE_REQ;
eProtocolType pt = ePROTOCOL_QDL_TX;
sSharedBuffer * pReq = 0;
pReq = new sSharedBuffer( (const BYTE *)&cmd, 1, pt );
if (pReq == 0)
{
rc = eGOBI_ERR_MEMORY;
}
else
{
sProtocolBuffer rsp = SendQDL( pReq, 0, 0, false );
rc = GetLastError();
}
}
mQDL.Disconnect();
mQDL.Exit();
mQDLPortNode.clear();
return rc;
}
/*===========================================================================
METHOD:
GetQDLImagesPreference (Public Method)
DESCRIPTION:
This function gets the current images preference as reported by the
device boot downloader
PARAMETERS:
pImageListSize [I/O] - Upon input the maximum number of elements that the
image info list can contain. Upon successful output
the actual number of elements in the image info list
pImageList [ O ] - The image info list
RETURN VALUE:
eGobiError - Return code
===========================================================================*/
eGobiError cGobiQDLCore::GetQDLImagesPreference(
ULONG * pImageListSize,
BYTE * pImageList )
{
if (pImageListSize == 0 || *pImageListSize == 0 || pImageList == 0)
{
return eGOBI_ERR_INVALID_ARG;
}
BYTE cmd = (BYTE)eQDL_CMD_GET_IMAGE_PREF_REQ;
eProtocolType pt = ePROTOCOL_QDL_TX;
sSharedBuffer * pReq = 0;
pReq = new sSharedBuffer( (const BYTE *)&cmd, 1, pt );
if (pReq == 0)
{
return eGOBI_ERR_MEMORY;
}
ULONG maxImages = (ULONG)*pImageListSize;
*pImageListSize = 0;
sProtocolBuffer rsp = SendQDL( pReq );
if (rsp.IsValid() == false)
{
return GetCorrectedLastError();
}
sQDLGetImagePref prefRsp( rsp.GetSharedBuffer() );
if (prefRsp.IsValid() == false)
{
sQDLError errRsp( rsp.GetSharedBuffer() );
if (errRsp.IsValid() == true)
{
eQDLError qdlErr = errRsp.GetErrorCode();
return GetCorrectedQDLError( qdlErr );
}
return eGOBI_ERR_MALFORMED_RSP;
}
std::list <sQDLRawImageID> imageIDs = prefRsp.GetImageIDs();
ULONG imageCount = (ULONG)imageIDs.size();
if (imageCount > maxImages)
{
imageCount = maxImages;
}
sQDLRawImageID * pOutList = (sQDLRawImageID *)pImageList;
std::list <sQDLRawImageID>::const_iterator pIter = imageIDs.begin();
for (ULONG i = 0; i < imageCount; i++)
{
*pOutList++ = *pIter++;
}
*pImageListSize = imageCount;
return eGOBI_ERR_NONE;
}
/*===========================================================================
METHOD:
PrepareQDLImageWrite (Public Method)
DESCRIPTION:
This function prepares the device boot downloader for an image write
PARAMETERS:
imageType [ I ] - Type of image being written
imageSize [ I ] - Size of image being written
pBlockSize [I/O] - Upon input the maximum size of image block supported
by host, upon successful output the maximum size of
image block supported by device
RETURN VALUE:
eGobiError - Return code
===========================================================================*/
eGobiError cGobiQDLCore::PrepareQDLImageWrite(
BYTE imageType,
ULONG imageSize,
ULONG * pBlockSize )
{
eQDLImageType it = (eQDLImageType)imageType;
if (::IsValid( it ) == false)
{
return eGOBI_ERR_INVALID_ARG;
}
if (pBlockSize == 0 || *pBlockSize == 0 || *pBlockSize > QDL_MAX_CHUNK_SIZE)
{
return eGOBI_ERR_INVALID_ARG;
}
sSharedBuffer * pReq = 0;
pReq = sQDLOpenUnframed::BuildOpenUnframedReq( it, imageSize, *pBlockSize );
if (pReq == 0)
{
return eGOBI_ERR_MEMORY;
}
sProtocolBuffer rsp = SendQDL( pReq );
if (rsp.IsValid() == false)
{
return GetCorrectedLastError();
}
ULONG tmp;
sQDLOpenUnframed openRsp( rsp.GetSharedBuffer() );
const sQDLRawOpenUnframedRsp * pTmp = openRsp.GetResponse();
if (pTmp == 0 || openRsp.GetChunkSize( tmp ) == false)
{
sQDLError errRsp( rsp.GetSharedBuffer() );
if (errRsp.IsValid() == true)
{
eQDLError qdlErr = errRsp.GetErrorCode();
return GetCorrectedQDLError( qdlErr );
}
return eGOBI_ERR_MALFORMED_RSP;
}
if (openRsp.IsSuccess() == false)
{
switch ((eQDLOpenStatus)pTmp->mStatus)
{
case eQDL_OPEN_STATUS_SIZE:
return eGOBI_ERR_QDL_OPEN_SIZE;
case eQDL_OPEN_STATUS_BAD_TYPE:
return eGOBI_ERR_QDL_OPEN_TYPE;
case eQDL_OPEN_STATUS_PROTECTION:
return eGOBI_ERR_QDL_OPEN_PROT;
case eQDL_OPEN_STATUS_NOT_NEEDED:
return eGOBI_ERR_QDL_OPEN_SKIP;
}
return eGOBI_ERR_QDL_ERR_GENERAL;
}
*pBlockSize = tmp;
return eGOBI_ERR_NONE;
}
/*===========================================================================
METHOD:
WriteQDLImageBlock (Public Method)
DESCRIPTION:
This function writes the specified image block to the device
PARAMETERS:
sequenceNumber [ I ] - Sequence number for image write
blockSize [ I ] - Size of image block
pImageBlock [ I ] - Image block
RETURN VALUE:
eGobiError - Return code
===========================================================================*/
eGobiError cGobiQDLCore::WriteQDLImageBlock(
USHORT sequenceNumber,
ULONG blockSize,
BYTE * pImageBlock )
{
if (blockSize > QDL_MAX_CHUNK_SIZE || pImageBlock == 0)
{
return eGOBI_ERR_INVALID_ARG;
}
sSharedBuffer * pReq = 0;
pReq = sQDLWriteUnframed::BuildWriteUnframedReq( sequenceNumber,
blockSize );
if (pReq == 0)
{
return eGOBI_ERR_MEMORY;
}
sProtocolBuffer rsp = SendQDL( pReq, pImageBlock, blockSize );
if (rsp.IsValid() == false)
{
return GetCorrectedLastError();
}
sQDLWriteUnframed writeRsp( rsp.GetSharedBuffer() );
const sQDLRawWriteUnframedRsp * pTmp = writeRsp.GetResponse();
if (pTmp == 0)
{
sQDLError errRsp( rsp.GetSharedBuffer() );
if (errRsp.IsValid() == true)
{
eQDLError qdlErr = errRsp.GetErrorCode();
return GetCorrectedQDLError( qdlErr );
}
return eGOBI_ERR_MALFORMED_RSP;
}
if (writeRsp.IsSuccess() == false)
{
switch ((eQDLWriteStatus)pTmp->mStatus)
{
case eQDL_WRITE_STATUS_CRC:
return eGOBI_ERR_QDL_CRC;
case eQDL_WRITE_STATUS_CONTENT:
return eGOBI_ERR_QDL_PARSING;
}
return eGOBI_ERR_QDL_ERR_GENERAL;
}
return eGOBI_ERR_NONE;
}
/*===========================================================================
METHOD:
ValidateQDLImages (Public Method)
DESCRIPTION:
This function requests the device validate the written images
PARAMETERS:
pImageType [ O ] - Upon failure this may contain the type of the image
that failed validation
RETURN VALUE:
eGobiError - Return code
===========================================================================*/
eGobiError cGobiQDLCore::ValidateQDLImages( BYTE * pImageType )
{
if (pImageType == 0)
{
return eGOBI_ERR_INVALID_ARG;
}
*pImageType = UCHAR_MAX;
BYTE cmd = (BYTE)eQDL_CMD_SESSION_DONE_REQ;
eProtocolType pt = ePROTOCOL_QDL_TX;
sSharedBuffer * pReq = 0;
pReq = new sSharedBuffer( (const BYTE *)&cmd, 1, pt );
if (pReq == 0)
{
return eGOBI_ERR_MEMORY;
}
sProtocolBuffer rsp = SendQDL( pReq );
if (rsp.IsValid() == false)
{
return GetCorrectedLastError();
}
sQDLDone doneRsp( rsp.GetSharedBuffer() );
const sQDLRawDoneRsp * pTmp = doneRsp.GetResponse();
if (pTmp == 0)
{
sQDLError errRsp( rsp.GetSharedBuffer() );
if (errRsp.IsValid() == true)
{
eQDLError qdlErr = errRsp.GetErrorCode();
return GetCorrectedQDLError( qdlErr );
}
return eGOBI_ERR_MALFORMED_RSP;
}
if (doneRsp.IsSuccess() == false)
{
*pImageType = pTmp->mImageType;
switch ((eQDLDoneStatus)pTmp->mStatus)
{
case eQDL_DONE_STATUS_AUTH:
return eGOBI_ERR_QDL_AUTH;
case eQDL_DONE_STATUS_WRITE:
return eGOBI_ERR_QDL_WRITE;
}
return eGOBI_ERR_QDL_ERR_GENERAL;
}
return eGOBI_ERR_NONE;
}
/*===========================================================================
METHOD:
SendQDL (Public Method)
DESCRIPTION:
Send a QDL request and wait for and return response (if needed)
PARAMETERS:
pRequest [ I ] - Request to schedule
pAuxData [ I ] - Auxiliary data for request
auxDataSz [ I ] - Size of auxiliary data
bWaitForResponse [ I ] - Wait for a response?
RETURN VALUE:
sProtocolBuffer - The response (invalid when no response was received)
===========================================================================*/
sProtocolBuffer cGobiQDLCore::SendQDL(
sSharedBuffer * pRequest,
const BYTE * pAuxData,
ULONG auxDataSz,
bool bWaitForResponse )
{
// Clear last error recorded
ClearLastError();
// Returned response
sProtocolBuffer rsp;
// Validate the arguments
if (pRequest == 0)
{
mLastError = eGOBI_ERR_MEMORY;
return rsp;
}
// We use the event based notification approach
cSyncQueue <sProtocolNotificationEvent> evts( 12, true );
cProtocolQueueNotification pn( &evts );
// Process up to the indicated timeout
cEvent & sigEvt = evts.GetSignalEvent();
// Build the request object
sProtocolRequest req( pRequest, 0, mQDLTimeout, 1, 1, &pn );
req.SetAuxiliaryData( pAuxData, auxDataSz );
if (bWaitForResponse == false)
{
req.SetTXOnly();
}
// Are we connected?
if ( (mQDLPortNode.empty() == true)
|| (mQDL.IsConnected() == false) )
{
mLastError = eGOBI_ERR_NO_CONNECTION;
return rsp;
}
// Grab the log from the server
const cProtocolLog & protocolLog = mQDL.GetLog();
// Schedule the request
ULONG reqID = mQDL.AddRequest( req );
if (reqID == INVALID_REQUEST_ID)
{
mLastError = eGOBI_ERR_REQ_SCHEDULE;
return rsp;
}
bool bReq = false;
bool bExit = false;
DWORD idx;
// Process up to the indicated timeout
while (bExit == false)
{
int wc = sigEvt.Wait( mQDLTimeout, idx );
if (wc == ETIME)
{
if (bReq == true)
{
mLastError = eGOBI_ERR_RESPONSE_TO;
}
else
{
mLastError = eGOBI_ERR_REQUEST_TO;
}
break;
}
else if (wc != 0)
{
mLastError = eGOBI_ERR_INTERNAL;
break;
}
sProtocolNotificationEvent evt;
bool bEvt = evts.GetElement( idx, evt );
if (bEvt == false)
{
mLastError = eGOBI_ERR_INTERNAL;
bExit = true;
break;
}
switch (evt.mEventType)
{
case ePROTOCOL_EVT_REQ_ERR:
mLastError = eGOBI_ERR_REQUEST;
bExit = true;
break;
case ePROTOCOL_EVT_RSP_ERR:
mLastError = eGOBI_ERR_RESPONSE;
bExit = true;
break;
case ePROTOCOL_EVT_REQ_SENT:
{
bReq = true;
if (bWaitForResponse == false)
{
// Success!
bExit = true;
}
}
break;
case ePROTOCOL_EVT_RSP_RECV:
// Success!
rsp = protocolLog.GetBuffer( evt.mParam2 );
bExit = true;
break;
}
}
if ( (mLastError == eGOBI_ERR_INTERNAL)
|| (mLastError == eGOBI_ERR_REQUEST_TO)
|| (mLastError == eGOBI_ERR_RESPONSE_TO) )
{
// Remove the request as our protocol notification object is
// about to go out of scope and hence be destroyed
mQDL.RemoveRequest( reqID );
}
return rsp;
}
/*===========================================================================
METHOD:
GetConnectedPortID (Public Method)
DESCRIPTION:
Get the device node of the currently connected Gobi device
PARAMETERS:
devNode [ O ] - Device node (IE: ttyUSB0)
RETURN VALUE:
bool
===========================================================================*/
bool cGobiQDLCore::GetConnectedPortID( std::string & devNode )
{
// Assume failure
bool bFound = false;
devNode.clear();
// Were we once connected?
if (mQDLPortNode.size() > 0)
{
// Yes, but is our device still present?
// NOTE: This does not garantee the device did not leave and come back
std::vector <std::string> devices = GetAvailableQDLPorts();
ULONG deviceCount = (ULONG)devices.size();
for (ULONG a = 0; a < deviceCount; a++)
{
if (devices[a] == mQDLPortNode)
{
devNode = devices[a];
bFound = true;
break;
}
}
if (bFound == false)
{
mLastError = eGOBI_ERR_NO_DEVICE;
}
}
else
{
// We are not connected
mLastError = eGOBI_ERR_NO_CONNECTION;
}
return bFound;
}
|