summaryrefslogtreecommitdiff
path: root/libgammu/phone/nokia/dct4s40/6510/n6510.c
blob: 9796c74d44ffdbd02de9424e1ef75bf249f453b8 (plain)
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
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
/* (c) 2002-2005 by Marcin Wiacek */
/* based on some Markus Plail, Pawel Kot work from Gnokii (www.gnokii.org)
 * (C) 1999-2000 Hugh Blemings & Pavel Janik ml. (C) 2001-2004 Pawel Kot
 * GNU GPL version 2 or later
 */
/* Due to a problem in the source code management, the names of some of
 * the authors have unfortunately been lost. We do not mean to belittle
 * their efforts and hope they will contact us to see their names
 * properly added to the Copyright notice above.
 * Having published their contributions under the terms of the GNU
 * General Public License (GPL) [version 2], the Copyright of these
 * authors will remain respected by adhering to the license they chose
 * to publish their code under.
 */

#include <gammu-config.h>

#ifdef GSM_ENABLE_NOKIA6510

#include <string.h>
#include <time.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include <gammu-nokia.h>

#include "../../../../misc/coding/coding.h"
#include "../../../../misc/locales.h"
#include "../../../../gsmcomon.h"
#include "../../../../service/gsmlogo.h"
#include "../../../../service/gsmnet.h"
#include "../../../../service/gsmring.h"
#include "../../../../service/sms/gsmmulti.h"
#include "../../nfunc.h"
#include "../../nfuncold.h"
#include "../../../pfunc.h"
#include "../dct4func.h"
#include "n6510.h"
#include "6510cal.h"
#include "6510file.h"

static GSM_Error N6510_Initialise (GSM_StateMachine *s)
{
	s->Phone.Data.Priv.N6510.CalendarIconsNum  = 0;
	s->Phone.Data.Priv.N6510.LastFreeMemoryLocation = 0;
	s->Phone.Data.Priv.N6510.LastFreeMemoryType = 0;
	s->Phone.Data.Priv.N6510.FilesLocationsAvail = 0;
	s->Phone.Data.Priv.N6510.FilesLocationsUsed = 0;
	s->Phone.Data.Priv.N6510.FilesCache = NULL;
	s->Phone.Data.Priv.N6510.ScreenWidth = 0;
	s->Phone.Data.Priv.N6510.ScreenHeight = 0;

	/* Default timeout for cables */
	s->Phone.Data.Priv.N6510.Timeout = 8;
	if (s->ConnectionType == GCT_IRDAPHONET || s->ConnectionType == GCT_FBUS2IRDA) {
		s->Phone.Data.Priv.N6510.Timeout = 40;
	}
	if (s->ConnectionType == GCT_BLUEPHONET || s->ConnectionType == GCT_BLUEFBUS2) {
		s->Phone.Data.Priv.N6510.Timeout = 20;
	}

	/* Enables various things like incoming SMS, call info, etc. */
	return N71_65_EnableFunctions (s, "\x01\x02\x06\x0A\x14\x17\x39", 7);
}

static GSM_Error N6510_Terminate (GSM_StateMachine *s)
{
	free(s->Phone.Data.Priv.N6510.FilesCache);
	s->Phone.Data.Priv.N6510.FilesCache = NULL;
	return ERR_NONE;
}

static GSM_Error N6510_ReplyGetMemory(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	smprintf(s, "Phonebook entry received\n");
	if (msg->Buffer[6] == 0x0f)
		return N71_65_ReplyGetMemoryError(msg->Buffer[10], s);

	if (msg->Length < 22) {
		return ERR_UNKNOWN;
	}
	return N71_65_DecodePhonebook(s,
			s->Phone.Data.Memory,
			s->Phone.Data.Bitmap,
			s->Phone.Data.SpeedDial,
			msg->Buffer + 22,
			msg->Length - 22,
			FALSE);
}

static GSM_Error N6510_GetMemory (GSM_StateMachine *s, GSM_MemoryEntry *entry)
{
	GSM_Error error;
	unsigned char req[] = {N6110_FRAME_HEADER, 0x07, 0x01, 0x01, 0x00, 0x01,
			       0xfe, 0x10, 	/* memory type */
			       0x00, 0x00, 0x00, 0x00,
			       0x00, 0x01, 	/* location */
			       0x00, 0x00, 0x01};

	req[9] = NOKIA_GetMemoryType(s, entry->MemoryType,N71_65_MEMORY_TYPES);
	if (req[9]==0xff) return ERR_NOTSUPPORTED;

	if (entry->Location==0x00) return ERR_INVALIDLOCATION;

	req[14] = entry->Location / 256;
	req[15] = entry->Location % 256;

	s->Phone.Data.Memory=entry;
	smprintf(s, "Getting phonebook entry\n");
	error = GSM_WaitFor (s, req, 19, 0x03, s->Phone.Data.Priv.N6510.Timeout, ID_GetMemory);
	if (error == ERR_WORKINPROGRESS) {
		sleep(2);
		smprintf(s, "Retrying to get phonebook entry\n");
		error = GSM_WaitFor (s, req, 19, 0x03, s->Phone.Data.Priv.N6510.Timeout, ID_GetMemory);
	}
	if (entry->MemoryType == MEM_DC || entry->MemoryType == MEM_RC || entry->MemoryType == MEM_MC) {
		/* 6111 */
		if (error == ERR_NOTSUPPORTED) return ERR_EMPTY;
	}
	return error;
}

static GSM_Error N6510_ReplyGetMemoryStatus(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	smprintf(s, "Memory status received\n");
	/* Quess ;-)) */
	if (msg->Buffer[14]==0x10) {
		Data->MemoryStatus->MemoryFree = msg->Buffer[18]*256 + msg->Buffer[19];
		Data->MemoryStatus->MemoryUsed = msg->Buffer[20]*256 + msg->Buffer[21];
	} else if (msg->Buffer[14]==0x02) {
		Data->MemoryStatus->MemoryFree = msg->Buffer[22];
		Data->MemoryStatus->MemoryUsed = msg->Buffer[21];
	} else {
		Data->MemoryStatus->MemoryFree = msg->Buffer[17];
		Data->MemoryStatus->MemoryUsed = msg->Buffer[20]*256 + msg->Buffer[21];
	}
	smprintf(s, "Size       : %i\n",Data->MemoryStatus->MemoryFree);
	smprintf(s, "Used       : %i\n",Data->MemoryStatus->MemoryUsed);
	Data->MemoryStatus->MemoryFree -= Data->MemoryStatus->MemoryUsed;
	smprintf(s, "Free       : %i\n",Data->MemoryStatus->MemoryFree);
	return ERR_NONE;
}

static GSM_Error N6510_GetMemoryStatus(GSM_StateMachine *s, GSM_MemoryStatus *Status)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x03, 0x02,
			       0x00,		/* memory type */
			       0x55, 0x55, 0x55, 0x00};

	req[5] = NOKIA_GetMemoryType(s, Status->MemoryType,N71_65_MEMORY_TYPES);
	if (req[5]==0xff) return ERR_NOTSUPPORTED;

	s->Phone.Data.MemoryStatus=Status;
	smprintf(s, "Getting memory status\n");
	return GSM_WaitFor (s, req, 10, 0x03, s->Phone.Data.Priv.N6510.Timeout, ID_GetMemoryStatus);
}

static GSM_Error N6510_ReplyGetSMSC(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	int 			i, current, j;
	GSM_Phone_Data		*Data = &s->Phone.Data;
	size_t pos;
	GSM_Error error;

	switch (msg->Buffer[4]) {
		case 0x00:
			smprintf(s, "SMSC received\n");
			break;
		case 0x02:
			smprintf(s, "SMSC empty\n");
			return ERR_INVALIDLOCATION;
		case 0x09:
			smprintf(s, "SMSC empty???\n");
			return ERR_INVALIDLOCATION;
		default:
			smprintf(s, "Unknown SMSC state: %02x\n",msg->Buffer[4]);
			return ERR_UNKNOWNRESPONSE;
	}
	memset(Data->SMSC,0,sizeof(GSM_SMSC));
	Data->SMSC->Location 	= msg->Buffer[8];
	Data->SMSC->Format 	= SMS_FORMAT_Text;
	switch (msg->Buffer[10]) {
		case 0x00: Data->SMSC->Format = SMS_FORMAT_Text; 	break;
		case 0x22: Data->SMSC->Format = SMS_FORMAT_Fax; 	break;
		case 0x26: Data->SMSC->Format = SMS_FORMAT_Pager;	break;
		case 0x32: Data->SMSC->Format = SMS_FORMAT_Email;	break;
	}
	Data->SMSC->Validity.Format	= SMS_Validity_RelativeFormat;
	Data->SMSC->Validity.Relative	= msg->Buffer[12];
	if (msg->Buffer[12] == 0x00) Data->SMSC->Validity.Relative = SMS_VALID_Max_Time;

	current = 14;
	for (i=0;i<msg->Buffer[13];i++) {
		switch (msg->Buffer[current]) {
		case 0x81:
			j=current+4;
			while (msg->Buffer[j]!=0) {j++;}
			j=j-33;
			if (j>GSM_MAX_SMSC_NAME_LENGTH) {
				smprintf(s, "Too long name\n");
				return ERR_UNKNOWNRESPONSE;
			}
			CopyUnicodeString(Data->SMSC->Name,msg->Buffer+current+4);
			smprintf(s, "   Name \"%s\"\n", DecodeUnicodeString(Data->SMSC->Name));
			break;
		case 0x82:
			switch (msg->Buffer[current+2]) {
			case 0x01:
				pos = current + 4;
				error = GSM_UnpackSemiOctetNumber(&(s->di), Data->SMSC->DefaultNumber, msg->Buffer, &pos, msg->Length, TRUE);
				if (error != ERR_NONE) {
					return error;
				}
				smprintf(s, "   Default number \"%s\"\n", DecodeUnicodeString(Data->SMSC->DefaultNumber));
				break;
			case 0x02:
				pos = current + 4;
				error = GSM_UnpackSemiOctetNumber(&(s->di), Data->SMSC->Number, msg->Buffer, &pos, msg->Length, FALSE);
				if (error != ERR_NONE) {
					return error;
				}
				smprintf(s, "   Number \"%s\"\n", DecodeUnicodeString(Data->SMSC->Number));
				break;
			default:
				smprintf(s, "Unknown SMSC number: %02x\n",msg->Buffer[current+2]);
				return ERR_UNKNOWNRESPONSE;
			}
			break;
		default:
			smprintf(s, "Unknown SMSC block: %02x\n",msg->Buffer[current]);
			return ERR_UNKNOWNRESPONSE;
		}
		current = current + msg->Buffer[current+1];
	}
	return ERR_NONE;
}

static GSM_Error N6510_GetSMSC(GSM_StateMachine *s, GSM_SMSC *smsc)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x14,
			       0x01,		/* location */
			       0x00};

	if (smsc->Location==0x00) return ERR_INVALIDLOCATION;

	req[4]=smsc->Location;

	s->Phone.Data.SMSC=smsc;
	smprintf(s, "Getting SMSC\n");
	return GSM_WaitFor (s, req, 6, 0x02, s->Phone.Data.Priv.N6510.Timeout, ID_GetSMSC);
}

static GSM_Error N6510_ReplySetSMSC(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[4]) {
		case 0x00:
			smprintf(s, "SMSC set OK\n");
			return ERR_NONE;
		case 0x02:
			smprintf(s, "Invalid SMSC location\n");
			return ERR_INVALIDLOCATION;
		default:
			smprintf(s, "Unknown SMSC state: %02x\n",msg->Buffer[4]);
			return ERR_UNKNOWNRESPONSE;
	}
}

static GSM_Error N6510_SetSMSC(GSM_StateMachine *s, GSM_SMSC *smsc)
{
	int 		count = 13,i;
	unsigned char 	req[256] = {N6110_FRAME_HEADER,
		    		    0x12, 0x55, 0x01, 0x0B, 0x34,
				    0x05,		/* Location 	*/
				    0x00,
				    0x00,		/* Format 	*/
				    0x00,
				    0xFF};		/* Validity	*/

	req[8]  = smsc->Location;
	switch (smsc->Format) {
		case SMS_FORMAT_Text:	req[10] = 0x00;	break;
		case SMS_FORMAT_Fax:	req[10] = 0x22;	break;
		case SMS_FORMAT_Pager:	req[10] = 0x26;	break;
		case SMS_FORMAT_Email:	req[10] = 0x32;	break;
	}
	req[12]  = smsc->Validity.Relative;

	/* We have now blocks. Number of blocks = 3 */
	req[count++] 	 	 = 0x03;

	/* -------------- SMSC number ----------------- */
	/* Block type: number */
	req[count++] 		 = 0x82;
	/* Offset to next block */
	req[count++] 		 = 0x1A;
 	/* Type of number: SMSC number */
	req[count++] 		 = 0x02;
	req[count] = GSM_PackSemiOctetNumber(smsc->Number, req+count+2, FALSE) + 1;
	if (req[count]>18) {
		smprintf(s, "Too long SMSC number in frame\n");
		return ERR_UNKNOWN;
	}
	req[count+1] = req[count] - 1;
	count += 23;

	/* --------------- Default number ------------- */
	/* Block type: number */
	req[count++] 		 = 0x82;
	/* Offset to next block */
	req[count++] 		 = 0x14;
 	/* Type of number: default number */
	req[count++] 		 = 0x01;
	req[count] = GSM_PackSemiOctetNumber(smsc->DefaultNumber, req+count+2, TRUE) + 1;
	if (req[count]*2>12) {
		smprintf(s, "Too long SMSC number in frame\n");
		return ERR_UNKNOWN;
	}
	req[count+1] = req[count] - 1;
	count += 17;

	/* -------------- SMSC name ------------------- */
	req[count++] 		 = 0x81;
	req[count++] 		 = UnicodeLength(smsc->Name)*2 + 2 + 4;
	req[count++] 		 = UnicodeLength(smsc->Name)*2 + 2;
	req[count++] 		 = 0x00;
	/* Can't make CopyUnicodeString(req+count,sms->Name) !!!!
	 * with MSVC6 count is changed then
	 */
	i = count;
	CopyUnicodeString(req+i,smsc->Name);
	count += UnicodeLength(smsc->Name)*2 + 2;

	smprintf(s, "Setting SMSC\n");
	return GSM_WaitFor (s, req, count, 0x02, s->Phone.Data.Priv.N6510.Timeout, ID_SetSMSC);
}

static GSM_Error N6510_ReplyGetNetworkInfo(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	int		current = msg->Buffer[7]+7, pos;
	GSM_Phone_Data	*Data = &s->Phone.Data;
	GSM_NetworkInfo network_info_local, *network_info;

	if (msg->Buffer[3] == 0xf0) {
		return ERR_NOTSUPPORTED;
	}

	if (Data->RequestID == ID_GetNetworkInfo) {
		network_info = Data->NetworkInfo;
	} else {
		network_info = &network_info_local;
	}

	network_info->NetworkName[0] = 0x00;
	network_info->NetworkName[1] = 0x00;

	switch (msg->Buffer[8]) {
		case 0x00:
			smprintf(s, "home network\n");
			network_info->State = GSM_HomeNetwork;
			break;
		case 0x01:
			smprintf(s, "roaming network\n");
			network_info->State = GSM_RoamingNetwork;
			break;
		case 0x04:
			smprintf(s, "not logged");
			network_info->State = GSM_NoNetwork;
			break;
		case 0x06:
			smprintf(s, "SIM card rejected\n");
			network_info->State = GSM_RegistrationDenied;
			break;
		case 0x09:
			smprintf(s, "not logged");
			network_info->State = GSM_NoNetwork;
			break;
		default:
			smprintf(s, "unknown %i!\n",msg->Buffer[8]);
			network_info->State = GSM_NetworkStatusUnknown;
	}
	if (network_info->State == GSM_HomeNetwork || network_info->State == GSM_RoamingNetwork) {
		pos = 10;
		NOKIA_GetUnicodeString(s, &pos, msg->Buffer, network_info->NetworkName, TRUE);
		smprintf(s, "Network name: %s ", DecodeUnicodeString(network_info->NetworkName));
		NOKIA_DecodeNetworkCode(msg->Buffer + (current + 7), network_info->NetworkCode);
		smprintf(s, "Network code: %s\n", network_info->NetworkCode);
		smprintf(s, "Network name in libGammu: %s ", DecodeUnicodeString(GSM_GetNetworkName(network_info->NetworkCode)));
		sprintf(network_info->LAC, "%02X%02X", msg->Buffer[current+1], msg->Buffer[current+2]);
		smprintf(s, "LAC: %s\n", network_info->LAC);
		sprintf(network_info->CID, "%02X%02X", msg->Buffer[current+5], msg->Buffer[current+6]);
		smprintf(s, "CID: %s\n", network_info->CID);
	}
	return ERR_NONE;
}

static GSM_Error N6510_GetNetworkInfo(GSM_StateMachine *s, GSM_NetworkInfo *netinfo)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x00, 0x00};

	netinfo->GPRS = 0;
	s->Phone.Data.NetworkInfo=netinfo;
	smprintf(s, "Getting network info\n");
	return GSM_WaitFor (s, req, 5, 0x0a, s->Phone.Data.Priv.N6510.Timeout, ID_GetNetworkInfo);
}

static GSM_Error N6510_EncodeSMSFrame(GSM_StateMachine *s, GSM_SMSMessage *sms, unsigned char *req, GSM_SMSMessageLayout *Layout, int *length)
{
	int			count = 0, pos1, pos2, pos3, pos4, pos5;
	GSM_Error		error;

	memset(Layout,255,sizeof(GSM_SMSMessageLayout));

	req[count++]		 = 0x01;
	if (sms->PDU != SMS_Deliver) {
		req[count++] 	 = 0x02;
	} else {
		req[count++] 	 = 0x00;
	}

	pos1		  	 = count; count++;
	/* firstbyte set in SMS Layout */
	Layout->firstbyte 	 = count; count++;
	if (sms->PDU != SMS_Deliver) {
		Layout->TPMR 	 = count; count++;

		Layout->TPPID	 = count; count++;

		/* TP.DCS set in SMS layout */
		Layout->TPDCS 	 = count; count++;
                req[count++] 	 = 0x00;
	} else {
		Layout->TPPID 	 = count; count++;
		/* TP.DCS set in SMS layout */
		Layout->TPDCS 	 = count; count++;
		Layout->DateTime = count; count += 7;
		req[count++] 	 = 0x55;
		req[count++] 	 = 0x55;
		req[count++] 	 = 0x55;
	}

	/* We have now blocks. Number of blocks = 3 or 4 */
	if (sms->PDU != SMS_Deliver) {
		req[count++] 	 = 0x04;
	} else {
		req[count++] 	 = 0x03;
	}

	/* -------------- Phone number ------------- */
	/* Block type: number */
	req[count++] 		 = 0x82;
	/* Offset to next block */
	req[count++] 		 = 0x10;
 	/* Type of number: default number */
	req[count++] 		 = 0x01;
	pos4 			 = count; count++;
 	/* now coded Number in SMS Layout */
	Layout->Number 		 = count; count+= 12;

	/* -------------- SMSC number -------------- */
	/* Block type: number */
	req[count++] 		 = 0x82;
	/* Offset to next block */
	req[count++] 		 = 0x10;
 	/* Type of number: SMSC number */
	req[count++] 		 = 0x02;
	pos5 			 = count; count++;
	/* now coded SMSC number in SMS Layout */
	Layout->SMSCNumber 	 = count; count += 12;

	/* -------------- SMS validity ------------- */
	if (sms->PDU != SMS_Deliver) {
 		/* Block type: validity */
		req[count++] 	 = 0x08;
		req[count++] 	 = 0x04;
 		/* data length */
		req[count++] 	 = 0x01;
		Layout->TPVP 	 = count; count++;
	}

	/* --------------- SMS text ---------------- */
	/* Block type: SMS text */
	req[count++] 		 = 0x80;
	/* this the same as req[11] but starting from req[42] */
	pos2			 = count; count++;
	pos3			 = count; count++;
 	/* FIXME*/
	Layout->TPUDL 		 = count; count++;
	/* SMS text and UDH coded in SMS Layout */
	Layout->Text 		 = count;

	error = PHONE_EncodeSMSFrame(s,sms,req,*Layout,length,FALSE);
	if (error != ERR_NONE) return error;

	req[pos1] 		 = *length - 1;
	req[pos2] 		 = *length - Layout->Text + 6;
	req[pos3] 		 = *length - Layout->Text;

	/* Convert number of semioctets to number of chars */
	req[pos4]		 = req[Layout->Number] + 4;
	if (req[pos4] % 2) req[pos4]++;
	req[pos4] /= 2;

	req[pos5]		 = req[Layout->SMSCNumber] + 1;

	if (req[pos4]>12 || req[pos5]>12) {
		smprintf(s, "Too long phone number in frame\n");
		return ERR_UNKNOWN;
	}

	return ERR_NONE;
}

static GSM_Error N6510_ReplyGetSMSFolders(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	int 			j, num = 0;
	size_t pos;
	GSM_Phone_Data		*Data = &s->Phone.Data;

	switch (msg->Buffer[3]) {
	case 0x13:
		smprintf(s, "SMS folders names received\n");
		Data->SMSFolders->Number = msg->Buffer[5]+2;
		pos 			 = 6;
		for (j=0;j<msg->Buffer[5];j++) {
			while (TRUE) {
				if (msg->Buffer[pos]   == msg->Buffer[6] &&
				    msg->Buffer[pos+1] == msg->Buffer[7]) break;
				if (pos+4 > msg->Length) return ERR_UNKNOWNRESPONSE;
				pos++;
			}
			pos+=4;
			smprintf(s, "Folder index: %02x",msg->Buffer[pos - 2]);
			if (msg->Buffer[pos - 1]>GSM_MAX_SMS_FOLDER_NAME_LEN) {
				smprintf(s, "Too long text\n");
				return ERR_UNKNOWNRESPONSE;
			}
			CopyUnicodeString(Data->SMSFolders->Folder[num].Name,msg->Buffer + pos);
			smprintf(s, ", folder name: \"%s\"\n",DecodeUnicodeString(Data->SMSFolders->Folder[num].Name));
			Data->SMSFolders->Folder[num].InboxFolder = FALSE;
			Data->SMSFolders->Folder[num].Memory 	  = MEM_ME;
			if (num == 0x01) { /* OUTBOX SIM */
				Data->SMSFolders->Folder[0].Memory 	= MEM_SM;
				Data->SMSFolders->Folder[0].InboxFolder = TRUE;
				Data->SMSFolders->Folder[0].OutboxFolder = FALSE;

				Data->SMSFolders->Folder[1].Memory 	= MEM_SM;
				Data->SMSFolders->Folder[1].InboxFolder = FALSE;
				Data->SMSFolders->Folder[1].OutboxFolder = TRUE;

				CopyUnicodeString(Data->SMSFolders->Folder[2].Name,Data->SMSFolders->Folder[0].Name);
				Data->SMSFolders->Folder[2].Memory 	= MEM_ME;
				Data->SMSFolders->Folder[2].InboxFolder = TRUE;
				Data->SMSFolders->Folder[2].OutboxFolder = FALSE;

				CopyUnicodeString(Data->SMSFolders->Folder[3].Name,Data->SMSFolders->Folder[1].Name);
				Data->SMSFolders->Folder[3].Memory 	= MEM_ME;
				Data->SMSFolders->Folder[3].InboxFolder = FALSE;
				Data->SMSFolders->Folder[3].OutboxFolder = TRUE;

				num+=2;
			}
			num++;
		}
		return ERR_NONE;
	case 0xf0:
		smprintf(s, "HINT: Maybe phone needs F_SMS_FILES?\n");
		break;
	}
	return ERR_UNKNOWNRESPONSE;
}

GSM_Error N6510_GetSMSFoldersS40_30(GSM_StateMachine *s UNUSED, GSM_SMSFolders *folders)
{
	folders->Number=4;
	EncodeUnicode(folders->Folder[0].Name,_("SIM"),strlen(_("SIM")));
	EncodeUnicode(folders->Folder[1].Name,_("Inbox"),strlen(_("Inbox")));
	EncodeUnicode(folders->Folder[2].Name,_("Sent items"),strlen(_("Sent items")));
	EncodeUnicode(folders->Folder[3].Name,_("Saved items"),strlen(_("Saved items")));
	folders->Folder[0].InboxFolder = TRUE;
	folders->Folder[1].InboxFolder = TRUE;
	folders->Folder[2].InboxFolder = FALSE;
	folders->Folder[3].InboxFolder = FALSE;
	folders->Folder[0].OutboxFolder = TRUE;
	folders->Folder[1].OutboxFolder = FALSE;
	folders->Folder[2].OutboxFolder = TRUE;
	folders->Folder[3].OutboxFolder = FALSE;
	folders->Folder[0].Memory      = MEM_SM;
	folders->Folder[1].Memory      = MEM_ME;
	folders->Folder[2].Memory      = MEM_ME;
	folders->Folder[3].Memory      = MEM_ME;
	return ERR_NONE;
}

static GSM_Error N6510_GetSMSFolders(GSM_StateMachine *s, GSM_SMSFolders *folders)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x12, 0x00, 0x00};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SMS_FILES)) return N6510_GetFilesystemSMSFolders(s,folders);
		return N6510_GetSMSFoldersS40_30(s,folders);
	}

	s->Phone.Data.SMSFolders=folders;
	smprintf(s, "Getting SMS folders\n");
	return GSM_WaitFor (s, req, 6, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_GetSMSFolders);
}

