P2P Capabilities

Last Updated on : 2024-04-03 07:13:49download

In traditional solutions, a server forwards data between an app and a device. During peak hours, this transmission mode consumes a high number of space and traffic resources on the service and results in costly business operations. Tuya’s peer-to-peer (P2P) connection solution is an ideal option to resolve this issue and achieve the best price performance. The class ThingSmartP2pChannel is implemented for this purpose.

Integrate with service

Add the following content to the Podfile:

pod 'ThingSmartP2pChannelKit'

Initialize the SDK

This initialization must be bound with the current user ID (UID). The initialization API method must work together with the deinitialization API method for the same P2P resource.

API description

- (void)initSDKWithUid:(NSString *)uid;

Parameters

Parameter Description
uid The user’s ID. The value for a logged-in account can be obtained in the query of ThingSmartUser.uid.

Example

NSString *uid = [ThingSmartUser sharedInstance].uid;
[[ThingSmartP2pChannel sharedInstance] initSDKWithUid:uid]

Deinitialize the SDK

Releases P2P internal resources if P2P capabilities are unused. Then, to use P2P capabilities again, initialize the resources.

API description

- (void)deInitSDK;

Create a P2P connection

Creates a P2P session with a specific device. This feature is supported by App SDK v3.4.8.

API description

- (NSString *)connectWithDeviceId:(NSString *)devId mode:(ThingSmartP2pConnectMode)mode timeout:(NSInteger)timeout success:(void(^)(int p2pHandle))success failure:(void(^)(NSInteger errCode))failure;

Parameters

Parameter Description
devId The unique identifier for the device.
mode The prioritized connection mode.
timeout The timeout value. Unit: milliseconds.
success The success callback. The ID of the P2P session is returned. If a negative number is returned, the connection attempt failed.
failure The failure callback.

ThingSmartP2pConnectMode

Enum value Description
ThingSmartP2pConnectModeInternet Internet connections prioritized
ThingSmartP2pConnectModeLAN LAN connections prioritized

Return values

Return value Description
traceId The value of traceId identifies and tracks the lifecycle of a connection. To close a connection, traceId is required.

Cancel an ongoing connection attempt

Cancels an ongoing connection attempt. The existing connections are not affected.

API description

- (void)breakConnectWithTraceId:(NSString *)traceId;

Parameters

Parameter Description
traceId The value of traceId returned for the created P2P connection.

Close a connection

Closes a P2P session from a specific device.

API description

- (void)disconnectWithP2pHandle:(int)handle;

Parameters

Parameter Description
handle The ID of the P2P session to be closed. The ID is returned when the connection is created.

Receive data

Reads data from a specific channel through which a P2P session is created.

API description

- (int)readDataWithP2pHandle:(int)handle channel:(int)channel buffer:(unsigned char*)buffer timeout:(int)timeout;

Parameters

Parameter Description
handle The ID of the P2P session.
channel The channel number.
buffer The buffer that receives data.
timeout The timeout value. Unit: milliseconds.

Return values

Return value Description
< 0 An error has occurred. A value of -3 indicates a timeout error. Other values indicate that the connection is closed.
≥ 0 The length of the received data.

Send data to the peer

Sends data to a specific channel through which a P2P session is created.

API description

- (int)sendDataWithP2pHandle:(int)handle channel:(int)channel buffer:(unsigned char*)buffer length:(int)len timeout:(int)timeout;

Parameters

Parameter Description
handle The ID of the P2P session.
channel The channel number.
buffer The pointer to the data to be sent.
len The length of the buffer.
timeout The timeout value. Unit: milliseconds.

Return values

Return value Description
< 0 An error has occurred. A value of -3 indicates a timeout error. Other values indicate that the connection is closed.
≥ 0 The length of the transmitted data.

Check the connection status

Checks the status of the current P2P session. Polling operations are performed to implement the check.

API description

- (int)activeCheckWithHandle:(int)p2pHandle;

Parameters

Parameter Description
p2pHandle The ID of the P2P session.

Return values

Return value Description
< 0 The connection is closed.
≥ 0 The connection is normal.

Query the P2P SDK version number

API description

@property (nonatomic, assign, readonly) NSString *p2pVersion;

Channel numbers

In the existing data protocols for Powered by Tuya (PBT) devices, eight data channels are initialized for each P2P session and six of these channels are defined and used.

Channel number Description
0 The channel to send and receive control commands.
1 The channel to transmit video data.
2 The channel to transmit audio data.
3 The channel to play back and transmit video footage on an SD card.
4 The data pass-through channel.
5 The channel to download and transmit files.

Non-Tuya enabled login system

If you use a login system that is not implemented by Tuya when you integrate with P2P capabilities, the SDK cannot listen for the login status of users. In this case, you must initialize initP2PSDKWithUid: and deinitialize deInitP2PSDK by yourself during successful login and logout.

Initialize P2P module

Initializes the P2P module. This API method is bound with users’ UID. We recommend that you call it after login.

API description

- (void)initP2PSDKWithUid:(NSString *)uid;

Parameters

Parameter Description
uid The user ID (UID).

Example

ThingSmartP2pChannel *p2pChannel = [ThingSmartP2pChannel sharedInstance];
[p2pChannel initP2PSDKWithUid:uid];

Deinitialize P2P module

We recommend that you deinitialize the P2P module during logout. Otherwise, P2P messages might be disordered.

API description

- (void)deInitP2PSDK;

Example

ThingSmartP2pChannel *p2pChannel = [ThingSmartP2pChannel sharedInstance];
[p2pChannel deInitP2PSDK];

Error codes

For more information, see Error Codes.