summaryrefslogtreecommitdiffstats
path: root/libcamera/SecCameraHWInterface.cpp
blob: 98f8aeec34ebc788391fa03196dc5b7fe79e7eda (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
/*
**
** Copyright 2008, The Android Open Source Project
** Copyright@ Samsung Electronics Co. LTD
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

//#define LOG_NDEBUG 0
#define LOG_TAG "CameraHardwareSec"
#include <utils/Log.h>

#include "SecCameraHWInterface.h"
#include <utils/threads.h>
#include <fcntl.h>
#include <sys/mman.h>

#if defined(BOARD_USES_OVERLAY)
#include <hardware/overlay.h>
#include <ui/Overlay.h>
#define CACHEABLE_BUFFERS       0x1
#define ALL_BUFFERS_FLUSHED     -66
int buf_idx = 0;
#endif

#ifdef SEND_YUV_RECORD_DATA
#define ALIGN_TO_32B(x)   ((((x) + (1 <<  5) - 1) >>  5) <<  5)
#define ALIGN_TO_128B(x)  ((((x) + (1 <<  7) - 1) >>  7) <<  7)
#define ALIGN_TO_8KB(x)   ((((x) + (1 << 13) - 1) >> 13) << 13)
#define RECORD_HEAP_SIZE (ALIGN_TO_8KB(ALIGN_TO_128B(1280) *        \
            ALIGN_TO_32B(720)) + ALIGN_TO_8KB(ALIGN_TO_128B(1280) * \
            ALIGN_TO_32B(720 / 2)))
#endif

namespace android {

struct addrs {
    unsigned int addr_y;
    unsigned int addr_cbcr;
    unsigned int buf_index;
    unsigned int reserved;
};

struct addrs_cap {
    unsigned int addr_y;
    unsigned int width;
    unsigned int height;
};

CameraHardwareSec::CameraHardwareSec(int cameraId)
        : mParameters(),
          mPreviewHeap(0),
          mRawHeap(0),
          mRecordHeap(0),
          mJpegHeap(0),
          mSecCamera(NULL),
          mPreviewRunning(false),
          mPreviewFrameSize(0),
          mRawFrameSize(0),
          mPreviewFrameRateMicrosec(33000),
          mNotifyCb(0),
          mDataCb(0),
          mDataCbTimestamp(0),
          mCallbackCookie(0),
          mMsgEnabled(0),
          mCurrentPreviewFrame(0),
#if defined(BOARD_USES_OVERLAY)
          mUseOverlay(false),
#endif
          mRecordRunning(false)
#ifdef JPEG_FROM_SENSOR
          ,
          mPostViewWidth(0),
          mPostViewHeight(0),
          mPostViewSize(0)
#endif
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
          ,
          mObjectTrackingStatus(0),
          mSmartautosceneRunning(false),
          mSmartautoscene_current_status(0),
          mSmartautoscene_previous_status(0)
#endif
{
    LOGV("%s()", __func__);
    int ret = 0;
    mNoHwHandle = 0;

    mSecCamera = SecCamera::createInstance();

    if (mSecCamera == NULL) {
        LOGE("ERR(%s):Fail on mSecCamera object creation", __func__);
    }

    ret = mSecCamera->initCamera(cameraId);
    if (ret < 0) {
        LOGE("ERR(%s):Fail on mSecCamera init", __func__);
    }

    if (mSecCamera->flagCreate() == 0) {
        LOGE("ERR(%s):Fail on mSecCamera->flagCreate()", __func__);
    }

#ifndef PREVIEW_USING_MMAP
    int previewHeapSize = sizeof(struct addrs) * kBufferCount;

    LOGV("mPreviewHeap : MemoryHeapBase(previewHeapSize(%d))", previewHeapSize);
    mPreviewHeap = new MemoryHeapBase(previewHeapSize);
    if (mPreviewHeap->getHeapID() < 0) {
            LOGE("ERR(%s): Preview heap creation fail", __func__);
                mPreviewHeap.clear();
    }
#endif

#ifdef SEND_YUV_RECORD_DATA
    int recordHeapSize = RECORD_HEAP_SIZE;
#else
    int recordHeapSize = sizeof(struct addrs) * kBufferCount;
#endif
    LOGV("mRecordHeap : MemoryHeapBase(recordHeapSize(%d))", recordHeapSize);
    mRecordHeap = new MemoryHeapBase(recordHeapSize);
    if (mRecordHeap->getHeapID() < 0) {
            LOGE("ERR(%s): Record heap creation fail", __func__);
                mRecordHeap.clear();
    }

#ifdef JPEG_FROM_SENSOR
    mSecCamera->getPostViewConfig(&mPostViewWidth, &mPostViewHeight, &mPostViewSize);
    LOGV("mPostViewWidth = %d mPostViewHeight = %d mPostViewSize = %d",
            mPostViewWidth,mPostViewHeight,mPostViewSize);
#endif

#ifdef DIRECT_DELIVERY_OF_POSTVIEW_DATA
    int rawHeapSize = mPostViewSize;
#else
    int rawHeapSize = sizeof(struct addrs_cap);
#endif
    LOGV("mRawHeap : MemoryHeapBase(previewHeapSize(%d))", rawHeapSize);
    mRawHeap = new MemoryHeapBase(rawHeapSize);
    if (mRawHeap->getHeapID() < 0) {
            LOGE("ERR(%s): Raw heap creation fail", __func__);
                mRawHeap.clear();
    }

    initDefaultParameters(cameraId);
}

void CameraHardwareSec::initDefaultParameters(int cameraId)
{
    if (mSecCamera == NULL) {
        LOGE("ERR(%s):mSecCamera object is NULL", __func__);
        return;
    }

    CameraParameters p;

    int preview_max_width   = 0;
    int preview_max_height  = 0;
    int snapshot_max_width  = 0;
    int snapshot_max_height = 0;

    p.set("camera-id", cameraId);
    /* set camera ID & reset camera */
    mSecCamera->setCameraId(cameraId);
    if (cameraId == SecCamera::CAMERA_ID_BACK) {
        p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
                "800x480,640x480");
        p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
                "2560x1920,2048x1536,1600x1200,800x480,640x480");
    }
    else {
        p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
                "640x480,320x240,160x120");
        p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
                "640x480");
    }

    // If these fail, then we are using an invalid cameraId and we'll leave the
    // sizes at zero to catch the error.
    if (mSecCamera->getPreviewMaxSize(&preview_max_width, &preview_max_height) < 0)
        LOGE("getPreviewMaxSize fail (%d / %d) \n", preview_max_width, preview_max_height);
    if (mSecCamera->getSnapshotMaxSize(&snapshot_max_width, &snapshot_max_height) < 0)
        LOGE("getSnapshotMaxSize fail (%d / %d) \n", snapshot_max_width, snapshot_max_height);

#ifdef PREVIEW_USING_MMAP
    p.setPreviewFormat(CameraParameters::PIXEL_FORMAT_YUV420SP);
#else
    p.setPreviewFormat("yuv420sp_custom");
#endif
    p.setPreviewSize(preview_max_width, preview_max_height);
    p.setPreviewFrameRate(30);

    p.setPictureFormat(CameraParameters::PIXEL_FORMAT_JPEG);
    p.setPictureSize(snapshot_max_width, snapshot_max_height);
    p.set(CameraParameters::KEY_JPEG_QUALITY, "100"); // maximum quality
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS,
        CameraParameters::PIXEL_FORMAT_YUV420SP);
    p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, "30,15");
    p.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS,
        CameraParameters::PIXEL_FORMAT_JPEG);
    p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES,
            "160x120,0x0");
    p.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT,
        CameraParameters::PIXEL_FORMAT_YUV420SP);

    String8 parameterString;

    parameterString = CameraParameters::FOCUS_MODE_AUTO;
    parameterString.append(",");
    parameterString.append(CameraParameters::FOCUS_MODE_MACRO);
    p.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES, parameterString.string());

    parameterString = CameraParameters::ANTIBANDING_AUTO;
    parameterString.append(",");
    parameterString.append(CameraParameters::ANTIBANDING_50HZ);
    parameterString.append(",");
    parameterString.append(CameraParameters::ANTIBANDING_60HZ);
    parameterString.append(",");
    parameterString.append(CameraParameters::ANTIBANDING_OFF);
    p.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING, parameterString.string());

    parameterString = CameraParameters::EFFECT_NONE;
    parameterString.append(",");
    parameterString.append(CameraParameters::EFFECT_MONO);
    parameterString.append(",");
    parameterString.append(CameraParameters::EFFECT_NEGATIVE);
    parameterString.append(",");
    parameterString.append(CameraParameters::EFFECT_SEPIA);
    p.set(CameraParameters::KEY_SUPPORTED_EFFECTS, parameterString.string());

    p.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES,
            CameraParameters::FLASH_MODE_OFF);

    parameterString = CameraParameters::SCENE_MODE_AUTO;
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_PORTRAIT);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_LANDSCAPE);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_NIGHT);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_BEACH);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_SNOW);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_SUNSET);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_FIREWORKS);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_SPORTS);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_PARTY);
    parameterString.append(",");
    parameterString.append(CameraParameters::SCENE_MODE_CANDLELIGHT);
    p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES, parameterString.string());

    parameterString = CameraParameters::WHITE_BALANCE_AUTO;
    parameterString.append(",");
    parameterString.append(CameraParameters::WHITE_BALANCE_INCANDESCENT);
    parameterString.append(",");
    parameterString.append(CameraParameters::WHITE_BALANCE_FLUORESCENT);
    parameterString.append(",");
    parameterString.append(CameraParameters::WHITE_BALANCE_DAYLIGHT);
    parameterString.append(",");
    parameterString.append(CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT);
    p.set(CameraParameters::KEY_GPS_PROCESSING_METHOD, parameterString.string());
//add the max and min for adjust value[20100728 giung.jung]
    p.set("sharpness-min", 0);
    p.set("sharpness-max", 4);
    p.set("saturation-min", 0);
    p.set("saturation-max", 4);
    p.set("contrast-min", 0);
    p.set("contrast-max", 4);
#endif

    // These values must be multiples of 16, so we can't do 427x320, which is the exact size on
    // screen we want to display at. 480x360 doesn't work either since it's a multiple of 8.
    p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, "160");
    p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, "120");
    p.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, "100");

    p.set(CameraParameters::KEY_ROTATION, 0);
    p.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    p.set(CameraParameters::KEY_EFFECT, CameraParameters::EFFECT_NONE);
    p.set(CameraParameters::KEY_SCENE_MODE, CameraParameters::SCENE_MODE_AUTO);
    p.set("vintagemode", "off");
    p.set("sharpness", 2);
    p.set("contrast", 2);
    p.set("saturation", 2);
    p.set("iso", "auto");
    p.set("metering", "center");
    p.set(CameraParameters::KEY_FLASH_MODE, CameraParameters::FLASH_MODE_OFF);
    p.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
    p.set("anti-shake", 0);
    p.set("wdr", 0);
    p.set("smart-auto",0);
    p.set("beauty-shot", 0);
    p.set(CameraParameters::KEY_ANTIBANDING, CameraParameters::ANTIBANDING_AUTO);
    p.set("video_recording_gamma", "off");
    p.set("slow_ae", "off");
    p.set("vtmode", 0);
    p.set("chk_dataline", 0);
    p.set("blur", 0);
