Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / utils / src / wrt_global_settings.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        wrt_global_settings.cpp
18  * @version     1.0
19  * @author      Pawel Sikorski(p.sikorski@samsung.com)
20  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
21  * @brief       runtime
22  */
23 #include <cstdlib>
24 #include <cstring>
25 #include <string>
26 #include <sys/utsname.h>
27 #include <dpl/utils/wrt_global_settings.h>
28
29 namespace GlobalSettings {
30
31 namespace {
32 const char* MACHINE_NAME_EMUL = "emulated"; // "arch_emulated"
33 enum MachineType
34 {
35     MACHINE_TYPE_TARGET,
36     MACHINE_TYPE_EMULATOR,
37     MACHINE_TYPE_UNKNOWN
38 };
39
40 struct Settings {
41     bool testMode;
42     bool isEmulator;
43
44     Settings()
45     : testMode(false),
46     isEmulator(false)
47     {}
48 };
49
50 Settings gSettings;
51
52 bool initializeGlobalSettings();
53 bool initHelper = initializeGlobalSettings();
54
55 MachineType getMachineType()
56 {
57     // get current machine name
58     struct utsname u;
59     if (0 == uname(&u)) {
60         if ((!u.machine) || (0 == strlen(u.machine))) {
61             return MACHINE_TYPE_UNKNOWN;
62         } else {
63             // If current machine is emul,
64             // machine name include "<arch>_emulated"
65             std::string machine = u.machine;
66             // find "emulated" string in the u.machine
67             if (std::string::npos != machine.find(MACHINE_NAME_EMUL)) {
68                 return MACHINE_TYPE_EMULATOR;
69             } else {
70                 return MACHINE_TYPE_TARGET;
71             }
72         }
73     }
74
75     return MACHINE_TYPE_UNKNOWN;
76 }
77
78 bool initializeGlobalSettings()
79 {
80     (void)initHelper;
81
82     // ignore environment variables if this flag is not set
83 #ifdef GLOBAL_SETTINGS_CONTROL
84     const char *env = getenv("WRT_TEST_MODE");
85     gSettings.testMode = (env != NULL && 0 == strncmp(env, "1", 1));
86     // TODO other settings initialization
87
88 #endif
89     gSettings.isEmulator = (MACHINE_TYPE_EMULATOR == getMachineType());
90     return false;
91 }
92 } // namespace
93
94 bool TestModeEnabled()
95 {
96     return gSettings.testMode;
97 }
98
99 bool IsEmulator()
100 {
101     return gSettings.isEmulator;
102 }
103
104 } // GlobalSettings