Difference between revisions of "EI-Agent Sample Client"

From ESS-WIKI
Jump to: navigation, search
(rollback)
Line 1: Line 1:
 
Advantech provides a WISE-PaaS 2.0 EI-Agent Sample to demonstrate how to implement a custom application to connect to WISE-PaaS 2.0 Server.
 
Advantech provides a WISE-PaaS 2.0 EI-Agent Sample to demonstrate how to implement a custom application to connect to WISE-PaaS 2.0 Server.
  
<h2> WISECore APIs </h2>
+
== WISECore APIs ==
<h3> List </h3>
+
 
<p><pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;bool core_initialize(char* strTenantID, char* strClientID, char* strHostName, char* strMAC, void* userdata);&lt;/syntaxhighlight&gt;</pre><i>Description</i>
+
=== List ===
</p>
+
 
<ul><li>General initialization of the WISECore API. Prior to calling any WISECore API functions,&#160;the library needs to be initialized by calling this function.
+
 
</li>
+
<syntaxhighlight lang="c">bool core_initialize(char* strTenantID, char* strClientID, char* strHostName, char* strMAC, void* userdata);</syntaxhighlight>''Description''
<li>The result for all&#160;WISECore API function will be false unless this function is called.
+
*General initialization of the WISECore API. Prior to calling any WISECore API functions,&nbsp;the library needs to be initialized by calling this function.
</li></ul><p><i>Parameters</i>
+
*The result for all&nbsp;WISECore API function will be false unless this function is called.
</p>
+
 
<ul><li>strTenantID[POINTER]
+
''Parameters''
<ul><li>the tenant ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
+
 
</li></ul></li>
+
*strTenantID[POINTER]
<li>strClientID[POINTER]
+
**the tenant ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
<ul><li>the client ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
+
*strClientID[POINTER]
</li></ul></li>
+
**the client ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
<li>strHostName[POINTER]
+
*strHostName[POINTER]
<ul><li>the host name string as device recognize&#160;name.
+
**the host name string as device recognize&nbsp;name.
</li></ul></li>
+
*strMAC[POINTER]
<li>strMAC[POINTER]
+
**first MAC address of the device.
<ul><li>first MAC address of the device.
+
*userdata[POINTER]
</li></ul></li>
+
**the unstructured pointer to reference to user data.
<li>userdata[POINTER]
+
 
<ul><li>the unstructured pointer to reference to user data.
+
''Return Values''
</li></ul></li></ul><p><i>Return Values</i>
+
 
</p>
+
*true/false
<ul><li>true/false
+
 
</li></ul><p><i>Example</i>
+
''Example''
</p><p>Reference to&#160;core_account_bind().
+
 
</p>
+
Reference to&nbsp;core_account_bind().
<hr />
+
 
<p>
+
----
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;void core_uninitialize();&lt;/syntaxhighlight&gt;</pre> <i>Description</i>
+
 
</p>
+
 
<ul><li>General function to uninitialize the WISECore API library that should be called before program exit.
+
<syntaxhighlight lang="c">void core_uninitialize();</syntaxhighlight> ''Description''
</li></ul><p><i>Parameters</i>
+
*General function to uninitialize the WISECore API library that should be called before program exit.
</p>
+
 
<ul><li>None
+
''Parameters''
</li></ul><p><i>Return Values</i>
+
 
</p>
+
*None
<ul><li>None
+
 
</li></ul><p><i>Example</i>
+
''Return Values''
</p><p>Reference to core_account_bind().
+
 
</p>
+
*None
<hr />
+
 
<p>
+
''Example''
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;bool core_product_info_set(char* strSerialNum, char* strParentID, char* strVersion, char* strType, char* strProduct, char* strManufacture);&lt;/syntaxhighlight&gt;</pre> <i>Description</i>
+
 
</p>
+
Reference to core_account_bind().
<ul><li>Assign the basic information to identify the device.
+
 
</li></ul><p><i>Parameters</i>
+
----
</p>
+
 
<ul><li>strSerialNum[POINTER]
+
 
<ul><li>the serial number string of target device.
+
<syntaxhighlight lang="c">bool core_product_info_set(char* strSerialNum, char* strParentID, char* strVersion, char* strType, char* strProduct, char* strManufacture);</syntaxhighlight> ''Description''
</li></ul></li>
+
*Assign the basic information to identify the device.
<li>strParentID[POINTER]
+
 
<ul><li>the parent device ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, used to point out the EIS or Gateway device ID&#160;if the taget device connect to server through an EIS or Gateway device.
+
''Parameters''
</li></ul></li>
+
 
<li>strVersion[POINTER]
+
*strSerialNum[POINTER]
<ul><li>the applicatin version string.
+
**the serial number string of target device.
</li></ul></li>
+
*strParentID[POINTER]
<li>strType[POINTER]
+
**the parent device ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, used to point out the EIS or Gateway device ID&nbsp;if the taget device connect to server through an EIS or Gateway device.
<ul><li>the applicatin type string, default is "IPC" for general usage.
+
*strVersion[POINTER]
</li></ul></li>
+
**the applicatin version string.
<li>strProduct[POINTER]
+
*strType[POINTER]
<ul><li>the target device product name.
+
**the applicatin type string, default is "IPC" for general usage.
</li></ul></li>
+
*strProduct[POINTER]
<li>strManufacture[POINTER]
+
**the target device product name.
<ul><li>the target device manufacture name.
+
*strManufacture[POINTER]
</li></ul></li></ul><p><i>Return Values</i>
+
**the target device manufacture name.
</p>
+
 
<ul><li>true/false
+
''Return Values''
</li></ul><p><i>Example</i>
+
 
</p><p>Reference to&#160;core_account_bind().
+
*true/false
</p>
+
 
<hr />
+
''Example''
<p>
+
 
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;bool core_tag_set(char* strTag);&lt;/syntaxhighlight&gt;</pre> <i>Description</i>
+
Reference to&nbsp;core_account_bind().
</p>
+
 
<ul><li>Assign the tag to markup the support product, for example: "RMM" for RMM product.
+
----
</li></ul><p><i>Parameters</i>
+
 
</p>
+
 
<ul><li>strManufacture[POINTER]
+
<syntaxhighlight lang="c">bool core_tag_set(char* strTag);</syntaxhighlight> ''Description''
<ul><li>the target device supported function set.
+
*Assign the tag to markup the support product, for example: "RMM" for RMM product.
</li></ul></li></ul><p><i>Return Values</i>
+
 