#else
    p.set("image-effects", "original");
#endif

    p.set(CameraParameters::KEY_ZOOM, "0");
    p.set(CameraParameters::KEY_ZOOM_SUPPORTED, CameraParameters::TRUE);
    p.set(CameraParameters::KEY_MAX_ZOOM, "12");
    p.set(CameraParameters::KEY_ZOOM_RATIOS,
            "100,125,150,175,200,225,250,275,300,324,350,375,400");

    p.set(CameraParameters::KEY_FOCAL_LENGTH, "3.79");

    p.set(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, "51.2");
    p.set(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, "39.4");

    p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, "0");
    p.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "4");
    p.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "-4");
    p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5");

    p.set("AppShutterSound", 0);

    if (setParameters(p) != NO_ERROR) {
        LOGE("ERR(%s):Fail on setParameters(p)", __func__);
    }
}

CameraHardwareSec::~CameraHardwareSec()
{
    LOGV("%s()", __func__);

    mSecCamera->DeinitCamera();

    if (mRawHeap != NULL)
        mRawHeap.clear();

    if (mJpegHeap != NULL)
        mJpegHeap.clear();

    if (mPreviewHeap != NULL)
        mPreviewHeap.clear();

    if (mRecordHeap != NULL)
        mRecordHeap.clear();

#if defined(BOARD_USES_OVERLAY)
    if(mUseOverlay) {
        mOverlay->destroy();
        mUseOverlay = false;
        mOverlay = NULL;
    }
#endif

    mSecCamera = NULL;

    singleton.clear();
}

sp<IMemoryHeap> CameraHardwareSec::getPreviewHeap() const
{
    return mPreviewHeap;
}

sp<IMemoryHeap> CameraHardwareSec::getRawHeap() const
{
    return mRawHeap;
}

//Kamat added: New code as per eclair framework
void CameraHardwareSec::setCallbacks(notify_callback notify_cb,
                                      data_callback data_cb,
                                      data_callback_timestamp data_cb_timestamp,
                                      void *user)
{
    Mutex::Autolock lock(mLock);
    mNotifyCb = notify_cb;
    mDataCb = data_cb;
    mDataCbTimestamp = data_cb_timestamp;
    mCallbackCookie = user;
}

void CameraHardwareSec::enableMsgType(int32_t msgType)
{
    Mutex::Autolock lock(mLock);
    mMsgEnabled |= msgType;
}

void CameraHardwareSec::disableMsgType(int32_t msgType)
{
    Mutex::Autolock lock(mLock);
    mMsgEnabled &= ~msgType;
}

bool CameraHardwareSec::msgTypeEnabled(int32_t msgType)
{
    Mutex::Autolock lock(mLock);
    return (mMsgEnabled & msgType);
}

// ---------------------------------------------------------------------------

int CameraHardwareSec::previewThread()
{
    int index;

    index = mSecCamera->getPreview();
    if (index < 0) {
        LOGE("ERR(%s):Fail on SecCamera->getPreview()", __func__);
        return UNKNOWN_ERROR;
    }

    nsecs_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);

#ifdef PREVIEW_USING_MMAP
    int width, height, frame_size, offset;

    mSecCamera->getPreviewSize(&width, &height, &frame_size);

    offset = (frame_size + 16) * index;
        sp<MemoryBase> buffer = new MemoryBase(mPreviewHeap, offset, frame_size);

    unsigned int phyYAddr = mSecCamera->getPhyAddrY(index);
    unsigned int phyCAddr = mSecCamera->getPhyAddrC(index);

    if (phyYAddr == 0xffffffff || phyCAddr == 0xffffffff) {
        LOGE("ERR(%s):Fail on SecCamera. Invalid PhyAddr, Y addr = %0x C addr = %0x",
                __func__, phyYAddr, phyCAddr);
        return UNKNOWN_ERROR;
    }
    memcpy(static_cast<unsigned char *>(mPreviewHeap->base()) + (offset + frame_size    ), &phyYAddr, 4);
    memcpy(static_cast<unsigned char *>(mPreviewHeap->base()) + (offset + frame_size + 4), &phyCAddr, 4);

#if defined(BOARD_USES_OVERLAY)
    if (mUseOverlay) {
        int ret;

        if (buf_idx == 0)
            buf_idx = 1;
        else
            buf_idx = 0;

        memcpy(static_cast<unsigned char*>(mPreviewHeap->base()) + offset + frame_size + sizeof(phyYAddr) + sizeof(phyCAddr),
                    &buf_idx, sizeof(buf_idx));

        ret = mOverlay->queueBuffer((void*)(static_cast<unsigned char *>(mPreviewHeap->base()) + (offset + frame_size)));

        if (ret == ALL_BUFFERS_FLUSHED) {
            goto OverlayEnd;
        } else if (ret == -1) {
            LOGE("ERR(%s):overlay queueBuffer fail", __func__);
            goto OverlayEnd;
        }

        overlay_buffer_t overlay_buffer;
        ret = mOverlay->dequeueBuffer(&overlay_buffer);

        if (ret == ALL_BUFFERS_FLUSHED) {
            goto OverlayEnd;
        } else if (ret == -1) {
            LOGE("ERR(%s):overlay dequeueBuffer fail", __func__);
            goto OverlayEnd;
        }
    }

OverlayEnd:
#endif

#else
    unsigned int phyYAddr = mSecCamera->getPhyAddrY(index);
    unsigned int phyCAddr = mSecCamera->getPhyAddrC(index);

    if (phyYAddr == 0xffffffff || phyCAddr == 0xffffffff) {
        LOGE("ERR(%s):Fail on SecCamera getPhyAddr Y addr = %0x C addr = %0x", __func__, phyYAddr, phyCAddr);
        return UNKNOWN_ERROR;
    }
    struct addrs *addrs = (struct addrs *)mPreviewHeap->base();

    sp<MemoryBase> buffer = new MemoryBase(mPreviewHeap, index * sizeof(struct addrs), sizeof(struct addrs));
    addrs[index].addr_y = phyYAddr;
    addrs[index].addr_cbcr = phyCAddr;
#endif //PREVIEW_USING_MMAP

    // Notify the client of a new frame. //Kamat --eclair
    if (mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME) {
        mDataCb(CAMERA_MSG_PREVIEW_FRAME, buffer, mCallbackCookie);
    }

    if (mRecordRunning == true) {
#ifdef SEND_YUV_RECORD_DATA
        int width, height, frame_size;
        unsigned char *virYAddr;
        unsigned char *virCAddr;

        mSecCamera->getPreviewSize(&width, &height, &frame_size);
        mSecCamera->getYUVBuffers(&virYAddr, &virCAddr, index);
        sp<MemoryBase> buffer = new MemoryBase(mRecordHeap, 0, frame_size);
        //memcpy(mRecordHeap->base(), (void*)virYAddr, width * height);
        //memcpy(mRecordHeap->base() + (width*height),(void*)virCAddr, width * height * 0.5);
        memcpy(mRecordHeap->base(), (void*)virYAddr, ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)));
        memcpy(mRecordHeap->base() + ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)),
                (void*)virCAddr, ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height / 2)));
#else
#ifdef DUAL_PORT_RECORDING
        int index = mSecCamera->getRecord();

        if (index < 0) {
            LOGE("ERR(%s):Fail on SecCamera->getRecord()", __func__);
            return UNKNOWN_ERROR;
        }

        unsigned int phyYAddr = mSecCamera->getRecPhyAddrY(index);
        unsigned int phyCAddr = mSecCamera->getRecPhyAddrC(index);

        if (phyYAddr == 0xffffffff || phyCAddr == 0xffffffff) {
            LOGE("ERR(%s):Fail on SecCamera getRectPhyAddr Y addr = %0x C addr = %0x", __func__, phyYAddr, phyCAddr);
            return UNKNOWN_ERROR;
        }
#endif//DUAL_PORT_RECORDING
        struct addrs *addrs = (struct addrs *)mRecordHeap->base();

        sp<MemoryBase> buffer = new MemoryBase(mRecordHeap, index * sizeof(struct addrs), sizeof(struct addrs));
        addrs[index].addr_y = phyYAddr;
        addrs[index].addr_cbcr = phyCAddr;
#endif
        // Notify the client of a new frame. //Kamat --eclair
        if (mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
            //nsecs_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
            mDataCbTimestamp(timestamp, CAMERA_MSG_VIDEO_FRAME, buffer, mCallbackCookie);
        }
#ifdef DUAL_PORT_RECORDING
    } else if (mRecordRunning == false) {
        if (mSecCamera->stopRecord() < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->stopRecord()", __func__);
            return UNKNOWN_ERROR;
        }
#endif
    }

    // Wait for it...
    if (mTimeStart.tv_sec == 0 && mTimeStart.tv_usec == 0) {
        gettimeofday(&mTimeStart, NULL);
    } else {
        gettimeofday(&mTimeStop, NULL);
        long time = measure_time(&mTimeStart, &mTimeStop);
        int delay = (mPreviewFrameRateMicrosec > time) ? mPreviewFrameRateMicrosec - time : 0;

        usleep(delay);
        //LOG_CAMERA_PREVIEW("delay = %d time = %ld us\n ", delay, time);
        gettimeofday(&mTimeStart, NULL);
    }

    return NO_ERROR;
}

