Mistake on this page? Email us

Creating a single device application for provisioning and connectivity

This tutorial explains how to combine the provisioning reference application (factory-configurator-client-example) with the connectivity reference application (mbed-cloud-client-example) to create a single device application for provisioning and connectivity.

To combine provisioning and connectivity logic in one application, your code must perform a series of operations in sequence:

  1. Call the fcc_init() function to initialize FCC at the beginning of your application code:

    #include "factory_configurator_client.h"
    #include "fcc_status.h"
    int main() {
    
        uint8_t *input_message;
        uint32_t input_message_size;
        uint8_t *response_message;
        size_t response_message_size;
        bool is_fcc_factory_disabled = false;
        FtcdCommBase *ftcd_comm = NULL;
        ...
        // Init basic platform functionality that is needed for the factory (storage is a must)
        ...
        // Init the factory
        fcc_init();
    
  2. Create a communication interface object (ftcd_comm):

    // This is example code from the factory-configurator-client-example reference application
    ftcd_comm = fce_create_comm_interface();
    

    Note: The factory-configurator-client-example reference application demonstrates how to work with the ethernet and serial interfaces. You can replace this application code with the code required by the interface in your factory.

  3. Initiate the ftcd_comm object:

    // This is example code from the factory-configurator-client-example reference application
    ftcd_comm->init();
    
  4. Check whether the factory flow (fcc_factory) is disabled:

    fcc_is_factory_disabled(&is_fcc_factory_disabled);
    
    if(!is_fcc_factory_disabled) {
    

    Note: You disable the factory flow by passing the --factory-fcc-disable flag when you provision information onto the device. This flag finalizes the provisioning process and blocks any attempt to provision the device again in the future. For more information about disabling the factory flow, see the FCU integration documentation or Disabling factory configuration after injection in the factory tool demo documentation.

    • If the factory flow is not disabled, proceed to the factory flow:

      1. Clean the storage:

        fcc_storage_delete();
        
      2. Create a loop to receive messages from FCU and send back the responses, exiting when the session with FCU is finished:

            while (true) {
                // wait for message from communication layer - this is example code from the factory-configurator-client-example reference application
                ftcd_comm->wait_for_message(&input_message, &input_message_size);
                // Process a message that was received from the FCU
                fcc_bundle_handler(input_message, input_message_size, &response_message, &response_message_size);
                // Send back the message to FCU - this is example code from the factory-configurator-client-example reference application
                ftcd_comm->send_response(response_message, response_message_size, ftcd_comm_status);
                // Check whether the session with FCU finished
                if (fcc_is_session_finished()) {
                    break;
                }
            }
        }
        
        fcc_finalize();
        
    • Otherwise, proceed to the connectivity part.

  5. Provide the application's connectivity logic:

    // See the mbed-cloud-client-example reference application for sample code