中文简体    English

IvyIoT P2P SDK Programming Guide

1. Overview

  P2P SDK is a software development kit provided by IvyIoT for smart hardware devices and mobile phone apps of third-party manufacturers. Users can easily connect hardware devices and mobile phone apps to IvyIoT cloud services by embedding P2P SDK in device firmware and mobile phone apps Support the rapid establishment of P2P connection between APP and hardware devices, and finally realize the real-time audio and video data transmission between APP and hardware devices.

The P2P SDK is provided in the form of library files. The entire SDK folder structure is as follows:
Doc folder:stores documents related to the P2P SDK, such as interface description documents and demo program usage documents.
Include folder:stores the header files provided by the P2P SDK.
Lib folder:store the library files provided by P2P SDK. (Based on different hardware platforms and system environments)
Sample folder:store the demo program. (Based on different hardware platforms and system environments)

2. P2P SDK overall application architecture

  In order to embed P2P SDK into APP and device firmware, you first need to understand the overall application architecture of P2P SDK. As shown in the figure below, when the P2P SDK is running, there are two internal working modes: passive mode and active mode. For the firmware side of the hardware device, the P2P SDK should be run in passive mode. For the APP side, the P2P SDK should be run in active mode.

  When both ends are working normally, triggered by APP, a P2P transmission channel will be established between the P2P SDKs at both ends. At this time, the P2P SDK on the APP side will work in the local server mode, and the P2P SDK on the device side will work in the local client mode as shown in the figure below.

  In order to realize the data transmission between the APP and the hardware device, the device firmware must work as a server . After the P2P connection is successfully established, the device-side P2P_SDK connects to the firmware server through the socket in client mode. Similarly, the APP must work in client mode . After the P2P connection is successfully established, the APP connects to the local server in the local P2P SDK through the socket in client mode. In the end, P2P SDK realizes the P2P data transmission between APP and hardware devices in the form of a transmission agent.

3. The overall process of P2P SDK interface call

  As shown in the figure above, many of the calling interfaces of the P2P SDK involve network communication. In order to ensure execution efficiency and simplify the complexity of application layer software design, these interface functions are designed to be in non-blocking mode. The form of the function is notified to the upper application asynchronously. The type of these callback functions is defined in the header file of the P2P SDK. The specific implementation needs to be completed by the user application and registered in the P2P SDK through the relevant interface.
  The C2 function interface of the P2P SDK will be used as an example to explain the calling logic of the SDK interface. For the Andriod platform, the SDK interface is packaged in JNI format. For the specific interface description, please refer to the P2PClient.class file in the IvyP2PDemo program and the last part of this article.

4. Key data types of P2P SDK

Type of data Explanation
p2p_transport P2P server connection object. Each connection established between the APP or the device and the P2P server has a p2p_transport connection object corresponding to it.
p2p_transport_cfg typedef struct _p2p_transport_cfg
{
 char* server;// P2P server address
 unsigned short port;// P2P server port
 unsigned char terminal_type; //0: device end, 1: APP end
 char* user;// device UID, APP end ignored this parameter
 char* password;// device, this parameter is ignored end of the APP
 void* user_data; // private data of the user registered as returned by the callback function p2p_transport_cb
 const p2p_transport_cb* cb; // P2P connection state set callback
}p2p_transport_cfg;
p2p_transport_cb typedef struct _p2p_transport_cb
{
 //P2P server connection result callback function.
 //The APP or device initiates a connection request to the P2P server, and the P2PSDK calls this callback function to notify the application of the connection result.
 void (*on_create_complete)(p2p_transport* transport,int status,void* user_data);

 //P2P server connection interruption callback function
//After APP or device side connects to the P2P server successfully, it will always maintain a long connection, if the subsequent network interruption or The server crashes and P2PSDK will call this callback function to notify the application of the interrupt event.
 void (*on_disconnect_server)(p2p_transport* transport, int status, void* user_data);

 //P2P connection result callback function.
 //After the APP initiates a P2P connection request to the device, P2PSDK will call this callback function to notify the application of the P2P connection result.
 void (*on_connect_complete)(p2p_transport* transport, int connection_id, int status, void* transport_user_data, void* connect_user_data);

 //The APP side detects the P2P connection interrupt callback function. (This callback function only needs to be implemented on the APP side)
 //After the P2P connection between the APP and the device side is successful, the two ends maintain the P2P connection. When the P2P connection is interrupted due to network or peer reasons, P2PSDK will call this callback function.
 void (*on_connection_disconnect)(p2p_transport* transport, int connection_id,void* transport_user_data, void* connect_user_data);

 //The callback function of the device side receiving the remote P2P connection request
 //The device side When receiving the P2P connection request from the APP, P2PSDK will call this callback function.
 void (*on_accept_remote_connection)(p2p_transport* transport, int connection_id, void* transport_user_data);

 void (*on_connection_recv)(…); // This interface is obsolete

 //The result callback function of the device-side P2PSDK connection device-side firmware proxy server
 //The device-side firmware must work as a server. The device-side P2PSDK connects to the firmware server in an asynchronous and non-blocking manner. The P2PSDK notifies the application of the connection result through this callback function.
 void (*on_tcp_proxy_connected)(p2p_transport* transport, unsigned short port, char* addr);
}p2p_transport_cb;