status_t CameraHardwareSec::startPreview()
{
    int ret = 0;        //s1 [Apply factory standard]

    LOGE("%s()", __func__);

    Mutex::Autolock lock(mLock);
    if (mPreviewThread != 0) {
        // already running
        return INVALID_OPERATION;
    }

    memset(&mTimeStart, 0, sizeof(mTimeStart));
    memset(&mTimeStop, 0, sizeof(mTimeStop));

    mSecCamera->stopPreview();

    ret  = mSecCamera->startPreview();
    LOGE("%s : return startPreview %d", __func__, ret);

    if (ret < 0) {
        LOGE("ERR(%s):Fail on mSecCamera->startPreview()", __func__);
        if (mMsgEnabled & CAMERA_MSG_ERROR) {
                mNotifyCb(CAMERA_MSG_ERROR, -2, 0, mCallbackCookie);
        }
        return -1; //UNKNOWN_ERROR;
    }

#ifdef PREVIEW_USING_MMAP
    if (mPreviewHeap != NULL)
        mPreviewHeap.clear();

    int width, height, frame_size;

    mSecCamera->getPreviewSize(&width, &height, &frame_size);

    int previewHeapSize = (frame_size + 16) * kBufferCount;

    LOGD("MemoryHeapBase(fd(%d), size(%d), width(%d), height(%d))", (int)mSecCamera->getCameraFd(), (size_t)(previewHeapSize), width, height);
    mPreviewHeap = new MemoryHeapBase((int)mSecCamera->getCameraFd(), (size_t)(previewHeapSize), (uint32_t)0);
#endif

#ifdef JPEG_FROM_SENSOR
    mSecCamera->getPostViewConfig(&mPostViewWidth, &mPostViewHeight, &mPostViewSize);
    LOGE("CameraHardwareSec: mPostViewWidth = %d mPostViewHeight = %d mPostViewSize = %d",mPostViewWidth,mPostViewHeight,mPostViewSize);
#endif

#ifdef DIRECT_DELIVERY_OF_POSTVIEW_DATA
    int rawHeapSize = mPostViewSize;
#else
    int rawHeapSize = sizeof(struct addrs_cap);
#endif
    LOGE("CameraHardwareSec: mRawHeap : MemoryHeapBase(previewHeapSize(%d))", rawHeapSize);
    mRawHeap = new MemoryHeapBase(rawHeapSize);
    if (mRawHeap->getHeapID() < 0) {
        LOGE("ERR(%s): Raw heap creation fail", __func__);
            mRawHeap.clear();
    }

    mPreviewRunning = true;
    mPreviewThread = new PreviewThread(this);

    return NO_ERROR;
}

#if defined(BOARD_USES_OVERLAY)
bool CameraHardwareSec::useOverlay()
{
    return true;
}

status_t CameraHardwareSec::setOverlay(const sp<Overlay> &overlay)
{
    LOGV("%s() : ", __func__);

    int overlayWidth  = 0;
    int overlayHeight = 0;
    int overlayFrameSize = 0;

    if (overlay == NULL) {
        goto setOverlayFail;
    }

    if (overlay->getHandleRef()== NULL && mUseOverlay == true) {
        if (mOverlay != 0)
            mOverlay->destroy();

        mOverlay = NULL;
        mUseOverlay = false;

        return NO_ERROR;
    }

    if (overlay->getStatus() != NO_ERROR) {
        LOGE("ERR(%s):overlay->getStatus() fail", __func__);
        goto setOverlayFail;
    }

    mSecCamera->getPreviewSize(&overlayWidth, &overlayHeight, &overlayFrameSize);

    if (overlay->setCrop(0, 0, overlayWidth, overlayHeight) != NO_ERROR) {
        LOGE("ERR(%s)::(mOverlay->setCrop(0, 0, %d, %d) fail", __func__, overlayWidth, overlayHeight);
        goto setOverlayFail;
    }

    mOverlay = overlay;
    mUseOverlay = true;

    return NO_ERROR;

setOverlayFail :
    if (mOverlay != 0)
        mOverlay->destroy();
    mOverlay = 0;

    mUseOverlay = false;

    return UNKNOWN_ERROR;
}
#endif

void CameraHardwareSec::stopPreview()
{
    LOGV("%s()", __func__);

    sp<PreviewThread> previewThread;

    { // scope for the lock
        Mutex::Autolock lock(mLock);
        previewThread = mPreviewThread;
    }

    // don't hold the lock while waiting for the thread to quit
    if (previewThread != 0) {
        previewThread->requestExitAndWait();
    }

    Mutex::Autolock lock(mLock);
    mPreviewThread.clear();

    if (!mNoHwHandle)
        if (mSecCamera->stopPreview() < 0)
            LOGE("ERR(%s):Fail on mSecCamera->stopPreview()", __func__);

    mPreviewRunning = false;
}

bool CameraHardwareSec::previewEnabled()
{
    LOGV("%s() : %d", __func__, mPreviewThread != 0);
    return mPreviewThread != 0;
}

// ---------------------------------------------------------------------------

status_t CameraHardwareSec::startRecording()
{
    LOGV("%s()", __func__);

#ifdef DUAL_PORT_RECORDING
        if (mSecCamera->startRecord() < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->startRecord()", __func__);
            return UNKNOWN_ERROR;
        }
#endif

    mRecordRunning = true;
    return NO_ERROR;
}

void CameraHardwareSec::stopRecording()
{
    LOGV("%s()", __func__);

    mRecordRunning = false;
}

bool CameraHardwareSec::recordingEnabled()
{
    LOGV("%s()", __func__);

    return mRecordRunning;
}

void CameraHardwareSec::releaseRecordingFrame(const sp<IMemory>& mem)
{
    LOG_CAMERA_PREVIEW("%s()", __func__);

//    ssize_t offset; size_t size;
//    sp<MemoryBase>       mem1 = mem;
//    sp<MemoryHeapBase> heap = mem->getMemory(&offset, &size);
//    sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);

//    mem1.clear();
//    heap.clear();
}

// ---------------------------------------------------------------------------

#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
int CameraHardwareSec::smartautosceneThread()
{
    mSmartautoscene_current_status = mSecCamera->getAutosceneStatus();

    if (mSmartautoscene_current_status < 0) {
        LOGE("ERR(%s):Fail on mSecCamera->getAutosceneStatus()", __func__);
        return UNKNOWN_ERROR;
    }

    if (mSmartautoscene_current_status != mSmartautoscene_previous_status) {
        //if (mMsgEnabled & CAMERA_MSG_SMARTAUTO_SCENE_STATUS)
        //mNotifyCb(CAMERA_MSG_SMARTAUTO_SCENE_STATUS, mSmartautoscene_current_status, 0, mCallbackCookie);
        LOGE("%s   CAMERA_MSG_SMARTAUTO_SCENE_STATUS(%d) Callback!!!!!!!!    ",
                __func__,mSmartautoscene_current_status);
        mSmartautoscene_previous_status = mSmartautoscene_current_status;
    } else {
        LOGE("%s   current_status(%d) is same with previous_status(%d)",
                __func__,mSmartautoscene_current_status,mSmartautoscene_previous_status);
    }
    usleep(2000 * 1000);  //2000ms delay
    LOGE("DELAY(2000ms)!!!!!!!");
    return NO_ERROR;
}

status_t CameraHardwareSec::startSmartautoscene()
{
    LOGV("%s()", __func__);

//  Mutex::Autolock lock(mLock);

    if (mSmartautosceneThread != 0) {
        // already running
        return INVALID_OPERATION;
    }

    mSmartautosceneRunning = true;
    mSmartautosceneThread = new SmartautosceneThread(this);
    return NO_ERROR;
}

void CameraHardwareSec::stopSmartautoscene()
{
    LOGV("%s()", __func__);

    sp<SmartautosceneThread> smartautosceneThread;

    { // scope for the lock
//  Mutex::Autolock lock(mLock);
        smartautosceneThread = mSmartautosceneThread;
    }

    // don't hold the lock while waiting for the thread to quit
    if (smartautosceneThread != 0) {
        smartautosceneThread->requestExitAndWait();
    }

//  Mutex::Autolock lock(mLock);
    mSmartautosceneThread.clear();

    mSmartautosceneRunning = false;
}

bool CameraHardwareSec::smartautosceneEnabled()
{
    LOGV("%s() : %d", __func__, mSmartautosceneThread != 0);
    return mSmartautosceneThread != 0;
}

#endif


int CameraHardwareSec::beginAutoFocusThread(void *cookie)
{
    LOGV("%s()", __func__);
    CameraHardwareSec *c = (CameraHardwareSec *)cookie;
    return c->autoFocusThread();
}

int CameraHardwareSec::autoFocusThread()
{
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    int count =0;
    int af_status =0 ;
#endif

    LOGV("%s()", __func__);
//  usleep(50000); // 1frame delay 50ms
    if (mSecCamera->setAutofocus() < 0) {
        LOGE("ERR(%s):Fail on mSecCamera->setAutofocus()", __func__);
        return UNKNOWN_ERROR;
    }

#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
//  usleep(10000);
    af_status = mSecCamera->getAutoFocusResult();

    if (af_status == 0x01) {
        LOGV("%s() AF Success!!", __func__);
        if (mMsgEnabled & CAMERA_MSG_FOCUS)
            mNotifyCb(CAMERA_MSG_FOCUS, true, 0, mCallbackCookie);
    } else if (af_status == 0x02) {
        LOGV("%s() AF Cancelled !!", __func__);
        if (mMsgEnabled & CAMERA_MSG_FOCUS)
            mNotifyCb(CAMERA_MSG_FOCUS, 0x02, 0, mCallbackCookie);
    } else {
        LOGV("%s() AF Fail !!", __func__);
        if (mMsgEnabled & CAMERA_MSG_FOCUS)
            mNotifyCb(CAMERA_MSG_FOCUS, false, 0, mCallbackCookie);
    }
#else
    if (mMsgEnabled & CAMERA_MSG_FOCUS)
        mNotifyCb(CAMERA_MSG_FOCUS, true, 0, mCallbackCookie);
#endif
    return NO_ERROR;
}

status_t CameraHardwareSec::autoFocus()
{
    LOGV("%s()", __func__);
    Mutex::Autolock lock(mLock);
    if (createThread(beginAutoFocusThread, this) == false)
        return UNKNOWN_ERROR;
    return NO_ERROR;
}

/* 2009.10.14 by icarus for added interface */
status_t CameraHardwareSec::cancelAutoFocus()
{
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    LOGV("%s()", __func__);

    if (mSecCamera->cancelAutofocus() < 0) {
        LOGE("ERR(%s):Fail on mSecCamera->cancelAutofocus()", __func__);
        return UNKNOWN_ERROR;
    }
#endif
    return NO_ERROR;
}

#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
status_t CameraHardwareSec::objectTracking(int onoff)
{
    LOGV("%s() onoff = %d", __func__, onoff);

    Mutex::Autolock lock(mLock);
    if (onoff) {
        if (mObjectTrackingThread == 0) {
            mObjectTrackingThread = new ObjectTrackingThread(this);
        }
        mObjectTrackingRunning=true;
    } else {
        if (mObjectTrackingThread != 0) {
            mObjectTrackingThread->requestExitAndWait();
        }
        mObjectTrackingThread.clear();
        mObjectTrackingRunning=false;
    }
    return 0;
}

int CameraHardwareSec::save_jpeg( unsigned char *real_jpeg, int jpeg_size)
{
    FILE *yuv_fp = NULL;
    char filename[100], *buffer = NULL;

    /* file create/open, note to "wb" */
    yuv_fp = fopen("/data/camera_dump.jpeg", "wb");
    if (yuv_fp == NULL) {
        LOGE("Save jpeg file open error");
        return -1;
    }

    LOGE("[BestIQ]  real_jpeg size ========>  %d\n", jpeg_size);
    buffer = (char *) malloc(jpeg_size);
    if (buffer == NULL) {
        LOGE("Save YUV] buffer alloc failed");
        if (yuv_fp)
            fclose(yuv_fp);

        return -1;
    }

    memcpy(buffer, real_jpeg, jpeg_size);

    fflush(stdout);

    fwrite(buffer, 1, jpeg_size, yuv_fp);

    fflush(yuv_fp);

    if (yuv_fp)
            fclose(yuv_fp);
    if (buffer)
            free(buffer);

    return 0;
}


