Skip to content

Commit

Permalink
[tcat] Implement tcat advertisement.
Browse files Browse the repository at this point in the history
Commit introduces implementation of TCAT advertisement over BLE compialnt with
Thread 1.3.1 specification.
  • Loading branch information
Przemyslaw Bida committed May 9, 2024
1 parent 74573b5 commit 2e0fa63
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 47 deletions.
15 changes: 15 additions & 0 deletions examples/platforms/simulation/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,18 @@ OT_TOOL_WEAK void otPlatBleGattServerOnWriteRequest(otInstance *aIns
* which is available in FTD/MTD library.
*/
}

otBleLinkCapabilities otPlatGetBleLinkCapabilities(otInstance *aInstance)
{
OT_UNUSED_VARIABLE(aInstance);
otBleLinkCapabilities dummy = {0};
return dummy;
}

otError otPlatBleGapAdvSetData(otInstance *aInstance, uint8_t *aAdvertisementData, uint16_t aAdvertisementLen)
{
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aAdvertisementData);
OT_UNUSED_VARIABLE(aAdvertisementLen);
return OT_ERROR_NOT_IMPLEMENTED;
}
13 changes: 12 additions & 1 deletion include/openthread/ble_secure.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ otError otBleSecureStart(otInstance *aInstance,
bool aTlvMode,
void *aContext);

/**
* Sets TCAT vendor info
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aVendorInfo A pointer to the Vendor Information (must remain valid after the method call.
*
* @retval OT_ERROR_NONE Successfully set value.
* @retval OT_ERROR_INVALID_ARGS Value not set.
*/
otError otBleTcatSetVendorInfo(otInstance *aInstance, const otTcatVendorInfo *aVendorInfo);

/**
* Enables the TCAT protocol over BLE Secure.
*
Expand All @@ -122,7 +133,7 @@ otError otBleSecureStart(otInstance *aInstance,
* @retval OT_ERROR_INVALID_STATE The BLE function has not been started or line mode is not selected.
*
*/
otError otBleSecureTcatStart(otInstance *aInstance, const otTcatVendorInfo *aVendorInfo, otHandleTcatJoin aHandler);
otError otBleSecureTcatStart(otInstance *aInstance, otHandleTcatJoin aHandler);

/**
* Stops the BLE Secure server.
Expand Down
2 changes: 1 addition & 1 deletion include/openthread/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (411)
#define OPENTHREAD_API_VERSION (412)

/**
* @addtogroup api-instance
Expand Down
45 changes: 45 additions & 0 deletions include/openthread/platform/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ extern "C" {

#define OT_BLE_DEFAULT_POWER 0

/**
* TOBLE service UUID
*/

#define OT_TOBLE_SERVICE_UUID 0xfffb

/**
* Represent BLE link capabilities
*
*/
typedef struct otBleLinkCapabilities
{
uint8_t mRsv : 6;
bool mL2CapDirect : 1;
bool mGattNotifications : 1;
} otBleLinkCapabilities;

/**
* Represents a BLE packet.
*
Expand Down Expand Up @@ -152,6 +169,26 @@ otError otPlatBleDisable(otInstance *aInstance);
* @section Bluetooth Low Energy GAP.
***************************************************************************/

/**
* Starts BLE Advertising procedure.
*
* The BLE device shall use undirected advertising with no filter applied.
* A single BLE Advertising packet must be sent on all advertising
* channels (37, 38 and 39).
*
* @note This function shall be used only for BLE Peripheral role.
*
* @param[in] aInstance The OpenThread instance structure.
* @param[in] aAdvertisementData The formatted TCAT advertisement frame.
* @param[in] aAdvertisementLen The TCAT advertisement frame length.
*
* @retval OT_ERROR_NONE Advertising procedure has been started.
* @retval OT_ERROR_INVALID_STATE BLE Device is in invalid state.
* @retval OT_ERROR_INVALID_ARGS Invalid interval value has been supplied.
*
*/
otError otPlatBleGapAdvSetData(otInstance *aInstance, uint8_t *aAdvertisementData, uint16_t aAdvertisementLen);

/**
* Starts BLE Advertising procedure.
*
Expand Down Expand Up @@ -281,6 +318,14 @@ otError otPlatBleGattServerIndicate(otInstance *aInstance, uint16_t aHandle, con
*/
extern void otPlatBleGattServerOnWriteRequest(otInstance *aInstance, uint16_t aHandle, const otBleRadioPacket *aPacket);