5. Detailed description of the device-side function interface

5.1 SDK initialization

Interface definition int p2p_init(LOG_FUNC log_func)
Interface Description The device or APP calls this interface to load the P2P SDK module to complete SDK initialization
Parameter Description LOG_FUNC log_func: implemented by the user and registered a callback function for logging.
The type of this callback function is defined void (*LOG_FUNC)(const char *data, int len)
If the user does not implement this callback function, this parameter can be NULL, and the SDK will output log information through printf
Return value 0: successful execution; other: failed execution
Remarks Both device and APP need to implement this callback function

5.2 Query free P2P servers

Interface definition int p2p_request_dispatch_server(char* user, char* password, char* ds_addr, void* user_data,DISPATCH_CALLBACK cb, void** dispatcher)
Interface Description The device requests a free P2P server from the IvyIoT cloud, and the result of the request is notified to the application in the form of a callback function.
Parameter Description char* user:The UID of the device.
char* password:The key of the device.
char* ds_addr:IvyIoT cloud server address or domain name.
void* user_data:user’s private data, which is finally returned as is through the callback function. The default is NULL
DISPATCH_CALLBACK cb:Register the callback function to process the information returned by the server.
void** dispatcher: After the function is successfully executed, the dispatcher object of this query is returned.
return value 0: successful execution; other: failed execution
Remarks Only device side call

5.3 P2P server query result callback function

Interface definition typedef void (*DISPATCH_CALLBACK)(void* dispatcher, int status, void* user_data, char* server, unsigned short port,unsigned int server_id)
Interface Description This callback function is implemented by the user. The user registers this callback function with P2PSDK when calling p2p_request_dispatch_server() or p2p_query_dispatch_server().
Parameter Description void*dispatcher: The dispatcher object returned by the user when calling the p2p_request_dispatch_server() or p2p_query_dispatch_server() interface.
int status:the result of this query, 0: indicates success, other: indicates query failure.
void* user_data:The private data object entered when the user calls the query interface. The default is NULL.
char* server:P2P server domain name or address returned by the cloud.
unsigned short port:P2P server port
unsigned int server_id:P2P server ID.
Return value void
Remarks Both device and APP need to implement this callback function

5.4 Release dispatcher object

Interface definition void destroy_p2p_dispatch_requester(void* dispatcher)
Interface Description When a user calls the p2p_request_dispatch_server() or p2p_query_dispatch_server() interface, a dispatcher object is generated and output internally.
After the query ends (receives the DISPATCH_CALLBACK callback function or the application waits for a timeout), the application needs to release the dispatcher object through this interface.
Parameter Description void* dispatcher:The dispatcher object returned by the user when calling p2p_request_dispatch_server() or p2p_query_dispatch_server().
Return value void
Remarks Both device and APP need to implement this callback function

