OpenThread  1.05.03.02
cli_coap_secure.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef CLI_COAP_SECURE_HPP_
35 #define CLI_COAP_SECURE_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
40 
41 #include <mbedtls/ssl.h>
42 
43 #include <openthread/coap_secure.h>
44 
45 #include "cli/cli_output.hpp"
46 
47 #ifndef CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER
48 #define CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER 0
49 #endif
50 
51 namespace ot {
52 namespace Cli {
53 
58 class CoapSecure : private OutputWrapper
59 {
60 public:
61  typedef Utils::CmdLineParser::Arg Arg;
62 
69  explicit CoapSecure(Output &aOutput);
70 
77  otError Process(Arg aArgs[]);
78 
79 private:
80  enum
81  {
82  kMaxUriLength = 32,
83  kMaxBufferSize = 16,
84  kPskMaxLength = 32,
85  kPskIdMaxLength = 32
86  };
87 
88  using Command = CommandEntry<CoapSecure>;
89 
90 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
91  enum BlockType : uint8_t{
92  kBlockType1,
93  kBlockType2,
94  };
95 #endif
96 
97  void PrintPayload(otMessage *aMessage);
98 
99  otError ProcessConnect(Arg aArgs[]);
100  otError ProcessDelete(Arg aArgs[]);
101  otError ProcessDisconnect(Arg aArgs[]);
102  otError ProcessGet(Arg aArgs[]);
103  otError ProcessHelp(Arg aArgs[]);
104  otError ProcessPost(Arg aArgs[]);
105  otError ProcessPsk(Arg aArgs[]);
106  otError ProcessPut(Arg aArgs[]);
107  otError ProcessResource(Arg aArgs[]);
108  otError ProcessSet(Arg aArgs[]);
109  otError ProcessStart(Arg aArgs[]);
110  otError ProcessStop(Arg aArgs[]);
111  otError ProcessX509(Arg aArgs[]);
112 
113  otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode);
114 
115  void Stop(void);
116 
117  static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
118  void HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo);
119 
120  static void HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
121  void HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
122 
123 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
124 
125  static otError BlockwiseReceiveHook(void * aContext,
126  const uint8_t *aBlock,
127  uint32_t aPosition,
128  uint16_t aBlockLength,
129  bool aMore,
130  uint32_t aTotalLength);
131  otError BlockwiseReceiveHook(const uint8_t *aBlock,
132  uint32_t aPosition,
133  uint16_t aBlockLength,
134  bool aMore,
135  uint32_t aTotalLength);
136  static otError BlockwiseTransmitHook(void * aContext,
137  uint8_t * aBlock,
138  uint32_t aPosition,
139  uint16_t *aBlockLength,
140  bool * aMore);
141  otError BlockwiseTransmitHook(uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore);
142 #endif
143 
144 #if CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER
145  static void DefaultHandler(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
146  void DefaultHandler(otMessage *aMessage, const otMessageInfo *aMessageInfo);
147 #endif // CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER
148 
149  static void HandleConnected(bool aConnected, void *aContext);
150  void HandleConnected(bool aConnected);
151 
152  static constexpr Command sCommands[] = {
153  {"connect", &CoapSecure::ProcessConnect},
154  {"delete", &CoapSecure::ProcessDelete},
155  {"disconnect", &CoapSecure::ProcessDisconnect},
156  {"get", &CoapSecure::ProcessGet},
157  {"help", &CoapSecure::ProcessHelp},
158  {"post", &CoapSecure::ProcessPost},
159 #ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
160  {"psk", &CoapSecure::ProcessPsk},
161 #endif
162  {"put", &CoapSecure::ProcessPut},
163  {"resource", &CoapSecure::ProcessResource},
164  {"set", &CoapSecure::ProcessSet},
165  {"start", &CoapSecure::ProcessStart},
166  {"stop", &CoapSecure::ProcessStop},
167 #ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
168  {"x509", &CoapSecure::ProcessX509},
169 #endif
170  };
171 
172  static_assert(BinarySearch::IsSorted(sCommands), "Command Table is not sorted");
173 
174 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
175  otCoapBlockwiseResource mResource;
176 #else
177  otCoapResource mResource;
178 #endif
179  char mUriPath[kMaxUriLength];
180  char mResourceContent[kMaxBufferSize];
181 
182  bool mShutdownFlag;
183  bool mUseCertificate;
184  uint8_t mPsk[kPskMaxLength];
185  uint8_t mPskLength;
186  uint8_t mPskId[kPskIdMaxLength];
187  uint8_t mPskIdLength;
188 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
189  uint32_t mBlockCount;
190 #endif
191 };
192 
193 } // namespace Cli
194 } // namespace ot
195 
196 #endif // OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
197 
198 #endif // CLI_COAP_SECURE_HPP_
Definition: cli.cpp:92
otCoapCode
Definition: include/openthread/coap.h:91
Definition: include/openthread/ip6.h:226
static constexpr bool IsSorted(const Entry(&aTable)[kLength])
Definition: binary_search.hpp:100
Definition: include/openthread/coap.h:415
enum OT_MUST_USE_RESULT otError otError
Definition: error.h:58
This file defines the top-level functions for the OpenThread CoAP Secure implementation.
Definition: include/openthread/coap.h:427
Definition: message.hpp:66
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale