mirror of
https://github.com/espressif/openthread.git
synced 2026-06-05 21:14:49 +00:00
[commissioner] update how Provisioning URL is stored (#4207)
This commit updates how the `Commissioner` stores the Provisioning URL (now saved as null terminated string). It also updates the related APIs (including the public `otCommissionerGetProvisioningUrl`) and its use in `NcpBase`.
This commit is contained in:
committed by
Jonathan Hui
parent
621ad11055
commit
9aed664596
@@ -81,6 +81,8 @@ typedef enum otCommissionerJoinerEvent
|
||||
#define OT_COMMISSIONING_PASSPHRASE_MIN_SIZE 6 ///< Minimum size of the Commissioning Passphrase
|
||||
#define OT_COMMISSIONING_PASSPHRASE_MAX_SIZE 255 ///< Maximum size of the Commissioning Passphrase
|
||||
|
||||
#define OT_PROVISIONING_URL_MAX_SIZE 64 ///< Max size (number of chars) in Provisioning URL string (excludes null char).
|
||||
|
||||
#define OT_STEERING_DATA_MAX_LENGTH 16 ///< Max steering data length (bytes)
|
||||
|
||||
/**
|
||||
@@ -230,23 +232,20 @@ otError otCommissionerRemoveJoiner(otInstance *aInstance, const otExtAddress *aE
|
||||
* This function gets the Provisioning URL.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[out] aLength A pointer to `uint16_t` to return the length (number of chars) in the URL string.
|
||||
*
|
||||
* Note that the returned URL string buffer is not necessarily null-terminated.
|
||||
*
|
||||
* @returns A pointer to char buffer containing the URL string, or NULL if @p aLength is NULL.
|
||||
* @returns A pointer to the URL string.
|
||||
*
|
||||
*/
|
||||
const char *otCommissionerGetProvisioningUrl(otInstance *aInstance, uint16_t *aLength);
|
||||
const char *otCommissionerGetProvisioningUrl(otInstance *aInstance);
|
||||
|
||||
/**
|
||||
* This function sets the Provisioning URL.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aProvisioningUrl A pointer to the Provisioning URL (may be NULL).
|
||||
* @param[in] aProvisioningUrl A pointer to the Provisioning URL (may be NULL to set as empty string).
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully set the Provisioning URL.
|
||||
* @retval OT_ERROR_INVALID_ARGS @p aProvisioningUrl is invalid.
|
||||
* @retval OT_ERROR_INVALID_ARGS @p aProvisioningUrl is invalid (too long).
|
||||
*
|
||||
*/
|
||||
otError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aProvisioningUrl);
|
||||
|
||||
@@ -102,18 +102,11 @@ otError otCommissionerSetProvisioningUrl(otInstance *aInstance, const char *aPro
|
||||
return instance.Get<MeshCoP::Commissioner>().SetProvisioningUrl(aProvisioningUrl);
|
||||
}
|
||||
|
||||
const char *otCommissionerGetProvisioningUrl(otInstance *aInstance, uint16_t *aLength)
|
||||
const char *otCommissionerGetProvisioningUrl(otInstance *aInstance)
|
||||
{
|
||||
const char *url = NULL;
|
||||
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
if (aLength != NULL)
|
||||
{
|
||||
url = instance.Get<MeshCoP::Commissioner>().GetProvisioningUrl(*aLength);
|
||||
}
|
||||
|
||||
return url;
|
||||
return instance.Get<MeshCoP::Commissioner>().GetProvisioningUrl();
|
||||
}
|
||||
|
||||
otError otCommissionerAnnounceBegin(otInstance * aInstance,
|
||||
|
||||
@@ -81,7 +81,8 @@ Commissioner::Commissioner(Instance &aInstance)
|
||||
mCommissionerAloc.mValid = true;
|
||||
mCommissionerAloc.mScopeOverride = Ip6::Address::kRealmLocalScope;
|
||||
mCommissionerAloc.mScopeOverrideValid = true;
|
||||
mProvisioningUrl.Init();
|
||||
|
||||
mProvisioningUrl[0] = '\0';
|
||||
}
|
||||
|
||||
void Commissioner::SetState(otCommissionerState aState)
|
||||
@@ -373,24 +374,28 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
const char *Commissioner::GetProvisioningUrl(uint16_t &aLength) const
|
||||
const char *Commissioner::GetProvisioningUrl(void) const
|
||||
{
|
||||
aLength = mProvisioningUrl.GetLength();
|
||||
|
||||
return mProvisioningUrl.GetProvisioningUrl();
|
||||
return mProvisioningUrl;
|
||||
}
|
||||
|
||||
otError Commissioner::SetProvisioningUrl(const char *aProvisioningUrl)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
uint8_t len;
|
||||
|
||||
if (aProvisioningUrl != NULL)
|
||||
if (aProvisioningUrl == NULL)
|
||||
{
|
||||
size_t len = strnlen(aProvisioningUrl, MeshCoP::ProvisioningUrlTlv::kMaxLength + 1);
|
||||
VerifyOrExit(len <= MeshCoP::ProvisioningUrlTlv::kMaxLength, error = OT_ERROR_INVALID_ARGS);
|
||||
mProvisioningUrl[0] = '\0';
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
mProvisioningUrl.SetProvisioningUrl(aProvisioningUrl);
|
||||
len = static_cast<uint8_t>(strnlen(aProvisioningUrl, sizeof(mProvisioningUrl)));
|
||||
|
||||
VerifyOrExit(len < sizeof(mProvisioningUrl), error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
memcpy(mProvisioningUrl, aProvisioningUrl, len);
|
||||
mProvisioningUrl[len] = '\0';
|
||||
|
||||
exit:
|
||||
return error;
|
||||
@@ -959,9 +964,10 @@ void Commissioner::HandleJoinerFinalize(Coap::Message &aMessage, const Ip6::Mess
|
||||
|
||||
if (Tlv::GetTlv(aMessage, Tlv::kProvisioningUrl, sizeof(provisioningUrl), provisioningUrl) == OT_ERROR_NONE)
|
||||
{
|
||||
if (provisioningUrl.GetProvisioningUrlLength() != mProvisioningUrl.GetProvisioningUrlLength() ||
|
||||
memcmp(provisioningUrl.GetProvisioningUrl(), mProvisioningUrl.GetProvisioningUrl(),
|
||||
provisioningUrl.GetProvisioningUrlLength()) != 0)
|
||||
uint8_t len = static_cast<uint8_t>(strnlen(mProvisioningUrl, sizeof(mProvisioningUrl)));
|
||||
|
||||
if ((provisioningUrl.GetProvisioningUrlLength() != len) ||
|
||||
!memcmp(provisioningUrl.GetProvisioningUrl(), mProvisioningUrl, len))
|
||||
{
|
||||
state = StateTlv::kReject;
|
||||
}
|
||||
|
||||
@@ -137,22 +137,18 @@ public:
|
||||
/**
|
||||
* This method gets the Provisioning URL.
|
||||
*
|
||||
* @param[out] aLength A reference to `uint16_t` to return the length (number of chars) in the URL string.
|
||||
*
|
||||
* Note that the returned URL string buffer is not necessarily null-terminated.
|
||||
*
|
||||
* @returns A pointer to char buffer containing the URL string.
|
||||
*
|
||||
*/
|
||||
const char *GetProvisioningUrl(uint16_t &aLength) const;
|
||||
const char *GetProvisioningUrl(void) const;
|
||||
|
||||
/**
|
||||
* This method sets the Provisioning URL.
|
||||
*
|
||||
* @param[in] aProvisioningUrl A pointer to the Provisioning URL (may be NULL).
|
||||
* @param[in] aProvisioningUrl A pointer to the Provisioning URL (may be NULL to set URL to empty string).
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully added the Joiner.
|
||||
* @retval OT_ERROR_INVALID_ARGS @p aProvisioningUrl is invalid.
|
||||
* @retval OT_ERROR_NONE Successfully set the Provisioning URL.
|
||||
* @retval OT_ERROR_INVALID_ARGS @p aProvisioningUrl is invalid (too long).
|
||||
*
|
||||
*/
|
||||
otError SetProvisioningUrl(const char *aProvisioningUrl);
|
||||
@@ -356,7 +352,7 @@ private:
|
||||
|
||||
Ip6::NetifUnicastAddress mCommissionerAloc;
|
||||
|
||||
ProvisioningUrlTlv mProvisioningUrl;
|
||||
char mProvisioningUrl[OT_PROVISIONING_URL_MAX_SIZE + 1]; // + 1 is for null char at end of string.
|
||||
|
||||
otCommissionerStateCallback mStateCallback;
|
||||
otCommissionerJoinerCallback mJoinerCallback;
|
||||
|
||||
@@ -1832,7 +1832,7 @@ class ProvisioningUrlTlv : public Tlv
|
||||
public:
|
||||
enum
|
||||
{
|
||||
kMaxLength = 64, // Maximum number of chars in the Provisioning URL string.
|
||||
kMaxLength = OT_PROVISIONING_URL_MAX_SIZE, // Maximum number of chars in the Provisioning URL string.
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -578,19 +578,7 @@ exit:
|
||||
|
||||
template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_MESHCOP_COMMISSIONER_PROVISIONING_URL>(void)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
uint16_t length = 0;
|
||||
const char *url = otCommissionerGetProvisioningUrl(mInstance, &length);
|
||||
|
||||
if (url != NULL && length > 0)
|
||||
{
|
||||
SuccessOrExit(error = mEncoder.WriteData(reinterpret_cast<const uint8_t *>(url), length));
|
||||
}
|
||||
|
||||
SuccessOrExit(error = mEncoder.WriteUint8(0));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
return mEncoder.WriteUtf8(otCommissionerGetProvisioningUrl(mInstance));
|
||||
}
|
||||
|
||||
template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_MESHCOP_COMMISSIONER_PROVISIONING_URL>(void)
|
||||
|
||||
Reference in New Issue
Block a user