1/* Copyright (c) 2016, The Linux Foundation. All rights reserved.
2*
3* Redistribution and use in source and binary forms, with or without
4* modification, are permitted provided that the following conditions are
5* met:
6*     * Redistributions of source code must retain the above copyright
7*       notice, this list of conditions and the following disclaimer.
8*     * Redistributions in binary form must reproduce the above
9*       copyright notice, this list of conditions and the following
10*       disclaimer in the documentation and/or other materials provided
11*       with the distribution.
12*     * Neither the name of The Linux Foundation nor the names of its
13*       contributors may be used to endorse or promote products derived
14*       from this software without specific prior written permission.
15*
16* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*
28*/
29
30#include "QCameraHAL3MainTestContext.h"
31#include "QCameraHAL3Base.h"
32
33namespace qcamera {
34
35#define MAX_CAMERA_SUPPORTED 20
36
37const CAMERA_BASE_MENU_TBL_T camera_main_menu_tbl[] = {
38    {MENU_START_PREVIEW,             "To Start Preview"},
39    {MENU_START_VIDEO,               "To Start Video"},
40    {MENU_START_CAPTURE,             "To Capture(Non-ZSL)"},
41    {MENU_START_RAW_CAPTURE,         "To Raw Capture"},
42    {MENU_TOGGLE_IR_MODE,            "Toggle IR Mode"},
43    {MENU_TOGGLE_SVHDR_MODE,         "Toggle SVHDR Mode"},
44    {MENU_TOGGLE_BINNING_CORRECTION, "Toggle Binning Correction"},
45    {MENU_EXIT,                      "EXIT"},
46};
47
48const HAL3TEST_SENSOR_MENU_TBL_T sensor_tbl[] = {
49    {"Rear Camera"},
50    {"Front Camera"},
51};
52
53CameraHAL3Base *mCamHal3Base = NULL;
54pthread_mutex_t gCamLock = PTHREAD_MUTEX_INITIALIZER;
55
56MainTestContext::MainTestContext()
57{
58    mTestRunning = false;
59    mCamHal3Base = NULL;
60    irmode = 0;
61    svhdrmode = 0;
62}
63
64int MainTestContext::hal3appGetUserEvent()
65{
66    int choice;
67    int rc = 0, req_capture = 0;
68    int preview_restart;
69    uint8_t num_of_cameras;
70    if (mCamHal3Base != NULL) {
71        delete mCamHal3Base;
72    }
73    mCamHal3Base = new CameraHAL3Base(0);
74    num_of_cameras = mCamHal3Base->hal3appCameraTestLoad();
75    if ((num_of_cameras <= 0) && (num_of_cameras >= MAX_CAMERA_SUPPORTED)) {
76        LOGE("\n Supported Camera Value is wrong : %d", num_of_cameras);
77        printf("\n Invalid Number Of Cameras");
78        goto exit;
79    }
80    else {
81        choice = hal3appDisplaySensorMenu(num_of_cameras);
82        if (choice >= num_of_cameras || choice < 0) {
83            printf("\n Unsupported Parameter");
84            goto exit;
85        }
86        else {
87            mCamHal3Base->mCameraIndex = choice;
88            rc = mCamHal3Base->hal3appCameraLibOpen(choice);
89        }
90    }
91    do {
92        choice = hal3appPrintMenu();
93        switch(choice) {
94            case MENU_START_PREVIEW:
95                mCamHal3Base->hal3appCameraPreviewInit(MENU_START_PREVIEW,
96                        mCamHal3Base->mCameraIndex, PREVIEW_WIDTH, PREVIEW_HEIGHT);
97                mCamHal3Base->mPreviewRunning = 1; mCamHal3Base->mVideoRunning = 0;
98                mCamHal3Base->mSnapShotRunning = 0;
99            break;
100
101            case MENU_START_VIDEO:
102                mCamHal3Base->hal3appCameraVideoInit(MENU_START_VIDEO,
103                        mCamHal3Base->mCameraIndex, VIDEO_WIDTH, VIDEO_HEIGHT);
104                mCamHal3Base->mPreviewRunning = 0; mCamHal3Base->mVideoRunning = 1;
105                mCamHal3Base->mSnapShotRunning = 0;
106            break;
107
108            case MENU_START_CAPTURE:
109                hal3appDisplaySnapshotMenu();
110                req_capture = 1; preview_restart = 0;
111                if (mCamHal3Base->mPreviewRunning == 1) {
112                    preview_restart = 1;
113                }
114                mCamHal3Base->hal3appCameraCaptureInit(0, 0, req_capture);
115                mCamHal3Base->mPreviewRunning = 0; mCamHal3Base->mVideoRunning = 0;
116                mCamHal3Base->mSnapShotRunning = 1;
117                if (preview_restart == 1) {
118                    mCamHal3Base->hal3appCameraPreviewInit(MENU_START_PREVIEW,
119                            mCamHal3Base->mCameraIndex, PREVIEW_WIDTH, PREVIEW_HEIGHT);
120                    mCamHal3Base->mPreviewRunning = 1; mCamHal3Base->mVideoRunning = 0;
121                    mCamHal3Base->mSnapShotRunning = 0;
122                }
123            break;
124
125            case MENU_START_RAW_CAPTURE:
126                hal3appDisplayRawCaptureMenu();
127                req_capture = 3;
128                mCamHal3Base->hal3appRawCaptureInit(0, 0, req_capture);
129                mCamHal3Base->mPreviewRunning = 0; mCamHal3Base->mVideoRunning = 0;
130                mCamHal3Base->mSnapShotRunning = 1;
131            break;
132
133            case MENU_TOGGLE_IR_MODE:
134                 if(mCamHal3Base->mPreviewRunning == 0)
135                    printf(" Cant set IR/SVHDR mode in preview mode only");
136                 else {
137                     irmode = !irmode;
138                     printf("\n Switching IR/SVHDR mode to %s",(irmode ? "On" : "Off"));
139                     ALOGE("\n Switching IR/SVHDR mode to %s and %d",
140                             (irmode ? "On" : "Off"),irmode);
141                     mCamHal3Base->ir_mode = irmode;
142                     mCamHal3Base->hal3appCameraPreviewInit(MENU_TOGGLE_IR_MODE,
143                             mCamHal3Base->mCameraIndex, PREVIEW_WIDTH, PREVIEW_HEIGHT);
144                 }
145            break;
146
147            case MENU_TOGGLE_SVHDR_MODE:
148                 if(mCamHal3Base->mPreviewRunning == 0)
149                    printf(" Cant set IR/SVHDR mode in preview mode only");
150                 else {
151                     svhdrmode = !svhdrmode;
152                     printf("\n Switching IR/SVHDR mode to %s",(svhdrmode ? "On" : "Off"));
153                     ALOGE("\n Switching IR/SVHDR mode to %s and %d",
154                             (svhdrmode ? "On" : "Off"),svhdrmode);
155                     mCamHal3Base->svhdr_mode = svhdrmode;
156                     mCamHal3Base->hal3appCameraPreviewInit(MENU_TOGGLE_SVHDR_MODE,
157                             mCamHal3Base->mCameraIndex, PREVIEW_WIDTH, PREVIEW_HEIGHT);
158                 }
159            break;
160            case MENU_TOGGLE_BINNING_CORRECTION:
161                     if (mCamHal3Base->binning_mode == 0)
162                         mCamHal3Base->binning_mode =  1;
163                     else
164                         mCamHal3Base->binning_mode =  0;
165                     LOGE(" Toggling Binning mode to :%d",mCamHal3Base->binning_mode);
166                     printf(" Toggling to Binning mode :%d",mCamHal3Base->binning_mode );
167                     if(mCamHal3Base->mPreviewRunning == 1)
168                     mCamHal3Base->hal3appCameraPreviewInit(MENU_TOGGLE_BINNING_CORRECTION,
169                                mCamHal3Base->mCameraIndex, PREVIEW_WIDTH, PREVIEW_HEIGHT);
170                     if(mCamHal3Base->mVideoRunning == 1)
171                                mCamHal3Base->hal3appCameraVideoInit(MENU_TOGGLE_BINNING_CORRECTION,
172                        mCamHal3Base->mCameraIndex, VIDEO_WIDTH, VIDEO_HEIGHT);
173            break;
174
175            case MENU_EXIT:
176                hal3appDisplayExitMenu();
177            break;
178
179            default:
180                printf("\n Option not in Menu\n");
181        }
182    }while(choice != MENU_EXIT);
183    exit:
184    return 0;
185}
186
187int MainTestContext::hal3appDisplaySensorMenu(uint8_t num_of_cameras)
188{
189    int i, choice;
190    printf("\n");
191    printf("===========================================\n");
192    printf("    Camera Sensor to be used:            \n");
193    printf("===========================================\n\n");
194
195    for ( i=0;i < num_of_cameras; i++) {
196        if (i <= 1) {
197            printf("\n Press %d to select %s", (i), sensor_tbl[i].menu_name);
198        }
199        else {
200            printf("\n Press %d to select Camera%d", (i), i);
201        }
202    }
203    printf("\n Enter your Choice:");
204    fscanf(stdin, "%d", &choice);
205    return choice;
206}
207
208void MainTestContext::hal3appDisplayCapabilityMenu()
209{
210    printf("\n");
211    printf("===========================================\n");
212    printf("      Sensor Capabilty are dumped at location:\n");
213    printf("===========================================\n\n");
214}
215
216int MainTestContext::hal3appDisplayPreviewMenu()
217{
218    int choice;
219    printf("\n");
220    printf("===========================================\n");
221    printf("Select Camera Preview Resolution:\n");
222    printf("===========================================\n\n");
223    printf("========Select Preview Resolutions================\n");
224    printf("\nPress 1 .Aspect Ratio(4:3) Resolution 1440 X 1080");
225    printf("\nPress 2 .Aspect Ratio(16:9) Resolution 1920 X 1080");
226    printf("\n Enter your Choice:");
227    fscanf(stdin, "%d", &choice);
228    return choice;
229}
230
231int MainTestContext::hal3appDisplayVideoMenu()
232{
233    int choice1;
234    printf("\n");
235    printf("===========================================\n");
236    printf("Testing Camera Recording on Different Resolution:\n");
237    printf("===========================================\n\n");
238
239    printf("========Select Video Resolutions================\n");
240    printf("\nPress 1 .Aspect Ratio(4:3) Resolution 640 X 480");
241    printf("\nPress 2 .Aspect Ratio(16:9) Resolution 1920 X 1080");
242    printf("\nPress 3 .To select both");
243
244    printf("\n Enter your Choice:");
245    fscanf(stdin, "%d", &choice1);
246    return choice1;
247}
248
249void MainTestContext::hal3appDisplayRawCaptureMenu()
250{
251    printf("\n");
252    printf("===========================================\n");
253    printf("Testing RAW Camera Capture on Different Resolution::\n");
254    printf("===========================================\n\n");
255}
256
257void MainTestContext::hal3appDisplaySnapshotMenu()
258{
259    printf("\n");
260    printf("===========================================\n");
261    printf("Testing Normal Camera Capture on Resolution 5344 X 4008\n");
262    printf("===========================================\n\n");
263}
264
265
266void MainTestContext::hal3appDisplayExitMenu()
267{
268    printf("\n");
269    printf("===========================================\n");
270    printf("      Exiting HAL3 APP test \n");
271    printf("===========================================\n\n");
272}
273
274int MainTestContext::hal3appPrintMenu()
275{
276    int i, choice = 0;
277    char ch = '0';
278    printf("\n");
279    printf("===========================================\n");
280    printf("       HAL3 MENU \n");
281    printf("===========================================\n\n");
282    for ( i = 0; i < (int)(sizeof(camera_main_menu_tbl)/sizeof(camera_main_menu_tbl[0])); i++) {
283        printf("\n Press %d to select %s", (i+1), camera_main_menu_tbl[i].menu_name);
284    }
285    printf("\n Enter your Choice:");
286    do {
287        std::cin >> ch;
288    } while(!(ch >= '1' && ch <= '9'));
289    choice = ch -'0';
290    return choice;
291}
292}
293
294int main()
295{
296    char tc_buf[3];
297    int mode = 0;
298    int rc = 0;
299    qcamera::MainTestContext main_ctx;
300    printf("Please Select Execution Mode:\n");
301    printf("0: Menu Based 1: Regression\n");
302    printf("\n Enter your choice:");
303    fgets(tc_buf, 3, stdin);
304    mode = tc_buf[0] - '0';
305    if (mode == 0) {
306        printf("\nStarting Menu based!!\n");
307    } else {
308        printf("\nPlease Enter 0 or 1\n");
309        printf("\nExisting the App!!\n");
310        exit(1);
311    }
312    rc = main_ctx.hal3appGetUserEvent();
313    printf("Exiting application\n");
314    return rc;
315}
316
317