BugFix: A establishment of websockets fails.
[profile/ivi/ico-vic-carsimulator.git] / src / CarSim_Daemon.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @file    CarSim_Daemon.cpp
11  * @brief   main entry point
12  */
13
14 #include <sys/types.h>
15 #include <iostream>
16 #include <unistd.h>
17 #include <strings.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 //#include <string>
21 #include "CGtCtrl.h"
22 #include "ico-util/ico_log.h"
23
24 using namespace std;
25
26 #define VERSION "0.1.2"
27
28 bool gbDevJs = false;
29
30 int main(int argc, char **argv)
31 {
32     // parse cmd line
33     bool bUseGps = false;
34     bool bTestMode = false;
35     bool b;
36     bool comFlg = false;
37     bool bDemoRunning = false;
38     int ii;
39
40 //    printf("ico-vic-carsim: userid='%s', uid=%d, euid=%d)\n", cuserid(NULL), getuid(), geteuid());
41     // parse command line
42     for (ii = 1; ii < argc; ii++) {
43         if (strcmp( argv[ii], "-h") == 0) {
44             printf("Usage: CarSim_Daemon [-g]\n");
45             printf("  -g\t Get GPS form smartphone\n");
46             return 0;
47         }
48         else if (strcmp( argv[ii], "-v") == 0) {
49             printf("CarSim_Daemon version:%s\n", VERSION);
50             return 0;
51         }
52         else if (strcmp( argv[ii], "-g") == 0) {
53             bUseGps = true;
54         }
55         else if (strcmp( argv[ii], "-c") == 0) {
56             printf("Using amb plug-in I/F\n");
57             comFlg = true;
58         }
59         else if (strcmp( argv[ii], "-t") == 0) {
60             bTestMode = true;
61         }
62         else if (strcmp( argv[ii], "-j") == 0) {
63             gbDevJs = true;
64         }
65         else if (strcmp( argv[ii], "-r") == 0) {
66             bDemoRunning = true;
67         }
68         else if (strcasecmp( argv[ii], "--user") == 0) {
69             ii ++;
70             if (ii < argc)  {
71                 if (strcmp(argv[ii], cuserid(NULL)) != 0)    {
72                     printf("ico-vic-carsim: abort(cannot run in a '%s' [uid=%d, euid=%d])\n", cuserid(NULL), getuid(), geteuid());
73                     return -1;
74                 }
75             }
76         }
77         else if (strcasecmp( argv[ii], "--uid") == 0) {
78             ii ++;
79             if (ii < argc)  {
80                 if (strtol(argv[ii], NULL, 10) != (long)getuid()) {
81                     printf("ico-vic-carsim: abort(cannot run in a '%s' [uid=%d, euid=%d])\n", cuserid(NULL), getuid(), geteuid());
82                     return -1;
83                 }
84             }
85         }
86     }
87
88     ico_log_open("ico-vic-carsim");
89     ICO_INF( "START_MODULE ico-vic-carsim" );
90
91     if (comFlg) {
92         CGtCtrl myGtCtrl;
93
94         b = myGtCtrl.Initialize();
95
96         if (b) {
97             myGtCtrl.m_bUseGps = bUseGps;
98
99             myGtCtrl.Run2();
100
101             myGtCtrl.Terminate();
102         }
103
104         ICO_INF( "END_MODULE ico-vic-carsim" );
105         return 0;
106     }
107     if (bTestMode) {
108         // test mode
109         CJoyStick cjs;
110         b = cjs.Open();
111         if (b) {
112             printf
113                 ("Carsim_Daemon Test mode...\n Press Ctrl+C to close program...\n");
114             int type;
115             int number;
116             int value;
117
118             while (true) {
119                 type = cjs.Read(&number, &value);
120
121                 if (type >= 0)
122                     printf("type=%d, number=%d, value=%d\n", type, number,
123                            value);
124
125                 usleep(50000);
126             }
127             cjs.Close();
128         }
129         else {
130             printf("joystick open error(test mode)\n");
131         }
132     }
133     else {
134         // change to super user
135         if (setuid(0) < 0)  {
136             printf("can not set super user [errno=%d]\n", errno);
137         }
138
139         CGtCtrl myGtCtrl;
140         myGtCtrl.m_bUseGps = bUseGps;
141         myGtCtrl.m_bDemoRunning = bDemoRunning;
142         b = myGtCtrl.Initialize();
143
144         if (b) {
145             myGtCtrl.Run();
146         }
147         myGtCtrl.Terminate();
148     }
149
150     ICO_INF( "END_MODULE ico-vic-carsim" );
151     return 0;
152 }
153
154 /**
155  * End of File.(CarSim_Daemon)
156  */