use CMAKE_INSTALL_LIBDIR instead of "/usr/lib"
[profile/ivi/smartdevicelink.git] / src / appMain / main.cc
1 /**
2  * \file appMain.cc
3  * \brief SmartDeviceLink main application sources
4  * Copyright (c) 2013, Ford Motor Company
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * Neither the name of the Ford Motor Company nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <cstdio>
38 #include <cstdlib>
39 #include <vector>
40 #include <string>
41 #include <iostream>  // cpplint: Streams are highly discouraged.
42 #include <fstream>   // cpplint: Streams are highly discouraged.
43
44 // ----------------------------------------------------------------------------
45
46 #include "./life_cycle.h"
47
48 #include "utils/signals.h"
49 #include "utils/system.h"
50 #include "config_profile/profile.h"
51
52 #if defined(EXTENDED_MEDIA_MODE)
53 #include <gst/gst.h>
54 #endif
55
56 #include "media_manager/media_manager_impl.h"
57 // ----------------------------------------------------------------------------
58 // Third-Party includes
59 #include "networking.h"  // cpplint: Include the directory when naming .h files
60
61 // ----------------------------------------------------------------------------
62
63 #ifndef SDL_LOG4CXX_PROPERTIES_FILE
64 #define SDL_LOG4CXX_PROPERTIES_FILE "log4cxx.properties"
65 #endif
66
67 #ifndef SDL_HMI_LINK_FILE
68 #define SDL_HMI_LINK_FILE "hmi_link"
69 #endif
70
71 #ifndef SDL_HMI_BROWSER_PATH
72 #define SDL_HMI_BROWSER_PATH "/usr/bin/chromium-browser"
73 #define SDL_HMI_BROWSER_ARG0 "chromium-browser"
74 #define SDL_HMI_BROWSER_ARG1 "--auth-schemes=basic,digest,ntlm"
75 #endif
76
77 CREATE_LOGGERPTR_GLOBAL(logger, "appMain")
78 namespace {
79
80 const std::string kBrowser = SDL_HMI_BROWSER_PATH;
81 const std::string kBrowserName = SDL_HMI_BROWSER_ARG0;
82
83 #ifdef SDL_HMI_BROWSER_ARG1
84 const std::string kBrowserParams = SDL_HMI_BROWSER_ARG1;
85 #endif
86
87 const std::string kLocalHostAddress = "127.0.0.1";
88 const std::string kApplicationVersion = "SDL_RB_B3.5";
89
90 #ifdef WEB_HMI
91 /**
92  * Initialize HTML based HMI.
93  * @return true if success otherwise false.
94  */
95 bool InitHmi() {
96
97 struct stat sb;
98 if (stat(SDL_HMI_LINK_FILE, &sb) == -1) {
99   LOG4CXX_FATAL(logger, "File with HMI link doesn't exist!");
100   return false;
101 }
102
103 std::ifstream file_str;
104 file_str.open(SDL_HMI_LINK_FILE);
105
106 if (!file_str.is_open()) {
107   LOG4CXX_FATAL(logger, "File with HMI link was not opened!");
108   return false;
109 }
110
111 file_str.seekg(0, std::ios::end);
112 int32_t length = file_str.tellg();
113 file_str.seekg(0, std::ios::beg);
114
115 std::string hmi_link;
116 std::getline(file_str, hmi_link);
117
118
119 LOG4CXX_INFO(logger,
120              "Input string:" << hmi_link << " length = " << hmi_link.size());
121 file_str.close();
122
123 if (stat(hmi_link.c_str(), &sb) == -1) {
124   LOG4CXX_INFO(logger, "HMI index.html doesn't exist!");
125   // The hmi_link file in Tizen contains the Crosswalk application ID,
126   // not a top-level HMI web page such as index.html, since we're
127   // launching the HMI through xwalk-launcher.  Ignore the fact that
128   // such a file doesn't exist.
129   //
130   // return false;
131 }
132   return utils::System(kBrowser, kBrowserName)
133 #ifdef SDL_HMI_BROWSER_ARG1
134       .Add(kBrowserParams)
135 #endif
136       .Add(hmi_link).Execute();
137 }
138 #endif  // WEB_HMI
139
140 #ifdef QT_HMI
141 /**
142  * Initialize HTML based HMI.
143  * @return true if success otherwise false.
144  */
145 bool InitHmi() {
146   std::string kStartHmi = "./start_hmi.sh";
147   struct stat sb;
148   if (stat(kStartHmi.c_str(), &sb) == -1) {
149     LOG4CXX_FATAL(logger, "HMI start script doesn't exist!");
150     return false;
151   }
152
153   return utils::System(kStartHmi).Execute();
154 }
155 #endif  // QT_HMI
156
157 }
158
159 /**
160  * \brief Entry point of the program.
161  * \param argc number of argument
162  * \param argv array of arguments
163  * \return EXIT_SUCCESS or EXIT_FAILURE
164  */
165 int32_t main(int32_t argc, char** argv) {
166
167   // --------------------------------------------------------------------------
168   // Logger initialization
169   INIT_LOGGER(SDL_LOG4CXX_PROPERTIES_FILE);
170
171   threads::Thread::SetNameForId(threads::Thread::CurrentId(), "MainThread");
172
173   LOG4CXX_INFO(logger, "Application started!");
174   LOG4CXX_INFO(logger, "Application version " << kApplicationVersion);
175
176   // Initialize gstreamer. Needed to activate debug from the command line.
177 #if defined(EXTENDED_MEDIA_MODE)
178   gst_init(&argc, &argv);
179 #endif
180
181   // --------------------------------------------------------------------------
182   // Components initialization
183   if ((argc > 1)&&(0 != argv)) {
184       profile::Profile::instance()->config_file_name(argv[1]);
185   } else {
186       profile::Profile::instance()->config_file_name(SDL_CONFIG_FILE);
187   }
188
189 #ifdef __QNX__
190   if (!profile::Profile::instance()->policy_turn_off()) {
191     if (!utils::System("./init_policy.sh").Execute(true)) {
192       LOG4CXX_ERROR(logger, "Failed initialization of policy database");
193       DEINIT_LOGGER();
194       exit(EXIT_FAILURE);
195     }
196   }
197 #endif  // __QNX__
198
199   main_namespace::LifeCycle::instance()->StartComponents();
200
201   // --------------------------------------------------------------------------
202   // Third-Party components initialization.
203
204   if (!main_namespace::LifeCycle::instance()->InitMessageSystem()) {
205     main_namespace::LifeCycle::instance()->StopComponents();
206     DEINIT_LOGGER();
207     exit(EXIT_FAILURE);
208   }
209   LOG4CXX_INFO(logger, "InitMessageBroker successful");
210
211   if (profile::Profile::instance()->launch_hmi()) {
212     if (profile::Profile::instance()->server_address() == kLocalHostAddress) {
213       LOG4CXX_INFO(logger, "Start HMI on localhost");
214
215 #ifndef NO_HMI
216       if (!InitHmi()) {
217         main_namespace::LifeCycle::instance()->StopComponents();
218         DEINIT_LOGGER();
219         exit(EXIT_FAILURE);
220       }
221       LOG4CXX_INFO(logger, "InitHmi successful");
222 #endif // #ifndef NO_HMI
223     }
224   }
225   // --------------------------------------------------------------------------
226
227   utils::SubscribeToTerminateSignal(
228     &main_namespace::LifeCycle::StopComponentsOnSignal);
229
230   pause();
231 }