2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "Environment.h"
18 #include <sys/utsname.h>
22 #include <iniparser.h>
26 #include <commons/Exception.h>
29 const char* VARIABLE_LANGUAGE_NAME = "LANG";
30 const char* VENDOR_NAME = "Samsung";
31 const char* FILE_INFO_INI = "/etc/info.ini";
32 const char* FILE_CPUINFO = "/proc/cpuinfo";
33 const char* PATTERN_CPUINFO_HARDWARE = "^Hardware\\s*:\\s+(\\w+)\\s*$";
36 namespace WrtPlugins {
39 // TODO: Most of those values (if not any) doesn't have to be recalculated every
40 // time someone request them. Optimalization could be introduced to do it
41 // only once, e.g. on first request.
43 Environment& Environment::getInstance()
45 static Environment instance;
49 std::string Environment::getLanguage() const
51 return getVariable(VARIABLE_LANGUAGE_NAME);
54 std::string Environment::getOsVersion() const
57 if (uname(&buf) != 0) {
58 ThrowMsg(Commons::PlatformException, "Could not get uname data.");
63 std::string Environment::getSoftwareVersion() const
65 dictionary* dict = iniparser_load(FILE_INFO_INI);
67 ThrowMsg(Commons::PlatformException, "Could not get SW version.");
70 iniparser_getstring(dict, ("Version:Major"), const_cast<char*>(""));
73 iniparser_getstring(dict, "Version:Minor", const_cast<char*>(""));
77 if ((*major != '\0') && (*minor != '\0')) {
81 iniparser_freedict(dict);
86 std::string Environment::getHardwareName() const
88 FILE* file = fopen(FILE_CPUINFO, "r");
90 ThrowMsg(Commons::UnsupportedException, "Could not open cpuinfo file.");
94 char line[LINE_MAX] = { 0 };
95 pcrecpp::RE re(PATTERN_CPUINFO_HARDWARE);
96 while (fgets(line, LINE_MAX, file) && !re.FullMatch(line, &result)) {
100 if (result.empty()) {
101 ThrowMsg(Commons::UnsupportedException, "Hardware name not found.");
107 std::string Environment::getVendorName() const
112 std::string Environment::getVariable(const char* name)
114 char* value = getenv(name);
116 std::stringstream ss;
117 ss << "Could not read variable " << name << ".";
118 ThrowMsg(Commons::PlatformException, ss.str());
123 Environment::Environment()