1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include "regionprovisioningoperation.h"
#include <QDebug>
#include <QLocale>
#include <QLoggingCategory>
#include "regioninfo.h"
#include "tempdirectories.h"
#include "remotefilefetcher.h"
#include "protobufplugininterface.h"
#include "flipperzero/devicestate.h"
#include "flipperzero/protobufsession.h"
#include "flipperzero/rpc/storagewriteoperation.h"
Q_DECLARE_LOGGING_CATEGORY(CATEGORY_DEBUG)
using namespace Flipper;
using namespace Zero;
RegionProvisioningOperation::RegionProvisioningOperation(ProtobufSession *rpc, DeviceState *_deviceState, QObject *parent):
AbstractUtilityOperation(rpc, _deviceState, parent),
m_regionInfoFile(globalTempDirs->createTempFile(this)),
m_regionDataFile(globalTempDirs->createTempFile(this))
{
connect(this, &AbstractOperation::started, this, [=]() {
deviceState()->setStatusString(QStringLiteral("Setting up region data..."));
});
}
const QString RegionProvisioningOperation::description() const
{
return QStringLiteral("Region Provisioning @%1").arg(deviceState()->deviceInfo().name);
}
const QByteArray RegionProvisioningOperation::localeCountry()
{
const auto localeName = QLocale::system().name();
return localeName.split('_').value(1).toLocal8Bit();
}
void RegionProvisioningOperation::nextStateLogic()
{
if(operationState() == Ready) {
setOperationState(CheckingHardwareRegion);
checkHardwareRegion();
} else if(operationState() == CheckingHardwareRegion) {
setOperationState(FetchingRegionInfo);
fetchRegionInfo();
} else if(operationState() == FetchingRegionInfo) {
setOperationState(GeneratingRegionData);
generateRegionData();
} else if(operationState() == GeneratingRegionData) {
setOperationState(UploadingRegionData);
uploadRegionData();
} else if(operationState() == UploadingRegionData) {
finish();
}
}
void RegionProvisioningOperation::checkHardwareRegion()
{
const auto &hardwareInfo = deviceState()->deviceInfo().hardware;
if(hardwareInfo.region == Region::Dev) {
qCDebug(CATEGORY_DEBUG) << "Development hardware region detected, skipping region provisioning...";
setOperationState(UploadingRegionData);
}
advanceOperationState();
}
void RegionProvisioningOperation::fetchRegionInfo()
{
static const auto apiUrl = QStringLiteral("https://update.flipperzero.one/regions/api/v0/bundle");
auto *fetcher = new RemoteFileFetcher(apiUrl, m_regionInfoFile, this);
if(fetcher->isError()) {
finishWithError(fetcher->error(), QStringLiteral("Failed to fetch region info file: %1").arg(fetcher->errorString()));
return;
}
connect(fetcher, &RemoteFileFetcher::finished, this, [=]() {
if(fetcher->isError()) {
finishWithError(fetcher->error(), QStringLiteral("Failed to fetch region info file: %1").arg(fetcher->errorString()));
} else {
advanceOperationState();
}
});
}
void RegionProvisioningOperation::generateRegionData()
{
if(!m_regionInfoFile->open(QIODevice::ReadOnly)) {
finishWithError(BackendError::DiskError, m_regionInfoFile->errorString());
return;
}
const RegionInfo regionInfo(m_regionInfoFile->readAll());
m_regionInfoFile->close();
if(!regionInfo.isValid()) {
finishWithError(BackendError::DataError, QStringLiteral("Server returned invalid data"));
return;
} else if(regionInfo.isError()) {
finishWithError(BackendError::DataError, regionInfo.errorString());
return;
}
const auto countryCode = regionInfo.hasCountryCode() ? regionInfo.detectedCountry() : localeCountry();
const auto bandKeys = regionInfo.countryBandKeys(countryCode);
qCDebug(CATEGORY_DEBUG) << "Detected region:" << (countryCode.isEmpty() ? QByteArrayLiteral("Unknown") : countryCode);
qCDebug(CATEGORY_DEBUG) << "Allowed bands:" << bandKeys;
BandInfoList bands;
// Convert RegionInfo::BandList to BandInfoList. Probably pointless, used only for decoupling reasons.
const auto allowedBands = regionInfo.bandsByKeys(bandKeys);
for(const auto &band : allowedBands) {
bands.append({
band.start,
band.end,
band.powerLimit,
band.dutyCycle
});
}
if(!m_regionDataFile->open(QIODevice::WriteOnly)) {
finishWithError(BackendError::DiskError, m_regionDataFile->errorString());
return;
}
const auto regionData = rpc()->pluginInstance()->regionBands(countryCode, bands);
if(regionData.isEmpty()) {
finishWithError(BackendError::UnknownError, QStringLiteral("Failed to encode region data"));
return;
}
const auto bytesWritten = m_regionDataFile->write(regionData);
m_regionDataFile->close();
if((bytesWritten <= 0) || (bytesWritten != regionData.size())) {
finishWithError(BackendError::DiskError, QStringLiteral("Failed to save region data to temporary file"));
} else {
advanceOperationState();
}
}
void RegionProvisioningOperation::uploadRegionData()
{
auto *operation = rpc()->storageWrite("/int/.region_data", m_regionDataFile);
connect(operation, &AbstractOperation::finished, this, [=]() {
if(operation->isError()) {
finishWithError(operation->error(), operation->errorString());
} else {
advanceOperationState();
}
});
}
|