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