Bluetooth Beacon Mesh Software

Last Updated on : 2024-07-26 03:49:56download

TuyaOS is designed with a modular approach, with capabilities covering basic services, security, networking middleware, and various IoT services. Built on top of TuyaOS capabilities, TuyaOS Bluetooth Development Framework is a set of SDKs used to build Bluetooth products for different specifications and scenarios. Unified APIs and a variety of components allow you to focus on building applications with a consistent development experience, without taking care of specific implementations.

Software architecture

Bluetooth Beacon Mesh Software

The diagram above shows that the TuyaOS Bluetooth beacon mesh development kit is divided into four layers. We will now review each layer, starting from the bottom.

  • The Tuya Kernel Layer (TKL) consists of Tuya-specific standard TKL APIs and chip vendor SDKs. The TKL provides unified APIs for the upper layer and adapts to different chip platforms. The adaptation development can be carried out by Tuya or chip vendors.

  • The Tuya Abstraction Layer (TAL) and Service Layer, the core parts of the development kit, consist of various components. The basic services include pairing, data communication, and system management.

  • The Application Layer covers various fields including lights, electrical products, sensors, remotes, and wireless switches. Besides the standard product routines, you can build with the standard demo projects without altering the code, or implement complex features on top of the demo.

Run process

Bluetooth Beacon Mesh Software

For more information, see Device Initialization.

Development kit directory

Bluetooth Beacon Mesh Software

Demo

➜ tuyaos_demo_beaconmesh_peripheral_phy6252 git:(master) ✗ tree
.
| -- app_config.h
| -- include
| `-- app_led.h
| -- src
| | -- app_led.c
| `-- main.c
`-- tuya_iot.config

This basic demo project allows you to test almost all the beacon mesh development kit features and develop complex applications accordingly.

main.c

Demo application initialization, beacon mesh data transmission/reception, and status callbacks. The demo uses a cool white light to describe how to use the beacon mesh SDK.

app_led.c

The business logic for the cool white light, including the device reset method.

Components

➜ components git:(master) ✗ tree -L 1
.
| -- tal_sdk_test
`-- tal_xfifo

