TuyaOS Link SDK Migration

Last Updated on : 2022-11-24 09:20:05download

The topic describes how to modify the source files and functions in the TuyaOS Link SDK. To run in various embedded C-based environments, such as real-time OS and embedded Linux, the SDK must be adjusted to use a specific TLS as available with specific hardware platforms. Finally, the devices can be connected to the Tuya Cloud to meet business application needs.

SDK description

The C-code file of this SDK is provided through the following directory structure. For more information, see the note behind the folder name.

The SDK directory structure is as follows:

  • certs: Device private key, device certificate, and server’s CA root certificate
  • docs: Development documentation
  • external_libs: External dependent libraries - cJSON, mbedTLS
  • include: Necessary migration interfaces for the platform, and SDK function interfaces
  • src: Source code to implement the SDK
  • platform: Interface adaptation for platform migration
  • examples: Routine

All makefiles in this SDK are configured by using the documented folder structure above, so you shall modify the makefile to move or rename the folder.

Integrate the SDK into your platform

This section describes the API calls that shall be implemented in order for the Link SDK to run on your platform. The Link SDK interfaces follow the driver model, where only the prototypes are defined by the Link SDK itself while the implementation is delegated to the Link SDK user who adjusts it to the platform used. The following sections list the functionality required for the device SDK to run successfully on any given platform.

System

  • void* system_malloc(size_t n);: Allocate the required memory space and return a pointer to it.

  • void* system_calloc(size_t n, size_t size);: Allocate the required memory space, return a pointer to it, and initialize the allocated memory to zero.

  • void system_free(void *ptr);: Release the memory space allocated by previously calling the system_malloc, system_calloc, or system_realloc.

  • uint64_t system_ticks();: System millisecond tick count.

  • uint32_t system_timestamp();: Get the timestamp.

Network

The SDK shall interact with the server through the MQTT and HTTP protocols based on TLS connections. Your platform shall have a TCP/IP protocol stack to implement the following APIs. The SDK includes the following interface examples depending on the mbedTLS library.

  • int iot_tls_init(Network *pNetwork, char *pRootCALocation, char *pDeviceCertLocation, char *pDevicePrivateKeyLocation, char *pDestinationURL, uint16_t DestinationPort, uint32_t timeout_ms, bool ServerVerificationFlag);: Initialize the TLS network connection management structure object.

  • int iot_tls_connect(Network *pNetwork, TLSConnectParams *TLSParams);: Use the credentials provided through the Network API call to create a TLS TCP socket to the configuration address.

  • int iot_tls_connect_async(Network *pNetwork, TLSConnectParams *params);: Use the credentials provided through the Network API call to implement non-blocking asynchronous connection interfaces.

  • int iot_tls_write(Network*, unsigned char*, size_t);: Write to the TLS network buffer.

  • int iot_tls_disconnect(Network *pNetwork);: Disconnect from TLS.

  • int iot_tls_destroy(Network *pNetwork);: Release the TLS connection context.

  • int iot_tls_is_connected(Network *pNetwork);: Check whether the TLS is connected.

Data persistence

The SDK needs to persistently store some configuration information in your device during operation so that the platform shall provide persistent KV interfaces.

  • int local_storage_set(const char* key, const uint8_t* buffer, size_t length);: Write the data to the KV system.

  • int local_storage_get(const char* key, uint8_t* buffer, size_t* length);: Read the data from the KV system.

  • int local_storage_del(const char* key);: Delete the data from the KV system.