#include "FirmwareUpdateManager.h" #include "lmdfu.h" #include "lmdfuwrap.h" #include "DFUMode.h" namespace Tango { namespace FirmwareUpdateLib { void FirmwareUpdateManager::Initialize() { tLMDFUErr eRetcode = _LMDFUInit(); if (eRetcode != DFU_OK) { switch (eRetcode) { case DFU_ERR_NOT_FOUND: throw gcnew InvalidOperationException(DFU_ERR_NOT_FOUND_ERROR); break; case DFU_ERR_INVALID_ADDR: throw gcnew InvalidOperationException(DFU_ERR_INVALID_ADDR_ERROR); break; default: throw gcnew InvalidOperationException(DFU_ERR_UNKNOWN_ERROR); break; } } } List^ FirmwareUpdateManager::GetAvailableDevices(bool fetchTwineExtras) { int iDevIndex = 0; tLMDFUDeviceInfo devInfo; tLMDFUHandle hHandle; tLMDFUErr eRetcode; unsigned short usStringLen; char pcStringBuf[256]; bool g_bReset = false; List^ devices = gcnew List(); do { eRetcode = _LMDFUDeviceOpen(iDevIndex, &devInfo, &hHandle); if (eRetcode == DFU_OK) { tLMDFUDeviceInfo *psDevInfo = &devInfo; DFUDevice^ device = gcnew DFUDevice(psDevInfo, hHandle); device->VID = String::Format("0x{0}", psDevInfo->usVID); device->PID = String::Format("0x{0}", psDevInfo->usPID); //Device Name. usStringLen = sizeof(pcStringBuf); eRetcode = _LMDFUDeviceASCIIStringGet(hHandle, psDevInfo->ucProductString, pcStringBuf, &usStringLen); device->DeviceName = eRetcode == DFU_OK ? gcnew String(pcStringBuf) : "Unknown"; //Manufacturer. usStringLen = sizeof(pcStringBuf); eRetcode = _LMDFUDeviceASCIIStringGet(hHandle, psDevInfo->ucManufacturerString, pcStringBuf, &usStringLen); device->Manufacturer = eRetcode == DFU_OK ? gcnew String(pcStringBuf) : "Unknown"; //DFU Interface. usStringLen = sizeof(pcStringBuf); eRetcode = _LMDFUDeviceASCIIStringGet(hHandle, psDevInfo->ucDFUInterfaceString, pcStringBuf, &usStringLen); device->DFUInterface = eRetcode == DFU_OK ? gcnew String(pcStringBuf) : "Unknown"; //Serial Number. usStringLen = sizeof(pcStringBuf); eRetcode = _LMDFUDeviceASCIIStringGet(hHandle, psDevInfo->ucSerialString, pcStringBuf, &usStringLen); device->SerialNumber = eRetcode == DFU_OK ? gcnew String(pcStringBuf) : "Unknown"; //Max bytes transfer. device->MaxTransfer = psDevInfo->usTransferSize; //Current device mode. device->Mode = psDevInfo->bDFUMode ? DFUMode::DFU : DFUMode::RunTime; //Supports Tiva extensions. (Good for updates) device->SupportsTivaExtensions = psDevInfo->bSupportsTivaExtensions; //Attributes. device->WillDetach = psDevInfo->ucDFUAttributes & DFU_ATTR_WILL_DETACH; device->ManifestTolerant = psDevInfo->ucDFUAttributes & DFU_ATTR_MANIFEST_TOLERANT; device->UploadCable = psDevInfo->ucDFUAttributes & DFU_ATTR_CAN_UPLOAD; device->DownloadCable = psDevInfo->ucDFUAttributes & DFU_ATTR_CAN_DOWNLOAD; //Read device flash parameters. tLMDFUParams sDFU; if (device->SupportsTivaExtensions) { eRetcode = _LMDFUParamsGet(hHandle, &sDFU); if (eRetcode == DFU_OK) { device->AppStartAddress = sDFU.ulAppStartAddr; device->FlashTopAddress = sDFU.ulFlashTop; device->FlashBlockSize = sDFU.usFlashBlockSize; device->NumFlashBlocks = sDFU.usNumFlashBlocks; device->ImageLength = device->FlashTopAddress - device->AppStartAddress; device->FlashSize = device->FlashBlockSize * device->NumFlashBlocks; if (fetchTwineExtras) { device->FetchTwineExtras(); } } } devices->Add(device); } iDevIndex++; } while (eRetcode == DFU_OK); return devices; } } }