static GSM_Error N6510_ReplyGetSMSFolderStatus(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	int			i;
	GSM_Phone_N6510Data	*Priv = &s->Phone.Data.Priv.N6510;

	smprintf(s, "SMS folder status received\n");
	Priv->LastSMSFolder.Number=msg->Buffer[6]*256+msg->Buffer[7];
	smprintf(s, "Number of Entries: %i\n",Priv->LastSMSFolder.Number);
	smprintf(s, "Locations: ");
	for (i=0;i<Priv->LastSMSFolder.Number;i++) {
		Priv->LastSMSFolder.Location[i]=msg->Buffer[8+(i*2)]*256+msg->Buffer[(i*2)+9];
		smprintf(s, "%i ",Priv->LastSMSFolder.Location[i]);
	}
	smprintf(s, "\n");
	NOKIA_SortSMSFolderStatus(s, &Priv->LastSMSFolder);
	return ERR_NONE;
}

static GSM_Error N6510_GetSMSFolderStatus(GSM_StateMachine *s, int folderid)
{
	unsigned char req[] = {N7110_FRAME_HEADER, 0x0C,
			       0x01,		/* 0x01=SIM, 0x02=ME 	*/
			       0x00,		/* Folder ID		*/
			       0x0f, 0x55, 0x55, 0x55};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		switch (folderid) {
			case 0x01: req[5] = 0x01; 			break; /* SIM 		*/
			default	 : req[5] = folderid; req[4] = 0x02; 	break; /* ME folders	*/
		}
	} else {
		switch (folderid) {
			case 0x01: req[5] = 0x02; 			 break; /* INBOX SIM 	*/
			case 0x02: req[5] = 0x03; 			 break; /* OUTBOX SIM 	*/
			default	 : req[5] = folderid - 1; req[4] = 0x02; break; /* ME folders	*/
		}
	}

	smprintf(s, "Getting SMS folder status\n");
	return GSM_WaitFor (s, req, 10, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_GetSMSFolderStatus);
}

static void N6510_GetSMSLocation(GSM_StateMachine *s, GSM_SMSMessage *sms, unsigned char *folderid, unsigned int *location)
{
	int ifolderid;

	/* simulate flat SMS memory */
	if (sms->Folder==0x00) {
		ifolderid = sms->Location / GSM_PHONE_MAXSMSINFOLDER;
		*folderid = ifolderid + 0x01;
		*location = sms->Location - ifolderid * GSM_PHONE_MAXSMSINFOLDER;
		if (*folderid == 0x1B) (*folderid)=0x99; /* 0x1A is Outbox in 6230i */
	} else {
		*folderid = sms->Folder;
		*location = sms->Location;
		if (*folderid == 0x1A) (*folderid)=0x99; /* 0x1A is Outbox in 6230i */
	}
	smprintf(s, "SMS folder %i & location %i -> 6510 folder %i & location %i\n",
		sms->Folder,sms->Location,*folderid,*location);
}

static void N6510_SetSMSLocation(GSM_StateMachine *s, GSM_SMSMessage *sms, unsigned char folderid, int location)
{
	sms->Folder	= 0;
	sms->Location	= (folderid - 0x01) * GSM_PHONE_MAXSMSINFOLDER + location;
	smprintf(s, "6510 folder %i & location %i -> SMS folder %i & location %i\n",
		folderid,location,sms->Folder,sms->Location);
}

static GSM_Error N6510_DecodeSMSFrame(GSM_StateMachine *s, GSM_SMSMessage *sms, unsigned char *buffer, size_t *current2)
{
	int 			i, current, blocks=0, SMSTemplateDateTime = 0;
	GSM_SMSMessageLayout 	Layout;
	GSM_Error		error;

	memset(&Layout,255,sizeof(GSM_SMSMessageLayout));
	Layout.firstbyte = 2;
	switch (buffer[0]) {
	case 0x00:
		smprintf(s, "SMS deliver\n");
		sms->PDU 	= SMS_Deliver;
		Layout.TPPID 	= 3;
		Layout.TPDCS 	= 4;
		Layout.DateTime = 5;
		blocks 		= 15;
		break;
	case 0x01:
		smprintf(s, "Delivery report\n");
		sms->PDU = SMS_Status_Report;
		Layout.TPMR	= 3;
		Layout.TPStatus	= 4;
		Layout.DateTime = 5;
		Layout.SMSCTime = 12;
		blocks 		= 19;
		break;
	case 0x02:
		smprintf(s, "SMS template\n");
		sms->PDU 	= SMS_Submit;
		Layout.TPMR	= 3;
		Layout.TPPID 	= 4;
		Layout.TPDCS 	= 5;
		blocks 		= 7;
		break;
	}
	current = blocks + 1;
	for (i=0;i<buffer[blocks];i++) {
		switch (buffer[current]) {
		case 0x08:
			smprintf(s, "SMSC timestamp (ignored)\n");
			break;
		case 0x80:
			smprintf(s, "SMS text\n");
			if (buffer[current + 2] > buffer[current + 3]) {
				Layout.TPUDL 	= current + 2;
			} else {
				Layout.TPUDL 	= current + 3;
			}
			Layout.Text 		= current + 4;
			break;
		case 0x82:
			switch (buffer[current+2]) {
			case 0x01:
				smprintf(s, "Phone number\n");
				Layout.Number = current + 4;
				break;
			case 0x02:
				smprintf(s, "SMSC number\n");
				Layout.SMSCNumber = current + 4;
				break;
			default:
				smprintf(s, "Unknown number\n");
				break;
			}
			break;
		case 0x84:
			smprintf(s, "Date and time of saving for SMS template\n");
			SMSTemplateDateTime = current + 2;
			break;
		default:
			smprintf(s, "Unknown block %02x\n",buffer[current]);
		}
		current = current + buffer[current + 1];
	}
	error = GSM_DecodeSMSFrame(&(s->di), sms,buffer,Layout);
	if (SMSTemplateDateTime != 0) {
		sms->PDU = SMS_Deliver;
		NOKIA_DecodeDateTime(s, buffer+SMSTemplateDateTime, &sms->DateTime, TRUE, FALSE);
	}
	(*current2) = current;
	return error;
}

GSM_Error N6510_ReplyGetSMSMessage(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	size_t			i,j;
	size_t			Width, Height;
	unsigned char		output[500]; /* output2[500]; */
	GSM_Phone_Data		*Data = &s->Phone.Data;
	GSM_Error		error;

	switch(msg->Buffer[3]) {
	case 0x03:
		smprintf(s, "SMS Message received\n");
		GSM_SetDefaultReceivedSMSData(Data->GetSMSMessage->SMS);
		Data->GetSMSMessage->Number=1;
		NOKIA_DecodeSMSState(s, msg->Buffer[5], &(Data->GetSMSMessage->SMS[0]));
		if (msg->Length < 15) {
			smprintf(s, "No message data!\n");
			Data->GetSMSMessage->SMS[0].PDU = SMS_Deliver;
			return ERR_NONE;
		}
		switch (msg->Buffer[14]) {
		case 0x00:
		case 0x01:
		case 0x02:
			if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
				Data->GetSMSMessage->Number=0;
				i = 14;
				while (TRUE) {
					error = N6510_DecodeSMSFrame(s, &Data->GetSMSMessage->SMS[Data->GetSMSMessage->Number],msg->Buffer+i,&j);
					if (error != ERR_NONE) return error;
					NOKIA_DecodeSMSState(s, msg->Buffer[5], &Data->GetSMSMessage->SMS[Data->GetSMSMessage->Number]);
					i+=j;
					Data->GetSMSMessage->Number++;
					if (i>=msg->Length) break;
				}
				return error;
			} else {
				return N6510_DecodeSMSFrame(s, &Data->GetSMSMessage->SMS[0],msg->Buffer+14,&j);
			}
		case 0xA0:
			smprintf(s, "Picture Image\n");
			Data->GetSMSMessage->Number = 0;
			i = 0;
			output[i++] = 0x30;	 /* Smart Messaging 3.0 */
			output[i++] = SM30_OTA;
			output[i++] = 0x01;	 /* Length */
			output[i++] = 0x00;	 /* Length */
			output[i++] = 0x00;
			PHONE_GetBitmapWidthHeight(GSM_NokiaPictureImage, &Width, &Height);
			output[i++] = Width;
			output[i++] = Height;
			output[i++] = 0x01;
			memcpy(output+i,msg->Buffer+30,PHONE_GetBitmapSize(GSM_NokiaPictureImage,0,0));
			i = i + PHONE_GetBitmapSize(GSM_NokiaPictureImage,0,0);
#if 0
 			if (msg->Length!=282) {
 				output[i++] = SM30_UNICODETEXT;
 				output[i++] = 0;
 				output[i++] = 0; /* Length - later changed */
 				GSM_UnpackEightBitsToSeven(0, msg->Length-282, msg->Length-304, msg->Buffer+282,output2);
 				DecodeDefault(output+i, output2, msg->Length - 282, TRUE, NULL);
 				output[i - 1] = UnicodeLength(output+i) * 2;
 				i = i + output[i-1];
 			}
#endif
			GSM_MakeMultiPartSMS(&(s->di), Data->GetSMSMessage,output,i,UDH_NokiaProfileLong,SMS_Coding_8bit,1,0);
			for (i=0;i<3;i++) {
                		Data->GetSMSMessage->SMS[i].Number[0]=0;
                		Data->GetSMSMessage->SMS[i].Number[1]=0;
			}
			if (Data->Bitmap != NULL) {
				Data->Bitmap->Location	= 0;
				PHONE_GetBitmapWidthHeight(GSM_NokiaPictureImage, &Width, &Height);
				Data->Bitmap->BitmapWidth	= Width;
				Data->Bitmap->BitmapHeight	= Height;
				PHONE_DecodeBitmap(GSM_NokiaPictureImage, msg->Buffer + 30, Data->Bitmap);
				Data->Bitmap->Sender[0] = 0x00;
				Data->Bitmap->Sender[1] = 0x00;
				Data->Bitmap->Text[0] = 0;
				Data->Bitmap->Text[1] = 0;
			}
			return ERR_NONE;
		default:
			smprintf(s, "Unknown SMS type: %i\n",msg->Buffer[8]);
		}
		break;
	case 0x0f:
		smprintf(s, "SMS message info received\n");
		CopyUnicodeString(Data->GetSMSMessage->SMS[0].Name,msg->Buffer+52);
		smprintf(s, "Name: \"%s\"\n",DecodeUnicodeString(Data->GetSMSMessage->SMS[0].Name));
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_PrivGetSMSMessageBitmap(GSM_StateMachine *s, GSM_MultiSMSMessage *sms, GSM_Bitmap *bitmap)
{
	GSM_Error		error;
	unsigned char		folderid,namebuffer[200];
	unsigned int		location;
	int			i;
	unsigned char req[] = {
		N6110_FRAME_HEADER,
		0x02,		/* msg type: 0x02 for getting sms, 0x0e for sms status */
		0x01,		/* 0x01=SIM, 0x02=ME 	*/
		0x00, 		/* FolderID */
		0x00, 0x02,	/* Location */
		0x01, 0x00};

	N6510_GetSMSLocation(s, &sms->SMS[0], &folderid, &location);

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		switch (folderid) {
			case 0x01: req[5] = 0x01; 			 break; /* SIM 		*/
			default	 : req[5] = folderid; req[4] = 0x02; 	 break; /* ME folders	*/
		}
	} else {
		switch (folderid) {
			case 0x01: req[5] = 0x02; 			 break; /* INBOX SIM 	*/
			case 0x02: req[5] = 0x03; 			 break; /* OUTBOX SIM 	*/
			default	 : req[5] = folderid - 1; req[4] = 0x02; break; /* ME folders	*/
		}
	}

	req[6]=location / 256;
	req[7]=location % 256;

	s->Phone.Data.GetSMSMessage 	= sms;
	s->Phone.Data.Bitmap 		= bitmap;
	smprintf(s, "Getting sms message info\n");
	req[3] = 0x0e; req[8] = 0x55; req[9] = 0x55;
	error=GSM_WaitFor (s, req, 10, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_GetSMSMessage);
	if (error!=ERR_NONE) return error;
	CopyUnicodeString(namebuffer,sms->SMS[0].Name);

	smprintf(s, "Getting sms\n");
	req[3] = 0x02; req[8] = 0x01; req[9] = 0x00;
	error = GSM_WaitFor (s, req, 10, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_GetSMSMessage);
	if (error == ERR_NONE) {
		if (sms->Number == 0) {
			return ERR_EMPTY;
		}
		for (i=0;i<sms->Number;i++) {
			N6510_SetSMSLocation(s, &sms->SMS[i], folderid, location);
			sms->SMS[i].Folder 	= folderid;

			if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
				sms->SMS[i].InboxFolder = TRUE;
				if (folderid > 2) sms->SMS[i].InboxFolder = FALSE;
				sms->SMS[i].Memory = MEM_ME;
				if (folderid == 0x01) sms->SMS[i].Memory = MEM_SM;
			} else {
				sms->SMS[i].InboxFolder = TRUE;
				if (folderid != 0x01 && folderid != 0x03) sms->SMS[i].InboxFolder = FALSE;
				sms->SMS[i].Memory	= MEM_ME;
				if (folderid == 0x01 || folderid == 0x02) sms->SMS[i].Memory = MEM_SM;
			}

			CopyUnicodeString(sms->SMS[i].Name,namebuffer);
		}
	}
	return error;
}

static GSM_Error N6510_GetSMSMessage(GSM_StateMachine *s, GSM_MultiSMSMessage *sms)
{
	GSM_Error		error;
	unsigned char		folderid;
	unsigned int		location;
	GSM_Phone_N6510Data	*Priv = &s->Phone.Data.Priv.N6510;
	int			i;
	gboolean			found = FALSE;

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SMS_FILES)) return ERR_NOTSUPPORTED;
	}

	/* Clear SMS structure of any possible junk */
	GSM_SetDefaultReceivedSMSData(&(sms->SMS[0]));

	N6510_GetSMSLocation(s, &(sms->SMS[0]), &folderid, &location);
	error=N6510_GetSMSFolderStatus(s, folderid);
	if (error!=ERR_NONE) return error;
	for (i=0;i<Priv->LastSMSFolder.Number;i++) {
		if (Priv->LastSMSFolder.Location[i]==location) {
			found = TRUE;
			break;
		}
	}
	if (!found) return ERR_EMPTY;
	return N6510_PrivGetSMSMessageBitmap(s,sms,NULL);
}

static GSM_Error N6510_GetNextSMSMessageBitmap(GSM_StateMachine *s, GSM_MultiSMSMessage *sms, gboolean start, GSM_Bitmap *bitmap)
{
	GSM_Phone_N6510Data	*Priv = &s->Phone.Data.Priv.N6510;
	unsigned char		folderid;
	unsigned int		location;
	GSM_Error		error;
	int			i;
	gboolean			findnextfolder = FALSE;

	if (start) {
		folderid	= 0x00;
		findnextfolder	= TRUE;
		error=N6510_GetSMSFolders(s,&Priv->LastSMSFolders);
		if (error!=ERR_NONE) return error;
	} else {
		N6510_GetSMSLocation(s, &(sms->SMS[0]), &folderid, &location);
		for (i=0;i<Priv->LastSMSFolder.Number;i++) {
			if (Priv->LastSMSFolder.Location[i]==location) break;
		}
		/* Is this last location in this folder ? */
		if (i==Priv->LastSMSFolder.Number-1) {
			findnextfolder=TRUE;
		} else {
			location=Priv->LastSMSFolder.Location[i+1];
		}
	}
	if (findnextfolder) {
		Priv->LastSMSFolder.Number=0;
		while (Priv->LastSMSFolder.Number==0) {
			folderid++;
			/* Too high folder number */
			if ((folderid-1)>=Priv->LastSMSFolders.Number) return ERR_EMPTY;
			/* Get next folder status */
			error=N6510_GetSMSFolderStatus(s, folderid);
			if (error!=ERR_NONE) return error;
			/* First location from this folder */
			location=Priv->LastSMSFolder.Location[0];
		}
	}
	N6510_SetSMSLocation(s, &sms->SMS[0], folderid, location);

	return N6510_PrivGetSMSMessageBitmap(s, sms, bitmap);
}

static GSM_Error N6510_GetNextSMSMessage(GSM_StateMachine *s, GSM_MultiSMSMessage *sms, gboolean start)
{
	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30) &&
			GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SMS_FILES)) {
		return N6510_GetNextFilesystemSMS(s,sms,start);
	}

	return N6510_GetNextSMSMessageBitmap(s, sms, start, NULL);
}

static GSM_Error N6510_ReplyStartupNoteLogo(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	if (Data->RequestID == ID_GetBitmap) {
		switch (msg->Buffer[4]) {
		case 0x01:
			smprintf(s, "Welcome note text received\n");
			CopyUnicodeString(Data->Bitmap->Text,msg->Buffer+6);
			smprintf(s, "Text is \"%s\"\n",DecodeUnicodeString(Data->Bitmap->Text));
			return ERR_NONE;
		case 0x10:
			smprintf(s, "Dealer note text received\n");
			CopyUnicodeString(Data->Bitmap->Text,msg->Buffer+6);
			smprintf(s, "Text is \"%s\"\n",DecodeUnicodeString(Data->Bitmap->Text));
			return ERR_NONE;
		case 0x0f:
			smprintf(s, "Startup logo received\n");
			PHONE_DecodeBitmap(GSM_Nokia7110StartupLogo, msg->Buffer + 22, Data->Bitmap);
			return ERR_NONE;
		}
	}
	if (Data->RequestID == ID_SetBitmap) {
		switch (msg->Buffer[4]) {
			case 0x01:
			case 0x10:
			case 0x0f:
			case 0x25:
				return ERR_NONE;
		}
	}
	return ERR_UNKNOWN;
}

static GSM_Error N6510_GetPictureImage(GSM_StateMachine *s, GSM_Bitmap *Bitmap, int *location)
{
	GSM_MultiSMSMessage 	sms;
	int			Number;
	GSM_Bitmap		bitmap;
	GSM_Error		error;

	sms.SMS[0].Folder	= 0;
	Number			= 0;
	bitmap.Location		= 255;
	error=N6510_GetNextSMSMessageBitmap(s, &sms, TRUE, &bitmap);
	while (error == ERR_NONE) {
		if (bitmap.Location != 255) {
			Number++;
			if (Number == Bitmap->Location) {
				bitmap.Location = Bitmap->Location;
				memcpy(Bitmap,&bitmap,sizeof(GSM_Bitmap));
				*location = sms.SMS[0].Location;
				return ERR_NONE;
			}
		}
		bitmap.Location		= 255;
		sms.SMS[0].Folder 	= 0;
		error=N6510_GetNextSMSMessageBitmap(s, &sms, FALSE, &bitmap);
	}
	return ERR_INVALIDLOCATION;
}

static GSM_Error N6510_GetBitmap(GSM_StateMachine *s, GSM_Bitmap *Bitmap)
{
	unsigned char reqOp	[] = {N6110_FRAME_HEADER, 0x23, 0x00, 0x00, 0x55, 0x55, 0x55};
	unsigned char reqStartup[] = {N6110_FRAME_HEADER, 0x02, 0x0f};
	unsigned char reqNote	[] = {N6110_FRAME_HEADER, 0x02, 0x01, 0x00};
	GSM_MemoryEntry	pbk;
	GSM_Error		error;
	int			Location;

	s->Phone.Data.Bitmap=Bitmap;
	switch (Bitmap->Type) {
	case GSM_StartupLogo:
		Bitmap->BitmapWidth  = 96;
		Bitmap->BitmapHeight = 65;
		GSM_ClearBitmap(Bitmap);
		smprintf(s, "Getting startup logo\n");
		return GSM_WaitFor (s, reqStartup, 5, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_GetBitmap);
	case GSM_DealerNote_Text:
		reqNote[4] = 0x10;
		smprintf(s, "Getting dealer note\n");
		return GSM_WaitFor (s, reqNote, 6, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_GetBitmap);
	case GSM_WelcomeNote_Text:
		smprintf(s, "Getting welcome note\n");
		return GSM_WaitFor (s, reqNote, 6, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_GetBitmap);
	case GSM_CallerGroupLogo:
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_PBK35)) return ERR_NOTSUPPORTED;
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iCALLER)) {
			pbk.MemoryType	= (GSM_MemoryType)MEM6510_CG2;
			pbk.Location	= Bitmap->Location;
			smprintf(s, "Getting caller group logo method 2\n");
			return N6510_GetMemory(s,&pbk);
		}
		Bitmap->BitmapWidth  	 = 72;
		Bitmap->BitmapHeight 	 = 14;
		GSM_ClearBitmap(Bitmap);
		pbk.MemoryType	= (GSM_MemoryType)MEM7110_CG;
		pbk.Location	= Bitmap->Location;
		smprintf(s, "Getting caller group logo\n");
		error=N6510_GetMemory(s,&pbk);
		if (error==ERR_NONE) NOKIA_GetDefaultCallerGroupName(Bitmap);
		return error;
	case GSM_OperatorLogo:
		smprintf(s, "Getting operator logo\n");
		return GSM_WaitFor (s, reqOp, 9, 0x0A, s->Phone.Data.Priv.N6510.Timeout, ID_GetBitmap);
	case GSM_PictureImage:
		return N6510_GetPictureImage(s, Bitmap, &Location);
	default:
		break;
	}
	return ERR_NOTSUPPORTED;
}

static GSM_Error N6510_ReplyGetIncSignalQuality(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	smprintf(s, "Network level changed to: %i\n",msg->Buffer[4]);
	return ERR_NONE;
}

static GSM_Error N6510_ReplyGetSignalQuality(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	smprintf(s, "Network level received: %i\n",msg->Buffer[8]);
    	Data->SignalQuality->SignalStrength 	= -1;
    	Data->SignalQuality->SignalPercent 	= ((int)msg->Buffer[8]);
    	Data->SignalQuality->BitErrorRate 	= -1;
	return ERR_NONE;
}

static GSM_Error N6510_GetSignalQuality(GSM_StateMachine *s, GSM_SignalQuality *sig)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x0B, 0x00, 0x02, 0x00, 0x00, 0x00};

	s->Phone.Data.SignalQuality = sig;
	smprintf(s, "Getting network level\n");
	return GSM_WaitFor (s, req, 9, 0x0a, s->Phone.Data.Priv.N6510.Timeout * 3, ID_GetSignalQuality);
}

static GSM_Error N6510_IncomingBatteryCharge(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	smprintf(s, "Incoming battery level received???: %i\n",
			msg->Buffer[9]*100/7);
	return ERR_NONE;
}

static GSM_Error N6510_ReplyGetBatteryCharge(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	Data->BatteryCharge->BatteryPercent 	= ((int)(msg->Buffer[9]*100/msg->Buffer[8]));
    	Data->BatteryCharge->ChargeState 	= 0;

	smprintf(s, "Battery level received: %i\n",
			Data->BatteryCharge->BatteryPercent);

	return ERR_NONE;
}

static GSM_Error N6510_GetBatteryCharge(GSM_StateMachine *s, GSM_BatteryCharge *bat)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x0A, 0x02, 0x00};

	GSM_ClearBatteryCharge(bat);
	s->Phone.Data.BatteryCharge = bat;
	smprintf(s, "Getting battery level\n");
	return GSM_WaitFor (s, req, 6, 0x17, s->Phone.Data.Priv.N6510.Timeout, ID_GetBatteryCharge);
}

static GSM_Error N6510_ReplyGetWAPBookmark(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	return DCT3DCT4_ReplyGetWAPBookmark (msg, s, TRUE);
}

static GSM_Error N6510_ReplyGetOperatorLogo(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	if (msg->Buffer[3] == 0xf0) return ERR_NOTSUPPORTED;

	smprintf(s, "Operator logo received\n");
	if (msg->Length <= 18) {
		smprintf(s, "Packet too short to contain operator logo\n");
		return ERR_EMPTY;
	}
	NOKIA_DecodeNetworkCode(msg->Buffer+12,Data->Bitmap->NetworkCode);
	smprintf(s, "Network code %s\n",Data->Bitmap->NetworkCode);
	Data->Bitmap->BitmapWidth	= msg->Buffer[20];
	Data->Bitmap->BitmapHeight	= msg->Buffer[21];
	PHONE_DecodeBitmap(GSM_Nokia6510OperatorLogo,msg->Buffer+26,Data->Bitmap);
	return ERR_NONE;
}

GSM_Error N6510_ReplyDeleteMemory(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	if (msg->Buffer[5] == 0x1) {
		switch (msg->Buffer[6]) {
		case 0x0f:
			switch (msg->Buffer[10]) {
			case 0x21:
				smprintf(s, "Still busy processing the last command\n");
				return ERR_BUSY;
			case 0x3B:
				smprintf(s, "Nothing to delete\n");
				return ERR_NONE;
			case 0x33:
				smprintf(s, "Entry is read only\n");
				return ERR_READ_ONLY;
	                default:
		                smprintf(s, "ERROR: unknown 0x%x\n", msg->Buffer[10]);
			        return ERR_UNKNOWNRESPONSE;
			}
		}
	}
	smprintf(s, "Phonebook entry deleted\n");
	return ERR_NONE;
}

GSM_Error N6510_DeleteMemory(GSM_StateMachine *s, GSM_MemoryEntry *entry)
{
	unsigned char req[] = {N7110_FRAME_HEADER, 0x0f, 0x55, 0x01,
			       0x04, 0x55, 0x00, 0x10, 0xFF, 0x02,
			       0x00, 0x01,		/* location	*/
			       0x00, 0x00, 0x00, 0x00,
			       0x05, 			/* memory type	*/
			       0x55, 0x55, 0x55};

	req[12] = entry->Location / 256;
	req[13] = entry->Location % 256;

	req[18] = NOKIA_GetMemoryType(s, entry->MemoryType,N71_65_MEMORY_TYPES);
	if (req[18]==0xff) return ERR_NOTSUPPORTED;

	smprintf(s, "Deleting phonebook entry\n");
	return GSM_WaitFor (s, req, 22, 0x03, s->Phone.Data.Priv.N6510.Timeout, ID_SetMemory);
}

