otError SourceMatchController::AddAddress(const Child &aChild) { otError error = OT_ERROR_NONE; if (aChild.IsIndirectSourceMatchShort()) { error = otPlatRadioAddSrcMatchShortEntry(&GetInstance(), aChild.GetRloc16()); otLogDebgMac(GetInstance(), "SrcAddrMatch - Adding short addr: 0x%04x -- %s (%d)", aChild.GetRloc16(), otThreadErrorToString(error), error); } else { otExtAddress addr; for (uint8_t i = 0; i < sizeof(addr); i++) { addr.m8[i] = aChild.GetExtAddress().m8[sizeof(addr) - 1 - i]; } error = otPlatRadioAddSrcMatchExtEntry(&GetInstance(), &addr); otLogDebgMac(GetInstance(), "SrcAddrMatch - Adding addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x -- %s (%d)", addr.m8[7], addr.m8[6], addr.m8[5], addr.m8[4], addr.m8[3], addr.m8[2], addr.m8[1], addr.m8[0], otThreadErrorToString(error), error); } return error; }
void CoapSecure::HandleTransmit(void) { otError error = OT_ERROR_NONE; ot::Message *message = mTransmitQueue.GetHead(); VerifyOrExit(message != NULL); mTransmitQueue.Dequeue(*message); if (mTransmitQueue.GetHead() != NULL) { mTransmitTask.Post(); } SuccessOrExit(error = mDtls.Send(*message, message->GetLength())); exit: if (error != OT_ERROR_NONE) { otLogNoteMeshCoP("CoapSecure Transmit: %s", otThreadErrorToString(error)); message->Free(); } else { otLogDebgMeshCoP("CoapSecure Transmit: %s", otThreadErrorToString(error)); } }
void SourceMatchController::ClearEntry(Child &aChild) { otError error = OT_ERROR_NONE; if (aChild.IsIndirectSourceMatchPending()) { otLogDebgMac(GetInstance(), "SrcAddrMatch - Clearing pending flag for 0x%04x", aChild.GetRloc16()); aChild.SetIndirectSourceMatchPending(false); ExitNow(); } if (aChild.IsIndirectSourceMatchShort()) { error = otPlatRadioClearSrcMatchShortEntry(&GetInstance(), aChild.GetRloc16()); otLogDebgMac(GetInstance(), "SrcAddrMatch - Clearing short address: 0x%04x -- %s (%d)", aChild.GetRloc16(), otThreadErrorToString(error), error); } else { otExtAddress addr; for (uint8_t i = 0; i < sizeof(addr); i++) { addr.m8[i] = aChild.GetExtAddress().m8[sizeof(aChild.GetExtAddress()) - 1 - i]; } error = otPlatRadioClearSrcMatchExtEntry(&GetInstance(), &addr); otLogDebgMac(GetInstance(), "SrcAddrMatch - Clearing addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x -- %s (%d)", addr.m8[7], addr.m8[6], addr.m8[5], addr.m8[4], addr.m8[3], addr.m8[2], addr.m8[1], addr.m8[0], otThreadErrorToString(error), error); } SuccessOrExit(error); if (!IsEnabled()) { SuccessOrExit(AddPendingEntries()); Enable(true); } exit: return; }
void Coap::HandleClientResponse(otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError) { if (aError != OT_ERROR_NONE) { mInterpreter.mServer->OutputFormat("Error receiving coap response message: Error %d: %s\r\n", aError, otThreadErrorToString(aError)); } else { mInterpreter.mServer->OutputFormat("Received coap response"); PrintPayload(aMessage); } OT_UNUSED_VARIABLE(aHeader); OT_UNUSED_VARIABLE(aMessageInfo); }
int Dtls::HandleMbedtlsTransmit(const unsigned char *aBuf, size_t aLength) { otError error; int rval = 0; if (mCipherSuites[0] == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) { otLogDebgMeshCoP("Dtls::HandleMbedtlsTransmit"); } #if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE else { otLogDebgCoap("Dtls::ApplicationCoapSecure HandleMbedtlsTransmit"); } #endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE error = HandleDtlsSend(aBuf, static_cast<uint16_t>(aLength), mMessageSubType); // Restore default sub type. mMessageSubType = mMessageDefaultSubType; switch (error) { case OT_ERROR_NONE: rval = static_cast<int>(aLength); break; case OT_ERROR_NO_BUFS: rval = MBEDTLS_ERR_SSL_WANT_WRITE; break; default: otLogWarnMeshCoP("Dtls::HandleMbedtlsTransmit: %s error", otThreadErrorToString(error)); rval = MBEDTLS_ERR_NET_SEND_FAILED; break; } return rval; }
void Joiner::HandleJoinerEntrust(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) { ThreadNetif & netif = GetNetif(); otError error; NetworkMasterKeyTlv masterKey; MeshLocalPrefixTlv meshLocalPrefix; ExtendedPanIdTlv extendedPanId; NetworkNameTlv networkName; ActiveTimestampTlv activeTimestamp; NetworkKeySequenceTlv networkKeySeq; VerifyOrExit(mState == OT_JOINER_STATE_ENTRUST && aMessage.GetType() == OT_COAP_TYPE_CONFIRMABLE && aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_DROP); otLogInfoMeshCoP("Received joiner entrust"); otLogCertMeshCoP("[THCI] direction=recv | type=JOIN_ENT.ntf"); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kNetworkMasterKey, sizeof(masterKey), masterKey)); VerifyOrExit(masterKey.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kMeshLocalPrefix, sizeof(meshLocalPrefix), meshLocalPrefix)); VerifyOrExit(meshLocalPrefix.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kExtendedPanId, sizeof(extendedPanId), extendedPanId)); VerifyOrExit(extendedPanId.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kNetworkName, sizeof(networkName), networkName)); VerifyOrExit(networkName.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kActiveTimestamp, sizeof(activeTimestamp), activeTimestamp)); VerifyOrExit(activeTimestamp.IsValid(), error = OT_ERROR_PARSE); SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kNetworkKeySequence, sizeof(networkKeySeq), networkKeySeq)); VerifyOrExit(networkKeySeq.IsValid(), error = OT_ERROR_PARSE); netif.GetKeyManager().SetMasterKey(masterKey.GetNetworkMasterKey()); netif.GetKeyManager().SetCurrentKeySequence(networkKeySeq.GetNetworkKeySequence()); netif.GetMle().SetMeshLocalPrefix(meshLocalPrefix.GetMeshLocalPrefix()); netif.GetMac().SetExtendedPanId(extendedPanId.GetExtendedPanId()); { otNetworkName name; memcpy(name.m8, networkName.GetNetworkName(), networkName.GetLength()); name.m8[networkName.GetLength()] = '\0'; netif.GetMac().SetNetworkName(name.m8); } otLogInfoMeshCoP("join success!"); // Send dummy response. SendJoinerEntrustResponse(aMessage, aMessageInfo); // Delay extended address configuration to allow DTLS wrap up. mTimer.Start(kConfigExtAddressDelay); exit: if (error != OT_ERROR_NONE) { otLogWarnMeshCoP("Error while processing joiner entrust: %s", otThreadErrorToString(error)); } }
void Coap::HandleServerResponse(otCoapHeader *aHeader, otMessage *aMessage, const otMessageInfo *aMessageInfo) { otError error = OT_ERROR_NONE; otCoapHeader responseHeader; otMessage *responseMessage; otCoapCode responseCode = OT_COAP_CODE_EMPTY; char responseContent = '0'; mInterpreter.mServer->OutputFormat("Received coap request from [%x:%x:%x:%x:%x:%x:%x:%x]: ", HostSwap16(aMessageInfo->mSockAddr.mFields.m16[0]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[1]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[2]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[3]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[4]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[5]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[6]), HostSwap16(aMessageInfo->mSockAddr.mFields.m16[7])); switch (otCoapHeaderGetCode(aHeader)) { case OT_COAP_CODE_GET: mInterpreter.mServer->OutputFormat("GET"); break; case OT_COAP_CODE_DELETE: mInterpreter.mServer->OutputFormat("DELETE"); break; case OT_COAP_CODE_PUT: mInterpreter.mServer->OutputFormat("PUT"); break; case OT_COAP_CODE_POST: mInterpreter.mServer->OutputFormat("POST"); break; default: mInterpreter.mServer->OutputFormat("Undefined\r\n"); return; } PrintPayload(aMessage); if ((otCoapHeaderGetType(aHeader) == OT_COAP_TYPE_CONFIRMABLE) || otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET) { if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET) { responseCode = OT_COAP_CODE_CONTENT; } else { responseCode = OT_COAP_CODE_VALID; } otCoapHeaderInit(&responseHeader, OT_COAP_TYPE_ACKNOWLEDGMENT, responseCode); otCoapHeaderSetMessageId(&responseHeader, otCoapHeaderGetMessageId(aHeader)); otCoapHeaderSetToken(&responseHeader, otCoapHeaderGetToken(aHeader), otCoapHeaderGetTokenLength(aHeader)); if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET) { otCoapHeaderSetPayloadMarker(&responseHeader); } responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET) { SuccessOrExit(error = otMessageAppend(responseMessage, &responseContent, sizeof(responseContent))); } SuccessOrExit(error = otCoapSendResponse(mInterpreter.mInstance, responseMessage, aMessageInfo)); } exit: if (error != OT_ERROR_NONE && responseMessage != NULL) { mInterpreter.mServer->OutputFormat("Cannot send coap response message: Error %d: %s\r\n", error, otThreadErrorToString(error)); otMessageFree(responseMessage); } else if (responseCode >= OT_COAP_CODE_RESPONSE_MIN) { mInterpreter.mServer->OutputFormat("coap response sent successfully!\r\n"); } }
void DataPollManager::HandlePollSent(otError aError) { bool shouldRecalculatePollPeriod = false; VerifyOrExit(mEnabled); switch (aError) { case OT_ERROR_NONE: if (mRemainingFastPolls != 0) { mRemainingFastPolls--; if (mRemainingFastPolls == 0) { shouldRecalculatePollPeriod = true; mFastPollsUsers = 0; } } if (mRetxMode == true) { mRetxMode = false; mPollTxFailureCounter = 0; shouldRecalculatePollPeriod = true; } otLogInfoMac("Sent data poll"); break; case OT_ERROR_CHANNEL_ACCESS_FAILURE: case OT_ERROR_ABORT: mRetxMode = true; shouldRecalculatePollPeriod = true; break; default: mPollTxFailureCounter++; otLogInfoMac("Failed to send data poll, error:%s, retx:%d/%d", otThreadErrorToString(aError), mPollTxFailureCounter, kMaxPollRetxAttempts); if (mPollTxFailureCounter < kMaxPollRetxAttempts) { if (mRetxMode == false) { mRetxMode = true; shouldRecalculatePollPeriod = true; } } else { mRetxMode = false; mPollTxFailureCounter = 0; shouldRecalculatePollPeriod = true; } break; } if (shouldRecalculatePollPeriod) { ScheduleNextPoll(kRecalculatePollPeriod); } exit: return; }
otError Dataset::ApplyConfiguration(Instance &aInstance) const { ThreadNetif &netif = aInstance.GetThreadNetif(); Mac::Mac & mac = netif.GetMac(); otError error = OT_ERROR_NONE; const Tlv * cur = reinterpret_cast<const Tlv *>(mTlvs); const Tlv * end = reinterpret_cast<const Tlv *>(mTlvs + mLength); VerifyOrExit(IsValid(), error = OT_ERROR_PARSE); while (cur < end) { switch (cur->GetType()) { case Tlv::kChannel: { uint8_t channel = static_cast<uint8_t>(static_cast<const ChannelTlv *>(cur)->GetChannel()); error = mac.SetPanChannel(channel); if (error != OT_ERROR_NONE) { otLogWarnMeshCoP(aInstance, "DatasetManager::ApplyConfiguration() Failed to set channel to %d (%s)", channel, otThreadErrorToString(error)); ExitNow(); } break; } case Tlv::kChannelMask: { const ChannelMask0Entry *mask0Entry = static_cast<const ChannelMaskTlv *>(cur)->GetMask0Entry(); if (mask0Entry != NULL) { mac.SetSupportedChannelMask(mask0Entry->GetMask()); } break; } case Tlv::kPanId: mac.SetPanId(static_cast<const PanIdTlv *>(cur)->GetPanId()); break; case Tlv::kExtendedPanId: mac.SetExtendedPanId(static_cast<const ExtendedPanIdTlv *>(cur)->GetExtendedPanId()); break; case Tlv::kNetworkName: { const NetworkNameTlv *name = static_cast<const NetworkNameTlv *>(cur); mac.SetNetworkName(name->GetNetworkName(), name->GetLength()); break; } case Tlv::kNetworkMasterKey: { const NetworkMasterKeyTlv *key = static_cast<const NetworkMasterKeyTlv *>(cur); netif.GetKeyManager().SetMasterKey(key->GetNetworkMasterKey()); break; } #if OPENTHREAD_FTD case Tlv::kPSKc: { const PSKcTlv *pskc = static_cast<const PSKcTlv *>(cur); netif.GetKeyManager().SetPSKc(pskc->GetPSKc()); break; } #endif case Tlv::kMeshLocalPrefix: { const MeshLocalPrefixTlv *prefix = static_cast<const MeshLocalPrefixTlv *>(cur); netif.GetMle().SetMeshLocalPrefix(prefix->GetMeshLocalPrefix()); break; } case Tlv::kSecurityPolicy: { const SecurityPolicyTlv *securityPolicy = static_cast<const SecurityPolicyTlv *>(cur); netif.GetKeyManager().SetKeyRotation(securityPolicy->GetRotationTime()); netif.GetKeyManager().SetSecurityPolicyFlags(securityPolicy->GetFlags()); break; } default: { break; } } cur = cur->GetNext(); } exit: return error; }