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