[coap] remove Coap sub-classes (#3364)

This commit simplifies the COAP modules by renaming `CoapBase` to
`Coap` and removing the now unnecessary sub-classes `ApplicationCoap`
and `ApplicationCoapSecure`.
This commit is contained in:
Abtin Keshavarzian
2018-12-14 12:20:59 -08:00
committed by Jonathan Hui
parent 2610b9d5e0
commit c482301ec7
7 changed files with 64 additions and 168 deletions
+39 -59
View File
@@ -48,10 +48,10 @@
namespace ot {
namespace Coap {
CoapBase::CoapBase(Instance &aInstance)
Coap::Coap(Instance &aInstance)
: InstanceLocator(aInstance)
, mSocket(aInstance.GetThreadNetif().GetIp6().GetUdp())
, mRetransmissionTimer(aInstance, &CoapBase::HandleRetransmissionTimer, this)
, mRetransmissionTimer(aInstance, &Coap::HandleRetransmissionTimer, this)
, mResources(NULL)
, mContext(NULL)
, mInterceptor(NULL)
@@ -62,20 +62,20 @@ CoapBase::CoapBase(Instance &aInstance)
mMessageId = Random::GetUint16();
}
otError CoapBase::Start(uint16_t aPort)
otError Coap::Start(uint16_t aPort)
{
otError error;
Ip6::SockAddr sockaddr;
sockaddr.mPort = aPort;
SuccessOrExit(error = mSocket.Open(&CoapBase::HandleUdpReceive, this));
SuccessOrExit(error = mSocket.Open(&Coap::HandleUdpReceive, this));
SuccessOrExit(error = mSocket.Bind(sockaddr));
exit:
return error;
}
otError CoapBase::Stop(void)
otError Coap::Stop(void)
{
Message * message = mPendingRequests.GetHead();
Message * messageToRemove;
@@ -96,7 +96,7 @@ otError CoapBase::Stop(void)
return mSocket.Close();
}
otError CoapBase::AddResource(Resource &aResource)
otError Coap::AddResource(Resource &aResource)
{
otError error = OT_ERROR_NONE;
@@ -112,7 +112,7 @@ exit:
return error;
}
void CoapBase::RemoveResource(Resource &aResource)
void Coap::RemoveResource(Resource &aResource)
{
if (mResources == &aResource)
{
@@ -134,13 +134,13 @@ exit:
aResource.mNext = NULL;
}
void CoapBase::SetDefaultHandler(otCoapRequestHandler aHandler, void *aContext)
void Coap::SetDefaultHandler(otCoapRequestHandler aHandler, void *aContext)
{
mDefaultHandler = aHandler;
mDefaultHandlerContext = aContext;
}
Message *CoapBase::NewMessage(const Header &aHeader, const otMessageSettings *aSettings)
Message *Coap::NewMessage(const Header &aHeader, const otMessageSettings *aSettings)
{
Message *message = NULL;
@@ -155,10 +155,10 @@ exit:
return message;
}
otError CoapBase::SendMessage(Message & aMessage,
const Ip6::MessageInfo &aMessageInfo,
otCoapResponseHandler aHandler,
void * aContext)
otError Coap::SendMessage(Message & aMessage,
const Ip6::MessageInfo &aMessageInfo,
otCoapResponseHandler aHandler,
void * aContext)
{
otError error;
Header header;
@@ -212,14 +212,12 @@ exit:
return error;
}
otError CoapBase::Send(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
otError Coap::Send(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
return mSocket.SendTo(aMessage, aMessageInfo);
}
otError CoapBase::SendEmptyMessage(Header::Type aType,
const Header & aRequestHeader,
const Ip6::MessageInfo &aMessageInfo)
otError Coap::SendEmptyMessage(Header::Type aType, const Header &aRequestHeader, const Ip6::MessageInfo &aMessageInfo)
{
otError error = OT_ERROR_NONE;
Header responseHeader;
@@ -244,9 +242,7 @@ exit:
return error;
}
otError CoapBase::SendHeaderResponse(Header::Code aCode,
const Header & aRequestHeader,
const Ip6::MessageInfo &aMessageInfo)
otError Coap::SendHeaderResponse(Header::Code aCode, const Header &aRequestHeader, const Ip6::MessageInfo &aMessageInfo)
{
otError error = OT_ERROR_NONE;
Header responseHeader;
@@ -290,12 +286,12 @@ exit:
return error;
}
void CoapBase::HandleRetransmissionTimer(Timer &aTimer)
void Coap::HandleRetransmissionTimer(Timer &aTimer)
{
static_cast<CoapBase *>(static_cast<TimerMilliContext &>(aTimer).GetContext())->HandleRetransmissionTimer();
static_cast<Coap *>(static_cast<TimerMilliContext &>(aTimer).GetContext())->HandleRetransmissionTimer();
}
void CoapBase::HandleRetransmissionTimer(void)
void Coap::HandleRetransmissionTimer(void)
{
uint32_t now = TimerMilli::GetNow();
uint32_t nextDelta = 0xffffffff;
@@ -357,12 +353,12 @@ void CoapBase::HandleRetransmissionTimer(void)
}
}
void CoapBase::FinalizeCoapTransaction(Message & aRequest,
const CoapMetadata & aCoapMetadata,
Header * aResponseHeader,
Message * aResponse,
const Ip6::MessageInfo *aMessageInfo,
otError aResult)
void Coap::FinalizeCoapTransaction(Message & aRequest,
const CoapMetadata & aCoapMetadata,
Header * aResponseHeader,
Message * aResponse,
const Ip6::MessageInfo *aMessageInfo,
otError aResult)
{
DequeueMessage(aRequest);
@@ -373,7 +369,7 @@ void CoapBase::FinalizeCoapTransaction(Message & aRequest,
}
}
otError CoapBase::AbortTransaction(otCoapResponseHandler aHandler, void *aContext)
otError Coap::AbortTransaction(otCoapResponseHandler aHandler, void *aContext)
{
otError error = OT_ERROR_NOT_FOUND;
Message * message;
@@ -395,9 +391,7 @@ otError CoapBase::AbortTransaction(otCoapResponseHandler aHandler, void *aContex
return error;
}
Message *CoapBase::CopyAndEnqueueMessage(const Message & aMessage,
uint16_t aCopyLength,
const CoapMetadata &aCoapMetadata)
Message *Coap::CopyAndEnqueueMessage(const Message &aMessage, uint16_t aCopyLength, const CoapMetadata &aCoapMetadata)
{
otError error = OT_ERROR_NONE;
Message *messageCopy = NULL;
@@ -439,7 +433,7 @@ exit:
return messageCopy;
}
void CoapBase::DequeueMessage(Message &aMessage)
void Coap::DequeueMessage(Message &aMessage)
{
mPendingRequests.Dequeue(aMessage);
@@ -456,7 +450,7 @@ void CoapBase::DequeueMessage(Message &aMessage)
// the timer would just shoot earlier and then it'd be setup again.
}
otError CoapBase::SendCopy(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
otError Coap::SendCopy(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
otError error;
Message *messageCopy = NULL;
@@ -478,10 +472,10 @@ exit:
return error;
}
Message *CoapBase::FindRelatedRequest(const Header & aResponseHeader,
const Ip6::MessageInfo &aMessageInfo,
Header & aRequestHeader,
CoapMetadata & aCoapMetadata)
Message *Coap::FindRelatedRequest(const Header & aResponseHeader,
const Ip6::MessageInfo &aMessageInfo,
Header & aRequestHeader,
CoapMetadata & aCoapMetadata)
{
Message *message = mPendingRequests.GetHead();
@@ -527,13 +521,13 @@ exit:
return message;
}
void CoapBase::HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
void Coap::HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
static_cast<CoapBase *>(aContext)->Receive(*static_cast<Message *>(aMessage),
*static_cast<const Ip6::MessageInfo *>(aMessageInfo));
static_cast<Coap *>(aContext)->Receive(*static_cast<Message *>(aMessage),
*static_cast<const Ip6::MessageInfo *>(aMessageInfo));
}
void CoapBase::Receive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
void Coap::Receive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
otError error;
Header header;
@@ -557,7 +551,7 @@ exit:
}
}
void CoapBase::ProcessReceivedResponse(Header &aResponseHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
void Coap::ProcessReceivedResponse(Header &aResponseHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
Header requestHeader;
CoapMetadata coapMetadata;
@@ -635,7 +629,7 @@ exit:
}
}
void CoapBase::ProcessReceivedRequest(Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
void Coap::ProcessReceivedRequest(Header &aHeader, Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
char uriPath[Resource::kMaxReceivedUriPath] = "";
char * curUriPath = uriPath;
@@ -931,19 +925,5 @@ uint32_t EnqueuedResponseHeader::GetRemainingTime(void) const
return remainingTime >= 0 ? static_cast<uint32_t>(remainingTime) : 0;
}
Coap::Coap(Instance &aInstance)
: CoapBase(aInstance)
{
}
#if OPENTHREAD_ENABLE_APPLICATION_COAP
ApplicationCoap::ApplicationCoap(Instance &aInstance)
: CoapBase(aInstance)
{
}
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP
} // namespace Coap
} // namespace ot
+12 -48
View File
@@ -91,7 +91,7 @@ enum
OT_TOOL_PACKED_BEGIN
class CoapMetadata
{
friend class CoapBase;
friend class Coap;
public:
/**
@@ -198,7 +198,7 @@ private:
*/
class Resource : public otCoapResource
{
friend class CoapBase;
friend class Coap;
public:
enum
@@ -438,10 +438,10 @@ private:
};
/**
* This class implements the common base for CoAP client and server.
* This class implements the CoAP client and server.
*
*/
class CoapBase : public InstanceLocator
class Coap : public InstanceLocator
{
friend class ResponsesQueue;
@@ -461,6 +461,14 @@ public:
*/
typedef otError (*Interceptor)(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext);
/**
* This constructor initializes the object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit Coap(Instance &aInstance);
/**
* This method starts the CoAP service.
*
@@ -663,14 +671,6 @@ public:
const MessageQueue &GetCachedResponses(void) const { return mResponsesQueue.GetResponses(); }
protected:
/**
* This constructor initializes the object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit CoapBase(Instance &aInstance);
/**
* This method sends a message.
*
@@ -730,42 +730,6 @@ private:
void * mDefaultHandlerContext;
};
/**
* This class implements the CoAP client and server.
*
*/
class Coap : public CoapBase
{
public:
/**
* This constructor initializes the object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit Coap(Instance &aInstance);
};
#if OPENTHREAD_ENABLE_APPLICATION_COAP
/**
* This class implements the application CoAP client and server.
*
*/
class ApplicationCoap : public CoapBase
{
public:
/**
* This constructor initializes the object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit ApplicationCoap(Instance &aInstance);
};
#endif
} // namespace Coap
} // namespace ot
+6 -15
View File
@@ -48,7 +48,7 @@ namespace ot {
namespace Coap {
CoapSecure::CoapSecure(Instance &aInstance, bool aLayerTwoSecurity)
: CoapBase(aInstance)
: Coap(aInstance)
, mConnectedCallback(NULL)
, mConnectedContext(NULL)
, mTransportCallback(NULL)
@@ -71,7 +71,7 @@ otError CoapSecure::Start(uint16_t aPort, TransportCallback aCallback, void *aCo
// to transmit/receive messages, so do not open it in that case.
if (mTransportCallback == NULL)
{
error = CoapBase::Start(aPort);
error = Coap::Start(aPort);
}
return error;
@@ -99,7 +99,7 @@ otError CoapSecure::Stop(void)
mTransportCallback = NULL;
mTransportContext = NULL;
return CoapBase::Stop();
return Coap::Stop();
}
otError CoapSecure::Connect(const Ip6::SockAddr &aSockAddr, ConnectedCallback aCallback, void *aContext)
@@ -209,7 +209,7 @@ otError CoapSecure::SendMessage(Message &aMessage, otCoapResponseHandler aHandle
VerifyOrExit(IsConnected(), error = OT_ERROR_INVALID_STATE);
error = CoapBase::SendMessage(aMessage, mPeerAddress, aHandler, aContext);
error = Coap::SendMessage(aMessage, mPeerAddress, aHandler, aContext);
exit:
return error;
@@ -220,7 +220,7 @@ otError CoapSecure::SendMessage(Message & aMessage,
otCoapResponseHandler aHandler,
void * aContext)
{
return CoapBase::SendMessage(aMessage, aMessageInfo, aHandler, aContext);
return Coap::SendMessage(aMessage, aMessageInfo, aHandler, aContext);
}
otError CoapSecure::Send(Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
@@ -299,7 +299,7 @@ void CoapSecure::HandleDtlsReceive(uint8_t *aBuf, uint16_t aLength)
VerifyOrExit((message = GetInstance().GetMessagePool().New(Message::kTypeIp6, 0)) != NULL);
SuccessOrExit(message->Append(aBuf, aLength));
CoapBase::Receive(*message, mPeerAddress);
Coap::Receive(*message, mPeerAddress);
exit:
@@ -382,15 +382,6 @@ exit:
}
}
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
ApplicationCoapSecure::ApplicationCoapSecure(Instance &aInstance)
: CoapSecure(aInstance, /* aLayerTwoSecurity */ true)
{
}
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
} // namespace Coap
} // namespace ot
+1 -26
View File
@@ -45,7 +45,7 @@ namespace ot {
namespace Coap {
class CoapSecure : public CoapBase
class CoapSecure : public Coap
{
public:
/**
@@ -343,31 +343,6 @@ private:
bool mLayerTwoSecurity : 1;
};
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
/**
* This class implements the application CoAP Secure client and server.
*
*/
class ApplicationCoapSecure : public CoapSecure
{
public:
/**
* This constructor initializes the object.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
*/
explicit ApplicationCoapSecure(Instance &aInstance);
private:
static void HandleRetransmissionTimer(Timer &aTimer);
static void HandleResponsesQueueTimer(Timer &aTimer);
static void HandleTransmit(Tasklet &aTasklet);
};
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
} // namespace Coap
} // namespace ot
+1 -1
View File
@@ -68,7 +68,7 @@ Instance::Instance(void)
, mApplicationCoap(*this)
#endif
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
, mApplicationCoapSecure(*this)
, mApplicationCoapSecure(*this, /* aLayerTwoSecurity */ true)
#endif
#if OPENTHREAD_ENABLE_CHANNEL_MONITOR
, mChannelMonitor(*this)
+4 -18
View File
@@ -320,7 +320,7 @@ public:
* @returns A reference to the application COAP object.
*
*/
Coap::ApplicationCoap &GetApplicationCoap(void) { return mApplicationCoap; }
Coap::Coap &GetApplicationCoap(void) { return mApplicationCoap; }
#endif
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
@@ -330,7 +330,7 @@ public:
* @returns A reference to the application COAP Secure object.
*
*/
Coap::ApplicationCoapSecure &GetApplicationCoapSecure(void) { return mApplicationCoapSecure; }
Coap::CoapSecure &GetApplicationCoapSecure(void) { return mApplicationCoapSecure; }
#endif
#if OPENTHREAD_ENABLE_CHANNEL_MONITOR
@@ -438,11 +438,11 @@ private:
ThreadNetif mThreadNetif;
#if OPENTHREAD_ENABLE_APPLICATION_COAP
Coap::ApplicationCoap mApplicationCoap;
Coap::Coap mApplicationCoap;
#endif
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
Coap::ApplicationCoapSecure mApplicationCoapSecure;
Coap::CoapSecure mApplicationCoapSecure;
#endif
#if OPENTHREAD_ENABLE_CHANNEL_MONITOR
@@ -599,20 +599,6 @@ template <> inline TimeSync &Instance::Get(void)
}
#endif
#if OPENTHREAD_ENABLE_APPLICATION_COAP
template <> inline Coap::ApplicationCoap &Instance::Get(void)
{
return GetApplicationCoap();
}
#endif
#if OPENTHREAD_ENABLE_APPLICATION_COAP_SECURE
template <> inline Coap::ApplicationCoapSecure &Instance::Get(void)
{
return GetApplicationCoapSecure();
}
#endif
#if OPENTHREAD_ENABLE_COMMISSIONER && OPENTHREAD_FTD
template <> inline MeshCoP::Commissioner &Instance::Get(void)
{
+1 -1
View File
@@ -55,7 +55,7 @@ enum
* This function create Message for MeshCoP
*
*/
inline Message *NewMeshCoPMessage(Coap::CoapBase &aCoap, const Coap::Header &aHeader)
inline Message *NewMeshCoPMessage(Coap::Coap &aCoap, const Coap::Header &aHeader)
{
otMessageSettings settings = {true, static_cast<otMessagePriority>(kMeshCoPMessagePriority)};
return aCoap.NewMessage(aHeader, &settings);