nanotool: Add the capability to retrieve/send saved mag calibration data

Change-Id: Ie5ba8db9be561e6a2878b5d44cf58b26639d3b27
Signed-off-by: Armando Visconti <armando.visconti@st.com>
diff --git a/util/nanotool/androidcontexthub.cpp b/util/nanotool/androidcontexthub.cpp
index 03b24a2..8ffecc0 100644
--- a/util/nanotool/androidcontexthub.cpp
+++ b/util/nanotool/androidcontexthub.cpp
@@ -50,6 +50,7 @@
 static const std::vector<std::tuple<const char *, SensorType>> kCalibrationKeys = {
     std::make_tuple("accel",     SensorType::Accel),
     std::make_tuple("gyro",      SensorType::Gyro),
+    std::make_tuple("mag",       SensorType::Magnetometer),
     std::make_tuple("proximity", SensorType::Proximity),
     std::make_tuple("barometer", SensorType::Barometer),
     std::make_tuple("light",     SensorType::AmbientLightSensor),
@@ -77,6 +78,21 @@
     return false;
 }
 
+static bool CopyFloatArray(const char *key,
+        sp<JSONObject> json, std::vector<uint8_t>& bytes) {
+    sp<JSONArray> array;
+    if (json->getArray(key, &array)) {
+        for (size_t i = 0; i < array->size(); i++) {
+            float val = 0;
+            array->getFloat(i, &val);
+            AppendBytes(&val, sizeof(float), bytes);
+        }
+
+        return true;
+    }
+    return false;
+}
+
 static bool GetCalibrationBytes(const char *key, SensorType sensor_type,
         std::vector<uint8_t>& bytes) {
     bool success = true;
@@ -92,6 +108,10 @@
         success = CopyInt32Array(key, json, bytes);
         break;
 
+      case SensorType::Magnetometer:
+        success = CopyFloatArray(key, json, bytes);
+        break;
+
       case SensorType::AmbientLightSensor:
       case SensorType::Barometer: {
         float value = 0;