static GSM_Error N6510_SetMemory(GSM_StateMachine *s, GSM_MemoryEntry *entry)
{
	int 		count = 22;
	size_t blocks;
	unsigned char 	req[5000] = {
		N7110_FRAME_HEADER, 0x0b, 0x00, 0x01, 0x01, 0x00, 0x00, 0x10, 0x02,
		0x00,  			/* memory type */
		0x00, 0x00,  		/* location */
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00};			/* Number of blocks */
	GSM_Error error;
	GSM_MemoryEntry tmp;
	GSM_MemoryStatus status;
	GSM_Phone_N6510Data *Priv = &s->Phone.Data.Priv.N6510;

	if (entry->Location == 0) {
		/*
		 * We want to remember last location so that we don't check the
		 * same place again and again.
		 */
		if (Priv->LastFreeMemoryType != entry->MemoryType) {
			Priv->LastFreeMemoryLocation = 0;
			Priv->LastFreeMemoryType = entry->MemoryType;
			status.MemoryType = entry->MemoryType;
			error = N6510_GetMemoryStatus(s, &status);
			if (error != ERR_NONE) return error;
			Priv->LastFreeMemorySize = status.MemoryUsed + status.MemoryFree;
		}

		/* Advance beyond last used location */
		tmp.MemoryType = entry->MemoryType;
		error = ERR_NONE;
		for (tmp.Location = Priv->LastFreeMemoryLocation + 1;
			tmp.Location < Priv->LastFreeMemorySize;
			tmp.Location++) {
			error = N6510_GetMemory(s, &tmp);
			if (error != ERR_NONE) break;
		}
		if (error == ERR_NONE) {
			/* Memory full */
			return ERR_FULL;
		} else if (error != ERR_EMPTY) {
			/* Other failure */
			return error;
		}
		/* We've got the location */
		entry->Location = tmp.Location;
		smprintf(s, "Found empty location: %d\n", entry->Location);
	}

	req[11] = NOKIA_GetMemoryType(s, entry->MemoryType,N71_65_MEMORY_TYPES);
	if (req[11]==0xff) return ERR_NOTSUPPORTED;

	req[12] = entry->Location / 256;
	req[13] = entry->Location % 256;

	count = count + N71_65_EncodePhonebookFrame(s, req+22, entry, &blocks, TRUE, GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_VOICETAGS));
	req[21] = blocks;

	smprintf(s, "Writing phonebook entry\n");
	return GSM_WaitFor (s, req, count, 0x03, s->Phone.Data.Priv.N6510.Timeout, ID_SetMemory);
}

static GSM_Error N6510_AddMemory(GSM_StateMachine *s, GSM_MemoryEntry *entry)
{
	entry->Location = 0;
	return N6510_SetMemory(s, entry);
}

static GSM_Error N6510_ReplySetOperatorLogo(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Operator logo set OK\n");
	return ERR_NONE;
}

static GSM_Error N6510_SetCallerLogo(GSM_StateMachine *s, GSM_Bitmap *bitmap)
{
	char		string[500];
	int		block=0, i;
	size_t Width, Height;
	unsigned int 	count = 22;
	unsigned char 	req[500] = {
		N6110_FRAME_HEADER, 0x0b, 0x00, 0x01, 0x01, 0x00, 0x00, 0x10,
		0xfe, 0x00,		/* memory type */
		0x00, 0x00,		/* location */
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

	/* Set memory type */
	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iCALLER)) {
		req[11] = MEM6510_CG2;
	} else {
		req[11] = MEM7110_CG;
	}

	req[13] = bitmap->Location;

	/* Enabling/disabling logo */
	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iCALLER)) {
		string[0] = bitmap->BitmapEnabled?1:0;
		string[1] = 0;
		count += N71_65_PackPBKBlock(s, N7110_PBK_LOGOON, 2, block++, string, req + count);
	}

	/* Ringtone */
	if (!bitmap->DefaultRingtone) {
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iCALLER)) {
			string[0] = 0x00;
			string[1] = 0x00;
			string[2] = 0x00;
			string[3] = 0x10;
			string[4] = 0x00;
			string[5] = 0x00;
			string[6] = bitmap->RingtoneID;
			string[7] = 0x00;
			string[8] = 0x00;
			string[9] = 0x00;
			count += N71_65_PackPBKBlock(s, N6510_PBK_RINGTONEFILE_ID, 10, block++, string, req + count);
			req[count - 1] = 0x01;
		} else if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_PBKTONEGAL)) {
			/* do nothing ? */
		} else {
			string[0] = 0x00;
			string[1] = 0x00;
			string[2] = bitmap->RingtoneID;
			count += N71_65_PackPBKBlock(s, N7110_PBK_RINGTONE_ID, 3, block++, string, req + count);
			count --;
			req[count-5] = 8;
		}
	}

	/* Number of group */
	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iCALLER)) {
		string[0] = bitmap->Location;
		string[1] = 0;
		count += N71_65_PackPBKBlock(s, N7110_PBK_GROUP, 2, block++, string, req + count);
	}

	/* Name */
	if (!bitmap->DefaultName) {
		i = UnicodeLength(bitmap->Text) * 2;
		string[0] = i + 2;
		memcpy(string + 1, bitmap->Text, i);
		string[i + 1] = 0;
		count += N71_65_PackPBKBlock(s, N7110_PBK_NAME, i + 2, block++, string, req + count);
	}

	/* Logo */
	if (!bitmap->DefaultBitmap) {
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iCALLER)) {
			/* write N6510_PBK_PICTURE_ID ? */
		} else {
			PHONE_GetBitmapWidthHeight(GSM_NokiaCallerLogo, &Width, &Height);
			string[0] = Width;
			string[1] = Height;
			string[2] = 0;
			string[3] = 0;
			string[4] = PHONE_GetBitmapSize(GSM_NokiaCallerLogo,0,0);
			PHONE_EncodeBitmap(GSM_NokiaCallerLogo, string + 5, bitmap);
			count += N71_65_PackPBKBlock(s, N7110_PBK_GROUPLOGO, PHONE_GetBitmapSize(GSM_NokiaCallerLogo,0,0) + 5, block++, string, req + count);
		}
	}

	req[21] = block;

	return GSM_WaitFor (s, req, count, 0x03, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
}

static GSM_Error N6510_ReplySetPicture(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	smprintf(s, "Picture Image written OK, folder %i, location %i\n",msg->Buffer[4],msg->Buffer[5]*256+msg->Buffer[6]);
	return ERR_NONE;
}

