Device Binding over LAN

Last Updated on : 2023-10-25 09:22:18download

The mobile app sends the token to the device on the same LAN. The device then completes the binding and activation process after receiving the token. This solution applies to devices that can complete the pairing process independently.

Process

DeviceRouterAppCloudWired or wireless connectionReturn the IP address afterbeing connected.Connect.Connected.Listen for advertisingpackets.Send advertising frames.Forward advertising frames.Initiate a connection request.Connected.Get the token.Return the token.Send the token.Received.Query device informationregularly.Get registered and activated.Activated.Return the activateddevices.DeviceRouterAppCloud

Development guide

Prerequisites

  • The mobile app and the device are on the same LAN.
  • The local ports 6667, 6668, and 7000 are not restricted on the device.
  • The router can access the WAN.

Device development

  • Set the pairing mode to wired.

    connect_mode = PARING_MODE_WIRED
    
    static OPERATE_RET __TUYA_IPC_SDK_START(TUYA_IPC_PARING_MODE_E connect_mode, CHAR_T *p_token)
    {
        printf("SDK Version:%s\r\n", tuya_ipc_get_sdk_info());
        TUYA_IPC_LINK_TYPE_E link_type = TUYA_IPC_LINK_WIRE;
    TUYA_IPC_SDK_RUN_VAR_S ipc_sdk_run_var ={0};
    memset(&ipc_sdk_run_var,0,sizeof(ipc_sdk_run_var));
    
    /*certification information(essential)*/
    strcpy(ipc_sdk_run_var.iot_info.product_key,s_ipc_pid);
    strcpy(ipc_sdk_run_var.iot_info.uuid,s_ipc_uuid);
    strcpy(ipc_sdk_run_var.iot_info.auth_key,s_ipc_authkey);
    strcpy(ipc_sdk_run_var.iot_info.dev_sw_version,s_app_version);
    strcpy(ipc_sdk_run_var.iot_info.cfg_storage_path,s_ipc_storage);
    //normal device
    ipc_sdk_run_var.iot_info.dev_type = NORMAL_POWER_DEV;
    //if needed, change to low power device
    //ipc_sdk_run_var.iot_info.dev_type= LOW_POWER_DEV;
    
    /*connect mode (essential)*/
    ipc_sdk_run_var.net_info.connect_mode = connect_mode;
    ipc_sdk_run_var.net_info.ipc_status_change_cb = TUYA_IPC_Status_Changed_cb;
        ipc_sdk_run_var.net_info.link_type = link_type;
        printf("MODE:%d  LINK_TYPE:%d\r\n", connect_mode, ipc_sdk_run_var.net_info.link_type);
    if(p_token)
    {
        strcpy(ipc_sdk_run_var.debug_info.qrcode_token,p_token);
    }
    
  • Set the connection mode to wired.

    TUYA_IPC_LINK_TYPE_E link_type = TUYA_IPC_LINK_WIRE;
    ipc_sdk_run_var.net_info.link_type = link_type;
    
  • Adapt tkl_wired.c. The development framework provides a set of APIs for Linux. You need to adapt the implementation to your specific hardware.
    Take care of the following APIs.

    • tkl_wired_set_status_cb

      OPERATE_RET tkl_wired_set_status_cb(TKL_WIRED_STATUS_CHANGE_CB cb);
      

      Notify the SDK of changes in network status through a callback.
      For example, when Ethernet is disconnected, cb(TKL_WIRED_LINK_DOWN) is invoked. When Ethernet is connected, with an IP address assigned, cb(TKL_WIRED_LINK_UP) is invoked.
      If the mobile app cannot discover the device on the LAN, or if the device does not send advertising packets, check if the SDK receives TKL_WIRED_LINK_UP.

    • tkl_wired_get_status

      The SDK calls this interface to request the connection status of the network interface. The assignment of an IP address is the key factor in determining the network status. If the network interface has been activated with an IP address assigned, TKL_WIRED_LINK_UP is returned. Otherwise, TKL_WIRED_LINK_DOWN is returned.

    • tkl_wired_get_ip

      The SDK calls this interface to request the IP address of the network interface to set the socket address. If the gateway has multiple Ethernet ports, the network interface of the returned IP address is used for communication. Typically, the network interface connected to the internet is used. Be sure to return the correct IP address based on your scenario.

  • API call sequence

Start pairing
Call tkl_wifi_init
Call tkl_wifi_station_get_status
Call tkl_wifi_get_mac
Call tkl_wired_get_ip
Call tkl_wifi_set_work_mode
Call tkl_wifi_get_work_mode
Call tkl_wired_set_status_cb
Call tkl_wired_get_status
Call tkl_wired_get_ip
Proactively notify the SDK of the wired
connection status through TKL_WIRED_STATUS_CHANGE_CB
SDK completes cloud registration and activation
Get notified of the online status through ipc_status_change_cb
End pairing

FAQs

Why can’t the mobile app find the device?

  1. Check if the mobile app and the device are on the same LAN.
  2. Check if the pairing mode and connection mode are set correctly.
  3. Check if tuya_ipc_init_sdk and tuya_ipc_start_sdk work properly.
  4. Check if all the required TKL APIs are adapted and if the SDK receives the TKL_WIRED_LINK_UP status.
  5. Check if the local ports 6667, 6668, and 7000 are restricted.
  6. If the logging level is set to TY_LOG_LEVEL_DEBUG, verify if lan init success is logged.
  7. Analyze the packet capture.

Why does the mobile app fail to add a device it has found?

  1. Check if the mobile app can access the internet.
  2. Check the value of PID, UUID, and AUTHKEY.

How can I enable a wireless device to support binding over LAN?

  • Set the pairing mode to: PARING_MODE_WIRED.
  • Set the connection mode to: TUYA_IPC_LINK_WIRE.
  • Both tkl_wifi.c and tkl_wired.c must be adapted.

How can I enable a device with both wired and wireless capabilities to support binding over LAN?

  • Set the pairing mode to: PARING_MODE_WIRED.
  • Set the connection mode to: TUYA_IPC_LINK_WIRE.
  • Set wired pairing as the default pairing mode with tuya_svc_netmgr_linkage_set_default(LINKAGE_TYPE_WIRED).

Why is unreg not found being logged continuously?

  1. Check if all the required tkl_queue_create_init APIs in tkl_system are adapted. Verify if the queue API fits your platform.
  2. Check if the adapted tkl_system is compiled into your code.
  3. Review other TKL API adaptations.