5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
22 #include "modelslist.h"
24 void getModelPath(char * path
, const char * filename
)
26 strcpy(path
, STR_MODELS_PATH
);
27 path
[sizeof(MODELS_PATH
)-1] = '/';
28 strcpy(&path
[sizeof(MODELS_PATH
)], filename
);
31 const char * writeFile(const char * filename
, const uint8_t * data
, uint16_t size
)
33 TRACE("writeFile(%s)", filename
);
39 FRESULT result
= f_open(&file
, filename
, FA_CREATE_ALWAYS
| FA_WRITE
);
40 if (result
!= FR_OK
) {
41 return SDCARD_ERROR(result
);
44 *(uint32_t*)&buf
[0] = OTX_FOURCC
;
47 *(uint16_t*)&buf
[6] = size
;
49 result
= f_write(&file
, buf
, 8, &written
);
50 if (result
!= FR_OK
|| written
!= 8) {
52 return SDCARD_ERROR(result
);
55 result
= f_write(&file
, data
, size
, &written
);
56 if (result
!= FR_OK
|| written
!= size
) {
58 return SDCARD_ERROR(result
);
65 const char * writeModel()
68 getModelPath(path
, g_eeGeneral
.currModelFilename
);
69 return writeFile(path
, (uint8_t *)&g_model
, sizeof(g_model
));
72 const char * openFile(const char * fullpath
, FIL
* file
, uint16_t* size
)
74 FRESULT result
= f_open(file
, fullpath
, FA_OPEN_EXISTING
| FA_READ
);
75 if (result
!= FR_OK
) {
76 return SDCARD_ERROR(result
);
79 if (f_size(file
) < 8) {
81 return STR_INCOMPATIBLE
;
87 result
= f_read(file
, (uint8_t *)buf
, sizeof(buf
), &read
);
88 if ((result
!= FR_OK
) || (read
!= sizeof(buf
))) {
90 return SDCARD_ERROR(result
);
93 uint8_t version
= (uint8_t)buf
[4];
94 if ((*(uint32_t*)&buf
[0] != OTX_FOURCC
&& *(uint32_t*)&buf
[0] != O9X_FOURCC
) || version
< FIRST_CONV_EEPROM_VER
|| version
> EEPROM_VER
|| buf
[5] != 'M') {
96 return STR_INCOMPATIBLE
;
99 *size
= *(uint16_t*)&buf
[6];
103 const char * loadFile(const char * fullpath
, uint8_t * data
, uint16_t maxsize
)
109 TRACE("loadFile(%s)", fullpath
);
111 const char* err
= openFile(fullpath
, &file
, &size
);
114 size
= min
<uint16_t>(maxsize
, size
);
115 FRESULT result
= f_read(&file
, data
, size
, &read
);
116 if (result
!= FR_OK
|| read
!= size
) {
118 return SDCARD_ERROR(result
);
125 const char * readModel(const char * filename
, uint8_t * buffer
, uint32_t size
)
128 getModelPath(path
, filename
);
129 return loadFile(path
, buffer
, size
);
132 const char * loadModel(const char * filename
, bool alarms
)
136 const char * error
= readModel(filename
, (uint8_t *)&g_model
, sizeof(g_model
));
138 TRACE("loadModel error=%s", error
);
147 postModelLoad(alarms
);
152 const char * loadRadioSettingsSettings()
154 const char * error
= loadFile(RADIO_SETTINGS_PATH
, (uint8_t *)&g_eeGeneral
, sizeof(g_eeGeneral
));
156 TRACE("loadRadioSettingsSettings error=%s", error
);
162 const char * writeGeneralSettings()
164 return writeFile(RADIO_SETTINGS_PATH
, (uint8_t *)&g_eeGeneral
, sizeof(g_eeGeneral
));
167 void storageCheck(bool immediately
)
169 if (storageDirtyMsk
& EE_GENERAL
) {
170 TRACE("eeprom write general");
171 storageDirtyMsk
-= EE_GENERAL
;
172 const char * error
= writeGeneralSettings();
174 TRACE("writeGeneralSettings error=%s", error
);
178 if (storageDirtyMsk
& EE_MODEL
) {
179 TRACE("eeprom write model");
180 storageDirtyMsk
-= EE_MODEL
;
181 const char * error
= writeModel();
183 TRACE("writeModel error=%s", error
);
188 void storageReadAll()
190 TRACE("storageReadAll");
192 if (loadRadioSettingsSettings() != NULL
) {
193 storageEraseAll(true);
197 for (uint8_t i
=0; languagePacks
[i
]!=NULL
; i
++) {
198 if (!strncmp(g_eeGeneral
.ttsLanguage
, languagePacks
[i
]->id
, 2)) {
199 currentLanguagePackIdx
= i
;
200 currentLanguagePack
= languagePacks
[i
];
205 if (loadModel(g_eeGeneral
.currModelFilename
, false) != NULL
) {
206 sdCheckAndCreateDirectory(MODELS_PATH
);
213 void storageCreateModelsList()
217 FRESULT result
= f_open(&file
, RADIO_MODELSLIST_PATH
, FA_CREATE_ALWAYS
| FA_WRITE
);
218 if (result
== FR_OK
) {
219 f_puts("[" DEFAULT_CATEGORY
"]\n" DEFAULT_MODEL_FILENAME
"\n", &file
);
226 sdCheckAndCreateDirectory(RADIO_PATH
);
227 sdCheckAndCreateDirectory(MODELS_PATH
);
228 storageCreateModelsList();
231 const char * createModel()
235 char filename
[LEN_MODEL_FILENAME
+1];
236 memset(filename
, 0, sizeof(filename
));
237 strcpy(filename
, "model.bin");
239 int index
= findNextFileIndex(filename
, LEN_MODEL_FILENAME
, MODELS_PATH
);
242 memcpy(g_eeGeneral
.currModelFilename
, filename
, sizeof(g_eeGeneral
.currModelFilename
));
243 storageDirty(EE_GENERAL
);
244 storageDirty(EE_MODEL
);
247 postModelLoad(false);
249 return g_eeGeneral
.currModelFilename
;
252 void storageEraseAll(bool warn
)
254 TRACE("storageEraseAll");
256 #if defined(COLORLCD)
257 // the theme has not been loaded before
265 ALERT(STR_STORAGE_WARNING
, STR_BAD_RADIO_DATA
, AU_BAD_RADIODATA
);
268 RAISE_ALERT(STR_STORAGE_WARNING
, STR_STORAGE_FORMAT
, NULL
, AU_NONE
);
271 storageDirty(EE_GENERAL
|EE_MODEL
);