static GSM_Error N6510_SetBitmap(GSM_StateMachine *s, GSM_Bitmap *Bitmap)
{
	GSM_SMSMessage		sms;
	GSM_Phone_Bitmap_Types	Type;
	size_t			Width, Height, i, count;
	unsigned char		folderid;
	unsigned int		location;
	GSM_NetworkInfo 	NetInfo;
	GSM_Error		error;
	unsigned char reqStartup[1000] = {
		N7110_FRAME_HEADER, 0x04, 0x0F,
		0x00, 0x00, 0x00,
		0x04, 0xC0, 0x02, 0x00,
		0x41, 0xC0, 0x03, 0x00,
		0x60, 0xC0, 0x04};
	unsigned char reqColourWallPaper[200] = {
		N6110_FRAME_HEADER, 0x07, 0x00, 0x00, 0x00, 0xD5,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x01, 0x00,
		0x18};		/* Bitmap ID */
	unsigned char reqColourStartup[200] = {
		N6110_FRAME_HEADER, 0x04, 0x25, 0x00, 0x01, 0x00, 0x18};
	unsigned char reqOp[1000] = {
		N7110_FRAME_HEADER, 0x25, 0x01,
		0x55, 0x00, 0x00, 0x55,
		0x01,			/* 0x01 - not set, 0x02 - set */
		0x0C, 0x08,
		0x62, 0xF0, 0x10,	/* Network code */
		0x03, 0x55, 0x55};
	unsigned char reqColourOp[200] = {
		N6110_FRAME_HEADER,
		0x07, 0x00, 0x00, 0x00, 0xE7, 0x00, 0x00, 0x00, 0xF9, 0x00,
		0x08, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00,
		0x18,			/* File ID */
		0x00,
		0x00, 0x00, 0x00};	/* Network code */
	unsigned char reqNote[200] = {N6110_FRAME_HEADER, 0x04, 0x01};
	unsigned char reqPicture[2000] = {
		N6110_FRAME_HEADER, 0x00,
		0x02, 0x05,		/* SMS folder 	*/
		0x00, 0x00,		/* location 	*/
		0x01, 0x01, 0xa0, 0x02, 0x01, 0x40, 0x00, 0x34,
		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x55, 0x55, 0x55, 0x03, 0x82, 0x10,
		0x01, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x10,
		0x02, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x04,
		0x00, 0x00, 0xa1, 0x55, 0x01, 0x08, 0x00, 0x00,
		0x00, 0x01, 0x48, 0x1c, 0x00, 0xfc, 0x00};

	switch (Bitmap->Type) {
	case GSM_ColourWallPaper_ID:
		reqColourWallPaper[21] = Bitmap->ID;
		smprintf(s, "Setting colour wall paper\n");
		return GSM_WaitFor (s, reqColourWallPaper, 22, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
	case GSM_StartupLogo:
		Type = GSM_Nokia7110StartupLogo;
		switch (Bitmap->Location) {
			case 1:	 PHONE_EncodeBitmap(Type, reqStartup + 22, Bitmap);
				 break;
			case 2:  memset(reqStartup+5,0x00,15);
				 PHONE_ClearBitmap(Type, reqStartup + 22,0,0);
				 break;
			default: return ERR_NOTSUPPORTED;
		}
		smprintf(s, "Setting startup logo\n");
		return GSM_WaitFor (s, reqStartup, 22+PHONE_GetBitmapSize(Type,0,0), 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
	case GSM_DealerNote_Text:
		reqNote[4] = 0x10;
		CopyUnicodeString(reqNote + 5, Bitmap->Text);
		i = 6 + UnicodeLength(Bitmap->Text) * 2;
		reqNote[i++] 	= 0;
		reqNote[i] 	= 0;
		return GSM_WaitFor (s, reqNote, i, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
	case GSM_WelcomeNote_Text:
		CopyUnicodeString(reqNote + 5, Bitmap->Text);
		i = 6 + UnicodeLength(Bitmap->Text) * 2;
		reqNote[i++] 	= 0;
		reqNote[i] 	= 0;
		return GSM_WaitFor (s, reqNote, i, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
	case GSM_OperatorLogo:
		/* We want to set operator logo, not clear */
		if (strcmp(Bitmap->NetworkCode,"000 00")) {
			memset(reqOp + 19, 0, 281);
			NOKIA_EncodeNetworkCode(reqOp+12, Bitmap->NetworkCode);
			Type = GSM_Nokia6510OperatorLogo;
			reqOp[9]  = 0x02;	/* Logo enabled */
			reqOp[18] = 0x1a;	/* FIXME */
			reqOp[19] = PHONE_GetBitmapSize(Type,0,0) + 8 + 29 + 2;
			PHONE_GetBitmapWidthHeight(Type, &Width, &Height);
			reqOp[20] = Width;
			reqOp[21] = Height;
			reqOp[22] = 0x00;
			reqOp[23] = PHONE_GetBitmapSize(Type,0,0) + 29;
			reqOp[24] = 0x00;
			reqOp[25] = PHONE_GetBitmapSize(Type,0,0) + 29;
			PHONE_EncodeBitmap(Type, reqOp + 26, Bitmap);
			smprintf(s, "Setting operator logo\n");
			return GSM_WaitFor (s, reqOp, reqOp[19]+reqOp[11]+10, 0x0A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
		} else {
			error=N6510_GetNetworkInfo(s,&NetInfo);
			if (error != ERR_NONE) return error;
			NOKIA_EncodeNetworkCode(reqOp+12, NetInfo.NetworkCode);
			smprintf(s, "Clearing operator logo\n");
			return GSM_WaitFor (s, reqOp, 18, 0x0A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
		}
	case GSM_ColourOperatorLogo_ID:
		/* We want to set operator logo, not clear */
		if (strcmp(Bitmap->NetworkCode,"000 00")) {
			EncodeBCD(reqColourOp+23, Bitmap->NetworkCode, 6, FALSE);
			reqColourOp[21] = Bitmap->ID;
		}
		smprintf(s, "Setting colour operator logo\n");
		return GSM_WaitFor (s, reqColourOp, 26, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
	case GSM_ColourStartupLogo_ID:
		switch (Bitmap->Location) {
			case 0: reqColourStartup[6] = 0x00;
				reqColourStartup[8] = 0x00;
				smprintf(s, "Setting colour startup logo\n");
				return GSM_WaitFor (s, reqColourStartup, 9, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
			case 1: reqColourStartup[8] = Bitmap->ID;
				smprintf(s, "Setting colour startup logo\n");
				return GSM_WaitFor (s, reqColourStartup, 9, 0x7A, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
			default:return ERR_NOTSUPPORTED;
		}
	case GSM_CallerGroupLogo:
		return N6510_SetCallerLogo(s,Bitmap);
	case GSM_PictureImage:
		error = N6510_GetPictureImage(s, Bitmap, &sms.Location);
		if (error == ERR_NONE) {
			sms.Folder = 0;
			N6510_GetSMSLocation(s, &sms, &folderid, &location);
			switch (folderid) {
				case 0x01: reqPicture[5] = 0x02; 				break; /* INBOX SIM 	*/
				case 0x02: reqPicture[5] = 0x03; 				break; /* OUTBOX SIM 	*/
				default	 : reqPicture[5] = folderid - 1; reqPicture[4] = 0x02; 	break; /* ME folders	*/
			}
			reqPicture[6]=location / 256;
			reqPicture[7]=location;
		}
		Type = GSM_NokiaPictureImage;
		count = 78;
		PHONE_EncodeBitmap(Type, reqPicture + count, Bitmap);
		count += PHONE_GetBitmapSize(Type,0,0);
		smprintf(s, "Setting Picture Image\n");
		return GSM_WaitFor (s, reqPicture, count, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_SetBitmap);
	default:
		break;
	}
	return ERR_NOTSUPPORTED;
}

static GSM_Error N6510_ReplyGetRingtoneID(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_N6510Data *Priv = &s->Phone.Data.Priv.N6510;

	smprintf(s, "Ringtone ID received\n");
	Priv->RingtoneID = msg->Buffer[15];
	return ERR_NONE;
}

static GSM_Error N6510_ReplySetBinRingtone(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Binary ringtone set\n");
	return ERR_NONE;
}

static GSM_Error N6510_SetRingtone(GSM_StateMachine *s, GSM_Ringtone *Ringtone, int *maxlength)
{
	GSM_Error		error;
	GSM_Phone_N6510Data 	*Priv = &s->Phone.Data.Priv.N6510;
	GSM_NetworkInfo		NetInfo;
	size_t			size=200, current;
	unsigned char 		GetIDReq[] = {
		N7110_FRAME_HEADER, 0x01, 0x00, 0x00,
		0x00, 0xFF, 0x06, 0xE1, 0x00,
		0xFF, 0x06, 0xE1, 0x01, 0x42};
	unsigned char		SetPreviewReq[1000] = {
		0xAE,		/* Ringtone ID */
		0x01, 0x00, 0x0D, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00,
		0x00};  	/*Length*/
	unsigned char		AddBinaryReq[33000] = {
		N7110_FRAME_HEADER, 0x0E, 0x7F, 0xFF, 0xFE};

	if (Ringtone->Format == RING_NOTETONE && Ringtone->Location==255)
	{
		smprintf(s, "Getting ringtone ID\n");
		error=GSM_WaitFor (s, GetIDReq, 14, 0xDB, s->Phone.Data.Priv.N6510.Timeout, ID_SetRingtone);
		if (error != ERR_NONE) return error;
		*maxlength=GSM_EncodeNokiaRTTLRingtone(Ringtone, SetPreviewReq+11, &size);
		SetPreviewReq[0]  = Priv->RingtoneID;
		SetPreviewReq[10] = size;
		smprintf(s, "Setting ringtone\n");
		error = s->Protocol.Functions->WriteMessage(s, SetPreviewReq, size+11, 0x00);
		if (error!=ERR_NONE) return error;
		sleep(1);
		/* We have to make something (not important, what) now */
		/* no answer from phone*/
		return s->Phone.Functions->GetNetworkInfo(s,&NetInfo);
	}
	if (Ringtone->Format == RING_NOKIABINARY) {
		AddBinaryReq[7] = UnicodeLength(Ringtone->Name);
		CopyUnicodeString(AddBinaryReq+8,Ringtone->Name);
		current = 8 + UnicodeLength(Ringtone->Name)*2;
		AddBinaryReq[current++] = Ringtone->NokiaBinary.Length/256 + 1;
		AddBinaryReq[current++] = Ringtone->NokiaBinary.Length%256 + 1;
		AddBinaryReq[current++] = 0x00;
		memcpy(AddBinaryReq+current,Ringtone->NokiaBinary.Frame,Ringtone->NokiaBinary.Length);
		current += Ringtone->NokiaBinary.Length;
		smprintf(s, "Adding binary ringtone\n");
		return GSM_WaitFor (s, AddBinaryReq, current, 0x1F, s->Phone.Data.Priv.N6510.Timeout, ID_SetRingtone);
	}
	if (Ringtone->Format == RING_MIDI) {
		AddBinaryReq[7] = UnicodeLength(Ringtone->Name);
		CopyUnicodeString(AddBinaryReq+8,Ringtone->Name);
		current = 8 + UnicodeLength(Ringtone->Name)*2;
		AddBinaryReq[current++] = Ringtone->NokiaBinary.Length/256;
		AddBinaryReq[current++] = Ringtone->NokiaBinary.Length%256;
		memcpy(AddBinaryReq+current,Ringtone->NokiaBinary.Frame,Ringtone->NokiaBinary.Length);
		current += Ringtone->NokiaBinary.Length;
		AddBinaryReq[current++] = 0x00;
		AddBinaryReq[current++] = 0x00;
		smprintf(s, "Adding binary or MIDI ringtone\n");
		return GSM_WaitFor (s, AddBinaryReq, current, 0x1F, s->Phone.Data.Priv.N6510.Timeout, ID_SetRingtone);
	}
	return ERR_NOTSUPPORTED;
}

static GSM_Error N6510_ReplyDeleteRingtones(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Ringtones deleted\n");
	return ERR_NONE;
}

static GSM_Error N6510_DeleteUserRingtones(GSM_StateMachine *s)
{
	unsigned char DelAllRingtoneReq[] = {N7110_FRAME_HEADER, 0x10, 0x7F, 0xFE};

	smprintf(s, "Deleting all user ringtones\n");
	return GSM_WaitFor (s, DelAllRingtoneReq, 6, 0x1F, s->Phone.Data.Priv.N6510.Timeout, ID_SetRingtone);
}

static GSM_Error N6510_ReplyPressKey(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	if (msg->Buffer[3] == 0x33) {
		smprintf(s, "Key auto released\n");
	} else if (msg->Buffer[3] == 0x12) {
		smprintf(s, "Key pressed\n");
	} else {
		return ERR_UNKNOWN;
	}
	return ERR_NONE;
}

static GSM_Error N6510_PressKey(GSM_StateMachine *s, GSM_KeyCode Key, gboolean Press)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x11, 0x00, 0x01, 0x00, 0x00,
			       0x00,		/* Event */
			       0x01};		/* Number of presses */

	if (Key != GSM_KEY_GREEN) {
		smprintf(s, "Mapping for key code %d missing!\n", Key);
		return ERR_NOTIMPLEMENTED;
	}

	req[7] = Key;
	if (Press) {
		req[8] = NOKIA_PRESSPHONEKEY;
		s->Phone.Data.PressKey = TRUE;
		smprintf(s, "Pressing key\n");
	} else {
		req[8] = NOKIA_RELEASEPHONEKEY;
		s->Phone.Data.PressKey = FALSE;
		smprintf(s, "Releasing key\n");
	}
	return GSM_WaitFor (s, req, 10, 0x0c, s->Phone.Data.Priv.N6510.Timeout, ID_PressKey);
}

static GSM_Error N6510_EnableConnectionFunctions(GSM_StateMachine *s, N6510_Connection_Settings Type)
{
	GSM_Error	error;
	unsigned char 	req2[] = {N6110_FRAME_HEADER, 0x00, 0x01};
	unsigned char 	req3[] = {N6110_FRAME_HEADER, 0x00, 0x03};
	unsigned char 	req4[] = {N6110_FRAME_HEADER, 0x00, 0x04};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_6230iWAP)) return ERR_NOTSUPPORTED;

	if (Type == N6510_MMS_SETTINGS    && GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_NOMMS)) return ERR_NOTSUPPORTED;
	if (Type == N6510_CHAT_SETTINGS   && !GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_CHAT)) return ERR_NOTSUPPORTED;
	if (Type == N6510_SYNCML_SETTINGS && !GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SYNCML)) return ERR_NOTSUPPORTED;

	error=DCT3DCT4_DisableConnectionFunctions(s);
	if (error!=ERR_NONE) return error;

	switch (Type) {
	case N6510_WAP_SETTINGS:
		return DCT3DCT4_EnableWAPFunctions(s);
	case N6510_MMS_SETTINGS:
		smprintf(s, "Enabling MMS\n");
		return GSM_WaitFor (s, req2, 5, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_EnableConnectFunc);
	case N6510_SYNCML_SETTINGS:
		smprintf(s, "Enabling SyncML\n");
		return GSM_WaitFor (s, req3, 5, 0x3f, 5, ID_EnableConnectFunc);
	case N6510_CHAT_SETTINGS:
		smprintf(s, "Enabling Chat\n");
		return GSM_WaitFor (s, req4, 5, 0x3f, 5, ID_EnableConnectFunc);
	default:
		return ERR_UNKNOWN;
	}
}

static GSM_Error N6510_ReplyGetConnectionSettings(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_N6510Data	*Priv = &s->Phone.Data.Priv.N6510;
	int 			tmp,num=0,i;
	GSM_Phone_Data		*Data = &s->Phone.Data;
	unsigned char		buff[2000];

	switch(msg->Buffer[3]) {
	case 0x16:
		smprintf(s, "Connection settings received OK\n");

		Data->WAPSettings->Number = Priv->BearerNumber;

		Data->WAPSettings->Proxy[0]   = 0x00;
		Data->WAPSettings->Proxy[1]   = 0x00;
		Data->WAPSettings->ProxyPort  = 8080;

		Data->WAPSettings->Proxy2[0]  = 0x00;
		Data->WAPSettings->Proxy2[1]  = 0x00;
		Data->WAPSettings->Proxy2Port = 8080;

		tmp = 4;

		NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[0].Title,TRUE);
		CopyUnicodeString(Data->WAPSettings->Settings[1].Title,Data->WAPSettings->Settings[0].Title);
		smprintf(s, "Title: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[0].Title));

		NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[0].HomePage,TRUE);
		CopyUnicodeString(Data->WAPSettings->Settings[1].HomePage,Data->WAPSettings->Settings[0].HomePage);
		smprintf(s, "Homepage: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[0].HomePage));

#ifdef DEBUG
		smprintf(s, "Connection type: ");
		switch (msg->Buffer[tmp]) {
			case 0x00: smprintf(s, "temporary\n");  break;
			case 0x01: smprintf(s, "continuous\n"); break;
			default:   smprintf(s, "unknown\n");
		}
		smprintf(s, "Connection security: ");
		switch (msg->Buffer[tmp+1]) {
			case 0x00: smprintf(s, "off\n");	break;
			case 0x01: smprintf(s, "on\n");		break;
			default:   smprintf(s, "unknown\n");
		}
		smprintf(s, "Bearer: ");
		switch (msg->Buffer[tmp+2]) {
			case 0x01: smprintf(s, "GSM data\n");	break;
			case 0x03: smprintf(s, "GPRS\n");	break;
			default:   smprintf(s, "unknown\n");
		}
		if (msg->Buffer[tmp+3] == 0x01) smprintf(s, "locked\n");
#endif
		Data->WAPSettings->Settings[0].IsContinuous = FALSE;
		if (msg->Buffer[tmp] == 0x01) Data->WAPSettings->Settings[0].IsContinuous = TRUE;
		Data->WAPSettings->Settings[1].IsContinuous = Data->WAPSettings->Settings[0].IsContinuous;

		Data->WAPSettings->Settings[0].IsSecurity = FALSE;
		if (msg->Buffer[tmp+1] == 0x01) Data->WAPSettings->Settings[0].IsSecurity = TRUE;
		Data->WAPSettings->Settings[1].IsSecurity = Data->WAPSettings->Settings[0].IsSecurity;

		Data->WAPSettings->ActiveBearer = WAPSETTINGS_BEARER_DATA;
		if (msg->Buffer[tmp+2] == 0x03) Data->WAPSettings->ActiveBearer = WAPSETTINGS_BEARER_GPRS;

		Data->WAPSettings->ReadOnly = FALSE;
		if (msg->Buffer[tmp+3] == 0x01) Data->WAPSettings->ReadOnly = TRUE;

		tmp+=3;

		if (Priv->BearerNumber == 2) {
			/* Here starts settings for data bearer */
			Data->WAPSettings->Settings[0].Bearer = WAPSETTINGS_BEARER_DATA;
			while ((msg->Buffer[tmp] != 0x01) || (msg->Buffer[tmp + 1] != 0x00)) tmp++;
			tmp += 4;

#ifdef DEBUG
			smprintf(s, "Authentication type: ");
			switch (msg->Buffer[tmp]) {
				case 0x00: smprintf(s, "normal\n");	break;
				case 0x01: smprintf(s, "secure\n");	break;
				default:   smprintf(s, "unknown\n");	break;
			}
			smprintf(s, "Data call type: ");
			switch (msg->Buffer[tmp+1]) {
				case 0x00: smprintf(s, "analogue\n");	break;
				case 0x01: smprintf(s, "ISDN\n");	break;
				default:   smprintf(s, "unknown\n");	break;
			}
			smprintf(s, "Data call speed: ");
			switch (msg->Buffer[tmp+2]) {
				case 0x00: smprintf(s, "automatic\n"); 	break;
				case 0x01: smprintf(s, "9600\n");	break;
				case 0x02: smprintf(s, "14400\n");	break;
				default:   smprintf(s, "unknown\n");	break;
			}
			smprintf(s, "Login Type: ");
			switch (msg->Buffer[tmp+4]) {
				case 0x00: smprintf(s, "manual\n");	break;
				case 0x01: smprintf(s, "automatic\n");	break;
				default:   smprintf(s, "unknown\n");	break;
			}
#endif
			Data->WAPSettings->Settings[0].IsNormalAuthentication=TRUE;
			if (msg->Buffer[tmp]==0x01) Data->WAPSettings->Settings[0].IsNormalAuthentication=FALSE;

			Data->WAPSettings->Settings[0].IsISDNCall=FALSE;
			if (msg->Buffer[tmp+1]==0x01) Data->WAPSettings->Settings[0].IsISDNCall=TRUE;

			switch (msg->Buffer[tmp+2]) {
				case 0x00: Data->WAPSettings->Settings[0].Speed=WAPSETTINGS_SPEED_AUTO;  break;
				case 0x01: Data->WAPSettings->Settings[0].Speed=WAPSETTINGS_SPEED_9600;	 break;
				case 0x02: Data->WAPSettings->Settings[0].Speed=WAPSETTINGS_SPEED_14400; break;
				default:
					smprintf(s, "Unknown speed settings: 0x%0x\n", msg->Buffer[tmp+2]);
					Data->WAPSettings->Settings[0].Speed=WAPSETTINGS_SPEED_AUTO;
					break;
			}

			Data->WAPSettings->Settings[0].ManualLogin=FALSE;
			if (msg->Buffer[tmp+4]==0x00) Data->WAPSettings->Settings[0].ManualLogin = TRUE;

			tmp+=5;

			NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[0].IPAddress,FALSE);
			smprintf(s, "IP address: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[0].IPAddress));

			NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[0].DialUp,TRUE);
			smprintf(s, "Dial-up number: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[0].DialUp));

			NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[0].User,TRUE);
			smprintf(s, "User name: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[0].User));

			NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[0].Password,TRUE);
			smprintf(s, "Password: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[0].Password));

			num = 1;
		} else {
			num = 0;
		}

		/* Here starts settings for gprs bearer */
		Data->WAPSettings->Settings[num].Bearer = WAPSETTINGS_BEARER_GPRS;
		while (msg->Buffer[tmp] != 0x03) tmp++;
		tmp += 4;

#ifdef DEBUG
		smprintf(s, "Authentication type: ");
		switch (msg->Buffer[tmp]) {
			case 0x00: smprintf(s, "normal\n");	break;
			case 0x01: smprintf(s, "secure\n");	break;
			default:   smprintf(s, "unknown\n");	break;
		}
		smprintf(s, "GPRS connection: ");
		switch (msg->Buffer[tmp+1]) {
			case 0x00: smprintf(s, "ALWAYS online\n"); break;
			case 0x01: smprintf(s, "when needed\n");   break;
			default:   smprintf(s, "unknown\n"); 	   break;
		}
		smprintf(s, "Login Type: ");
		switch (msg->Buffer[tmp+2]) {
			case 0x00: smprintf(s, "manual\n");	break;
			case 0x01: smprintf(s, "automatic\n");	break;
			default:   smprintf(s, "unknown\n");	break;
		}
#endif
		Data->WAPSettings->Settings[num].IsNormalAuthentication=TRUE;
		if (msg->Buffer[tmp]==0x01) Data->WAPSettings->Settings[num].IsNormalAuthentication=FALSE;

		Data->WAPSettings->Settings[num].IsISDNCall=FALSE;
		Data->WAPSettings->Settings[num].Speed = WAPSETTINGS_SPEED_AUTO;

		Data->WAPSettings->Settings[num].IsContinuous = TRUE;
		if (msg->Buffer[tmp+1] == 0x01) Data->WAPSettings->Settings[num].IsContinuous = FALSE;

		Data->WAPSettings->Settings[num].ManualLogin=FALSE;
		if (msg->Buffer[tmp+2]==0x00) Data->WAPSettings->Settings[num].ManualLogin = TRUE;

		tmp+=3;

		NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[num].DialUp,FALSE);
		smprintf(s, "Access point: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[num].DialUp));

		NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[num].IPAddress,TRUE);
		smprintf(s, "IP address: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[num].IPAddress));

		NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[num].User,TRUE);
		smprintf(s, "User name: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[num].User));

		NOKIA_GetUnicodeString(s, &tmp, msg->Buffer, Data->WAPSettings->Settings[num].Password,TRUE);
		smprintf(s, "Password: \"%s\"\n",DecodeUnicodeString(Data->WAPSettings->Settings[num].Password));

		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_WAPMMSPROXY)) {
			if (msg->Buffer[tmp] == 0x00 && msg->Buffer[tmp+1] == 0x00) tmp = tmp+2;

			memcpy(buff,msg->Buffer+tmp+10,msg->Buffer[tmp+4]);
			buff[msg->Buffer[tmp+4]] = 0x00;
			smprintf(s, "Proxy 1: \"%s\", port %i\n",buff,msg->Buffer[tmp+6]*256+msg->Buffer[tmp+7]);
			EncodeUnicode(Data->WAPSettings->Proxy,buff,strlen(buff));
			Data->WAPSettings->ProxyPort = msg->Buffer[tmp+6]*256+msg->Buffer[tmp+7];

			memcpy(buff,msg->Buffer+tmp+10+msg->Buffer[tmp+4],msg->Buffer[tmp+5]);
			buff[msg->Buffer[tmp+5]] = 0x00;
			smprintf(s, "Proxy 2: \"%s\", port %i\n",buff,msg->Buffer[tmp+8]*256+msg->Buffer[tmp+9]);
			EncodeUnicode(Data->WAPSettings->Proxy2,buff,strlen(buff));
			Data->WAPSettings->Proxy2Port = msg->Buffer[tmp+8]*256+msg->Buffer[tmp+9];

			tmp = tmp + msg->Buffer[tmp+3] + 19;

			for (i=0;i<4;i++) {
#ifdef DEBUG
				smprintf(s, "Proxy data %i\n",i+1);
				if (msg->Buffer[tmp+2]!=0) memcpy(buff,msg->Buffer+tmp+9,msg->Buffer[tmp+2]*2);
				buff[msg->Buffer[tmp+2]*2]  =0;
				buff[msg->Buffer[tmp+2]*2+1]=0;
				smprintf(s, "IP: \"%s\"",DecodeUnicodeString(buff));
				smprintf(s, ", port %i\n",msg->Buffer[tmp+3]*256+msg->Buffer[tmp+4]);
#endif
				tmp = tmp + msg->Buffer[tmp];
			}

#ifdef DEBUG
			smprintf(s, "%02x %02x\n",msg->Buffer[tmp],msg->Buffer[tmp+1]);
			smprintf(s, "Port %i\n",msg->Buffer[tmp+3]*256+msg->Buffer[tmp+4]);
			tmp = tmp + msg->Buffer[tmp];
#endif
		}

		return ERR_NONE;
	case 0xf0:
		/*
		 * Don't know exactly what 0x0f means, but the message is too short
		 * to contain information:
		 *
		 * 01 |58X|00 |F0 |01 |15 |00 |00 |00 |00
		 */
		smprintf(s, "Connection settings receiving error, assuming empty\n");
		return ERR_EMPTY;
	case 0x11:
	case 0x17:
		smprintf(s, "Connection settings receiving error\n");
		switch (msg->Buffer[4]) {
		case 0x01:
			smprintf(s, "Security error. Inside phone settings menu\n");
			return ERR_INSIDEPHONEMENU;
		case 0x02:
		case 0x03: /* Guess */
			smprintf(s, "Invalid or empty\n");
			return ERR_INVALIDLOCATION;
		default:
			smprintf(s, "ERROR: unknown %i\n",msg->Buffer[4]);
			return ERR_UNKNOWNRESPONSE;
		}
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetConnectionSettings(GSM_StateMachine *s, GSM_MultiWAPSettings *settings, N6510_Connection_Settings Type)
{
	GSM_Phone_N6510Data	*Priv = &s->Phone.Data.Priv.N6510;
	GSM_Error 		error;
	unsigned char 		req[] = {N6110_FRAME_HEADER, 0x15,
				 	 0x00};		/* Location */

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) return ERR_NOTSUPPORTED;
	if (!strcmp(s->Phone.Data.ModelInfo->model,"6020")) return ERR_NOTSUPPORTED;

	error = N6510_EnableConnectionFunctions(s, Type);
	if (error!=ERR_NONE) return error;

	req[4] 			  = settings->Location-1;
	s->Phone.Data.WAPSettings = settings;

	switch (Type) {
	case N6510_MMS_SETTINGS:
		smprintf(s, "Getting MMS settings\n");
		Priv->BearerNumber = 1;
		break;
	case N6510_WAP_SETTINGS:
		smprintf(s, "Getting WAP settings\n");
		Priv->BearerNumber = 2;
		break;
	case N6510_SYNCML_SETTINGS:
		smprintf(s, "Getting SyncML settings\n");
		Priv->BearerNumber = 2;
		break;
	case N6510_CHAT_SETTINGS:
		smprintf(s, "Getting Chat settings\n");
		Priv->BearerNumber = 1;
		break;
	}

	error=GSM_WaitFor (s, req, 5, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_GetConnectSet);
	if (error != ERR_NONE) {
		if (error == ERR_INVALIDLOCATION || error == ERR_INSIDEPHONEMENU) {
			DCT3DCT4_DisableConnectionFunctions(s);
		}
		return error;
	}

	error=DCT3DCT4_GetActiveConnectSet(s);
	if (error != ERR_NONE) return error;

	return DCT3DCT4_DisableConnectionFunctions(s);
}

static GSM_Error N6510_GetWAPSettings(GSM_StateMachine *s, GSM_MultiWAPSettings *settings)
{
	return N6510_GetConnectionSettings(s, settings, N6510_WAP_SETTINGS);
}

static GSM_Error N6510_GetMMSSettings(GSM_StateMachine *s, GSM_MultiWAPSettings *settings)
{
	return N6510_GetConnectionSettings(s, settings, N6510_MMS_SETTINGS);
}

static GSM_Error N6510_ReplyGetSyncMLSettings(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_SyncMLSettings *Sett = s->Phone.Data.SyncMLSettings;

	smprintf(s, "SyncML settings received OK\n");
	CopyUnicodeString(Sett->User,msg->Buffer+18);
	CopyUnicodeString(Sett->Password,msg->Buffer+86);
	CopyUnicodeString(Sett->PhonebookDataBase,msg->Buffer+130);
	CopyUnicodeString(Sett->CalendarDataBase,msg->Buffer+234);
	CopyUnicodeString(Sett->Server,msg->Buffer+338);

	Sett->SyncPhonebook = FALSE;
	Sett->SyncCalendar  = FALSE;
	if ((msg->Buffer[598] & 0x02)==0x02) Sett->SyncCalendar = TRUE;
	if ((msg->Buffer[598] & 0x01)==0x01) Sett->SyncPhonebook = TRUE;

	return ERR_NONE;
}

static GSM_Error N6510_ReplyGetSyncMLName(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_SyncMLSettings *Sett = s->Phone.Data.SyncMLSettings;

	smprintf(s, "SyncML names received OK\n");

	CopyUnicodeString(Sett->Name,msg->Buffer+18);

	return ERR_NONE;
}

static GSM_Error N6510_GetSyncMLSettings(GSM_StateMachine *s, GSM_SyncMLSettings *settings)
{
	GSM_Error 	error;
/* 	unsigned char 	NameReq[] = {N6110_FRAME_HEADER, 0x05, */
/* 				 0x00, 0x00, 0x00, 0x31, 0x00, */
/* 				 0x06, 0x00, 0x00, 0x00, 0xDE, 0x00, 0x00}; */
/* 	unsigned char 	GetActive[] = {N6110_FRAME_HEADER, 0x05, */
/* 				 0x00, 0x00, 0x00, 0x31, 0x00, */
/* 				 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}; */
	unsigned char 	req[] = {N6110_FRAME_HEADER, 0x05,
				 0x00, 0x00, 0x00, 0x31, 0x00,
				 0x01, /* location */
				 0x00, 0x00, 0x02, 0x46, 0x00, 0x00};

	settings->Connection.Location = settings->Location;
	error = N6510_GetConnectionSettings(s, &settings->Connection, N6510_SYNCML_SETTINGS);
	if (error != ERR_NONE) return error;

	settings->Active = settings->Connection.Active;

	settings->Name[0] = 0;
	settings->Name[1] = 0;
	s->Phone.Data.SyncMLSettings  = settings;

/* 	smprintf(s, "Getting SyncML settings name\n"); */
/* 	error = GSM_WaitFor (s, NameReq, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_GetSyncMLName); */
/* 	if (error != ERR_NONE) return error; */

	req[9] = settings->Location - 1;
	smprintf(s, "Getting additional SyncML settings\n");
	return GSM_WaitFor (s, req, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_GetSyncMLSettings);
}

static GSM_Error N6510_ReplyGetChatSettings(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_ChatSettings 	*Sett = s->Phone.Data.ChatSettings;
	int			i;

	Sett->Name[0] 	  = 0;
	Sett->Name[1] 	  = 0;
	Sett->HomePage[0] = 0;
	Sett->HomePage[1] = 0;
	Sett->User[0] 	  = 0;
	Sett->User[1] 	  = 0;
	Sett->Password[0] = 0;
	Sett->Password[1] = 0;

	switch(msg->Buffer[3]) {
	case 0x3B:
		smprintf(s, "Chat settings received OK\n");
		memcpy(Sett->Name,msg->Buffer+20,msg->Buffer[12]*2);
		Sett->Name[msg->Buffer[12]*2] 	   = 0;
		Sett->Name[msg->Buffer[12]*2+1] 	   = 0;
		memcpy(Sett->HomePage,msg->Buffer+20+msg->Buffer[12]*2,msg->Buffer[15]*2);
		Sett->HomePage[msg->Buffer[15]*2]   = 0;
		Sett->HomePage[msg->Buffer[15]*2+1] = 0;
		i = msg->Buffer[12]*2 + msg->Buffer[15]*2 + 29;
		memcpy(Sett->User,msg->Buffer+i+3,msg->Buffer[i]*2);
		Sett->User[msg->Buffer[i]*2]   = 0;
		Sett->User[msg->Buffer[i]*2+1] = 0;
		memcpy(Sett->Password,msg->Buffer+i+3+msg->Buffer[i]*2,msg->Buffer[i+1]*2);
		Sett->Password[msg->Buffer[i+1]*2]   = 0;
		Sett->Password[msg->Buffer[i+1]*2+1] = 0;
		return ERR_NONE;
	case 0x3C:
		smprintf(s, "Empty chat settings received\n");
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetChatSettings(GSM_StateMachine *s, GSM_ChatSettings *settings)
{
	GSM_Error 	error;
	unsigned char 	req[] = {N6110_FRAME_HEADER, 0x3a,
				 0x09,			/*  location */
				 0x01, 0x0e};

	settings->Connection.Location = settings->Location;
	error = N6510_GetConnectionSettings(s, &settings->Connection, N6510_CHAT_SETTINGS);
	if (error != ERR_NONE) return error;

	settings->Active = settings->Connection.Active;

	s->Phone.Data.ChatSettings  = settings;
	req[4] 			    = settings->Location - 1;
	smprintf(s, "Getting additional Chat settings\n");
	return GSM_WaitFor (s, req, 7, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_GetChatSettings);
}

static GSM_Error N6510_ReplySetConnectionSettings(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[3]) {
	case 0x19:
		smprintf(s, "Connection settings cleaned\n");
		return ERR_NONE;
	case 0x1a:
		smprintf(s, "Connection settings setting status\n");
		switch (msg->Buffer[4]) {
		case 0x01:
			smprintf(s, "Security error. Inside phone settings menu\n");
			return ERR_INSIDEPHONEMENU;
		case 0x03:
			smprintf(s, "Invalid location\n");
			return ERR_INVALIDLOCATION;
		case 0x05:
			smprintf(s, "Written OK\n");
			return ERR_NONE;
		default:
			smprintf(s, "ERROR: unknown %i\n",msg->Buffer[4]);
			return ERR_UNKNOWNRESPONSE;
		}
	case 0x28:
	case 0x2B:
		smprintf(s, "Set OK\n");
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_SetConnectionSettings(GSM_StateMachine *s, GSM_MultiWAPSettings *settings, N6510_Connection_Settings Type)
{
	GSM_Error 	error;
	int 		i, pad = 0, length, pos = 5, loc1=-1,loc2=-1,port;
	unsigned char	*Proxy;
	unsigned char 	req[2000] = {N6110_FRAME_HEADER, 0x18,
				     0x00};		/* Location */
	unsigned char 	Lock[5] = {N6110_FRAME_HEADER, 0x27,
				   0x00};		/* Location */
	unsigned char 	UnLock[5] = {N6110_FRAME_HEADER, 0x2A,
				   0x00};		/* Location */

	error = N6510_EnableConnectionFunctions(s, Type);
	if (error!=ERR_NONE) return error;

	memset(req + pos, 0, 1000 - pos);

	req[4] = settings->Location-1;

	for (i=0;i<settings->Number;i++) {
		if (settings->Settings[i].Bearer == WAPSETTINGS_BEARER_DATA) loc1=i;
		if (settings->Settings[i].Bearer == WAPSETTINGS_BEARER_GPRS) loc2=i;
	}

	if (loc1 != -1) {
		/* Name */
		length = UnicodeLength(settings->Settings[loc1].Title);
		if (!(length % 2)) pad = 1;
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc1].Title, FALSE);

		/* Home */
		length = UnicodeLength(settings->Settings[loc1].HomePage);
		if (((length + pad) % 2)) pad = 2; else pad = 0;
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc1].HomePage, TRUE);

		if (settings->Settings[loc1].IsContinuous) req[pos] = 0x01; pos++;
		if (settings->Settings[loc1].IsSecurity) req[pos] = 0x01; pos++;
	} else if (loc2 != -1) {
		/* Name */
		length = UnicodeLength(settings->Settings[loc2].Title);
		if (!(length % 2)) pad = 1;
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc2].Title, FALSE);

		/* Home */
		length = UnicodeLength(settings->Settings[loc2].HomePage);
		if (((length + pad) % 2)) pad = 2; else pad = 0;
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc2].HomePage, TRUE);

		if (settings->Settings[loc2].IsContinuous) req[pos] = 0x01; pos++;
		if (settings->Settings[loc2].IsSecurity) req[pos] = 0x01; pos++;
	} else {
		/* Name */
		length = 0;
		if (!(length % 2)) pad = 1;
		pos ++;

		/* Home */
		length = 0;
		if (((length + pad) % 2)) pad = 2; else pad = 0;
		pos += 2;

		pos += 2;
	}

	if (Type == N6510_MMS_SETTINGS || Type == N6510_CHAT_SETTINGS) {
		req[pos++] = 0x03; /* active bearer: GPRS */
	} else {
		if (settings->ActiveBearer == WAPSETTINGS_BEARER_GPRS && loc2 != -1) {
			req[pos++] = 0x03; /* active bearer: GPRS */
		} else {
			req[pos++] = 0x01; /* active bearer: data set */
		}
	}

	/* Number of sent bearers */
	if (Type == N6510_MMS_SETTINGS || Type == N6510_CHAT_SETTINGS) {
		req[pos] = 0x01;
	} else {
		req[pos] = 0x02;
	}
	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_WAPMMSPROXY)) req[pos] += 2;
	pos++;
 	pos += pad;

	if (Type != N6510_MMS_SETTINGS && Type != N6510_CHAT_SETTINGS) {
		/* GSM data block */
		memcpy(req + pos, "\x01\x00", 2);	pos += 2;

		if (loc1 != -1) {
			length  = UnicodeLength(settings->Settings[loc1].IPAddress)*2+1;
			length += UnicodeLength(settings->Settings[loc1].DialUp)   *2+2;
			length += UnicodeLength(settings->Settings[loc1].User)     *2+2;
			length += UnicodeLength(settings->Settings[loc1].Password) *2+2;
		} else {
			length = 1 + 2 + 2 + 2;
		}
		length += 11;
		req[pos++] = length / 256;
		req[pos++] = length % 256;

		if (loc1 != -1) {
			if (!settings->Settings[loc1].IsNormalAuthentication) req[pos]=0x01; pos++;
			if (settings->Settings[loc1].IsISDNCall) req[pos]=0x01;	pos++;
			switch (settings->Settings[loc1].Speed) {
				case WAPSETTINGS_SPEED_AUTO	: 		 break;
				case WAPSETTINGS_SPEED_9600	: req[pos]=0x01; break;
				case WAPSETTINGS_SPEED_14400	: req[pos]=0x02; break;
			}
			pos++;
			req[pos++]=0x01;
			if (!settings->Settings[loc1].ManualLogin) req[pos] = 0x01; pos++;

			pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc1].IPAddress, FALSE);
			pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc1].DialUp, TRUE);
			pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc1].User, TRUE);
			pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc1].Password, TRUE);
		} else {
			pos += 3;
			req[pos++]=0x01;
			pos += 8;
		}

		/* Padding */
		pos+=2;
	}

	/* GPRS block */
	memcpy(req + pos, "\x03\x00", 2);	pos += 2;

	if (loc2 != -1) {
		length  = UnicodeLength(settings->Settings[loc2].DialUp)   *2+1;
		length += UnicodeLength(settings->Settings[loc2].IPAddress)*2+2;
		length += UnicodeLength(settings->Settings[loc2].User)     *2+2;
		length += UnicodeLength(settings->Settings[loc2].Password) *2+2;
	} else {
		length = 7;
	}
	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_WAPMMSPROXY)) length+=2;
	length += 7;
	req[pos++] = length / 256;
	req[pos++] = length % 256;

	if (loc2 != -1) {
		if (!settings->Settings[loc2].IsNormalAuthentication) req[pos] = 0x01; pos++;
		if (!settings->Settings[loc2].IsContinuous) req[pos] = 0x01; pos++;
		if (!settings->Settings[loc2].ManualLogin) req[pos] = 0x01; pos++;

		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc2].DialUp, FALSE);
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc2].IPAddress, TRUE);
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc2].User, TRUE);
		pos += NOKIA_SetUnicodeString(s, req + pos, settings->Settings[loc2].Password, TRUE);
	} else {
		pos += 10;
	}

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_WAPMMSPROXY)) {
		req[pos++] = 0x00;
		req[pos++] = 0x00;

		/* Proxy block */
		req[pos++] = 0x06;
		req[pos++] = 0x01;
		if (UnicodeLength(settings->Proxy)!=0 ||
		    UnicodeLength(settings->Proxy2)!=0) {
			req[pos++] = (UnicodeLength(settings->Proxy)+UnicodeLength(settings->Proxy2)+13)/256;
			req[pos++] = (UnicodeLength(settings->Proxy)+UnicodeLength(settings->Proxy2)+13)%256;
		} else {
			req[pos++] = (UnicodeLength(settings->Proxy)+UnicodeLength(settings->Proxy2)+12)/256;
			req[pos++] = (UnicodeLength(settings->Proxy)+UnicodeLength(settings->Proxy2)+12)%256;
		}
		req[pos++] = UnicodeLength(settings->Proxy);
		req[pos++] = UnicodeLength(settings->Proxy2);
		req[pos++] = settings->ProxyPort/256;
		req[pos++] = settings->ProxyPort%256;
		req[pos++] = settings->Proxy2Port/256;
		req[pos++] = settings->Proxy2Port%256;
		if (UnicodeLength(settings->Proxy)!=0) {
			sprintf(req+pos,"%s",DecodeUnicodeString(settings->Proxy));
			pos+=UnicodeLength(settings->Proxy);
		}
		if (UnicodeLength(settings->Proxy2)!=0) {
			sprintf(req+pos,"%s",DecodeUnicodeString(settings->Proxy2));
			pos+=UnicodeLength(settings->Proxy2);
		}
		if (UnicodeLength(settings->Proxy)!=0 ||
		    UnicodeLength(settings->Proxy2)!=0) {
			req[pos++] = 0x00;
		}
		req[pos++] = 0x00; req[pos++] = 0x00;
		req[pos++] = 0x07; /* unknown */
		req[pos++] = 0x00; req[pos++] = 0x00;
		req[pos++] = 0x80; /* unknown */
		req[pos++] = 0x01; /* unknown */
		req[pos++] = 0x05; /* unknown */
		req[pos++] = 0x00; req[pos++] = 0x00;

		/* Proxy data blocks */
		for (i=0;i<4;i++) {
			port  = 8080;
			Proxy = NULL;
			if (i==0) {
				port  = settings->ProxyPort;
				Proxy = settings->Proxy;
			} else if (i==1) {
				port  = settings->Proxy2Port;
				Proxy = settings->Proxy2;
			}
			req[pos++] = 0x08; req[pos++] = 0x00;
			if (Proxy != NULL && UnicodeLength(Proxy)!=0) {
				if (UnicodeLength(Proxy)%2 != 0) {
					req[pos++] = (12 + (UnicodeLength(Proxy)+1)*2)/256;
					req[pos++] = (12 + (UnicodeLength(Proxy)+1)*2)%256;
				} else {
					req[pos++] = (12 + UnicodeLength(Proxy)*2)/256;
					req[pos++] = (12 + UnicodeLength(Proxy)*2)%256;
				}
			} else {
				req[pos++] = 12/256;
				req[pos++] = 12%256;
			}
			req[pos++] = i+1;
			if (Proxy != NULL) {
				req[pos++] = UnicodeLength(Proxy);
			} else {
				req[pos++] = 0;
			}
			req[pos++] = port/256;
			req[pos++] = port%256;
			req[pos++] = 0x00;

			req[pos++] = 0x00;
			req[pos++] = 0x01;

			req[pos++] = 0x00;
			if (Proxy != NULL && UnicodeLength(Proxy)!=0) {
				CopyUnicodeString(req+pos,Proxy);
				pos+=UnicodeLength(Proxy)*2;
				if (UnicodeLength(Proxy)%2 != 0) {
					req[pos++] = 0x00;
					req[pos++] = 0x00;
				}
			}
		}

		req[pos++] = 0x09; req[pos++] = 0x00; req[pos++] = 0x00;
		req[pos++] = 0x0C; req[pos++] = 0x02; req[pos++] = 0x00;
		req[pos++] = 0x00; req[pos++] = 0x02; req[pos++] = 0x00;
		req[pos++] = 0x00; req[pos++] = 0x00; req[pos++] = 0x00;
	} else {
		/* end of blocks ? */
		memcpy(req + pos, "\x80\x00\x00\x0c", 4);	pos += 4;
	}

	UnLock[4] = settings->Location-1;
	smprintf(s, "Making Connection settings read-write\n");
	error = GSM_WaitFor (s, UnLock, 5, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_SetConnectSet);
	if (error != ERR_NONE) return error;

	switch (Type) {
	case N6510_MMS_SETTINGS:
		smprintf(s, "Setting MMS settings\n");
		break;
	case N6510_CHAT_SETTINGS:
		smprintf(s, "Setting Chat settings\n");
		break;
	case N6510_WAP_SETTINGS:
		smprintf(s, "Setting WAP settings\n");
		break;
	case N6510_SYNCML_SETTINGS:
		smprintf(s, "Setting SyncML settings\n");
		break;
	}
	error = GSM_WaitFor (s, req, pos, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_SetConnectSet);
	if (error != ERR_NONE) {
		if (error == ERR_INSIDEPHONEMENU || error == ERR_INVALIDLOCATION) {
			DCT3DCT4_DisableConnectionFunctions(s);
		}
		return error;
	}

	if (settings->ReadOnly) {
		Lock[4] = settings->Location-1;
		smprintf(s, "Making Connection settings readonly\n");
		error = GSM_WaitFor (s, Lock, 5, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_SetConnectSet);
		if (error != ERR_NONE) return error;
	}

	error = DCT3DCT4_SetActiveConnectSet(s, settings);
	if (error != ERR_NONE) return error;

	return DCT3DCT4_DisableConnectionFunctions(s);
}

