• 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_output.hpp"
44 
45 namespace ot {
46 namespace Cli {
47 
48 /**
49  * This class implements the CLI CoAP server and client.
50  *
51  */
52 class Coap : private OutputWrapper
53 {
54 public:
55     typedef Utils::CmdLineParser::Arg Arg;
56 
57     /**
58      * Constructor
59      *
60      * @param[in]  aOutput The CLI console output context
61      *
62      */
63     explicit Coap(Output &aOutput);
64 
65     /**
66      * This method interprets a list of CLI arguments.
67      *
68      * @param[in]  aArgs        An array of command line arguments.
69      *
70      */
71     otError Process(Arg aArgs[]);
72 
73 private:
74     enum
75     {
76         kMaxUriLength  = 32,
77         kMaxBufferSize = 16
78     };
79 
80     using Command = CommandEntry<Coap>;
81 
82 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
83     enum BlockType : uint8_t{
84         kBlockType1,
85         kBlockType2,
86     };
87 #endif
88 
89     template <CommandId kCommandId> otError Process(Arg aArgs[]);
90 
91 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
92     otError CancelResourceSubscription(void);
93     void    CancelSubscriber(void);
94 #endif
95 
96     void PrintPayload(otMessage *aMessage);
97 
98 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
99     otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve = false);
100 #else
101     otError        ProcessRequest(Arg aArgs[], otCoapCode aCoapCode);
102 #endif
103 
104     static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
105     void        HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo);
106 
107 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
108     static void HandleNotificationResponse(void *               aContext,
109                                            otMessage *          aMessage,
110                                            const otMessageInfo *aMessageInfo,
111                                            otError              aError);
112     void        HandleNotificationResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
113 #endif
114 
115     static void HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
116     void        HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError);
117 
118 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
119 
120     static otError BlockwiseReceiveHook(void *         aContext,
121                                         const uint8_t *aBlock,
122                                         uint32_t       aPosition,
123                                         uint16_t       aBlockLength,
124                                         bool           aMore,
125                                         uint32_t       aTotalLength);
126     otError        BlockwiseReceiveHook(const uint8_t *aBlock,
127                                         uint32_t       aPosition,
128                                         uint16_t       aBlockLength,
129                                         bool           aMore,
130                                         uint32_t       aTotalLength);
131     static otError BlockwiseTransmitHook(void *    aContext,
132                                          uint8_t * aBlock,
133                                          uint32_t  aPosition,
134                                          uint16_t *aBlockLength,
135                                          bool *    aMore);
136     otError        BlockwiseTransmitHook(uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore);
137 #endif
138 
GetRequestTxParameters(void) const139     const otCoapTxParameters *GetRequestTxParameters(void) const
140     {
141         return mUseDefaultRequestTxParameters ? nullptr : &mRequestTxParameters;
142     }
143 
GetResponseTxParameters(void) const144     const otCoapTxParameters *GetResponseTxParameters(void) const
145     {
146         return mUseDefaultResponseTxParameters ? nullptr : &mResponseTxParameters;
147     }
148 
149     bool mUseDefaultRequestTxParameters;
150     bool mUseDefaultResponseTxParameters;
151 
152     otCoapTxParameters mRequestTxParameters;
153     otCoapTxParameters mResponseTxParameters;
154 
155 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
156     otCoapBlockwiseResource mResource;
157 #else
158     otCoapResource mResource;
159 #endif
160 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
161     otIp6Address mRequestAddr;
162     otSockAddr   mSubscriberSock;
163     char         mRequestUri[kMaxUriLength];
164     uint8_t      mRequestToken[OT_COAP_MAX_TOKEN_LENGTH];
165     uint8_t      mSubscriberToken[OT_COAP_MAX_TOKEN_LENGTH];
166 #endif
167     char mUriPath[kMaxUriLength];
168     char mResourceContent[kMaxBufferSize];
169 #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE
170     uint32_t mObserveSerial;
171     uint8_t  mRequestTokenLength;
172     uint8_t  mSubscriberTokenLength;
173     bool     mSubscriberConfirmableNotifications;
174 #endif
175 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
176     uint32_t mBlockCount;
177 #endif
178 };
179 
180 } // namespace Cli
181 } // namespace ot
182 
183 #endif // OPENTHREAD_CONFIG_COAP_API_ENABLE
184 
185 #endif // CLI_COAP_HPP_
186