/**
* Function to retrieve from platform BLE link capabilities.
*
* @param[in] aInstance The OpenThread instance structure.
*
*/
otBleLinkCapabilities otPlatGetBleLinkCapabilities(otInstance *aInstance);

/**
* @}
*
Expand Down
21 changes: 20 additions & 1 deletion include/openthread/tcat.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ extern "C" {
#define OT_TCAT_MAX_SERVICE_NAME_LENGTH \
15 ///< Maximum string length of a UDP or TCP service name (does not include null char).

#define OT_TCAT_ADVERTISEMENT_MAX_LEN 29 ///< Maximum length of TCAT advertisement.
#define OT_TCAT_VERSION 0x5 ///< TCAT version number.
#define OT_TCAT_OPCODE 0x2 ///< TCAT Operation Code.
#define OT_TCAT_MAX_VENDORID_SIZE 6 ///< TCAT max size of vendor ID including null as termination.

/**
* Represents TCAT status code.
*
Expand Down Expand Up @@ -111,6 +116,19 @@ typedef enum otTcatCommandClass

} otTcatCommandClass;

/**
* Represents Device ID type.
*
*/
typedef enum otTcatDeviceIdType
{
OT_TCAT_DEVICE_ID_EMPTY = 0, ///< Vendor device ID type not set
OT_TCAT_DEVICE_ID_OUI24 = 1, ///< Vendor device ID type OUI24
OT_TCAT_DEVICE_ID_OUI36 = 2, ///< Vendor device ID type OUI36
OT_TCAT_DEVICE_ID_DISCRIMINATOR = 3, ///< Vendor device ID type Discriminator
OT_TCAT_DEVICE_ID_IANAPEN = 4, ///< Vendor device ID type IANA PEN
} otTcatDeviceIdType;

