1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
39
40
41
42
43
44
45
46
47
48
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
98
99
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
163
164
165
166
167
168
169
170
171
172
173
174
175
179
180
184
185
189
190
195
196
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
218
219
220
221
222
223
224
225
226
227
231
232
233
234
235
240
244
245
246
247
248
249
/* ... */
#include "coap_secure.hpp"
#if OPENTHREAD_CONFIG_SECURE_TRANSPORT_ENABLE
#include "instance/instance.hpp"
/* ... */
namespace ot {
namespace Coap {
RegisterLogModule("CoapSecure");
CoapSecure::CoapSecure(Instance &aInstance, bool aLayerTwoSecurity)
: CoapBase(aInstance, &CoapSecure::Send)
, mDtls(aInstance, aLayerTwoSecurity)
, mTransmitTask(aInstance, CoapSecure::HandleTransmit, this)
{
}{ ... }
Error CoapSecure::Start(uint16_t aPort) { return Start(aPort, 0, nullptr, nullptr); }
Error CoapSecure::Start(uint16_t aPort, uint16_t aMaxAttempts, AutoStopCallback aCallback, void *aContext)
{
Error error;
SuccessOrExit(error = Open(aMaxAttempts, aCallback, aContext));
error = mDtls.Bind(aPort);
exit:
return error;
}{ ... }
Error CoapSecure::Start(MeshCoP::SecureTransport::TransportCallback aCallback, void *aContext)
{
Error error;
SuccessOrExit(error = Open( 0, nullptr, nullptr));
error = mDtls.Bind(aCallback, aContext);
exit:
return error;
}{ ... }
Error CoapSecure::Open(uint16_t aMaxAttempts, AutoStopCallback aCallback, void *aContext)
{
Error error = kErrorAlready;
SuccessOrExit(mDtls.SetMaxConnectionAttempts(aMaxAttempts, HandleDtlsAutoClose, this));
mAutoStopCallback.Set(aCallback, aContext);
mConnectEventCallback.Clear();
SuccessOrExit(mDtls.Open(HandleDtlsReceive, HandleDtlsConnectEvent, this));
error = kErrorNone;
exit:
return error;
}{ ... }
void CoapSecure::Stop(void)
{
mDtls.Close();
mTransmitQueue.DequeueAndFreeAll();
ClearRequestsAndResponses();
}{ ... }
Error CoapSecure::Connect(const Ip6::SockAddr &aSockAddr, ConnectEventCallback aCallback, void *aContext)
{
mConnectEventCallback.Set(aCallback, aContext);
return mDtls.Connect(aSockAddr);
}{ ... }
void CoapSecure::SetPsk(const MeshCoP::JoinerPskd &aPskd)
{
static_assert(static_cast<uint16_t>(MeshCoP::JoinerPskd::kMaxLength) <=
static_cast<uint16_t>(MeshCoP::SecureTransport::kPskMaxLength),
"The maximum length of DTLS PSK is smaller than joiner PSKd");
SuccessOrAssert(mDtls.SetPsk(reinterpret_cast<const uint8_t *>(aPskd.GetAsCString()), aPskd.GetLength()));
}{ ... }
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
Error CoapSecure::SendMessage(Message &aMessage,
ResponseHandler aHandler,
void *aContext,
otCoapBlockwiseTransmitHook aTransmitHook,
otCoapBlockwiseReceiveHook aReceiveHook)
{
Error error = kErrorNone;
VerifyOrExit(IsConnected(), error = kErrorInvalidState);
error = CoapBase::SendMessage(aMessage, mDtls.GetMessageInfo(), TxParameters::GetDefault(), aHandler, aContext,
aTransmitHook, aReceiveHook);
exit:
return error;
}{...}
Error CoapSecure::SendMessage(Message &aMessage,
const Ip6::MessageInfo &aMessageInfo,
ResponseHandler aHandler,
void *aContext,
otCoapBlockwiseTransmitHook aTransmitHook,
otCoapBlockwiseReceiveHook aReceiveHook)
{
return CoapBase::SendMessage(aMessage, aMessageInfo, TxParameters::GetDefault(), aHandler, aContext, aTransmitHook,
aReceiveHook);
}{...}
/* ... */#else
Error CoapSecure::SendMessage(Message &aMessage, ResponseHandler aHandler, void *aContext)
{
Error error = kErrorNone;
VerifyOrExit(IsConnected(), error = kErrorInvalidState);
error = CoapBase::SendMessage(aMessage, mDtls.GetMessageInfo(), aHandler, aContext);
exit:
return error;
}{ ... }
Error CoapSecure::SendMessage(Message &aMessage,
const Ip6::MessageInfo &aMessageInfo,
ResponseHandler aHandler,
void *aContext)
{
return CoapBase::SendMessage(aMessage, aMessageInfo, aHandler, aContext);
}{ ... }
#endif/* ... */
Error CoapSecure::Send(ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
{
OT_UNUSED_VARIABLE(aMessageInfo);
mTransmitQueue.Enqueue(aMessage);
mTransmitTask.Post();
return kErrorNone;
}{ ... }
void CoapSecure::HandleDtlsConnectEvent(MeshCoP::SecureTransport::ConnectEvent aEvent, void *aContext)
{
return static_cast<CoapSecure *>(aContext)->HandleDtlsConnectEvent(aEvent);
}{ ... }
void CoapSecure::HandleDtlsConnectEvent(MeshCoP::SecureTransport::ConnectEvent aEvent)
{
mConnectEventCallback.InvokeIfSet(aEvent);
}{ ... }
void CoapSecure::HandleDtlsAutoClose(void *aContext)
{
return static_cast<CoapSecure *>(aContext)->HandleDtlsAutoClose();
}{ ... }
void CoapSecure::HandleDtlsAutoClose(void)
{
Stop();
mAutoStopCallback.InvokeIfSet();
}{ ... }
void CoapSecure::HandleDtlsReceive(void *aContext, uint8_t *aBuf, uint16_t aLength)
{
return static_cast<CoapSecure *>(aContext)->HandleDtlsReceive(aBuf, aLength);
}{ ... }
void CoapSecure::HandleDtlsReceive(uint8_t *aBuf, uint16_t aLength)
{
ot::Message *message = nullptr;
VerifyOrExit((message = Get<MessagePool>().Allocate(Message::kTypeIp6, Message::GetHelpDataReserved())) != nullptr);
SuccessOrExit(message->AppendBytes(aBuf, aLength));
CoapBase::Receive(*message, mDtls.GetMessageInfo());
exit:
FreeMessage(message);
}{ ... }
void CoapSecure::HandleTransmit(Tasklet &aTasklet)
{
static_cast<CoapSecure *>(static_cast<TaskletContext &>(aTasklet).GetContext())->HandleTransmit();
}{ ... }
void CoapSecure::HandleTransmit(void)
{
Error error = kErrorNone;
ot::Message *message = mTransmitQueue.GetHead();
VerifyOrExit(message != nullptr);
mTransmitQueue.Dequeue(*message);
if (mTransmitQueue.GetHead() != nullptr)
{
mTransmitTask.Post();
}{...}
SuccessOrExit(error = mDtls.Send(*message, message->GetLength()));
exit:
if (error != kErrorNone)
{
LogNote("Transmit: %s", ErrorToString(error));
message->Free();
}{...}
else
{
LogDebg("Transmit: %s", ErrorToString(error));
}{...}
}{ ... }
}{...}
}{...}
/* ... */
#endif