• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017, 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 
29 /**
30  * @file
31  *   This file contains definitions for a simple CLI CoAP server and client.
32  */
33 
34 #ifndef CLI_COAP_HPP_
35 #define CLI_COAP_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_COAP_API_ENABLE
40 
41 #include <openthread/coap.h>
42 
43 #include "cli/cli_utils.hpp"
44 
45 namespace ot {
46 namespace Cli {
47 
48 /**
49  * Implements the CLI CoAP server and client.
50  */
51 class Coap : private Utils
52 {
53 public:
54     /**
55      * Constructor
56      *
57      * @param[in]  aInstance            The OpenThread Instance.
58      * @param[in]  aOutputImplementer   An `OutputImplementer`.
59      */
60     Coap(otInstance *aInstance, OutputImplementer &aOutputImplementer);
61 
62     /**
63      * Processes a CLI sub-command.
64      *
65      * @param[in]  aArgs     An array of command line arguments.
66      *
67      * @retval OT_ERROR_NONE              Successfully executed the CLI command.
68      * @retval OT_ERROR_PENDING           The CLI command was successfully started but final result is pending.
69      * @retval OT_ERROR_INVALID_COMMAND   Invalid or unknown CLI command.
70      * @retval OT_ERROR_INVALID_ARGS      Invalid arguments.
71      * @retval ...                        Error during execution of the CLI command.
72      */
73     otError Process(Arg aArgs[]);
74 
75 private:
76     static constexpr uint16_t kMaxUriLength  = 32;
77     static constexpr uint16_t kMaxBufferSize = 16;
78 
79     using Command = CommandEntry<Coap>;
80 
81 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
82     enum BlockType : uint8_t{
83         kBlockType1,
84         kBlockType2,
85     };
86 #endif
87 
88     template <CommandId kCommandId> otError Process(Arg aArgs[]);
89 
90 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
91     otError CancelResourceSubscription(void);
92     void    CancelSubscriber(void);
93 #endif
94 
95     void PrintPayload(otMessage *aMessage);
96 
97 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
98     otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve = false);
99 #else
100     otError        ProcessRequest(Arg aArgs[], otCoapCode aCoapCode);
101 #endif
102 
103     static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
104     void        HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo);
105 
106 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
107     static void HandleNotificationResponse(void                *aContext,
108                                            otMessage           *aMessage,
109                                            const otMessageInfo *aMessageInfo,
110                                            otError              aError);
111     void        HandleNotificationResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
112 #endif
113 
114     static void HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
115     void        HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
116 
117 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
118 
119     static otError BlockwiseReceiveHook(void          *aContext,
120                                         const uint8_t *aBlock,
121                                         uint32_t       aPosition,
122                                         uint16_t       aBlockLength,
123                                         bool           aMore,
124                                         uint32_t       aTotalLength);
125     otError        BlockwiseReceiveHook(const uint8_t *aBlock,
126                                         uint32_t       aPosition,
127                                         uint16_t       aBlockLength,
128                                         bool           aMore,
129                                         uint32_t       aTotalLength);
130     static otError BlockwiseTransmitHook(void     *aContext,
131                                          uint8_t  *aBlock,
132                                          uint32_t  aPosition,
133                                          uint16_t *aBlockLength,
134                                          bool     *aMore);
135     otError        BlockwiseTransmitHook(uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore);
136 #endif
137 
GetRequestTxParameters(void) const138     const otCoapTxParameters *GetRequestTxParameters(void) const
139     {
140         return mUseDefaultRequestTxParameters ? nullptr : &mRequestTxParameters;
141     }
142 
GetResponseTxParameters(void) const143     const otCoapTxParameters *GetResponseTxParameters(void) const
144     {
145         return mUseDefaultResponseTxParameters ? nullptr : &mResponseTxParameters;
146     }
147 
148     bool mUseDefaultRequestTxParameters;
149     bool mUseDefaultResponseTxParameters;
150 
151     otCoapTxParameters mRequestTxParameters;
152     otCoapTxParameters mResponseTxParameters;
153 
154 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
155     otCoapBlockwiseResource mResource;
156 #else
157     otCoapResource mResource;
158 #endif
159 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
160     otIp6Address mRequestAddr;
161     otSockAddr   mSubscriberSock;
162     char         mRequestUri[kMaxUriLength];
163     uint8_t      mRequestToken[OT_COAP_MAX_TOKEN_LENGTH];
164     uint8_t      mSubscriberToken[OT_COAP_MAX_TOKEN_LENGTH];
165 #endif
166     char mUriPath[kMaxUriLength];
167     char mResourceContent[kMaxBufferSize];
168 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
169     uint32_t mObserveSerial;
170     uint8_t  mRequestTokenLength;
171     uint8_t  mSubscriberTokenLength;
172     bool     mSubscriberConfirmableNotifications;
173 #endif
174 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
175     uint32_t mBlockCount;
176 #endif
177 };
178 
179 } // namespace Cli
180 } // namespace ot
181 
182 #endif // OPENTHREAD_CONFIG_COAP_API_ENABLE
183 
184 #endif // CLI_COAP_HPP_
185