Difference between revisions of "WISE-Agent Sample Client"

From ESS-WIKI
Jump to: navigation, search
(Created page with "== SAClient APIs == == Client Sample Code == <syntaxhighlight lang="c">agent connected callback function: void on_connect_cb() { SUSIAccessAgentLog(Normal, "CB_Connecte...")
 
Line 1: Line 1:
 +
== Introduction ==
 +
 +
'''WISE Agnet – a software development framework to communicate between device and RMM Server.'''<br/>Advantech provides a software development framework to communicate and exchange information between a device and RMM Server, called a WISE Agent framework.<br/>The WISE Agent framework provides a rich set of user-friendly, intelligent and integrated interfaces, which speeds development, enhances security and makes agent application easier and simpler to communicate with RMM Server.<br/>The framework has three benefits:
 +
 +
*Standardization - The communication protocol is based on the [http://mqtt.org/ MQTT protocol] to communicate and exchange data with WISE-PaaS/RMM Server. The IoT sensor data report format is following the [http://www.ipso-alliance.org/ IPSO Spec.] in [http://www.json.org/ JSON format].
 +
*Portability - Whole framework is written in C language and follow the [https://en.wikipedia.org/wiki/ANSI_C ANSI C Standard], that C compilers are available for most systems and are often the first compiler provided for a new system.
 +
*Scalability - The WISE Agent Framework is functional partitioning into discrete scalable, reusable modules, and plug & playable.
 +
 +
 +
 +
== Architecture ==
 +
 +
=== WISE Agent ===
 +
 +
In '''WISE Agent Framework''', it includes two parts: '''WISE Agent''' and '''Handlers'''.
 +
 +
*In '''Provisioning & Communication''' block,&nbsp;There are defined two structures:
 +
 +
#'''Platform Profiler''' structure describe the target platform, include: OS version, SN, Device Name, MAC address, …
 +
#'''Configuration''' structure describe how to connect to RMM server, such as Server IP, Listen Port, TLS/SSL setting, …
 +
 +
&nbsp;And the '''Agent Client''' is the main library of WISE Agent to communicate with RMM Server, based on MQTT protocol.
 +
 +
 +
 +
*In '''Core Management''' block,
 +
 +
#The '''Core Manager''' integrates and manages the WISE Agent resources and keeps them alive.
 +
#The '''Handler Loader''' is responsible to load and manage the handlers, that indicated in module_config.xml file.
 +
#The '''RMM Command''' is responsible to handle the commands that control the Agent, such as: Agent Rename or Agent Update.
 +
 +
&nbsp;
 +
 +
*The '''Handlers''' part includes the handlers we implement for RMM Agent, such as SUSI Control, Power On/Off. And any other custom handlers that implemented follow our Handler APIs.
 +
 +
[[File:WISEAgentArchitecture.png|760x600px|WISEAgent SW Architecture]]
 +
 
== SAClient APIs ==
 
== SAClient APIs ==
  

Revision as of 05:26, 4 October 2016

Introduction

WISE Agnet – a software development framework to communicate between device and RMM Server.
Advantech provides a software development framework to communicate and exchange information between a device and RMM Server, called a WISE Agent framework.
The WISE Agent framework provides a rich set of user-friendly, intelligent and integrated interfaces, which speeds development, enhances security and makes agent application easier and simpler to communicate with RMM Server.
The framework has three benefits:

  • Standardization - The communication protocol is based on the MQTT protocol to communicate and exchange data with WISE-PaaS/RMM Server. The IoT sensor data report format is following the IPSO Spec. in JSON format.
  • Portability - Whole framework is written in C language and follow the ANSI C Standard, that C compilers are available for most systems and are often the first compiler provided for a new system.
  • Scalability - The WISE Agent Framework is functional partitioning into discrete scalable, reusable modules, and plug & playable.


Architecture

WISE Agent

In WISE Agent Framework, it includes two parts: WISE Agent and Handlers.

  • In Provisioning & Communication block, There are defined two structures:
  1. Platform Profiler structure describe the target platform, include: OS version, SN, Device Name, MAC address, …
  2. Configuration structure describe how to connect to RMM server, such as Server IP, Listen Port, TLS/SSL setting, …

 And the Agent Client is the main library of WISE Agent to communicate with RMM Server, based on MQTT protocol.


  • In Core Management block,
  1. The Core Manager integrates and manages the WISE Agent resources and keeps them alive.
  2. The Handler Loader is responsible to load and manage the handlers, that indicated in module_config.xml file.
  3. The RMM Command is responsible to handle the commands that control the Agent, such as: Agent Rename or Agent Update.

 

  • The Handlers part includes the handlers we implement for RMM Agent, such as SUSI Control, Power On/Off. And any other custom handlers that implemented follow our Handler APIs.

WISEAgent SW Architecture

SAClient APIs

Client Sample Code

/*agent connected callback function*/
void on_connect_cb()
{
	SUSIAccessAgentLog(Normal, "CB_Connected ");
}

/*agent lost connect callback function*/
void on_lost_connect_cb()
{
	SUSIAccessAgentLog(Normal, "CB_Lostconnect ");
}

/*agent disconnect callback function*/
void on_disconnect_cb()
{
	SUSIAccessAgentLog(Normal, "CB_Disconnect ");
}

/*agent received message callback function*/
void on_msgrecv(char* topic, susiaccess_packet_body_t *pkt, void *pRev1, void* pRev2)
{
	/*user can process received command here*/
	SUSIAccessAgentLog(Normal, "Packet received,&nbsp;%s\r\n", pkt->content);
}

int main(int argc, char *argv[])
{
	int iRet = 0;
	char moudlePath[MAX_PATH] = {0};
	
	/*agent configuration structure: define how does the agent connect to Server*/
	susiaccess_agent_conf_body_t config;

	/*agent profile structure: define agent platform information*/
	susiaccess_agent_profile_body_t profile;

	memset(moudlePath, 0 , sizeof(moudlePath));
	util_module_path_get(moudlePath);
		
	// Initialize Log Library
	SUSIAccessAgentLogHandle = InitLog(moudlePath);
	SUSIAccessAgentLog(Normal, "Current path:&nbsp;%s", moudlePath);

	// Pre-set Agent Config struct
	memset(&config, 0 , sizeof(susiaccess_agent_conf_body_t));
	strcpy(config.runMode,"remote"); //runMode default is remote. There are no other mode in WISE Agent version 3.x
	strcpy(config.autoStart,"True"); //autoStart default is True. The Agent will reconnect to server automatically.
	strcpy(config.serverIP,"dev-wisepaas.eastasia.cloudapp.azure.com"); //serverIP indicate the server RUL or IP Address
	strcpy(config.serverPort,"1883"); //serverPort indocate the server (MQTT Broker) listen port, default is 1883 in WISE Agent version 3.1 or later, WISE Agent version 3.0 is 10001.
	strcpy(config.serverAuth,"fENl4B7tnuwpIbs61I5xJQ=="); //serverAuth is the server (MQTT Broker) authentication string. the string is encode from <ID>:<PASS>. It only worked on SSL Mode.
	config.tlstype = tls_type_none; //tlstype define the TLS (SSL) mode
	switch(config.tlstype)
	{
	case tls_type_none: //disable TLS (SSL).
		break;
	case tls_type_tls: //setup TLS with certificate file.
		{
			strcpy(config.cafile, "ca.crt");
			strcpy(config.capath, "");
			strcpy(config.certfile, "server.crt");
			strcpy(config.keyfile, "server.key");
			strcpy(config.cerpasswd, "123456");
		}
		break;
	case tls_type_psk: //setup TLS with pre share key.
		{
			strcpy(config.psk, "");
			strcpy(config.identity, "SAClientSample");
			strcpy(config.ciphers, "");
		}
		break;
	}

	// Pre-set Agent Profile struct
	memset(&profile, 0 , sizeof(susiaccess_agent_profile_body_t));
	snprintf(profile.version, DEF_VERSION_LENGTH, "%d.%d.%d.%d", 3, 1, 0, 0);  //version indicate the version fo the application.
	strcpy(profile.hostname,"SAClientSample"); //hostname indicate the name of target device ro agent.
	strcpy(profile.devId,"000014DAE996BE04"); //devId is the Unique ID of the defice or agent.
	strcpy(profile.sn,"14DAE996BE04"); //sn indicate the device serial number.
	strcpy(profile.mac,"14DAE996BE04"); //mac indicate the MAC Address of first ethernet or wireless card.
	strcpy(profile.type,"IPC"); //type indicate the agent type, defualt is IPC. User can define their own type for customization.
	strcpy(profile.product,"Sample Agent"); //produce indicate the product name
	strcpy(profile.manufacture,"test"); //manufacture indicate the manufacture name
	strcpy(profile.osversion,"NA"); //osversion indicate the OS version of target device
	strcpy(profile.biosversion,"NA"); //biosversion indicate the BIOS version of target device
	strcpy(profile.platformname,"NA"); //platformname indicate the platform (board) name of target device
	strcpy(profile.processorname,"NA"); //processorname indicate the processor name of target device
	strcpy(profile.osarchitect,"NA"); //osarchitect indicate the OS architecture name of target device
	profile.totalmemsize = 40832; //totalmemsize indicate the OS recognized total memory size of target device
	strcpy(profile.maclist,"14DAE996BE04"); //maclist list all the ethernet and wireless card MAC Address.
	strcpy(profile.localip,"172.21.73.151"); //localip indicate the local IP of target device
	strcpy(profile.account,"anonymous"); //account bind the device or anget to the sepcific account, default is anonymous.
	strcpy(profile.passwd,""); //passwd indicate the encrypted password of account.
	strcpy(profile.workdir, moudlePath); //workdir indicate current executable binary file location.


	/*Initialize SAClient with Agent Configure and Profile structure, and the Log File Handle*/
	iRet = saclient_initialize(&config, &profile, SUSIAccessAgentLogHandle);

	if(iRet&nbsp;!= saclient_success)
	{
		SUSIAccessAgentLog(Error, "Unable to initialize AgentCore.");
		goto EXIT;
	}

	SUSIAccessAgentLog(Normal, "Agent Initialized");

	/*register the conect, lost connect and disconnect callback function*/
	saclient_connection_callback_set(on_connect_cb, NULL, on_disconnect_cb);

	SUSIAccessAgentLog(Normal, "Agent Set Callback");
	
	/*start connect to server, server is defined in agent config*/
	iRet = saclient_connect();

	if(iRet&nbsp;!= saclient_success){
		SUSIAccessAgentLog(Error, "sampleagent Unable to connect to broker.");
		goto EXIT;
	} else {
		SUSIAccessAgentLog(Normal, "sampleagent Connect to broker:&nbsp;%s", config.serverIP);
	}
	
	{
		
		char topicStr[128] = {0};
		susiaccess_packet_body_t pkt;

		/* Add  subscribe topic Callback*/
		sprintf(topicStr, "/cagent/admin/%s/testreq", profile.devId);
		saclient_subscribe(topicStr, 0, on_msgrecv);
		
		/*Send test packet to specific topic*/
		strcpy(pkt.devId, profile.devId);
		strcpy(pkt.handlerName, "Test");
		pkt.requestID = 0;
		pkt.cmd = 0;
		pkt.content = (char*)malloc(strlen("{\"Test\":100}")+1);
		memset(pkt.content, 0, strlen("{\"Test\":100}")+1);
		strcpy(pkt.content, "{\"Test\":100}");
		saclient_publish(topicStr, 0, false, &pkt);
		free(pkt.content);
	}

EXIT:
	printf("Click enter to exit");
	fgetc(stdin);

	/*disconnect from server*/
	saclient_disconnect();
	SUSIAccessAgentLog(Normal, "Send Client Info: disconnect");
	/*release SAClient resource*/
	saclient_uninitialize();
    SUSIAccessAgentLog(Normal, "Agent Uninitialize");
	/*release log resource*/
	UninitLog(SUSIAccessAgentLogHandle);

	return iRet;
}