static GSM_Error N6510_SetWAPSettings(GSM_StateMachine *s, GSM_MultiWAPSettings *settings)
{
	return N6510_SetConnectionSettings(s, settings, N6510_WAP_SETTINGS);
}

static GSM_Error N6510_SetMMSSettings(GSM_StateMachine *s, GSM_MultiWAPSettings *settings)
{
	return N6510_SetConnectionSettings(s, settings, N6510_MMS_SETTINGS);
}

static GSM_Error N6510_ReplyGetOriginalIMEI(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	if (msg->Buffer[7] == 0x00) {
		smprintf(s, "No SIM card\n");
		return ERR_NOSIM;
	} else {
		return NOKIA_ReplyGetPhoneString(msg, s);
	}
}

static GSM_Error N6510_GetOriginalIMEI(GSM_StateMachine *s, char *value)
{
	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) return ERR_NOTSUPPORTED;

	return NOKIA_GetPhoneString(s,"\x00\x07\x02\x01\x00\x01",6,0x42,value,ID_GetOriginalIMEI,14);
}

static GSM_Error N6510_ReplyGetSMSStatus(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	switch (msg->Buffer[3]) {
	case 0x09:
		switch (msg->Buffer[4]) {
		case 0x00:
			smprintf(s, "Max. in phone memory   : %i\n",msg->Buffer[10]*256+msg->Buffer[11]);
			smprintf(s, "Used in phone memory   : %i\n",msg->Buffer[12]*256+msg->Buffer[13]);
			smprintf(s, "Unread in phone memory : %i\n",msg->Buffer[14]*256+msg->Buffer[15]);
			smprintf(s, "Max. in SIM            : %i\n",msg->Buffer[22]*256+msg->Buffer[23]);
			smprintf(s, "Used in SIM            : %i\n",msg->Buffer[24]*256+msg->Buffer[25]);
			smprintf(s, "Unread in SIM          : %i\n",msg->Buffer[26]*256+msg->Buffer[27]);
			Data->SMSStatus->PhoneSize	= msg->Buffer[10]*256+msg->Buffer[11];
			Data->SMSStatus->PhoneUsed	= msg->Buffer[12]*256+msg->Buffer[13];
			Data->SMSStatus->PhoneUnRead 	= msg->Buffer[14]*256+msg->Buffer[15];
			Data->SMSStatus->SIMSize	= msg->Buffer[22]*256+msg->Buffer[23];
			Data->SMSStatus->SIMUsed 	= msg->Buffer[24]*256+msg->Buffer[25];
			Data->SMSStatus->SIMUnRead 	= msg->Buffer[26]*256+msg->Buffer[27];
			return ERR_NONE;
		case 0x0f:
			smprintf(s, "No PIN\n");
			return ERR_SECURITYERROR;
		default:
			smprintf(s, "ERROR: unknown SMS status %i\n",msg->Buffer[4]);
			return ERR_UNKNOWNRESPONSE;
		}
	case 0x1a:
		smprintf(s, "Wait a moment. Phone is during power on and busy now\n");
		return ERR_SECURITYERROR;
	case 0xf0:
		/* 01 |00 |00 |F0ð|01 |12 |00 |00 |00 */
		smprintf(s, "Handling of this reply is not known, please help!\n");
		return ERR_NOTIMPLEMENTED;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetSMSStatus(GSM_StateMachine *s, GSM_SMSMemoryStatus *status)
{
	GSM_Error 		error;
	GSM_Phone_N6510Data	*Priv = &s->Phone.Data.Priv.N6510;
	unsigned char req[] = {N6110_FRAME_HEADER, 0x08, 0x00, 0x00};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) return ERR_NOTSUPPORTED;

	s->Phone.Data.SMSStatus=status;
	smprintf(s, "Getting SMS status\n");
	error = GSM_WaitFor (s, req, 6, 0x14, 20, ID_GetSMSStatus);
	if (error != ERR_NONE) return error;

	/* DCT4 family doesn't show in frame with SMS status info
         * about Templates. We get separately info about this SMS folder.
	 */
	error = N6510_GetSMSFolderStatus(s, 0x06);
	if (error != ERR_NONE) return error;
	status->TemplatesUsed = Priv->LastSMSFolder.Number;

	return error;
}

static GSM_Error N6510_ReplyDeleteSMSMessage(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[3]) {
		case 0x05:
			smprintf(s, "SMS deleted OK\n");
			return ERR_NONE;
		case 0x06:
			switch (msg->Buffer[4]) {
				case 0x02:
					smprintf(s, "Invalid location\n");
					return ERR_INVALIDLOCATION;
				default:
					smprintf(s, "Unknown error: %02x\n",msg->Buffer[4]);
					return ERR_UNKNOWNRESPONSE;
			}
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_DeleteSMSMessage(GSM_StateMachine *s, GSM_SMSMessage *sms)
{
	unsigned char		folderid;
	unsigned int		location;
	unsigned char 		req[] = {N6110_FRAME_HEADER, 0x04,
					 0x01, 		/* 0x01=SM, 0x02=ME */
					 0x00, 		/* FolderID */
					 0x00, 0x02, 	/* Location */
					 0x0F, 0x55};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SMS_FILES)) return ERR_NOTSUPPORTED;
	}

	N6510_GetSMSLocation(s, sms, &folderid, &location);

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		switch (folderid) {
			case 0x01: req[5] = 0x01; 			 break; /* SIM 		*/
			default	 : req[5] = folderid; req[4] = 0x02; 	 break; /* ME folders	*/
		}
	} else {
		switch (folderid) {
			case 0x01: req[5] = 0x02; 			 break; /* INBOX SIM 	*/
			case 0x02: req[5] = 0x03; 			 break; /* OUTBOX SIM 	*/
			default	 : req[5] = folderid - 1; req[4] = 0x02; break; /* ME folders	*/
		}
	}
	req[6]=location / 256;
	req[7]=location % 256;

	smprintf(s, "Deleting sms\n");
	return GSM_WaitFor (s, req, 10, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_DeleteSMSMessage);
}

static GSM_Error N6510_ReplySendSMSMessage(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[8]) {
	case 0x00:
		smprintf(s, "SMS sent OK, TPMR for sent sms is %d\n",msg->Buffer[10]);
		if (s->User.SendSMSStatus!=NULL) s->User.SendSMSStatus(s,0,msg->Buffer[10], s->User.SendSMSStatusUserData);
		return ERR_NONE;
	default:
		smprintf(s, "SMS not sent OK, error code probably %i\n",msg->Buffer[8]);
		if (s->User.SendSMSStatus!=NULL) s->User.SendSMSStatus(s,msg->Buffer[8],msg->Buffer[10], s->User.SendSMSStatusUserData);
		return ERR_NONE;
	}
}

static GSM_Error N6510_SendSMSMessage(GSM_StateMachine *s, GSM_SMSMessage *sms)
{
	int			length = 11;
	GSM_Error		error;
	GSM_SMSMessageLayout 	Layout;
	unsigned char req [300] = {
		N6110_FRAME_HEADER, 0x02, 0x00, 0x00, 0x00, 0x55, 0x55};

	if (sms->PDU == SMS_Deliver) sms->PDU = SMS_Submit;
	memset(req+9,0x00,sizeof(req) - 9);
	error=N6510_EncodeSMSFrame(s, sms, req + 9, &Layout, &length);
	if (error != ERR_NONE) return error;

	smprintf(s, "Sending sms\n");
	return s->Protocol.Functions->WriteMessage(s, req, length + 9, 0x02);
}

static GSM_Error N6510_ReplyGetSecurityStatus(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	if (msg->Buffer[3] == 0xf0) {
		return ERR_NOTSUPPORTED;
	}

	smprintf(s, "Security Code status received: ");
	switch (msg->Buffer[4]) {
	case 0x01 : smprintf(s, "waiting for Security Code.\n"); *Data->SecurityStatus = SEC_SecurityCode;	break;
	case 0x07 :
	case 0x02 : smprintf(s, "waiting for PIN.\n");		 *Data->SecurityStatus = SEC_Pin;		break;
	case 0x03 : smprintf(s, "waiting for PUK.\n");		 *Data->SecurityStatus = SEC_Puk;		break;
	case 0x05 : smprintf(s, "PIN ok, SIM ok\n");		 *Data->SecurityStatus = SEC_None;		break;
	case 0x06 : smprintf(s, "No input status\n"); 		 *Data->SecurityStatus = SEC_None;		break;
	case 0x16 : smprintf(s, "No SIM card\n");		 return ERR_NOSIM;
	case 0x1A : smprintf(s, "SIM card rejected!\n");	 *Data->SecurityStatus = SEC_None;		break;
	default   : smprintf(s, "ERROR: unknown %i\n",msg->Buffer[4]);
		    return ERR_UNKNOWNRESPONSE;
	}
	return ERR_NONE;
}

static GSM_Error N6510_GetSecurityStatus(GSM_StateMachine *s, GSM_SecurityCodeType *Status)
{
	unsigned char req[5] = {N6110_FRAME_HEADER, 0x11, 0x00};

	s->Phone.Data.SecurityStatus=Status;
	smprintf(s, "Getting security code status\n");
	return GSM_WaitFor (s, req, 5, 0x08, 2, ID_GetSecurityStatus);
}

static GSM_Error N6510_ReplyEnterSecurityCode(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[3]) {
	case 0x08:
		smprintf(s, "Security code OK\n");
		return ERR_NONE;
	case 0x09:
		switch (msg->Buffer[4]) {
		case 0x06:
			smprintf(s, "Wrong PIN\n");
			return ERR_SECURITYERROR;
		case 0x09:
			smprintf(s, "Wrong PUK\n");
			return ERR_SECURITYERROR;
		default:
			smprintf(s, "ERROR: unknown security code status %i\n",msg->Buffer[4]);
		}
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_EnterSecurityCode(GSM_StateMachine *s, GSM_SecurityCode *Code)
{
	int 		len = 0;
	unsigned char 	req[15] = {N6110_FRAME_HEADER, 0x07,
				   0x00};	/* Code type */

	switch (Code->Type) {
		case SEC_Pin	: req[4] = 0x02; break;
		case SEC_Puk	: req[4] = 0x03; break;/* FIXME */
		default		: return ERR_NOTSUPPORTED;
	}

	len = strlen(Code->Code);
	memcpy(req+5,Code->Code,len);
	req[5+len]=0x00;

	smprintf(s, "Entering security code\n");
	return GSM_WaitFor (s, req, 6+len, 0x08, s->Phone.Data.Priv.N6510.Timeout, ID_EnterSecurityCode);
}

static GSM_Error N6510_ReplySaveSMSMessage(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	unsigned char 		folder;
	GSM_Phone_Data		*Data = &s->Phone.Data;

	switch (msg->Buffer[3]) {
	case 0x01:
		switch (msg->Buffer[4]) {
		case 0x00:
			smprintf(s, "Done OK\n");
			break;
		case 0x02:
			smprintf(s, "Incorrect location\n");
			return ERR_INVALIDLOCATION;
		case 0x03:
			smprintf(s, "Memory full (for example no empty space in SIM)\n");
			return ERR_FULL;
		case 0x05:
			smprintf(s, "Incorrect folder\n");
			return ERR_INVALIDLOCATION;
		default:
			smprintf(s, "ERROR: unknown reply on saving message %i\n",msg->Buffer[4]);
			return ERR_UNKNOWNRESPONSE;
		}

		smprintf(s, "Folder info: %i %i\n",msg->Buffer[5],msg->Buffer[8]);
		Data->SaveSMSMessage->Memory = MEM_ME;
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
			folder = msg->Buffer[8];
		} else {
			folder = msg->Buffer[8] + 1;
			/* inbox,outbox */
			if (msg->Buffer[8] == 0x02 || msg->Buffer[8] == 0x03) {
				if (msg->Buffer[5] == 0x01) {
					folder = msg->Buffer[8] - 1;
					Data->SaveSMSMessage->Memory = MEM_SM;
				}
			}
		}
		N6510_SetSMSLocation(s, Data->SaveSMSMessage,folder,msg->Buffer[6]*256+msg->Buffer[7]);
		smprintf(s, "Saved in folder %i at location %i\n",folder, msg->Buffer[6]*256+msg->Buffer[7]);
		Data->SaveSMSMessage->Folder = folder;
		return ERR_NONE;
	case 0x17:
		smprintf(s, "SMS name changed\n");
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_PrivSetSMSMessage(GSM_StateMachine *s, GSM_SMSMessage *sms)
{
	int			length = 11;
	unsigned int		location;
	unsigned char		folderid, folder;
	GSM_SMSMessageLayout 	Layout;
	GSM_Error		error;
	unsigned char req [300] = {
		N6110_FRAME_HEADER, 0x00,
		0x01,			/* 1 = SIM, 2 = ME 	*/
		0x02,			/* Folder   		*/
		0x00, 0x01,		/* Location 		*/
		0x01};			/* SMS state 		*/
	unsigned char NameReq[200] = {
		N6110_FRAME_HEADER, 0x16,
		0x01,			/* 1 = SIM, 2 = ME 	*/
		0x02,			/* Folder   		*/
		0x00, 0x01};		/* Location 		*/

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SMS_FILES)) return ERR_NOTSUPPORTED;
	}

	N6510_GetSMSLocation(s, sms, &folderid, &location);
	if (folderid == 0x99) return ERR_INVALIDLOCATION;
	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		switch (folderid) {
			case 0x02: req[4] = 0x02; req[5] = 0x02; break; /* inbox */
			/* sms saved to sent items make problems later during reading */
			/* case 0x03: req[4] = 0x02; req[5] = 0x03; break; //sent items */
			default	 : return ERR_NOTSUPPORTED; /* at least 6111 doesn't support saving to other */
		}
	} else {
		switch (folderid) {
			case 0x01: req[5] = 0x02; 			 break; /* INBOX SIM 	*/
			case 0x02: req[5] = 0x03; 			 break; /* OUTBOX SIM 	*/
			default	 : req[5] = folderid - 1; req[4] = 0x02; break; /* ME folders	*/
		}
	}
	req[6]=location / 256;
	req[7]=location % 256;

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		switch (sms->PDU) {
		case SMS_Status_Report: /* this is SMS submit with delivery report request */
		case SMS_Submit:
			break;
		case SMS_Deliver:
			/* Sent items */
			if (folderid == 0x03) sms->PDU = SMS_Submit;
			break;
		default:
			return ERR_UNKNOWN;
		}
	} else {
		switch (sms->PDU) {
		case SMS_Status_Report: /* this is SMS submit with delivery report request */
		case SMS_Submit:
			/* Inbox */
			if (folderid == 0x01 || folderid == 0x03) sms->PDU = SMS_Deliver;
			break;
		case SMS_Deliver:
			/* SIM Outbox */
			if (folderid == 0x02) sms->PDU = SMS_Submit;
			break;
		default:
			return ERR_UNKNOWN;
		}
		if (sms->PDU == SMS_Deliver) {
			switch (sms->State) {
				case SMS_Sent	: /* We use GSM_Read, because phone return error */
				case SMS_Read	: req[8] = 0x01; break;
				case SMS_UnSent	: /* We use GSM_UnRead, because phone return error */
				case SMS_UnRead	: req[8] = 0x03; break;
			}
		} else {
			switch (sms->State) {
				case SMS_Sent	: /* We use GSM_Sent, because phone change folder */
				case SMS_Read	: req[8] = 0x05; break;
				case SMS_UnSent	: /* We use GSM_UnSent, because phone change folder */
				case SMS_UnRead	: req[8] = 0x07; break;
			}
		}
	}
	memset(req+9,0x00,sizeof(req) - 9);
	error=N6510_EncodeSMSFrame(s, sms, req + 9, &Layout, &length);
	if (error != ERR_NONE) return error;

	s->Phone.Data.SaveSMSMessage=sms;
	smprintf(s, "Saving sms\n");
	error=GSM_WaitFor (s, req, length+9, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_SaveSMSMessage);
	if (error != ERR_NONE) return error;

	/* no adding to SIM SMS */
	if (UnicodeLength(sms->Name)==0 || sms->Folder < 3) return ERR_NONE;

	folder = sms->Folder;
	sms->Folder = 0;
	N6510_GetSMSLocation(s, sms, &folderid, &location);

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) {
		switch (folderid) {
			case 0x03: NameReq[4] = 0x02; NameReq[5] = 0x02; break; /* sent items */
			case 0x02: NameReq[4] = 0x02; NameReq[5] = 0x03; break; /* inbox */
			default	 : return ERR_NOTSUPPORTED; /* at least 6111 doesn't support saving to other */
		}
	} else {
		switch (folderid) {
			case 0x01: NameReq[5] = 0x02; 				 break; /* INBOX SIM 	*/
			case 0x02: NameReq[5] = 0x03; 			 	 break; /* OUTBOX SIM 	*/
			default	 : NameReq[5] = folderid - 1; NameReq[4] = 0x02; break; /* ME folders	*/
		}
	}
	NameReq[6]=location / 256;
	NameReq[7]=location % 256;
	length = 8;
	CopyUnicodeString(NameReq+length, sms->Name);
	length = length+UnicodeLength(sms->Name)*2;
	NameReq[length++] = 0;
	NameReq[length++] = 0;
	error=GSM_WaitFor (s, NameReq, length, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_SaveSMSMessage);
	sms->Folder = folder;
	return error;
}

static GSM_Error N6510_SetSMS(GSM_StateMachine *s, GSM_SMSMessage *sms)
{
	unsigned int		location;
	unsigned char		folderid;

	N6510_GetSMSLocation(s, sms, &folderid, &location);
	if (location == 0) return ERR_INVALIDLOCATION;
	return N6510_PrivSetSMSMessage(s, sms);
}

static GSM_Error N6510_AddSMS(GSM_StateMachine *s, GSM_SMSMessage *sms)
{
	unsigned int		location;
	unsigned char		folderid;

	N6510_GetSMSLocation(s, sms, &folderid, &location);
	location = 0;
	N6510_SetSMSLocation(s, sms, folderid, location);
	return N6510_PrivSetSMSMessage(s, sms);
}

static GSM_Error N6510_ReplyGetDateTime(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	smprintf(s, "Date & time received\n");
	if (msg->Buffer[4]==0x01) {
		NOKIA_DecodeDateTime(s, msg->Buffer+10, s->Phone.Data.DateTime, TRUE, FALSE);
		return ERR_NONE;
	}
	smprintf(s, "Not set in phone\n");
	return ERR_EMPTY;
}

static GSM_Error N6510_GetDateTime(GSM_StateMachine *s, GSM_DateTime *date_time)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x0A, 0x00, 0x00};

	s->Phone.Data.DateTime=date_time;
	smprintf(s, "Getting date & time\n");
	return GSM_WaitFor (s, req, 6, 0x19, s->Phone.Data.Priv.N6510.Timeout, ID_GetDateTime);
}

static GSM_Error N6510_ReplySetDateTime(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Date & time set\n");
	return ERR_NONE;
}

static GSM_Error N6510_SetDateTime(GSM_StateMachine *s, GSM_DateTime *date_time)
{
	unsigned char req[]  = {N6110_FRAME_HEADER,
				0x01, 0x00, 0x01, 0x01, 0x0c, 0x01, 0x03,
				0x00, 0x00,	/* Year */
				0x08, 0x01,     /* Month & Day */
				0x15, 0x1f,	/* Hours & Minutes */
				0x2b,		/* Second ? */
				0x00};

	NOKIA_EncodeDateTime(s, req+10, date_time);
	req[16] = date_time->Second;
	smprintf(s, "Setting date & time\n");
	return GSM_WaitFor (s, req, 18, 0x19, s->Phone.Data.Priv.N6510.Timeout, ID_SetDateTime);
}

static GSM_Error N6510_ReplyGetManufactureMonth(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	if (msg->Buffer[7] == 0x00) {
		smprintf(s, "No SIM card\n");
		return ERR_NOSIM;
	} else {
		sprintf(s->Phone.Data.PhoneString,"%02i/%04i",msg->Buffer[13],msg->Buffer[14]*256+msg->Buffer[15]);
	        return ERR_NONE;
	}
}

static GSM_Error N6510_GetManufactureMonth(GSM_StateMachine *s, char *value)
{
	unsigned char req[6] = {0x00, 0x05, 0x02, 0x01, 0x00, 0x02};
/* 	unsigned char req[6] = {0x00, 0x03, 0x04, 0x0B, 0x01, 0x00}; */

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) return ERR_NOTSUPPORTED;

	s->Phone.Data.PhoneString=value;
	smprintf(s, "Getting manufacture month\n");
	return GSM_WaitFor (s, req, 6, 0x42, 2, ID_GetManufactureMonth);
/* 	return GSM_WaitFor (s, req, 6, 0x1B, 2, ID_GetManufactureMonth); */
}

