From 80c1cfa6dba3bb212f36efde57677eccd89d4660 Mon Sep 17 00:00:00 2001 From: jflyper Date: Thu, 17 Jan 2019 02:40:14 +0900 Subject: [PATCH 1/2] Call vtxInit after device parameters are initialized --- src/main/drivers/vtx_common.c | 2 +- src/main/fc/init.c | 1 - src/main/io/vtx.c | 10 +++++++++- src/main/io/vtx_rtc6705.c | 3 +++ src/main/io/vtx_smartaudio.c | 2 ++ src/main/io/vtx_tramp.c | 4 ++++ 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/drivers/vtx_common.c b/src/main/drivers/vtx_common.c index 56c611393b..55381e8459 100644 --- a/src/main/drivers/vtx_common.c +++ b/src/main/drivers/vtx_common.c @@ -168,7 +168,7 @@ bool vtxCommonLookupBandChan(const vtxDevice_t *vtxDevice, uint16_t freq, uint8_ // Returns frequency value (in MHz), or 0 if band/channel out of range. uint16_t vtxCommonLookupFrequency(const vtxDevice_t *vtxDevice, int band, int channel) { - if (band > 0 && band <= vtxDevice->capability.bandCount && + if (vtxDevice && band > 0 && band <= vtxDevice->capability.bandCount && channel > 0 && channel <= vtxDevice->capability.channelCount) { return vtxDevice->frequencyTable[(band - 1) * vtxDevice->capability.channelCount + (channel - 1)]; diff --git a/src/main/fc/init.c b/src/main/fc/init.c index 5b27214fdd..b12adff4be 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -710,7 +710,6 @@ void init(void) #if defined(USE_VTX_COMMON) vtxCommonInit(); - vtxInit(); #endif #ifdef USE_VTX_SMARTAUDIO diff --git a/src/main/io/vtx.c b/src/main/io/vtx.c index 15c8f004e8..24fe44aa79 100644 --- a/src/main/io/vtx.c +++ b/src/main/io/vtx.c @@ -69,8 +69,16 @@ void vtxInit(void) { bool settingsUpdated = false; + vtxDevice_t *vtxDevice = vtxCommonDevice(); + + if (!vtxDevice) { + // If a device is not registered, we don't have any table to refer. + // Don't manipulate settings and just return in this case. + return; + } + // sync frequency in parameter group when band/channel are specified - const uint16_t freq = vtxCommonLookupFrequency(vtxCommonDevice(), vtxSettingsConfig()->band, vtxSettingsConfig()->channel); + const uint16_t freq = vtxCommonLookupFrequency(vtxDevice, vtxSettingsConfig()->band, vtxSettingsConfig()->channel); if (vtxSettingsConfig()->band && freq != vtxSettingsConfig()->freq) { vtxSettingsConfigMutable()->freq = freq; settingsUpdated = true; diff --git a/src/main/io/vtx_rtc6705.c b/src/main/io/vtx_rtc6705.c index cb2a372643..2ed249692a 100644 --- a/src/main/io/vtx_rtc6705.c +++ b/src/main/io/vtx_rtc6705.c @@ -36,6 +36,7 @@ #include "drivers/time.h" #include "drivers/vtx_rtc6705.h" +#include "io/vtx.h" #include "io/vtx_rtc6705.h" #include "io/vtx_string.h" @@ -69,6 +70,8 @@ bool vtxRTC6705Init(void) vtxCommonSetDevice(&vtxRTC6705); + vtxInit(); + return true; } diff --git a/src/main/io/vtx_smartaudio.c b/src/main/io/vtx_smartaudio.c index d0b5f797c6..fa810b79c8 100644 --- a/src/main/io/vtx_smartaudio.c +++ b/src/main/io/vtx_smartaudio.c @@ -706,6 +706,8 @@ bool vtxSmartAudioInit(void) vtxCommonSetDevice(&vtxSmartAudio); + vtxInit(); + return true; } diff --git a/src/main/io/vtx_tramp.c b/src/main/io/vtx_tramp.c index 5dabda3c20..ec6bcab19f 100644 --- a/src/main/io/vtx_tramp.c +++ b/src/main/io/vtx_tramp.c @@ -614,6 +614,7 @@ bool vtxTrampInit(void) return false; } + // XXX Effect of USE_VTX_COMMON should be reviewed, as following call to vtxInit will do nothing if vtxCommonSetDevice is not called. #if defined(USE_VTX_COMMON) vtxTramp.capability.bandCount = VTX_TRAMP_BAND_COUNT; vtxTramp.capability.channelCount = VTX_TRAMP_CHANNEL_COUNT; @@ -626,8 +627,11 @@ bool vtxTrampInit(void) vtxTramp.powerValues = trampPowerTable; vtxCommonSetDevice(&vtxTramp); + #endif + vtxInit(); + return true; } From 53ab1ad23ed6274626a0060f26567c5aa89ff476 Mon Sep 17 00:00:00 2001 From: jflyper Date: Thu, 17 Jan 2019 13:52:51 +0900 Subject: [PATCH 2/2] Protect vtxCommonLookupXXX against null vtxDevice call --- src/main/drivers/vtx_common.c | 58 +++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/src/main/drivers/vtx_common.c b/src/main/drivers/vtx_common.c index 55381e8459..422e090a90 100644 --- a/src/main/drivers/vtx_common.c +++ b/src/main/drivers/vtx_common.c @@ -128,30 +128,44 @@ bool vtxCommonGetDeviceCapability(const vtxDevice_t *vtxDevice, vtxDeviceCapabil const char *vtxCommonLookupBandName(const vtxDevice_t *vtxDevice, int band) { - return vtxDevice->bandNames[band]; + if (vtxDevice) { + return vtxDevice->bandNames[band]; + } else { + return "?"; + } } char vtxCommonLookupBandLetter(const vtxDevice_t *vtxDevice, int band) { - return vtxDevice->bandLetters[band]; + if (vtxDevice) { + return vtxDevice->bandLetters[band]; + } else { + return '?'; + } } const char *vtxCommonLookupChannelName(const vtxDevice_t *vtxDevice, int channel) { - return vtxDevice->channelNames[channel]; + if (vtxDevice) { + return vtxDevice->channelNames[channel]; + } else { + return "?"; + } } //Converts frequency (in MHz) to band and channel values. bool vtxCommonLookupBandChan(const vtxDevice_t *vtxDevice, uint16_t freq, uint8_t *pBand, uint8_t *pChannel) { - // Use reverse lookup order so that 5880Mhz - // get Raceband 7 instead of Fatshark 8. - for (int band = vtxDevice->capability.bandCount - 1 ; band >= 0 ; band--) { - for (int channel = 0 ; channel < vtxDevice->capability.channelCount ; channel++) { - if (vtxDevice->frequencyTable[band * vtxDevice->capability.channelCount + channel] == freq) { - *pBand = band + 1; - *pChannel = channel + 1; - return true; + if (vtxDevice) { + // Use reverse lookup order so that 5880Mhz + // get Raceband 7 instead of Fatshark 8. + for (int band = vtxDevice->capability.bandCount - 1 ; band >= 0 ; band--) { + for (int channel = 0 ; channel < vtxDevice->capability.channelCount ; channel++) { + if (vtxDevice->frequencyTable[band * vtxDevice->capability.channelCount + channel] == freq) { + *pBand = band + 1; + *pChannel = channel + 1; + return true; + } } } } @@ -168,21 +182,31 @@ bool vtxCommonLookupBandChan(const vtxDevice_t *vtxDevice, uint16_t freq, uint8_ // Returns frequency value (in MHz), or 0 if band/channel out of range. uint16_t vtxCommonLookupFrequency(const vtxDevice_t *vtxDevice, int band, int channel) { - if (vtxDevice && band > 0 && band <= vtxDevice->capability.bandCount && - channel > 0 && channel <= vtxDevice->capability.channelCount) { - return vtxDevice->frequencyTable[(band - 1) * vtxDevice->capability.channelCount + (channel - 1)]; - + if (vtxDevice) { + if (band > 0 && band <= vtxDevice->capability.bandCount && + channel > 0 && channel <= vtxDevice->capability.channelCount) { + return vtxDevice->frequencyTable[(band - 1) * vtxDevice->capability.channelCount + (channel - 1)]; + } } + return 0; } const char *vtxCommonLookupPowerName(const vtxDevice_t *vtxDevice, int index) { - return vtxDevice->powerNames[index]; + if (vtxDevice) { + return vtxDevice->powerNames[index]; + } else { + return "?"; + } } uint16_t vtxCommonLookupPowerValue(const vtxDevice_t *vtxDevice, int index) { - return vtxDevice->powerValues[index]; + if (vtxDevice) { + return vtxDevice->powerValues[index]; + } else { + return 0; + } } #endif