mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Revision:
96:73a85768f235
Parent:
90:cff3317ad4b0
Child:
97:b141b6b8b37f
--- a/source/ConnectorEndpoint.cpp	Thu Apr 20 19:17:47 2017 +0000
+++ b/source/ConnectorEndpoint.cpp	Thu Apr 20 20:11:52 2017 +0000
@@ -32,6 +32,11 @@
 // Device Manager support
 #include "mbed-connector-interface/DeviceManager.h"
 
+// factory storage and configurator support (mbed Cloud R1.2+)
+#ifdef ENABLE_MBED_CLOUD_SUPPORT
+#include "mbed_factory_configurator_client.h"
+#endif
+
 // our endpoint instance
 static Connector::Endpoint *__endpoint = NULL;
 
@@ -195,13 +200,61 @@
 // Connector::Endpoint: create our interface
 void Endpoint::createEndpointInterface() {
 #ifdef ENABLE_MBED_CLOUD_SUPPORT
-	this->createCloudEndpointInterface();
+	if (this->initializeStorage() && this->initializeFactoryFlow())) {
+		this->createCloudEndpointInterface();
+	}
 #else
 	this->createConnectorEndpointInterface();
 #endif
 }
 
 #ifdef ENABLE_MBED_CLOUD_SUPPORT
+// mbedCloudClient: initialize Storage
+bool Endpoint::initializeStorage() {
+	// initialize mbed-trace
+	mbed_trace_init();
+	
+	// Resets storage to an empty state.
+	// Use this function when you want to clear SD card from all the factory-tool generated data and user data.
+	// After this operation device must be injected again by using factory tool or developer certificate.
+#ifdef RESET_STORAGE
+    this->logger()->log("initializeStorage: Resetting storage to an empty state...");
+    mfcc_status_e delete_status = mfcc_storage_delete();
+    if (delete_status != MFCC_STATUS_SUCCESS) {
+        this->logger()->log("initializeStorage: Failed to reset storage to an empty state. status=%d (OK)...", delete_status);
+    }
+#endif
+    mfcc_status_e status = mfcc_init();
+    if(status != MFCC_STATUS_SUCCESS) {
+        this->logger()->log("initializeStorage: ERROR: mfcc_init failed with status=%d...", status);
+        return false;
+    }
+    return true;
+}
+
+// mbedCloudClient:: initialize factory flow
+bool Endpoint::initializeFactoryFlow() {
+#ifdef MBED_CONF_APP_DEVELOPER_MODE
+    this->logger()->log("initializeFactoryFlow: Start developer flow...");
+    status = mfcc_developer_flow();
+    if (status == MFCC_STATUS_KCM_FILE_EXIST_ERROR) {
+        this->logger()->log("initializeFactoryFlow: Developer credentials already exists (OK)...");
+    } else if (status != MFCC_STATUS_SUCCESS) {
+        this->logger()->log("initializeFactoryFlow: ERROR: Failed to load developer credentials");
+        return false;
+    }
+    status = mfcc_verify_device_configured_4mbed_cloud();
+    if (status != MFCC_STATUS_SUCCESS) {
+        this->logger()->log("initializeFactoryFlow: ERROR: Device not configured for mbed Cloud");
+        return false;
+    }
+    return true;
+#else
+	this->logger()->log("initializeFactoryFlow: non-developer factory flow chosen... continuing...");
+	return true;
+#endif
+}
+
 // mbedCloudClient: create our interface
 void Endpoint::createCloudEndpointInterface() {
 	if (this->m_endpoint_interface == NULL) {