Skip to content

Commit

Permalink
Utilities: refactor and coverity
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Dec 15, 2017
1 parent c8b7f8b commit a82679c
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 161 deletions.
22 changes: 11 additions & 11 deletions Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ enum PX4_CUSTOM_SUB_MODE_AUTO
PX4_CUSTOM_SUB_MODE_AUTO_RTGS
}

public static List<KeyValuePair<int, string>> getModesList(CurrentState cs)
public static List<KeyValuePair<int, string>> getModesList(MainV2.Firmwares firmware)
{
log.Info("getModesList Called");

if (cs.firmware == MainV2.Firmwares.PX4)
if (firmware == MainV2.Firmwares.PX4)
{
/*
union px4_custom_mode {
Expand Down Expand Up @@ -158,10 +158,10 @@ struct {

return temp;
}
else if (cs.firmware == MainV2.Firmwares.ArduPlane)
else if (firmware == MainV2.Firmwares.ArduPlane)
{
var flightModes = Utilities.ParameterMetaDataRepository.GetParameterOptionsInt("FLTMODE1",
cs.firmware.ToString());
firmware.ToString());
flightModes.Add(new KeyValuePair<int, string>(16, "INITIALISING"));

flightModes.Add(new KeyValuePair<int, string>(17, "QStabilize"));
Expand All @@ -172,25 +172,25 @@ struct {

return flightModes;
}
else if (cs.firmware == MainV2.Firmwares.Ateryx)
else if (firmware == MainV2.Firmwares.Ateryx)
{
var flightModes = Utilities.ParameterMetaDataRepository.GetParameterOptionsInt("FLTMODE1",
cs.firmware.ToString()); //same as apm
firmware.ToString()); //same as apm
return flightModes;
}
else if (cs.firmware == MainV2.Firmwares.ArduCopter2)
else if (firmware == MainV2.Firmwares.ArduCopter2)
{
var flightModes = Utilities.ParameterMetaDataRepository.GetParameterOptionsInt("FLTMODE1",
cs.firmware.ToString());
firmware.ToString());
return flightModes;
}
else if (cs.firmware == MainV2.Firmwares.ArduRover)
else if (firmware == MainV2.Firmwares.ArduRover)
{
var flightModes = Utilities.ParameterMetaDataRepository.GetParameterOptionsInt("MODE1",
cs.firmware.ToString());
firmware.ToString());
return flightModes;
}
else if (cs.firmware == MainV2.Firmwares.ArduTracker)
else if (firmware == MainV2.Firmwares.ArduTracker)
{
var temp = new List<KeyValuePair<int, string>>();
temp.Add(new KeyValuePair<int, string>(0, "MANUAL"));
Expand Down
4 changes: 2 additions & 2 deletions CurrentState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ public void UpdateCurrentSettings(System.Windows.Forms.BindingSource bs, bool up

if ((highlatency.base_mode & (byte) MAVLink.MAV_MODE_FLAG.CUSTOM_MODE_ENABLED) != 0)
{
List<KeyValuePair<int, string>> modelist = Common.getModesList(this);
List<KeyValuePair<int, string>> modelist = Common.getModesList(this.firmware);

if (modelist != null)
{
Expand Down Expand Up @@ -1890,7 +1890,7 @@ public void UpdateCurrentSettings(System.Windows.Forms.BindingSource bs, bool up
// prevent running thsi unless we have to
if (_mode != hb.custom_mode)
{
List<KeyValuePair<int, string>> modelist = Common.getModesList(this);
List<KeyValuePair<int, string>> modelist = Common.getModesList(this.firmware);

if (modelist != null)
{
Expand Down
2 changes: 0 additions & 2 deletions ExtLibs/Controls/BackstageView/BackstageView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,6 @@ public void ActivatePage(BackstageViewPage associatedPage)
{
if (associatedPage == null)
{
if (associatedPage.Page == null)
return;
if (_activePage == null)
DrawMenu(null, true);
return;
Expand Down
4 changes: 2 additions & 2 deletions ExtLibs/Controls/MainSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public void Dispose()
{
try
{
Console.WriteLine("MainSwitcher dispose " + item.Name);
if (item != null && item.Control != null)
Console.WriteLine("MainSwitcher dispose " + item?.Name);
if (item?.Control != null)
{
item.Control.Close();
item.Control.Dispose();
Expand Down
4 changes: 2 additions & 2 deletions ExtLibs/Controls/MyUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace System.Windows.Forms
/// </summary>
[ComVisible(true)]
public class MyUserControl : System.Windows.Forms.UserControl
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// implement an on closing event to tidy up enviroment.
/// Using preedefined refrence as can easerly change between form and user control this way.
Expand Down
89 changes: 22 additions & 67 deletions Log/BinaryLog.cs → ExtLibs/Utilities/BinaryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using MissionPlanner.Controls;
using uint8_t = System.Byte;
using MissionPlanner.Utilities;

namespace MissionPlanner.Log
namespace MissionPlanner.Utilities
{
/// <summary>
/// Convert a binary log to an assci log
Expand Down Expand Up @@ -51,60 +49,25 @@ public UnionArray(byte[] bytes)

[FieldOffset(0)]
public short[] Shorts;
}

private IProgressReporterDialogue prd;
}

private string inputfn;
private string outputfn;
private event convertProgress convertstatus;
private string outputfn;

object locker = new object();

private delegate void convertProgress(IProgressReporterDialogue prd, float progress);


Dictionary<string, log_Format> logformat = new Dictionary<string, log_Format>();

public static void ConvertBin(string inputfn, string outputfn, bool showui = true)
{
if (!showui)
{
new BinaryLog().ConvertBini(inputfn, outputfn, false);
return;
}

new BinaryLog().doUI(inputfn, outputfn, true);
}

void doUI(string inputfn, string outputfn, bool showui = true)
{
this.inputfn = inputfn;
this.outputfn = outputfn;

prd = new ProgressReporterDialogue();

prd.DoWork += prd_DoWork;

prd.UpdateProgressAndStatus(-1, Strings.Converting_bin_to_log);
public static event getFlightMode onFlightMode;

this.convertstatus += BinaryLog_convertstatus;
public delegate string getFlightMode(string firmware, int modeno);

ThemeManager.ApplyThemeTo(prd);

prd.RunBackgroundOperationAsync();

prd.Dispose();
}

void BinaryLog_convertstatus(IProgressReporterDialogue prd, float progress)
{
prd.UpdateProgressAndStatus((int) progress, Strings.Converting_bin_to_log);
}

void prd_DoWork(object sender, ProgressWorkerEventArgs e, object passdata = null)
public static void ConvertBin(string inputfn, string outputfn, Action<int> progress = null)
{
this.ConvertBini(inputfn, outputfn, true);
new BinaryLog().ConvertBini(inputfn, outputfn, progress);
}

void ConvertBini(string inputfn, string outputfn, bool showui = true)
void ConvertBini(string inputfn, string outputfn, Action<int> progress = null)
{
using (var stream = File.Open(outputfn, FileMode.Create))
{
Expand All @@ -116,8 +79,8 @@ void ConvertBini(string inputfn, string outputfn, bool showui = true)
{
if (displaytimer.Second != DateTime.Now.Second)
{
if (convertstatus != null && prd != null)
convertstatus(prd, (br.BaseStream.Position/(float) br.BaseStream.Length)*100);
if (progress != null)
progress((int) ((br.BaseStream.Position / (float) br.BaseStream.Length) * 100));

Console.WriteLine("ConvertBin " + (br.BaseStream.Position/(float) br.BaseStream.Length)*100);
displaytimer = DateTime.Now;
Expand All @@ -129,6 +92,8 @@ void ConvertBini(string inputfn, string outputfn, bool showui = true)
}
}

private string _firmware = "";

public string ReadMessage(Stream br)
{
lock (locker)
Expand Down Expand Up @@ -171,25 +136,25 @@ public string ReadMessage(Stream br)
if (line.Contains("PARM, RATE_RLL_P") || line.Contains("ArduCopter") ||
line.Contains("Copter"))
{
MainV2.comPort.MAV.cs.firmware = MainV2.Firmwares.ArduCopter2;
_firmware = "ArduCopter2";
}
else if ((line.Contains("PARM, H_SWASH_PLATE")) || line.Contains("ArduCopter"))
{
MainV2.comPort.MAV.cs.firmware = MainV2.Firmwares.ArduCopter2;
_firmware = "ArduCopter2";
}
else if (line.Contains("PARM, PTCH2SRV_P") || line.Contains("ArduPlane") ||
line.Contains("Plane"))
{
MainV2.comPort.MAV.cs.firmware = MainV2.Firmwares.ArduPlane;
_firmware ="ArduPlane";
}
else if (line.Contains("PARM, SKID_STEER_OUT") || line.Contains("ArduRover") ||
line.Contains("Rover"))
{
MainV2.comPort.MAV.cs.firmware = MainV2.Firmwares.ArduRover;
_firmware = "ArduRover";
}
else if (line.Contains("AntennaTracker") || line.Contains("Tracker"))
{
MainV2.comPort.MAV.cs.firmware = MainV2.Firmwares.ArduTracker;
_firmware = "ArduTracker";
}

return line;
Expand Down Expand Up @@ -815,18 +780,8 @@ string ProcessMessage(byte[] message, string name, string format)
break;
case 'M':
int modeno = message[offset];
var modes = Common.getModesList(MainV2.comPort.MAV.cs);
string currentmode = "";

foreach (var mode in modes)
{
if (mode.Key == modeno)
{
currentmode = mode.Value;
break;
}
}

var mode = onFlightMode?.Invoke(_firmware, modeno);
string currentmode = mode == null ? modeno.ToString() : mode;
line.Append(", " + currentmode);
offset++;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using System.IO;
using System.Linq;
using System.Text;
using MissionPlanner.Utilities;

namespace MissionPlanner.Log
namespace MissionPlanner.Utilities
{
public class CollectionBuffer : IEnumerable<String>, IDisposable
{
Expand Down
7 changes: 3 additions & 4 deletions Log/DFLog.cs → ExtLibs/Utilities/DFLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text;
using log4net;

namespace MissionPlanner.Log
namespace MissionPlanner.Utilities
{
/// <summary>
/// read log and extract log
Expand All @@ -33,7 +33,7 @@ public struct DFItem
public int timems;
public int lineno;

internal DFLog parent;
public DFLog parent;

public string this[string item]
{
Expand Down Expand Up @@ -233,7 +233,6 @@ public List<DFItem> ReadLog(Stream fn)
catch (OutOfMemoryException ex)
{
log.Error(ex);
CustomMessageBox.Show("out of memory");
return answer;
}
catch
Expand Down Expand Up @@ -504,7 +503,7 @@ public static DateTime gpsTimeToTime(int week, double sec)
public int FindMessageOffset(string linetype, string find)
{
if (logformat.ContainsKey(linetype.ToUpper()))
return Log.DFLog.FindInArray(logformat[linetype].FieldNames, find);
return FindInArray(logformat[linetype].FieldNames, find);

return -1;
}
Expand Down
1 change: 1 addition & 0 deletions Log/DashWare.cs → ExtLibs/Utilities/DashWare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MissionPlanner.Utilities;

namespace MissionPlanner.Log
{
Expand Down
Loading

0 comments on commit a82679c

Please sign in to comment.