➜ libs git:(master) ✗ tree
.
| -- libtal_beacon2.lib
`-- libtal_xxtea.lib

tal_sdk_test

Components for peripheral testing, including the test code.

tal_gpio_test

Implementation of the GPIO testing feature for the Tuya production tool.

tal_xfifo

A simple multi-instance queue component.

libtal_beacon2.lib

Beacon mesh stack library.

libtal_xxtea.lib

A simple encryption algorithm library.

Vendor

➜ phy6252_adv git:(master) ✗ tree -L 2
.
| -- sdk                     # Chip vendor SDK
| `-- phy6252_adv_sdk
| -- toolchain               # Flashing tool and project template from chip vendor
| | -- software
| `-- templates
| -- tuyaos                  # Various types of adaptations
| | -- bluetooth               # Bluetooth beacon transceiver adaptation
| | -- drivers                 # Peripheral drivers
| `-- include                 # board.h header file
| -- README.md
| -- prepare.py
| -- release_note.md
`-- toolchain.yaml

The vendor directory stores the development environment, including chip vendor SDKs, adaptation layers, and common header files. It is maintained by Tuya and chip vendors.

Chip vendor SDK

The chip vendor SDK is developed by Tuya based on the open routines or adapted by chip vendors.

Adaptation layers

APIs for accessing Bluetooth, peripheral drivers (such as GPIO, PWM, ADC, I2C, and SPI), system drivers (such as memory, OTA, and sleep), secure encryption and hash algorithms (AES and MD5), and common tools.

Common header files

To enable a single codebase above the TKL to run across different platforms, platform-specific code such as flash address and peripheral pins is stored in board.h as macro definitions.

Header files

Bluetooth Beacon Mesh Software
  • tuya_error_code.h: Definitions for error codes.

  • tuya_iot_config: Definitions for system configuration and component enablement or configuration.

Common APIs

Initialization

tuya_init_first()

Initializes basic peripherals, configurations, and memory.

tuya_init_second()

Initializes logging, software timer, and Bluetooth.

tuya_init_third()

Initializes complex peripherals and peripheral components.

tuya_init_last()

Initializes the Bluetooth pairing protocol and the test code, and enables Bluetooth adverting.

After this API is executed, the main loop will be run.

ty_beacon2_node_init

Initializes the beacon mesh stack.

ty_beacon_remoter_init

Initializes the beacon mesh remote.

Main loop

tuya_main_loop()

tuya_main_loop() is a callback within the main loop, allowing you to inject custom operations into it.

Note: This API is intended for debugging and validation purposes. Use it with caution. Excessive time slices used by this API can compromise the stability of the entire framework.

Event callback

ty_beacon2_event_cb

Beacon mesh event callbacks include successful pairing, reset, network recovery, adding or removing groups, and binding or unbinding a remote.

Receive data

app_dps_download

The callback triggered when the beacon mesh device receives DP commands from a mobile phone, gateway, or remote.

void app_dps_download(u8 dpid, u8 dpty, u8 dplen, u8 *dpvalue, beacon_frame_s *p_frame){
    PR_DEBUG("dpid=%d dpty=%d, dplen=%d, dpvalue=[",dpid,dpty,dplen);
    PR_DEBUG_HEX_ARRAY(dpvalue,dplen);
    PR_DEBUG_RAW("]\n");
    switch(dpid){
        case 0x01://onoff
        case 101:
            app_led_onoff(dpvalue[0]);
            break;
        case 0x02://mode:white,color,scene,music
            break;
        default:
            break;
    }
}

Send data

Function u8 frame_send(u8 head, u16 dst, u8 subcmd, u8* payload, u8 *key, u8 ttl); Packet
Purpose Send the payload data to the destination address, with the time to live (TTL) up to 3.

The demo application describes how to use this basic DP data sending function to encapsulate application functions for transmitting different types of DP data and heartbeats.

When there is an ongoing transmission, calling this function will interrupt the current packet transmission and initiate a new one.
/
Request parameter u8 head The head.
-> u16 dst The destination address.
-> u8 subcmd The subcommand.
-> u8* payload The payload.
-> u8 *key The secret key.
-> u8 ttl The TTL.

Example:

Report DP status

u8 payload[16];
memset(payload,0,16);
payload[0] = 1;//dp id = 1
payload[1] = 0x11;//dp kind = 1; dp len = 1 (each 4 bits)
payload[2] = led_onoff;//dp value
frame_send(0x08, 0x8000, 0x0A, payload, beacon_dev.beaconkey, 3);

Note: The mobile phone stays in the receiving window for a specified period after sending a command to the device. If reporting to the gateway is not supported, the device will be restricted by a timed reporting window. There is no such limitation for the gateway.

Device reset

Function u8 ty_beacon2_node_reset(u8 is_not_recovery, u32 pair_timeout_us) -
Purpose The system will enter pairing mode. -
Request parameter u8 is_not_recovery Specify whether to restore the device to the previous pairing status if pairing times out.
-> u32 pair_timeout_us The pairing timeout, in microseconds.

Enable pairing

Function u8 ty_beacon2_node_pair_start(u32 timeout_us) -
Purpose The system will enter pairing mode. -
Request parameter u32 timeout_us Set pairing timeout

Get pairing state

You can check the pairing status with beacon_dev.state.

  • #define STATE_NOT_PAIRED 0
  • #define STATE_PAIRED 1
  • #define STATE_PAIRING 2 (leave it as is)

A beacon mesh device can be either in being paired or paired status.

  • Calling the start pairing or reset device function will change the device status from paired to being paired.
  • Pairing timeout will change the device status from being paired to uncontrollable paired.

Common features

Logging

The demo has logging turned off by default. You can set #define APP_LOG_ENABLE 1 in tuya_iot_config.h to enable log output through the API in tkl_uart. The components compiled into a lib do not have logs. To debug them, contact Tuya’s technical support.

Note: Focus specifically on these two platforms for logging.

  • PHY6230 platform: Open your project in .log using CDK and then the project settings. Click the Compiler tab and change _DEF_DEBUG_INFO_ in Define(-D) to 3.
  • PHY6252 follows the functions in tuya_iot_config.h.

Drivers

TuyaOS Kernel Layer (TKL) provides a set of standard driver APIs required to run a minimum viable TuyaOS product. Note that not all drivers have the TKL API.

If the driver you need in the TKL is not implemented or not available, you can directly use the API provided by the chip vendor.