5.5 Connect to P2P server

Interface definition int p2p_transport_create(p2p_transport_cfg* cfg, p2p_transport** transport)
Interface Description P2PSDK connects to P2P server
Parameter Description Input parameters:
p2p_transport_cfg* cfg:connection configuration information, including the necessary information required by the P2PSDK to connect to the P2P server, such as P2P server address information.

Output parameters:
p2p_transport** transport:If the interface is called successfully, the p2p_transport connection object is generated. (Save and release by user)
Return value 0: successful execution; other: failed execution
Remarks Both device and APP need to implement this callback function

5.6 P2P server connection result callback function

Interface definition typedef void (*on_create_complete)(p2p_transport* transport, int status, void* user_data)
Interface Description This callback function is implemented by the user. The user registers this callback function when calling p2p_transport_create().
When the P2P SDK internally obtains the result of connecting to the P2P server, the P2P SDK calls this callback function to notify the application of the connection result.
Parameter Description void* transport:P2P server connection object. The p2p_transport object generated by the user when calling p2p_transport_create().
int status:the result of connecting to the P2P server, 0: indicates a successful connection, other: indicates a failed connection.
void* user_data:When the application calls the p2p_transport_create interface, the private data object specified by the p2p_transport_cfg parameter, the default is NULL
Return value void
Remarks Both device and APP need to implement this callback function

5.7 P2P server connection interruption callback function

Interface definition typedef void (*on_disconnect_server)(p2p_transport* transport, int status, void* user_data)
Interface Description The device or APP calls this interface to load the P2P SDK module to complete SDK initialization
Parameter Description void* transport:P2P server connection object. The p2p_transport object generated by the user when calling p2p_transport_create().
int status:the result of connecting to the P2P server, 0: indicates a successful connection, other: indicates a failed connection.
void* user_data:When the application calls the p2p_transport_create interface, the private data object specified by the p2p_transport_cfg parameter, the default is NULL
Return value void
Remarks Both device and APP need to implement this callback function

5.8 Actively disconnect from P2P server

Interface definition void p2p_transport_destroy(p2p_transport *transport)
Interface Description The device or APP actively disconnects from the P2P server and destroys the p2p_transport object.
When the device or APP needs to re-establish a connection with the P2P server, you need to call this interface to release the previous connection
Parameter Description p2p_transport *transport:P2P server connection object, p2p_transport object generated when the user calls p2p_transport_create().
Return value void
Remarks Both device and APP need to implement this callback function

5.9 Callback function for receiving remote P2P connection request

Interface definition typedef void(*on_accept_remote_connection)(p2p_transport *transport, int connection_id, void *transport_user_data)
Interface Description This callback function is implemented by the user. The user registers this callback function when calling p2p_transport_create().
When the device receives a P2P connection request from the far end, the P2P SDK internally notifies the application through this callback function.
*Parameter Description p2p_transport* transport:P2P server connection object. The p2p_transport object generated by the user when calling p2p_transport_create().
int connection_id:P2P connection ID generated when calling p2p_transport_connect().
void* transport_user_data:When calling p2p_transport_create(), the user’s private data object specified by the p2p_transport_cfg parameter is NULL by default.
Return value void
Remarks Only the device side needs to implement this callback function

5.10 SDK deinitialization

Interface definition void p2p_uninit()
Interface Description The device or APP calls this interface to uninstall the P2P SDK module
Parameter Description
Return value void
Remarks This function needs to be called on the device side and the APP side

6. Detailed description of APP function interface

6.1 SDK initialization as same as device side

6.2 Query the P2P server to which the target device belongs

Interface definition int p2p_query_dispatch_server(char* dest_user, char* ds_addr, void* user_data, DISPATCH_CALLBACK cb, void** dispatcher)
Interface Description The APP queries the IvyIoT cloud which P2P server the specified target device currently belongs to, and the result of the query notifies the application in a callback function.
Parameter Description char* user:The UID of the target device.
char* ds_addr:IvyIoT cloud server address or domain name
void* user_data:user’s private data, which is finally returned as is through the callback function. The default is NULL.
DISPATCH_CALLBACK cb:Register a callback function to process the information returned by the server.
void** dispatcher:After the function is successfully executed, the dispatcher object of this query is returned.
Return value 0: successful execution; other: failed execution
Remarks Only the APP needs to call this function

