From 74ce084c30d9a84bf66db084e25532ec34d5f517 Mon Sep 17 00:00:00 2001
From: Don Gagne <dongagne@outlook.com>
Date: Fri, 26 Apr 2019 13:55:04 -0700
Subject: [PATCH 1/4] Check for CubeBlack service bulletin

---
 src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc | 34 ++++++++++++++++++++++++++
 src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h  |  3 +++
 src/comm/SerialLink.h                          |  3 +++
 3 files changed, 40 insertions(+)

diff --git a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
index 2254331..476d0eb 100644
--- a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
+++ b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
@@ -29,6 +29,9 @@
 #include "APMSubFrameComponent.h"
 #include "ESP8266Component.h"
 #include "APMHeliComponent.h"
+#include "QGCApplication.h"
+
+#include <QSerialPortInfo>
 
 /// This is the AutoPilotPlugin implementatin for the MAV_AUTOPILOT_ARDUPILOT type.
 APMAutoPilotPlugin::APMAutoPilotPlugin(Vehicle* vehicle, QObject* parent)
@@ -50,6 +53,8 @@ APMAutoPilotPlugin::APMAutoPilotPlugin(Vehicle* vehicle, QObject* parent)
     , _heliComponent            (NULL)
 {
     APMAirframeLoader::loadAirframeFactMetaData();
+
+    connect(vehicle->parameterManager(), &ParameterManager::parametersReadyChanged, this, &APMAutoPilotPlugin::_checkForBadCubeBlack);
 }
 
 APMAutoPilotPlugin::~APMAutoPilotPlugin()
@@ -170,3 +175,32 @@ QString APMAutoPilotPlugin::prerequisiteSetup(VehicleComponent* component) const
 
     return QString();
 }
+
+/// The following code is executed when the Vehicle is parameter ready. It checks for the service bulletin against Cube Blacks.
+void APMAutoPilotPlugin::_checkForBadCubeBlack(void)
+{
+    bool cubeBlackFound = false;
+    for (const QVariant& varLink: _vehicle->links()) {
+        SerialLink* serialLink = varLink.value<SerialLink*>();
+        if (serialLink && QSerialPortInfo(*serialLink->_hackAccessToPort()).description().contains(QStringLiteral("CubeBlack"))) {
+            cubeBlackFound = true;
+        }
+
+    }
+    if (!cubeBlackFound) {
+        return;
+    }
+
+    ParameterManager* paramMgr = _vehicle->parameterManager();
+
+    QString paramAcc3("INS_ACC3_ID");
+    QString paramGyr3("INS_GYR3_ID");
+    QString paramEnableMask("INS_ENABLE_MASK");
+
+    if (paramMgr->parameterExists(-1, paramAcc3) && paramMgr->getParameter(-1, paramAcc3)->rawValue().toInt() == 0 &&
+            paramMgr->parameterExists(-1, paramGyr3) && paramMgr->getParameter(-1, paramGyr3)->rawValue().toInt() == 0 &&
+            paramMgr->parameterExists(-1, paramEnableMask) && paramMgr->getParameter(-1, paramEnableMask)->rawValue().toInt() >= 7) {
+        qgcApp()->showMessage(tr("WARNING: The flight board you are using has a critical service bulletin against ti which advise against flying. https://discuss.cubepilot.org/t/sb-0000002-critical-service-bulletin-for-cubes-purchased-between-january-2019-to-present-do-not-fly/406"));
+
+    }
+}
diff --git a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h
index 09bb7c2..83785af 100644
--- a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h
+++ b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h
@@ -59,6 +59,9 @@ protected:
     ESP8266Component*           _esp8266Component;
     APMHeliComponent*           _heliComponent;
 
+private slots:
+    void _checkForBadCubeBlack(void);
+
 private:
     QVariantList                _components;
 };
diff --git a/src/comm/SerialLink.h b/src/comm/SerialLink.h
index 3d02197..5dd0074 100644
--- a/src/comm/SerialLink.h
+++ b/src/comm/SerialLink.h
@@ -145,6 +145,9 @@ public:
     bool    connect(void);
     bool    disconnect(void);
 