int CameraHardwareSec::objectTrackingThread()
{
    int new_obj_status;

    new_obj_status = mSecCamera->getObjectTrackingStatus();
#if 0 //temp till define callback msg
    if (mObjectTrackingStatus != new_obj_status) {
        mObjectTrackingStatus = new_obj_status;
        if (mMsgEnabled & CAMERA_MSG_OBJ_TRACKING)
            mNotifyCb(CAMERA_MSG_OBJ_TRACKING, new_obj_status, 0, mCallbackCookie);
    }
#endif
    usleep(100000); //100ms
    return NO_ERROR;
}
#endif
/*static*/ int CameraHardwareSec::beginPictureThread(void *cookie)
{
    LOGV("%s()", __func__);
    CameraHardwareSec *c = (CameraHardwareSec *)cookie;
    return c->pictureThread();
}

void CameraHardwareSec::save_postview(const char *fname, uint8_t *buf, uint32_t size)
{
    int nw;
    int cnt = 0;
    uint32_t written = 0;

    LOGD("opening file [%s]\n", fname);
    int fd = open(fname, O_RDWR | O_CREAT);
    if (fd < 0) {
        LOGE("failed to create file [%s]: %s", fname, strerror(errno));
    return;
    }

    LOGD("writing %d bytes to file [%s]\n", size, fname);
    while (written < size) {
        nw = ::write(fd, buf + written, size - written);
        if (nw < 0) {
            LOGE("failed to write to file %d [%s]: %s",written,fname, strerror(errno));
            break;
        }
        written += nw;
        cnt++;
    }
    LOGD("done writing %d bytes to file [%s] in %d passes\n",size, fname, cnt);
    ::close(fd);
}

int CameraHardwareSec::pictureThread()
{
    LOGV("%s()", __func__);

    int jpeg_size = 0;
    int ret = NO_ERROR;
    unsigned char *jpeg_data = NULL;
    int postview_offset = 0;
    unsigned char *postview_data = NULL;

    //unsigned int addr;
    unsigned char *addr = NULL;
    int mPostViewWidth, mPostViewHeight, mPostViewSize;
    int cap_width, cap_height, cap_frame_size;

    unsigned int output_size = 0;

    mSecCamera->getPostViewConfig(&mPostViewWidth, &mPostViewHeight, &mPostViewSize);
    //*size = (BACK_CAMERA_POSTVIEW_WIDTH * BACK_CAMERA_POSTVIEW_HEIGHT * BACK_CAMERA_POSTVIEW_BPP) / 8;
    int postviewHeapSize = mPostViewWidth * mPostViewHeight * 2;
    mSecCamera->getSnapshotSize(&cap_width, &cap_height, &cap_frame_size);
    LOGE("[kidggang]:func(%s):line(%d)&cap_width(%d), &cap_height(%d), &cap_frame_size(%d)\n",
            __func__,__LINE__,cap_width, cap_height, cap_frame_size);

//  sp<MemoryBase> buffer = new MemoryBase(mRawHeap, 0, postviewHeapSize);

    LOG_TIME_DEFINE(0)
    LOG_TIME_START(0)
#ifdef DIRECT_DELIVERY_OF_POSTVIEW_DATA
    sp<MemoryBase> buffer = new MemoryBase(mRawHeap, 0, mPostViewSize + 8);
#else
    sp<MemoryBase> buffer = new MemoryBase(mRawHeap, 0, sizeof(struct addrs_cap));
#endif

    struct addrs_cap *addrs = (struct addrs_cap *)mRawHeap->base();

#ifdef JPEG_FROM_SENSOR
    addrs[0].width = mPostViewWidth;
    addrs[0].height = mPostViewHeight;
    LOGV("[5B] mPostViewWidth = %d mPostViewHeight = %d\n",mPostViewWidth,mPostViewHeight);
#else
    mParameters.getPictureSize((int*)&addrs[0].width, (int*)&addrs[0].height);
#endif


    if (mSecCamera->getCameraId() == SecCamera::CAMERA_ID_BACK) { //[zzangdol] CAMERA_ID_BACK
#ifndef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
        if (mMsgEnabled & CAMERA_MSG_SHUTTER) {
            mNotifyCb(CAMERA_MSG_SHUTTER, 0, 0, mCallbackCookie);
        }
#endif
    }//[zzangdol]CAMERA_ID_BACK

    if (mMsgEnabled & CAMERA_MSG_RAW_IMAGE) {
        LOG_TIME_DEFINE(1)
        LOG_TIME_START(1)

        int picture_size, picture_width, picture_height;
        mSecCamera->getSnapshotSize(&picture_width, &picture_height, &picture_size);
        int picture_format = mSecCamera->getSnapshotPixelFormat();

        unsigned int phyAddr;
#ifdef JPEG_FROM_SENSOR
        // Modified the shutter sound timing for Jpeg capture
        if (mSecCamera->getCameraId() == SecCamera::CAMERA_ID_BACK)
            mSecCamera->setSnapshotCmd();
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
        if (mMsgEnabled & CAMERA_MSG_SHUTTER) {
            mNotifyCb(CAMERA_MSG_SHUTTER, 0, 0, mCallbackCookie);
        }
#endif

        if (mSecCamera->getCameraId() == SecCamera::CAMERA_ID_BACK){ //[zzangdol] CAMERA_ID_BACK
            jpeg_data = mSecCamera->getJpeg(&jpeg_size, &phyAddr);
            if (jpeg_data == NULL) {
                LOGE("ERR(%s):Fail on SecCamera->getSnapshot()", __func__);
                ret = UNKNOWN_ERROR;
            }
        } else {
            addr = mSecCamera->getSnapshotAndJpeg(&output_size);
            //LOGV("[zzangdol] getSnapshotAndJpeg\n");
        }
#else
        phyAddr = mSecCamera->getSnapshotAndJpeg();
        jpeg_data = mSecCamera->yuv2Jpeg((unsigned char*)phyAddr, 0, &jpeg_size,
                        picture_width, picture_height, picture_format);
#endif

#ifdef DIRECT_DELIVERY_OF_POSTVIEW_DATA
        postview_offset = mSecCamera->getPostViewOffset();
        if (jpeg_data != NULL)
            memcpy(mRawHeap->base(), jpeg_data + postview_offset, mPostViewSize);
#else
        addrs[0].addr_y = phyAddr;
#endif

        LOG_TIME_END(1)
        LOG_CAMERA("getSnapshotAndJpeg interval: %lu us", LOG_TIME(1));
    }

    int JpegImageSize, JpegExifSize;

    sp<MemoryHeapBase> PostviewHeap = new MemoryHeapBase(mPostViewSize);
    sp<MemoryHeapBase> JpegHeap = new MemoryHeapBase(4300000);
    decodeInterleaveData(jpeg_data, 4261248, mPostViewWidth, mPostViewHeight,
                         &JpegImageSize, JpegHeap->base(), PostviewHeap->base());

    sp<MemoryBase> postview = new MemoryBase(PostviewHeap, 0, postviewHeapSize);
    memcpy(mRawHeap->base(),PostviewHeap->base(), postviewHeapSize);
    if (mMsgEnabled & CAMERA_MSG_RAW_IMAGE) {
        mDataCb(CAMERA_MSG_RAW_IMAGE, buffer, mCallbackCookie);
    }
#if 0//def SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    if (mMsgEnabled & CAMERA_MSG_POSTVIEW_FRAME) {
        int postviewHeapSize = mPostViewSize;
        sp<MemoryHeapBase> mPostviewHeap = new MemoryHeapBase(postviewHeapSize);

        postview_data = jpeg_data + postview_offset;
        sp<MemoryBase> postview = new MemoryBase(mPostviewHeap, 0, postviewHeapSize);

        if (postview_data != NULL)
            memcpy(mPostviewHeap->base(), postview_data, postviewHeapSize);

        mDataCb(CAMERA_MSG_POSTVIEW_FRAME, postview, mCallbackCookie);
    }
#endif //#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION

    if (mMsgEnabled & CAMERA_MSG_COMPRESSED_IMAGE) {
        if (mSecCamera->getCameraId() == SecCamera::CAMERA_ID_BACK) {
            const int EXIF_FILE_SIZE = 28800;
            const int JPG_STREAM_BUF_SIZE = 3145728;

            sp<MemoryHeapBase> ExifHeap = new MemoryHeapBase(EXIF_FILE_SIZE + JPG_STREAM_BUF_SIZE);
            JpegExifSize = mSecCamera->getExif ((unsigned char *)ExifHeap->base(),
                                               (unsigned char *)PostviewHeap->base());

            LOGE("JpegExifSize=%d", JpegExifSize);
            unsigned char *ExifStart = (unsigned char *)JpegHeap->base() + 2;
            unsigned char *ImageStart = ExifStart + JpegExifSize;

            memmove(ImageStart, ExifStart, JpegImageSize - 2);
            memcpy(ExifStart, ExifHeap->base(), JpegExifSize);
            sp<MemoryBase> mem = new MemoryBase(JpegHeap, 0, JpegImageSize + JpegExifSize);

            mDataCb(CAMERA_MSG_COMPRESSED_IMAGE, mem, mCallbackCookie);
        } else {
            LOGV("[zzangdol] COMPRESSED_IMAGE\n");
            //mSecCamera->getSnapshotSize(&cap_width, &cap_height, &cap_frame_size);
            //sp<MemoryHeapBase> mHeap = new MemoryHeapBase((int)mSecCamera->getCameraFd(), (size_t)(cap_frame_size * kBufferCount), (uint32_t)0);
            sp<MemoryHeapBase> mHeap = new MemoryHeapBase(2000000);
            memcpy(mHeap->base(), addr, cap_frame_size);
            sp<MemoryBase> mem = new MemoryBase(mHeap , 0, cap_frame_size);
            //save_postview("/data/front.yuv", (uint8_t *)mHeap->base(), yuv_frame_size2);
            mDataCb(CAMERA_MSG_COMPRESSED_IMAGE, mem, mCallbackCookie);//[zzangdol]
        }
    }

    LOG_TIME_END(0)
    LOG_CAMERA("pictureThread interval: %lu us", LOG_TIME(0));

    return ret;
}

status_t CameraHardwareSec::takePicture()
{
    LOGV("%s()", __func__);

    stopPreview();
    mNoHwHandle = 0;

    if (createThread(beginPictureThread, this) == false)
        return -1;

    return NO_ERROR;
}

status_t CameraHardwareSec::cancelPicture()
{
    return NO_ERROR;
}

