# Develop your application to communicate with AirVantage

**URL:** <https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498>\
**Category:** Device Management\
**Created:** [April 21, 2016, 7:51am UTC](https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498 "2016-04-21T07:51:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cslee](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cslee](https://forum.sierrawireless.com/u/cslee)\
**Post date:** [April 21, 2016, 7:51am UTC](https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498/1 "2016-04-21T07:51:03Z")

</div>

Hello,  
Trying to go through the demo for “Develop your application to communicate with AirVantage”. [source.sierrawireless.com/airvan … sset-data/](http://source.sierrawireless.com/airvantage/av/howto/hardware/samples/legato-asset-data/) , but encounter a problem.  
The example uses Command-Line but I am using Developer Studio IDE.  
I am trying to find similar document but unable. So, I am creating the project from the beginning.  
I am not sure if the process is correct but please go through the whole process until the point I had encountered problem or maybe you can correct my mistake or suggest a better way of doing this.

I am using mangOH with Legato version 16.01.0.Beta\_aa0…\_modified with firmware SWI9X15Y\_07.08.02.00 r31088 CARMD-EV-FRMWR1; Developer Studio 4.0, build version 4.0.0.201602241605 with Ubuntu 4.2.0-16-generic.

1. File \> New \> Legato Application Project \> Project name: assetDataTutorial \> Legato swi-mdm9x15 Wp8548 359377060011672(WP85XX) \> Next \> Target platform selectio Mode : [Built-in] Legato for WP85XX(16.1.0.wp85-native-x86\_64-…) \> Finish.
2. assetDataTutorial.adef

```auto
sandboxed: false
executables:
{
    assetDataTutorial = ( assetDataTutorialComponent )
}
processes:
{
    envVars:
    {
        LE_LOG_LEVEL = DEBUG
    }
    run:
    {
        (assetDataTutorial)
    }
}
bindings:
{
    assetDataTutorial.assetDataTutorialComponent.le_avdata -> avcService.le_avdata
    assetDataTutorial.assetDataTutorialComponent.le_avc -> avcService.le_avc
}
start: manual
```

1. assetDataTutorial.c

```auto
#include "legato.h"
#include "interfaces.h"

le_avdata_AssetInstanceRef_t _assetInstRef[2]; //reference to the 2 asset instances

char* _roomName[2]; //array of 2 Variable Name
int _currentTemperature[2]; //array of 2 Variable Temperature
bool _isAcOn[2]; //array of 2 Variable IsAC_on
int _targetTemperature[2]; //array of 2 Setting TargetTemperature
bool _isAvcSessionOpened = false;

le_timer_Ref_t _avcTimerRef;
le_clk_Time_t _interval = { 15, 0 }; //AVC start/stop Timer delay is 15 seconds

le_avc_StatusEventHandlerRef_t _avcEventHandlerRef = NULL;//reference to AirVantage Controller (AVC) Session handler

int _endProgramCountDown = 40; ///AVC session, Countdown to quit application

void OnReadVariable(le_avdata_AssetInstanceRef_t instRef, const char *fieldName, void *contextPtr)
{
    int i;
    for (i=0; i<2; i++)
    {
        if (instRef == _assetInstRef[i])
        {
            if (strcmp(fieldName, "Name") == 0)
            {
                LE_INFO("Legato AssetData: Read Variable request: Instance(%d).%s is %s", i, fieldName, _roomName[i]);
                le_avdata_SetString(instRef, fieldName, _roomName[i]);
            }
            else if (strcmp(fieldName, "Temperature") == 0)
            {
                LE_INFO("Legato AssetData: Read Variable request: Instance(%d).%s is %d C", i, fieldName, _currentTemperature[i]);
                le_avdata_SetInt(instRef, fieldName, _currentTemperature[i]);
            }
            else if (strcmp(fieldName, "IsAC_on") == 0)
            {
                LE_INFO("Legato AssetData: Read Variable request: Instance(%d).%s is %d", i, fieldName, _isAcOn[i]);
                le_avdata_SetBool(instRef, fieldName, _isAcOn[i]);
            }
            break;
        }
    }
}

void OnWriteSetting(le_avdata_AssetInstanceRef_t instRef, const char *fieldName, void *contextPtr)
{
    int i;
    for (i=0; i<2; i++)
    {
        if (instRef == _assetInstRef[i])
        {
            if (strcmp(fieldName, "TargetTemperature") == 0)
            {
                LE_INFO("Legato AssetData: Setting Change request: Instance(%d).%s is %d C°", i, fieldName, _targetTemperature[i]);
                int nTemp;
                le_avdata_GetInt(instRef, fieldName, &nTemp); //Get the new setting from AirVantage

                if (nTemp != _targetTemperature[i])
                {
                    _isAcOn[i] = true; //let's set the AC status to ON
                    le_avdata_SetBool(instRef, "IsAC_on", _isAcOn[i]); //reflect the new value to instance

                    _targetTemperature[i] = nTemp;
                    LE_INFO("Legato AssetData: Setting Change request: Instance(%d).%s has been set to %d C°", i, fieldName, _targetTemperature[i]);

                    le_avdata_SetInt(instRef, fieldName, _targetTemperature[i]); //reflect the new value to instance
                }
            }
            break;
        }
    }
}

void OnCommand(le_avdata_AssetInstanceRef_t instRef, const char *fieldName, void *contextPtr)
{
    int i;
    for (i=0; i<2; i++)
    {
        if (instRef == _assetInstRef[i])
        {
            if (strcmp(fieldName, "TurnOffAC") == 0)
            {
                LE_INFO("Legato AssetData: Execute Command Request: Triggered Instance(%d).TurnOffAC", i);
                _isAcOn[i] = false; //Execute the commande, just turn AC status to OFF

                le_avdata_SetBool(instRef, "IsAC_on", _isAcOn[i]); //reflect the new value to instance
            }
            break;
        }
    }
}

static void AVsessionHandler
(
    le_avc_Status_t updateStatus,
    int32_t totalNumBytes,
    int32_t progress,
    void* contextPtr
)
{
    LE_INFO("AVsessionHandler-callback: status %i", updateStatus);
}

static void timerExpiredHandler(le_timer_Ref_t timerRef)
{
    //LE_INFO("timer has expired !, let's close AVC session");
    if (_isAvcSessionOpened)
    {
        LE_INFO("Legato AssetData: Close AVC session");
        le_avc_StopSession();
        _isAvcSessionOpened = false;

        LE_INFO("Legato AssetData: Exit App count down = %d", _endProgramCountDown);

        if (--_endProgramCountDown <= 0)
        {
            le_timer_Stop(timerRef);

            if (NULL != _avcEventHandlerRef)
            {
                //unregister the handler
                le_avc_RemoveStatusEventHandler(_avcEventHandlerRef);
            }

            LE_INFO("Legato AssetData: Legato AssetDataApp Ended");

            //Quit the app
            exit(EXIT_SUCCESS);
        }
    }
    else
    {
        LE_INFO("Legato AssetData: Open AVC session");
        le_result_t result = le_avc_StartSession(); //Start AVC session. Note: AVC handler must be registered prior starting a session
        if (LE_FAULT == result)
        {
            le_avc_StopSession();
            result = le_avc_StartSession();
        }
        if (LE_OK == result)
        {
            _isAvcSessionOpened = true;
        }
    }
}

COMPONENT_INIT
{
	int i = 0;

    LE_INFO("Legato AssetData: Start Legato AssetDataApp");

	//Create 2 asset instances
    LE_INFO("Legato AssetData: Create 2 instances AssetData ");

    _assetInstRef[i] = le_avdata_Create("Room"); //create instance #1 for asset "Room"
    //Assign default value to asset data fields
	_roomName[i] = (char *) malloc(16);
	strcpy(_roomName[i], "bedroom");
	_currentTemperature[i] = 23;
	_isAcOn[i] = false;
	_targetTemperature[i] = 19;

	i++;
    _assetInstRef[i] = le_avdata_Create("Room"); //create instance #2 for asset "Room"
	_roomName[i] = (char *) malloc(16);
	strcpy(_roomName[i], "living-room");
	_currentTemperature[i] = 19;
	_isAcOn[i] = true;
	_targetTemperature[i] = 19;

	//Register handler for Variables, Settings and Commands
	for (i=0; i< 2; i++)
	{
        #if 0 //temporarily disable, not supported for now
	    // call OnReadVariable() handler whenever the variable "Name" is accessed
	    le_avdata_AddFieldEventHandler(_assetInstRef[i], "Name", OnReadVariable, NULL);
	    // call OnReadVariable() handler whenever the variable "Temperature" is accessed
	    le_avdata_AddFieldEventHandler(_assetInstRef[i], "Temperature", OnReadVariable, NULL);
	    // call OnReadVariable() handler whenever the variable "IsAC_on" is accessed
	    le_avdata_AddFieldEventHandler(_assetInstRef[i], "IsAC_on", OnReadVariable, NULL);
        #else
        //set the variable values
        le_avdata_SetString(_assetInstRef[i], "Name", _roomName[i]);
        le_avdata_SetInt(_assetInstRef[i], "Temperature", _currentTemperature[i]);
        le_avdata_SetBool(_assetInstRef[i], "IsAC_on", _isAcOn[i]);
        le_avdata_SetInt(_assetInstRef[i], "TargetTemperature", _targetTemperature[i]);
        #endif

	    // call OnWriteSetting() handler whenever the setting "TargetTemperature" is accessed
	    le_avdata_AddFieldEventHandler(_assetInstRef[i], "TargetTemperature", OnWriteSetting, NULL);
	    // call OnCommand() handler whenever the setting "TurnOffAC" is accessed
	    le_avdata_AddFieldEventHandler(_assetInstRef[i], "TurnOffAC", OnCommand, NULL);
	}

	//Start AVC Session
	_avcEventHandlerRef = le_avc_AddStatusEventHandler(AVsessionHandler, NULL); //register a AVC handler
	le_result_t	result = le_avc_StartSession(); //Start AVC session. Note: AVC handler must be registered prior starting a session
	if (LE_FAULT == result)
	{
		le_avc_StopSession();
		le_avc_StartSession();
	}

    _isAvcSessionOpened = true;

    LE_INFO("Legato AssetData: Started LWM2M session with AirVantage");

	_avcTimerRef = le_timer_Create("assetDataAppSessionTimer"); //create timer
	le_timer_SetInterval(_avcTimerRef, _interval);
	le_timer_SetRepeat(_avcTimerRef, 0); //set repeat to always

	//set callback function to handle timer expiration event
	le_timer_SetHandler(_avcTimerRef, timerExpiredHandler);

	//start timer
	le_timer_Start(_avcTimerRef);

}
```

1. Component.cdef

```auto
requires:
{
    api:
    {
        le_avdata.api
        le_avc.api
    }
}
sources:
{
    assetDataTutorial.c
}
assets:
{
    Room = 
    {
        variables:
        {
            string Name
            int Temperature
            bool IsAC_on
        }

        settings:
        {
            int TargetTemperature
        }

        commands:
        {
            TurnOffAC
        }
    }
}
```

1. Hit the hammer! \> done
2. Run AS Legato Application. Notice that on Legato Console, “…Exit App count down 40”, …39 …38.
3. Stop app
4. I am not sure if this is the correct way to export the file to AirVantage.  
At Project Explorer Workspace, right click on “assetDataTutorial” root directory, select “Export” \> “Develper Studio” \> “Create Legato Application for Airvantage” \> Next \> select file under assetDataTutorial \> Next \> finish
5. Login to AirVantage, click on developer \> My Apps \> Release (the arrow up sign) \> select “assetDataTutorial-xxxxxxx.zip” \> Advanced setting \> Publish.
6. Goto Inventory \> Systems \> click edit (the pen sign) on one of the systems registered (I have only one) \> click on “select application” and select “assetDataTutorial(…)” \> save.
7. Goto Configure \> Reports \> Create a report based on application (the plus sign on the right) \> “Select a application” \> “assetDataTutorial(…)” \> continue.
8. the next screen supposed to show me “Room asset” but it is showing me “assetDataTutorial/All assets UnselectAll Data”. Different from the actual screen show.

Need help to continue from here.

Thanks,  
CSLEE

---

<div class="post-metadata">

**Author:** ![cslee](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cslee](https://forum.sierrawireless.com/u/cslee)\
**Post date:** [April 21, 2016, 8:38am UTC](https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498/2 "2016-04-21T08:38:59Z")

</div>

Bump

---

<div class="post-metadata">

**Author:** ![cslee](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cslee](https://forum.sierrawireless.com/u/cslee)\
**Post date:** [April 26, 2016, 2:12am UTC](https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498/3 "2016-04-26T02:12:12Z")

</div>

Bump

---

<div class="post-metadata">

**Author:** ![bbo](https://sea1.discourse-cdn.com/flex025/user_avatar/forum.sierrawireless.com/bbo/32/3905_2.png) [@bbo](https://forum.sierrawireless.com/u/bbo)\
**Post date:** [April 27, 2016, 11:19am UTC](https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498/4 "2016-04-27T11:19:50Z")

</div>

Hi CSLEE,

Got a similar problem and finally got going… (sort of).  
I’m using Linux Ubuntu 14.04 and the latest dev studio:  
 Developer Studio 4.0  
Build Version 4.0.0.201602241605  
Supported Legato versions: [15.10, 16.1]

In this version, there is a built-in function to export to AirVantage package:  
In Project Explorer, Right click on your project \> Export \> Developer Studio \> Create Legato Application for AirVantage

However, it appears that this function is not working well in the current DS version.

Only way I found to get around this is to use the av-pack application in command line. Still cannot escape CL 🙂  
On my machine, this application is located in folder (need to replace by your username): /home//legato/packages/legato.framework.16.1.2.m1/resources/legato/bin

- Once your application is compiled successfully, open a terminal
- Go to the folder /legato/ds/workspace/assetDataTutorial/Target\_Legato\_Debug/app/assetDataTutorial/
- execute /home//legato/packages/legato.framework.16.1.2.m1/resources/legato/bin/av-pack -f assetDataTutorial.wp85 assetDataTutorial-1.2.20160427004330
- I ended up with a file assetDataTutorial.zip (don’t know why the time stamp disappeared from the name)
- …et voila!

This zip file can be used as an application model in Airvantage

Additional comment:  
The application relies on the network access of your MangOH. You need to initiate the communication using the connection manager first:

- cm data apn “your apn”
- cm data connect &

This procedure probably needs a bit of refinement, but it worked for me

Hope it helps!  
B

---

<div class="post-metadata">

**Author:** ![cslee](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cslee](https://forum.sierrawireless.com/u/cslee)\
**Post date:** [April 29, 2016, 1:28am UTC](https://forum.sierrawireless.com/t/develop-your-application-to-communicate-with-airvantage/8498/5 "2016-04-29T01:28:26Z")

</div>

Hi bbo,  
It works. 😃  
Thanks for showing me.  
Best regards,  
CSLEE
