TRANSFER.C
29.6 KB
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
/*-----------------------------------------------------------------------------
This is a part of the Microsoft Source Code Samples.
Copyright (C) 1995 Microsoft Corporation.
All rights reserved.
This source code is only intended as a supplement to
Microsoft Development Tools and/or WinHelp documentation.
See these sources for detailed information regarding the
Microsoft samples programs.
MODULE: Transfer.c
PURPOSE: Transfer a file (receive or send).
FUNCTIONS:
TransferRepeatCreate - Preps program for a repeated send
TransferRepeatDestroy - Completes a repeated send
TransferRepeatDo - Sends the data to the writer thread
TransferFileTextStart - Preps program for a text file send
TransferFileTextEnd - Completes a file transfer
TransferThreadProc - Thread procedure to do actual transfer
TransferFileText - Preps program for a text file send
ReceiveFileText - Preps program for a text file capture
OpenTheFile - Opens a file
CreateTheFile - Creates a file
GetTransferSizes - Determines transfer metrics from file and buffer sizes
ShowTransferStatistics - Displays transfer stats
CheckForMessges - Peek message check to keep things flowing
during a transfer
SendFile - Send a file
CaptureFile - Sets the receive state for file capture
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <commctrl.h>
#include "mttty.h"
#pragma comment(lib,"ws2_32.lib")
//
// Globals used in this file only
//
//HANDLE hFile;
HANDLE hTransferAbortEvent;
HANDLE hTransferThread;
UINT uTimerId;
MMRESULT mmTimer = (MMRESULT)NULL;
char * lpBuf;
//
// Prototypes for functions called only within this file
//
DWORD WINAPI TransferThreadProc(LPVOID);
HANDLE OpenTheFile( LPCTSTR );
HANDLE CreateTheFile( LPCTSTR );
//void CaptureFile( HANDLE, HWND );
UINT CheckForMessages( void );
BOOL GetTransferSizes( HANDLE, DWORD *, DWORD *);
DWORD upgrade_rf_test_buf = 0x703F0000;
DWORD upgrade_version_buf = 0x705f0000;
DWORD upgrade_rf_test_buf_len = 0;
HANDLE*upgrade_fp;
//char uart_upgrade_rf_test_bin[]?=?{'B','l','u','e','S','e','a','_','u','a','r','t','_','u','p','g','r','a','d','e','.','b','i','n',0x0};
/*-----------------------------------------------------------------------------
FUNCTION: TransferRepeatCreate(LPCTSTR, DWORD)
PURPOSE: 重复的文件传输(发送)准备
PARAMETERS:
lpstrFileName - name of file selected to send
dwFrequency - frequency of timer
COMMENTS:
This function sets up a window timer to fire off every so often. When it fires, TransferRepeatDo is called with the same name as above. This causes the file transfer to actuall take place.
TransferRepeatDestroy is called to kill the timer. This function disables certain menu items that should not be available for the duration of a repeated send even if the actual Tx is not taking place.
HISTORY: Date: Author: Comment:
1/29/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
//void TransferRepeatCreate(LPCTSTR lpszFileName, DWORD dwFrequency)
//{
// HMENU hMenu;
// UINT MenuFlags;
// DWORD dwFileSize;
// DWORD dwMaxPackets;
// DWORD dwPacketSize;
// DWORD dwRead;
//
// // open the file
// hFile = OpenTheFile(lpszFileName);
// if (hFile == INVALID_HANDLE_VALUE)
// return;
// // modify transfer menu
// hMenu = GetMenu(ghwndMain);
// MenuFlags = MF_DISABLED | MF_GRAYED;
// EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
// // enable abort button and progress bar
// SetWindowText(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), "Abort Tx");
// EnableWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), TRUE);
// if (!GetTransferSizes(hFile, &dwPacketSize, &dwMaxPackets, &dwFileSize))
// {
// TransferRepeatDestroy();
// return;
// }
// lpBuf = HeapAlloc(ghWriterHeap, 0, dwFileSize);
// if (lpBuf == NULL)
// {
// ErrorReporter("HeapAlloc (data block from writer heap).\r\nFile is too large");
// TransferRepeatDestroy();
// return;
// }
// if (!ReadFile(hFile, lpBuf, dwFileSize, &dwRead, NULL))
// {
// ErrorReporter("Can't read from file\n");
// TransferRepeatDestroy();
// }
// if (dwRead != dwFileSize)
// ErrorReporter("Didn't read entire file\n");
// mmTimer = timeSetEvent((UINT) dwFrequency, 10, TransferRepeatDo, dwRead, TIME_PERIODIC);
// if (mmTimer == (MMRESULT) NULL)
// {
// ErrorReporter("Could not create mm timer");
// TransferRepeatDestroy();
// }
// else
// {
// g_appdata.fRepeating = TRUE;
// OutputDebugString("Timer setup.\n");
// }
// return;
//}
/*-----------------------------------------------------------------------------
FUNCTION: TransferRepeatDestroy( void )
PURPOSE: Stops a repeated text file transfer (send)
COMMENTS: Kills the repeated-send timer.
HISTORY: Date: Author: Comment:
1/29/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
//void TransferRepeatDestroy()
//{
// HMENU hMenu;
// DWORD MenuFlags;
// MMRESULT mmRes;
//
// if (mmTimer != (MMRESULT) NULL) {
// mmRes = timeKillEvent(mmTimer);
// if (mmRes != TIMERR_NOERROR)
// ErrorReporter("Can't kill mm timer");
// mmTimer = (MMRESULT) NULL;
// }
//
// // close the file
// CloseHandle(hFile);
//
// // inform writer to abort all pending write requests
// if (!WriterAddFirstNodeTimeout(WRITE_ABORT, 0, 0, NULL, NULL, NULL, 500))
// ErrorReporter("Couldn't inform writer to abort sending.");
//
// // free the buffer
// if (!HeapFree(ghWriterHeap, 0, lpBuf))
// ErrorReporter("HeapFree (data block from writer heap)");
//
// g_appdata.fRepeating = FALSE;
// OutputDebugString("Repeated transfer destroyed.\r\n");
//
// //
// // enable transfer menu
// //
// hMenu = GetMenu(ghwndMain);
// MenuFlags = MF_ENABLED;
// EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
// EnableWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), FALSE);
//
// return;
//}
/*-----------------------------------------------------------------------------
FUNCTION: TransferRepeatDo( void )
PURPOSE: Performs a single text file transfer (send)
COMMENTS: Allocates a block to hold the file.
Prepares the writer packet.
HISTORY: Date: Author: Comment:
1/29/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
//void CALLBACK TransferRepeatDo( UINT uTimerId, UINT uRes, DWORD dwFileSize, DWORD dwRes1, DWORD dwRes2)
//{
// if (!WriterAddNewNodeTimeout(WRITE_BLOCK, dwFileSize, 0, lpBuf, 0, 0, 10))
// PostMessage(ghWndStatusDlg, WM_COMMAND, IDC_ABORTBTN,0);
// return;
//}
/*-----------------------------------------------------------------------------
FUNCTION: TransferFileTextStart(LPCTSTR)
PURPOSE: Prepares program for a text file transfer (send)
PARAMETERS:
lpstrFileName - name of file selected to send
COMMENTS: Modifies menus and dialog control, then restores them
HISTORY: Date: Author: Comment:
1/26/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void TransferFileTextStart(LPCTSTR lpstrFileName)
{
DWORD dwThreadId;
HMENU hMenu;
UINT MenuFlags ;
//
// modify transfer menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_DISABLED | MF_GRAYED;
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
//
// enable abort button and progress bar
//
gfAbortTransfer = FALSE;
SetWindowText(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), "停止下载");
EnableWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), TRUE);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_SHOW);
// start the transfer thread
hTransferAbortEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hTransferAbortEvent == NULL)
ErrorReporter("CreateEvent(Transfer Abort Event)");
hTransferThread = CreateThread(NULL, 0,
TransferThreadProc,
(LPVOID) g_appdata.hFileOfEarphoneBin, 0, &dwThreadId);
if (hTransferThread == NULL) {
ErrorReporter("CreateThread (Transfer Thread)");
TransferFileTextEnd();
}
else
g_appdata.fTransferring = TRUE;
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferFileTextEnd()
PURPOSE: Stops a text file transfer (send)
COMMENTS: Modifies menus and dialog control, then restores them
HISTORY: Date: Author: Comment:
1/26/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void TransferFileTextEnd()
{
HMENU hMenu;
UINT MenuFlags ;
// stop the transfer thread
SetEvent(hTransferAbortEvent);
OutputDebugString("Waiting for transfer thread...\n");
if (WaitForSingleObject(hTransferThread, 3000) != WAIT_OBJECT_0) {
ErrorReporter("TransferThread didn't stop.");
TerminateThread(hTransferThread, 0);
}
else
OutputDebugString("Transfer thread exited\n");
CloseHandle(hTransferAbortEvent);
CloseHandle(hTransferThread);
g_appdata.fTransferring = FALSE;
//
// enable transfer menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_ENABLED;
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
//
// disable abort button and progress bar
//
EnableWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), FALSE);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_HIDE);
//
// close the file
//
CloseHandle(g_appdata.hFileOfEarphoneBin);
}
/*-----------------------------------------------------------------------------
FUNCTION: ReceiveFileText(LPCTSTR)
PURPOSE: Prepares program for a text file transfer (receive)
PARAMETERS:
lpstrFileName - name of file selected for receiving
COMMENTS: Modifies menus and control, then restores them
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void ReceiveFileText(LPCTSTR lpstrFileName)
{
HMENU hMenu;
UINT MenuFlags ;
//
// create the file
//
ghFileCapture = CreateTheFile(lpstrFileName);
if (ghFileCapture == INVALID_HANDLE_VALUE)
return;
/*
setup transfer
disable file menu
*/
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_DISABLED | MF_GRAYED;
//
// disable transfer menu
//
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
//
// enable abort button and progress bar
//
gfAbortTransfer = FALSE;
SetWindowText(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), "Close Capture");
EnableWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), TRUE);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_SHOW);
//
// send file until done or abort
//
// CaptureFile(ghFileCapture, GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS));
//
// enable menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_ENABLED;
ChangeConnection(ghwndMain, g_appdata.fConnected);
//
// enable transfer menu
//
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
//
// hide abort button and progress bar
//
EnableWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), FALSE);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_HIDE);
gfAbortTransfer = FALSE;
CloseHandle(ghFileCapture);
return; // returns when file transfer is complete or aborted
}
/*-----------------------------------------------------------------------------
FUNCTION: OpenTheFile(LPCTSTR)
PURPOSE: Open a file and return the file handle
PARAMETERS:
lpFName - name of file to open
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
HANDLE OpenTheFile(LPCTSTR lpFName)
{
HANDLE hTemp;
hTemp = CreateFile(lpFName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,NULL);
if (hTemp == INVALID_HANDLE_VALUE)
ErrorReporter("CreateFile");
return hTemp;
}
/*-----------------------------------------------------------------------------
FUNCTION: CreateTheFile(LPCTSTR)
PURPOSE: Creates a file and returns the file handle
PARAMETERS:
lpFName - name of file to create
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
HANDLE CreateTheFile(LPCTSTR lpFName)
{
HANDLE hTemp;
hTemp = CreateFile(lpFName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0,NULL);
if (hTemp == INVALID_HANDLE_VALUE)
ErrorReporter("CreateFile");
return hTemp;
}
/*-----------------------------------------------------------------------------
FUNCTION: GetTransferSizes(HANDLE, DWORD *, DWORD *, DWORD *)
PURPOSE: Examines file and determines packet size, number of packets,
and file size.
PARAMETERS:
hFile - handle of file to get size information from
pdwDataPacketSize - size of an individual data packet
pdwNumPackets - total number of packets
pdwFileSize - size of file
RETURN:
TRUE - all metrics could be determined
FALSE - something wrong with the file metrics, can't transfer
COMMENTS:
This module can't handle files that are extremely large, so
it may return FALSE if a large file is specified.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
BOOL GetTransferSizes(HANDLE hFile)
{
BY_HANDLE_FILE_INFORMATION fi;
if (!GetFileInformationByHandle(hFile, &fi)) {
ErrorReporter("GetFileInformationByHandle");
return FALSE;
}
else {
if (fi.nFileSizeHigh) {
MessageBox(ghwndMain, "File is too large to transfer.", "File Transfer Error", MB_OK);
return FALSE;
}
//
// setup packet size, file size and compute the number of packets
g_appdata.BinSizeFileEarphone = fi.nFileSizeLow;
g_appdata.MaxPackets = g_appdata.BinSizeFileEarphone / MAX_WRITE_BUFFER;
if (g_appdata.MaxPackets > 65534) {
MessageBox(ghwndMain, "File is too large for buffer size.", "File Transfer Error", MB_OK);
return FALSE;
}
}
return TRUE;
}
/*-----------------------------------------------------------------------------
FUNCTION: ShowTransferStatistics(DWORD, DWORD, DWORD)
PURPOSE: Displays bytes transferred and bytes per second
PARAMETERS:
dwEnd - ending time in milliseconds
dwStart - starting time
dwBytesTransferred - bytes sent
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void ShowTransferStatistics(DWORD dwEnd, DWORD dwStart, DWORD dwBytesTransferred)
{
char szTemp[100];
DWORD dwSecs;
dwSecs = (dwEnd - dwStart) / 1000;
//
// display only if dwSecs != 0; if dwSecs == 0, then divide by zero occurs.
//
if (dwSecs != 0) {
wsprintf(szTemp, "Bytes transferred: %d\r\nBytes/Second: %d\r\n", dwBytesTransferred, dwBytesTransferred / dwSecs);
UpdateStatus(szTemp);
}
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: CheckForMessages
PURPOSE: Check for a message and dispatch it.
RETURN:
If the WM_CLOSE message or the WM_SYSCOMMAND (SC_CLOSE) is
retrieved, WM_CLOSE is posted, and WM_CLOSEQUIT is
returned by the function. This allows the caller
to detect this as an abort condition and exit properly. When
the caller exits, the main message loop in the WinMain function
should be entered again and the WM_CLOSE message will be
handled properly.
If there is a message other than those above, it is dispatched
and TRUE is returned indicating that a message was dispatched.
If no message is found, FALSE is returned.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
UINT CheckForMessages()
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_CLOSE) {
PostMessage(ghwndMain, WM_CLOSE, 0, 0);
return WM_CLOSE;
}
if (msg.message == WM_SYSCOMMAND && msg.wParam == SC_CLOSE) {
PostMessage(ghwndMain, WM_CLOSE, 0, 0);
return WM_CLOSE;
}
if (!TranslateAccelerator( ghwndMain, ghAccel, &msg )) {
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
return TRUE ;
}
return FALSE ;
}
/*-----------------------------------------------------------------------------
FUNCTION: CaptureFile(HANDLE, HWND)
PURPOSE: Receives a file
PARAMETERS:
hFile - handle of file to receive the data being captured (not used)
hWndProgress - window handle of progress bar (not used)
COMMENTS: Sets the receive state and waits for capture to end
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
//void CaptureFile(HANDLE hFile, HWND hWndProgress)
//{
// UINT uMsgResult;
// gdwReceiveState = RECEIVE_CAPTURED;
//
// while ( !gfAbortTransfer ) {
//
// uMsgResult = CheckForMessages();
//
// //
// // If WM_CLOSE is retrieved, then exit.
// // If no message is retrieved, then sleep a little.
// // If any other message is retrieved, check for another one.
// //
// switch(uMsgResult)
// {
// case WM_CLOSE: gfAbortTransfer = TRUE; break;
// case FALSE: Sleep(200); break;
// case TRUE: break;
// }
// }
//
// gdwReceiveState = RECEIVE_TTY;
//
// return;
//}
/**********************************************/
/* fileCheck_fix_MACID锟斤拷锟斤拷 */
/* */
/*********************************************/
#define HEADER_START 0x44332211
#define BAUDRATE_DET_DATA 0xFFAAFF55
#define BAUDRATE_DET_DATA_LEN 0xB00
#define HEADER_LEN 0x20
#define MAC_ADDR_OFFSET 0x68000
#define U32 unsigned int
enum {
READ_CPU_TICKS_ERR,
WRONG_FILE_FORMAT,
CHECKSUM_ERROR,
MACID_SUB_USELESS,
FILE_CHCK_SUCCESSFUL
};
typedef struct {
U32 rx_header_start;
U32 rx_copy_addr;
U32 rx_run_addr;
U32 rx_total_bytes;
U32 rx_bootloader_flag;
U32 rx_sflash_size;
U32 rx_checksum;
U32 rx_dummy_data_size;
} BOOT_HEADER;
const char dft_bdaddr[6] = { 0x00,0x00,0x00,0x3F,0x9f,0x94 };
char macid_sub[3] = {0x9e, 0x8b, 0x0};
char macid_company[3] = {0x0, 0x1, 0x2};
//char *fData,
int fileCheck_fix_MACID(DWORD dataLen, const char macid_sub[3], const char macid_company[3])
{
int i;
int ret = FILE_CHCK_SUCCESSFUL;
char *p = g_appdata.BufEarphoneBin + MAC_ADDR_OFFSET + BAUDRATE_DET_DATA_LEN + HEADER_LEN;
//char *p = g_appdata.BufEarphoneBin + MAC_ADDR_OFFSET + 0xB00 + 0x20;
U32 *p1 = (U32 *)g_appdata.BufEarphoneBin;
unsigned char baaddr[6];
BOOT_HEADER *p_header = (BOOT_HEADER *)(g_appdata.BufEarphoneBin + BAUDRATE_DET_DATA_LEN);
if ((macid_sub[0] == 0x9e) && (macid_sub[1] == 0x8b) && ((macid_sub[2] >= 0x0) && (macid_sub[2] <= 0x3f)))
{
ret = MACID_SUB_USELESS;
}
memcpy(baaddr, macid_sub, sizeof(macid_sub));
memcpy(baaddr + (sizeof(macid_sub)), macid_company, sizeof(macid_company));
//memset(baaddr,0,sizeof(baaddr));
//app_lcd_device.display_char (" ", 0, 0);
//app_lcd_device.display_char (" ", 1, 0);
/***********det data check*********************/
for (i = 0; i < BAUDRATE_DET_DATA_LEN / 4; i++)
{
if (*p1++ != BAUDRATE_DET_DATA)
{
ret = WRONG_FILE_FORMAT;
//app_lcd_device.display_char("wrong file",0,0);
break;
}
}
/***********header check*********************/
if (ret == FILE_CHCK_SUCCESSFUL)
{
if (p_header->rx_header_start != HEADER_START)
{
ret = WRONG_FILE_FORMAT;
//app_lcd_device.display_char("wrong file",0,0);
}
else if (p_header->rx_total_bytes != (dataLen - BAUDRATE_DET_DATA_LEN - sizeof(BOOT_HEADER)))
{
ret = WRONG_FILE_FORMAT;
//app_lcd_device.display_char("wrong file",0,0);
}
}
/***********checksum*********************/
if (ret == FILE_CHCK_SUCCESSFUL)
{
int checksum = 0;
p1 = (U32*)(g_appdata.BufEarphoneBin + BAUDRATE_DET_DATA_LEN + sizeof(BOOT_HEADER));
for (i = 0; i < (p_header->rx_total_bytes - 4) / 4; i++)
{
checksum += *p1++;
}
if ((checksum != *p1) || (checksum != p_header->rx_checksum))
{
ret = CHECKSUM_ERROR;
//app_lcd_device.display_char("check err",0,0);
}
}
//BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
//LARGE_INTEGER ticks;
#if 0//
baaddr[0] = (unsigned char)(macid_0) & 0x000000ff;
baaddr[1] = (unsigned char)(macid_1) & 0x000000ff;
baaddr[2] = (unsigned char)(macid_2) & 0x000000ff;
baaddr[3] = (unsigned char)(macid_3) & 0x000000ff;
baaddr[4] = (unsigned char)(macid_4) & 0x000000ff;
baaddr[5] = (unsigned char)(macid_5) & 0x000000ff;
//#else
U32 U32_ticks = read_ccount();
if (U32_ticks)
{
//char szMessage[70];
baaddr[0] = (unsigned char)(U32_ticks & 0x000000ff);
baaddr[1] = (unsigned char)(U32_ticks >> 8) & 0x000000ff;
baaddr[2] = (unsigned char)(U32_ticks >> 16) & 0x000000ff;
//wsprintf(szMessage, "bt bdaddr:0x%02x%02x%02x%02x%02x%02x\r\n",baaddr[5],baaddr[4],baaddr[3],baaddr[2],baaddr[1],baaddr[0]);
//UpdateStatus(szMessage);
}
else
{
ret = READ_CPU_TICKS_ERR;
app_lcd_device.display_char("read clk err", 0, 0);
}
#endif
// "Optek Bt\0\0"
*p++ = 'O';
*p++ = 'p';
*p++ = 't';
*p++ = 'e';
*p++ = 'k';
*p++ = ' ';
*p++ = 'B';
*p++ = 't';
// *p++ = 0;
//u32 wr_cn;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
// bt mac id
/* *p++ = 0x11;
*p++ = 0x12;
*p++ = 0x13;
*p++ = 0x14;
*p++ = 0x15;
*p++ = 0x16;*/
memcpy(p, baaddr, sizeof(baaddr));
p += 6;
p = g_appdata.BufEarphoneBin + MAC_ADDR_OFFSET + 0X1000 + 0xb20;
// "Optek Bt\0\0"
*p++ = 'O';
*p++ = 'p';
*p++ = 't';
*p++ = 'e';
*p++ = 'k';
*p++ = ' ';
*p++ = 'B';
*p++ = 't';
// *p++ = 0;
//u32 wr_cn;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
*p++ = 0;
// bt mac id
/* *p++ = 0x11;
*p++ = 0x12;
*p++ = 0x13;
*p++ = 0x14;
*p++ = 0x15;
*p++ = 0x16;*/
memcpy(p, baaddr, sizeof(baaddr));
p += 6;
//a-a5
/*
buf = buf+0xfc00c;
buf[0] = a;
buf[1] = a1;
buf[2] = a2;
buf[3] = a3;
buf[4] = a4;
buf[5] = a5;
buf = buf-0xfc00c;
*/
if (ret == FILE_CHCK_SUCCESSFUL)
{
int checksum = 0;
p1 = (U32*)(g_appdata.BufEarphoneBin + BAUDRATE_DET_DATA_LEN + sizeof(BOOT_HEADER));
for (i = 0; i < (p_header->rx_total_bytes - 4) / 4; i++)
{
checksum += *p1++;
}
*p1 = checksum;
p_header->rx_checksum = checksum;
//app_lcd_device.display_char("check ok",0,0);
}
return ret;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferThreadProc(LPVOID)
PURPOSE: Worker thread does all the file transfer work
PARAMETERS:
lpV - actually a HANDLE for the file
COMMENTS: Function allows the hTransferAbortEvent to
signal an abort condition.
If the thread finishes OK, then the thread
calls the TransferFileTextEnd function itself.
HISTORY: Date: Author: Comment:
1/26/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
DWORD WINAPI TransferThreadProc(LPVOID lpV)
{
DWORD dwFileSize;
DWORD dwStartTime;
HWND hWndProgress;
HANDLE hFileHandle;
HANDLE hDataHeap;
BOOL fStarted = TRUE;
BOOL fAborting = FALSE;
BOOL userAborting = TRUE;
char * pRead;
DWORD dwRead;
int err;
//hFileHandle = (HANDLE) lpV;
hWndProgress = GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS);
{
SendMessage(hWndProgress, PBM_SETRANGE, 0, MAKELPARAM(0, g_appdata.MaxPackets + 1));
SendMessage(hWndProgress, PBM_SETSTEP, (WPARAM) 1, 0);
SendMessage(hWndProgress, PBM_SETPOS, 0, 0);
}
// set up transfer heaps
if (!fAborting)
{
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
hDataHeap = HeapCreate(0, sysInfo.dwPageSize * 2, sysInfo.dwPageSize * 10);
if (hDataHeap == NULL)
{
ErrorReporter("HeapCreate (Data Heap)");
fAborting = TRUE;
}
}
#if 1 //def check file
/**********************check file*********************************/
//lpfileBuf = HeapAlloc(hDataHeap, 0, dwFileSize);
pRead = g_appdata.BufEarphoneBin;
err = fileCheck_fix_MACID(g_appdata.BinSizeFileEarphone, macid_sub, macid_company);
if (err == WRONG_FILE_FORMAT)
{
ErrorReporter("wrong file format!");
userAborting = FALSE;
fAborting = TRUE;
}
else if (err == CHECKSUM_ERROR)
{
ErrorReporter("check sum err!");
userAborting = FALSE;
fAborting = TRUE;
}
else if (err == READ_CPU_TICKS_ERR)
{
ErrorReporter("read cpu ticks error!");
userAborting = FALSE;
fAborting = TRUE;
}
#endif //check file
if (WaitForSingleObject(hTransferAbortEvent, 0) == WAIT_OBJECT_0)
fAborting = TRUE;
// inform writer thread that a file is about to be transferred
if (!fAborting)
{
if (!WriterAddNewNode(WRITE_FILESTART, g_appdata.BinSizeFileEarphone, 0, NULL, NULL, NULL))
fAborting = TRUE;
}
OutputDebugString("Xfer: About to start sending data\n");
// Get Transfer Start Time
dwStartTime = GetTickCount();
//if (WaitForSingleObject(hTransferAbortEvent, 0) == WAIT_OBJECT_0)
// fAborting = TRUE;
while (!fAborting)
{
char * lpDataBuf;
PWRITEREQUEST pWrite;
// transfer file, loop until all blocks of file have been read
lpDataBuf = HeapAlloc(hDataHeap, 0, MAX_WRITE_BUFFER);
pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
if ((lpDataBuf != NULL) && (pWrite != NULL))
{
DWORD dwRead;
// read from file into new buffer
if (ReadFile(hFileHandle, lpDataBuf, MAX_WRITE_BUFFER, &dwRead, NULL))
{
WriterAddExistingNode(pWrite, WRITE_FILE, dwRead, 0, lpDataBuf, hDataHeap, hWndProgress);
if (dwRead != MAX_WRITE_BUFFER) break;
}
}
else
{
BOOL fRes;
/*
Either the data heap is full, or the writer heap is full.
Free any allocated block, wait a little and try again.
Waiting lets the writer thread send some blocks and free
the data blocks from the data heap and the control
blocks from the writer heap.
*/
if (lpDataBuf)
{
EnterCriticalSection(&gcsDataHeap);
fRes = HeapFree(hDataHeap, 0, lpDataBuf);
LeaveCriticalSection(&gcsDataHeap);
if (!fRes) ErrorReporter("HeapFree (Data block)");
}
if (pWrite)
{
EnterCriticalSection(&gcsWriterHeap);
fRes = HeapFree(ghWriterHeap, 0, pWrite);
LeaveCriticalSection(&gcsWriterHeap);
if (!fRes)
ErrorReporter("HeapFree (Writer block)");
}
OutputDebugString("Xfer: A heap is full. Waiting...\n");
// wait a little
// check for abort during the wait
if (WaitForSingleObject(hTransferAbortEvent, 200) == WAIT_OBJECT_0)
fAborting = TRUE;
}
// has the user aborted?
if (WaitForSingleObject(hTransferAbortEvent, 0) == WAIT_OBJECT_0)
fAborting = TRUE;
}
OutputDebugString("Xfer: Done sending packets.\n");
if (fAborting)
{
// inform writer that transfer is aborting
OutputDebugString("Xfer: Sending Abort Packet to writer\n");
WriterAddFirstNodeTimeout(WRITE_ABORT, dwFileSize, 0, NULL, NULL, NULL, 500);
}
else
WriterAddNewNodeTimeout(WRITE_FILEEND, dwFileSize, 0, NULL, NULL, NULL, 500);
{
// wait til writer thread finishes with all blocks
HANDLE hEvents[2];
DWORD dwRes;
BOOL fTransferComplete;
hEvents[0] = ghTransferCompleteEvent;
hEvents[1] = hTransferAbortEvent;
OutputDebugString("Xfer: Waiting for transfer complete signal from writer\n");
do
{
ResetEvent(hTransferAbortEvent);
dwRes = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
switch (dwRes)
{
case WAIT_OBJECT_0:
fTransferComplete = TRUE;
OutputDebugString("Transfer complete signal rec'd\n");
break;
case WAIT_OBJECT_0 + 1:
fAborting = TRUE;
OutputDebugString("Transfer abort signal rec'd\n");
OutputDebugString("Xfer: Sending Abort Packet to writer\n");
if (!WriterAddFirstNodeTimeout(WRITE_ABORT, dwFileSize, 0, NULL, NULL, NULL, 500))
ErrorReporter("Can't add abort packet\n");
break;
case WAIT_TIMEOUT:
break;
default:
ErrorReporter("WaitForMultipleObjects(Transfer Complete Event and Transfer Abort Event)");
fTransferComplete = TRUE;
break;
}
}
while(!fTransferComplete);
}
OutputDebugString("Xfer: transfer complete\n");
// report statistics
if (!fAborting)
ShowTransferStatistics(GetTickCount(), dwStartTime, dwFileSize);
// break down metrics
PostMessage(hWndProgress, PBM_SETPOS, 0, 0);
// break down heaps
if (hDataHeap != NULL)
{
if (!HeapDestroy(hDataHeap))
ErrorReporter("HeapDestroy (data heap)");
}
// If I am done without user intervention, then post the
// "abort" message myself. This will cause the main thread to
// clean up after the file transfer.
//if (!fAborting) PostMessage(ghwndMain, WM_COMMAND, ID_TRANSFER_ABORTSENDING, 0);
// exit thread
return 0;
}