#include "my_candidate_applications.h" #include "mbed_trace.h" #if MBED_CONF_MBED_TRACE_ENABLE #define TRACE_GROUP "MyCandidateApplications" #endif // MBED_CONF_MBED_TRACE_ENABLE update_client::CandidateApplications *createCandidateApplications( BlockDevice &blockDevice, mbed::bd_addr_t storageAddr, mbed::bd_size_t storageSize, uint32_t headerSize, uint32_t nSlots ){ tr_debug("Create my candidate application"); update_client::CandidateApplications* candidate = new bike_computer::MyCandidateApplications(blockDevice, storageAddr, storageSize, headerSize, nSlots); return candidate; } namespace bike_computer { MyCandidateApplications::MyCandidateApplications( BlockDevice &blockDevice, mbed::bd_addr_t storageAddr, mbed::bd_size_t storageSize, uint32_t headerSize, uint32_t nSlots ) : update_client::CandidateApplications(blockDevice, storageAddr, storageSize, headerSize, nSlots) { } uint32_t MyCandidateApplications::getSlotForCandidate() { tr_debug("Get slot for candidate"); uint32_t nbrOfSlots = getNbrOfSlots(); for (uint32_t slotIndex = 0; slotIndex < nbrOfSlots; slotIndex++) { if (! getBlockDeviceApplication(slotIndex).isValid()) { return slotIndex; } } // return the slot of the oldest firmware candidates uint32_t oldestSlotIndex = 0; uint64_t oldestFirmwareVersion = getBlockDeviceApplication(oldestSlotIndex).getFirmwareVersion(); for (uint32_t slotIndex = 1; slotIndex < nbrOfSlots; slotIndex++) { mbed::bd_addr_t candidateAddress = 0; mbed::bd_size_t slotSize = 0; getCandidateAddress(slotIndex, candidateAddress, slotSize); tr_debug("Checking application at slot %" PRIu32 " (address 0x%" PRIx64 ", size %" PRIu64 ")", slotIndex, candidateAddress, slotSize); if (getBlockDeviceApplication(slotIndex).getFirmwareVersion() < oldestFirmwareVersion) { oldestSlotIndex = slotIndex; oldestFirmwareVersion = getBlockDeviceApplication(slotIndex).getFirmwareVersion(); } } return oldestSlotIndex; } //uint32_t MyCandidateApplications::getSlotForCandidate() { // mbed::bd_addr_t _storageAddress = MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS; // tr_debug("Get slot for candidate"); // // FlashIAPBlockDevice flashIAPBlockDevice(MBED_ROM_START, MBED_ROM_SIZE); // int ret = flashIAPBlockDevice.init(); // // if(ret == 0){ // for (int i = 0; i < getNbrOfSlots(); i++) { // mbed::bd_addr_t headerAddress = HEADER_ADDR - MBED_ROM_START; // mbed::bd_addr_t applicationAddress = _storageAddress + i * getSlotSize(); // update_client::BlockDeviceApplication activeApplication(flashIAPBlockDevice, headerAddress, applicationAddress); // update_client::UCErrorCode rc = activeApplication.checkApplication(); // // if (update_client::UCErrorCode::UC_ERR_NONE != rc) { // return _storageAddress + i * getSlotSize(); // } // } // } // return -1; //} }