mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-26 09:59:01 +00:00
Add LoadFileState to differentiate types of success / failures (#3625)
This commit is contained in:
parent
1d97544041
commit
2803fa964e
@ -551,12 +551,17 @@ static const char *moduleConfigFileName = "/prefs/module.proto";
|
|||||||
static const char *channelFileName = "/prefs/channels.proto";
|
static const char *channelFileName = "/prefs/channels.proto";
|
||||||
static const char *oemConfigFile = "/oem/oem.proto";
|
static const char *oemConfigFile = "/oem/oem.proto";
|
||||||
|
|
||||||
/** Load a protobuf from a file, return true for success */
|
/** Load a protobuf from a file, return LoadFileResult */
|
||||||
bool NodeDB::loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields, void *dest_struct)
|
LoadFileResult NodeDB::loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields,
|
||||||
|
void *dest_struct)
|
||||||
{
|
{
|
||||||
bool okay = false;
|
LoadFileResult state = LoadFileResult::OTHER_FAILURE;
|
||||||
#ifdef FSCom
|
#ifdef FSCom
|
||||||
// static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM
|
|
||||||
|
if (!FSCom.exists(filename)) {
|
||||||
|
LOG_INFO("File %s not found\n", filename);
|
||||||
|
return LoadFileResult::NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
auto f = FSCom.open(filename, FILE_O_READ);
|
auto f = FSCom.open(filename, FILE_O_READ);
|
||||||
|
|
||||||
@ -564,30 +569,32 @@ bool NodeDB::loadProto(const char *filename, size_t protoSize, size_t objSize, c
|
|||||||
LOG_INFO("Loading %s\n", filename);
|
LOG_INFO("Loading %s\n", filename);
|
||||||
pb_istream_t stream = {&readcb, &f, protoSize};
|
pb_istream_t stream = {&readcb, &f, protoSize};
|
||||||
|
|
||||||
// LOG_DEBUG("Preload channel name=%s\n", channelSettings.name);
|
|
||||||
|
|
||||||
memset(dest_struct, 0, objSize);
|
memset(dest_struct, 0, objSize);
|
||||||
if (!pb_decode(&stream, fields, dest_struct)) {
|
if (!pb_decode(&stream, fields, dest_struct)) {
|
||||||
LOG_ERROR("Error: can't decode protobuf %s\n", PB_GET_ERROR(&stream));
|
LOG_ERROR("Error: can't decode protobuf %s\n", PB_GET_ERROR(&stream));
|
||||||
|
state = LoadFileResult::DECODE_FAILED;
|
||||||
} else {
|
} else {
|
||||||
okay = true;
|
LOG_INFO("Loaded %s successfully\n", filename);
|
||||||
|
state = LoadFileResult::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
f.close();
|
f.close();
|
||||||
} else {
|
} else {
|
||||||
LOG_INFO("No %s preferences found\n", filename);
|
LOG_ERROR("Could not open / read %s\n", filename);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
LOG_ERROR("ERROR: Filesystem not implemented\n");
|
LOG_ERROR("ERROR: Filesystem not implemented\n");
|
||||||
|
state = LoadFileState::NO_FILESYSTEM;
|
||||||
#endif
|
#endif
|
||||||
return okay;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeDB::loadFromDisk()
|
void NodeDB::loadFromDisk()
|
||||||
{
|
{
|
||||||
// static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM
|
// static DeviceState scratch; We no longer read into a tempbuf because this structure is 15KB of valuable RAM
|
||||||
if (!loadProto(prefFileName, sizeof(meshtastic_DeviceState) + MAX_NUM_NODES * sizeof(meshtastic_NodeInfo),
|
auto state = loadProto(prefFileName, sizeof(meshtastic_DeviceState) + MAX_NUM_NODES * sizeof(meshtastic_NodeInfo),
|
||||||
sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg, &devicestate)) {
|
sizeof(meshtastic_DeviceState), &meshtastic_DeviceState_msg, &devicestate);
|
||||||
|
|
||||||
|
if (state != LoadFileResult::SUCCESS) {
|
||||||
installDefaultDeviceState(); // Our in RAM copy might now be corrupt
|
installDefaultDeviceState(); // Our in RAM copy might now be corrupt
|
||||||
} else {
|
} else {
|
||||||
if (devicestate.version < DEVICESTATE_MIN_VER) {
|
if (devicestate.version < DEVICESTATE_MIN_VER) {
|
||||||
@ -602,8 +609,9 @@ void NodeDB::loadFromDisk()
|
|||||||
}
|
}
|
||||||
meshNodes->resize(MAX_NUM_NODES);
|
meshNodes->resize(MAX_NUM_NODES);
|
||||||
|
|
||||||
if (!loadProto(configFileName, meshtastic_LocalConfig_size, sizeof(meshtastic_LocalConfig), &meshtastic_LocalConfig_msg,
|
state = loadProto(configFileName, meshtastic_LocalConfig_size, sizeof(meshtastic_LocalConfig), &meshtastic_LocalConfig_msg,
|
||||||
&config)) {
|
&config);
|
||||||
|
if (state != LoadFileResult::SUCCESS) {
|
||||||
installDefaultConfig(); // Our in RAM copy might now be corrupt
|
installDefaultConfig(); // Our in RAM copy might now be corrupt
|
||||||
} else {
|
} else {
|
||||||
if (config.version < DEVICESTATE_MIN_VER) {
|
if (config.version < DEVICESTATE_MIN_VER) {
|
||||||
@ -614,8 +622,9 @@ void NodeDB::loadFromDisk()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!loadProto(moduleConfigFileName, meshtastic_LocalModuleConfig_size, sizeof(meshtastic_LocalModuleConfig),
|
state = loadProto(moduleConfigFileName, meshtastic_LocalModuleConfig_size, sizeof(meshtastic_LocalModuleConfig),
|
||||||
&meshtastic_LocalModuleConfig_msg, &moduleConfig)) {
|
&meshtastic_LocalModuleConfig_msg, &moduleConfig);
|
||||||
|
if (state != LoadFileResult::SUCCESS) {
|
||||||
installDefaultModuleConfig(); // Our in RAM copy might now be corrupt
|
installDefaultModuleConfig(); // Our in RAM copy might now be corrupt
|
||||||
} else {
|
} else {
|
||||||
if (moduleConfig.version < DEVICESTATE_MIN_VER) {
|
if (moduleConfig.version < DEVICESTATE_MIN_VER) {
|
||||||
@ -626,8 +635,9 @@ void NodeDB::loadFromDisk()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!loadProto(channelFileName, meshtastic_ChannelFile_size, sizeof(meshtastic_ChannelFile), &meshtastic_ChannelFile_msg,
|
state = loadProto(channelFileName, meshtastic_ChannelFile_size, sizeof(meshtastic_ChannelFile), &meshtastic_ChannelFile_msg,
|
||||||
&channelFile)) {
|
&channelFile);
|
||||||
|
if (state != LoadFileResult::SUCCESS) {
|
||||||
installDefaultChannels(); // Our in RAM copy might now be corrupt
|
installDefaultChannels(); // Our in RAM copy might now be corrupt
|
||||||
} else {
|
} else {
|
||||||
if (channelFile.version < DEVICESTATE_MIN_VER) {
|
if (channelFile.version < DEVICESTATE_MIN_VER) {
|
||||||
@ -638,7 +648,8 @@ void NodeDB::loadFromDisk()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadProto(oemConfigFile, meshtastic_OEMStore_size, sizeof(meshtastic_OEMStore), &meshtastic_OEMStore_msg, &oemStore)) {
|
state = loadProto(oemConfigFile, meshtastic_OEMStore_size, sizeof(meshtastic_OEMStore), &meshtastic_OEMStore_msg, &oemStore);
|
||||||
|
if (state == LoadFileResult::SUCCESS) {
|
||||||
LOG_INFO("Loaded OEMStore\n");
|
LOG_INFO("Loaded OEMStore\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,19 @@ uint32_t sinceLastSeen(const meshtastic_NodeInfoLite *n);
|
|||||||
/// Given a packet, return how many seconds in the past (vs now) it was received
|
/// Given a packet, return how many seconds in the past (vs now) it was received
|
||||||
uint32_t sinceReceived(const meshtastic_MeshPacket *p);
|
uint32_t sinceReceived(const meshtastic_MeshPacket *p);
|
||||||
|
|
||||||
|
enum LoadFileResult {
|
||||||
|
// Successfully opened the file
|
||||||
|
SUCCESS = 1,
|
||||||
|
// File does not exist
|
||||||
|
NOT_FOUND = 2,
|
||||||
|
// Device does not have a filesystem
|
||||||
|
NO_FILESYSTEM = 3,
|
||||||
|
// File exists, but could not decode protobufs
|
||||||
|
DECODE_FAILED = 4,
|
||||||
|
// File exists, but open failed for some reason
|
||||||
|
OTHER_FAILURE = 5
|
||||||
|
};
|
||||||
|
|
||||||
class NodeDB
|
class NodeDB
|
||||||
{
|
{
|
||||||
// NodeNum provisionalNodeNum; // if we are trying to find a node num this is our current attempt
|
// NodeNum provisionalNodeNum; // if we are trying to find a node num this is our current attempt
|
||||||
@ -115,7 +128,8 @@ class NodeDB
|
|||||||
|
|
||||||
bool factoryReset();
|
bool factoryReset();
|
||||||
|
|
||||||
bool loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields, void *dest_struct);
|
LoadFileResult loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields,
|
||||||
|
void *dest_struct);
|
||||||
bool saveProto(const char *filename, size_t protoSize, const pb_msgdesc_t *fields, const void *dest_struct);
|
bool saveProto(const char *filename, size_t protoSize, const pb_msgdesc_t *fields, const void *dest_struct);
|
||||||
|
|
||||||
void installRoleDefaults(meshtastic_Config_DeviceConfig_Role role);
|
void installRoleDefaults(meshtastic_Config_DeviceConfig_Role role);
|
||||||
|
@ -664,9 +664,9 @@ ProcessMessage CannedMessageModule::handleReceived(const meshtastic_MeshPacket &
|
|||||||
|
|
||||||
void CannedMessageModule::loadProtoForModule()
|
void CannedMessageModule::loadProtoForModule()
|
||||||
{
|
{
|
||||||
if (!nodeDB->loadProto(cannedMessagesConfigFile, meshtastic_CannedMessageModuleConfig_size,
|
if (nodeDB->loadProto(cannedMessagesConfigFile, meshtastic_CannedMessageModuleConfig_size,
|
||||||
sizeof(meshtastic_CannedMessageModuleConfig), &meshtastic_CannedMessageModuleConfig_msg,
|
sizeof(meshtastic_CannedMessageModuleConfig), &meshtastic_CannedMessageModuleConfig_msg,
|
||||||
&cannedMessageModuleConfig)) {
|
&cannedMessageModuleConfig) != LoadFileResult::SUCCESS) {
|
||||||
installDefaultCannedMessageModuleConfig();
|
installDefaultCannedMessageModuleConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -284,8 +284,8 @@ ExternalNotificationModule::ExternalNotificationModule()
|
|||||||
// moduleConfig.external_notification.alert_message_buzzer = true;
|
// moduleConfig.external_notification.alert_message_buzzer = true;
|
||||||
|
|
||||||
if (moduleConfig.external_notification.enabled) {
|
if (moduleConfig.external_notification.enabled) {
|
||||||
if (!nodeDB->loadProto(rtttlConfigFile, meshtastic_RTTTLConfig_size, sizeof(meshtastic_RTTTLConfig),
|
if (nodeDB->loadProto(rtttlConfigFile, meshtastic_RTTTLConfig_size, sizeof(meshtastic_RTTTLConfig),
|
||||||
&meshtastic_RTTTLConfig_msg, &rtttlConfig)) {
|
&meshtastic_RTTTLConfig_msg, &rtttlConfig) != LoadFileResult::SUCCESS) {
|
||||||
memset(rtttlConfig.ringtone, 0, sizeof(rtttlConfig.ringtone));
|
memset(rtttlConfig.ringtone, 0, sizeof(rtttlConfig.ringtone));
|
||||||
strncpy(rtttlConfig.ringtone,
|
strncpy(rtttlConfig.ringtone,
|
||||||
"24:d=32,o=5,b=565:f6,p,f6,4p,p,f6,p,f6,2p,p,b6,p,b6,p,b6,p,b6,p,b,p,b,p,b,p,b,p,b,p,b,p,b,p,b,1p.,2p.,p",
|
"24:d=32,o=5,b=565:f6,p,f6,4p,p,f6,p,f6,2p,p,b6,p,b6,p,b6,p,b6,p,b,p,b,p,b,p,b,p,b,p,b,p,b,p,b,1p.,2p.,p",
|
||||||
|
@ -233,8 +233,8 @@ meshtastic_Neighbor *NeighborInfoModule::getOrCreateNeighbor(NodeNum originalSen
|
|||||||
|
|
||||||
void NeighborInfoModule::loadProtoForModule()
|
void NeighborInfoModule::loadProtoForModule()
|
||||||
{
|
{
|
||||||
if (!nodeDB->loadProto(neighborInfoConfigFile, meshtastic_NeighborInfo_size, sizeof(meshtastic_NeighborInfo),
|
if (nodeDB->loadProto(neighborInfoConfigFile, meshtastic_NeighborInfo_size, sizeof(meshtastic_NeighborInfo),
|
||||||
&meshtastic_NeighborInfo_msg, &neighborState)) {
|
&meshtastic_NeighborInfo_msg, &neighborState) != LoadFileResult::SUCCESS) {
|
||||||
neighborState = meshtastic_NeighborInfo_init_zero;
|
neighborState = meshtastic_NeighborInfo_init_zero;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user