</p>
+
''Parameters''
<ul><li>true/false
+
 
</li></ul><p><i>Example</i>
+
*strManufacture[POINTER]
</p><p>Reference to core_account_bind().
+
**the target device supported function set.
</p>
+
 
<hr />
+
''Return Values''
<p>
+
 
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;bool core_account_bind(char* strLoginID, char* strLoginPW);&lt;/syntaxhighlight&gt;</pre> <i>Description</i>
+
*true/false
</p>
+
 
<ul><li>Bind the device to a specific user account automatically.
+
''Example''
</li></ul><p><i>Parameters</i>
+
 
</p>
+
Reference to core_account_bind().
<ul><li>strLoginID[POINTER]
+
 
<ul><li>Account ID.
+
----
</li></ul></li>
+
 
<li>strLoginPW[POINTER]
+
 
<ul><li>Account Password, need to encrypt with AgentEncrypt application.
+
<syntaxhighlight lang="c">bool core_account_bind(char* strLoginID, char* strLoginPW);</syntaxhighlight> ''Description''
</li></ul></li></ul><p><i>Return Values</i>
+
*Bind the device to a specific user account automatically.
</p>
+
 
<ul><li>true/false.
+
''Parameters''
</li></ul><p><i>Example</i>
+
 
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;#include &amp;quot;WISECore.h&amp;quot;
+
*strLoginID[POINTER]
&#160;
+
**Account ID.
 +
*strLoginPW[POINTER]
 +
**Account Password, need to encrypt with AgentEncrypt application.
 +
 
 +
''Return Values''
 +
 
 +
*true/false.
 +
 
 +
''Example''
 +
<syntaxhighlight lang="c">#include "WISECore.h"
 +
 
 
int main (int argc, char *argv[])
 
int main (int argc, char *argv[])
 
{
 
{
 
int iRet = 0;
 
int iRet = 0;
 
/*Initialize WISECore*/
 
/*Initialize WISECore*/
if(!core_initialize(&amp;quot;general&amp;quot;, &amp;quot;00000001-0000-0000-0000-305A3A77B1CC&amp;quot;, &amp;quot;TestClient&amp;quot;, &amp;quot;305A3A77B1CC&amp;quot;, NULL))
+
if(!core_initialize("general", "00000001-0000-0000-0000-305A3A77B1CC", "TestClient", "305A3A77B1CC", NULL))
 
     {
 
     {
         printf(&amp;quot;Unable to initialize WISECore.\n&amp;quot;);
+
         printf("Unable to initialize WISECore.\n");
 
         goto EXIT;
 
         goto EXIT;
 
     }
 
     }
     printf(&amp;quot;Agent Initialized\n&amp;quot;);
+
     printf("Agent Initialized\n");
 
     /*Setup device identify information*/
 
     /*Setup device identify information*/
     core_product_info_set(&amp;quot;305A3A77B1CC&amp;quot;, NULL/*no parent ID*/, &amp;quot;2.0.1&amp;quot;, &amp;quot;IPC&amp;quot;, &amp;quot;test&amp;quot;, &amp;quot;test&amp;quot;);  
+
     core_product_info_set("305A3A77B1CC", NULL/*no parent ID*/, "2.0.1", "IPC", "test", "test");  
 
     /*Setup support function tag*/
 
     /*Setup support function tag*/
     core_tag_set(&amp;quot;RMM&amp;quot;);
+
     core_tag_set("RMM");
 
     /*binding to a specific account*/
 
     /*binding to a specific account*/
     core_account_bind(&amp;quot;admin&amp;quot;,&amp;quot;GP25qY7TjJA=&amp;quot;);
+
     core_account_bind("admin","GP25qY7TjJA=");
 
EXIT:
 
EXIT:
 
     /*Release WISECore*/
 
     /*Release WISECore*/
Line 128: Line 138:
 
return iRet;
 
return iRet;
 
}
 
}
&lt;/syntaxhighlight&gt;</pre>
+
</syntaxhighlight>
</p>
+
----
<hr />
+
 
<p>
+
 
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;bool core_tls_set(const char *cafile, const char *capath, const char *certfile, const char *keyfile, const char *password);&lt;/syntaxhighlight&gt;</pre> <i>Description</i>
+
<syntaxhighlight lang="c">bool core_tls_set(const char *cafile, const char *capath, const char *certfile, const char *keyfile, const char *password);</syntaxhighlight> ''Description''
</p>
+
*Configure the client for certificate based SSL/TLS support. Must be called&nbsp;before &lt;core_connect&gt;.
<ul><li>Configure the client for certificate based SSL/TLS support. Must be called&#160;before &lt;core_connect&gt;.
+
*Cannot be used in conjunction with &lt;core_tls_psk_set&gt;.
</li>
+
*Define the Certificate Authority certificates to be trusted (ie. the server&nbsp;certificate must be signed with one of these certificates) using cafile.&nbsp;If the server you are connecting to requires clients to provide a&nbsp;certificate, define certfile and keyfile with your client certificate and&nbsp;private key. If your private key is encrypted, provide a password callback&nbsp;function.
<li>Cannot be used in conjunction with &lt;core_tls_psk_set&gt;.
+
 
