#include "config.h" #include "ch32v30x_flash.h" #include "util/hexdump.h" #include "util/log.h" PersistentData_t persistent; #define TAG "Config" NodeEntry * getNextNode() { uint32_t oldest_timestamp = UINT32_MAX; NodeEntry *selectedNode = &(persistent.contacts[0]); for (int i = 0; i < CONTACT_COUNT; i++) { NodeEntry *curNode = &(persistent.contacts[i]); if (curNode->last_seen_lt == 0) { break; } if (curNode->last_seen_lt < oldest_timestamp) { oldest_timestamp = curNode->last_seen_lt; selectedNode = curNode; } } return selectedNode; } NodeEntry * getNode(uint8_t hash) { NodeEntry *selectedNode = NULL; for (int i = 0; i < CONTACT_COUNT; i++) { NodeEntry *curNode = &(persistent.contacts[i]); if (curNode->pubKey[0] == hash) { selectedNode = curNode; break; } } return selectedNode; } void printNodeDB() { printf("Node database:\n"); for (int i = 0; i < CONTACT_COUNT; i++) { const NodeEntry *node = &(persistent.contacts[i]); if (node->last_seen_lt == 0) continue; // skip inactive nodes printf("Node %d:\n", i); printf(" Name: %s\n", node->name); hexdump("Pubkey", node->pubKey, sizeof(node->pubKey)); hexdump("Secret", node->secret, sizeof(node->secret)); printf("\n"); printf(" GPS: lat=%d, lon=%d\n", node->gps_latitude, node->gps_longitude); printf(" Path: ... (not expanded, add if needed)\n"); printf(" Flags: 0x%02X\n", node->flags); printf(" Type: 0x%02X\n", node->type); printf(" Authenticated: %s\n", node->authenticated ? "Yes" : "No"); printf(" Last Seen (remote ts): %d\n", node->last_seen_rt); printf(" Last Seen (local ts): %d\n", node->last_seen_lt); printf(" Sync timestamp: %d\n", node->sync_timestamp); printf("--------------------------------------\n"); } } void loadConfig() { memcpy (&persistent, FLASH_USER_PAGE_ADDR, sizeof (persistent)); uint32_t crcSum = *((uint32_t *)(((uint8_t *)&persistent) + (sizeof (persistent) - 2))); memset ((((uint8_t *)&persistent) + (sizeof (persistent) - sizeof(crcSum))), 0, 4); CRC_ResetDR(); uint32_t currentSum = CRC_CalcBlockCRC ((uint32_t *)&persistent, sizeof (persistent) - 2); if (currentSum != crcSum) { memset (&persistent, 0, sizeof (persistent)); } } void saveConfig() { CRC_ResetDR(); uint32_t currentSum = CRC_CalcBlockCRC ((uint32_t *)&persistent, sizeof (persistent) - 2); memcpy ((((uint8_t *)&persistent) + (sizeof (persistent) - sizeof(currentSum))), (uint8_t *)currentSum, 4); FLASH_Unlock(); FLASH_ErasePage_Fast (1919); FLASH_ProgramPage_Fast (1919, (uint32_t *)&persistent); FLASH_Lock(); }