-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-utils.hpp
More file actions
182 lines (168 loc) · 5.86 KB
/
Copy pathsetup-utils.hpp
File metadata and controls
182 lines (168 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef SETUP_UTILS_HPP
#define SETUP_UTILS_HPP
#include "config-editor.hpp"
#include <iostream>
class SetupUtils
{
private:
/**
* @brief Path to the configuration file.
*/
std::string path;
/**
* @brief Configuration editor instance for managing the config file.
*/
ConfigEditor configEditor;
public:
/**
* @brief Constructor that initializes the setup utility with a configuration file path.
* @param configFilePath The path to the configuration file.
*/
explicit SetupUtils(const std::string &configFilePath);
/**
* @brief Destructor
*/
~SetupUtils() = default;
/**
* @brief Save the configuration changes.
* @return true if the configuration was saved successfully, false otherwise.
*/
bool saveConfig();
/**
* @brief Get the current system hostname.
* @return The current hostname as a string.
*/
static std::string getHostname()
{
// For QNX, the hostname is saved in /boot/network
std::string networkConfigPath = "/boot/network";
FILE *file = fopen(networkConfigPath.c_str(), "r");
if (file)
{
char buffer[256];
while (fgets(buffer, sizeof(buffer), file))
{
std::string line(buffer);
if (line.find("HOSTNAME=") == 0)
{
fclose(file);
return line.substr(9, line.find_first_of("\n\r", 9) - 9);
}
}
fclose(file);
return "unknown";
}
else
{
std::cerr << "Error: Unable to open network configuration file: " << networkConfigPath << std::endl;
exit(1);
}
}
/**
* @brief Set the system hostname.
* @param hostname The desired hostname.
* @return The set hostname.
*/
static std::string setHostname(const std::string &hostname)
{
// For QNX, simply write "HOSTNAME=new_hostname" to /boot/network
std::string networkConfigPath = "/boot/network";
FILE *file = fopen(networkConfigPath.c_str(), "a");
if (file)
{
fprintf(file, "HOSTNAME=%s\n", hostname.c_str());
fclose(file);
return hostname;
}
else
{
std::cerr << "Error: Unable to open network configuration file: " << networkConfigPath << std::endl;
exit(1);
}
}
/**
* @brief Get a list of available keyboard layouts.
* @return A vector of strings representing available keyboard layouts.
*/
std::vector<std::string> getAvailableKeyboardLayouts() const
{
return {
"cs_CZ_102",
"da_DK_102",
"de_CH_102",
"de_DE_102",
"en_CA_101",
"en_CA_101_dvorak",
"en_GB_102",
"en_US_101",
"en_US_101_dvorak",
"es_ES_102",
"fr_BE_102",
"fr_CA_102",
"fr_CH_102",
"fr_FR_102",
"hr_HR_102",
"it_IT_102",
"ja_JP_106",
"nl_NL_102",
"no_NO_102",
"pl_PL_102",
"pt_PT_102",
"se_SE_102",
"sk_SK_102"};
}
/**
* @brief Set the keyboard layout in the configuration.
* @param layout The keyboard layout to set (e.g., `en_CA_101`, `fr_CA_102`).
* @return std::string The set keyboard layout.
*/
std::string setKeyboardLayout(const std::string &layout);
/**
* @brief Set the display configuration.
* @param width The display width in pixels (e.g., 1920).
* @param height The display height in pixels (e.g., 1080).
* @param refreshRate The display refresh rate in Hz (e.g., 60).
* @param stackSize The stack size for the display server (default: 65536).
* @param forceComposition Whether to force composition (default: true).
* @param cursor Whether to enable the cursor (default: true, which is to be 'on' in the actual configuration).
* @return std::string The set display configuration.
*/
std::string setDisplay(
const int width, const int height, const int refreshRate,
const int stackSize = 65536, const bool forceComposition = true, const bool cursor = true);
/**
* @brief Set the system timezone.
* @param timezone The desired timezone (e.g., `America/Toronto`, `UTC`, `GMT+2`, `-05:00`).
* @return std::string The set timezone.
* @note This function uses the `_CS_TIMEZONE` to set the timezone in QNX. It is a QNX-only implementation.
*/
static std::string setTimezone(const std::string &timezone)
{
// In QNX, timezone is set via `setconf _CS_TIMEZONE timezone` command.
std::string command = "setconf _CS_TIMEZONE " + timezone;
if (system(command.c_str()) != 0)
{
std::cerr << "Error: Unable to set timezone to " << timezone << std::endl;
exit(1);
}
return timezone;
}
/**
* @brief Update Wi-Fi configuration in a wpa_supplicant.conf file
*
* This function updates the SSID, key management type, and pre-shared key (PSK)
* in a wpa_supplicant.conf file. It reads the existing configuration, modifies
* the relevant fields, and writes the updated configuration back to the file.
*
* @param configPath Path to the wpa_supplicant.conf file
* @param newSSID New SSID to set
* @param newKeyMgmt New key management type (e.g., WPA-PSK)
* @param newPSK New pre-shared key (password)
* @return true if the update was successful, false otherwise
*/
static bool updateWifiConfig(const std::string &configPath,
const std::string &newSSID,
const std::string &newKeyMgmt,
const std::string &newPSK);
};
#endif // SETUP_UTILS_HPP