</li>
+
''Parameters''
<li>Define the Certificate Authority certificates to be trusted (ie. the server&#160;certificate must be signed with one of these certificates) using cafile.&#160;If the server you are connecting to requires clients to provide a&#160;certificate, define certfile and keyfile with your client certificate and&#160;private key. If your private key is encrypted, provide a password callback&#160;function.
+
 
</li></ul><p><i>Parameters</i>
+
*cafile[POINTER]
</p>
+
**path to a file containing the PEM encoded trusted CA certificate files.
<ul><li>cafile[POINTER]
+
**Either cafile or capath must not be NULL.
<ul><li>path to a file containing the PEM encoded trusted CA certificate files.
+
*capath[NUMBER]
</li>
+
**path to a directory containing the PEM encoded trusted CA certificate files. See mosquitto.conf for more details on configuring this directory.
<li>Either cafile or capath must not be NULL.
+
**Either cafile or capath must not&nbsp;be NULL.
</li></ul></li>
+
*certfile[POINTER]
<li>capath[NUMBER]
+
**path to a file containing the PEM encoded certificate file&nbsp;for this client.
<ul><li>path to a directory containing the PEM encoded trusted CA certificate files. See mosquitto.conf for more details on configuring this directory.
+
**If NULL, keyfile must also be NULL and no&nbsp;client certificate will be used.
</li>
+
*keyfile[POINTER]
<li>Either cafile or capath must not&#160;be NULL.
+
**path to a file containing the PEM encoded private key for this client.
</li></ul></li>
+
**If NULL, certfile must also be NULL and no client certificate will be used.
<li>certfile[POINTER]
+
*password[POINTER]
<ul><li>path to a file containing the PEM encoded certificate file&#160;for this client.
+
**if keyfile is encrypted, set the password to allow your client&nbsp;to pass the correct password for decryption.
</li>
+
**need to encrypt with AgentEncrypt application.
<li>If NULL, keyfile must also be NULL and no&#160;client certificate will be used.
+
 
</li></ul></li>
+
''Return Values''
<li>keyfile[POINTER]
+
 
<ul><li>path to a file containing the PEM encoded private key for this client.
+
*true/false
</li>
+
 
<li>If NULL, certfile must also be NULL and no client certificate will be used.
+
''Example''
</li></ul></li>
+
<syntaxhighlight lang="c">#include "WISECore.h"
<li>password[POINTER]
 
<ul><li>if keyfile is encrypted, set the password to allow your client&#160;to pass the correct password for decryption.
 
</li>
 
<li>need to encrypt with AgentEncrypt application.
 
</li></ul></li></ul><p><i>Return Values</i>
 
</p>
 
<ul><li>true/false
 
</li></ul><p><i>Example</i>
 
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;#include &amp;quot;WISECore.h&amp;quot;
 
 
int main (int argc, char *argv[])  
 
int main (int argc, char *argv[])  
 
{
 
{
     int iRet = 0; /*Initialize WISECore*/ if(!core_initialize(&amp;quot;general&amp;quot;, &amp;quot;00000001-0000-0000-0000-305A3A77B1CC&amp;quot;, &amp;quot;TestClient&amp;quot;, &amp;quot;305A3A77B1CC&amp;quot;, NULL))
+
     int iRet = 0; /*Initialize WISECore*/ if(!core_initialize("general", "00000001-0000-0000-0000-305A3A77B1CC", "TestClient", "305A3A77B1CC", NULL))
 
     {
 
     {
         printf(&amp;quot;Unable to initialize WISECore.\n&amp;quot;);
+
         printf("Unable to initialize WISECore.\n");
 
         goto EXIT;
 
         goto EXIT;
 
     }
 
     }
     printf(&amp;quot;Agent Initialized\n&amp;quot;);
+
     printf("Agent Initialized\n");
 
     /*Setup TLS*/
 
     /*Setup TLS*/
     core_tls_set( &amp;quot;server.crt&amp;quot;, NULL, &amp;quot;ca.crt&amp;quot;, &amp;quot;ca.key&amp;quot;, &amp;quot;05155853&amp;quot;);
+
     core_tls_set( "server.crt", NULL, "ca.crt", "ca.key", "05155853");
 
     /*Connect to Server*/
 
     /*Connect to Server*/
     core_connect(&amp;quot;127.0.0.1&amp;quot;,1883,&amp;quot;admin&amp;quot;,&amp;quot;05155853&amp;quot;);
+
     core_connect("127.0.0.1",1883,"admin","05155853");
  
 
     /*Disconnect*/
 
     /*Disconnect*/
Line 191: Line 192:
 
     return iRet;  
 
     return iRet;  
 
}
 
}
&lt;/syntaxhighlight&gt;</pre>
+
</syntaxhighlight>
</p>
+
----
<hr />
+
 
<p>
+
 
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;bool core_tls_psk_set(const char *psk, const char *identity, const char *ciphers);&#160;&lt;/syntaxhighlight&gt;</pre> <i>Description</i>
+
<syntaxhighlight lang="c">bool core_tls_psk_set(const char *psk, const char *identity, const char *ciphers); </syntaxhighlight> ''Description''
</p>
+
*Configure the client for pre-shared-key based TLS support. Must be called&nbsp;before &lt;wc_connect&gt;.
<ul><li>Configure the client for pre-shared-key based TLS support. Must be called&#160;before &lt;wc_connect&gt;.
+
*Cannot be used in conjunction with &lt;core_tls_set&gt;.
</li>
+
 
<li>Cannot be used in conjunction with &lt;core_tls_set&gt;.
+
''Parameters''
</li></ul><p><i>Parameters</i>
+
 
</p>
+
*psk&nbsp;[POINTER]
<ul><li>psk&#160;[POINTER]
+
**the pre-shared-key in hex format with no leading "0x".
<ul><li>the pre-shared-key in hex format with no leading "0x".
+
*identity[POINTER]
</li></ul></li>
+
**the identity of this client. May be used as the username&nbsp;depending on the server settings.
<li>identity[POINTER]
+
*ciphers[POINTER]
<ul><li>the identity of this client. May be used as the username&#160;depending on the server settings.
+
**a string describing the PSK ciphers available for use. See the "openssl ciphers" tool for more information.
</li></ul></li>
+
**If NULL, the default ciphers will be used.
<li>ciphers[POINTER]
+
 
<ul><li>a string describing the PSK ciphers available for use. See the "openssl ciphers" tool for more information.
+
''Return Values''
</li>
+
 
<li>If NULL, the default ciphers will be used.
+
*true/false
</li></ul></li></ul><p><i>Return Values</i>
+
 
</p>
+
''Example''
<ul><li>true/false
+
<syntaxhighlight lang="c"> 
</li></ul><p><i>Example</i>
+
</syntaxhighlight>
<pre class="fck_mw_syntaxhighlight">&lt;syntaxhighlight lang=&quot;c&quot;&gt;&#160;
+
=== Callback Function ===
&lt;/syntaxhighlight&gt;</pre>
+
<pre>typedef&nbsp;void (*SACLIENT_MESSAGE_RECV_CALLBACK)(char* topic, susiaccess_packet_body_t *pkt, void *pRev1, void* pRev2);</pre>
</p>
+
 
<h3> Callback Function </h3>
+
''Description''
<pre>typedef&amp;nbsp;void (*SACLIENT_MESSAGE_RECV_CALLBACK)(char* topic, susiaccess_packet_body_t *pkt, void *pRev1, void* pRev2);</pre>
+
 
<p><i>Description</i>
+
*define the message receive callback function.
</p>
+
 
<ul><li>define the message receive callback function.
+
''Parameters''
</li></ul><p><i>Parameters</i>
+
 
</p>
+
*topic[POINTER]
<ul><li>topic[POINTER]
+
**MQTT topic string
<ul><li>MQTT topic string
+
*pkt[POINTER]
</li></ul></li>
+
**the packet&nbsp;structure pointer.
<li>pkt[POINTER]
+
*pRev1[POINTER]
<ul><li>the packet&#160;structure pointer.
+
**preserved pointer.
</li></ul></li>
+
*pRev2[POINTER]
<li>pRev1[POINTER]
+
**preserved pointer.
<ul><li>preserved pointer.
+
 
</li></ul></li>
+
''Return Values''
<li>pRev2[POINTER]
+
 
<ul><li>preserved pointer.
+
*None.
</li></ul></li></ul><p><i>Return Values</i>
+
 
</p>
+
----
<ul><li>None.
+
 
</li></ul><hr />
+
 
<p>
+
<pre>typedef&nbsp;void (*SACLIENT_CONNECTED_CALLBACK)();</pre>
</p>
+
 
<pre>typedef&amp;nbsp;void (*SACLIENT_CONNECTED_CALLBACK)();</pre>
+
''Description''
<p><i>Description</i>
+
 
</p>
+
*define the on connected callback function.
<ul><li>define the on connected callback function.
+
 
</li></ul><p><i>Parameters</i>
+
''Parameters''
</p>
+
 
<ul><li>None
+
*None
</li></ul><p><i>Return Values</i>
+
 
</p>
+
''Return Values''
<ul><li>None.
+
 
</li></ul><hr />
+
*None.
<p>
+
 
</p>
+
----
<pre>typedef&amp;nbsp;void (*SACLIENT_LOSTCONNECT_CALLBACK)();</pre>
+
 
<p><i>Description</i>
+
 
</p>
+
<pre>typedef&nbsp;void (*SACLIENT_LOSTCONNECT_CALLBACK)();</pre>
<ul><li>define the on lost connect&#160;callback function.
+
 
</li></ul><p><i>Parameters</i>
+
''Description''
</p>
+
 
<ul><li>None
+
*define the on lost connect&nbsp;callback function.
</li></ul><p><i>Return Values</i>
+
 
</p>
+
''Parameters''
<ul><li>None.
+
 
</li></ul><hr />
+
*None
<p>
+
 
</p>
+
''Return Values''
<pre>typedef&amp;nbsp;void (*SACLIENT_DISCONNECT_CALLBACK)();</pre>
+
 
<p><i>Description</i>
+
*None.
</p>
+
 
<ul><li>define the on disconnect&#160;callback function.
+
----
</li></ul><p><i>Parameters</i>
+
 
</p>
+
 
<ul><li>None
+
<pre>typedef&nbsp;void (*SACLIENT_DISCONNECT_CALLBACK)();</pre>
</li></ul><p><i>Return Values</i>
+
 
</p>
+
''Description''
<ul><li>None.
+
 
</li></ul><h3> Configuration Structure </h3>
+
*define the on disconnect&nbsp;callback function.
 +
 
 +
''Parameters''
 +
 
 +
*None
 +
 
 +
''Return Values''
 +
 
 +
*None.
 +
 
 +
=== Configuration Structure ===
 
<pre>typedef struct {
 
<pre>typedef struct {
 
/*Connection Mode*/
 
/*Connection Mode*/
Line 302: Line 313:
 
char ciphers[DEF_MAX_CIPHER];
 
char ciphers[DEF_MAX_CIPHER];
 
}susiaccess_agent_conf_body_t;</pre>
 
}susiaccess_agent_conf_body_t;</pre>
<p><i>Description</i>
+
 
</p>
+
''Description''
<ul><li>The structure for agent connect&#160;configuration, defined in&#160;susiaccess_def.h.
+
 
</li></ul><p><i>Parameters</i>
+
*The structure for agent connect&nbsp;configuration, defined in&nbsp;susiaccess_def.h.
</p>
+
 
<ul><li>runMode[POINTER]
+
''Parameters''
<ul><li>default is remote.
+
 
</li>
+
*runMode[POINTER]
<li>no other mode in WISE Agent version 3.x
+
**default is remote.
</li></ul></li>
+
**no other mode in WISE Agent version 3.x
<li>autoStart[POINTER]
+
*autoStart[POINTER]
<ul><li>The Agent will reconnect to server automatically,&#160;default is True.
+
**The Agent will reconnect to server automatically,&nbsp;default is True.
</li></ul></li>
+
*serverIP[POINTER]
<li>serverIP[POINTER]
+
**indicate the server URL or IP Address
<ul><li>indicate the server URL or IP Address
+
*serverPort[POINTER]
</li></ul></li>
+
**Server (MQTT Broker) listen port,
<li>serverPort[POINTER]
+
**in WISE Agent version 3.1 or later the&nbsp;default port is 1883
<ul><li>Server (MQTT Broker) listen port,
+
**in WISE Agent version 3.0 the&nbsp;default port&nbsp;is 10001.
</li>
+
*serverAuth[POINTER]
<li>in WISE Agent version 3.1 or later the&#160;default port is 1883
+
**Server (MQTT Broker) authentication string.
</li>
+
**The string is encode from &lt;ID&gt;:&lt;PASS&gt;.
<li>in WISE Agent version 3.0 the&#160;default port&#160;is 10001.
+
**It worked on SSL Mode.
</li></ul></li>
+
*tlstype&nbsp;[NUMBER]
<li>serverAuth[POINTER]
+
**define the TLS (SSL) mode:
<ul><li>Server (MQTT Broker) authentication string.
+
***tls_type_none
</li>
+
***tls_type_tls
<li>The string is encode from &lt;ID&gt;:&lt;PASS&gt;.
+
***tls_type_psk
</li>
+
*cafile[POINTER]
<li>It worked on SSL Mode.
+
**the filepath of SSL Certificate Authority&nbsp;file in&nbsp;tls_type_tls mode.
</li></ul></li>
+
*capath[POINTER]
<li>tlstype&#160;[NUMBER]
+
**the path of SSL Certificate Authority&nbsp;files in&nbsp;tls_type_tls mode.
<ul><li>define the TLS (SSL) mode:
+
*certfile[POINTER]
<ul><li>tls_type_none
+
**the filepath of SSL Certificate file in&nbsp;tls_type_tls mode.
</li>
+
*keyfile[POINTER]
<li>tls_type_tls
+
**the&nbsp;filepath of SSL Key file in&nbsp;tls_type_tls mode.
</li>
+
*cerpasswd[POINTER]
<li>tls_type_psk
+
**the string of SSL Certificate file password in&nbsp;tls_type_tls mode.
</li></ul></li></ul></li>
+
*psk[POINTER]
<li>cafile[POINTER]
+
**the string of SSL pre-share key in&nbsp;tls_type_psk mode.
<ul><li>the filepath of SSL Certificate Authority&#160;file in&#160;tls_type_tls mode.
+
*identity[POINTER]
</li></ul></li>
+
**the unique string of SSL identity&nbsp;in&nbsp;tls_type_psk mode.
<li>capath[POINTER]
+
*ciphers[POINTER]
<ul><li>the path of SSL Certificate Authority&#160;files in&#160;tls_type_tls mode.
+
**the supported cipher list in&nbsp;tls_type_psk mode.
</li></ul></li>
+
 
<li>certfile[POINTER]
+
=== Profile Structure ===
<ul><li>the filepath of SSL Certificate file in&#160;tls_type_tls mode.
 
</li></ul></li>
 
<li>keyfile[POINTER]
 
<ul><li>the&#160;filepath of SSL Key file in&#160;tls_type_tls mode.
 
</li></ul></li>
 
<li>cerpasswd[POINTER]
 
<ul><li>the string of SSL Certificate file password in&#160;tls_type_tls mode.
 
</li></ul></li>
 
<li>psk[POINTER]
 
<ul><li>the string of SSL pre-share key in&#160;tls_type_psk mode.
 
</li></ul></li>
 
<li>identity[POINTER]
 
<ul><li>the unique string of SSL identity&#160;in&#160;tls_type_psk mode.
 
</li></ul></li>
 
<li>ciphers[POINTER]
 
<ul><li>the supported cipher list in&#160;tls_type_psk mode.
 
</li></ul></li></ul><h3> Profile Structure </h3>
 
 
<pre>typedef struct {
 
<pre>typedef struct {
 
/*Agent Info*/
 
/*Agent Info*/
Line 391: Line 385:
 
char localip[DEF_MAX_STRING_LENGTH];
 
char localip[DEF_MAX_STRING_LENGTH];
 
}susiaccess_agent_profile_body_t;</pre>
 
}susiaccess_agent_profile_body_t;</pre>
<p><i>Description</i>
+
 
</p>
+
''Description''
<ul><li>The structure for &#160;platform description, defined in&#160;susiaccess_def.h.
+
 
</li></ul><p><i>Parameters</i>
+
*The structure for &nbsp;platform description, defined in&nbsp;susiaccess_def.h.
</p>
+
 
<ul><li>version[POINTER]
+
''Parameters''
<ul><li>The version fo the application.
+
 
</li></ul></li>
+
*version[POINTER]
<li>hostname&#160;[POINTER]
+
**The version fo the application.
<ul><li>The name of target device ro agent.
+
*hostname&nbsp;[POINTER]
</li></ul></li>
+
**The name of target device ro agent.
<li>devId[POINTER]
+
*devId[POINTER]
<ul><li>The Unique ID of the target device or agent.
+
**The Unique ID of the target device or agent.
</li></ul></li>
+
*sn[POINTER]
<li>sn[POINTER]
+
**The target&nbsp;device serial number
<ul><li>The target&#160;device serial number
+
*mac[POINTER]
</li></ul></li>
+
**the MAC Address of first ethernet or wireless card.
<li>mac[POINTER]
+
*type[POINTER]
<ul><li>the MAC Address of first ethernet or wireless card.
+
**the agent type, defualt is IPC.
</li></ul></li>
+
**User can define their own type for customization.
<li>type[POINTER]
+
*produce[POINTER]
<ul><li>the agent type, defualt is IPC.
+
**the product name.
</li>
+
*manufacture[POINTER]
<li>User can define their own type for customization.
+
**the manufacture name.
</li></ul></li>
+
*osversion[POINTER]
<li>produce[POINTER]
+
**the OS version of target device.
<ul><li>the product name.
+
*biosversion[POINTER]
</li></ul></li>
+
**the BIOS version of target device.
<li>manufacture[POINTER]
+
*platformname[POINTER]
<ul><li>the manufacture name.
+
**the platform (board) name of target device.
</li></ul></li>
+
*processorname[POINTER]
<li>osversion[POINTER]
+
**the processor name of target device.
<ul><li>the OS version of target device.
+
*osarchitect[POINTER]
</li></ul></li>
+
**the OS architecture name of target device.
<li>biosversion[POINTER]
+
*totalmemsize&nbsp;[NUMBER]
<ul><li>the BIOS version of target device.
+
**the OS recognized total memory size of target device.
</li></ul></li>
+
*maclist[POINTER]
<li>platformname[POINTER]
+
**list all the ethernet and wireless card MAC Address.
<ul><li>the platform (board) name of target device.
+
*localip[POINTER]
</li></ul></li>
+
**the local IP of target device.
<li>processorname[POINTER]
+
*account[POINTER]
<ul><li>the processor name of target device.
+
**bind the device or anget to the sepcific account, default is anonymous.
</li></ul></li>
+
*passwd[POINTER]
<li>osarchitect[POINTER]
+
**the encrypted password string of account.
<ul><li>the OS architecture name of target device.
+
*workdir[POINTER]
</li></ul></li>
+
**current executable binary file location.
<li>totalmemsize&#160;[NUMBER]
+
 
<ul><li>the OS recognized total memory size of target device.
+
=== Packet Structure ===
</li></ul></li>
+
<pre>&nbsp;typedef struct{
<li>maclist[POINTER]
 
<ul><li>list all the ethernet and wireless card MAC Address.
 
</li></ul></li>
 
<li>localip[POINTER]
 
<ul><li>the local IP of target device.
 
</li></ul></li>
 
<li>account[POINTER]
 
<ul><li>bind the device or anget to the sepcific account, default is anonymous.
 
</li></ul></li>
 
<li>passwd[POINTER]
 
<ul><li>the encrypted password string of account.
 
</li></ul></li>
 
<li>workdir[POINTER]
 
<ul><li>current executable binary file location.
 
</li></ul></li></ul><h3> Packet Structure </h3>
 
<pre>&amp;nbsp;typedef struct{
 
 
int cmd;
 
int cmd;
int requestID;      /**&lt; Callback request id */
+
int requestID;      /**< Callback request id */
char devId[DEF_DEVID_LENGTH]; /**&lt; Agent device id */
+
char devId[DEF_DEVID_LENGTH]; /**< Agent device id */
 
char handlerName[MAX_TOPIC_LEN];
 
char handlerName[MAX_TOPIC_LEN];
char* content;    /**&lt; Callback request content */
+
char* content;    /**< Callback request content */
 
packet_type type;
 
packet_type type;
 
}susiaccess_packet_body_t;</pre>
 
}susiaccess_packet_body_t;</pre>
<p><i>Description</i>
 
</p>
 
<ul><li>the structure for transmit packet, defined in susiaccess_def.h.
 
</li></ul><p><i>Parameters</i>
 
</p>
 
<ul><li>cmd&#160;[NUMBER]
 
<ul><li>The command id defined in Plugins (Handlers).
 
</li></ul></li>
 
<li>requestID&#160;[NUMBER]
 
<ul><li>The unique ID for Plugins (Handlers),
 
</li>
 
<li>No used for V3.1 or later version, preserved for V3.0.
 
</li></ul></li>
 
<li>devId[POINTER]
 
<ul><li>The target device or Agent ID.
 
</li></ul></li>
 
<li>handlerName[POINTER]
 
<ul><li>The string of Plugin (Handler) Name.
 
</li></ul></li>
 
<li>content[POINTER]
 
<ul><li>the string of packet content message.
 
</li></ul></li>
 
<li>type [NUMBER]
 
<ul><li>the packet type:
 
<ul><li>pkt_type_susiaccess: common RMM message packet wrapped with&#160;RMM pre-defined header.
 
</li>
 
<li>pkt_type_custom: customized message packet without wrapped with RMM header.
 
</li></ul></li></ul></li></ul>
 
  
== Client Sample Code ==
+
''Description''
 +
 
 +
*the structure for transmit packet, defined in susiaccess_def.h.
 +
 
 +
''Parameters''
  
 +
*cmd&nbsp;[NUMBER]
 +
**The command id defined in Plugins (Handlers).
 +
*requestID&nbsp;[NUMBER]
 +
**The unique ID for Plugins (Handlers),
 +
**No used for V3.1 or later version, preserved for V3.0.
 +
*devId[POINTER]
 +
**The target device or Agent ID.
 +
*handlerName[POINTER]
 +
**The string of Plugin (Handler) Name.
 +
*content[POINTER]
 +
**the string of packet content message.
 +
*type [NUMBER]
 +
**the packet type:
 +
***pkt_type_susiaccess: common RMM message packet wrapped with&nbsp;RMM pre-defined header.
 +
***pkt_type_custom: customized message packet without wrapped with RMM header.
  
 +
== Client Sample Code ==
 
<pre>#include "stdafx.h"
 
<pre>#include "stdafx.h"
 
#include <SAClient.h>
 
#include <SAClient.h>
Line 521: Line 491:
 
{
 
{
 
/*user can process received command here*/
 
/*user can process received command here*/
SUSIAccessAgentLog(Normal, "Packet received, %s\r\n", pkt->content);
+
SUSIAccessAgentLog(Normal, "Packet received,&nbsp;%s\r\n", pkt->content);
 
}
 
}
  
Line 540: Line 510:
 
// Initialize Log Library
 
// Initialize Log Library
 
SUSIAccessAgentLogHandle = InitLog(moudlePath);
 
SUSIAccessAgentLogHandle = InitLog(moudlePath);
SUSIAccessAgentLog(Normal, "Current path: %s", moudlePath);
+
SUSIAccessAgentLog(Normal, "Current path:&nbsp;%s", moudlePath);
  
 
// Pre-set Agent Config struct
 
// Pre-set Agent Config struct
Line 598: Line 568:
 
iRet = saclient_initialize(&config, &profile, SUSIAccessAgentLogHandle);
 
iRet = saclient_initialize(&config, &profile, SUSIAccessAgentLogHandle);
  
if(iRet != saclient_success)
+
if(iRet&nbsp;!= saclient_success)
 
{
 
{
 
SUSIAccessAgentLog(Error, "Unable to initialize AgentCore.");
 
SUSIAccessAgentLog(Error, "Unable to initialize AgentCore.");
Line 614: Line 584:
 
iRet = saclient_connect();
 
iRet = saclient_connect();
  
if(iRet != saclient_success){
+
if(iRet&nbsp;!= saclient_success){
 
SUSIAccessAgentLog(Error, "sampleagent Unable to connect to broker.");
 
SUSIAccessAgentLog(Error, "sampleagent Unable to connect to broker.");
 
goto EXIT;
 
goto EXIT;
 
} else {
 
} else {
SUSIAccessAgentLog(Normal, "sampleagent Connect to broker: %s", config.serverIP);
+
SUSIAccessAgentLog(Normal, "sampleagent Connect to broker:&nbsp;%s", config.serverIP);
 
}
 
}
 
 

Revision as of 06:50, 18 May 2017

Advantech provides a WISE-PaaS 2.0 EI-Agent Sample to demonstrate how to implement a custom application to connect to WISE-PaaS 2.0 Server.

WISECore APIs

List

bool core_initialize(char* strTenantID, char* strClientID, char* strHostName, char* strMAC, void* userdata);
Description
  • General initialization of the WISECore API. Prior to calling any WISECore API functions, the library needs to be initialized by calling this function.
  • The result for all WISECore API function will be false unless this function is called.

Parameters

  • strTenantID[POINTER]
    • the tenant ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
  • strClientID[POINTER]
    • the client ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
  • strHostName[POINTER]
    • the host name string as device recognize name.
  • strMAC[POINTER]
    • first MAC address of the device.
  • userdata[POINTER]
    • the unstructured pointer to reference to user data.

Return Values

  • true/false

Example

Reference to core_account_bind().



void core_uninitialize();
Description
  • General function to uninitialize the WISECore API library that should be called before program exit.

Parameters

  • None

Return Values

  • None

Example

Reference to core_account_bind().



bool core_product_info_set(char* strSerialNum, char* strParentID, char* strVersion, char* strType, char* strProduct, char* strManufacture);
Description
  • Assign the basic information to identify the device.

Parameters

  • strSerialNum[POINTER]
    • the serial number string of target device.
  • strParentID[POINTER]
    • the parent device ID string in UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, used to point out the EIS or Gateway device ID if the taget device connect to server through an EIS or Gateway device.
  • strVersion[POINTER]
    • the applicatin version string.
  • strType[POINTER]
    • the applicatin type string, default is "IPC" for general usage.
  • strProduct[POINTER]
    • the target device product name.
  • strManufacture[POINTER]
    • the target device manufacture name.

Return Values

  • true/false

Example

Reference to core_account_bind().



bool core_tag_set(char* strTag);
Description
  • Assign the tag to markup the support product, for example: "RMM" for RMM product.

Parameters

  • strManufacture[POINTER]
    • the target device supported function set.

Return Values

  • true/false

Example

Reference to core_account_bind().



bool core_account_bind(char* strLoginID, char* strLoginPW);
Description
  • Bind the device to a specific user account automatically.

Parameters

  • strLoginID[POINTER]
    • Account ID.
  • strLoginPW[POINTER]
    • Account Password, need to encrypt with AgentEncrypt application.

Return Values

  • true/false.

Example

#include "WISECore.h"
 
int main (int argc, char *argv[])
{
	int iRet = 0;
	/*Initialize WISECore*/
	if(!core_initialize("general", "00000001-0000-0000-0000-305A3A77B1CC", "TestClient", "305A3A77B1CC", NULL))
    {
        printf("Unable to initialize WISECore.\n");
        goto EXIT;
    }
    printf("Agent Initialized\n");
    /*Setup device identify information*/
    core_product_info_set("305A3A77B1CC", NULL/*no parent ID*/, "2.0.1", "IPC", "test", "test"); 
    /*Setup support function tag*/
    core_tag_set("RMM");
    /*binding to a specific account*/
    core_account_bind("admin","GP25qY7TjJA=");
EXIT:
    /*Release WISECore*/
    core_uninitialize();
	return iRet;
}


bool core_tls_set(const char *cafile, const char *capath, const char *certfile, const char *keyfile, const char *password);
Description
  • Configure the client for certificate based SSL/TLS support. Must be called before <core_connect>.
  • Cannot be used in conjunction with <core_tls_psk_set>.
  • Define the Certificate Authority certificates to be trusted (ie. the server certificate must be signed with one of these certificates) using cafile. If the server you are connecting to requires clients to provide a certificate, define certfile and keyfile with your client certificate and private key. If your private key is encrypted, provide a password callback function.

Parameters

  • cafile[POINTER]
    • path to a file containing the PEM encoded trusted CA certificate files.
    • Either cafile or capath must not be NULL.
  • capath[NUMBER]
    • path to a directory containing the PEM encoded trusted CA certificate files. See mosquitto.conf for more details on configuring this directory.
    • Either cafile or capath must not be NULL.
  • certfile[POINTER]
    • path to a file containing the PEM encoded certificate file for this client.
    • If NULL, keyfile must also be NULL and no client certificate will be used.
  • keyfile[POINTER]
    • path to a file containing the PEM encoded private key for this client.
    • If NULL, certfile must also be NULL and no client certificate will be used.
  • password[POINTER]
    • if keyfile is encrypted, set the password to allow your client to pass the correct password for decryption.
    • need to encrypt with AgentEncrypt application.

Return Values

  • true/false

Example

#include "WISECore.h"
int main (int argc, char *argv[]) 
{
    int iRet = 0; /*Initialize WISECore*/ if(!core_initialize("general", "00000001-0000-0000-0000-305A3A77B1CC", "TestClient", "305A3A77B1CC", NULL))
    {
        printf("Unable to initialize WISECore.\n");
        goto EXIT;
    }
    printf("Agent Initialized\n");
    /*Setup TLS*/
    core_tls_set( "server.crt", NULL, "ca.crt", "ca.key", "05155853");
    /*Connect to Server*/
    core_connect("127.0.0.1",1883,"admin","05155853");

    /*Disconnect*/
    core_disconnect(false);

EXIT:
    /*Release WISECore*/
    core_uninitialize();
    return iRet; 
}


bool core_tls_psk_set(const char *psk, const char *identity, const char *ciphers); 
Description
  • Configure the client for pre-shared-key based TLS support. Must be called before <wc_connect>.
  • Cannot be used in conjunction with <core_tls_set>.

Parameters

  • psk [POINTER]
    • the pre-shared-key in hex format with no leading "0x".
  • identity[POINTER]
    • the identity of this client. May be used as the username depending on the server settings.
  • ciphers[POINTER]
    • a string describing the PSK ciphers available for use. See the "openssl ciphers" tool for more information.
    • If NULL, the default ciphers will be used.

Return Values

  • true/false

Example

 

Callback Function

typedef void (*SACLIENT_MESSAGE_RECV_CALLBACK)(char* topic, susiaccess_packet_body_t *pkt, void *pRev1, void* pRev2);

Description

  • define the message receive callback function.

Parameters

  • topic[POINTER]
    • MQTT topic string
  • pkt[POINTER]
    • the packet structure pointer.
  • pRev1[POINTER]
    • preserved pointer.
  • pRev2[POINTER]
    • preserved pointer.

Return Values

  • None.


typedef void (*SACLIENT_CONNECTED_CALLBACK)();

Description

  • define the on connected callback function.

Parameters

  • None

Return Values

  • None.


typedef void (*SACLIENT_LOSTCONNECT_CALLBACK)();

Description

  • define the on lost connect callback function.

Parameters

  • None

Return Values

  • None.


typedef void (*SACLIENT_DISCONNECT_CALLBACK)();

Description

  • define the on disconnect callback function.

Parameters

  • None

Return Values

  • None.

Configuration Structure

typedef struct {
	/*Connection Mode*/
	char runMode[DEF_RUN_MODE_LENGTH];
	char autoStart[DEF_ENABLE_LENGTH];

	/*Connection Info*/
	char serverIP[DEF_MAX_STRING_LENGTH];
	char serverPort[DEF_PORT_LENGTH];
	char serverAuth[DEF_USER_PASS_LENGTH];

	tls_type tlstype;
	/*TLS*/
	char cafile[DEF_MAX_PATH];
	char capath[DEF_MAX_PATH];
	char certfile[DEF_MAX_PATH];
	char keyfile[DEF_MAX_PATH];
	char cerpasswd[DEF_USER_PASS_LENGTH];
	/*pre-shared-key*/
	char psk[DEF_USER_PASS_LENGTH];
	char identity[DEF_USER_PASS_LENGTH];
	char ciphers[DEF_MAX_CIPHER];
}susiaccess_agent_conf_body_t;

Description

  • The structure for agent connect configuration, defined in susiaccess_def.h.

Parameters

  • runMode[POINTER]
    • default is remote.
    • no other mode in WISE Agent version 3.x
  • autoStart[POINTER]
    • The Agent will reconnect to server automatically, default is True.
  • serverIP[POINTER]
    • indicate the server URL or IP Address
  • serverPort[POINTER]
    • Server (MQTT Broker) listen port,
    • in WISE Agent version 3.1 or later the default port is 1883
    • in WISE Agent version 3.0 the default port is 10001.
  • serverAuth[POINTER]
    • Server (MQTT Broker) authentication string.
    • The string is encode from <ID>:<PASS>.
    • It worked on SSL Mode.
  • tlstype [NUMBER]
    • define the TLS (SSL) mode:
      • tls_type_none
      • tls_type_tls
      • tls_type_psk
  • cafile[POINTER]
    • the filepath of SSL Certificate Authority file in tls_type_tls mode.
  • capath[POINTER]
    • the path of SSL Certificate Authority files in tls_type_tls mode.
  • certfile[POINTER]
    • the filepath of SSL Certificate file in tls_type_tls mode.
  • keyfile[POINTER]
    • the filepath of SSL Key file in tls_type_tls mode.
  • cerpasswd[POINTER]
    • the string of SSL Certificate file password in tls_type_tls mode.
  • psk[POINTER]
    • the string of SSL pre-share key in tls_type_psk mode.
  • identity[POINTER]
    • the unique string of SSL identity in tls_type_psk mode.
  • ciphers[POINTER]
    • the supported cipher list in tls_type_psk mode.

Profile Structure

typedef struct {
	/*Agent Info*/
	char version[DEF_MAX_STRING_LENGTH];
	char hostname[DEF_HOSTNAME_LENGTH];
	char devId[DEF_DEVID_LENGTH];
	char sn[DEF_SN_LENGTH];
	char mac[DEF_MAC_LENGTH];
	char lal[DEF_LAL_LENGTH];
	char account[DEF_USER_PASS_LENGTH];
	char passwd[DEF_USER_PASS_LENGTH];
	char workdir[DEF_MAX_PATH];

	/*Custom Info*/
	char type[DEF_MAX_STRING_LENGTH];
	char product[DEF_MAX_STRING_LENGTH];
	char manufacture[DEF_MAX_STRING_LENGTH];

	/*OS Info*/
	char osversion[DEF_OSVERSION_LEN];
	char biosversion[DEF_VERSION_LENGTH];
	char platformname[DEF_FILENAME_LENGTH];
	char processorname[DEF_PROCESSOR_NAME_LEN];
	char osarchitect[DEF_FILENAME_LENGTH];
	long totalmemsize;
	char maclist[DEF_MAC_LENGTH*16];
	char localip[DEF_MAX_STRING_LENGTH];
}susiaccess_agent_profile_body_t;

Description

  • The structure for  platform description, defined in susiaccess_def.h.

Parameters

  • version[POINTER]
    • The version fo the application.
  • hostname [POINTER]
    • The name of target device ro agent.
  • devId[POINTER]
    • The Unique ID of the target device or agent.
  • sn[POINTER]
    • The target device serial number
  • mac[POINTER]
    • the MAC Address of first ethernet or wireless card.
  • type[POINTER]
    • the agent type, defualt is IPC.
    • User can define their own type for customization.
  • produce[POINTER]
    • the product name.
  • manufacture[POINTER]
    • the manufacture name.
  • osversion[POINTER]
    • the OS version of target device.
  • biosversion[POINTER]
    • the BIOS version of target device.
  • platformname[POINTER]
    • the platform (board) name of target device.
  • processorname[POINTER]
    • the processor name of target device.
  • osarchitect[POINTER]
    • the OS architecture name of target device.
  • totalmemsize [NUMBER]
    • the OS recognized total memory size of target device.
  • maclist[POINTER]
    • list all the ethernet and wireless card MAC Address.
  • localip[POINTER]
    • the local IP of target device.
  • account[POINTER]
    • bind the device or anget to the sepcific account, default is anonymous.
  • passwd[POINTER]
    • the encrypted password string of account.
  • workdir[POINTER]
    • current executable binary file location.

Packet Structure

 typedef struct{
	int cmd;
	int requestID;      /**< Callback request id */
	char devId[DEF_DEVID_LENGTH]; /**< Agent device id */
	char handlerName[MAX_TOPIC_LEN];
	char* content;     /**< Callback request content */
	packet_type type;
}susiaccess_packet_body_t;

Description

  • the structure for transmit packet, defined in susiaccess_def.h.

Parameters

  • cmd [NUMBER]
    • The command id defined in Plugins (Handlers).
  • requestID [NUMBER]
    • The unique ID for Plugins (Handlers),
    • No used for V3.1 or later version, preserved for V3.0.
  • devId[POINTER]
    • The target device or Agent ID.
  • handlerName[POINTER]
    • The string of Plugin (Handler) Name.
  • content[POINTER]
    • the string of packet content message.
  • type [NUMBER]
    • the packet type:
      • pkt_type_susiaccess: common RMM message packet wrapped with RMM pre-defined header.
      • pkt_type_custom: customized message packet without wrapped with RMM header.

Client Sample Code

#include "stdafx.h"
#include <SAClient.h>
#include <Windows.h>
#include <Log.h>
/*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, %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: %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 URL 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 target device 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 != 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, on_disconnect_cb, on_disconnect_cb);

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

	if(iRet != saclient_success){
		SUSIAccessAgentLog(Error, "sampleagent Unable to connect to broker.");
		goto EXIT;
	} else {
		SUSIAccessAgentLog(Normal, "sampleagent Connect to broker: %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;
}