# Flutter API
<p>[TOC]</p>
<h1>tk_initSdk</h1>
<ul>
<li>Function: IOTC initialization, confirming the legality of sdkLicensekey</li>
<li>The Key value needs to be applied to TUTK</li>
</ul>
<pre><code class="language-dart">/**
IOTC Initial
/// Initialize SDK, Change TUTK Server's realm
/// Need to be called before IOTC_Initialize2()
/// return TUTK_ER_NoERROR if successfully set master realm.
/// * \return Error code if return value &lt; 0
/// * - #TUTK_ER_INVALID_ARG if the region is invalid
/// * - #TUTK_ER_INVALID_LICENSE_KEY if the key is invalid
/// * - #TUTK_ER_MEM_INSUFFICIENT if the memory allocation failed
/// * [licenseKey] The TUTK Server's realm to be used.
*/
static Future&lt;int&gt; tk_initSdk(String licenseKey);
Example:
FlutterTunnel.tk_initSdk('licensekey');
</code></pre>
<h1>tk_connectTunnel</h1>
<ul>
<li>Function: Establish a Tunnel connection</li>
</ul>
<pre><code class="language-dart">/**
Tunnel Connect
/// This function is for a tunnel agent to connect a device
/// * [connectInfo]
/// var connectInfo = {&quot;uid&quot;: _uid, &quot;username&quot;: _username, &quot;password&quot;: _password};
/// * [_uid] The UID of that tunnel server to be connected
/// * [_username] The predefined view account
/// * [_password] The predefined view password
/// return Tunnel session ID if return value &gt;= 0 and equal to the input parameter SID.
/// return Error code if return value &lt; 0
*/
static Future&lt;int&gt;tk_connectTunnel(Map connectInfo)
Example:
var connectInfo = {&quot;uid&quot;: _uid, &quot;username&quot;: &quot;admin&quot;, &quot;password&quot;: &quot;testpwd&quot;};
int sid = await FlutterTunnel.tk_connectTunnel(connectInfo);</code></pre>
<h1>tk_startPortMapping</h1>
<ul>
<li>Function: Start mapping the ports on both ends</li>
</ul>
<pre><code class="language-dart">/**
Port Mapping
/// Start port mapping service
/// This function used by a tunnel agent to start port mapping service
// * provided by P2PTunnel module. The tunnel agent specifies the local port
// * in local host through which a tunnel is created with the remote port defined
// * in the tunnel server.
/// * [portInfo]
/// var portInfo = {&quot;uid&quot;: _uid, &quot;localPort&quot;: _localPort, &quot;remotePort&quot;: _remotePort};
/// * [_uid] The UID of that tunnel server to be connected
/// * [_localPort] The local port used to create a tunnel with the tunnel server
/// * [_remotePort] The remote port defined in the tunnel server to create a tunnel
/// return The port mapping index if return value &gt;= 0
/// return Error code if return value &lt; 0
*/
static Future&lt;int&gt;tk_startPortMapping(Map portInfo)
Example:
int webIndex;
var portInfo = {&quot;uid&quot;: _uid, &quot;localPort&quot;: 10000, &quot;remotePort&quot;: 80};
try {
webIndex = await FlutterTunnel.tk_startPortMapping(portInfo);
}
on Exception {
webIndex = -1;
}</code></pre>
<h1>tk_stopPortMapping</h1>
<ul>
<li>Function: Stop mapping the ports on both ends</li>
</ul>
<pre><code class="language-dart">/**
Stop Port Mapping
/// Stop port mapping service
/// This function used by a tunnel agent to stop port mapping service
// * on a given port mapping index which is started by P2PTunnelAgent_PortMapping()
/// * [portInfo]
/// var portInfo = {&quot;uid&quot;: _uid};
/// * [_uid] The UID of that tunnel server to be connected
*/
static Future&lt;void&gt;tk_stopPortMapping(Map portInfo)
Example:
var portInfo = {&quot;uid&quot;: _uid};
FlutterTunnel.tk_stopPortMapping(portInfo);
</code></pre>
<h1>tk_disconnectTunnel</h1>
<ul>
<li>Function: Close Tunnel Connection</li>
</ul>
<pre><code class="language-dart">/**
Disconnect
/// It's to stop the progressing of connection.
/// This API is for a client to stop connecting to a device.
// * We can use it to stop connecting when client blocks in P2PTunnelAgent_Connect_Ex().
/// * [connectInfo]
/// var connectInfo = {&quot;uid&quot;: _uid};
/// * [_uid] The UID of that tunnel server to be connected
*/
static Future&lt;void&gt;tk_disconnectTunnel(Map connectInfo)
Example:
var connectInfo = {&quot;uid&quot;: _uid};
FlutterTunnel.tk_disconnectTunnel(connectInfo);</code></pre>
<h1>tk_unInitSdk</h1>
<ul>
<li>Function: IOTC uninitialization</li>
</ul>
<pre><code class="language-dart">/**
IOTC Uninitial
/// Deinitialize P2PTunnel module in a tunnel agent
/// This function will deinitialize P2PTunnel module in a tunnel agent
/// * \return #TUNNEL_ER_NoERROR if deinitialize successfully
/// * \return Error code if return value &lt; 0
*/
static Future&lt;void&gt; tk_unInitSdk()
Example:
FlutterTunnel.tk_unInitSdk();</code></pre>
<h1>tk_setTunnelStatusCB</h1>
<ul>
<li>Function: Report the status of the Tunnel connection</li>
</ul>
<pre><code class="language-dart">/**
Tunnel connection status callback
/// return the tunnel status
/// This function will set a callback function pointer in P2PTunnel
// * module for a tunnel agent to get the tunnel status from given callback
// * function. Whenever tunnel status changes, P2PTunnel module will invoke
// * the given callback function to notify tunnel agent with corresponding
// * status.
/// * [_uid] The UID of that tunnel server to be connected
*/
static Future&lt;void&gt; tk_setTunnelStatusCB(String _uid, void Function(dynamic) callback)
Example:
FlutterTunnel.tk_setTunnelStatusCB(_uid, (data) {
String uid = data[&quot;uid&quot;];
int sid = data[&quot;sid&quot;];
int error = data[&quot;error&quot;];
setState(() {
_tunnelStatusCB = error;
if(_tunnelStatusCB &lt; 0) {
disconnectTunnel();
}
});
});
</code></pre>