/**
* This structure represents a TCAT vendor information.
*
Expand All @@ -127,14 +145,15 @@ typedef struct otTcatVendorInfo
const char *mPskdString; ///< Vendor managed pre-shared key for device
const char *mInstallCode; ///< Vendor managed install code string
const char *mDeviceId; ///< Vendor managed device ID string (if NULL: device ID is set to EUI-64 in binary format)
otTcatDeviceIdType mDeviceIdType; ///< Vendor managed device id type

} otTcatVendorInfo;

/**
* Pointer to call when application data was received over a TCAT TLS connection.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aMessage A pointer to the message.
* @param[in] aMessagotTcatVendorIdTypee A pointer to the message.
* @param[in] aOffset The offset where the application data begins.
* @param[in] aTcatApplicationProtocol The protocol type of the message received.
* @param[in] aServiceName The name of the service the message is direced to.
Expand Down
73 changes: 69 additions & 4 deletions src/cli/cli_tcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
#include "cli/cli_utils.hpp"

#include "cli/cli_tcat.hpp"
#include "common/code_utils.hpp"

#include <openthread/ble_secure.h>

#include <mbedtls/oid.h>
#include <openthread/error.h>
#include <openthread/tcat.h>
#include <openthread/platform/ble.h>

Expand Down Expand Up @@ -79,8 +81,10 @@ namespace ot {

namespace Cli {

const char kPskdVendor[] = "J01NM3";
const char kUrl[] = "dummy_url";
otTcatVendorInfo sVendorInfo;
const char kPskdVendor[] = "J01NM3";
const char kUrl[] = "dummy_url";
static char sVendorId[OT_TCAT_MAX_VENDORID_SIZE];

static void HandleBleSecureReceive(otInstance *aInstance,
const otMessage *aMessage,
Expand Down Expand Up @@ -109,6 +113,66 @@ static void HandleBleSecureReceive(otInstance *aInstance,
IgnoreReturnValue(otBleSecureFlush(aInstance));
}

template <> otError Tcat::Process<Cmd("vendorid")>(Arg aArgs[])
{
otError error = OT_ERROR_NONE;
static const char *const kVendorIdTypes[] = {"empty", "oui24", "oui36", "discriminator", "ianapen"};

if (aArgs[0].IsEmpty())
{
if (sVendorInfo.mDeviceId != nullptr)
{
OutputLine("type %s, value: %s", kVendorIdTypes[sVendorInfo.mDeviceIdType], sVendorInfo.mDeviceId);
}
else
{
OutputLine("%s", kVendorIdTypes[OT_TCAT_DEVICE_ID_EMPTY]);
}
ExitNow();
}

if (aArgs[0] == kVendorIdTypes[OT_TCAT_DEVICE_ID_OUI24])
{
sVendorInfo.mDeviceIdType = OT_TCAT_DEVICE_ID_OUI24;
}
else if (aArgs[0] == kVendorIdTypes[OT_TCAT_DEVICE_ID_OUI36])
{
sVendorInfo.mDeviceIdType = OT_TCAT_DEVICE_ID_OUI36;
}
else if (aArgs[0] == kVendorIdTypes[OT_TCAT_DEVICE_ID_DISCRIMINATOR])
{
sVendorInfo.mDeviceIdType = OT_TCAT_DEVICE_ID_DISCRIMINATOR;
}
else if (aArgs[0] == kVendorIdTypes[OT_TCAT_DEVICE_ID_IANAPEN])
{
sVendorInfo.mDeviceIdType = OT_TCAT_DEVICE_ID_IANAPEN;
}
else if (aArgs[0] == kVendorIdTypes[OT_TCAT_DEVICE_ID_EMPTY])
{
sVendorInfo.mDeviceIdType = OT_TCAT_DEVICE_ID_EMPTY;
sVendorInfo.mDeviceId = nullptr;
ExitNow();
}
else
{
ExitNow(error = OT_ERROR_INVALID_ARGS);
}

if (aArgs[1].GetLength() < (OT_TCAT_MAX_VENDORID_SIZE) && !aArgs[1].IsEmpty())
{
strcpy(sVendorId, aArgs[1].GetCString());
sVendorInfo.mDeviceId = sVendorId;
}
else
{
sVendorInfo.mDeviceIdType = OT_TCAT_DEVICE_ID_EMPTY;
sVendorInfo.mDeviceId = nullptr;
ExitNow(error = OT_ERROR_INVALID_ARGS);
}
exit:
return error;
}

template <> otError Tcat::Process<Cmd("start")>(Arg aArgs[])
{
OT_UNUSED_VARIABLE(aArgs);
Expand All @@ -129,8 +193,9 @@ template <> otError Tcat::Process<Cmd("start")>(Arg aArgs[])

otBleSecureSetSslAuthMode(GetInstancePtr(), true);

SuccessOrExit(error = otBleTcatSetVendorInfo(GetInstancePtr(), &sVendorInfo));
SuccessOrExit(error = otBleSecureStart(GetInstancePtr(), nullptr, HandleBleSecureReceive, true, nullptr));
SuccessOrExit(error = otBleSecureTcatStart(GetInstancePtr(), &mVendorInfo, nullptr));
SuccessOrExit(error = otBleSecureTcatStart(GetInstancePtr(), nullptr));

exit:
return error;
Expand All @@ -152,7 +217,7 @@ otError Tcat::Process(Arg aArgs[])
aCommandString, &Tcat::Process<Cmd(aCommandString)> \
}

static constexpr Command kCommands[] = {CmdEntry("start"), CmdEntry("stop")};
static constexpr Command kCommands[] = {CmdEntry("start"), CmdEntry("stop"), CmdEntry("vendorid")};

static_assert(BinarySearch::IsSorted(kCommands), "kCommands is not sorted");

Expand Down
9 changes: 7 additions & 2 deletions src/core/api/ble_secure_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ otError otBleSecureStart(otInstance *aInstance,
return AsCoreType(aInstance).Get<Ble::BleSecure>().Start(aConnectHandler, aReceiveHandler, aTlvMode, aContext);
}

otError otBleSecureTcatStart(otInstance *aInstance, const otTcatVendorInfo *aVendorInfo, otHandleTcatJoin aHandler)
otError otBleTcatSetVendorInfo(otInstance *aInstance, const otTcatVendorInfo *aVendorInfo)
{
return AsCoreType(aInstance).Get<Ble::BleSecure>().TcatStart(AsCoreType(aVendorInfo), aHandler);
return AsCoreType(aInstance).Get<Ble::BleSecure>().TcatSetVendorInfo(AsCoreType(aVendorInfo));
}

otError otBleSecureTcatStart(otInstance *aInstance, otHandleTcatJoin aHandler)
{
return AsCoreType(aInstance).Get<Ble::BleSecure>().TcatStart(aHandler);
}

void otBleSecureStop(otInstance *aInstance) { AsCoreType(aInstance).Get<Ble::BleSecure>().Stop(); }
Expand Down
Loading

0 comments on commit 2e0fa63

Please sign in to comment.