6.3 P2P server query result callback function as same as device side

6.4 Release the dispatcher objectas same as device side

6.5 Connect to P2P serveras same as device side

6.6 P2P server connection result callback functionas same as device side

6.7 P2P server connection interruption callback functionas same as device side

6.8 Actively disconnect from the P2P serveras same as device side

6.9 Initiate a P2P connection request to the device

Interface definition int p2p_transport_connect(p2p_transport transport, char remote_user, void* user_data, int* connection_id)
Interface Description The APP side initiates a P2P connection request to the designated device side.
Parameter Description Input parameters:
p2p_transport* transport:P2P server connection object. The p2p_transport object generated when calling p2p_transport_create().
char* remote_user:the UID of the target device
void* user_data:the user’s private data object, this parameter will be returned as is when the callback function is called.

Output parameters:
int* connection_id:If the call is successful, return a local unique P2P connection ID.
Return value 0: successful execution; other: failed execution
Remarks Only the APP needs to call this function

6.10 P2P connection result callback function

Interface definition P2P connection result callback function
Interface Description This callback function is implemented by the user. The user registers this callback function when calling p2p_transport_create().
When the user calls p2p_transport_connect() and initiates a P2P connection request to the target device, this callback function will be called when the P2P SDK internally obtains the P2P connection result.
Parameter Description p2p_transport* transport:P2P server connection object. The p2p_transport object generated when calling p2p_transport_create().
int connection_id:P2P connection ID generated when calling p2p_transport_connect().
int status:the result of connecting to the P2P server, 0: indicates a successful connection, other: indicates a failed connection.
void* transport_user_data:When calling p2p_transport_create(), the user’s private data object specified by the p2p_transport_cfg parameter is NULL by default.
void* connect_user_data:User private data object specified when calling p2p_transport_connect(), default is NULL.
Return value void
Remarks Only the APP needs to implement this callback function

6.11 P2P connection interrupt callback function

Interface definition typedef void (*on_connection_disconnect)(p2p_transport *transport, int connection_id, void *transport_user_data, void *connect_user_data)
Interface Descriptio This callback function is implemented by the user. The user registers this callback function when calling p2p_transport_create().
When the device receives a P2P connection request from the far end, the P2P SDK internally notifies the application through this callback function.
Parameter Description p2p_transport* transport:P2P server connection object. The p2p_transport object generated when calling p2p_transport_create().
int connection_id:P2P connection ID generated when calling p2p_transport_connect().
void* transport_user_data:When calling p2p_transport_create(), the user’s private data object specified by the p2p_transport_cfg parameter is NULL by default.
void* connect_user_data:User private data object specified when calling p2p_transport_connect(), default is NULL.
Return value void
Remarks Both device and APP need to implement this callback function

6.12 Actively disconnect the specified P2P connection

Interface Descriptio void p2p_transport_disconnect(p2p_transport *transport, int connection_id)
Interface Description The APP side or the device side calls this interface to actively disconnect the specified P2P connection
Parameter Description p2p_transport *transport:P2P server connection object. The p2p_transport object generated when calling p2p_transport_create().
int connection_id:P2P connection ID generated when calling p2p_transport_connect()
Return value 0: successful execution; other: failed execution
Remarks Only the APP needs to call this function

6.13 Create a local proxy service

Interface Descriptio int p2p_create_tcp_proxy(p2p_transport transport, int connection_id, unsigned short remote_listen_port, unsigned short local_proxy_port)
Interface Description APP needs to call this interface to create a proxy service inside the P2PSDK, and then the APP connects to the proxy service inside the SDK in client mode.
Parameter Description Input parameters:
p2p_transport* transport:P2P server connection object. The p2p_transport object generated when calling p2p_transport_create().
int connection_id:P2P connection ID generated when calling p2p_transport_connect().
unsigned short remote_listen_port:Specifies the listening port of the SDK internal proxy service.
Output parameters:
unsigned short* local_proxy_port:the listening port where the internal proxy service actually takes effect.
Return value 0: successful execution; other: failed execution
Remarks Only the APP needs to call this function