static GSM_Error N6510_ReplyGetAlarm(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	switch(msg->Buffer[3]) {
	case 0x1A:
		smprintf(s, "   Alarm: %02d:%02d\n", msg->Buffer[14], msg->Buffer[15]);
		Data->Alarm->Repeating 		= TRUE;
		Data->Alarm->Text[0] 		= 0;
		Data->Alarm->Text[1] 		= 0;
		Data->Alarm->DateTime.Hour	= msg->Buffer[14];
		Data->Alarm->DateTime.Minute	= msg->Buffer[15];
		Data->Alarm->DateTime.Second	= 0;
		return ERR_NONE;
	case 0x20:
		smprintf(s, "Alarm state received\n");
		if (msg->Buffer[37] == 0x01) {
			smprintf(s, "   Not set in phone\n");
			return ERR_EMPTY;
		}
		smprintf(s, "Enabled\n");
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetAlarm(GSM_StateMachine *s, GSM_Alarm *Alarm)
{
	unsigned char   StateReq[] = {N6110_FRAME_HEADER, 0x1f, 0x01, 0x00};
	unsigned char   GetReq  [] = {N6110_FRAME_HEADER, 0x19, 0x00, 0x02};
	GSM_Error	error;

	if (Alarm->Location != 1) return ERR_NOTSUPPORTED;

	s->Phone.Data.Alarm=Alarm;
	smprintf(s, "Getting alarm state\n");
	error = GSM_WaitFor (s, StateReq, 6, 0x19, s->Phone.Data.Priv.N6510.Timeout, ID_GetAlarm);
	if (error != ERR_NONE) return error;

	smprintf(s, "Getting alarm\n");
	return GSM_WaitFor (s, GetReq, 6, 0x19, s->Phone.Data.Priv.N6510.Timeout, ID_GetAlarm);
}

static GSM_Error N6510_ReplySetAlarm(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Alarm set\n");
	return ERR_NONE;
}

static GSM_Error N6510_SetAlarm(GSM_StateMachine *s, GSM_Alarm *Alarm)
{
	unsigned char req[]  = {N6110_FRAME_HEADER,
				0x11, 0x00, 0x01, 0x01, 0x0c, 0x02,
				0x01, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00,		/* Hours, Minutes */
				0x00, 0x00, 0x00 };

	if (Alarm->Location != 1) return ERR_NOTSUPPORTED;

	req[14] = Alarm->DateTime.Hour;
	req[15] = Alarm->DateTime.Minute;

	smprintf(s, "Setting alarm\n");
	return GSM_WaitFor (s, req, 19, 0x19, s->Phone.Data.Priv.N6510.Timeout, ID_SetAlarm);
}

static GSM_Error N6510_ReplyGetRingtonesInfo(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	int 			tmp,i;
	GSM_Phone_Data		*Data = &s->Phone.Data;

	smprintf(s, "Ringtones info received\n");
	memset(Data->RingtonesInfo,0,sizeof(GSM_AllRingtonesInfo));
	if (msg->Buffer[4] * 256 + msg->Buffer[5] == 0x00) return ERR_EMPTY;
	Data->RingtonesInfo->Number = msg->Buffer[4] * 256 + msg->Buffer[5];
	/*  allocate array of ringtones based on number */
	Data->RingtonesInfo->Ringtone = calloc(Data->RingtonesInfo->Number, sizeof(GSM_RingtoneInfo));
	tmp = 6;
	for (i=0;i<Data->RingtonesInfo->Number;i++) {
		Data->RingtonesInfo->Ringtone[i].Group = msg->Buffer[tmp+4];
		Data->RingtonesInfo->Ringtone[i].ID    = msg->Buffer[tmp+2] * 256 + msg->Buffer[tmp+3];
		memcpy(Data->RingtonesInfo->Ringtone[i].Name,msg->Buffer+tmp+8,(msg->Buffer[tmp+6]*256+msg->Buffer[tmp+7])*2);
		smprintf(s, "%5i (%5i). \"%s\"\n",
			Data->RingtonesInfo->Ringtone[i].ID,
			Data->RingtonesInfo->Ringtone[i].Group,
			DecodeUnicodeString(Data->RingtonesInfo->Ringtone[i].Name));
		tmp = tmp + (msg->Buffer[tmp]*256+msg->Buffer[tmp+1]);
	}
	return ERR_NONE;
}

static GSM_Error N6510_PrivGetRingtonesInfo(GSM_StateMachine *s, GSM_AllRingtonesInfo *Info, gboolean AllRingtones)
{
	GSM_Error	error;
	unsigned char 	UserReq[8] = {N7110_FRAME_HEADER, 0x07, 0x00, 0x00, 0x00, 0x02};
/* 	unsigned char 	All_Req[9] = {N7110_FRAME_HEADER, 0x07, 0x00, 0x00, 0xFE, 0x00, 0x7D}; */
	unsigned char 	All_Req[8] = {N7110_FRAME_HEADER, 0x07, 0x00, 0x00, 0x00, 0x00};

	s->Phone.Data.RingtonesInfo=Info;
	smprintf(s, "Getting binary ringtones ID\n");
	if (AllRingtones) {
/* 		error = GSM_WaitFor (s, All_Req, 9, 0x1f, s->Phone.Data.Priv.N6510.Timeout, ID_GetRingtonesInfo); */
		error = GSM_WaitFor (s, All_Req, 8, 0x1f, s->Phone.Data.Priv.N6510.Timeout, ID_GetRingtonesInfo);
		if (error == ERR_EMPTY && Info->Number == 0) return ERR_NOTSUPPORTED;
		return error;
	} else {
		error = GSM_WaitFor (s, UserReq, 8, 0x1f, s->Phone.Data.Priv.N6510.Timeout, ID_GetRingtonesInfo);
		if (error == ERR_EMPTY && Info->Number == 0) return ERR_NOTSUPPORTED;
		return error;
	}
}

static GSM_Error N6510_GetRingtonesInfo(GSM_StateMachine *s, GSM_AllRingtonesInfo *Info)
{
	return N6510_PrivGetRingtonesInfo(s, Info, TRUE);
}

static GSM_Error N6510_ReplyGetRingtone(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	size_t 			tmp,i;
	GSM_Phone_Data		*Data = &s->Phone.Data;

	smprintf(s, "Ringtone received\n");
	memcpy(Data->Ringtone->Name,msg->Buffer+8,msg->Buffer[7]*2);
	Data->Ringtone->Name[msg->Buffer[7]*2]=0;
	Data->Ringtone->Name[msg->Buffer[7]*2+1]=0;
	smprintf(s, "Name \"%s\"\n",DecodeUnicodeString(Data->Ringtone->Name));
 	if (msg->Buffer[msg->Buffer[7]*2+10] == 'M' &&
 	    msg->Buffer[msg->Buffer[7]*2+11] == 'T' &&
 	    msg->Buffer[msg->Buffer[7]*2+12] == 'h' &&
 	    msg->Buffer[msg->Buffer[7]*2+13] == 'd') {
 		smprintf(s,"MIDI\n");
 		tmp 	= msg->Buffer[7]*2+10;
 		i 	= msg->Length - 2; /* ?????? */
 		Data->Ringtone->Format = RING_MIDI;
 	} else {
 		/* Looking for end */
 		i=8+msg->Buffer[7]*2+3;
 		tmp = i;
 		while (TRUE) {
 			if (msg->Buffer[i]==0x07 && msg->Buffer[i+1]==0x0b) {
 				i=i+2; break;
 			}
 			i++;
 			if (i==msg->Length) return ERR_EMPTY;
 		}
	}
	/* Copying frame */
	memcpy(Data->Ringtone->NokiaBinary.Frame,msg->Buffer+tmp,i-tmp);
	Data->Ringtone->NokiaBinary.Length=i-tmp;
	return ERR_NONE;
}

static GSM_Error N6510_GetRingtone(GSM_StateMachine *s, GSM_Ringtone *Ringtone, gboolean PhoneRingtone)
{
 	GSM_AllRingtonesInfo 	Info = {0, NULL};
	GSM_Error		error;
	unsigned char 		req2[6] = {N7110_FRAME_HEADER, 0x12,
					   0x00, 0xe7}; 	/* Location */

	if (Ringtone->Format == 0x00) Ringtone->Format = RING_NOKIABINARY;

	switch (Ringtone->Format) {
	case RING_NOTETONE:
		/* In the future get binary and convert */
		return ERR_NOTSUPPORTED;
	case RING_NOKIABINARY:
		s->Phone.Data.Ringtone	= Ringtone;
		Info.Number 		= 0;
		error=N6510_PrivGetRingtonesInfo(s, &Info, PhoneRingtone);
		if (error != ERR_NONE) return error;
		if (Ringtone->Location > Info.Number) return ERR_INVALIDLOCATION;
		req2[4] = Info.Ringtone[Ringtone->Location-1].ID / 256;
		req2[5] = Info.Ringtone[Ringtone->Location-1].ID % 256;
		smprintf(s, "Getting binary ringtone\n");
 		error = GSM_WaitFor (s, req2, 6, 0x1f, s->Phone.Data.Priv.N6510.Timeout, ID_GetRingtone);
 		if (Info.Ringtone) {
			free(Info.Ringtone);
			Info.Ringtone=NULL;
		}
 		return error;
	case RING_MIDI:
	case RING_MMF:
		return ERR_NOTSUPPORTED;
	}
	return ERR_NOTSUPPORTED;
}

static GSM_Error N6510_PlayTone(GSM_StateMachine *s, int Herz, unsigned char Volume, gboolean start)
{
	GSM_Error 	error;
	unsigned char 	reqStart[] = {
		0x00,0x06,0x01,0x00,0x07,0x00 };
	unsigned char 	reqPlay[] = {
		0x00,0x06,0x01,0x14,0x05,0x04,
		0x00,0x00,0x00,0x03,0x03,0x08,
		0x00,0x00,0x00,0x01,0x00,0x00,
		0x03,0x08,0x01,0x00,
		0x07,0xd0,	/*Frequency */
		0x00,0x00,0x03,0x08,0x02,0x00,0x00,
		0x05,		/*Volume */
		0x00,0x00};
	unsigned char 	reqOff[] = {
		0x00,0x06,0x01,0x14,0x05,0x05,
		0x00,0x00,0x00,0x01,0x03,0x08,
		0x05,0x00,0x00,0x08,0x00,0x00};
/* 	unsigned char 	reqOff2[] = { */
/* 		0x00,0x06,0x01,0x14,0x05,0x04, */
/* 		0x00,0x00,0x00,0x01,0x03,0x08, */
/* 		0x00,0x00,0x00,0x00,0x00,0x00}; */

	if (start) {
		smprintf(s, "Enabling sound - part 1\n");
		error=GSM_WaitFor (s, reqStart, 6, 0x0b, s->Phone.Data.Priv.N6510.Timeout, ID_PlayTone);
		if (error!=ERR_NONE) return error;
		smprintf(s, "Enabling sound - part 2 (disabling sound command)\n");
		error=GSM_WaitFor (s, reqOff, 18, 0x0b, s->Phone.Data.Priv.N6510.Timeout, ID_PlayTone);
		if (error!=ERR_NONE) return error;
	}

	/* For Herz==255*255 we have silent */
	if (Herz!=255*255) {
		reqPlay[23] = Herz%256;
		reqPlay[22] = Herz/256;
		reqPlay[31] = Volume;
		smprintf(s, "Playing sound\n");
		return GSM_WaitFor (s, reqPlay, 34, 0x0b, s->Phone.Data.Priv.N6510.Timeout, ID_PlayTone);
	} else {
		reqPlay[23] = 0;
		reqPlay[22] = 0;
		reqPlay[31] = 0;
		smprintf(s, "Playing silent sound\n");
		return GSM_WaitFor (s, reqPlay, 34, 0x0b, s->Phone.Data.Priv.N6510.Timeout, ID_PlayTone);

/* 		smprintf(s, "Disabling sound - part 1\n"); */
/* 		error=GSM_WaitFor (s, reqOff, 18, 0x0b, s->Phone.Data.Priv.N6510.Timeout, ID_PlayTone); */
/* 		if (error!=ERR_NONE) return error; */
/* 		smprintf(s, "Disabling sound - part 2\n"); */
/* 		return GSM_WaitFor (s, reqOff2, 18, 0x0b, s->Phone.Data.Priv.N6510.Timeout, ID_PlayTone); */
	}
}

static GSM_Error N6510_ReplyGetPPM(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Phone_Data 	*Data = &s->Phone.Data;
	size_t		pos = 6,len;

	smprintf(s, "Received phone info\n");

	while(pos < msg->Length) {
		if (msg->Buffer[pos] == 0x55 && msg->Buffer[pos+1] == 0x55) {
			while(1) {
				if (msg->Buffer[pos] != 0x55) break;
				pos++;
			}
		}
		len = pos;
		while(len < msg->Length) {
			if (msg->Buffer[len] == 0x00 && msg->Buffer[len+1] == 0x00) break;
			len++;
		}
		while(len < msg->Length) {
			if (msg->Buffer[len] != 0x00) break;
			len++;
		}
		len = len-pos;
		smprintf(s, "Block with ID %02x",msg->Buffer[pos]);
#ifdef DEBUG
		if (s->di.dl == DL_TEXTALL || s->di.dl == DL_TEXTALLDATE) DumpMessage(&s->di, msg->Buffer+pos, len);
#endif
		switch (msg->Buffer[pos]) {
		case 0x49:
			smprintf(s, "hardware version\n");
			break;
		case 0x58:
			pos += 3;
			while (msg->Buffer[pos] != 0x00) pos++;
			Data->PhoneString[0] = msg->Buffer[pos - 1];
			Data->PhoneString[1] = 0x00;
			smprintf(s, "PPM %s\n",Data->PhoneString);
			return ERR_NONE;
		default:
			break;
		}
		pos += len;
	}
	return ERR_NOTSUPPORTED;
}

static GSM_Error N6510_GetPPM(GSM_StateMachine *s,char *value)
{
/* 	unsigned char req[6] = {N6110_FRAME_HEADER, 0x07, 0x01, 0xff}; */
	unsigned char req[6] = {N6110_FRAME_HEADER, 0x07, 0x01, 0x00};

	s->Phone.Data.PhoneString=value;
	smprintf(s, "Getting PPM\n");
	return GSM_WaitFor (s, req, 6, 0x1b, 3, ID_GetPPM);
}

static GSM_Error N6510_GetSpeedDial(GSM_StateMachine *s, GSM_SpeedDial *SpeedDial)
{
	GSM_MemoryEntry 	pbk;
	GSM_Error		error;

	pbk.MemoryType			= (GSM_MemoryType)MEM7110_SP;
	pbk.Location			= SpeedDial->Location;
	SpeedDial->MemoryLocation 	= 0;
	s->Phone.Data.SpeedDial		= SpeedDial;

	smprintf(s, "Getting speed dial\n");
	error=N6510_GetMemory(s,&pbk);
	switch (error) {
	case ERR_NOTSUPPORTED:
		smprintf(s, "No speed dials set in phone\n");
		return ERR_EMPTY;
	case ERR_NONE:
		if (SpeedDial->MemoryLocation == 0) {
			smprintf(s, "Speed dial not assigned or error in firmware\n");
			return ERR_EMPTY;
		}
		return ERR_NONE;
	default:
		return error;
	}
}

static GSM_Error N6510_ReplyGetProfile(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	unsigned char 	*blockstart;
	int 		i,j;
	GSM_Phone_Data	*Data = &s->Phone.Data;

	switch (msg->Buffer[3]) {
	case 0x02:
	blockstart = msg->Buffer + 7;
	for (i = 0; i < 11; i++) {
		smprintf(s, "Profile feature %02x ",blockstart[1]);
#ifdef DEBUG
		if (s->di.dl == DL_TEXTALL || s->di.dl == DL_TEXTALLDATE) DumpMessage(&s->di, blockstart, blockstart[0]);
#endif

		switch (blockstart[1]) {
		case 0x03:
			smprintf(s, "Ringtone ID\n");
			Data->Profile->FeatureID	[Data->Profile->FeaturesNumber] = Profile_RingtoneID;
			Data->Profile->FeatureValue	[Data->Profile->FeaturesNumber] = blockstart[7];
			if (blockstart[7] == 0x00) {
				Data->Profile->FeatureValue[Data->Profile->FeaturesNumber] = blockstart[10];
			}
			Data->Profile->FeaturesNumber++;
			break;
		case 0x05:	/* SMS tone */
			j = Data->Profile->FeaturesNumber;
			NOKIA_FindFeatureValue(s, Profile71_65,blockstart[1],blockstart[7],Data,FALSE);
			if (j == Data->Profile->FeaturesNumber) {
				Data->Profile->FeatureID	[Data->Profile->FeaturesNumber] = Profile_MessageTone;
				Data->Profile->FeatureValue	[Data->Profile->FeaturesNumber] = PROFILE_MESSAGE_PERSONAL;
				Data->Profile->FeaturesNumber++;
				Data->Profile->FeatureID	[Data->Profile->FeaturesNumber] = Profile_MessageToneID;
				Data->Profile->FeatureValue	[Data->Profile->FeaturesNumber] = blockstart[7];
				Data->Profile->FeaturesNumber++;
			}
			break;
		case 0x08:	/* Caller groups */
			NOKIA_FindFeatureValue(s, Profile71_65,blockstart[1],blockstart[7],Data,TRUE);
			break;
		case 0x0c :
			CopyUnicodeString(Data->Profile->Name,blockstart + 7);
			smprintf(s, "profile Name: \"%s\"\n", DecodeUnicodeString(Data->Profile->Name));
			Data->Profile->DefaultName = FALSE;
			break;
		default:
			NOKIA_FindFeatureValue(s, Profile71_65,blockstart[1],blockstart[7],Data,FALSE);
		}
		blockstart = blockstart + blockstart[0];
	}
	return ERR_NONE;
	case 0x06:
		Data->Profile->Active = FALSE;
		if (Data->Profile->Location == msg->Buffer[5]) Data->Profile->Active = TRUE;
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetProfile(GSM_StateMachine *s, GSM_Profile *Profile)
{
	unsigned char 	req[150]    = {N6110_FRAME_HEADER, 0x01, 0x01, 0x0C, 0x01};
	unsigned char	reqActive[] = {N6110_FRAME_HEADER, 0x05};
	int 		i, length = 7;
	GSM_Error	error;

	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_PROFILES)) return ERR_NOTSUPPORTED;

/* 	if (!strcmp(s->Phone.Data.ModelInfo->model,"3510")) { */
/* 		if (s->Phone.Data.VerNum>3.37) return ERR_NOTSUPPORTED; */
/* 	if (!strcmp(s->Phone.Data.ModelInfo->model,"6230")) { */
/* 	if (!strcmp(s->Phone.Data.ModelInfo->model,"6220")) { */
/* 	if (!strcmp(s->Phone.Data.ModelInfo->model,"5140")) { */
/* 	if (!strcmp(s->Phone.Data.ModelInfo->model,"6230i")) { */
/* 	if (!strcmp(s->Phone.Data.ModelInfo->model,"6020")) { */

	if (Profile->Location>5) return ERR_INVALIDLOCATION;

	for (i = 0; i < 0x0a; i++) {
		req[length++] = 0x04;
		req[length++] = Profile->Location;
		req[length++] = i;
		req[length++] = 0x01;
	}

	req[length++] = 0x04;
	req[length++] = Profile->Location;
	req[length++] = 0x0c;
	req[length++] = 0x01;

	req[length++] = 0x04;

	Profile->CarKitProfile	= FALSE;
	Profile->HeadSetProfile	= FALSE;

	Profile->FeaturesNumber = 0;

	s->Phone.Data.Profile=Profile;
	smprintf(s, "Getting profile\n");
	error = GSM_WaitFor (s, req, length, 0x39, s->Phone.Data.Priv.N6510.Timeout, ID_GetProfile);
	if (error != ERR_NONE) return error;

	smprintf(s, "Checking, which profile is active\n");
	return GSM_WaitFor (s, reqActive, 4, 0x39, s->Phone.Data.Priv.N6510.Timeout, ID_GetProfile);
}

static GSM_Error N6510_ReplySetProfile(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	unsigned char 	*blockstart;
	int 		i;

	smprintf(s, "Response to profile writing received!\n");

	blockstart = msg->Buffer + 6;
	for (i = 0; i < msg->Buffer[5]; i++) {
		switch (blockstart[2]) {
			case 0x00: smprintf(s, "keypad tone level"); 	break;
			case 0x02: smprintf(s, "call alert"); 		break;
			case 0x03: smprintf(s, "ringtone"); 		break;
			case 0x04: smprintf(s, "ringtone volume"); 	break;
			case 0x05: smprintf(s, "SMS tone"); 		break;
			case 0x06: smprintf(s, "vibration"); 		break;
			case 0x07: smprintf(s, "warning tone level"); 	break;
			case 0x08: smprintf(s, "caller groups"); 	break;
			case 0x09: smprintf(s, "automatic answer"); 	break;
			case 0x0c: smprintf(s, "name"); 		break;
			default:
				smprintf(s, "Unknown block type %02x", blockstart[2]);
				break;
		}
		if (msg->Buffer[4] == 0x00) {
			smprintf(s, ": set OK\n");
		} else {
			smprintf(s, ": setting error %i\n", msg->Buffer[4]);
		}
		blockstart = blockstart + blockstart[1];
	}
	return ERR_NONE;
}

static GSM_Error N6510_SetProfile(GSM_StateMachine *s, GSM_Profile *Profile)
{
	int 		i, length = 7, blocks = 0;
	gboolean		found;
	unsigned char	ID,Value;
	unsigned char 	req[150] = {N6110_FRAME_HEADER, 0x03, 0x01,
				    0x06,		/* Number of blocks */
				    0x03};

	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_PROFILES)) return ERR_NOTSUPPORTED;

	if (Profile->Location>5) return ERR_INVALIDLOCATION;

	for (i=0;i<Profile->FeaturesNumber;i++) {
		found = FALSE;
		switch (Profile->FeatureID[i]) {
			case Profile_RingtoneID:
				ID 	= 0x03;
				Value 	= Profile->FeatureValue[i];
				found 	= TRUE;
				break;
			default:
				found=NOKIA_FindPhoneFeatureValue(
					s,
					Profile71_65,
					Profile->FeatureID[i],Profile->FeatureValue[i],
					&ID,&Value);
		}
		if (found) {
			req[length] 	= 0x09;
			req[length + 1] = ID;
			req[length + 2] = Profile->Location;
			memcpy(req + length + 4, "\x00\x00\x01", 3);
			req[length + 8] = 0x03;
			req[length + 3] = req[length + 7] = Value;
			blocks++;
			length += 9;
		}
	}

	smprintf(s, "Setting profile\n");
	return GSM_WaitFor (s, req, length, 0x39, s->Phone.Data.Priv.N6510.Timeout, ID_SetProfile);
}

static GSM_Error N6510_ReplyIncomingCB(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[3]) {
		case 0x21:
			smprintf(s, "Ignoring cell broadcast ok event\n");
			break;
		case 0x22:
			smprintf(s, "Ignoring cell broadcast fail event\n");
			break;
		case 0x23:
			smprintf(s, "Ignoring cell broadcast read event\n");
			break;
		default:
			smprintf(s, "Ignoring cell broadcast event 0x%02x\n", msg->Buffer[3]);
			break;
	}
	return ERR_NONE;
}
static GSM_Error N6510_ReplyIncomingSMS(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_SMSMessage 	sms;
	size_t 		i;

#ifdef DEBUG
	smprintf(s, "SMS message received\n");
	N6510_DecodeSMSFrame(s, &sms, msg->Buffer+10,&i);
#endif

	if (s->Phone.Data.EnableIncomingSMS && s->User.IncomingSMS!=NULL) {
		GSM_SetDefaultSMSData(&sms);

		sms.State 	 = SMS_UnRead;
		sms.InboxFolder  = TRUE;

		N6510_DecodeSMSFrame(s, &sms, msg->Buffer+10,&i);

		s->User.IncomingSMS(s, &sms, s->User.IncomingSMSUserData);
	}
	return ERR_NONE;
}

static GSM_Error N6510_DialVoice(GSM_StateMachine *s, char *number, GSM_CallShowNumber ShowNumber)
{
	unsigned int 	pos2 = 15;
	unsigned int	pos = 4;
	unsigned char 	req2[100] = {N6110_FRAME_HEADER,0x01,
				    0x00,0x02,0x07,0x04,
				    0x01,  /*  1 - voice, 2 - data */
				    0x00,0x03,
				    0x18,  /*  length of rest + 1 */
				    0x00,0x00,0x00};
	unsigned char 	req[100] = {N6110_FRAME_HEADER,0x01,
				    0x0c};	/* Number length */
	GSM_Error	error;

	/* USSD not supported */
	if (number[0] == '*') return ERR_NOTSUPPORTED;
	if (number[0] == '#') return ERR_NOTSUPPORTED;

	req[pos++] = strlen(number);
	EncodeUnicode(req+pos,number,strlen(number));
	pos += strlen(number)*2;
	req[pos++] = 0x05; /* call type: voice - 0x05, data - 0x01 */
	req[pos++] = 0x01;
	req[pos++] = 0x05;
	req[pos++] = 0x00;
	req[pos++] = 0x02;
	req[pos++] = 0x00;
	req[pos++] = 0x00;
	switch (ShowNumber) {
	case GSM_CALL_HideNumber:
		req[pos++] = 0x02;
		break;
	case GSM_CALL_ShowNumber:
		req[pos++] = 0x03;
		break;
	case GSM_CALL_DefaultNumberPresence:
		req[pos++] = 0x01;
		break;
	}
	smprintf(s, "Making voice call\n");
	error = GSM_WaitFor (s, req, pos, 0x01, s->Phone.Data.Priv.N6510.Timeout, ID_DialVoice);
	if (error != ERR_NOTSUPPORTED) return error;

	if (ShowNumber != GSM_CALL_DefaultNumberPresence) return ERR_NOTSUPPORTED;

	req2[11] = strlen(number)*2+6;
	req2[pos2++] = strlen(number);
	EncodeUnicode(req2+pos2,number,strlen(number));
	pos2 += strlen(number)*2;

	smprintf(s, "Making voice call\n");
	error = GSM_WaitFor (s, req2, pos2, 0x01, s->Phone.Data.Priv.N6510.Timeout, ID_DialVoice);
	if (error == ERR_NOTSUPPORTED) return ERR_NONE;
	return error;
}

static GSM_Error N6510_ReplyLogIntoNetwork(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Probably phone says: I log into network\n");
	return ERR_NONE;
}

void N6510_EncodeFMFrequency(double freq, unsigned char *buff)
{
	unsigned int		freq2;

 	freq2 = (unsigned int)(freq * 100);
 	freq2	= freq2 - 0xffff;
 	buff[0] = freq2 / 0x100;
 	buff[1] = freq2 % 0x100;
}

void N6510_DecodeFMFrequency(double *freq, unsigned char *buff)
{
	*freq = (double)(0xffff + buff[0] * 0x100 + buff[1])/1000.0;
}

static GSM_Error N6510_ReplyGetFMStatus(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
  	smprintf(s, "getting FM status OK\n");
	memcpy(s->Phone.Data.Priv.N6510.FMStatus,msg->Buffer,msg->Length);
	s->Phone.Data.Priv.N6510.FMStatusLength = msg->Length;
	return ERR_NONE;
}

static GSM_Error N6510_GetFMStatus(GSM_StateMachine *s)
{
 	unsigned char req[7] = {N6110_FRAME_HEADER, 0x0d, 0x00, 0x00, 0x01};

 	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_RADIO)) return ERR_NOTSUPPORTED;
 	return GSM_WaitFor (s, req, 7, 0x3E, 2, ID_GetFMStation);
}

