diff --git a/examples/drivers/windows/otApi/otApi.cpp b/examples/drivers/windows/otApi/otApi.cpp index c4977d0bb..86b4d66d0 100644 --- a/examples/drivers/windows/otApi/otApi.cpp +++ b/examples/drivers/windows/otApi/otApi.cpp @@ -1506,7 +1506,7 @@ otLinkSetExtendedAddress( } OTAPI -const uint8_t * +const otExtendedPanId * OTCALL otThreadGetExtendedPanId( _In_ otInstance *aInstance @@ -1515,12 +1515,13 @@ otThreadGetExtendedPanId( if (aInstance == nullptr) return nullptr; otExtendedPanId *Result = (otExtendedPanId*)malloc(sizeof(otExtendedPanId)); - if (Result && QueryIOCTL(aInstance, IOCTL_OTLWF_OT_EXTENDED_PANID, Result) != ERROR_SUCCESS) + if (Result == nullptr) return nullptr; + if (QueryIOCTL(aInstance, IOCTL_OTLWF_OT_EXTENDED_PANID, Result) != ERROR_SUCCESS) { free(Result); Result = nullptr; } - return (uint8_t*)Result; + return Result; } OTAPI @@ -1528,11 +1529,11 @@ otError OTCALL otThreadSetExtendedPanId( _In_ otInstance *aInstance, - const uint8_t *aExtendedPanId + const otExtendedPanId *aExtendedPanId ) { if (aInstance == nullptr) return OT_ERROR_INVALID_ARGS; - return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_EXTENDED_PANID, (const otExtendedPanId*)aExtendedPanId)); + return DwordToThreadError(SetIOCTL(aInstance, IOCTL_OTLWF_OT_EXTENDED_PANID, aExtendedPanId)); } OTAPI diff --git a/examples/drivers/windows/otLwf/iocontrol.c b/examples/drivers/windows/otLwf/iocontrol.c index 2566f38e3..aff992478 100644 --- a/examples/drivers/windows/otLwf/iocontrol.c +++ b/examples/drivers/windows/otLwf/iocontrol.c @@ -1256,12 +1256,13 @@ otLwfIoCtl_otExtendedPanId( if (InBufferLength >= sizeof(otExtendedPanId)) { - status = ThreadErrorToNtstatus(otThreadSetExtendedPanId(pFilter->otCtx, (uint8_t*)InBuffer)); + status = ThreadErrorToNtstatus(otThreadSetExtendedPanId(pFilter->otCtx, (otExtendedPanId*)InBuffer)); *OutBufferLength = 0; } else if (*OutBufferLength >= sizeof(otExtendedPanId)) { - memcpy(OutBuffer, otThreadGetExtendedPanId(pFilter->otCtx), sizeof(otExtendedPanId)); + const otExtendedPanId* aExtendedPanId = otThreadGetExtendedPanId(pFilter->otCtx); + memcpy(OutBuffer, aExtendedPanId, sizeof(otExtendedPanId)); *OutBufferLength = sizeof(otExtendedPanId); status = STATUS_SUCCESS; } diff --git a/include/openthread/commissioner.h b/include/openthread/commissioner.h index cc5ab0ff3..b8a2d4c95 100644 --- a/include/openthread/commissioner.h +++ b/include/openthread/commissioner.h @@ -35,6 +35,7 @@ #ifndef OPENTHREAD_COMMISSIONER_H_ #define OPENTHREAD_COMMISSIONER_H_ +#include #include #include #include @@ -346,11 +347,11 @@ OTAPI otCommissionerState OTCALL otCommissionerGetState(otInstance *aInstance); * @retval OT_ERROR_INVALID_ARGS If any of the input arguments is invalid. * */ -OTAPI otError OTCALL otCommissionerGeneratePSKc(otInstance * aInstance, - const char * aPassPhrase, - const char * aNetworkName, - const uint8_t *aExtPanId, - uint8_t * aPSKc); +OTAPI otError OTCALL otCommissionerGeneratePSKc(otInstance * aInstance, + const char * aPassPhrase, + const char * aNetworkName, + const otExtendedPanId *aExtPanId, + uint8_t * aPSKc); /** * @} diff --git a/include/openthread/dataset.h b/include/openthread/dataset.h index 053672105..656919530 100644 --- a/include/openthread/dataset.h +++ b/include/openthread/dataset.h @@ -86,10 +86,17 @@ typedef struct otNetworkName * This structure represents an Extended PAN ID. * */ -typedef struct otExtendedPanId +OT_TOOL_PACKED_BEGIN +struct otExtendedPanId { uint8_t m8[OT_EXT_PAN_ID_SIZE]; ///< Byte values -} otExtendedPanId; +} OT_TOOL_PACKED_END; + +/** + * This structure represents an Extended PAN ID. + * + */ +typedef struct otExtendedPanId otExtendedPanId; #define OT_MESH_LOCAL_PREFIX_SIZE 8 ///< Size of the Mesh Local Prefix (bytes) diff --git a/include/openthread/thread.h b/include/openthread/thread.h index 949cefe37..0d4dd0a39 100644 --- a/include/openthread/thread.h +++ b/include/openthread/thread.h @@ -262,7 +262,7 @@ OTAPI void OTCALL otThreadSetChildTimeout(otInstance *aInstance, uint32_t aTimeo * * @sa otThreadSetExtendedPanId */ -OTAPI const uint8_t *OTCALL otThreadGetExtendedPanId(otInstance *aInstance); +OTAPI const otExtendedPanId *OTCALL otThreadGetExtendedPanId(otInstance *aInstance); /** * Set the IEEE 802.15.4 Extended PAN ID. @@ -279,7 +279,7 @@ OTAPI const uint8_t *OTCALL otThreadGetExtendedPanId(otInstance *aInstance); * * @sa otThreadGetExtendedPanId */ -OTAPI otError OTCALL otThreadSetExtendedPanId(otInstance *aInstance, const uint8_t *aExtendedPanId); +OTAPI otError OTCALL otThreadSetExtendedPanId(otInstance *aInstance, const otExtendedPanId *aExtendedPanId); /** * This function returns a pointer to the Leader's RLOC. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 29bfbb1ff..8f164e2fe 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -956,17 +956,17 @@ void Interpreter::ProcessExtPanId(int argc, char *argv[]) if (argc == 0) { - otBufferPtr extPanId(otThreadGetExtendedPanId(mInstance)); + otBufferPtr extPanId(reinterpret_cast(otThreadGetExtendedPanId(mInstance))); OutputBytes(extPanId, OT_EXT_PAN_ID_SIZE); mServer->OutputFormat("\r\n"); } else { - uint8_t extPanId[8]; + otExtendedPanId extPanId; - VerifyOrExit(Hex2Bin(argv[0], extPanId, sizeof(extPanId)) >= 0, error = OT_ERROR_PARSE); + VerifyOrExit(Hex2Bin(argv[0], extPanId.m8, sizeof(extPanId)) >= 0, error = OT_ERROR_PARSE); - error = otThreadSetExtendedPanId(mInstance, extPanId); + error = otThreadSetExtendedPanId(mInstance, &extPanId); } exit: diff --git a/src/core/api/commissioner_api.cpp b/src/core/api/commissioner_api.cpp index fcefe6a8a..46eae704b 100644 --- a/src/core/api/commissioner_api.cpp +++ b/src/core/api/commissioner_api.cpp @@ -292,18 +292,18 @@ otCommissionerState otCommissionerGetState(otInstance *aInstance) return state; } -otError otCommissionerGeneratePSKc(otInstance * aInstance, - const char * aPassPhrase, - const char * aNetworkName, - const uint8_t *aExtPanId, - uint8_t * aPSKc) +otError otCommissionerGeneratePSKc(otInstance * aInstance, + const char * aPassPhrase, + const char * aNetworkName, + const otExtendedPanId *aExtPanId, + uint8_t * aPSKc) { otError error = OT_ERROR_DISABLED_FEATURE; #if OPENTHREAD_FTD && OPENTHREAD_ENABLE_COMMISSIONER Instance &instance = *static_cast(aInstance); - error = instance.GetThreadNetif().GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, aExtPanId, aPSKc); + error = instance.GetThreadNetif().GetCommissioner().GeneratePSKc(aPassPhrase, aNetworkName, *aExtPanId, aPSKc); #else OT_UNUSED_VARIABLE(aInstance); OT_UNUSED_VARIABLE(aPassPhrase); diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index c973c9847..a9ff568af 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -57,14 +57,14 @@ void otThreadSetChildTimeout(otInstance *aInstance, uint32_t aTimeout) instance.GetThreadNetif().GetMle().SetTimeout(aTimeout); } -const uint8_t *otThreadGetExtendedPanId(otInstance *aInstance) +const otExtendedPanId *otThreadGetExtendedPanId(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); - return instance.GetThreadNetif().GetMac().GetExtendedPanId(); + return &instance.GetThreadNetif().GetMac().GetExtendedPanId(); } -otError otThreadSetExtendedPanId(otInstance *aInstance, const uint8_t *aExtendedPanId) +otError otThreadSetExtendedPanId(otInstance *aInstance, const otExtendedPanId *aExtendedPanId) { otError error = OT_ERROR_NONE; Instance &instance = *static_cast(aInstance); @@ -73,10 +73,10 @@ otError otThreadSetExtendedPanId(otInstance *aInstance, const uint8_t *aExtended VerifyOrExit(instance.GetThreadNetif().GetMle().GetRole() == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE); - instance.GetThreadNetif().GetMac().SetExtendedPanId(aExtendedPanId); + instance.GetThreadNetif().GetMac().SetExtendedPanId(*aExtendedPanId); mlPrefix[0] = 0xfd; - memcpy(mlPrefix + 1, aExtendedPanId, 5); + memcpy(mlPrefix + 1, aExtendedPanId->m8, 5); mlPrefix[6] = 0x00; mlPrefix[7] = 0x00; instance.GetThreadNetif().GetMle().SetMeshLocalPrefix(mlPrefix); diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 21bc727c3..4ab030b95 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -64,8 +64,10 @@ static const otExtAddress sMode2ExtAddress = { {0x35, 0x06, 0xfe, 0xb8, 0x23, 0xd4, 0x87, 0x12}, }; -static const uint8_t sExtendedPanidInit[] = {0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0xca, 0xfe}; -static const char sNetworkNameInit[] = "OpenThread"; +static const otExtendedPanId sExtendedPanidInit = { + {0xde, 0xad, 0x00, 0xbe, 0xef, 0x00, 0xca, 0xfe}, +}; +static const char sNetworkNameInit[] = "OpenThread"; #ifdef _WIN32 const uint32_t kMinBackoffSum = kMinBackoff + (kUnitBackoffPeriod * OT_RADIO_SYMBOL_TIME * (1 << kMinBE)) / 1000; @@ -635,12 +637,12 @@ exit: return OT_ERROR_NONE; } -otError Mac::SetExtendedPanId(const uint8_t *aExtPanId) +otError Mac::SetExtendedPanId(const otExtendedPanId &aExtendedPanId) { - VerifyOrExit(memcmp(mExtendedPanId.m8, aExtPanId, sizeof(mExtendedPanId)) != 0, + VerifyOrExit(memcmp(mExtendedPanId.m8, aExtendedPanId.m8, sizeof(mExtendedPanId)) != 0, GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_EXT_PANID)); - memcpy(mExtendedPanId.m8, aExtPanId, sizeof(mExtendedPanId)); + mExtendedPanId = aExtendedPanId; GetNotifier().Signal(OT_CHANGED_THREAD_EXT_PANID); exit: diff --git a/src/core/mac/mac.hpp b/src/core/mac/mac.hpp index 30ff5cdb5..96afe0c63 100644 --- a/src/core/mac/mac.hpp +++ b/src/core/mac/mac.hpp @@ -715,17 +715,17 @@ public: * @returns A pointer to the IEEE 802.15.4 Extended PAN ID. * */ - const uint8_t *GetExtendedPanId(void) const { return mExtendedPanId.m8; } + const otExtendedPanId &GetExtendedPanId(void) const { return mExtendedPanId; } /** * This method sets the IEEE 802.15.4 Extended PAN ID. * - * @param[in] aExtPanId The IEEE 802.15.4 Extended PAN ID. + * @param[in] aExtendedPanId The IEEE 802.15.4 Extended PAN ID. * * @retval OT_ERROR_NONE Successfully set the IEEE 802.15.4 Extended PAN ID. * */ - otError SetExtendedPanId(const uint8_t *aExtPanId); + otError SetExtendedPanId(const otExtendedPanId &aExtendedPanId); #if OPENTHREAD_ENABLE_MAC_FILTER /** diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 4e5cae9c3..2cae57986 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -1059,10 +1059,10 @@ exit: return error; } -otError Commissioner::GeneratePSKc(const char * aPassPhrase, - const char * aNetworkName, - const uint8_t *aExtPanId, - uint8_t * aPSKc) +otError Commissioner::GeneratePSKc(const char * aPassPhrase, + const char * aNetworkName, + const otExtendedPanId &aExtPanId, + uint8_t * aPSKc) { otError error = OT_ERROR_NONE; const char *saltPrefix = "Thread"; @@ -1077,7 +1077,7 @@ otError Commissioner::GeneratePSKc(const char * aPassPhrase, memcpy(salt, saltPrefix, strlen(saltPrefix)); saltLen += static_cast(strlen(saltPrefix)); - memcpy(salt + saltLen, aExtPanId, OT_EXT_PAN_ID_SIZE); + memcpy(salt + saltLen, aExtPanId.m8, sizeof(aExtPanId)); saltLen += OT_EXT_PAN_ID_SIZE; memcpy(salt + saltLen, aNetworkName, strlen(aNetworkName)); diff --git a/src/core/meshcop/commissioner.hpp b/src/core/meshcop/commissioner.hpp index a74b8ff48..a6ae51d7f 100644 --- a/src/core/meshcop/commissioner.hpp +++ b/src/core/meshcop/commissioner.hpp @@ -210,10 +210,10 @@ public: * @retval OT_ERROR_INVALID_ARGS If the length of passphrase is out of range. * */ - static otError GeneratePSKc(const char * aPassPhrase, - const char * aNetworkName, - const uint8_t *aExtPanId, - uint8_t * aPSKc); + static otError GeneratePSKc(const char * aPassPhrase, + const char * aNetworkName, + const otExtendedPanId &aExtPanId, + uint8_t * aPSKc); /** * This method returns a reference to the AnnounceBeginClient instance. diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 8f3035f5c..30f02f8f0 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -166,8 +166,8 @@ void Dataset::Get(otOperationalDataset &aDataset) const case Tlv::kExtendedPanId: { - const ExtendedPanIdTlv *tlv = static_cast(cur); - memcpy(aDataset.mExtendedPanId.m8, tlv->GetExtendedPanId(), sizeof(aDataset.mExtendedPanId)); + const ExtendedPanIdTlv *tlv = static_cast(cur); + aDataset.mExtendedPanId = tlv->GetExtendedPanId(); aDataset.mComponents.mIsExtendedPanIdPresent = true; break; } @@ -310,7 +310,7 @@ otError Dataset::Set(const otOperationalDataset &aDataset) { MeshCoP::ExtendedPanIdTlv tlv; tlv.Init(); - tlv.SetExtendedPanId(aDataset.mExtendedPanId.m8); + tlv.SetExtendedPanId(aDataset.mExtendedPanId); Set(tlv); } diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 28f518b56..3369b989a 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -421,7 +421,7 @@ otError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset, con { ExtendedPanIdTlv extpanid; extpanid.Init(); - extpanid.SetExtendedPanId(aDataset.mExtendedPanId.m8); + extpanid.SetExtendedPanId(aDataset.mExtendedPanId); SuccessOrExit(error = message->Append(&extpanid, sizeof(extpanid))); } diff --git a/src/core/meshcop/meshcop_tlvs.hpp b/src/core/meshcop/meshcop_tlvs.hpp index 202cf7f1d..f6c97e830 100644 --- a/src/core/meshcop/meshcop_tlvs.hpp +++ b/src/core/meshcop/meshcop_tlvs.hpp @@ -351,7 +351,7 @@ public: * @returns The Extended PAN ID value. * */ - const uint8_t *GetExtendedPanId(void) const { return mExtendedPanId; } + const otExtendedPanId &GetExtendedPanId(void) const { return mExtendedPanId; } /** * This method sets the Extended PAN ID value. @@ -359,10 +359,10 @@ public: * @param[in] aExtendedPanId A pointer to the Extended PAN ID value. * */ - void SetExtendedPanId(const uint8_t *aExtendedPanId) { memcpy(mExtendedPanId, aExtendedPanId, OT_EXT_PAN_ID_SIZE); } + void SetExtendedPanId(const otExtendedPanId &aExtendedPanId) { mExtendedPanId = aExtendedPanId; } private: - uint8_t mExtendedPanId[OT_EXT_PAN_ID_SIZE]; + otExtendedPanId mExtendedPanId; } OT_TOOL_PACKED_END; /** diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 43c62e5b3..1788c0f95 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -158,7 +158,7 @@ Mle::Mle(Instance &aInstance) // initialize Mesh Local Prefix meshLocalPrefix[0] = 0xfd; - memcpy(meshLocalPrefix + 1, GetNetif().GetMac().GetExtendedPanId(), 5); + memcpy(meshLocalPrefix + 1, GetNetif().GetMac().GetExtendedPanId().m8, 5); meshLocalPrefix[6] = 0x00; meshLocalPrefix[7] = 0x00; @@ -3683,7 +3683,7 @@ otError Mle::HandleDiscoveryResponse(const Message &aMessage, const Ip6::Message case MeshCoP::Tlv::kExtendedPanId: aMessage.Read(offset, sizeof(extPanId), &extPanId); VerifyOrExit(extPanId.IsValid(), error = OT_ERROR_PARSE); - memcpy(&result.mExtendedPanId, extPanId.GetExtendedPanId(), sizeof(result.mExtendedPanId)); + result.mExtendedPanId = extPanId.GetExtendedPanId(); break; case MeshCoP::Tlv::kNetworkName: diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index c93e466be..9d6db68e6 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -2640,7 +2640,7 @@ otError MleRouter::HandleDiscoveryRequest(const Message &aMessage, const Ip6::Me case MeshCoP::Tlv::kExtendedPanId: aMessage.Read(offset, sizeof(extPanId), &extPanId); VerifyOrExit(extPanId.IsValid(), error = OT_ERROR_PARSE); - VerifyOrExit(memcmp(netif.GetMac().GetExtendedPanId(), extPanId.GetExtendedPanId(), OT_EXT_PAN_ID_SIZE)); + VerifyOrExit(memcmp(&netif.GetMac().GetExtendedPanId(), &extPanId.GetExtendedPanId(), OT_EXT_PAN_ID_SIZE)); break; diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index 4b37182cb..a13e31676 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -395,7 +395,7 @@ exit: template <> otError NcpBase::HandlePropertyGet(void) { - return mEncoder.WriteData(otThreadGetExtendedPanId(mInstance), sizeof(spinel_net_xpanid_t)); + return mEncoder.WriteData(otThreadGetExtendedPanId(mInstance)->m8, sizeof(spinel_net_xpanid_t)); } template <> otError NcpBase::HandlePropertySet(void) @@ -408,7 +408,7 @@ template <> otError NcpBase::HandlePropertySet(void) VerifyOrExit(len == sizeof(spinel_net_xpanid_t), error = OT_ERROR_PARSE); - error = otThreadSetExtendedPanId(mInstance, ptr); + error = otThreadSetExtendedPanId(mInstance, reinterpret_cast(ptr)); exit: return error; diff --git a/tests/unit/test_pskc.cpp b/tests/unit/test_pskc.cpp index 4e79e3bd0..8948f1f40 100644 --- a/tests/unit/test_pskc.cpp +++ b/tests/unit/test_pskc.cpp @@ -34,7 +34,7 @@ #include "test_platform.h" #include "test_util.h" -static const uint8_t sXPanId[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; +static const otExtendedPanId sXPanId = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}; void TestMinimumPassphrase(void) {