int CameraHardwareSec::decodeInterleaveData(unsigned char *pInterleaveData,
                                                 int interleaveDataSize,
                                                 int yuvWidth,
                                                 int yuvHeight,
                                                 int *pJpegSize,
                                                 void *pJpegData,
                                                 void *pYuvData)
{
    if (pInterleaveData == NULL)
        return false;

    bool ret = true;
    unsigned int *interleave_ptr = (unsigned int *)pInterleaveData;
    unsigned char *jpeg_ptr = (unsigned char *)pJpegData;
    unsigned char *yuv_ptr = (unsigned char *)pYuvData;
    unsigned char *p;
    int jpeg_size = 0;
    int yuv_size = 0;

    int i = 0;

    LOGE("decodeInterleaveData Start~~~");
    while (i < interleaveDataSize) {
        if ((*interleave_ptr == 0xFFFFFFFF) || (*interleave_ptr == 0x02FFFFFF) ||
                (*interleave_ptr == 0xFF02FFFF)) {
            // Padding Data
//            LOGE("%d(%x) padding data\n", i, *interleave_ptr);
            interleave_ptr++;
            i += 4;
        }
        else if ((*interleave_ptr & 0xFFFF) == 0x05FF) {
            // Start-code of YUV Data
//            LOGE("%d(%x) yuv data\n", i, *interleave_ptr);
            p = (unsigned char *)interleave_ptr;
            p += 2;
            i += 2;

            // Extract YUV Data
            if (pYuvData != NULL) {
                memcpy(yuv_ptr, p, yuvWidth * 2);
                yuv_ptr += yuvWidth * 2;
                yuv_size += yuvWidth * 2;
            }
            p += yuvWidth * 2;
            i += yuvWidth * 2;

            // Check End-code of YUV Data
            if ((*p == 0xFF) && (*(p + 1) == 0x06)) {
                interleave_ptr = (unsigned int *)(p + 2);
                i += 2;
            } else {
                ret = false;
                break;
            }
        } else {
            // Extract JPEG Data
//            LOGE("%d(%x) jpg data, jpeg_size = %d bytes\n", i, *interleave_ptr, jpeg_size);
            if (pJpegData != NULL) {
                memcpy(jpeg_ptr, interleave_ptr, 4);
                jpeg_ptr += 4;
                jpeg_size += 4;
            }
            interleave_ptr++;
            i += 4;
        }
    }
    if (ret) {
        if (pJpegData != NULL) {
            // Remove Padding after EOI
            for (i = 0; i < 3; i++) {
                if (*(--jpeg_ptr) != 0xFF) {
                    break;
                }
                jpeg_size--;
            }
            *pJpegSize = jpeg_size;

        }
        // Check YUV Data Size
        if (pYuvData != NULL) {
            if (yuv_size != (yuvWidth * yuvHeight * 2)) {
                ret = false;
            }
        }
    }
    LOGE("decodeInterleaveData End~~~");
    return ret;
}

status_t CameraHardwareSec::dump(int fd, const Vector<String16>& args) const
{
    const size_t SIZE = 256;
    char buffer[SIZE];
    String8 result;
    AutoMutex lock(&mLock);

    if (mSecCamera != 0) {
        mSecCamera->dump(fd, args);
        mParameters.dump(fd, args);
        snprintf(buffer, 255, " preview frame(%d), size (%d), running(%s)\n", mCurrentPreviewFrame, mPreviewFrameSize, mPreviewRunning?"true": "false");
        result.append(buffer);
    } else {
        result.append("No camera client yet.\n");
    }
    write(fd, result.string(), result.size());
    return NO_ERROR;
}

status_t CameraHardwareSec::setParameters(const CameraParameters& params)
{
    LOGV("%s()", __func__);

    Mutex::Autolock lock(mLock);

    status_t ret = NO_ERROR;

    mParameters = params;

    // set camera id
    int new_camera_id = params.getInt("camera-id");
    if (0 <= new_camera_id) {
        if (mSecCamera->setCameraId(new_camera_id) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setCameraId(camera_id(%d))", __func__, new_camera_id);
            ret = UNKNOWN_ERROR;
        }
    }

    // preview size
    int new_preview_width  = 0;
    int new_preview_height = 0;
    params.getPreviewSize(&new_preview_width, &new_preview_height);
    const char *new_str_preview_format = params.getPreviewFormat();

    if (0 < new_preview_width && 0 < new_preview_height && new_str_preview_format != NULL) {
        int new_preview_format = 0;

        if (strcmp(new_str_preview_format, CameraParameters::PIXEL_FORMAT_RGB565) == 0)
            new_preview_format = V4L2_PIX_FMT_RGB565;
        else if (strcmp(new_str_preview_format, CameraParameters::PIXEL_FORMAT_YUV420SP) == 0)
            new_preview_format = V4L2_PIX_FMT_NV21; //Kamat
        else if (strcmp(new_str_preview_format, "yuv420sp_custom") == 0)
            new_preview_format = V4L2_PIX_FMT_NV12T; //Kamat
        else if (strcmp(new_str_preview_format, "yuv420p") == 0)
            new_preview_format = V4L2_PIX_FMT_YUV420;
        else if (strcmp(new_str_preview_format, "yuv422i") == 0)
            new_preview_format = V4L2_PIX_FMT_YUYV;
        else if (strcmp(new_str_preview_format, "yuv422p") == 0)
            new_preview_format = V4L2_PIX_FMT_YUV422P;
        else
            new_preview_format = V4L2_PIX_FMT_NV21; //for 3rd party

        if (mSecCamera->setPreviewSize(new_preview_width, new_preview_height, new_preview_format) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setPreviewSize(width(%d), height(%d), format(%d))",
                    __func__, new_preview_width, new_preview_height, new_preview_format);
            ret = UNKNOWN_ERROR;
        }
#if defined(BOARD_USES_OVERLAY)
        if (mUseOverlay == true && mOverlay != 0) {
            if (mOverlay->setCrop(0, 0, new_preview_width, new_preview_height) != NO_ERROR)     {
                LOGE("ERR(%s)::(mOverlay->setCrop(0, 0, %d, %d) fail",
                        __func__, new_preview_width, new_preview_height);
            }
        }
#endif
    }

    int new_picture_width  = 0;
    int new_picture_height = 0;

    params.getPictureSize(&new_picture_width, &new_picture_height);
    if (0 < new_picture_width && 0 < new_picture_height) {
        if (mSecCamera->setSnapshotSize(new_picture_width, new_picture_height) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setSnapshotSize(width(%d), height(%d))",
                    __func__, new_picture_width, new_picture_height);
            ret = UNKNOWN_ERROR;
        }
    }

    // picture format
    const char *new_str_picture_format = params.getPictureFormat();

    if (new_str_picture_format != NULL) {
        int new_picture_format = 0;

        if (strcmp(new_str_picture_format, CameraParameters::PIXEL_FORMAT_RGB565) == 0)
            new_picture_format = V4L2_PIX_FMT_RGB565;
        else if (strcmp(new_str_picture_format, CameraParameters::PIXEL_FORMAT_YUV420SP) == 0)
            new_picture_format = V4L2_PIX_FMT_NV21; //Kamat: Default format
        else if (strcmp(new_str_picture_format, "yuv420sp_custom") == 0)
            new_picture_format = V4L2_PIX_FMT_NV12T;
        else if (strcmp(new_str_picture_format, "yuv420p") == 0)
            new_picture_format = V4L2_PIX_FMT_YUV420;
        else if (strcmp(new_str_picture_format, "yuv422i") == 0)
            new_picture_format = V4L2_PIX_FMT_YUYV;
        else if (strcmp(new_str_picture_format, "uyv422i_custom") == 0) //Zero copy UYVY format
            new_picture_format = V4L2_PIX_FMT_UYVY;
        else if (strcmp(new_str_picture_format, "uyv422i") == 0) //Non-zero copy UYVY format
            new_picture_format = V4L2_PIX_FMT_UYVY;
        else if (strcmp(new_str_picture_format, CameraParameters::PIXEL_FORMAT_JPEG) == 0)
#ifdef JPEG_FROM_SENSOR
            new_picture_format = V4L2_PIX_FMT_UYVY;
#else
            new_picture_format = V4L2_PIX_FMT_YUYV;
#endif
        else if (strcmp(new_str_picture_format, "yuv422p") == 0)
            new_picture_format = V4L2_PIX_FMT_YUV422P;
        else
            new_picture_format = V4L2_PIX_FMT_NV21; //for 3rd party

        if (mSecCamera->setSnapshotPixelFormat(new_picture_format) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setSnapshotPixelFormat(format(%d))", __func__, new_picture_format);
            ret = UNKNOWN_ERROR;
        }
    }

#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    //JPEG image quality
    int new_jpeg_quality = params.getInt(CameraParameters::KEY_JPEG_QUALITY);

    if (new_jpeg_quality < 1 || new_jpeg_quality > 100) {
        LOGE("ERR(%s): Invalid quality(%d))", __func__, new_jpeg_quality);

        new_jpeg_quality = 100;

        mParameters.set(CameraParameters::KEY_JPEG_QUALITY, "100");
    }

    mSecCamera->setJpegQuality(new_jpeg_quality);
#else
    //JPEG image quality
    int new_jpeg_quality = params.getInt(CameraParameters::KEY_JPEG_QUALITY);

    if (new_jpeg_quality < 0) {
    LOGW("JPEG-image quality is not specified or is negative, defaulting to 100");
        new_jpeg_quality = 100;
        mParameters.set(CameraParameters::KEY_JPEG_QUALITY, "100");
    }
    mSecCamera->setJpegQuality(new_jpeg_quality);
#endif

    // JPEG thumbnail size
    int new_jpeg_thumbnail_width = params.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH);
    int new_jpeg_thumbnail_height= params.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT);

    if (0 < new_jpeg_thumbnail_width && 0 < new_jpeg_thumbnail_height) {
        if (mSecCamera->setJpegThumbnailSize(new_jpeg_thumbnail_width, new_jpeg_thumbnail_height) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setJpegThumbnailSize(width(%d), height(%d))", __func__, new_jpeg_thumbnail_width, new_jpeg_thumbnail_height);
            ret = UNKNOWN_ERROR;
        }
    }

    // frame rate
    int new_frame_rate = params.getPreviewFrameRate();

    if (new_frame_rate < 5 || new_frame_rate > 30) {
        new_frame_rate = 30;
    }

    mParameters.setPreviewFrameRate(new_frame_rate);
    // Calculate how long to wait between frames.
    mPreviewFrameRateMicrosec = (int)(1000000.0f / float(new_frame_rate));

    LOGD("frame rate:%d, mPreviewFrameRateMicrosec:%d", new_frame_rate, mPreviewFrameRateMicrosec);

    mSecCamera->setFrameRate(new_frame_rate);

    //vt mode
    int new_vtmode = params.getInt("vtmode");

    if (0 <= new_vtmode) {
        if (mSecCamera->setVTmode(new_vtmode) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setVTMode(%d)", __func__, new_vtmode);
            ret = UNKNOWN_ERROR;
        }
    }

    // rotation
    int new_rotation = params.getInt(CameraParameters::KEY_ROTATION);
    int new_exif_rotation = 1;

    if (new_rotation != -1) {
        if (new_vtmode == SecCamera::VT_MODE_ON ) {
            LOGE("ERR(%s):VT mode is on. Rotate(%d))", __func__, new_rotation);

            if (mSecCamera->SetRotate(new_rotation) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->SetRotate(rotation(%d))", __func__, new_rotation);
                ret = UNKNOWN_ERROR;
            }
        } else {
            if (mSecCamera->SetRotate(0) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->SetRotate(rotation(%d))", __func__, new_rotation);
                ret = UNKNOWN_ERROR;
            }

            if (new_rotation == 0)
                new_exif_rotation = 1;
            else if (new_rotation == 90)
                new_exif_rotation = 6;
            else if (new_rotation == 180)
                new_exif_rotation = 3;
            else if (new_rotation == 270)
                new_exif_rotation = 8;
            else
                new_exif_rotation = 1;
        }
    }