static GSM_Error N6510_ReplyGetFMStation(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
  	unsigned char 	name[GSM_MAX_FMSTATION_LENGTH*2+2];
  	int		length;
  	GSM_Phone_Data	*Data = &s->Phone.Data;

	switch (msg->Buffer[3]) {
	case 0x06:
	  	smprintf(s, "Received FM station\n");
 		length = msg->Buffer[8];
		if (length > GSM_MAX_FMSTATION_LENGTH) {
			smprintf(s, "FM station name too long (%d), truncating!\n", length);
			length = GSM_MAX_FMSTATION_LENGTH;
		}
 		memcpy(name,msg->Buffer+18,length*2);
 		name[length*2]	 = 0x00;
 		name[length*2+1] = 0x00;
 		CopyUnicodeString(Data->FMStation->StationName,name);
		smprintf(s,"Station name: \"%s\"\n",DecodeUnicodeString(Data->FMStation->StationName));
		N6510_DecodeFMFrequency(&Data->FMStation->Frequency, msg->Buffer+16);
		return ERR_NONE;
	case 0x16:
	  	smprintf(s, "Received FM station. Empty ?\n");
		return ERR_EMPTY;
	case 0xf0:
	  	smprintf(s, "FM radio not supported\n");
		return ERR_NOTSUPPORTED;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetFMStation (GSM_StateMachine *s, GSM_FMStation *FMStation)
{
 	GSM_Error 		error;
 	int			location;
  	unsigned char 		req[7] = {N6110_FRAME_HEADER, 0x05,
 					  0x00, 		/*  location */
	    				  0x00,0x01};

  	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_RADIO)) return ERR_NOTSUPPORTED;
   	if (FMStation->Location > GSM_MAX_FM_STATION) return ERR_INVALIDLOCATION;

  	s->Phone.Data.FMStation = FMStation;

 	error = N6510_GetFMStatus(s);
 	if (error != ERR_NONE) return error;

 	location = FMStation->Location-1;
  	if (s->Phone.Data.Priv.N6510.FMStatus[14+location] == 0xFF) return ERR_EMPTY;
 	req[4]   = s->Phone.Data.Priv.N6510.FMStatus[14+location];

 	smprintf(s, "Getting FM Station %i\n",FMStation->Location);
 	return GSM_WaitFor (s, req, 7, 0x3E, 2, ID_GetFMStation);
}

static GSM_Error N6510_ReplySetFMStation(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
#ifdef DEBUG
 	switch (msg->Buffer[4]){
 		case 0x03: smprintf(s, "FM stations cleaned\n");	  break;
 		case 0x11: smprintf(s, "Setting FM station status OK\n"); break;
 		case 0x12: smprintf(s, "Setting FM station OK\n");	  break;
 	}
#endif
 	return ERR_NONE;
}

static GSM_Error N6510_ClearFMStations (GSM_StateMachine *s)
{
	unsigned char req[7] = {N6110_FRAME_HEADER, 0x03,0x0f,0x00,0x01};

	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_RADIO)) return ERR_NOTSUPPORTED;

	smprintf(s, "Cleaning FM Stations\n");
	return GSM_WaitFor (s, req, 7, 0x3E, 2, ID_SetFMStation);
}

static GSM_Error N6510_SetFMStation (GSM_StateMachine *s, GSM_FMStation *FMStation)
{
	unsigned int 		len, location;
 	GSM_Error 		error;
 	unsigned char setstatus[36] = {N6110_FRAME_HEADER,0x11,0x00,0x01,0x01,
 	    			0x00,0x00,0x1c,0x00,0x14,0x00,0x00,
 				0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
 				0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
 				0xff,0xff,0xff,0xff,0xff,0x01};
 	unsigned char req[64] = {N6110_FRAME_HEADER, 0x12,0x00,0x01,0x00,
 				0x00, 			/*  0x0e + (strlen(name) * 2) */
 				0x00,			/*  strlen(name) */
 				0x14,0x09,0x00,
 				0x00, 			/*  location */
 				0x00,0x00,0x01,
 				0x00, 			/*  freqHi */
 				0x00, 			/*  freqLo */
 				0x01};

 	if (!GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_RADIO)) return ERR_NOTSUPPORTED;

 	s->Phone.Data.FMStation = FMStation;
 	location = FMStation->Location-1;

 	error = N6510_GetFMStatus(s);
 	if (error != ERR_NONE) return error;

 	memcpy(setstatus+14,s->Phone.Data.Priv.N6510.FMStatus+14,20);
 	setstatus [14+location] = location;

 	smprintf(s, "Setting FM status %i\n",FMStation->Location);
 	error = GSM_WaitFor (s, setstatus, 36 , 0x3E, 2, ID_SetFMStation);
 	if (error != ERR_NONE) return error;

 	req[12] = location;

	/* Name */
 	len 	= UnicodeLength(FMStation->StationName);
 	req[8] 	= len;
 	req[7] 	= 0x0e + len * 2;
 	memcpy (req+18,FMStation->StationName,len*2);

	/* Frequency */
	N6510_EncodeFMFrequency(FMStation->Frequency, req+16);

 	smprintf(s, "Setting FM Station %i\n",FMStation->Location);
 	return GSM_WaitFor (s, req, 0x13+len*2, 0x3E, 2, ID_SetFMStation);
}

static GSM_Error N6510_ReplySetLight(GSM_Protocol_Message *msg UNUSED, GSM_StateMachine *s)
{
	smprintf(s, "Light set\n");
	return ERR_NONE;
}

GSM_Error N6510_SetLight(GSM_StateMachine *s, N6510_PHONE_LIGHTS light, gboolean enable)
{
	unsigned char req[14] = {
		N6110_FRAME_HEADER, 0x05,
		0x01,		/* 0x01 = Display, 0x03 = keypad */
		0x01,		/* 0x01 = Enable, 0x02 = disable */
		0x00, 0x00, 0x00, 0x01,
		0x05, 0x04, 0x02, 0x00};

	req[4] = light;
	if (!enable) req[5] = 0x02;
 	smprintf(s, "Setting light\n");
	return GSM_WaitFor (s, req, 14, 0x3A, s->Phone.Data.Priv.N6510.Timeout, ID_SetLight);
}

static GSM_Error N6510_ShowStartInfo(GSM_StateMachine *s, gboolean enable)
{
	GSM_Error error;

	if (enable) {
		error=N6510_SetLight(s,N6510_LIGHT_DISPLAY,TRUE);
		if (error != ERR_NONE) return error;

		error=N6510_SetLight(s,N6510_LIGHT_TORCH,TRUE);
		if (error != ERR_NONE) return error;

		return N6510_SetLight(s,N6510_LIGHT_KEYPAD,TRUE);
	} else {
		error=N6510_SetLight(s,N6510_LIGHT_DISPLAY,FALSE);
		if (error != ERR_NONE) return error;

		error=N6510_SetLight(s,N6510_LIGHT_TORCH,FALSE);
		if (error != ERR_NONE) return error;

		return N6510_SetLight(s,N6510_LIGHT_KEYPAD,FALSE);
	}
}

static GSM_Error N6510_ReplyGetGPRSAccessPoint(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_GPRSAccessPoint *point = s->Phone.Data.GPRSPoint;

	switch (msg->Buffer[13]) {
	case 0x01:
		smprintf(s,"Active GPRS point received\n");
		point->Active = FALSE;
		if (point->Location == msg->Buffer[18]) point->Active = TRUE;
		return ERR_NONE;
	case 0xD2:
		smprintf(s,"Names for GPRS points received\n");
		CopyUnicodeString(point->Name,msg->Buffer+18+(point->Location-1)*42);
		smprintf(s,"\"%s\"\n",DecodeUnicodeString(point->Name));
		return ERR_NONE;
	case 0xF2:
		smprintf(s,"URL for GPRS points received\n");
		CopyUnicodeString(point->URL,msg->Buffer+18+(point->Location-1)*202);
		smprintf(s,"\"%s\"\n",DecodeUnicodeString(point->URL));
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetGPRSAccessPoint(GSM_StateMachine *s, GSM_GPRSAccessPoint *point)
{
	GSM_Error	error;
	unsigned char 	URL[] = {
		N7110_FRAME_HEADER, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00,
		0x00, 0x00, 0x00, 0x03, 0xF2, 0x00, 0x00};
	unsigned char 	Name[] = {
		N7110_FRAME_HEADER, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00,
		0x01, 0x00, 0x00, 0x00, 0xD2, 0x00, 0x00};
	unsigned char 	Active[] = {
		N7110_FRAME_HEADER, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00,
		0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_NOGPRSPOINT)) return ERR_NOTSUPPORTED;
	if (point->Location < 1) return ERR_UNKNOWN;
	if (point->Location > 5) return ERR_INVALIDLOCATION;

	s->Phone.Data.GPRSPoint = point;

	smprintf(s, "Getting GPRS access point name\n");
	error=GSM_WaitFor (s, Name, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_GetGPRSPoint);
	if (error != ERR_NONE) return error;

	smprintf(s, "Getting GPRS access point URL\n");
	error=GSM_WaitFor (s, URL, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_GetGPRSPoint);
	if (error != ERR_NONE) return error;

	smprintf(s, "Getting number of active GPRS access point\n");
	error=GSM_WaitFor (s, Active, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_GetGPRSPoint);
	if (error != ERR_NONE) return error;

	if (UnicodeLength(point->URL)==0 && UnicodeLength(point->Name)==0) return ERR_EMPTY;
	return error;
}

static GSM_Error N6510_ReplySetGPRSAccessPoint1(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	switch (msg->Buffer[13]) {
	case 0x01:
	case 0xD2:
	case 0xF2:
		memcpy(s->Phone.Data.Priv.N6510.GPRSPoints,msg->Buffer,msg->Length);
		s->Phone.Data.Priv.N6510.GPRSPointsLength = msg->Length;
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_SetGPRSAccessPoint(GSM_StateMachine *s, GSM_GPRSAccessPoint *point)
{
	unsigned char	*buff = s->Phone.Data.Priv.N6510.GPRSPoints;
	GSM_Error	error;
	unsigned char 	URL[] = {
		N7110_FRAME_HEADER, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00,
		0x00, 0x00, 0x00, 0x03, 0xF2, 0x00, 0x00};
	unsigned char 	Name[] = {
		N7110_FRAME_HEADER, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00,
		0x01, 0x00, 0x00, 0x00, 0xD2, 0x00, 0x00};
	unsigned char 	Active[] = {
		N7110_FRAME_HEADER, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00,
		0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_NOGPRSPOINT)) return ERR_NOTSUPPORTED;
	if (point->Location < 1) return ERR_UNKNOWN;
	if (point->Location > 5) return ERR_INVALIDLOCATION;

	s->Phone.Data.GPRSPoint = point;

	smprintf(s, "Getting GPRS access point name\n");
	error=GSM_WaitFor (s, Name, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetGPRSPoint);
	if (error != ERR_NONE) return error;
	CopyUnicodeString(buff+18+(point->Location-1)*42,point->Name);
	buff[0] = 0x00;
	buff[1] = 0x01;
	buff[2] = 0x01;
	buff[3] = 0x07;
	smprintf(s, "Setting GPRS access point name\n");
	error=GSM_WaitFor (s, buff, s->Phone.Data.Priv.N6510.GPRSPointsLength, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetGPRSPoint);
	if (error != ERR_NONE) return error;

	smprintf(s, "Getting GPRS access point URL\n");
	error=GSM_WaitFor (s, URL, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetGPRSPoint);
	if (error != ERR_NONE) return error;
	CopyUnicodeString(buff+18+(point->Location-1)*42,point->URL);
	buff[0] = 0x00;
	buff[1] = 0x01;
	buff[2] = 0x01;
	buff[3] = 0x07;
	smprintf(s, "Setting GPRS access point URL\n");
	error=GSM_WaitFor (s, buff, s->Phone.Data.Priv.N6510.GPRSPointsLength, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetGPRSPoint);
	if (error != ERR_NONE) return error;

	if (point->Active) {
		smprintf(s, "Getting number of active GPRS access point\n");
		error=GSM_WaitFor (s, Active, 16, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetGPRSPoint);
		if (error != ERR_NONE) return error;
		buff[0] = 0x00;
		buff[1] = 0x01;
		buff[2] = 0x01;
		buff[3] = 0x07;
		buff[18]= point->Location;
		smprintf(s, "Setting number of active GPRS access point\n");
		error=GSM_WaitFor (s, buff, s->Phone.Data.Priv.N6510.GPRSPointsLength, 0x43, s->Phone.Data.Priv.N6510.Timeout, ID_SetGPRSPoint);
		if (error != ERR_NONE) return error;
	}

	return error;
}

static GSM_Error N6510_ReplyGetLocale(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	GSM_Locale *locale = s->Phone.Data.Locale;

	switch (msg->Buffer[3]) {
	case 0x8A:
		smprintf(s, "Date settings received\n");
		switch (msg->Buffer[4]) {
		case 0x00:
			locale->DateFormat 	= GSM_Date_DDMMYYYY;
			locale->DateSeparator 	= '.';
			break;
		case 0x01:
			locale->DateFormat 	= GSM_Date_MMDDYYYY;
			locale->DateSeparator 	= '.';
			break;
		case 0x02:
			locale->DateFormat 	= GSM_Date_YYYYMMDD;
			locale->DateSeparator 	= '.';
			break;
		case 0x04:
			locale->DateFormat 	= GSM_Date_DDMMYYYY;
			locale->DateSeparator 	= '/';
			break;
		case 0x05:
			locale->DateFormat 	= GSM_Date_MMDDYYYY;
			locale->DateSeparator 	= '/';
			break;
		case 0x06:
			locale->DateFormat 	= GSM_Date_YYYYMMDD;
			locale->DateSeparator 	= '/';
			break;
		case 0x08:
			locale->DateFormat 	= GSM_Date_DDMMYYYY;
			locale->DateSeparator 	= '-';
			break;
		case 0x09:
			locale->DateFormat 	= GSM_Date_MMDDYYYY;
			locale->DateSeparator 	= '-';
			break;
		case 0x0A:
			locale->DateFormat 	= GSM_Date_YYYYMMDD;
			locale->DateSeparator 	= '-';
			break;
		default:/* FIXME */
			locale->DateFormat 	= GSM_Date_DDMMYYYY;
			locale->DateSeparator 	= '/';
			break;
		}
		return ERR_NONE;
	}
	return ERR_UNKNOWNRESPONSE;
}

static GSM_Error N6510_GetLocale(GSM_StateMachine *s, GSM_Locale *locale)
{
	unsigned char req[] = {N6110_FRAME_HEADER, 0x89};

	s->Phone.Data.Locale = locale;

	smprintf(s, "Getting date format\n");
	return GSM_WaitFor (s, req, 4, 0x13, s->Phone.Data.Priv.N6510.Timeout, ID_GetLocale);
}

GSM_Error N6510_CancelCall(GSM_StateMachine *s, int ID, gboolean all)
{
	if (all) return ERR_NOTSUPPORTED;
	return DCT3DCT4_CancelCall(s,ID);
}

GSM_Error N6510_AnswerCall(GSM_StateMachine *s, int ID, gboolean all)
{
	if (all) return ERR_NOTSUPPORTED;
	return DCT3DCT4_AnswerCall(s,ID);
}

static GSM_Error N6510_ReplyAddSMSFolder(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	smprintf(s,"SMS folder \"%s\" has been added\n",DecodeUnicodeString(msg->Buffer+10));
	return ERR_NONE;
}

GSM_Error N6510_AddSMSFolder(GSM_StateMachine *s, unsigned char *name)
{
	unsigned char req[200] = {N6110_FRAME_HEADER, 0x10, 0x01, 0x00, 0x01,
			          0x00,     		/* Length */
				  0x00, 0x00};

	CopyUnicodeString(req+10,name);
	req[7] = UnicodeLength(name)*2 + 6;

	smprintf(s, "Adding SMS folder\n");
	return GSM_WaitFor (s, req, req[7] + 6, 0x14, s->Phone.Data.Priv.N6510.Timeout, ID_AddSMSFolder);
}

static GSM_Error N6510_SetWAPBookmark(GSM_StateMachine *s, GSM_WAPBookmark *bookmark)
{
	GSM_Error 	error;
	int 		count=4, location;
	unsigned char 	req[600] = {N6110_FRAME_HEADER, 0x09};

	/* We have to enable WAP frames in phone */
	error=N6510_EnableConnectionFunctions(s,N6510_WAP_SETTINGS);
	if (error!=ERR_NONE) return error;

	location = bookmark->Location - 1;
	if (bookmark->Location == 0) location = 0xffff;
	req[count++] = (location & 0xff00) >> 8;
	req[count++] = location & 0x00ff;

	count += NOKIA_SetUnicodeString(s, req+count, bookmark->Title,   TRUE);
	count += NOKIA_SetUnicodeString(s, req+count, bookmark->Address, TRUE);

	req[count++] = 0x00;
	req[count++] = 0x00;
	req[count++] = 0x00;
	req[count++] = 0x00;

	smprintf(s, "Setting WAP bookmark\n");
	error = GSM_WaitFor (s, req, count, 0x3f, s->Phone.Data.Priv.N6510.Timeout, ID_SetWAPBookmark);
	if (error != ERR_NONE) {
		if (error == ERR_INSIDEPHONEMENU || error == ERR_EMPTY || error == ERR_FULL) {
			DCT3DCT4_DisableConnectionFunctions(s);
		}
		return error;
	}

	return DCT3DCT4_DisableConnectionFunctions(s);
}

GSM_Error N6510_DeleteWAPBookmark(GSM_StateMachine *s, GSM_WAPBookmark *bookmark)
{
	GSM_Error error;

	/* We have to enable WAP frames in phone */
	error=N6510_EnableConnectionFunctions(s,N6510_WAP_SETTINGS);
	if (error!=ERR_NONE) return error;

	return DCT3DCT4_DeleteWAPBookmarkPart(s,bookmark);
}

GSM_Error N6510_GetWAPBookmark(GSM_StateMachine *s, GSM_WAPBookmark *bookmark)
{
	GSM_Error error;

	if (GSM_IsPhoneFeatureAvailable(s->Phone.Data.ModelInfo, F_SERIES40_30)) return ERR_NOTSUPPORTED;

	/* We have to enable WAP frames in phone */
	error=N6510_EnableConnectionFunctions(s,N6510_WAP_SETTINGS);
	if (error!=ERR_NONE) return error;

	return DCT3DCT4_GetWAPBookmarkPart(s,bookmark);
}

static GSM_Error DCT4_ReplyGetScreenDump(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	int len;

	/* Data is at offset 10 */

	len = msg->Buffer[7] | (msg->Buffer[6] << 8);
	smprintf(s, "Received screenshot part, length %d\n", len);
	s->Phone.Data.Picture->Buffer = realloc(s->Phone.Data.Picture->Buffer, s->Phone.Data.Picture->Length + len);
	if (s->Phone.Data.Picture->Buffer == NULL) {
		return ERR_MOREMEMORY;
	}
	memcpy(s->Phone.Data.Picture->Buffer + s->Phone.Data.Picture->Length, msg->Buffer + 10, len);
	s->Phone.Data.Picture->Length += len;
	if (len < 500) {
		return ERR_NONE;
	}
	return ERR_NEEDANOTHERANSWER;
}

static GSM_Error DCT4_ReplyGetScreenInfo(GSM_Protocol_Message *msg, GSM_StateMachine *s)
{
	s->Phone.Data.Priv.N6510.ScreenWidth = msg->Buffer[5] | (msg->Buffer[4] << 8);
	s->Phone.Data.Priv.N6510.ScreenHeight = msg->Buffer[7] | (msg->Buffer[6] << 8);
	smprintf(s, "Screen size %dx%d\n", s->Phone.Data.Priv.N6510.ScreenWidth, s->Phone.Data.Priv.N6510.ScreenHeight);

	return ERR_NONE;
}

GSM_Error DCT4_Screenshot(GSM_StateMachine *s, GSM_BinaryPicture *picture)
{
	unsigned char req_data[] = {N6110_FRAME_HEADER, 0x07, 0x01, 0x00};
	unsigned char req_screen[] = {N6110_FRAME_HEADER, 0x06, 0x01, 0x00};
	unsigned char bmp_header[] = {0x42, 0x4D, /* BMP magic "BM" */
				0, 0, 0, 0, /* Size of BMP in bytes */
				0, 0, 0, 0, /* reserved */
				0x7a, 0, 0, 0 , /* Offset of data */
				0x6c, 0, 0, 0, /* Size of header */
				0, 0, 0, 0, /* width */
				0, 0, 0, 0, /* height */
				1, 0, /* color planes */
				32, 0, /* bpp */
				3, 0, 0, 0, /* compression, BI_BITFIELDS */
				32, 0, 0, 0, /* Size of image pixel */
				0x13,0x0b,0x00,0x00, /* XPelsPerMeter */
				0x13,0x0b,0x00,0x00, /* YPelsPerMeter */
				0, 0, 0, 0, /* palette */
				0, 0, 0, 0, /* important colors */
				0, 0xFF, 0, 0, /* red mask */
				0, 0, 0xFF, 0, /* green mask */
				0, 0, 0, 0xFF, /* blue mask */
				0xFF, 0, 0, 0, /* alpha mask */
				0x20, 0x6E, 0x69, 0x57, /* LCS_WINDOWS_COLOR_SPACE */
				0, 0, 0, 0, /* CIEXYZTRIPLE */
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0,
				0, 0, 0, 0, /* red gamma */
				0, 0, 0, 0, /* green gamma */
				0, 0, 0, 0, /* blue gamma */
	};
	GSM_Error error;
	int tmp;

	/* Get screen size */
	error = GSM_WaitFor(s, req_screen, 6, 0x0E, 4, ID_GetScreenSize);
	if (error != ERR_NONE) {
		return error;
	}

	/* Allocate buffer */
	s->Phone.Data.Picture = picture;
	picture->Type = PICTURE_BMP;
	picture->Buffer = malloc(sizeof(bmp_header));
	if (picture->Buffer == NULL) {
		return ERR_MOREMEMORY;
	}
	memcpy(picture->Buffer, bmp_header, sizeof(bmp_header));
	picture->Length = sizeof(bmp_header);

	/* Fill BMP header */
#define STORE_INT(data, pos) picture->Buffer[pos] = data & 0xff; picture->Buffer[pos + 1] = (data >> 8) & 0xff; picture->Buffer[pos + 2] = (data >> 16) & 0xff; picture->Buffer[pos + 3] = (data >> 24) & 0xff;
	/* Size */
	tmp = 0x7a + (s->Phone.Data.Priv.N6510.ScreenWidth * s->Phone.Data.Priv.N6510.ScreenHeight * 4);
	STORE_INT(tmp, 2);
	tmp = s->Phone.Data.Priv.N6510.ScreenWidth;
	STORE_INT(tmp, 18);
	tmp = -s->Phone.Data.Priv.N6510.ScreenHeight;
	STORE_INT(tmp, 22);

	/* And fill in bitmap */
	return  GSM_WaitFor(s, req_data, 6, 0x0E, 4, ID_Screenshot);
}


static GSM_Reply_Function N6510ReplyFunctions[] = {
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x02,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x03,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x04,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x05,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x07,ID_AnswerCall	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x07,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x09,ID_CancelCall	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x09,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x0A,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x0B,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x0C,ID_DialVoice		  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x0C,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x0F,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x10,ID_DialVoice		  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x10,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x23,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x25,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x27,ID_IncomingFrame	  },
	{N71_65_ReplySendDTMF,		  "\x01",0x03,0x51,ID_SendDTMF		  },
	{N71_65_ReplySendDTMF,		  "\x01",0x03,0xf0,ID_SendDTMF		  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0x53,ID_IncomingFrame	  },
	{N71_65_ReplySendDTMF,		  "\x01",0x03,0x59,ID_SendDTMF		  },
	{N71_65_ReplySendDTMF,		  "\x01",0x03,0x5E,ID_SendDTMF		  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0xA6,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0xD2,ID_IncomingFrame	  },
	{N71_65_ReplyCallInfo,		  "\x01",0x03,0xD3,ID_IncomingFrame	  },
	{N6510_ReplyPressKey,		  "\x01",0x03,0x33,ID_PressKey		  },
	{N6510_ReplyPressKey,		  "\x01",0x03,0x12,ID_PressKey		  },

	{N6510_ReplySendSMSMessage,	  "\x02",0x03,0x03,ID_IncomingFrame	  },
	{N6510_ReplyIncomingSMS,	  "\x02",0x03,0x04,ID_IncomingFrame	  },
	{N6510_ReplySetSMSC,		  "\x02",0x03,0x13,ID_SetSMSC		  },
	{N6510_ReplyGetSMSC,		  "\x02",0x03,0x15,ID_GetSMSC		  },
	{N6510_ReplyIncomingCB,		  "\x02",0x03,0x21,ID_IncomingFrame	  },
	{N6510_ReplyIncomingCB,		  "\x02",0x03,0x22,ID_IncomingFrame	  },
	{N6510_ReplyIncomingCB,		  "\x02",0x03,0x23,ID_IncomingFrame	  },

	{N6510_ReplyGetMemoryStatus,	  "\x03",0x03,0x04,ID_GetMemoryStatus	  },
	{N6510_ReplyGetMemory,		  "\x03",0x03,0x08,ID_GetMemory		  },
	{N71_65_ReplyWritePhonebook,	  "\x03",0x03,0x0C,ID_SetBitmap		  },
	{N71_65_ReplyWritePhonebook,	  "\x03",0x03,0x0C,ID_SetMemory		  },
	{N6510_ReplyDeleteMemory,	  "\x03",0x03,0x10,ID_SetMemory		  },

	{DCT3DCT4_ReplyCallDivert,	  "\x06",0x03,0x02,ID_Divert		  },
	{N71_65_ReplyUSSDInfo,		  "\x06",0x03,0x03,ID_IncomingFrame	  },
	{NoneReply,			  "\x06",0x03,0x06,ID_IncomingFrame	  },
	{NoneReply,			  "\x06",0x03,0x09,ID_IncomingFrame	  },

	{N6510_ReplyEnterSecurityCode,	  "\x08",0x03,0x08,ID_EnterSecurityCode	  },
	{N6510_ReplyEnterSecurityCode,	  "\x08",0x03,0x09,ID_EnterSecurityCode	  },
	{N6510_ReplyGetSecurityStatus,	  "\x08",0x03,0x12,ID_GetSecurityStatus	  },
	{N6510_ReplyGetSecurityStatus,	  "\x08",0x03,0xf0,ID_GetSecurityStatus	  },

	{N6510_ReplyGetNetworkInfo,	  "\x0A",0x03,0x01,ID_GetNetworkInfo	  },
	{N6510_ReplyGetNetworkInfo,	  "\x0A",0x03,0xf0,ID_GetNetworkInfo	  },
	{N6510_ReplyGetSignalQuality,	  "\x0A",0x03,0x0C,ID_GetSignalQuality	  },
	{N6510_ReplyGetOperatorLogo,	  "\x0A",0x03,0x24,ID_GetBitmap		  },
	{N6510_ReplyGetOperatorLogo,	  "\x0A",0x03,0xf0,ID_GetBitmap		  },
	{N6510_ReplySetOperatorLogo,	  "\x0A",0x03,0x26,ID_SetBitmap		  },
	/* This needs to be last of type */
	{N6510_ReplyGetNetworkInfo,	  "\x0A",0x03,0x01,ID_IncomingFrame	  },
	{N6510_ReplyLogIntoNetwork,	  "\x0A",0x03,0x02,ID_IncomingFrame	  },
	{NoneReply,			  "\x0A",0x03,0x16,ID_IncomingFrame	  },
	{N6510_ReplyGetIncSignalQuality,  "\x0A",0x03,0x1E,ID_IncomingFrame	  },
	{NoneReply,			  "\x0A",0x03,0x20,ID_IncomingFrame	  },
	{N6510_ReplyGetNetworkInfo,	  "\x0A",0x03,0xf0,ID_IncomingFrame	  },

	{NoneReply,			  "\x0B",0x03,0x01,ID_PlayTone		  },
	{NoneReply,			  "\x0B",0x03,0x15,ID_PlayTone		  },
	{NoneReply,			  "\x0B",0x03,0x16,ID_PlayTone		  },

	{N71_65_ReplyAddCalendar1,	  "\x13",0x03,0x02,ID_SetCalendarNote	  },
	{N71_65_ReplyAddCalendar1,	  "\x13",0x03,0x04,ID_SetCalendarNote	  },
	{N71_65_ReplyAddCalendar1,	  "\x13",0x03,0x06,ID_SetCalendarNote	  },
	{N71_65_ReplyAddCalendar1,	  "\x13",0x03,0x08,ID_SetCalendarNote	  },
	{N71_65_ReplyDelCalendar,	  "\x13",0x03,0x0C,ID_DeleteCalendarNote  },
	{N71_65_ReplyDelCalendar,	  "\x13",0x03,0xF0,ID_DeleteCalendarNote  },
	{N71_65_ReplyGetNextCalendar1,	  "\x13",0x03,0x1A,ID_GetCalendarNote	  },/*method 1*/
	{N6510_ReplyGetCalendarNotePos,	  "\x13",0x03,0x32,ID_GetCalendarNotePos  },/*method 1*/
	{N6510_ReplyGetCalendarNotePos,	  "\x13",0x03,0xF0,ID_GetCalendarNotePos  },/*method 1*/
	{N6510_ReplyGetCalendarInfo,	  "\x13",0x03,0x3B,ID_GetCalendarNotesInfo},/*method 1*/
#ifdef DEBUG
	{N71_65_ReplyGetNextCalendar2,	  "\x13",0x03,0x3F,ID_GetCalendarNote	  },
#endif
	{N71_65_ReplyAddCalendar2,	  "\x13",0x03,0x41,ID_SetCalendarNote	  },/*method 2*/
	{N6510_ReplyAddCalendar3,	  "\x13",0x03,0x66,ID_SetCalendarNote	  },/*method 3*/
	{N6510_ReplyAddToDo2,		  "\x13",0x03,0x66,ID_SetToDo		  },
	{N6510_ReplyAddNote,		  "\x13",0x03,0x66,ID_SetNote		  },
	{N6510_ReplyGetCalendar3,	  "\x13",0x03,0x7E,ID_GetCalendarNote	  },/*method 3*/
	{N6510_ReplyGetToDo2,		  "\x13",0x03,0x7E,ID_GetToDo		  },
	{N6510_ReplyGetNote,		  "\x13",0x03,0x7E,ID_GetNote		  },
	{N6510_ReplyGetCalendarSettings,  "\x13",0x03,0x86,ID_GetCalendarSettings },
	{N6510_ReplyGetLocale,		  "\x13",0x03,0x8A,ID_GetLocale		  },
	{N6510_ReplyGetCalendarSettings,  "\x13",0x03,0x8E,ID_GetCalendarSettings },
	{N6510_ReplyGetCalendarNotePos,	  "\x13",0x03,0x96,ID_GetCalendarNotePos  },/*method 3*/
	{N6510_ReplyGetToDoFirstLoc2,	  "\x13",0x03,0x96,ID_SetToDo		  },
	{N6510_ReplyGetNoteFirstLoc,	  "\x13",0x03,0x96,ID_SetNote		  },
	{N6510_ReplyGetCalendarInfo,	  "\x13",0x03,0x9F,ID_GetCalendarNotesInfo},/*method 3*/
	{N6510_ReplyGetToDoStatus2,	  "\x13",0x03,0x9F,ID_GetToDo		  },
	{N6510_ReplyGetNoteInfo,	  "\x13",0x03,0x9F,ID_GetNote		  },

	{N6510_ReplySaveSMSMessage,	  "\x14",0x03,0x01,ID_SaveSMSMessage	  },
	{N6510_ReplySetPicture,		  "\x14",0x03,0x01,ID_SetBitmap		  },
	{N6510_ReplyGetSMSMessage,	  "\x14",0x03,0x03,ID_GetSMSMessage	  },
	{N6510_ReplyDeleteSMSMessage,	  "\x14",0x03,0x05,ID_DeleteSMSMessage	  },
	{N6510_ReplyDeleteSMSMessage,	  "\x14",0x03,0x06,ID_DeleteSMSMessage	  },
	{N6510_ReplyGetSMSStatus,	  "\x14",0x03,0x09,ID_GetSMSStatus	  },
	{N6510_ReplyGetSMSFolderStatus,	  "\x14",0x03,0x0d,ID_GetSMSFolderStatus  },
	{N6510_ReplyGetSMSMessage,	  "\x14",0x03,0x0f,ID_GetSMSMessage	  },
	{N6510_ReplyAddSMSFolder,	  "\x14",0x03,0x11,ID_AddSMSFolder	  },
	{N6510_ReplyGetSMSFolders,	  "\x14",0x03,0x13,ID_GetSMSFolders	  },
	{N6510_ReplySaveSMSMessage,	  "\x14",0x03,0x17,ID_SaveSMSMessage	  },
	{N6510_ReplyGetSMSStatus,	  "\x14",0x03,0x1a,ID_GetSMSStatus	  },
	{N6510_ReplyGetSMSStatus,	  "\x14",0x03,0xf0,ID_GetSMSStatus	  },

	{DCT4_ReplySetPhoneMode,	  "\x15",0x03,0x64,ID_Reset		  },
	{DCT4_ReplyGetPhoneMode,	  "\x15",0x03,0x65,ID_Reset		  },
	{NoneReply,		  	  "\x15",0x03,0x68,ID_Reset		  },

	{N6510_ReplyGetBatteryCharge,	  "\x17",0x03,0x0B,ID_GetBatteryCharge	  },
	{N6510_IncomingBatteryCharge,	  "\x17",0x03,0x2c,ID_IncomingFrame	  },

	{N6510_ReplySetDateTime,	  "\x19",0x03,0x02,ID_SetDateTime	  },
	{N6510_ReplyGetDateTime,	  "\x19",0x03,0x0B,ID_GetDateTime	  },
	{N6510_ReplySetAlarm,		  "\x19",0x03,0x12,ID_SetAlarm		  },
	{N6510_ReplyGetAlarm,		  "\x19",0x03,0x1A,ID_GetAlarm		  },
	{N6510_ReplyGetAlarm,		  "\x19",0x03,0x20,ID_GetAlarm		  },

	{DCT4_ReplyGetIMEI,		  "\x1B",0x03,0x01,ID_GetIMEI		  },
	{NOKIA_ReplyGetPhoneString,	  "\x1B",0x03,0x08,ID_GetHardware	  },
	{N6510_ReplyGetPPM,		  "\x1B",0x03,0x08,ID_GetPPM		  },
	{NOKIA_ReplyGetPhoneString,	  "\x1B",0x03,0x0C,ID_GetProductCode	  },

	/* 0x1C - vibra */

	{N6510_ReplyGetRingtonesInfo,	  "\x1f",0x03,0x08,ID_GetRingtonesInfo	  },
	{N6510_ReplyDeleteRingtones,	  "\x1f",0x03,0x11,ID_SetRingtone	  },
	{N6510_ReplyGetRingtone,	  "\x1f",0x03,0x13,ID_GetRingtone	  },
	{N6510_ReplySetBinRingtone,	  "\x1f",0x03,0x0F,ID_SetRingtone	  },

	/* 0x23 - voice records */

	{N6510_ReplyGetProfile,		  "\x39",0x03,0x02,ID_GetProfile	  },
	{N6510_ReplySetProfile,		  "\x39",0x03,0x04,ID_SetProfile	  },
	{N6510_ReplyGetProfile,		  "\x39",0x03,0x06,ID_GetProfile	  },

	{N6510_ReplySetLight,		  "\x3A",0x03,0x06,ID_SetLight		  },

 	{N6510_ReplyGetFMStation,	  "\x3E",0x03,0x06,ID_GetFMStation	  },
 	{N6510_ReplyGetFMStatus,	  "\x3E",0x03,0x0E,ID_GetFMStation	  },
 	{N6510_ReplySetFMStation,	  "\x3E",0x03,0x15,ID_SetFMStation	  },
 	{N6510_ReplyGetFMStation,	  "\x3E",0x03,0x16,ID_GetFMStation	  },
 	{N6510_ReplyGetFMStation,	  "\x3E",0x03,0xF0,ID_GetFMStation	  },

	{DCT3DCT4_ReplyEnableConnectFunc, "\x3f",0x03,0x01,ID_EnableConnectFunc	  },
	{DCT3DCT4_ReplyEnableConnectFunc, "\x3f",0x03,0x02,ID_EnableConnectFunc	  },
	{DCT3DCT4_ReplyDisableConnectFunc,"\x3f",0x03,0x04,ID_DisableConnectFunc  },
	{DCT3DCT4_ReplyDisableConnectFunc,"\x3f",0x03,0x05,ID_DisableConnectFunc  },
	{N6510_ReplyGetWAPBookmark,	  "\x3f",0x03,0x07,ID_GetWAPBookmark	  },
	{N6510_ReplyGetWAPBookmark,	  "\x3f",0x03,0x08,ID_GetWAPBookmark	  },
	{DCT3DCT4_ReplySetWAPBookmark,	  "\x3f",0x03,0x0A,ID_SetWAPBookmark	  },
	{DCT3DCT4_ReplySetWAPBookmark,	  "\x3f",0x03,0x0B,ID_SetWAPBookmark	  },
	{DCT3DCT4_ReplyDelWAPBookmark,	  "\x3f",0x03,0x0D,ID_DeleteWAPBookmark	  },
	{DCT3DCT4_ReplyDelWAPBookmark,	  "\x3f",0x03,0x0E,ID_DeleteWAPBookmark	  },
	{DCT3DCT4_ReplyGetActiveConnectSet,"\x3f",0x03,0x10,ID_GetConnectSet	  },
	{DCT3DCT4_ReplySetActiveConnectSet,"\x3f",0x03,0x13,ID_SetConnectSet	  },
	{N6510_ReplyGetConnectionSettings,"\x3f",0x03,0x11,ID_GetConnectSet	  },
	{N6510_ReplyGetConnectionSettings,"\x3f",0x03,0x16,ID_GetConnectSet	  },
	{N6510_ReplyGetConnectionSettings,"\x3f",0x03,0x17,ID_GetConnectSet	  },
	{N6510_ReplySetConnectionSettings,"\x3f",0x03,0x19,ID_SetConnectSet	  },
	{N6510_ReplySetConnectionSettings,"\x3f",0x03,0x1A,ID_SetConnectSet	  },
	{N6510_ReplySetConnectionSettings,"\x3f",0x03,0x28,ID_SetConnectSet	  },
	{N6510_ReplySetConnectionSettings,"\x3f",0x03,0x2B,ID_SetConnectSet	  },
	{N6510_ReplyGetChatSettings,	  "\x3f",0x03,0x3B,ID_GetChatSettings	  },
	{N6510_ReplyGetChatSettings,	  "\x3f",0x03,0x3C,ID_GetChatSettings	  },
	{N6510_ReplyGetConnectionSettings,"\x3f",0x03,0xf0,ID_GetConnectSet	  },

	{N6510_ReplyGetOriginalIMEI,	  "\x42",0x07,0x00,ID_GetOriginalIMEI	  },
	{N6510_ReplyGetManufactureMonth,  "\x42",0x07,0x00,ID_GetManufactureMonth },
	{N6510_ReplyGetOriginalIMEI,	  "\x42",0x07,0x01,ID_GetOriginalIMEI	  },
	{N6510_ReplyGetManufactureMonth,  "\x42",0x07,0x02,ID_GetManufactureMonth },

	{N6510_ReplySetOperatorLogo,	  "\x43",0x03,0x08,ID_SetBitmap		  },
	{N6510_ReplyGetGPRSAccessPoint,	  "\x43",0x03,0x06,ID_GetGPRSPoint	  },
	{N6510_ReplySetGPRSAccessPoint1,  "\x43",0x03,0x06,ID_SetGPRSPoint	  },
	{N6510_ReplyGetSyncMLSettings,	  "\x43",0x03,0x06,ID_GetSyncMLSettings	  },
	{N6510_ReplyGetSyncMLName,	  "\x43",0x03,0x06,ID_GetSyncMLName	  },
	{NoneReply,			  "\x43",0x03,0x08,ID_SetGPRSPoint	  },

	/* 0x4A - voice records */

	/* 0x53 - simlock */

	{N6510_ReplyAddToDo1,		  "\x55",0x03,0x02,ID_SetToDo		  },
	{N6510_ReplyGetToDo1,		  "\x55",0x03,0x04,ID_GetToDo		  },
	{N6510_ReplyGetToDoFirstLoc1,	  "\x55",0x03,0x10,ID_SetToDo		  },
	{N6510_ReplyDeleteAllToDo1,	  "\x55",0x03,0x12,ID_DeleteAllToDo	  },
	{N6510_ReplyGetToDoStatus1,	  "\x55",0x03,0x16,ID_GetToDo		  },

	{N6510_ReplyAddFileHeader1,	  "\x6D",0x03,0x03,ID_AddFile		  },
	{N6510_ReplyAddFolder1,		  "\x6D",0x03,0x05,ID_AddFolder		  },
	{N6510_ReplyGetFilePart12,	  "\x6D",0x03,0x0F,ID_GetFile		  },/* filesystem1&2 */
	{N6510_ReplyAddFileHeader1,	  "\x6D",0x03,0x13,ID_AddFile		  },
	{N6510_ReplyGetFileFolderInfo1,	  "\x6D",0x03,0x15,ID_GetFileInfo	  },
	{NoneReply,			  "\x6D",0x03,0x19,ID_SetAttrib		  },
	{N6510_ReplyDeleteFileFolder1,	  "\x6D",0x03,0x1F,ID_DeleteFile	  },
	{N6510_ReplyGetFileSystemStatus1, "\x6D",0x03,0x23,ID_FileSystemStatus	  },
	{N6510_ReplyGetFileFolderInfo1,	  "\x6D",0x03,0x2F,ID_GetFileInfo	  },
	{N6510_ReplyGetFileSystemStatus1, "\x6D",0x03,0x2F,ID_FileSystemStatus	  },
	{N6510_ReplyGetFileFolderInfo1,	  "\x6D",0x03,0x33,ID_GetFileInfo	  },
	{N6510_ReplyGetFileFolderInfo1,	  "\x6D",0x03,0xF0,ID_GetFileInfo	  },
	{N6510_ReplyAddFilePart1,	  "\x6D",0x03,0x41,ID_AddFile		  },
	{N6510_ReplyGetFileCRC12,	  "\x6D",0x03,0x43,ID_GetCRC		  },
	{NoneReply,			  "\x6D",0x03,0x59,ID_AddFile		  },/* filesystem2 */
	{N6510_ReplyGetFilePart12,	  "\x6D",0x03,0x5F,ID_GetFile		  },/* filesystem2 */
	{N6510_ReplyDeleteFile2,	  "\x6D",0x03,0x63,ID_DeleteFile	  },/* filesystem2 */
	{N6510_ReplyAddFolder2,		  "\x6D",0x03,0x65,ID_AddFolder		  },/* filesystem2 */
	{N6510_ReplyGetFileCRC12,   	  "\x6D",0x03,0x67,ID_GetCRC		  },/* filesystem2 */
	{N6510_ReplyGetFileFolderInfo2,   "\x6D",0x03,0x69,ID_IncomingFrame	  },/* filesystem2 */
	{N6510_ReplyDeleteFolder2,	  "\x6D",0x03,0x6B,ID_DeleteFolder	  },/* filesystem2 */
	{N6510_ReplyGetFileFolderInfo2,   "\x6D",0x03,0x6D,ID_GetFileInfo	  },/* filesystem2 */
	{N6510_ReplySetAttrib2,		  "\x6D",0x03,0x6F,ID_SetAttrib		  },/* filesystem2 */
	{N6510_ReplyOpenFile2,		  "\x6D",0x03,0x73,ID_OpenFile		  },/* filesystem2 */
	{NoneReply,			  "\x6D",0x03,0x75,ID_CloseFile		  },/* filesystem2 */
	{N6510_ReplySetFileDate2,	  "\x6D",0x03,0x87,ID_AddFile		  },/* filesystem2 */

	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x01,ID_GetBitmap		  },
	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x01,ID_SetBitmap		  },
	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x0F,ID_GetBitmap		  },
	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x0F,ID_SetBitmap		  },
	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x10,ID_GetBitmap		  },
	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x10,ID_SetBitmap		  },
	{N6510_ReplyStartupNoteLogo,	  "\x7A",0x04,0x25,ID_SetBitmap		  },

	{DCT3DCT4_ReplyGetModelFirmware,  "\xD2",0x02,0x00,ID_GetModel		  },
	{DCT3DCT4_ReplyGetModelFirmware,  "\xD2",0x02,0x00,ID_GetFirmware	  },

	/* 0xD7 - Bluetooth */

	{N6510_ReplyGetRingtoneID,	  "\xDB",0x03,0x02,ID_SetRingtone	  },

	{DCT4_ReplyGetScreenDump,	  "\x0E",0x00,0x00,ID_Screenshot		},
	{DCT4_ReplyGetScreenInfo,	  "\x0E",0x00,0x00,ID_GetScreenSize	  },

	{NULL,				  "\x00",0x00,0x00,ID_None		  }
};

