Alios Things 3.3 adds OTA function in linksdk

need:

The newly released Alios Things 3.3.0 has many routines that are convenient for everyone to develop, and the functions can be developed quickly in the routines. Through the linksdk_demo routine, we can upload the end node data to the platform, and IOT Studio sends and sets the object model attributes of the end node. ota_demo can easily complete the function of ota version management.

When a product is used on-site, the OTA function is a very important maintenance method. After all, no one can guarantee that there will be no problems in writing their own code. Therefore, the combination of linksdk_demo and ota_demo is a common combination.

This article details how to combine the two routines.

Table of contents

1. Observe the difference

1.1 Downstream code differences

1.2 Differences in event handling code

2. Merge code

3. Add macro definitions and references

4. Properly upgrade successfully

1. Observe the difference

1.1 Downstream code differences

In terms of MQTT message processing, linkSDK has created an additional instance called DATA-MODEL to process the received downlink messages. We can hand it over to him for processing, because the downlink message processing of ota does not actually do anything. That is to say, there is no need to merge this demo_mqtt_default_recv_handler) into linksdk_demo, and you can directly use DATA-MODEL to process downlink attribute packets.

OTA’s message processing code

/* Configure MQTT default message receiving callback function */  
aiot_mqtt_setopt(mqtt_handle, AIOT_MQTTOPT_RECV_HANDLER, ( void  *)demo_mqtt_default_recv_handler);

Downlink message processing code of LinkSDK   

 /* Create DATA-MODEL instance */

    dm_handle = aiot_dm_init();

    if (dm_handle == NULL) {

        printf("aiot_dm_init failed");

        return -1;

    }

    /* Configure MQTT instance handle */

    aiot_dm_setopt(dm_handle, AIOT_DMOPT_MQTT_HANDLE, mqtt_handle);

    /* Configure the message receiving and processing callback function */

    aiot_dm_setopt(dm_handle, AIOT_DMOPT_RECV_HANDLER, (void *)demo_dm_recv_handler);

1.2 Differences in event handling code

leftdk routines

/* MQTT event callback function, triggered when network connection/reconnection/disconnection, event definition see core/aiot_mqtt_api.h */

void demo_mqtt_event_handler(void *handle, const aiot_mqtt_event_t *event, void *userdata)

{

    switch (event->type) {

        /* The SDK has successfully established a connection with the mqtt server because the user called the aiot_mqtt_connect() interface */

        case  AIOT_MQTTEVT_CONNECT: {

            printf("AIOT_MQTTEVT_CONNECT\n");

            /*  TODO:  Process SDK connection successfully, you can't call blocking function that takes a long time here*/

            

        }

        break;

The ota routine has one more mqtt_connected_cb(handle);

/* MQTT event callback function, triggered when network connection/reconnection/disconnection, event definition see core/aiot_mqtt_api.h */

void demo_mqtt_event_handler(void *handle, const aiot_mqtt_event_t *event, void *userdata)

{

    switch (event->type) {

        /* The SDK has successfully established a connection with the mqtt server because the user called the aiot_mqtt_connect() interface */

        case  AIOT_MQTTEVT_CONNECT: {

            printf("AIOT_MQTTEVT_CONNECT\n");

            /*  TODO:  Deal with the successful establishment of the SDK connection, you cannot call the blocking function that takes a long time here- */

            mqtt_connected_cb(handle);

        }

        break;

2. Merge code

There are three sub-functions in the OTA routine, copy them to linksdk_demo;

void ota_module_upgrade_start(void *ctx) 
static int ota_upgrade_cb(ota_service_t *pctx, char *ver, char *module_name) 
static void mqtt_connected_cb(void *handle)

When the OTA handles the MQTT connection event, there is an extra line of code

case  AIOT_MQTTEVT_CONNECT: {

            printf("AIOT_MQTTEVT_CONNECT\n");

            /*  TODO:  Deal with the successful connection of the SDK, you cannot call the blocking function that takes a long time here */

            mqtt_connected_cb(handle);

        }

We add to the same location of the linksdk, there are some things involving ota, so that our linksdk_demo has the function of processing ota:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <aos/cli.h>
#include "netmgr.h"
#include "ulog/ulog.h"
#include "ota_agent.h"
#include "core_mqtt.h"
#include "netmgr_wifi.h"
#include "aiot_mqtt_api.h"
#include "aiot_state_api.h"
#include "aiot_sysdep_api.h"
#include <uservice/eventid.h>
#include <uservice/uservice.h>

#define MY_APP_VER     "app-1.0.0"
#define USER_MODULE_VER "m-1.0.0"
#define USER_MODULE_VER2 "m-2.0.0"
#define USER_MODE_NAME  "module1"
#define SUBDEV_FILE_PATH "/data/module1.bin"


static char mqtt_started = 0;
static ota_store_module_info_t g_module_info[3];
static ota_service_t ctx = {0};

3. Add macro definitions and references

Others are some macro definitions and the like.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <aos/cli.h>
#include "netmgr.h"
#include "ulog/ulog.h"
#include "ota_agent.h"
#include "core_mqtt.h"
#include "netmgr_wifi.h"
#include "aiot_mqtt_api.h"
#include "aiot_state_api.h"
#include "aiot_sysdep_api.h"
#include <uservice/eventid.h>
#include <uservice/uservice.h>

#define MY_APP_VER     "app-1.0.0"
#define USER_MODULE_VER "m-1.0.0"
#define USER_MODULE_VER2 "m-2.0.0"
#define USER_MODE_NAME  "module1"
#define SUBDEV_FILE_PATH "/data/module1.bin"


static char mqtt_started = 0;
static ota_store_module_info_t g_module_info[3];
static ota_service_t ctx = {0};

Remember to paste the public key of ota into ota_hal_digest.c.

4. Properly upgrade successfully

The upgrade process can be done according to this document: /HaaSTech/article/details/116380520

This completes the merge, tested it, and the linksdk_demo can be upgraded normally.

Leave a Comment

Your email address will not be published. Required fields are marked *