+    /// Don't even think of calling this method!
+    QSerialPort* _hackAccessToPort(void) { return _port; }
+
 private slots:
     /**
      * @brief Write a number of bytes to the interface.

From 0ed0d0c4f90e1b1b5a6e009c7966c87159d4192e Mon Sep 17 00:00:00 2001
From: Don Gagne <dongagne@outlook.com>
Date: Fri, 26 Apr 2019 13:56:05 -0700
Subject: [PATCH 2/4] Update

---
 ChangeLog.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/ChangeLog.md b/ChangeLog.md
index 156bbfb..7834477 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -9,6 +9,7 @@ Note: This file only contains high level features or important fixes.
 * Major rewrite and bug fix pass through Structure Scan. Previous version had such bad problems that it can no longer be supported. Plans with Structure Scan will need to be recreated. New QGC will not load old Structure Scan plans.
 
 ### 3.5.1 - Not yet released
+* Add ArduPilot CubeBlack Service Bulletin check
 * Fix visibility of PX4/ArduPilot logo in toolbar
 * Fix tile set count but in OfflineMaps which would cause image and elevation tile set to have incorrect counts and be incorrectly marked as download incomplete.
 

From 1a963e4d04cd1cbd247b1bfe3457bfa7658c765b Mon Sep 17 00:00:00 2001
From: Don Gagne <dongagne@outlook.com>
Date: Fri, 26 Apr 2019 14:37:11 -0700
Subject: [PATCH 3/4] Fix no serial and android builds

---
 src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc | 4 ++++
 src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h  | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
index 476d0eb..dcd78df 100644
--- a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
+++ b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
@@ -54,7 +54,9 @@ APMAutoPilotPlugin::APMAutoPilotPlugin(Vehicle* vehicle, QObject* parent)
 {
     APMAirframeLoader::loadAirframeFactMetaData();
 
+#if !defined(NO_SERIAL_LINK) && !defined(__android__)
     connect(vehicle->parameterManager(), &ParameterManager::parametersReadyChanged, this, &APMAutoPilotPlugin::_checkForBadCubeBlack);
+#endif
 }
 
 APMAutoPilotPlugin::~APMAutoPilotPlugin()
@@ -176,6 +178,7 @@ QString APMAutoPilotPlugin::prerequisiteSetup(VehicleComponent* component) const
     return QString();
 }
 
+#if !defined(NO_SERIAL_LINK) && !defined(__android__)
 /// The following code is executed when the Vehicle is parameter ready. It checks for the service bulletin against Cube Blacks.
 void APMAutoPilotPlugin::_checkForBadCubeBlack(void)
 {
@@ -204,3 +207,4 @@ void APMAutoPilotPlugin::_checkForBadCubeBlack(void)
 
     }
 }
+#endif
diff --git a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h
index 83785af..5c6a5ac 100644
--- a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h
+++ b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.h
@@ -59,8 +59,10 @@ protected:
     ESP8266Component*           _esp8266Component;
     APMHeliComponent*           _heliComponent;
 
+#if !defined(NO_SERIAL_LINK) && !defined(__android__)
 private slots:
     void _checkForBadCubeBlack(void);
+#endif
 
 private:
     QVariantList                _components;

From 93b26d6cc123d1e9dd3fcd1f7a71f1f37ca89137 Mon Sep 17 00:00:00 2001
From: Don Gagne <dongagne@outlook.com>
Date: Fri, 26 Apr 2019 14:49:27 -0700
Subject: [PATCH 4/4] More work on no serial port ifdef

---
 src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
index dcd78df..abc156e 100644
--- a/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
+++ b/src/AutoPilotPlugins/APM/APMAutoPilotPlugin.cc
@@ -31,7 +31,9 @@
 #include "APMHeliComponent.h"
 #include "QGCApplication.h"
 
+#if !defined(NO_SERIAL_LINK) && !defined(__android__)
 #include <QSerialPortInfo>
+#endif
 
 /// This is the AutoPilotPlugin implementatin for the MAV_AUTOPILOT_ARDUPILOT type.
 APMAutoPilotPlugin::APMAutoPilotPlugin(Vehicle* vehicle, QObject* parent)