vmpreload.c
49.9 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
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
#include "vmswitch.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "MMI_mre_trc.h"
#include "mmi_trc.h"
#ifdef __MRE_SAL_SOCKET__
extern void _vm_trace(char* fmt, ...);
#ifdef WIN32
#define vm_trace _vm_trace
#else
#define vm_trace(...) _vm_trace(__VA_ARGS__)
#endif
#include "vmsock.h"
#include "vmchset.h"
#include "vmstdlib.h"
#include "vmio.h"
#include "vmpromng.h"
#include "vmresmng.h"
#include "vmmem.h"
#include "vmmod.h"
#include "vmmacrostub.h"
#include "MRESrvDownloadGprot.h"
#include "DtcntSrvIprot.h"
#include "DLAgentSrvGProt.h"
#define LF 10
#define CR 13
#define BUFFER_LEN (2*1024)
static kal_char * CONTENT_LENGTH = "Content-Length:";
//#define B_START "--------------------------------96003044DE287"
//#define B_END "----------------------------------96003044DE287--"
typedef struct
{
VMINT res_id;
VMINT p_hdl;
VMINT app_id;
VMINT soc_id;
void * user_data;
void (*cb)(VMINT hdl, void * para);
VMCHAR url[256];
VMINT port;
VMCHAR host[64];
VMINT apn;
VMINT update;
VMWCHAR path[260];
VMINT twice_confirmed;
VMINT g_http_content_length;
VMINT need_recv_len;
VMINT status;
VMBOOL bDLA;
vm_preload_query_info_t query_info;
}vm_preload_ctx_t;
//static VMINT g_http_content_length = 0;
//static VMINT need_recv_len = 0;
//static vm_preload_ctx_t ctx;
extern const char* applib_inet_get_user_agent_string(void);
static void _vm_preload_free_ctx(vm_preload_ctx_t * ctx_p);
static vm_preload_ctx_t * _vm_preload_malloc_ctx(void);
static vm_preload_ctx_t * _vm_preload_get_ctx(VM_P_HANDLE p_hdl,VMINT id, VMINT type);
extern kal_int32 app_strnicmp(kal_char *s, kal_char *t, int n);
static void _vm_preload_cb(VMINT handle, VMINT event);
static void _vm_preload_clean(vm_preload_ctx_t * ctx_p);
static VMINT _vm_preload_check_valid(vm_preload_ctx_t * ctx_p);
static void PRELOAD_PMNG_WRAP_CALLBACK(VM_P_HANDLE p, void (*cb)(VMINT hdl, void * para), VMINT rid, void * data)
{
if (vm_pmng_set_ctx(p) == VM_PMNG_OP_OK)
{
if (cb)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_S, 3, __LINE__);
cb(rid, data);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E, 3, __LINE__);
}
vm_pmng_reset_ctx();
}
}
static void vm_preload_do_connection(vm_preload_ctx_t * ctx_p, VMINT n)
{
vm_apn_info_ext info = {0};
VMUINT dtacct_id;
vm_get_encoded_dtacct_id(VM_TCP_APN_CMWAP, &dtacct_id);
if (vm_wifi_is_connected())
{
ctx_p->soc_id = vm_tcp_connect(ctx_p->host, ctx_p->port, ctx_p->apn, _vm_preload_cb);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload cnt wifi1[%s, %d] npx[%d]",
ctx_p->host, ctx_p->port, n);
}
else
{
if (0 == vm_get_apn_info(&info)) // px
{
memset(&info, 0, sizeof(info));
if (0 == vm_get_default_apn_info(&info))
{
if (info.apn_info_id)
{
//ctx_p->apn = 2;
ctx_p->soc_id = vm_tcp_connect(info.proxy_ip, info.proxy_port, VM_TCP_APN_CMWAP, _vm_preload_cb);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload cnt [%s, %d] px[%d]",
info.proxy_ip, info.proxy_port, n);
}
else
{
ctx_p->soc_id = vm_tcp_connect(ctx_p->host, ctx_p->port, ctx_p->apn, _vm_preload_cb);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload cnt [%s, %d] npx[%d]",
info.proxy_ip, info.proxy_port, n);
}
}
else // wifi without sim
{
ctx_p->soc_id = vm_tcp_connect(ctx_p->host, ctx_p->port, ctx_p->apn, _vm_preload_cb);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload cnt 1[%s, %d] npx[%d]",
ctx_p->host, ctx_p->port, n);
}
}
else // npx
{
ctx_p->soc_id = vm_tcp_connect(ctx_p->host, ctx_p->port, ctx_p->apn, _vm_preload_cb);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload cnt 2[%s, %d] npx[%d]",
ctx_p->host, ctx_p->port, n);
}
}
}
static void vm_preload_free_resource(VM_P_HANDLE process_handle)
{
VMINT resHandle = 0, bufSize = 0;
vm_preload_ctx_t * ctx_p = NULL;
while ((resHandle = vm_res_findfirst(process_handle, VM_RES_TYPE_PRELOAD))
!= VM_RES_NOT_FIND)
{
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
resHandle, (void**)&ctx_p, &bufSize) == 0 && ctx_p != NULL)
{
if (ctx_p)
{
//_vm_preload_clean(ctx_p);
vm_common_t common = {E_PRELOAD_ABORT, NULL};
common.user_data = ctx_p->user_data;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_S, 1, __LINE__);
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&common);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E, 1, __LINE__);
_vm_preload_free_ctx(ctx_p);
}
}
else
{
}
vm_res_release_data(VM_RES_TYPE_PRELOAD, resHandle);
vm_res_findclose(VM_RES_TYPE_PRELOAD);
}
}
static void vm_preload_notify_process_status(VM_P_HANDLE process_handle,
VMINT sys_state)
{
switch (sys_state)
{
//case VM_PMNG_UNLOAD:
case VM_PMNG_CLOSING:
vm_preload_free_resource(process_handle);
break;
}
}
static VMINT vm_preload_initialize_resource(void)
{
if (vm_res_type_set_notify_callback(VM_RES_TYPE_PRELOAD,
vm_preload_notify_process_status) != 0)
{
return -1;
}
///TODO: other init.
return 0;
}
static VMINT vm_preload_finialize_resource(void)
{
vm_preload_free_resource(-1);
vm_res_type_set_notify_callback(VM_RES_TYPE_PRELOAD, NULL);
return 0;
}
static VMINT vm_preload_mod_evt_proc(MRE_MOD_LIFECIRCLE_EVT event)
{
switch(event)
{
case EVT_MOD_INIT:
if (vm_preload_initialize_resource() != 0)
return -1;
break;
case EVT_MOD_RELEASE:
vm_preload_finialize_resource();
break;
case EVT_MOD_ACTIVE:
break;
case EVT_MOD_INACTIVE:
break;
default:
return -2;
}
return 0;
}
VMINT _vm_reg_preload_module(void)
{
int res_code = REG_MRE_MODULE_SUCCESS;
if ((res_code = _vm_reg_module("PLD", (MOD_EVT_PROCESS)vm_preload_mod_evt_proc)) != REG_MRE_MODULE_SUCCESS)
{
}
return res_code;
}
static vm_preload_ctx_t * _vm_preload_get_ctx(VM_P_HANDLE p_hdl,VMINT id, VMINT type)
{
VMINT resHandle = 0, bufSize = 0;
vm_preload_ctx_t * ctx_p = NULL;
if ((resHandle = vm_res_findfirst(p_hdl, VM_RES_TYPE_PRELOAD)) != VM_RES_NOT_FIND)
{
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
resHandle, (void**)&ctx_p, &bufSize) == 0 && ctx_p != NULL)
{
if (type == 0)
{
if (ctx_p->app_id == id)
{
vm_res_findclose(VM_RES_TYPE_PRELOAD);
return ctx_p;
}
}
else if (type == 1)
{
if (ctx_p->soc_id == id)
{
vm_res_findclose(VM_RES_TYPE_PRELOAD);
return ctx_p;
}
}
}
while ((resHandle = vm_res_findnext(p_hdl, VM_RES_TYPE_PRELOAD)) != VM_RES_NOT_FIND)
{
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
resHandle, (void**)&ctx_p, &bufSize) == 0 && ctx_p != NULL)
{
if (type == 0)
{
if (ctx_p->app_id == id)
{
vm_res_findclose(VM_RES_TYPE_PRELOAD);
return ctx_p;
}
}
else if (type == 1)
{
if (ctx_p->soc_id == id)
{
vm_res_findclose(VM_RES_TYPE_PRELOAD);
return ctx_p;
}
}
}
}
vm_res_findclose(VM_RES_TYPE_PRELOAD);
}
return NULL;
}
static vm_preload_ctx_t * _vm_preload_malloc_ctx(void)
{
vm_preload_ctx_t * ctx_p = NULL;
if ((ctx_p = _vm_kernel_calloc(sizeof(vm_preload_ctx_t))) != NULL)
{
if ((ctx_p->res_id = vm_res_save_data(VM_RES_TYPE_PRELOAD,
(void*)ctx_p, sizeof(ctx_p), NULL, vm_pmng_get_current_handle())) >= 0)
{
return ctx_p;
}
else
{
_vm_kernel_free(ctx_p);
}
}
return NULL;
}
static void _vm_preload_free_ctx(vm_preload_ctx_t * ctx_p)
{
VMINT res_id;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2853 );
if (NULL == ctx_p)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2856 );
return;
}
res_id = ctx_p->res_id;
_vm_preload_clean(ctx_p);
if (VM_RES_OK == vm_res_release_data(VM_RES_TYPE_PRELOAD, res_id))
{
_vm_kernel_free(ctx_p);
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2863 );
}
VMINT vm_preload_query(VMINT apn,
const VMCHAR * url,
VMINT port,
const vm_preload_query_info_t * info,
void (*callback)(VMINT hdl, void * para),
void * user_data)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
VMCHAR http_host[128] = {0};
vm_apn_info_ext apn_info;
vm_common_t common = {E_PRELOAD_QUERYING, NULL};
vm_preload_ctx_t * ctx_p = NULL;
//srv_dtcnt_sim_type_enum sim_type;
VMINT with_port = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2884 );
if (NULL == url ||
0 > port ||
NULL == info ||
NULL == info->ua ||
NULL == callback)
// NULL == user_data)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2893 );
return -2;
}
if (VM_TCP_APN_CMNET == apn ||
VM_TCP_APN_CMNET_ONLY == apn ||
VM_TCP_APN_WIFI == apn &&
apn != VM_APN_USER_DEFINE)
{
VMINT pos = 7; // http://
for(; url[pos]; pos ++)
{
if (url[pos] == ':')
{
with_port = atoi(&url[pos] + 1);
break;
}
if (url[pos] == '/')
break;
}
strncpy(http_host, url + 7, pos - 7);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2907 , http_host);
}
else if (VM_TCP_APN_CMWAP == apn ||
VM_TCP_APN_CMWAP_ONLY == apn)
{
VMUINT dtacct_id;
vm_get_encoded_dtacct_id(VM_TCP_APN_CMWAP, &dtacct_id);
if (0 == vm_get_apn_info(&apn_info) && !apn_info.apn_info_id)
{
VMINT pos = 7; // http://
for(; url[pos]; pos ++)
{
if (url[pos] == ':')
{
with_port = atoi(&url[pos] + 1);
break;
}
if (url[pos] == '/')
break;
}
strncpy(http_host, url + 7, pos - 7);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2923 , apn_info.proxy_ip);
}
else
{
with_port = apn_info.proxy_port;// atoi(&url[pos] + 1);
strncpy(http_host, apn_info.proxy_ip, sizeof(apn_info.proxy_ip));
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2929 , http_host);
}
else
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2933 );
return -3;
}
ctx_p = _vm_preload_get_ctx(vm_pmng_get_current_handle(), info->app_id, 0);
if (NULL == ctx_p)
{
ctx_p = _vm_preload_malloc_ctx();
if (NULL == ctx_p)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2943 );
return -4;
}
}
/*
else
{
if (1 == ctx_p->update)
{
vm_trace("vm_preload_query res not enough -5");
return -5;
}
}
*/
ctx_p->p_hdl = vm_pmng_get_current_handle();
ctx_p->app_id = info->app_id;
ctx_p->user_data = user_data;
ctx_p->cb = callback;
strcpy(ctx_p->url, url);
ctx_p->port = with_port ? with_port : port;
strcpy(ctx_p->host, http_host);
ctx_p->apn = apn;
ctx_p->update = 0;
memcpy(&(ctx_p->query_info), info, sizeof(vm_preload_query_info_t));
ctx_p->twice_confirmed = 0;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2959 , ctx_p->host, ctx_p->port, apn);
#ifdef WIN32
//strcpy(ctx_p->host, "172.21.87.241");
#endif
if (ctx_p->soc_id > 0)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2965 );
vm_tcp_close(ctx_p->soc_id);
}
//srv_dtcnt_get_sim_preference(&sim_type);
if ((stub_tcp_is_support_wifi() && VM_TCP_APN_WIFI == ctx_p->apn)
#ifdef __COSMOS_MMI_PACKAGE__
// || (srv_mre_da_get_account_id(SRV_MRE_DA_WIFI_ONLY) == 1)
// || (sim_type == SRV_DTCNT_SIM_TYPE_NONE)
#endif
)
{
if (SRV_DTCNT_WLAN_STATUS_INACTIVE == srv_dtcnt_wlan_status())
{
//MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_UDP_E3, 2, __LINE__);
_vm_preload_free_ctx(ctx_p);
return -6;
}
}
#if 1
vm_preload_do_connection(ctx_p, 1);
#else
/* under construction !*/
#endif
if (0 > ctx_p->soc_id)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2973 );
_vm_preload_free_ctx(ctx_p);
return -5;
}
common.user_data = ctx_p->user_data;
common.state = E_PRELOAD_CONNECTING;
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&common);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2983 );
ctx_p->status = E_PRELOAD_DOWNLOADING;
return ctx_p->res_id;
}
#if defined(MMI_DA_HTTP_DOWNLOAD_SUPPORT)
void vm_preload_download_callback(srv_da_download_progress_struct *report_struct)
{
vm_preload_ctx_t * ctx_p = report_struct->user_data;
VMINT found = 0;
vm_preload_recv_data_t data;
found = _vm_preload_check_valid(ctx_p);
if (!found)
{
return;
}
if (0 == report_struct->status)
{
data.head.state = E_PRELOAD_DOWNLOADING;
ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADING;
}
else if (1 == report_struct->status)
{
data.head.state = E_PRELOAD_DOWNLOADED;
ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADED;
}
else
{
//srv_da_http_dl_operation(report_struct->arg, SRV_DA_DOWNLOAD_STOP);
data.head.state = E_PRELOAD_ABORT;
ctx_p->status = (VMINT)E_PRELOAD_ABORT;
}
data.head.user_data = ctx_p->user_data;
data.received = report_struct->curr_size;
data.total = report_struct->total_size;
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&data);
if (data.head.state != E_PRELOAD_DOWNLOADING )
{
//_vm_preload_clean(ctx_p);
_vm_preload_free_ctx(ctx_p);
}
return;
}
void vm_preload_download_setting(
S32 session_id,
S32 mime_type,
S32 mime_subtype,
S32 action,
U32 filesize,
S8 *url,
S8 *mime_type_string,
U32 content_len,
S8 *content,
srv_da_setting_struct *setting
)
{
}
void vm_preload_download_dispatch(
S32 session_id,
S32 mime_type,
S32 mime_subtype,
S32 action,
U16 *filepath,
S8 *url,
S8 *mime_type_string
)
{
}
#endif
#if !(defined(MMI_DA_HTTP_DOWNLOAD_SUPPORT))
VMBOOL g_no_dla = 1;
#elif (!defined(__MMI_APP_MANAGER_SUPPORT__) && defined(__PLUTO_MMI_PACKAGE__))
VMBOOL g_no_dla = 1;
#else
VMBOOL g_no_dla = 0;
#endif
static VMINT _vm_preload_try_download(vm_preload_ctx_t * ctx_p)
{
VMCHAR *http_header;
VMINT ret = 0;
VMINT written = 0;
VMINT bufSize = 0;
ASSERT(ctx_p);
if (0 > ctx_p->soc_id && ctx_p->update)
{
// host is changed.
if (0 == strstr(ctx_p->url, ctx_p->host))
{
VMINT nspan = 7; // http:// is 7 bytes
for(nspan = 7; ctx_p->url[nspan] != '\0'; nspan ++)
{
if (ctx_p->url[nspan] == ':')
{
ctx_p->port = atoi(&ctx_p->url[nspan + 1]);
break;
}
if (ctx_p->url[nspan] == '/')
break;
}
memset(ctx_p->host, 0, sizeof(ctx_p->host));
memcpy(ctx_p->host, &ctx_p->url[7], nspan - 7);
}
#if 1
vm_preload_do_connection(ctx_p, 2);
#else
/* under construction !*/
#endif
if (0 > ctx_p->soc_id)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3023 );
return -4;
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3026 );
ctx_p->update = 2;
return 0;
}
http_header = vm_calloc(BUFFER_LEN);
if (NULL == http_header)
return -5;
// http header
sprintf(http_header, "GET %s HTTP/1.1\r\nHost: %s:%d\r\n"
#ifdef WIN32
//"Proxy-Authorization: Basic TXRrODAwMjM6MWFjNmIxMDEx\r\n"
#endif
"User-Agent: %s\r\n"
"Accept: %s\r\n"
"Accept-Language: %s\r\n\r\n",
ctx_p->url,
ctx_p->host,
ctx_p->port,
(VMCHAR*)applib_inet_get_user_agent_string(),
"*/*",
"en");
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3051 , ctx_p->url, ctx_p->port, ctx_p->host);
while (0 < (ret = vm_tcp_write(ctx_p->soc_id, http_header + written, strlen(http_header) - written)))
{
written += ret;
}
vm_free(http_header);
return written;
}
VMINT vm_preload_download_ex(VMINT hdl, const VMWCHAR * data, VMBOOL bDLA,void (*callback)(VMINT hdl, void * para), void * user_data)
{
VMCHAR http_header[BUFFER_LEN] = {0};
VMINT ret = 0;
VMINT written = 0;
vm_preload_ctx_t * ctx_p = NULL;
VMINT bufSize = 0;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2997 );
if (NULL == data || 0 > hdl)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3000 );
return -2;
}
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
hdl, (void**)&ctx_p, &bufSize) != VM_RES_OK)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3058 , -6);
return -6;
}
if (NULL == ctx_p)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3009 );
return -3;
}
vm_wstrcpy(ctx_p->path, (const VMWSTR)data);
ctx_p->cb = callback;
ctx_p->user_data = user_data;
ctx_p->bDLA = bDLA;
if (!bDLA || g_no_dla)
{
ret = _vm_preload_try_download(ctx_p);
if (ret <= 0)
return ret;
}
else
{
#if defined(MMI_DA_HTTP_DOWNLOAD_SUPPORT)
VMUINT account_id;
srv_da_start_download_struct da_param;
vm_apn_info_ext info = {0};
memset(&da_param,0,sizeof(da_param));
vm_get_encoded_dtacct_id(VM_TCP_APN_CMWAP, &account_id);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload dla dt[0x%x]",
account_id);
if (0 == vm_get_apn_info(&info)) // px
{
if (0 == vm_get_default_apn_info(&info))
{
if (info.apn_info_id)
{
vm_get_encoded_dtacct_id(VM_TCP_APN_CMWAP, &account_id);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload dla px[0x%x]",
account_id);
}
else
{
vm_get_encoded_dtacct_id(VM_TCP_APN_CMNET, &account_id);
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload dla npx[0x%x]",
account_id);
}
}
else // wifi without sim
{
}
}
srv_dtcnt_get_auto_acc_id(account_id, &da_param.acc_id);
mmi_asc_n_to_ucs2((CHAR *)http_header,ctx_p->url,BUFFER_LEN);
da_param.url = (WCHAR*)http_header;
//da_param.acc_id = account_id;
da_param.show_confirm = MMI_FALSE;
da_param.user_data = ctx_p;
da_param.filepath = (U16 *)data;
da_param.dl_progress = vm_preload_download_callback;
da_param.dispatch_callback = NULL;
da_param.setting_hdlr = srv_da_default_setting;// MAUI_03202723 srv_da_bypass_setting -> srv_da_default_setting; //vm_preload_download_setting;
if (NULL == srv_da_start_http_download(&da_param))
{
//_vm_preload_clean(ctx_p);
_vm_preload_free_ctx(ctx_p);
return -5;
}
#endif
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3058 , ret);
ctx_p->update = 2;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3060 );
return 0;
}
VMINT vm_preload_download(VMINT hdl, const VMWCHAR * data, void (*callback)(VMINT hdl, void * para), void * user_data)
{
return vm_preload_download_ex(hdl,data,0,callback,user_data);
}
VMINT vm_preload_cancel(VMINT hdl)
{
vm_preload_ctx_t * ctx_p = NULL;
VMINT bufSize = 0;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3068 );
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
hdl, (void**)&ctx_p, &bufSize) == 0 && ctx_p != NULL)
{
vm_common_t common = {E_PRELOAD_ABORT, NULL};
common.user_data = ctx_p->user_data;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_S, 2, __LINE__);
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&common);
//_vm_preload_clean(ctx_p);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E, 2, __LINE__);
_vm_preload_free_ctx(ctx_p);
}
if (NULL == ctx_p)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3075 );
return -2;
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3081 );
return 0;
}
VMINT vm_preload_status(VMINT app_id)
{
VMINT result;
vm_preload_ctx_t* ctx_p = _vm_preload_get_ctx(vm_pmng_get_current_handle(),app_id, 0);
if (!ctx_p)
{
result = -2;
}
else
{
result = ctx_p->status;
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3058 , result);
return result;
}
void vm_preload_show_download_list(void)
{
#if defined(__MMI_DOWNLOAD_AGENT_MULTI_DL__)
srv_da_display_download_list();
#endif
}
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
static void _vm_preload_try_query(vm_preload_ctx_t * ctx_p)
{
VMCHAR http_header[BUFFER_LEN] = {0};
VMCHAR http_body[BUFFER_LEN] = {0};
VMINT ret = 0;
VMINT written = 0;
vm_common_t head = {E_PRELOAD_QUERYING, NULL};
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3114 );
// vm_preload_cancel check
if (NULL == ctx_p->cb)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3117 );
return;
}
//memset(ctx_p->path, 0, sizeof(ctx_p->path));
//vm_wstrcpy(ctx_p->path, (VMWSTR)data);
//app_id=%d; ver=\"%s\"; imsi=\"%s\"; imei=\"%s\"; sc=\"%s\"; skb=\"%s\"; stch=\"%s\"; sgs=\"%s\"; scs=\"%s\"; w=%d; h=%d\r\n\
// http body
head.state = E_PRELOAD_QUERYING;
head.user_data = ctx_p->user_data;
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&head);
// vm_preload_cancel check
if (NULL == ctx_p->cb)
{
return;
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3130 );
#ifndef _POST_
// http header
if (ctx_p->query_info.ua[0])
{
sprintf(http_header, "GET %s?%s HTTP/1.1\r\nHost: %s:%d\r\n"
#ifdef WIN32
//"Proxy-Authorization: Basic TXRrODAwMjM6MWFjNmIxMDEx\r\n"
#endif
"User-Agent: %s\r\n"
"Accept: %s\r\n"
"Accept-Language: %s\r\n\r\n",
ctx_p->url,
/* ctx_p->query_info.app_id,
ctx_p->query_info.ver,
ctx_p->query_info.imsi,
ctx_p->query_info.imei, */
ctx_p->query_info.ua,
ctx_p->host,
ctx_p->port,
(VMCHAR*)applib_inet_get_user_agent_string(),
"*/*",
"en");
}
else
{
sprintf(http_header, "GET %s HTTP/1.1\r\nHost: %s:%d\r\n"
"User-Agent: %s\r\n"
"Accept: %s\r\n"
"Accept-Language: %s\r\n\r\n",
ctx_p->url,
/* ctx_p->query_info.app_id,
ctx_p->query_info.ver,
ctx_p->query_info.imsi,
ctx_p->query_info.imei, */
ctx_p->host,
ctx_p->port,
(VMCHAR*)applib_inet_get_user_agent_string(),
"*/*",
"en");
}
#else
//app_id=%d; ver=\"%s\"; imsi=\"%s\"; imei=\"%s\"; sc=\"%s\"; skb=\"%s\"; stch=\"%s\"; sgs=\"%s\"; scs=\"%s\"; w=%d; h=%d\r\n\r\n
//app_id: %d\r\nver: %s\r\nimsi: %s\r\nimei: %s\r\nsc: %s\r\nskb: %s\r\nstch: %s\r\nsgs: %s\r\nscs: %s\r\nw: %d\r\nh: %d\r\n\r\n
/* sprintf(http_body, "app_id=%d&ver=%s&imsi=%s&imei=%s&ua=%s&w=128&h=320&asdfasdf=asdfasdf&hahsdj=asdf&sss=fff",
ctx_p->query_info.app_id,
ctx_p->query_info.ver,
ctx_p->query_info.imsi,
ctx_p->query_info.imei,
ctx_p->query_info.ua);*/
strcpy(http_body,ctx_p->query_info.ua);
sprintf(http_header, "POST %s HTTP/1.1\r\nHost: %s:%d\r\nProxy-Authorization: Basic TXRrODAwMjM6MWFjNmIxMDEx\r\n\
User-Agent: %s\r\n\
Accept: %s\r\n\
Accept-Language: %s\r\n\
Accept-Charset: %s\r\n\
Accept-Encoding: %s\r\n\
Connection: %s\r\n\
Content-Type: %s\r\n\
Content-Length: %d\r\n\r\n",
ctx_p->url, ctx_p->host, ctx_p->port,
(VMCHAR*)applib_inet_get_user_agent_string(),
"*/*",
"zh_CN,zh",
"UTF-8,ISO-8859-1,US-ASCII,ISO-10646-UCS-2;q=0.6",
"identity",
"Keep-Alive",
"form-data/application/x-www-form-urlencoded",
strlen(http_body));
strcat(http_header, http_body);
#endif
while (0 < (ret = vm_tcp_write(ctx_p->soc_id, http_header + written, strlen(http_header) - written)))
{
written += ret;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3190 , written);
}
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3200 , ret);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3201 );
}
static VMINT vm_preload_is_url_valid(const VMCHAR * body, vm_preload_ctx_t * ctx_p, VMINT append)
{
const VMCHAR * stamp1 = "<go href=";
const VMCHAR * stamp2 = "<a href=";
VMCHAR *p, *p1, *p2;
VMINT i = 0;
VMINT len = strlen(body);
p1 = strstr(body, stamp1);
p2 = strstr(body, stamp2);
if (NULL == p1 && NULL == p2)
{
vm_trace("[WML] VALID [%d]", __LINE__);
return 1;
}
if (p1)
p = p1 + strlen(stamp1) + 1;
else if (p2)
p = p2 + strlen(stamp2) + 1;
while (i < len)
{
if ('\"' == p[i] || '\'' == p[i])
break;
i++;
}
//memset(ctx_p->url, 0, sizeof(ctx_p->url));
//memcpy(ctx_p->url, p, i - 1);
len = i;
vm_trace("[WML] 1 [%s][%d]", p, __LINE__);
for (i = len - 1; i >= 0; i--)
{
if ('&' == p[i] || ';' == p[i] || '?' == p[i])
{
break;
}
}
if (append)
{
strcat(ctx_p->query_info.ua, "&");
strncat(ctx_p->query_info.ua, &p[i + 1], len - i - 1);
vm_trace("[WML] ua [%s][%d]", ctx_p->query_info.ua, __LINE__);
}
else
{
strcat(ctx_p->url, "?");
strncat(ctx_p->url, &p[i + 1], len - i - 1);
vm_trace("[WML] url [%s][%d]", ctx_p->url, __LINE__);
}
return 0;
}
static void _vm_preload_clean_and_free_ctx(vm_preload_ctx_t * ctx_p, void *data)
{
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, data);
//_vm_preload_clean(ctx_p);
_vm_preload_free_ctx(ctx_p);
}
static VMINT vm_preload_get_feedback(VMBYTE *buf, VMINT feedback)
{
char *res;
res = strstr(buf, "<title>");
if (res)
{
res += strlen("<title>") - 1;
if (('0' > res[0] || '9' < res[0]) &&
('0' <= res[2] && '9' >= res[2]) &&
('0' <= res[3] && '9' >= res[3]) &&
('0' > res[4] || '9' < res[4]))
{
return atoi(res + 1);
}
}
return feedback;
}
static void _vm_preload_read_address(vm_preload_ctx_t * ctx_p)
{
//VMCHAR http_header[BUFFER_LEN*2] = {0};
//VMCHAR http_buffer[BUFFER_LEN] = {0};
static VMINT len = 0;
static VMINT wblock = 0;
VMINT i = 0, j = 0;
VMINT http_header_received = 0;
VMINT http_content_downloaded = 0;
VMINT http_content_length = 0;
VMINT http_header_len = 0;
VMINT file_handle = -1;
VMINT ret = 0;
VMINT feed_back = 0;
VMBYTE * http_header = NULL;
static VMBYTE * buf = NULL;
vm_common_t head = {E_PRELOAD_QUERYING, NULL};
const VMCHAR * http_ver = "HTTP/1.1 ";
VMINT location_len = 0;
VMCHAR * Location = NULL;
// read block
if (!wblock)
{
buf = (VMBYTE *)_vm_kernel_malloc(2*BUFFER_LEN);
if (NULL == buf)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 5, __LINE__);
return;
}
memset(buf, 0, sizeof(2*BUFFER_LEN));
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3223 );
while ((ret = vm_tcp_read (ctx_p->soc_id, buf + len, 2*BUFFER_LEN - len)) > 0)
{
len += ret;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3227 , ret, len);
}
// read block
http_header = _vm_kernel_malloc(BUFFER_LEN);
if (NULL == http_header)
{
_vm_kernel_free(buf);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 6, __LINE__);
return;
}
memset(http_header, 0, sizeof(BUFFER_LEN));
for(i = 0; i < len; i++)
{
http_header[http_header_len++] = buf[i];
if (http_header_len >= 2)
{
if (http_header[http_header_len - 1] == LF && http_header[http_header_len - 2] == CR)
{
if (http_header_len == 2) // 2
{
http_content_downloaded += len - i - 1;
j = i + 1;
//data.received += len - i - 1;
break;
}
else // 1
{
http_header_len = 0;
if (app_strnicmp((kal_char *)http_header, (kal_char *)CONTENT_LENGTH, strlen(CONTENT_LENGTH)) == 0)
{
//http_header[http_header_len - 2] = '\0';
http_content_downloaded = 0;
http_content_length = atoi((const char *)http_header + strlen(CONTENT_LENGTH) + 1);
}
if (0 == app_strnicmp((kal_char*)http_header, (kal_char*)http_ver, strlen(http_ver)))
{
feed_back = atoi((const char *)http_header + strlen(http_ver));
}
if (0 == app_strnicmp(http_header, "Location: ", strlen("Location: ")))
{
Location = _vm_kernel_malloc(BUFFER_LEN);
memset(Location, 0, BUFFER_LEN);
strcpy(Location, http_header + strlen("Location: "));
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload 302/301Loc[%s]", Location);
while (location_len < BUFFER_LEN)
{
if (Location[location_len + 1] == LF && Location[location_len] == CR)
{
break;
}
++location_len;
}
//http_header_len = location_len;
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload 302/301 Loc len[%d]", location_len);
}
}
}
}
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3264 , http_content_length, feed_back);
// read block
if (http_content_length > http_content_downloaded)
{
// recveived partitial data.
wblock = 1;
//_vm_preload_read_address(ctx_p);
_vm_kernel_free(http_header);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3227 , -2, len);
return;
}
else // not read block
{
wblock = 0;
len = 0;
}
//if (200 == feed_back)
{
// judge whether the url belong to the g-mobi(some times operator will rsp to client fristly when with wap.
buf[j + http_content_length] = 0;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3284 , buf + j);
if (vm_preload_is_url_valid((const VMCHAR *)buf + j, ctx_p, 1))//if (0 == strstr(buf + j, "go href")) // confirm the data form server.
{
//VMINT pos = 0, ctx_len = http_content_downloaded < http_content_length ? http_content_downloaded - 1 : http_content_length;
//while (0 != *(buf + j + pos) && pos < ctx_len)
//{
// if (*(buf + j + pos) == '\r' && *(buf + j + pos + 1) == '\n')
// break;
// pos++;
//}
//*(buf + j + pos) = 0;
ctx_p->twice_confirmed = 1;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3287 );
// unexpected in response in body
feed_back = vm_preload_get_feedback(buf + j, feed_back);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3287 );
if (200 == feed_back && 0 < http_content_length)
ctx_p->update = 1;
if (302 == feed_back || 301 == feed_back)
{
memset(ctx_p->url, 0, sizeof(ctx_p->url));
memcpy(ctx_p->url, Location, location_len);
ctx_p->query_info.ua[0] = 0;
MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload 302/301[%s]", ctx_p->url);
_vm_preload_try_query(ctx_p);
_vm_kernel_free(http_header);
_vm_kernel_free(buf);
_vm_kernel_free(Location);
return;
}
}
}
if (ctx_p->update)
{
strncpy((char*)ctx_p->url, (const char*)buf + j, http_content_length);
//strcpy(ctx_p->url, buf + 2);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3297 );
head.state = E_PRELOAD_AVAILABLE_UPDATE;
if (ctx_p->soc_id > 0)
{
vm_tcp_close(ctx_p->soc_id);
ctx_p->soc_id = -1;
}
}
else
{
head.state = E_PRELOAD_NO_UPDATE;
ctx_p->status = (VMINT)E_PRELOAD_NO_UPDATE;
if (0 == ctx_p->twice_confirmed)
{
_vm_preload_try_query(ctx_p);
ctx_p->twice_confirmed = 1;
_vm_kernel_free(buf);
_vm_kernel_free(http_header);
return;
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3318 );
head.user_data = ctx_p->user_data;
_vm_preload_clean_and_free_ctx(ctx_p, &head);
_vm_kernel_free(buf);
_vm_kernel_free(http_header);
return;
}
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_LOG, 4, __LINE__,
ctx_p->update,
head.state,
ctx_p->res_id,
ctx_p->user_data,
ctx_p->cb,
0
);
head.user_data = ctx_p->user_data;
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&head);
if (1 == ctx_p->update)
ctx_p->cb = NULL;
else if (2 == ctx_p->update)
ctx_p->update = 1; // recover it
_vm_kernel_free(buf);
_vm_kernel_free(http_header);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3335 );
}
static void _vm_preload_read_data(vm_preload_ctx_t * ctx_p)
{
VMCHAR *body = (VMCHAR *)_vm_kernel_calloc(BUFFER_LEN);
//VMCHAR http_buffer[BUFFER_LEN] = {0};
VMINT len = 0, i = 0;
VMINT http_header_received = 0;
VMINT first_downloaded = 0;
VMINT http_header_len = 0;
VMINT file_handle = -1;
VMINT ret = 0;
VMBYTE * buf = NULL;
VMFILE f_handle = -1;
VMUINT written = 0;
vm_preload_recv_data_t data = {{E_PRELOAD_QUERYING, NULL}, 0, 0};
data.head.user_data = ctx_p->user_data;
//memset(buf, 0, sizeof(2*BUFFER_LEN));
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3358 , ctx_p->g_http_content_length);
// read the header
if (0 == ctx_p->g_http_content_length)
{
buf = (VMBYTE *)_vm_kernel_malloc(BUFFER_LEN);
if (NULL == buf)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 7, __LINE__);
return;
}
memset(buf, 0, sizeof(BUFFER_LEN));
while ((ret = vm_tcp_read (ctx_p->soc_id, buf + len, BUFFER_LEN - len)) > 0)
{
len += ret;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3369 , ret, len);
}
for(i = 1; i < len; i++)
{
if (buf[i] == LF && buf[i - 1] == CR)
{
if (app_strnicmp((kal_char *)&buf[http_header_len], (kal_char *)CONTENT_LENGTH, strlen(CONTENT_LENGTH)) == 0)
{
buf[i - 1] = '\0';
first_downloaded = 0;
ctx_p->g_http_content_length = atoi((const char *)&buf[http_header_len] + strlen(CONTENT_LENGTH) + 1);
ctx_p->need_recv_len = ctx_p->g_http_content_length;
//data.total = http_content_length;I
ctx_p->update = 1;
}
http_header_len = i + 1;
if ((i + 2) < len &&
(buf[i + 2] == LF && buf[i + 1] == CR))
{
http_header_len += 2;
first_downloaded = len - http_header_len;
break;
}
}
}
//strncpy(body, buf + 2, first_downloaded);
memcpy(body, &buf[http_header_len], first_downloaded);
_vm_kernel_free(buf);
if (!vm_preload_is_url_valid(body, ctx_p, 0))
{
_vm_preload_try_download(ctx_p);
ctx_p->g_http_content_length = ctx_p->need_recv_len = 0;
//ctx_p->update = 0;
_vm_kernel_free(body);
return;
}
// preparing file
f_handle = vm_file_open(ctx_p->path, MODE_READ, 1);
if (0 <= f_handle)
{
vm_file_close(f_handle);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3411 );
vm_file_delete(ctx_p->path);
}
f_handle = vm_file_open(ctx_p->path, MODE_CREATE_ALWAYS_WRITE, 1);
}
// preparing file
if (0 > f_handle)
{
f_handle = vm_file_open(ctx_p->path, MODE_APPEND, 1);
}
if (0 > f_handle)
{
VMCHAR buf[260];
vm_ucs2_to_ascii(buf, 260, ctx_p->path);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3429 , buf);
data.head.state = E_PRELOAD_ERR_PATH;
ctx_p->status = E_PRELOAD_ERR_PATH;
_vm_preload_clean_and_free_ctx(ctx_p, &data);
_vm_kernel_free(body);
return;
}
// no disk space
{
VMWCHAR drv_wname[4] = {0};
VMUINT size = 0;
vm_wstrncpy(drv_wname, ctx_p->path, 1);
size = vm_get_disk_free_space(drv_wname);
if (size < (VMUINT)ctx_p->g_http_content_length)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3429 , (VMCHAR*)drv_wname);
data.head.state = E_PRELOAD_FAILURE;
ctx_p->status = E_PRELOAD_FAILURE;
if (0 <= f_handle)
{
vm_file_close(f_handle);
vm_file_delete(ctx_p->path);
}
_vm_preload_clean_and_free_ctx(ctx_p, &data);
_vm_kernel_free(body);
return;
}
}
do
{
len = first_downloaded;
while ((ret = vm_tcp_read (ctx_p->soc_id, body + len,
(ctx_p->g_http_content_length > BUFFER_LEN ? BUFFER_LEN : ctx_p->g_http_content_length) - len)) > 0)
{
// would block
if (0 == ret)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3448 );
break;
}
len += ret;
if (BUFFER_LEN == len)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3454 );
break;
}
}
ctx_p->g_http_content_length -= len;
first_downloaded = 0;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3462 , len);
data.head.state = E_PRELOAD_DOWNLOADING;
ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADING;
data.total = ctx_p->need_recv_len;
data.received = ctx_p->need_recv_len - ctx_p->g_http_content_length;
//data.buf = body;
//data.size = len;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3469 , data.received, data.total);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3470 );
vm_file_write(f_handle, body, len, &written);
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&data);
} while (BUFFER_LEN == len && 0 != ret); // would block break
vm_file_close(f_handle);
if (0 == ctx_p->g_http_content_length)
{
data.head.state = E_PRELOAD_DOWNLOADED;
ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADED;
data.total = ctx_p->g_http_content_length;
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3485 , data.total);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3486 );
_vm_preload_clean_and_free_ctx(ctx_p, &data);
}
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
_vm_kernel_free(body);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3582 );
}
static VMINT _vm_preload_check_valid(vm_preload_ctx_t * ctx_p)
{
VMINT resHandle = 0, bufSize = 0;
vm_preload_ctx_t * ctx_tmp_p = NULL;
VMINT found = 0;
do
{
if ((resHandle = vm_res_findfirst(-1, VM_RES_TYPE_PRELOAD)) != VM_RES_NOT_FIND)
{
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
resHandle, (void**)&ctx_tmp_p, &bufSize) == 0 && ctx_tmp_p != NULL)
{
if (ctx_tmp_p == ctx_p)
{
vm_res_findclose(VM_RES_TYPE_PRELOAD);
found = 1;
break;
}
}
while ((resHandle = vm_res_findnext(-1, VM_RES_TYPE_PRELOAD)) != VM_RES_NOT_FIND)
{
if (vm_res_get_data(VM_RES_TYPE_PRELOAD,
resHandle, (void**)&ctx_tmp_p, &bufSize) == 0 && ctx_tmp_p != NULL)
{
if (ctx_tmp_p == ctx_p)
{
found = 1;
break;
}
}
}
vm_res_findclose(VM_RES_TYPE_PRELOAD);
}
}
while (0);
return found;
}
static void _vm_preload_clean(vm_preload_ctx_t * ctx_p)
{
VMINT found = 0;
found = _vm_preload_check_valid(ctx_p);
if (found)
{
if (E_PRELOAD_DOWNLOADED != ctx_p->status)
{
vm_file_delete(ctx_p->path);
}
if (0 < ctx_p->soc_id)
{
vm_tcp_close(ctx_p->soc_id);
}
memset(ctx_p, 0, sizeof(vm_preload_ctx_t));
//ctx_p->p_hdl = 0;
ctx_p->soc_id = -1;
//ctx_p->res_id = -10;
}
}
static void _vm_preload_cb(VMINT handle, VMINT event)
{
vm_common_t common = {E_PRELOAD_QUERYING, NULL};
vm_preload_ctx_t * ctx_p = NULL;
ctx_p = _vm_preload_get_ctx((VM_P_HANDLE)-1, handle, 1);
// vm_preload_cancel check
if (NULL == ctx_p)
{
return;
}
common.user_data = ctx_p->user_data;
switch (event)
{
case VM_TCP_EVT_HOST_NOT_FOUND :
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3622 );
common.state = E_PRELOAD_HOST_NOT_FOUND;
ctx_p->status = (VMINT)E_PRELOAD_HOST_NOT_FOUND;
_vm_preload_clean_and_free_ctx(ctx_p, &common);
}break;
case VM_TCP_EVT_CONNECTED :
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3630 );
//_set_status(HTTP_STATUS_CONNECTED);
if (0 == ctx_p->update)
{
common.state = E_PRELOAD_CONNECTED;
PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl,
ctx_p->cb, ctx_p->res_id, (void*)&common);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3637 );
// vm_preload_cancel check
if (ctx_p->cb)
{
_vm_preload_try_query(ctx_p);
}
}
else
{
vm_preload_download_ex(ctx_p->res_id, ctx_p->path, ctx_p->bDLA,ctx_p->cb, ctx_p->user_data);
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3643 );
}
}break;
case VM_TCP_EVT_CAN_WRITE :
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3649 );
//vm_tcp_write(handle, layer_buffer,length);
}break;
case VM_TCP_EVT_CAN_READ :
{
if (0 == ctx_p->update)
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3656 );
_vm_preload_read_address(ctx_p);
//ctx_p->update = 1;
}
else
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3662 );
_vm_preload_read_data(ctx_p);
//ctx_p->update = 0;
}
}break;
case VM_TCP_EVT_PIPE_BROKEN :
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3670 );
common.state = E_PRELOAD_PIPE_BROKEN;
ctx_p->status = (VMINT)E_PRELOAD_PIPE_BROKEN;
_vm_preload_clean_and_free_ctx(ctx_p, &common);
}break;
case VM_TCP_EVT_PIPE_CLOSED :
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3678 );
common.state = E_PRELOAD_PIPE_CLOSED;
ctx_p->status = (VMINT)E_PRELOAD_PIPE_CLOSED;
_vm_preload_clean_and_free_ctx(ctx_p, &common);
}break;
default:
{
MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3689 );
common.state = E_PRELOAD_FAILURE;
ctx_p->status = (VMINT)E_PRELOAD_FAILURE;
_vm_preload_clean_and_free_ctx(ctx_p, &common);
}break;
}
}
#endif /* __MRE_SAL_SOCKET__ */