GSM_Phone_Functions N6510Phone = {
	"1100|1100a|1100b|1112|1200|1209|1600|1650|1680|2600|2610|2630|2650|2660|2680|2760|3100|3100b|3105|3108|3109c|3200|3200a|3205|3220|3300|3510|3510i|3530|3589i|3590|3595|5000|5100|5140|5140i|5200|5220|5300|5310|6020|6020b|6021|6030|6060|6070|6085|6086|6100|6101|6103|6111|6125|6131|6151|6170|6200|6220|6220c|6225|6230|6230i|6233|6234|6270|6275i|6280|6300|6303c|6310|6310i|6340i|6385|6500c|6500s|6510|6600|6610|6610i|6800|6810|6820|6822|7200|7210|7210s|7230|7250|7250i|7260|7270|7360|7370|7500|7600|7900|8310|8390|8800|8910|8910i",
	N6510ReplyFunctions,
	NOTSUPPORTED,			/* 	Install			*/
	N6510_Initialise,
	N6510_Terminate,
	GSM_DispatchMessage,
	N6510_ShowStartInfo,
	NOKIA_GetManufacturer,
	DCT3DCT4_GetModel,
	DCT3DCT4_GetFirmware,
	DCT4_GetIMEI,
	N6510_GetOriginalIMEI,
	N6510_GetManufactureMonth,
	DCT4_GetProductCode,
	DCT4_GetHardware,
	N6510_GetPPM,
	NOTSUPPORTED,			/*	GetSIMIMSI		*/
	N6510_GetDateTime,
	N6510_SetDateTime,
	N6510_GetAlarm,
	N6510_SetAlarm,
	N6510_GetLocale,
	NOTSUPPORTED,			/* 	SetLocale		*/
	N6510_PressKey,
	DCT4_Reset,
	NOTIMPLEMENTED,			/*	ResetPhoneSettings	*/
	N6510_EnterSecurityCode,
	N6510_GetSecurityStatus,
	NOTSUPPORTED,			/*	GetDisplayStatus	*/
	NOTIMPLEMENTED,			/*	SetAutoNetworkLogin	*/
	N6510_GetBatteryCharge,
	N6510_GetSignalQuality,
	N6510_GetNetworkInfo,
	NOTSUPPORTED,       		/*  	GetCategory 		*/
 	NOTSUPPORTED,       		/*  	AddCategory 		*/
	NOTSUPPORTED,        		/*  	GetCategoryStatus 	*/
	N6510_GetMemoryStatus,
	N6510_GetMemory,
	NOTIMPLEMENTED,			/*	GetNextMemory		*/
	N6510_SetMemory,
	N6510_AddMemory,
	N6510_DeleteMemory,
	NOTIMPLEMENTED,			/*	DeleteAllMemory		*/
	N6510_GetSpeedDial,
	NOTIMPLEMENTED,			/*	SetSpeedDial		*/
	N6510_GetSMSC,
	N6510_SetSMSC,
	N6510_GetSMSStatus,
	N6510_GetSMSMessage,
	N6510_GetNextSMSMessage,
	N6510_SetSMS,
	N6510_AddSMS,
	N6510_DeleteSMSMessage,
	N6510_SendSMSMessage,
	NOTSUPPORTED,			/*	SendSavedSMS		*/
	NOTSUPPORTED,			/*	SetFastSMSSending	*/
	NOKIA_SetIncomingSMS,
	NOTIMPLEMENTED,			/* 	SetIncomingCB		*/
	N6510_GetSMSFolders,
 	N6510_AddSMSFolder,
 	NOTIMPLEMENTED,			/* 	DeleteSMSFolder		*/
	N6510_DialVoice,
        NOTIMPLEMENTED,			/*	DialService		*/
 	N6510_AnswerCall,
 	N6510_CancelCall,
 	NOTIMPLEMENTED,			/* 	HoldCall 		*/
 	NOTIMPLEMENTED,			/* 	UnholdCall 		*/
 	NOTIMPLEMENTED,			/* 	ConferenceCall 		*/
 	NOTIMPLEMENTED,			/* 	SplitCall		*/
 	NOTIMPLEMENTED,			/* 	TransferCall		*/
 	NOTIMPLEMENTED,			/* 	SwitchCall		*/
 	DCT3DCT4_GetCallDivert,
 	DCT3DCT4_SetCallDivert,
 	DCT3DCT4_CancelAllDiverts,
	NOKIA_SetIncomingCall,
	NOKIA_SetIncomingUSSD,
	DCT3DCT4_SendDTMF,
	N6510_GetRingtone,
	N6510_SetRingtone,
	N6510_GetRingtonesInfo,
	N6510_DeleteUserRingtones,
	N6510_PlayTone,
	N6510_GetWAPBookmark,
	N6510_SetWAPBookmark,
	N6510_DeleteWAPBookmark,
	N6510_GetWAPSettings,
	N6510_SetWAPSettings,
	N6510_GetSyncMLSettings,
	NOTSUPPORTED,			/*	SetSyncMLSettings	*/
	N6510_GetChatSettings,
	NOTSUPPORTED,			/*	SetChatSettings		*/
	N6510_GetMMSSettings,
	N6510_SetMMSSettings,
	N6510_GetMMSFolders,
	N6510_GetNextMMSFileInfo,
	N6510_GetBitmap,
	N6510_SetBitmap,
	N6510_GetToDoStatus,
	NOTIMPLEMENTED,			/*	GetToDo			*/
	N6510_GetNextToDo,
	NOTIMPLEMENTED,			/*	SetToDo			*/
	N6510_AddToDo,
	N6510_DeleteToDo2,
	N6510_DeleteAllToDo1,
	N6510_GetCalendarStatus,
	NOTIMPLEMENTED,			/*	GetCalendar		*/
    	N6510_GetNextCalendar,
	NOTIMPLEMENTED,			/*	SetCalendar		*/
	N6510_AddCalendar,
	N71_65_DelCalendar,
	NOTIMPLEMENTED,			/*	DeleteAllCalendar	*/
	N6510_GetCalendarSettings,
	NOTSUPPORTED,			/* 	SetCalendarSettings	*/
	N6510_GetNoteStatus,
	NOTIMPLEMENTED,			/*	GetNote			*/
	N6510_GetNextNote,
	NOTIMPLEMENTED,			/*	SetNote			*/
	N6510_AddNote,
	N6510_DeleteNote,
	NOTSUPPORTED,			/*	DeleteAllNotes		*/
	N6510_GetProfile,
	N6510_SetProfile,
    	N6510_GetFMStation,
     	N6510_SetFMStation,
 	N6510_ClearFMStations,
	N6510_GetNextFileFolder,
	N6510_GetFolderListing,
	N6510_GetNextRootFolder,
	N6510_SetFileAttributes,
	N6510_GetFilePart,
	N6510_AddFilePart,
	NOTSUPPORTED,			/* 	SendFilePart		*/
	N6510_GetFileSystemStatus,
	N6510_DeleteFile,
	N6510_AddFolder,
	N6510_DeleteFolder,
	N6510_GetGPRSAccessPoint,
	N6510_SetGPRSAccessPoint,
	DCT4_Screenshot,
	NOTSUPPORTED,			/* 	SetPower		*/
	NOTSUPPORTED			/* 	PostConnect	*/
};

#endif

/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */