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