#ifndef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    // brightness
    int new_exposure_compensation = params.getInt(CameraParameters::KEY_EXPOSURE_COMPENSATION);
    int max_exposure_compensation = params.getInt(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION);
    int min_exposure_compensation = params.getInt(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION);

    if ((min_exposure_compensation <= new_exposure_compensation) &&
        (max_exposure_compensation >= new_exposure_compensation)) {
        if (mSecCamera->setBrightness(new_exposure_compensation) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setBrightness(brightness(%d))", __func__, new_exposure_compensation);
            ret = UNKNOWN_ERROR;
        }
    }

    // whitebalance
    const char *new_white_str = params.get(CameraParameters::KEY_WHITE_BALANCE);

    if (new_white_str != NULL) {
        int new_white = -1;

        if (strcmp(new_white_str, CameraParameters::WHITE_BALANCE_AUTO) == 0)
            new_white = SecCamera::WHITE_BALANCE_AUTO;
        else if (strcmp(new_white_str, "indoor3100") == 0)
            new_white = SecCamera::WHITE_BALANCE_INDOOR3100;
        else if (strcmp(new_white_str, "outdoor5100") == 0)
            new_white = SecCamera::WHITE_BALANCE_OUTDOOR5100;
        else if (strcmp(new_white_str, "indoor2000") == 0)
            new_white = SecCamera::WHITE_BALANCE_INDOOR2000;
        else if (strcmp(new_white_str, "halt") == 0)
            new_white = SecCamera::WHITE_BALANCE_HALT;
        else if (strcmp(new_white_str, "cloudy") == 0)
            new_white = SecCamera::WHITE_BALANCE_CLOUDY;
        else if (strcmp(new_white_str, "sunny") == 0)
            new_white = SecCamera::WHITE_BALANCE_SUNNY;
        else {
            LOGE("ERR(%s):Invalid white balance(%s)", __func__, new_white_str);
            ret = UNKNOWN_ERROR;
        }

        if (0 <= new_white) {
            // white_balance
            if (mSecCamera->setWhiteBalance(new_white) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->setWhiteBalance(white(%d))", __func__, new_white);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    // image effect
    const char *new_image_effect_str = params.get("image-effects");

    if (new_image_effect_str != NULL) {
        int  new_image_effect = -1;

        if (strcmp(new_image_effect_str, "original") == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_ORIGINAL;
        else if (strcmp(new_image_effect_str, "arbitrary") == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_ARBITRARY;
        else if (strcmp(new_image_effect_str, "negative") == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_NEGATIVE;
        else if (strcmp(new_image_effect_str, "freeze") == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_FREEZE;
        else if (strcmp(new_image_effect_str, "embossing") == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_EMBOSSING;
        else if (strcmp(new_image_effect_str, "silhouette") == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_SILHOUETTE;
        else {
            LOGE("ERR(%s):Invalid effect(%s)", __func__, new_image_effect_str);
            ret = UNKNOWN_ERROR;
        }

        if (new_image_effect >= 0) {
            // white_balance
            if (mSecCamera->setImageEffect(new_image_effect) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->setImageEffect(effect(%d))", __func__, new_image_effect);
                ret = UNKNOWN_ERROR;
            }
        }
    }

#else
    // scene mode
    const char *new_scene_mode_str = params.get(CameraParameters::KEY_SCENE_MODE);

    LOGV("%s()  new_scene_mode_str %s", __func__,new_scene_mode_str);

    if (new_scene_mode_str != NULL) {
        int  new_scene_mode = -1;

        const char *new_iso_str = NULL;
        const char *new_metering_str = NULL;
        int new_exposure_compensation = 0;
        const char *new_white_str = NULL;
        int new_sharpness = 0;
        int new_saturation = 0;
        const char *new_focus_mode_str = NULL;
        const char *new_flash_mode_str = NULL;

        if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_AUTO) == 0) {
            new_scene_mode = SecCamera::SCENE_MODE_NONE;

            new_iso_str = params.get("iso");
            new_metering_str = params.get("metering");
            new_exposure_compensation = params.getInt(CameraParameters::KEY_EXPOSURE_COMPENSATION);
            new_white_str = params.get(CameraParameters::KEY_WHITE_BALANCE);
            new_sharpness = params.getInt("sharpness");
            new_saturation = params.getInt("saturation");
            new_focus_mode_str = params.get(CameraParameters::KEY_FOCUS_MODE);
            new_flash_mode_str = params.get(CameraParameters::KEY_FLASH_MODE);
        } else {
            if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_PORTRAIT) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_PORTRAIT;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_MINUS_1);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, "facedetect");
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_LANDSCAPE) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_LANDSCAPE;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "matrix");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_PLUS_1);
                mParameters.set("saturation", SecCamera::SATURATION_PLUS_1);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_SPORTS) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_SPORTS;

                mParameters.set("iso", "sports");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_PARTY) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_PARTY_INDOOR;

                mParameters.set("iso", "200");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_PLUS_1);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
            } else if ((strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_BEACH) == 0) ||
                        (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_SNOW) == 0)) {
                new_scene_mode = SecCamera::SCENE_MODE_BEACH_SNOW;

                mParameters.set("iso", "50");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_PLUS_2);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_PLUS_1);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_SUNSET) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_SUNSET;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_DAYLIGHT);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, "dusk-dawn") == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_DUSK_DAWN;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_FLUORESCENT);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, "fall-color") == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_FALL_COLOR;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_PLUS_2);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_NIGHT) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_NIGHTSHOT;

                mParameters.set("iso", "night");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, "back-light") == 0) {
                const char *flash_mode_str = params.get(CameraParameters::KEY_FLASH_MODE);

                new_scene_mode = SecCamera::SCENE_MODE_BACK_LIGHT;

                mParameters.set("iso", "auto");
                if (strcmp(flash_mode_str, "off") == 0)
                    mParameters.set("metering", "spot");
                else
                    mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_FIREWORKS) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_FIREWORKS;

                mParameters.set("iso", "50");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else if (strcmp(new_scene_mode_str, "text") == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_TEXT;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_AUTO);
                mParameters.set("sharpness", SecCamera::SHARPNESS_PLUS_2);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_MACRO);
            } else if (strcmp(new_scene_mode_str, CameraParameters::SCENE_MODE_CANDLELIGHT) == 0) {
                new_scene_mode = SecCamera::SCENE_MODE_CANDLE_LIGHT;

                mParameters.set("iso", "auto");
                mParameters.set("metering", "center");
                mParameters.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, SecCamera::BRIGHTNESS_NORMAL);
                mParameters.set(CameraParameters::KEY_WHITE_BALANCE, CameraParameters::WHITE_BALANCE_DAYLIGHT);
                mParameters.set("sharpness", SecCamera::SHARPNESS_NORMAL);
                mParameters.set("saturation", SecCamera::SATURATION_NORMAL);
                mParameters.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);
                mParameters.set(CameraParameters::KEY_FLASH_MODE, "off");
            } else {
                LOGE("%s::unmatched scene_mode(%s)", __func__, new_scene_mode_str); //action, night-portrait, theatre, steadyphoto
                ret = UNKNOWN_ERROR;
            }

            new_iso_str = mParameters.get("iso");
            new_metering_str = mParameters.get("metering");
            new_exposure_compensation = mParameters.getInt(CameraParameters::KEY_EXPOSURE_COMPENSATION);
            new_white_str = mParameters.get(CameraParameters::KEY_WHITE_BALANCE);
            new_sharpness = mParameters.getInt("sharpness");
            new_saturation = mParameters.getInt("saturation");
            new_focus_mode_str = mParameters.get(CameraParameters::KEY_FOCUS_MODE);
            new_flash_mode_str = mParameters.get(CameraParameters::KEY_FLASH_MODE);
        }

        // 1. ISO
        if (new_iso_str != NULL) {
            int  new_iso = -1;
            if (strcmp(new_iso_str, "auto") == 0)
                new_iso = SecCamera::ISO_AUTO;
            else if (strcmp(new_iso_str, "50") == 0)
                new_iso = SecCamera::ISO_50;
            else if (strcmp(new_iso_str, "100") == 0)
                new_iso = SecCamera::ISO_100;
            else if (strcmp(new_iso_str, "200") == 0)
                new_iso = SecCamera::ISO_200;
            else if (strcmp(new_iso_str, "400") == 0)
                new_iso = SecCamera::ISO_400;
            else if (strcmp(new_iso_str, "800") == 0)
                new_iso = SecCamera::ISO_800;
            else if (strcmp(new_iso_str, "1600") == 0)
                new_iso = SecCamera::ISO_1600;
            else if (strcmp(new_iso_str, "sports") == 0)
                new_iso = SecCamera::ISO_SPORTS;
            else if (strcmp(new_iso_str, "night") == 0)
                new_iso = SecCamera::ISO_NIGHT;
            else if (strcmp(new_iso_str, "movie") == 0)
                new_iso = SecCamera::ISO_MOVIE;
            else {
                LOGE("%s::unmatched iso(%d)", __func__, new_iso);
                ret = UNKNOWN_ERROR;
            }

            if (0 <= new_iso) {
                if (mSecCamera->setISO(new_iso) < 0) {
                    LOGE("%s::mSecCamera->setISO(%d) fail", __func__, new_iso);
                    ret = UNKNOWN_ERROR;
                }
            }
        }

        // 2. metering
        if (new_metering_str != NULL) {
            int  new_metering = -1;
            if (strcmp(new_metering_str, "matrix") == 0)
                new_metering = SecCamera::METERING_MATRIX;
            else if (strcmp(new_metering_str, "center") == 0)
                new_metering = SecCamera::METERING_CENTER;
            else if (strcmp(new_metering_str, "spot") == 0)
                new_metering = SecCamera::METERING_SPOT;
            else {
                LOGE("%s::unmatched metering(%s)", __func__, new_metering_str);
                ret = UNKNOWN_ERROR;
            }
            if (0 <= new_metering) {
                if (mSecCamera->setMetering(new_metering) < 0) {
                    LOGE("%s::mSecCamera->setMetering(%d) fail", __func__, new_metering);
                    ret = UNKNOWN_ERROR;
                }
            }
        }

        // 3. brightness
        int max_exposure_compensation = params.getInt(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION);
        int min_exposure_compensation = params.getInt(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION);

        if ((min_exposure_compensation <= new_exposure_compensation) &&
            (max_exposure_compensation >= new_exposure_compensation)) {
            if (mSecCamera->setBrightness(new_exposure_compensation) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->setBrightness(brightness(%d))", __func__, new_exposure_compensation);
                ret = UNKNOWN_ERROR;
            }
        }

        // 4. whitebalance
        if (new_white_str != NULL) {
            int new_white = -1;

            if (strcmp(new_white_str, CameraParameters::WHITE_BALANCE_AUTO) == 0)
                new_white = SecCamera::WHITE_BALANCE_AUTO;
            else if (strcmp(new_white_str, CameraParameters::WHITE_BALANCE_DAYLIGHT) == 0)
                new_white = SecCamera::WHITE_BALANCE_DAYLIGHT;
            else if ((strcmp(new_white_str, "cloudy") == 0) || (strcmp(new_white_str, "cloudy-daylight") == 0))
                new_white = SecCamera::WHITE_BALANCE_CLOUDY;
            else if (strcmp(new_white_str, CameraParameters::WHITE_BALANCE_FLUORESCENT) == 0)
                new_white = SecCamera::WHITE_BALANCE_FLUORESCENT;
            else if (strcmp(new_white_str, CameraParameters::WHITE_BALANCE_INCANDESCENT) == 0)
                new_white = SecCamera::WHITE_BALANCE_INCANDESCENT;
            else {
                LOGE("ERR(%s):Invalid white balance(%s)", __func__, new_white_str); //twilight, shade, warm_flourescent
                ret = UNKNOWN_ERROR;
            }

            if (0 <= new_white) {
                // white_balance
                if (mSecCamera->setWhiteBalance(new_white) < 0) {
                    LOGE("ERR(%s):Fail on mSecCamera->setWhiteBalance(white(%d))", __func__, new_white);
                    ret = UNKNOWN_ERROR;
                }
            }
        }

        //5. sharpness
        if (0 <= new_sharpness) {
            if (mSecCamera->setSharpness(new_sharpness) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->setSharpness(%d)", __func__,new_sharpness);
                ret = UNKNOWN_ERROR;
            }
        }

        //6. saturation
        if (0 <= new_saturation) {
            if (mSecCamera->setSaturation(new_saturation) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->setSaturation(%d)", __func__, new_saturation);
                ret = UNKNOWN_ERROR;
            }
        }

        // 7. focus mode
        if (new_focus_mode_str != NULL) {
            int  new_focus_mode = -1;

            if ((strcmp(new_focus_mode_str, CameraParameters::FOCUS_MODE_AUTO) == 0) || (strcmp(new_focus_mode_str, CameraParameters::FOCUS_MODE_FIXED) == 0) ||(strcmp(new_focus_mode_str, CameraParameters::FOCUS_MODE_INFINITY) == 0))
                new_focus_mode = SecCamera::FOCUS_MODE_AUTO;
            else if (strcmp(new_focus_mode_str, CameraParameters::FOCUS_MODE_MACRO) == 0)
                new_focus_mode = SecCamera::FOCUS_MODE_MACRO;
            else if (strcmp(new_focus_mode_str, "facedetect") == 0)
                new_focus_mode = SecCamera::FOCUS_MODE_FACEDETECT;
            else {
                LOGE("%s::unmatched focus_mode(%s)", __func__, new_focus_mode_str); //infinity
                ret = UNKNOWN_ERROR;
            }

            if (0 <= new_focus_mode) {
                if (mSecCamera->setFocusMode(new_focus_mode) < 0) {
                    LOGE("%s::mSecCamera->setFocusMode(%d) fail", __func__, new_focus_mode);
                    ret = UNKNOWN_ERROR;
                }
            }
        }

        //  8. flash..
        if (new_flash_mode_str != NULL) {
            int  new_flash_mode = -1;

            if (strcmp(new_flash_mode_str, CameraParameters::FLASH_MODE_OFF) == 0)
                new_flash_mode = SecCamera::FLASH_MODE_OFF;
            else if (strcmp(new_flash_mode_str, CameraParameters::FLASH_MODE_AUTO) == 0)
                new_flash_mode = SecCamera::FLASH_MODE_AUTO;
            else if (strcmp(new_flash_mode_str, CameraParameters::FLASH_MODE_ON) == 0)
                new_flash_mode = SecCamera::FLASH_MODE_ON;
            else if (strcmp(new_flash_mode_str, CameraParameters::FLASH_MODE_TORCH) == 0)
                new_flash_mode = SecCamera::FLASH_MODE_TORCH;
            else {
                LOGE("%s::unmatched flash_mode(%s)", __func__, new_flash_mode_str); //red-eye
                ret = UNKNOWN_ERROR;
            }
            if (0 <= new_flash_mode) {
                if (mSecCamera->setFlashMode(new_flash_mode) < 0) {
                    LOGE("%s::mSecCamera->setFlashMode(%d) fail", __func__, new_flash_mode);
                    ret = UNKNOWN_ERROR;
                }
            }
        }

        //  9. scene..
        if (0 <= new_scene_mode) {
            if (mSecCamera->setSceneMode(new_scene_mode) < 0) {
                LOGE("%s::mSecCamera->setSceneMode(%d) fail", __func__, new_scene_mode);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    // ---------------------------------------------------------------------------

    // image effect
    const char *new_image_effect_str = params.get(CameraParameters::KEY_EFFECT);
    if (new_image_effect_str != NULL) {

        int  new_image_effect = -1;

        if ((strcmp(new_image_effect_str, "auto") == 0) ||
            (strcmp(new_image_effect_str, CameraParameters::EFFECT_NONE) == 0))
            new_image_effect = SecCamera::IMAGE_EFFECT_NONE;
        else if ((strcmp(new_image_effect_str, "bnw") == 0) ||
                (strcmp(new_image_effect_str, CameraParameters::EFFECT_MONO) == 0))
            new_image_effect = SecCamera::IMAGE_EFFECT_BNW;
        else if (strcmp(new_image_effect_str, CameraParameters::EFFECT_SEPIA) == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_SEPIA;
        else if (strcmp(new_image_effect_str, CameraParameters::EFFECT_AQUA) == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_AQUA;
        else if (strcmp(new_image_effect_str, "antique") == 0) //added at samsung
            new_image_effect = SecCamera::IMAGE_EFFECT_ANTIQUE;
        else if (strcmp(new_image_effect_str, CameraParameters::EFFECT_NEGATIVE) == 0)
            new_image_effect = SecCamera::IMAGE_EFFECT_NEGATIVE;
        else if (strcmp(new_image_effect_str, "sharpen") == 0) //added at samsung
            new_image_effect = SecCamera::IMAGE_EFFECT_SHARPEN;
        else {
            //posterize, whiteboard, blackboard, solarize
            LOGE("ERR(%s):Invalid effect(%s)", __func__, new_image_effect_str);
            ret = UNKNOWN_ERROR;
        }

        if (new_image_effect >= 0) {
            if (mSecCamera->setImageEffect(new_image_effect) < 0) {
                LOGE("ERR(%s):Fail on mSecCamera->setImageEffect(effect(%d))", __func__, new_image_effect);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    //antiBanding
    const char *new_antibanding_str = params.get(CameraParameters::KEY_ANTIBANDING);

    if (new_antibanding_str != NULL) {
        int new_antibanding = -1;

        if (strcmp(new_antibanding_str, CameraParameters::ANTIBANDING_AUTO) == 0)
            new_antibanding = SecCamera::ANTI_BANDING_AUTO;
        else if (strcmp(new_antibanding_str, CameraParameters::ANTIBANDING_50HZ) == 0)
            new_antibanding = SecCamera::ANTI_BANDING_50HZ;
        else if (strcmp(new_antibanding_str, CameraParameters::ANTIBANDING_60HZ) == 0)
            new_antibanding = SecCamera::ANTI_BANDING_60HZ;
        else if (strcmp(new_antibanding_str, CameraParameters::ANTIBANDING_OFF) == 0)
            new_antibanding = SecCamera::ANTI_BANDING_OFF;
        else {
            LOGE("%s::unmatched antibanding(%s)", __func__, new_antibanding_str);
            ret = UNKNOWN_ERROR;
        }

        if (0 <= new_antibanding) {
            if (mSecCamera->setAntiBanding(new_antibanding) < 0) {
                LOGE("%s::mSecCamera->setAntiBanding(%d) fail", __func__, new_antibanding);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    //contrast
    int new_contrast = params.getInt("contrast");

    if (0 <= new_contrast) {
        if (mSecCamera->setContrast(new_contrast) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setContrast(%d)", __func__, new_contrast);
            ret = UNKNOWN_ERROR;
        }
    }

    //WDR
    int new_wdr = params.getInt("wdr");

    if (0 <= new_wdr) {
        if (mSecCamera->setWDR(new_wdr) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setWDR(%d)", __func__, new_wdr);
            ret = UNKNOWN_ERROR;
        }
    }

    //anti shake
    int new_anti_shake = params.getInt("anti-shake");

    if (0 <= new_anti_shake) {
        if (mSecCamera->setAntiShake(new_anti_shake) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setWDR(%d)", __func__, new_anti_shake);
            ret = UNKNOWN_ERROR;
        }
    }

    //zoom
    int new_zoom_level = params.getInt(CameraParameters::KEY_ZOOM);

    if (0 <= new_zoom_level) {
        if (mSecCamera->setZoom(new_zoom_level) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setZoom(%d)", __func__, new_zoom_level);
            ret = UNKNOWN_ERROR;
        }
    }

    //object tracking
    int new_obj_tracking = params.getInt("obj-tracking");

    if (0 <= new_obj_tracking) {
        if (mSecCamera->setObjectTracking(new_obj_tracking) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setObjectTracking(%d)", __func__, new_obj_tracking);
            ret = UNKNOWN_ERROR;
        }
    }

    // smart auto
    int new_smart_auto = params.getInt("smart-auto");

    if (0 <= new_smart_auto) {
        if (mSecCamera->setSmartAuto(new_smart_auto) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setSmartAuto(%d)", __func__, new_smart_auto);
            ret = UNKNOWN_ERROR;
        }

        //smart auto on => start Smartautoscene Thread
        if (mSecCamera->getSmartAuto() == 1) {
            if (startSmartautoscene() == INVALID_OPERATION) {
                LOGE("Smartautoscene thread is already running");
            } else
                LOGE("Smartautoscene thread start");
        } else { //smart auto off  => stop Smartautoscene Thread
            if (mSmartautosceneRunning == true) {
                stopSmartautoscene();
                LOGV("Smartautoscene thread stop");
            } else
                LOGV("Smartautoscene thread was already stop");
        }
    }

    // beauty_shot
    int new_beauty_shot = params.getInt("face_beauty");

    if (0 <= new_beauty_shot) {
        if (mSecCamera->setBeautyShot(new_beauty_shot) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setBeautyShot(%d)", __func__, new_beauty_shot);
            ret = UNKNOWN_ERROR;
        }
    }

    // vintage mode
    const char *new_vintage_mode_str = params.get("vintagemode");

    if (new_vintage_mode_str != NULL) {
        int  new_vintage_mode = -1;

        if (strcmp(new_vintage_mode_str, "off") == 0)
            new_vintage_mode = SecCamera::VINTAGE_MODE_OFF;
        else if (strcmp(new_vintage_mode_str, "normal") == 0)
            new_vintage_mode = SecCamera::VINTAGE_MODE_NORMAL;
        else if (strcmp(new_vintage_mode_str, "warm") == 0)
            new_vintage_mode = SecCamera::VINTAGE_MODE_WARM;
        else if (strcmp(new_vintage_mode_str, "cool") == 0)
            new_vintage_mode = SecCamera::VINTAGE_MODE_COOL;
        else if (strcmp(new_vintage_mode_str, "bnw") == 0)
            new_vintage_mode = SecCamera::VINTAGE_MODE_BNW;
        else {
            LOGE("%s::unmatched vintage_mode(%d)", __func__, new_vintage_mode);
            ret = UNKNOWN_ERROR;
        }

        if (0 <= new_vintage_mode) {
            if (mSecCamera->setVintageMode(new_vintage_mode) < 0) {
                LOGE("%s::mSecCamera->setVintageMode(%d) fail", __func__, new_vintage_mode);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    // gps latitude
    const char *new_gps_latitude_str = params.get("gps-latitude");

    if (mSecCamera->setGPSLatitude(new_gps_latitude_str) < 0) {
        LOGE("%s::mSecCamera->setGPSLatitude(%s) fail", __func__, new_gps_latitude_str);
        ret = UNKNOWN_ERROR;
    }

    // gps longitude
    const char *new_gps_longitute_str = params.get("gps-longitude");

    if (mSecCamera->setGPSLongitude(new_gps_longitute_str) < 0) {
        LOGE("%s::mSecCamera->setGPSLongitude(%s) fail", __func__, new_gps_longitute_str);
        ret = UNKNOWN_ERROR;
    }

    // gps altitude
    const char *new_gps_altitude_str = params.get("gps-altitude");

    if (mSecCamera->setGPSAltitude(new_gps_altitude_str) < 0) {
        LOGE("%s::mSecCamera->setGPSAltitude(%s) fail", __func__, new_gps_altitude_str);
        ret = UNKNOWN_ERROR;
    }

    // gps timestamp
    const char *new_gps_timestamp_str = params.get("gps-timestamp");

    if (mSecCamera->setGPSTimeStamp(new_gps_timestamp_str) < 0) {
        LOGE("%s::mSecCamera->setGPSTimeStamp(%s) fail", __func__, new_gps_timestamp_str);
        ret = UNKNOWN_ERROR;
    }

    // Recording size
    int new_recording_width = params.getInt("recording-size-width");
    int new_recording_height= params.getInt("recording-size-height");

    if (0 < new_recording_width && 0 < new_recording_height) {
        if (mSecCamera->setRecordingSize(new_recording_width, new_recording_height) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setRecordingSize(width(%d), height(%d))", __func__, new_recording_width, new_recording_height);
            ret = UNKNOWN_ERROR;
        }
    } else {
        if (mSecCamera->setRecordingSize(new_preview_width, new_preview_height) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setRecordingSize(width(%d), height(%d))", __func__, new_preview_width, new_preview_height);
            ret = UNKNOWN_ERROR;
        }
    }

    //gamma
    const char *new_gamma_str = params.get("video_recording_gamma");

    if (new_gamma_str != NULL) {
        int new_gamma = -1;

        if (strcmp(new_gamma_str, "off") == 0)
            new_gamma = SecCamera::GAMMA_OFF;
        else if (strcmp(new_gamma_str, "on") == 0)
            new_gamma = SecCamera::GAMMA_ON;
        else {
            LOGE("%s::unmatched gamma(%s)", __func__, new_gamma_str);
            ret = UNKNOWN_ERROR;
        }

        if (0 <= new_gamma) {
            if (mSecCamera->setGamma(new_gamma) < 0) {
                LOGE("%s::mSecCamera->setGamma(%d) fail", __func__, new_gamma);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    //slow ae
    const char *new_slow_ae_str = params.get("slow_ae");

    if (new_slow_ae_str != NULL) {
        int new_slow_ae = -1;

        if (strcmp(new_slow_ae_str, "off") == 0)
            new_slow_ae = SecCamera::SLOW_AE_OFF;
        else if (strcmp(new_slow_ae_str, "on") == 0)
            new_slow_ae = SecCamera::SLOW_AE_ON;
        else {
            LOGE("%s::unmatched slow_ae(%s)", __func__, new_slow_ae_str);
            ret = UNKNOWN_ERROR;
        }

        if (0 <= new_slow_ae) {
            if (mSecCamera->setSlowAE(new_slow_ae) < 0) {
                LOGE("%s::mSecCamera->setSlowAE(%d) fail", __func__, new_slow_ae);
                ret = UNKNOWN_ERROR;
            }
        }
    }

    //exif orientation info
    int new_exif_orientation = params.getInt("exifOrientation");

    if (0 <= new_exif_orientation) {
        if (mSecCamera->setExifOrientationInfo(new_exif_orientation) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setExifOrientationInfo(%d)", __func__, new_exif_orientation);
            ret = UNKNOWN_ERROR;
        }
    }

    /*Camcorder fix fps*/
    int new_sensor_mode = params.getInt("cam_mode");

    if (0 <= new_sensor_mode) {
        if (mSecCamera->setSensorMode(new_sensor_mode) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setSensorMode(%d)", __func__, new_sensor_mode);
            ret = UNKNOWN_ERROR;
        }
    } else {
        new_sensor_mode=0;
    }

    /*Shot mode*/
    int new_shot_mode = params.getInt("shot_mode");

    if (0 <= new_shot_mode) {
        if (mSecCamera->setShotMode(new_shot_mode) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setShotMode(%d)", __func__, new_shot_mode);
            ret = UNKNOWN_ERROR;
        }
    } else {
        new_shot_mode=0;
    }

    //blur for Video call
    int new_blur_level = params.getInt("blur");

    if (0 <= new_blur_level) {
        if (mSecCamera->setBlur(new_blur_level) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setBlur(%d)", __func__, new_blur_level);
            ret = UNKNOWN_ERROR;
        }
    }


    // chk_dataline
    int new_dataline = params.getInt("chk_dataline");

    if (0 <= new_dataline) {
        if (mSecCamera->setDataLineCheck(new_dataline) < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setDataLineCheck(%d)", __func__, new_dataline);
            ret = UNKNOWN_ERROR;
        }
    }

    //Batch Command
    if (new_camera_id == SecCamera::CAMERA_ID_BACK) {
        if (mSecCamera->setBatchReflection() < 0) {
            LOGE("ERR(%s):Fail on mSecCamera->setBatchCmd", __func__);
            ret = UNKNOWN_ERROR;
        }
    }
#endif
    return ret;
}

CameraParameters CameraHardwareSec::getParameters() const
{
    LOGV("%s()", __func__);
    Mutex::Autolock lock(mLock);
    return mParameters;
}

status_t CameraHardwareSec::sendCommand(int32_t command, int32_t arg1, int32_t arg2)
{
#ifdef SWP1_CAMERA_ADD_ADVANCED_FUNCTION
    LOGV("%s() : command = %d, arg1 =%d, arg2= %d", __func__,command, arg1, arg2);
    switch (command) {
    case COMMAND_AE_AWB_LOCK_UNLOCK:
        mSecCamera->setAEAWBLockUnlock(arg1, arg2);
        break;
    case COMMAND_FACE_DETECT_LOCK_UNLOCK:
        mSecCamera->setFaceDetectLockUnlock(arg1);
        break;
    case COMMAND_OBJECT_POSITION:
        mSecCamera->setObjectPosition(arg1, arg2);
        break;
    case COMMAND_OBJECT_TRACKING_STARTSTOP:
        mSecCamera->setObjectTrackingStartStop(arg1);
        objectTracking(arg1);
        break;
    case CONTINUOUS_SHOT_START_CAPTURE:
        break;
    case CONTINUOUS_SHOT_STOP_AND_ENCODING:
        mSecCamera->setAEAWBLockUnlock(arg1, arg2);
        LOGV("Continuous shot command received");
        break;
    case COMMAND_TOUCH_AF_STARTSTOP:
        mSecCamera->setTouchAFStartStop(arg1);
        break;
    case COMMAND_CHECK_DATALINE:
        mSecCamera->setDataLineCheckStop();
        break;
    case COMMAND_DEFAULT_IMEI:
        mSecCamera->setDefultIMEI(arg1);
        break;
    defualt:
        LOGV("%s()", __func__);
        break;
    }

    return NO_ERROR;
#else
    return BAD_VALUE;
#endif
}

void CameraHardwareSec::release()
{
    LOGV("%s()", __func__);
}

wp<CameraHardwareInterface> CameraHardwareSec::singleton;

sp<CameraHardwareInterface> CameraHardwareSec::createInstance(int cameraId)
{
    LOGV("%s()", __func__);
    if (singleton != 0) {
        sp<CameraHardwareInterface> hardware = singleton.promote();
        if (hardware != 0) {
            return hardware;
        }
    }
    sp<CameraHardwareInterface> hardware(new CameraHardwareSec(cameraId));
    singleton = hardware;
    return hardware;
}
#if 0
extern "C" sp<CameraHardwareInterface> openCameraHardware()
{
    LOGV("%s()", __func__);
    return CameraHardwareSec::createInstance();
}
#endif

static CameraInfo sCameraInfo[] = {
    {
        CAMERA_FACING_BACK,
        90,  /* orientation */
    },
    {
        CAMERA_FACING_FRONT,
        0,  /* orientation */
    }
};

extern "C" int HAL_getNumberOfCameras()
{
    return sizeof(sCameraInfo) / sizeof(sCameraInfo[0]);
}

extern "C" void HAL_getCameraInfo(int cameraId, struct CameraInfo *cameraInfo)
{
    memcpy(cameraInfo, &sCameraInfo[cameraId], sizeof(CameraInfo));
}

extern "C" sp<CameraHardwareInterface> HAL_openCameraHardware(int cameraId)
{
    return CameraHardwareSec::createInstance(cameraId);
}

}; // namespace android