Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 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 | #include <linux/input.h> |
| 18 | #include <pthread.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #include <string> |
| 34 | #include <utility> |
| 35 | #include <map> |
| 36 | #include <fstream> |
| 37 | #include <sstream> |
| 38 | |
| 39 | #include "variables.h" |
| 40 | #include "data.hpp" |
| 41 | #include "partitions.hpp" |
| 42 | |
| 43 | extern "C" |
| 44 | { |
| 45 | #include "common.h" |
| 46 | #include "data.h" |
Dees_Troy | 7d15c25 | 2012-09-05 20:47:21 -0400 | [diff] [blame] | 47 | #include "gui/pages.h" |
| 48 | |
| 49 | void gui_notifyVarChange(const char *name, const char* value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | #define FILE_VERSION 0x00010001 |
| 53 | |
| 54 | using namespace std; |
| 55 | |
| 56 | map<string, DataManager::TStrIntPair> DataManager::mValues; |
| 57 | map<string, string> DataManager::mConstValues; |
| 58 | string DataManager::mBackingFile; |
| 59 | int DataManager::mInitialized = 0; |
| 60 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 61 | // Device ID functions |
| 62 | void DataManager::sanitize_device_id(char* device_id) { |
| 63 | const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._"; |
| 64 | char str[50]; |
| 65 | char* c = str; |
| 66 | |
| 67 | strcpy(str, device_id); |
| 68 | memset(device_id, 0, sizeof(device_id)); |
| 69 | while (*c) { |
| 70 | if (strchr(whitelist, *c)) |
| 71 | strncat(device_id, c, 1); |
| 72 | c++; |
| 73 | } |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | #define CMDLINE_SERIALNO "androidboot.serialno=" |
| 78 | #define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO)) |
| 79 | #define CPUINFO_SERIALNO "Serial" |
| 80 | #define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO)) |
| 81 | #define CPUINFO_HARDWARE "Hardware" |
| 82 | #define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE)) |
| 83 | |
| 84 | void DataManager::get_device_id(void) { |
| 85 | FILE *fp; |
| 86 | char line[2048]; |
| 87 | char hardware_id[32], device_id[64]; |
| 88 | char* token; |
| 89 | |
| 90 | // Assign a blank device_id to start with |
| 91 | device_id[0] = 0; |
| 92 | #ifndef TW_FORCE_CPUINFO_FOR_DEVICE_ID |
| 93 | // First, try the cmdline to see if the serial number was supplied |
| 94 | fp = fopen("/proc/cmdline", "rt"); |
| 95 | if (fp != NULL) |
| 96 | { |
| 97 | // First step, read the line. For cmdline, it's one long line |
| 98 | fgets(line, sizeof(line), fp); |
| 99 | fclose(fp); |
| 100 | |
| 101 | // Now, let's tokenize the string |
| 102 | token = strtok(line, " "); |
| 103 | |
| 104 | // Let's walk through the line, looking for the CMDLINE_SERIALNO token |
| 105 | while (token) |
| 106 | { |
| 107 | // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL |
| 108 | if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0) |
| 109 | { |
| 110 | // We found the serial number! |
| 111 | strcpy(device_id, token + CMDLINE_SERIALNO_LEN); |
| 112 | sanitize_device_id((char *)device_id); |
| 113 | mConstValues.insert(make_pair("device_id", device_id)); |
| 114 | return; |
| 115 | } |
| 116 | token = strtok(NULL, " "); |
| 117 | } |
| 118 | } |
| 119 | #endif |
| 120 | // Now we'll try cpuinfo for a serial number |
| 121 | fp = fopen("/proc/cpuinfo", "rt"); |
| 122 | if (fp != NULL) |
| 123 | { |
| 124 | while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line. |
| 125 | if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial" |
| 126 | { |
| 127 | // We found the serial number! |
| 128 | token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial" |
| 129 | while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon |
| 130 | if (*token != 0) { |
| 131 | token[30] = 0; |
| 132 | if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed |
| 133 | memset(device_id, 0, sizeof(device_id)); |
| 134 | strncpy(device_id, token, strlen(token) - 1); |
| 135 | } else { |
| 136 | strcpy(device_id, token); |
| 137 | } |
| 138 | LOGI("=> serial from cpuinfo: '%s'\n", device_id); |
| 139 | fclose(fp); |
| 140 | sanitize_device_id((char *)device_id); |
| 141 | mConstValues.insert(make_pair("device_id", device_id)); |
| 142 | return; |
| 143 | } |
| 144 | } else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID |
| 145 | // We found the hardware ID |
| 146 | token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware" |
| 147 | while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon |
| 148 | if (*token != 0) { |
| 149 | token[30] = 0; |
| 150 | if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed |
| 151 | memset(hardware_id, 0, sizeof(hardware_id)); |
| 152 | strncpy(hardware_id, token, strlen(token) - 1); |
| 153 | } else { |
| 154 | strcpy(hardware_id, token); |
| 155 | } |
| 156 | LOGI("=> hardware id from cpuinfo: '%s'\n", hardware_id); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | fclose(fp); |
| 161 | } |
| 162 | |
| 163 | if (hardware_id[0] != 0) { |
| 164 | LOGW("\nusing hardware id for device id: '%s'\n", hardware_id); |
| 165 | strcpy(device_id, hardware_id); |
| 166 | sanitize_device_id((char *)device_id); |
| 167 | mConstValues.insert(make_pair("device_id", device_id)); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | strcpy(device_id, "serialno"); |
| 172 | LOGE("=> device id not found, using '%s'.", device_id); |
| 173 | mConstValues.insert(make_pair("device_id", device_id)); |
| 174 | return; |
| 175 | } |
| 176 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | int DataManager::ResetDefaults() |
| 178 | { |
| 179 | mValues.clear(); |
| 180 | mConstValues.clear(); |
| 181 | SetDefaultValues(); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int DataManager::LoadValues(const string filename) |
| 186 | { |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 187 | string str, dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 188 | |
| 189 | if (!mInitialized) |
| 190 | SetDefaultValues(); |
| 191 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 192 | GetValue("device_id", dev_id); |
| 193 | // Save off the backing file for set operations |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | mBackingFile = filename; |
| 195 | |
| 196 | // Read in the file, if possible |
| 197 | FILE* in = fopen(filename.c_str(), "rb"); |
| 198 | if (!in) return 0; |
| 199 | |
| 200 | int file_version; |
| 201 | if (fread(&file_version, 1, sizeof(int), in) != sizeof(int)) goto error; |
| 202 | if (file_version != FILE_VERSION) goto error; |
| 203 | |
| 204 | while (!feof(in)) |
| 205 | { |
| 206 | string Name; |
| 207 | string Value; |
| 208 | unsigned short length; |
| 209 | char array[512]; |
| 210 | |
| 211 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 212 | if (length >= 512) goto error; |
| 213 | if (fread(array, 1, length, in) != length) goto error; |
| 214 | Name = array; |
| 215 | |
| 216 | if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error; |
| 217 | if (length >= 512) goto error; |
| 218 | if (fread(array, 1, length, in) != length) goto error; |
| 219 | Value = array; |
| 220 | |
| 221 | map<string, TStrIntPair>::iterator pos; |
| 222 | |
| 223 | pos = mValues.find(Name); |
| 224 | if (pos != mValues.end()) |
| 225 | { |
| 226 | pos->second.first = Value; |
| 227 | pos->second.second = 1; |
| 228 | } |
| 229 | else |
| 230 | mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1))); |
| 231 | } |
| 232 | fclose(in); |
| 233 | |
| 234 | str = GetCurrentStoragePath(); |
| 235 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 236 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 237 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 238 | |
| 239 | return 0; |
| 240 | |
| 241 | error: |
| 242 | // File version mismatch. Use defaults. |
| 243 | fclose(in); |
| 244 | str = GetCurrentStoragePath(); |
| 245 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 246 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | int DataManager::Flush() |
| 252 | { |
| 253 | return SaveValues(); |
| 254 | } |
| 255 | |
| 256 | int DataManager::SaveValues() |
| 257 | { |
| 258 | if (mBackingFile.empty()) return -1; |
| 259 | |
| 260 | string mount_path = GetSettingsStoragePath(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 261 | PartitionManager.Mount_By_Path(mount_path.c_str(), 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 262 | |
| 263 | FILE* out = fopen(mBackingFile.c_str(), "wb"); |
| 264 | if (!out) return -1; |
| 265 | |
| 266 | int file_version = FILE_VERSION; |
| 267 | fwrite(&file_version, 1, sizeof(int), out); |
| 268 | |
| 269 | map<string, TStrIntPair>::iterator iter; |
| 270 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) |
| 271 | { |
| 272 | // Save only the persisted data |
| 273 | if (iter->second.second != 0) |
| 274 | { |
| 275 | unsigned short length = (unsigned short) iter->first.length() + 1; |
| 276 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 277 | fwrite(iter->first.c_str(), 1, length, out); |
| 278 | length = (unsigned short) iter->second.first.length() + 1; |
| 279 | fwrite(&length, 1, sizeof(unsigned short), out); |
| 280 | fwrite(iter->second.first.c_str(), 1, length, out); |
| 281 | } |
| 282 | } |
| 283 | fclose(out); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | int DataManager::GetValue(const string varName, string& value) |
| 288 | { |
| 289 | string localStr = varName; |
| 290 | |
| 291 | if (!mInitialized) |
| 292 | SetDefaultValues(); |
| 293 | |
| 294 | // Strip off leading and trailing '%' if provided |
| 295 | if (localStr.length() > 2 && localStr[0] == '%' && localStr[localStr.length()-1] == '%') |
| 296 | { |
| 297 | localStr.erase(0, 1); |
| 298 | localStr.erase(localStr.length() - 1, 1); |
| 299 | } |
| 300 | |
| 301 | // Handle magic values |
| 302 | if (GetMagicValue(localStr, value) == 0) return 0; |
| 303 | |
| 304 | map<string, string>::iterator constPos; |
| 305 | constPos = mConstValues.find(localStr); |
| 306 | if (constPos != mConstValues.end()) |
| 307 | { |
| 308 | value = constPos->second; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | map<string, TStrIntPair>::iterator pos; |
| 313 | pos = mValues.find(localStr); |
| 314 | if (pos == mValues.end()) |
| 315 | return -1; |
| 316 | |
| 317 | value = pos->second.first; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int DataManager::GetValue(const string varName, int& value) |
| 322 | { |
| 323 | string data; |
| 324 | |
| 325 | if (GetValue(varName,data) != 0) |
| 326 | return -1; |
| 327 | |
| 328 | value = atoi(data.c_str()); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | // This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str |
| 333 | string& DataManager::GetValueRef(const string varName) |
| 334 | { |
| 335 | if (!mInitialized) |
| 336 | SetDefaultValues(); |
| 337 | |
| 338 | map<string, string>::iterator constPos; |
| 339 | constPos = mConstValues.find(varName); |
| 340 | if (constPos != mConstValues.end()) |
| 341 | return constPos->second; |
| 342 | |
| 343 | map<string, TStrIntPair>::iterator pos; |
| 344 | pos = mValues.find(varName); |
| 345 | if (pos == mValues.end()) |
| 346 | pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first; |
| 347 | |
| 348 | return pos->second.first; |
| 349 | } |
| 350 | |
| 351 | // This function will return an empty string if the value doesn't exist |
| 352 | string DataManager::GetStrValue(const string varName) |
| 353 | { |
| 354 | string retVal; |
| 355 | |
| 356 | GetValue(varName, retVal); |
| 357 | return retVal; |
| 358 | } |
| 359 | |
| 360 | // This function will return 0 if the value doesn't exist |
| 361 | int DataManager::GetIntValue(const string varName) |
| 362 | { |
| 363 | string retVal; |
| 364 | |
| 365 | GetValue(varName, retVal); |
| 366 | return atoi(retVal.c_str()); |
| 367 | } |
| 368 | |
| 369 | int DataManager::SetValue(const string varName, string value, int persist /* = 0 */) |
| 370 | { |
| 371 | if (!mInitialized) |
| 372 | SetDefaultValues(); |
| 373 | |
| 374 | // Don't allow empty values or numerical starting values |
| 375 | if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9')) |
| 376 | return -1; |
| 377 | |
| 378 | map<string, string>::iterator constChk; |
| 379 | constChk = mConstValues.find(varName); |
| 380 | if (constChk != mConstValues.end()) |
| 381 | return -1; |
| 382 | |
| 383 | map<string, TStrIntPair>::iterator pos; |
| 384 | pos = mValues.find(varName); |
| 385 | if (pos == mValues.end()) |
| 386 | pos = (mValues.insert(TNameValuePair(varName, TStrIntPair(value, persist)))).first; |
| 387 | else |
| 388 | pos->second.first = value; |
| 389 | |
| 390 | if (pos->second.second != 0) |
| 391 | SaveValues(); |
| 392 | |
| 393 | gui_notifyVarChange(varName.c_str(), value.c_str()); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int DataManager::SetValue(const string varName, int value, int persist /* = 0 */) |
| 398 | { |
| 399 | ostringstream valStr; |
| 400 | valStr << value; |
| 401 | if (varName == "tw_use_external_storage") { |
| 402 | string str; |
| 403 | |
| 404 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 405 | if (value == 0) { |
| 406 | str = GetStrValue(TW_INTERNAL_PATH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 407 | } else { |
| 408 | str = GetStrValue(TW_EXTERNAL_PATH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | } |
| 410 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 411 | str = GetStrValue(TW_INTERNAL_PATH); |
| 412 | else |
| 413 | str = GetStrValue(TW_EXTERNAL_PATH); |
| 414 | |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 415 | string dev_id; |
| 416 | |
| 417 | GetValue("device_id", dev_id); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 418 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 419 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 420 | |
| 421 | SetValue(TW_BACKUPS_FOLDER_VAR, str); |
| 422 | } |
| 423 | return SetValue(varName, valStr.str(), persist);; |
| 424 | } |
| 425 | |
| 426 | int DataManager::SetValue(const string varName, float value, int persist /* = 0 */) |
| 427 | { |
| 428 | ostringstream valStr; |
| 429 | valStr << value; |
| 430 | return SetValue(varName, valStr.str(), persist);; |
| 431 | } |
| 432 | |
| 433 | void DataManager::DumpValues() |
| 434 | { |
| 435 | map<string, TStrIntPair>::iterator iter; |
| 436 | ui_print("Data Manager dump - Values with leading X are persisted.\n"); |
| 437 | for (iter = mValues.begin(); iter != mValues.end(); ++iter) |
| 438 | { |
| 439 | ui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str()); |
| 440 | } |
| 441 | } |
| 442 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 443 | void DataManager::update_tz_environment_variables(void) { |
| 444 | setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1); |
| 445 | tzset(); |
| 446 | } |
| 447 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 448 | void DataManager::SetDefaultValues() |
| 449 | { |
| 450 | string str, path; |
| 451 | |
| 452 | get_device_id(); |
| 453 | |
| 454 | mInitialized = 1; |
| 455 | |
| 456 | mConstValues.insert(make_pair("true", "1")); |
| 457 | mConstValues.insert(make_pair("false", "0")); |
| 458 | |
| 459 | mConstValues.insert(make_pair(TW_VERSION_VAR, TW_VERSION_STR)); |
| 460 | |
| 461 | #ifdef BOARD_HAS_NO_REAL_SDCARD |
| 462 | mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "0")); |
| 463 | #else |
| 464 | mConstValues.insert(make_pair(TW_ALLOW_PARTITION_SDCARD, "1")); |
| 465 | #endif |
| 466 | |
| 467 | #ifdef TW_INCLUDE_DUMLOCK |
| 468 | mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "1")); |
| 469 | #else |
| 470 | mConstValues.insert(make_pair(TW_SHOW_DUMLOCK, "0")); |
| 471 | #endif |
| 472 | |
| 473 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 474 | LOGI("Internal path defined: '%s'\n", EXPAND(TW_INTERNAL_STORAGE_PATH)); |
| 475 | mValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, make_pair("0", 1))); |
| 476 | mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1")); |
| 477 | mConstValues.insert(make_pair(TW_INTERNAL_PATH, EXPAND(TW_INTERNAL_STORAGE_PATH))); |
| 478 | mConstValues.insert(make_pair(TW_INTERNAL_LABEL, EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT))); |
| 479 | path.clear(); |
| 480 | path = "/"; |
| 481 | path += EXPAND(TW_INTERNAL_STORAGE_MOUNT_POINT); |
| 482 | mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, path)); |
| 483 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 484 | LOGI("External path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 485 | // Device has dual storage |
| 486 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1")); |
| 487 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1")); |
| 488 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH))); |
| 489 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT))); |
| 490 | mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1))); |
| 491 | path.clear(); |
| 492 | path = "/"; |
| 493 | path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT); |
| 494 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path)); |
| 495 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 496 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1))); |
| 497 | } else { |
| 498 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 499 | } |
| 500 | #else |
| 501 | LOGI("Just has internal storage.\n"); |
| 502 | // Just has internal storage |
| 503 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 504 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0")); |
| 505 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0")); |
| 506 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "0")); |
| 507 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "0")); |
| 508 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "0")); |
| 509 | #endif |
| 510 | #else |
| 511 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 512 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 513 | LOGI("Has /data/media + external storage in '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 514 | // Device has /data/media + external storage |
| 515 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "1")); |
| 516 | #else |
| 517 | LOGI("Single storage only -- data/media.\n"); |
| 518 | // Device just has external storage |
| 519 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0")); |
| 520 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "0")); |
| 521 | #endif |
| 522 | #else |
| 523 | LOGI("Single storage only.\n"); |
| 524 | // Device just has external storage |
| 525 | mConstValues.insert(make_pair(TW_HAS_DUAL_STORAGE, "0")); |
| 526 | #endif |
| 527 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 528 | LOGI("Device has /data/media defined.\n"); |
| 529 | // Device has /data/media |
| 530 | mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "0")); |
| 531 | mConstValues.insert(make_pair(TW_HAS_INTERNAL, "1")); |
| 532 | mConstValues.insert(make_pair(TW_INTERNAL_PATH, "/data/media")); |
| 533 | mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "/data")); |
| 534 | mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "data")); |
| 535 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 536 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 537 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/emmc", 1))); |
| 538 | } else { |
| 539 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 540 | } |
| 541 | #else |
| 542 | mValues.insert(make_pair(TW_ZIP_INTERNAL_VAR, make_pair("/sdcard", 1))); |
| 543 | #endif |
| 544 | #else |
| 545 | LOGI("No internal storage defined.\n"); |
| 546 | // Device has no internal storage |
| 547 | mConstValues.insert(make_pair(TW_USE_EXTERNAL_STORAGE, "1")); |
| 548 | mConstValues.insert(make_pair(TW_HAS_INTERNAL, "0")); |
| 549 | mConstValues.insert(make_pair(TW_INTERNAL_PATH, "0")); |
| 550 | mConstValues.insert(make_pair(TW_INTERNAL_MOUNT, "0")); |
| 551 | mConstValues.insert(make_pair(TW_INTERNAL_LABEL, "0")); |
| 552 | #endif |
| 553 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 554 | LOGI("Only external path defined: '%s'\n", EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 555 | // External has custom definition |
| 556 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1")); |
| 557 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, EXPAND(TW_EXTERNAL_STORAGE_PATH))); |
| 558 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT))); |
| 559 | mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1))); |
| 560 | path.clear(); |
| 561 | path = "/"; |
| 562 | path += EXPAND(TW_EXTERNAL_STORAGE_MOUNT_POINT); |
| 563 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, path)); |
| 564 | #else |
| 565 | #ifndef RECOVERY_SDCARD_ON_DATA |
| 566 | LOGI("No storage defined, defaulting to /sdcard.\n"); |
| 567 | // Standard external definition |
| 568 | mConstValues.insert(make_pair(TW_HAS_EXTERNAL, "1")); |
| 569 | mConstValues.insert(make_pair(TW_EXTERNAL_PATH, "/sdcard")); |
| 570 | mConstValues.insert(make_pair(TW_EXTERNAL_MOUNT, "/sdcard")); |
| 571 | mConstValues.insert(make_pair(TW_EXTERNAL_LABEL, "sdcard")); |
| 572 | mValues.insert(make_pair(TW_ZIP_EXTERNAL_VAR, make_pair("/sdcard", 1))); |
| 573 | #endif |
| 574 | #endif |
| 575 | #endif |
| 576 | |
| 577 | #ifdef TW_DEFAULT_EXTERNAL_STORAGE |
| 578 | SetValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 579 | LOGI("Defaulting to external storage.\n"); |
| 580 | #endif |
| 581 | |
| 582 | str = GetCurrentStoragePath(); |
| 583 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 584 | #ifndef TW_EXTERNAL_STORAGE_PATH |
| 585 | SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1); |
| 586 | #else |
| 587 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 588 | SetValue(TW_ZIP_LOCATION_VAR, "/emmc", 1); |
| 589 | } else { |
| 590 | SetValue(TW_ZIP_LOCATION_VAR, "/sdcard", 1); |
| 591 | } |
| 592 | #endif |
| 593 | #else |
| 594 | SetValue(TW_ZIP_LOCATION_VAR, str.c_str(), 1); |
| 595 | #endif |
| 596 | str += "/TWRP/BACKUPS/"; |
Dees_Troy | fdf5fcc | 2012-09-11 10:27:01 -0400 | [diff] [blame] | 597 | |
| 598 | string dev_id; |
| 599 | GetValue("device_id", dev_id); |
| 600 | |
| 601 | str += dev_id; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 602 | SetValue(TW_BACKUPS_FOLDER_VAR, str, 0); |
| 603 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 604 | #ifdef SP1_DISPLAY_NAME |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 605 | if (strlen(EXPAND(SP1_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_DISPLAY_NAME))); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 606 | #else |
| 607 | #ifdef SP1_NAME |
| 608 | if (strlen(EXPAND(SP1_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP1_NAME))); |
| 609 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 610 | #endif |
| 611 | #ifdef SP2_DISPLAY_NAME |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 612 | if (strlen(EXPAND(SP2_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP2_PARTITION_NAME_VAR, EXPAND(SP2_DISPLAY_NAME))); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 613 | #else |
| 614 | #ifdef SP2_NAME |
| 615 | if (strlen(EXPAND(SP2_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP2_NAME))); |
| 616 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 617 | #endif |
| 618 | #ifdef SP3_DISPLAY_NAME |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 619 | if (strlen(EXPAND(SP3_DISPLAY_NAME))) mConstValues.insert(make_pair(TW_SP3_PARTITION_NAME_VAR, EXPAND(SP3_DISPLAY_NAME))); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 620 | #else |
| 621 | #ifdef SP3_NAME |
| 622 | if (strlen(EXPAND(SP3_NAME))) mConstValues.insert(make_pair(TW_SP1_PARTITION_NAME_VAR, EXPAND(SP3_NAME))); |
| 623 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 624 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 625 | |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 626 | mConstValues.insert(make_pair(TW_REBOOT_SYSTEM, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 627 | #ifdef TW_NO_REBOOT_RECOVERY |
| 628 | mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "0")); |
| 629 | #else |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 630 | mConstValues.insert(make_pair(TW_REBOOT_RECOVERY, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 631 | #endif |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 632 | mConstValues.insert(make_pair(TW_REBOOT_POWEROFF, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 633 | #ifdef TW_NO_REBOOT_BOOTLOADER |
| 634 | mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "0")); |
| 635 | #else |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 636 | mConstValues.insert(make_pair(TW_REBOOT_BOOTLOADER, "1")); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 637 | #endif |
| 638 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 639 | mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "1")); |
| 640 | #else |
| 641 | mConstValues.insert(make_pair(TW_HAS_DATA_MEDIA, "0")); |
| 642 | #endif |
| 643 | #ifdef TW_NO_BATT_PERCENT |
| 644 | mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "1")); |
| 645 | #else |
| 646 | mConstValues.insert(make_pair(TW_NO_BATTERY_PERCENT, "0")); |
| 647 | #endif |
| 648 | #ifdef TW_CUSTOM_POWER_BUTTON |
| 649 | mConstValues.insert(make_pair(TW_POWER_BUTTON, EXPAND(TW_CUSTOM_POWER_BUTTON))); |
| 650 | #else |
| 651 | mConstValues.insert(make_pair(TW_POWER_BUTTON, "0")); |
| 652 | #endif |
| 653 | #ifdef TW_ALWAYS_RMRF |
| 654 | mConstValues.insert(make_pair(TW_RM_RF_VAR, "1")); |
| 655 | #endif |
| 656 | #ifdef TW_NEVER_UNMOUNT_SYSTEM |
| 657 | mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "1")); |
| 658 | #else |
| 659 | mConstValues.insert(make_pair(TW_DONT_UNMOUNT_SYSTEM, "0")); |
| 660 | #endif |
| 661 | #ifdef TW_NO_USB_STORAGE |
| 662 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0")); |
| 663 | #else |
| 664 | mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1")); |
| 665 | #endif |
| 666 | #ifdef TW_INCLUDE_INJECTTWRP |
| 667 | mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "1")); |
| 668 | mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("1", 1))); |
| 669 | #else |
| 670 | mConstValues.insert(make_pair(TW_HAS_INJECTTWRP, "0")); |
| 671 | mValues.insert(make_pair(TW_INJECT_AFTER_ZIP, make_pair("0", 1))); |
| 672 | #endif |
| 673 | #ifdef TW_FLASH_FROM_STORAGE |
| 674 | mConstValues.insert(make_pair(TW_FLASH_ZIP_IN_PLACE, "1")); |
| 675 | #endif |
| 676 | #ifdef TW_HAS_DOWNLOAD_MODE |
| 677 | mConstValues.insert(make_pair(TW_DOWNLOAD_MODE, "1")); |
| 678 | #endif |
| 679 | #ifdef TW_INCLUDE_CRYPTO |
| 680 | mConstValues.insert(make_pair(TW_HAS_CRYPTO, "1")); |
| 681 | LOGI("Device has crypto support compiled into recovery.\n"); |
| 682 | #endif |
| 683 | #ifdef TW_SDEXT_NO_EXT4 |
| 684 | mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "1")); |
| 685 | #else |
| 686 | mConstValues.insert(make_pair(TW_SDEXT_DISABLE_EXT4, "0")); |
| 687 | #endif |
| 688 | |
| 689 | mConstValues.insert(make_pair(TW_MIN_SYSTEM_VAR, TW_MIN_SYSTEM_SIZE)); |
| 690 | mValues.insert(make_pair(TW_BACKUP_NAME, make_pair("(Current Date)", 0))); |
| 691 | mValues.insert(make_pair(TW_BACKUP_SYSTEM_VAR, make_pair("1", 1))); |
| 692 | mValues.insert(make_pair(TW_BACKUP_DATA_VAR, make_pair("1", 1))); |
| 693 | mValues.insert(make_pair(TW_BACKUP_BOOT_VAR, make_pair("1", 1))); |
| 694 | mValues.insert(make_pair(TW_BACKUP_RECOVERY_VAR, make_pair("0", 1))); |
| 695 | mValues.insert(make_pair(TW_BACKUP_CACHE_VAR, make_pair("0", 1))); |
| 696 | mValues.insert(make_pair(TW_BACKUP_SP1_VAR, make_pair("0", 1))); |
| 697 | mValues.insert(make_pair(TW_BACKUP_SP2_VAR, make_pair("0", 1))); |
| 698 | mValues.insert(make_pair(TW_BACKUP_SP3_VAR, make_pair("0", 1))); |
| 699 | mValues.insert(make_pair(TW_BACKUP_ANDSEC_VAR, make_pair("0", 1))); |
| 700 | mValues.insert(make_pair(TW_BACKUP_SDEXT_VAR, make_pair("0", 1))); |
| 701 | mValues.insert(make_pair(TW_BACKUP_SYSTEM_SIZE, make_pair("0", 0))); |
| 702 | mValues.insert(make_pair(TW_BACKUP_DATA_SIZE, make_pair("0", 0))); |
| 703 | mValues.insert(make_pair(TW_BACKUP_BOOT_SIZE, make_pair("0", 0))); |
| 704 | mValues.insert(make_pair(TW_BACKUP_RECOVERY_SIZE, make_pair("0", 0))); |
| 705 | mValues.insert(make_pair(TW_BACKUP_CACHE_SIZE, make_pair("0", 0))); |
| 706 | mValues.insert(make_pair(TW_BACKUP_ANDSEC_SIZE, make_pair("0", 0))); |
| 707 | mValues.insert(make_pair(TW_BACKUP_SDEXT_SIZE, make_pair("0", 0))); |
| 708 | mValues.insert(make_pair(TW_BACKUP_SP1_SIZE, make_pair("0", 0))); |
| 709 | mValues.insert(make_pair(TW_BACKUP_SP2_SIZE, make_pair("0", 0))); |
| 710 | mValues.insert(make_pair(TW_BACKUP_SP3_SIZE, make_pair("0", 0))); |
| 711 | mValues.insert(make_pair(TW_STORAGE_FREE_SIZE, make_pair("0", 0))); |
| 712 | |
| 713 | mValues.insert(make_pair(TW_REBOOT_AFTER_FLASH_VAR, make_pair("0", 1))); |
| 714 | mValues.insert(make_pair(TW_SIGNED_ZIP_VERIFY_VAR, make_pair("0", 1))); |
| 715 | mValues.insert(make_pair(TW_FORCE_MD5_CHECK_VAR, make_pair("0", 1))); |
| 716 | mValues.insert(make_pair(TW_COLOR_THEME_VAR, make_pair("0", 1))); |
| 717 | mValues.insert(make_pair(TW_USE_COMPRESSION_VAR, make_pair("0", 1))); |
| 718 | mValues.insert(make_pair(TW_IGNORE_IMAGE_SIZE, make_pair("0", 1))); |
| 719 | mValues.insert(make_pair(TW_SHOW_SPAM_VAR, make_pair("0", 1))); |
| 720 | mValues.insert(make_pair(TW_TIME_ZONE_VAR, make_pair("CST6CDT", 1))); |
| 721 | mValues.insert(make_pair(TW_SORT_FILES_BY_DATE_VAR, make_pair("0", 1))); |
| 722 | mValues.insert(make_pair(TW_GUI_SORT_ORDER, make_pair("1", 1))); |
| 723 | mValues.insert(make_pair(TW_RM_RF_VAR, make_pair("0", 1))); |
| 724 | mValues.insert(make_pair(TW_SKIP_MD5_CHECK_VAR, make_pair("0", 1))); |
| 725 | mValues.insert(make_pair(TW_SKIP_MD5_GENERATE_VAR, make_pair("0", 1))); |
| 726 | mValues.insert(make_pair(TW_SDEXT_SIZE, make_pair("512", 1))); |
| 727 | mValues.insert(make_pair(TW_SWAP_SIZE, make_pair("32", 1))); |
| 728 | mValues.insert(make_pair(TW_SDPART_FILE_SYSTEM, make_pair("ext3", 1))); |
| 729 | mValues.insert(make_pair(TW_TIME_ZONE_GUISEL, make_pair("CST6;CDT", 1))); |
| 730 | mValues.insert(make_pair(TW_TIME_ZONE_GUIOFFSET, make_pair("0", 1))); |
| 731 | mValues.insert(make_pair(TW_TIME_ZONE_GUIDST, make_pair("1", 1))); |
| 732 | mValues.insert(make_pair(TW_ACTION_BUSY, make_pair("0", 0))); |
| 733 | mValues.insert(make_pair(TW_BACKUP_AVG_IMG_RATE, make_pair("15000000", 1))); |
| 734 | mValues.insert(make_pair(TW_BACKUP_AVG_FILE_RATE, make_pair("3000000", 1))); |
| 735 | mValues.insert(make_pair(TW_BACKUP_AVG_FILE_COMP_RATE, make_pair("2000000", 1))); |
| 736 | mValues.insert(make_pair(TW_RESTORE_AVG_IMG_RATE, make_pair("15000000", 1))); |
| 737 | mValues.insert(make_pair(TW_RESTORE_AVG_FILE_RATE, make_pair("3000000", 1))); |
| 738 | mValues.insert(make_pair(TW_RESTORE_AVG_FILE_COMP_RATE, make_pair("2000000", 1))); |
| 739 | if (GetIntValue(TW_HAS_INTERNAL) == 1 && GetIntValue(TW_HAS_DATA_MEDIA) == 1 && GetIntValue(TW_HAS_EXTERNAL) == 0) |
| 740 | SetValue(TW_HAS_USB_STORAGE, 0, 0); |
| 741 | else |
| 742 | SetValue(TW_HAS_USB_STORAGE, 1, 0); |
| 743 | mValues.insert(make_pair(TW_ZIP_INDEX, make_pair("0", 0))); |
| 744 | mValues.insert(make_pair(TW_ZIP_QUEUE_COUNT, make_pair("0", 0))); |
| 745 | mValues.insert(make_pair(TW_FILENAME, make_pair("/sdcard", 0))); |
| 746 | mValues.insert(make_pair(TW_SIMULATE_ACTIONS, make_pair("0", 1))); |
| 747 | mValues.insert(make_pair(TW_SIMULATE_FAIL, make_pair("0", 1))); |
| 748 | mValues.insert(make_pair(TW_IS_ENCRYPTED, make_pair("0", 0))); |
| 749 | mValues.insert(make_pair(TW_IS_DECRYPTED, make_pair("0", 0))); |
| 750 | mValues.insert(make_pair(TW_CRYPTO_PASSWORD, make_pair("0", 0))); |
| 751 | mValues.insert(make_pair(TW_DATA_BLK_DEVICE, make_pair("0", 0))); |
| 752 | mValues.insert(make_pair("tw_terminal_state", make_pair("0", 0))); |
| 753 | mValues.insert(make_pair("tw_background_thread_running", make_pair("0", 0))); |
| 754 | mValues.insert(make_pair(TW_RESTORE_FILE_DATE, make_pair("0", 0))); |
| 755 | } |
| 756 | |
| 757 | // Magic Values |
| 758 | int DataManager::GetMagicValue(const string varName, string& value) |
| 759 | { |
| 760 | // Handle special dynamic cases |
| 761 | if (varName == "tw_time") |
| 762 | { |
| 763 | char tmp[32]; |
| 764 | |
| 765 | struct tm *current; |
| 766 | time_t now; |
| 767 | now = time(0); |
| 768 | current = localtime(&now); |
| 769 | |
| 770 | if (current->tm_hour >= 12) |
| 771 | sprintf(tmp, "%d:%02d PM", current->tm_hour == 12 ? 12 : current->tm_hour - 12, current->tm_min); |
| 772 | else |
| 773 | sprintf(tmp, "%d:%02d AM", current->tm_hour == 0 ? 12 : current->tm_hour, current->tm_min); |
| 774 | |
| 775 | value = tmp; |
| 776 | return 0; |
| 777 | } |
| 778 | if (varName == "tw_battery") |
| 779 | { |
| 780 | char tmp[16]; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 781 | static char charging = ' '; |
| 782 | static int lastVal = -1; |
| 783 | static time_t nextSecCheck = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 784 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 785 | struct timeval curTime; |
| 786 | gettimeofday(&curTime, NULL); |
| 787 | if (curTime.tv_sec > nextSecCheck) |
| 788 | { |
| 789 | char cap_s[4]; |
| 790 | FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt"); |
| 791 | if (cap){ |
| 792 | fgets(cap_s, 4, cap); |
| 793 | fclose(cap); |
| 794 | lastVal = atoi(cap_s); |
| 795 | if (lastVal > 100) lastVal = 101; |
| 796 | if (lastVal < 0) lastVal = 0; |
| 797 | } |
| 798 | cap = fopen("/sys/class/power_supply/battery/status","rt"); |
| 799 | if (cap) { |
| 800 | fgets(cap_s, 2, cap); |
| 801 | fclose(cap); |
| 802 | if (cap_s[0] == 'C') |
| 803 | charging = '+'; |
| 804 | else |
| 805 | charging = ' '; |
| 806 | } |
| 807 | nextSecCheck = curTime.tv_sec + 60; |
| 808 | } |
| 809 | |
| 810 | sprintf(tmp, "%i%%%c", lastVal, charging); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 811 | value = tmp; |
| 812 | return 0; |
| 813 | } |
| 814 | return -1; |
| 815 | } |
| 816 | |
| 817 | void DataManager::ReadSettingsFile(void) |
| 818 | { |
| 819 | // Load up the values for TWRP - Sleep to let the card be ready |
| 820 | char mkdir_path[255], settings_file[255]; |
| 821 | int is_enc, has_dual, use_ext, has_data_media, has_ext; |
| 822 | |
| 823 | GetValue(TW_IS_ENCRYPTED, is_enc); |
| 824 | GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
| 825 | if (is_enc == 1 && has_data_media == 1) { |
| 826 | LOGI("Cannot load settings -- encrypted.\n"); |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | memset(mkdir_path, 0, sizeof(mkdir_path)); |
| 831 | memset(settings_file, 0, sizeof(settings_file)); |
| 832 | sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath()); |
| 833 | sprintf(settings_file, "%s/.twrps", mkdir_path); |
| 834 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 835 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 836 | { |
| 837 | usleep(500000); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 838 | if (!PartitionManager.Mount_Settings_Storage(false)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 839 | LOGE("Unable to mount %s when trying to read settings file.\n", DataManager_GetSettingsStorageMount()); |
| 840 | } |
| 841 | |
| 842 | mkdir(mkdir_path, 0777); |
| 843 | |
| 844 | LOGI("Attempt to load settings from settings file...\n"); |
| 845 | LoadValues(settings_file); |
| 846 | GetValue(TW_HAS_DUAL_STORAGE, has_dual); |
| 847 | GetValue(TW_USE_EXTERNAL_STORAGE, use_ext); |
| 848 | GetValue(TW_HAS_EXTERNAL, has_ext); |
| 849 | if (has_dual != 0 && use_ext == 1) { |
| 850 | // Attempt to sdcard using external storage |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 851 | if (!PartitionManager.Mount_Current_Storage(false)) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 852 | LOGE("Failed to mount external storage, using internal storage.\n"); |
| 853 | // Remount failed, default back to internal storage |
| 854 | SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 855 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | e2920fa | 2012-09-19 16:18:00 -0400 | [diff] [blame] | 856 | string int_zip_path; |
| 857 | GetValue(TW_ZIP_INTERNAL_VAR, int_zip_path); |
| 858 | SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 859 | SetValue(TW_ZIP_LOCATION_VAR, int_zip_path); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 860 | } |
| 861 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 862 | PartitionManager.Mount_Current_Storage(true); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 863 | } |
| 864 | if (has_data_media == 1) { |
| 865 | if (has_dual == 0) { |
| 866 | LOGI("Mounting /data/media to /sdcard\n"); |
| 867 | system("umount /sdcard"); |
| 868 | system("mount /data/media /sdcard"); |
| 869 | } else { |
| 870 | string ext_path; |
| 871 | |
| 872 | GetValue(TW_EXTERNAL_PATH, ext_path); |
| 873 | if (ext_path == "/sdcard") { |
| 874 | LOGI("Mounting /data/media to /emmc\n"); |
| 875 | system("cd / && mkdir emmc"); |
| 876 | system("umount /emmc"); |
| 877 | system("mount /data/media /emmc"); |
| 878 | } else { |
| 879 | LOGI("Mounting /data/media to /sdcard\n"); |
| 880 | system("umount /sdcard"); |
| 881 | system("mount /data/media /sdcard"); |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 886 | if (has_ext) { |
| 887 | string ext_path; |
| 888 | |
| 889 | GetValue(TW_EXTERNAL_PATH, ext_path); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 890 | PartitionManager.Mount_By_Path(ext_path, 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 891 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 892 | update_tz_environment_variables(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | string DataManager::GetCurrentStoragePath(void) |
| 896 | { |
| 897 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 898 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 899 | return GetStrValue(TW_INTERNAL_PATH); |
| 900 | else |
| 901 | return GetStrValue(TW_EXTERNAL_PATH); |
| 902 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 903 | return GetStrValue(TW_INTERNAL_PATH); |
| 904 | else |
| 905 | return GetStrValue(TW_EXTERNAL_PATH); |
| 906 | } |
| 907 | |
| 908 | string& DataManager::CGetCurrentStoragePath() |
| 909 | { |
| 910 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 911 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 912 | return GetValueRef(TW_INTERNAL_PATH); |
| 913 | else |
| 914 | return GetValueRef(TW_EXTERNAL_PATH); |
| 915 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 916 | return GetValueRef(TW_INTERNAL_PATH); |
| 917 | else |
| 918 | return GetValueRef(TW_EXTERNAL_PATH); |
| 919 | } |
| 920 | |
| 921 | string DataManager::GetCurrentStorageMount(void) |
| 922 | { |
| 923 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 924 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 925 | return GetStrValue(TW_INTERNAL_MOUNT); |
| 926 | else |
| 927 | return GetStrValue(TW_EXTERNAL_MOUNT); |
| 928 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 929 | return GetStrValue(TW_INTERNAL_MOUNT); |
| 930 | else |
| 931 | return GetStrValue(TW_EXTERNAL_MOUNT); |
| 932 | } |
| 933 | |
| 934 | string& DataManager::CGetCurrentStorageMount() |
| 935 | { |
| 936 | if (GetIntValue(TW_HAS_DUAL_STORAGE) == 1) { |
| 937 | if (GetIntValue(TW_USE_EXTERNAL_STORAGE) == 0) |
| 938 | return GetValueRef(TW_INTERNAL_MOUNT); |
| 939 | else |
| 940 | return GetValueRef(TW_EXTERNAL_MOUNT); |
| 941 | } else if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 942 | return GetValueRef(TW_INTERNAL_MOUNT); |
| 943 | else |
| 944 | return GetValueRef(TW_EXTERNAL_MOUNT); |
| 945 | } |
| 946 | |
| 947 | string DataManager::GetSettingsStoragePath(void) |
| 948 | { |
| 949 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 950 | return GetStrValue(TW_INTERNAL_PATH); |
| 951 | else |
| 952 | return GetStrValue(TW_EXTERNAL_PATH); |
| 953 | } |
| 954 | |
| 955 | string& DataManager::CGetSettingsStoragePath() |
| 956 | { |
| 957 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 958 | return GetValueRef(TW_INTERNAL_PATH); |
| 959 | else |
| 960 | return GetValueRef(TW_EXTERNAL_PATH); |
| 961 | } |
| 962 | |
| 963 | string DataManager::GetSettingsStorageMount(void) |
| 964 | { |
| 965 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 966 | return GetStrValue(TW_INTERNAL_MOUNT); |
| 967 | else |
| 968 | return GetStrValue(TW_EXTERNAL_MOUNT); |
| 969 | } |
| 970 | |
| 971 | string& DataManager::CGetSettingsStorageMount() |
| 972 | { |
| 973 | if (GetIntValue(TW_HAS_INTERNAL) == 1) |
| 974 | return GetValueRef(TW_INTERNAL_MOUNT); |
| 975 | else |
| 976 | return GetValueRef(TW_EXTERNAL_MOUNT); |
| 977 | } |
| 978 | |
| 979 | extern "C" int DataManager_ResetDefaults() |
| 980 | { |
| 981 | return DataManager::ResetDefaults(); |
| 982 | } |
| 983 | |
| 984 | extern "C" void DataManager_LoadDefaults() |
| 985 | { |
| 986 | return DataManager::SetDefaultValues(); |
| 987 | } |
| 988 | |
| 989 | extern "C" int DataManager_LoadValues(const char* filename) |
| 990 | { |
| 991 | return DataManager::LoadValues(filename); |
| 992 | } |
| 993 | |
| 994 | extern "C" int DataManager_Flush() |
| 995 | { |
| 996 | return DataManager::Flush(); |
| 997 | } |
| 998 | |
| 999 | extern "C" int DataManager_GetValue(const char* varName, char* value) |
| 1000 | { |
| 1001 | int ret; |
| 1002 | string str; |
| 1003 | |
| 1004 | ret = DataManager::GetValue(varName, str); |
| 1005 | if (ret == 0) |
| 1006 | strcpy(value, str.c_str()); |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | extern "C" const char* DataManager_GetStrValue(const char* varName) |
| 1011 | { |
| 1012 | string& str = DataManager::GetValueRef(varName); |
| 1013 | return str.c_str(); |
| 1014 | } |
| 1015 | |
| 1016 | extern "C" const char* DataManager_GetCurrentStoragePath(void) |
| 1017 | { |
| 1018 | string& str = DataManager::CGetCurrentStoragePath(); |
| 1019 | return str.c_str(); |
| 1020 | } |
| 1021 | |
| 1022 | extern "C" const char* DataManager_GetSettingsStoragePath(void) |
| 1023 | { |
| 1024 | string& str = DataManager::CGetSettingsStoragePath(); |
| 1025 | return str.c_str(); |
| 1026 | } |
| 1027 | |
| 1028 | extern "C" const char* DataManager_GetCurrentStorageMount(void) |
| 1029 | { |
| 1030 | string& str = DataManager::CGetCurrentStorageMount(); |
| 1031 | return str.c_str(); |
| 1032 | } |
| 1033 | |
| 1034 | extern "C" const char* DataManager_GetSettingsStorageMount(void) |
| 1035 | { |
| 1036 | string& str = DataManager::CGetSettingsStorageMount(); |
| 1037 | return str.c_str(); |
| 1038 | } |
| 1039 | |
| 1040 | extern "C" int DataManager_GetIntValue(const char* varName) |
| 1041 | { |
| 1042 | return DataManager::GetIntValue(varName); |
| 1043 | } |
| 1044 | |
| 1045 | extern "C" int DataManager_SetStrValue(const char* varName, char* value) |
| 1046 | { |
| 1047 | return DataManager::SetValue(varName, value, 0); |
| 1048 | } |
| 1049 | |
| 1050 | extern "C" int DataManager_SetIntValue(const char* varName, int value) |
| 1051 | { |
| 1052 | return DataManager::SetValue(varName, value, 0); |
| 1053 | } |
| 1054 | |
| 1055 | extern "C" int DataManager_SetFloatValue(const char* varName, float value) |
| 1056 | { |
| 1057 | return DataManager::SetValue(varName, value, 0); |
| 1058 | } |
| 1059 | |
| 1060 | extern "C" int DataManager_ToggleIntValue(const char* varName) |
| 1061 | { |
| 1062 | if (DataManager::GetIntValue(varName)) |
| 1063 | return DataManager::SetValue(varName, 0); |
| 1064 | else |
| 1065 | return DataManager::SetValue(varName, 1); |
| 1066 | } |
| 1067 | |
| 1068 | extern "C" void DataManager_DumpValues() |
| 1069 | { |
| 1070 | return DataManager::DumpValues(); |
| 1071 | } |
| 1072 | |
| 1073 | extern "C" void DataManager_ReadSettingsFile() |
| 1074 | { |
| 1075 | return DataManager::ReadSettingsFile(); |
| 1076 | } |