[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / storage / bineeprom.cpp
blobc27d5681c03172aee2605df3ac9dcc3d3ae40b0f
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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.
21 #include "bineeprom.h"
22 #include "eeprominterface.h"
23 #include <QFile>
24 #include <bitset>
26 // TODO should be RleFormat, RawFormat etc.
28 bool BinEepromFormat::load(RadioData & radioData)
30 QFile file(filename);
32 int size = file.size();
34 if (!file.open(QFile::ReadOnly)) {
35 qDebug() << "Unable to open" << filename << file.errorString();
36 return false;
39 QByteArray eeprom(size, 0);
40 int result = file.read((char *)eeprom.data(), size);
41 if (result != size) {
42 setError(QObject::tr("Error reading %1: %2").arg(filename).arg(file.errorString()));
43 return false;
46 return extract(radioData, eeprom);
49 bool BinEepromFormat::write(const RadioData & radioData)
51 bool result;
52 EEPROMInterface * eepromInterface = getCurrentEEpromInterface();
53 int size = Boards::getEEpromSize(eepromInterface->getBoard());
54 if (size == 0) {
55 return false;
57 uint8_t * eeprom = (uint8_t *)malloc(size);
58 int eeprom_size = eepromInterface->save(eeprom, radioData, 0, getCurrentFirmware()->getVariantNumber());
59 if (eeprom_size) {
60 result = writeToFile(eeprom, eeprom_size);
62 else {
63 // TODO here we could call setError(eepromInterface->errors())
64 setError(QObject::tr("Cannot save EEPROM"));
65 result = false;
67 free(eeprom);
68 return result;
71 bool BinEepromFormat::writeToFile(const uint8_t * eeprom, uint32_t size)
73 QFile file(filename);
74 if (!file.open(QIODevice::WriteOnly)) {
75 setError(QObject::tr("Cannot open file %1:\n%2.").arg(filename).arg(file.errorString()));
76 return false;
79 QTextStream outputStream(&file);
80 qint64 len = file.write((char *)eeprom, size);
81 if (len != qint64(size)) {
82 setError(QObject::tr("Error writing file %1:\n%2.").arg(filename).arg(file.errorString()));
83 return false;
86 return true;
89 bool BinEepromFormat::extract(RadioData & radioData, const QByteArray & eeprom)
91 std::bitset<NUM_ERRORS> errors;
93 foreach(EEPROMInterface * eepromInterface, eepromInterfaces) {
94 std::bitset<NUM_ERRORS> result((unsigned long long)eepromInterface->load(radioData, (uint8_t *)eeprom.data(), eeprom.size()));
95 if (result.test(ALL_OK)) {
96 if (errors.test(HAS_WARNINGS)) {
97 // TODO ShowEepromWarnings(this, QObject::tr("Warning"), errors.to_ulong());
99 board = eepromInterface->getBoard();
100 return true;
102 else {
103 errors |= result;
107 setError(QObject::tr("Invalid binary EEPROM file %1").arg(filename));
108 return false;