6.14 Destroy the local proxy service

Interface Descriptio int p2p_destory_tcp_proxy(p2p_transport transport, int connection_id, unsigned short local_proxy_port)
Interface Description APP calls this interface to destroy the proxy service inside P2P SDK.
Parameter Description p2p_transport* transport:P2P server connection object. The p2p_transport object generated when calling p2p_transport_create().
int connection_id:P2P connection ID generated when calling p2p_transport_connect().
unsigned short* local_proxy_port:The listening port currently used by the internal proxy service.
Return value 0: successful execution; other: failed execution
Remarks Only the APP needs to call this function

6.15 SDK deinitializationas same as device side

7 Detailed description of the Andriod platform interface

For the Andriod platform, the P2P SDK uses JNI to encapsulate into a java interface class. The specific interfaces are as follows:

public class P2PClient {
    public P2PClient() {
    }

    public static native int init(); // SDK initialization
    public static native void uninit(); // SDK deinitialization

    // Connect P2P server
    private static native P2PResult p2p_transport_create(
                        String server, // P2P server address
                        int port, // P2P server port
                        String user, // device UID
                        String password, // device KEY
                        int terminal_type, // APP:1
                                            // device:0
                        int user_data, // default is 0
                        int use_tcp_connect_srv, //default is 0
                        String proxy_addr, // default is 0
                        Object cb); // callback object

    // Connect P2P server
    public static P2PResult p2p_transport_create(p2p_transport_cfg cfg) {
            return p2p_transport_create(cfg.server, cfg.port, cfg.user,
                            cfg.password, cfg.terminal_type, cfg.user_data,
                            cfg.use_tcp_connect_srv, cfg.proxy_addr, cfg.cb);
    }

    // actively disconnect from P2P server
    public static native void p2p_transport_destroy(int transport);

    //  initiate a P2P connection request to the device side
    public static native P2PResult p2p_transport_connect(
                                    int transport, 
                                    String user, 
                                    int user_data); 

    // actively disconnect the specified P2P connection
    public static native void p2p_transport_disconnect(int transport, int conn_id);

    // create a local proxy service
    public static native P2PResult p2p_create_tcp_proxy(int transport, 
                                        int conn_id, int remote_listen_port);

    // destroy the local proxy service
    public static native void p2p_destroy_tcp_proxy(int transport, 
                                        int conn_id, int local_proxy_port);

    //  query free P2P Server
    public static native int p2p_request_dispatch_server(
                                                    String user,
                                                    String password,
                                                    String ds_addr,
                                                    int user_data,
                            P2PClient.P2PClientDispatchCallBack cb);

    //  Query the P2P server to which the target device belongs。
    public static native int p2p_query_dispatch_server(
                                                String dest_user,
                                                String ds_addr,
                                                int user_data,
                            P2PClient.P2PClientDispatchCallBack cb);

    public static native int p2p_conn_set_buf_size(int transport, int conn_id, int opt, int size);

    // callback object
    public interface P2PClientCallBack {
        void on_create_complete(int transport, int status, int user_data);

void on_connect_complete(int transport,
                                int connection_id,
                                int status,
                                int transport_user_data,
                                int connect_user_data);

        void on_connection_disconnect(int transport,
                                    int connection_id,
                                    int transport_user_data,
                                    int connect_user_data);

        void on_accept_remote_connection(int transport,
                                        int connection_id,
                                        int transport_user_data);
    }

    public interface P2PClientDispatchCallBack {
        void on_dispatch_result(int status,
                                int user_data,
                                String server,
                                int port,
                                int server_id);
    }
}
文档更新时间: 2020-06-30 17:44   作者:admin