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