1 /* 2 * Copyright (c) 2020, 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 includes JSON formatter definition for RESTful HTTP server. 32 */ 33 34 #ifndef OTBR_REST_JSON_HPP_ 35 #define OTBR_REST_JSON_HPP_ 36 37 #include "openthread/link.h" 38 #include "openthread/thread_ftd.h" 39 40 #include "rest/types.hpp" 41 #include "utils/hex.hpp" 42 43 namespace otbr { 44 namespace rest { 45 46 /** 47 * The functions within this namespace provides a tranformation from an object/string/number to a serialized Json 48 * string. 49 * 50 */ 51 namespace Json { 52 53 /** 54 * This method formats an integer to a Json number and serialize it to a string. 55 * 56 * @param[in] aNumber An integer need to be format. 57 * 58 * @returns A string of serialized Json number. 59 * 60 */ 61 std::string Number2JsonString(const uint32_t &aNumber); 62 63 /** 64 * This method formats a Bytes array to a Json string and serialize it to a string. 65 * 66 * @param[in] aBytes A Bytes array representing a hex number. 67 * 68 * @returns A string of serialized Json string. 69 * 70 */ 71 std::string Bytes2HexJsonString(const uint8_t *aBytes, uint8_t aLength); 72 73 /** 74 * This method formats a C string to a Json string and serialize it to a string. 75 * 76 * @param[in] aCString A char pointer pointing to a C string. 77 * 78 * @returns A string of serialized Json string. 79 * 80 */ 81 std::string CString2JsonString(const char *aCString); 82 83 /** 84 * This method formats a string to a Json string and serialize it to a string. 85 * 86 * @param[in] aString A string. 87 * 88 * @returns A string of serialized Json string. 89 * 90 */ 91 std::string String2JsonString(const std::string &aString); 92 93 /** 94 * This method formats a Node object to a Json object and serialize it to a string. 95 * 96 * @param[in] aNode A Node object. 97 * 98 * @returns A string of serialized Json object. 99 * 100 */ 101 std::string Node2JsonString(const NodeInfo &aNode); 102 103 /** 104 * This method formats a vector of diagnostic objects to a Json array and serialize it to a string. 105 * 106 * @param[in] aDiagSet A vector of diagnostic objects. 107 * 108 * @returns A string of serialized Json array. 109 * 110 */ 111 std::string Diag2JsonString(const std::vector<std::vector<otNetworkDiagTlv>> &aDiagSet); 112 113 /** 114 * This method formats an Ipv6Address to a Json string and serialize it to a string. 115 * 116 * @param[in] aAddress An Ip6Address object. 117 * 118 * @returns A string of serialized Json string. 119 * 120 */ 121 std::string IpAddr2JsonString(const otIp6Address &aAddress); 122 123 /** 124 * This method formats a LinkModeConfig object to a Json object and serialize it to a string. 125 * 126 * @param[in] aMode A LinkModeConfig object. 127 * 128 * @returns A string of serialized Json object. 129 * 130 */ 131 std::string Mode2JsonString(const otLinkModeConfig &aMode); 132 133 /** 134 * This method formats a Connectivity object to a Json object and serialize it to a string. 135 * 136 * @param[in] aConnectivity A Connectivity object. 137 * 138 * @returns A string of serialized Json object. 139 * 140 */ 141 std::string Connectivity2JsonString(const otNetworkDiagConnectivity &aConnectivity); 142 143 /** 144 * This method formats a Route object to a Json object and serialize it to a string. 145 * 146 * @param[in] aRoute A Route object. 147 * 148 * @returns A string of serialized Json object. 149 * 150 */ 151 std::string Route2JsonString(const otNetworkDiagRoute &aRoute); 152 153 /** 154 * This method formats a RouteData object to a Json object and serialize it to a string. 155 * 156 * @param[in] aRouteData A RouteData object. 157 * 158 * @returns A string of serialized Json object. 159 * 160 */ 161 std::string RouteData2JsonString(const otNetworkDiagRouteData &aRouteData); 162 163 /** 164 * This method formats a LeaderData object to a Json object and serialize it to a string. 165 * 166 * @param[in] aLeaderData A LeaderData object. 167 * 168 * @returns A string of serialized Json object. 169 * 170 */ 171 std::string LeaderData2JsonString(const otLeaderData &aLeaderData); 172 173 /** 174 * This method formats a MacCounters object to a Json object and serialize it to a string. 175 * 176 * @param[in] aMacCounters A MacCounters object. 177 * 178 * @returns A string of serialized Json object. 179 * 180 */ 181 std::string MacCounters2JsonString(const otNetworkDiagMacCounters &aMacCounters); 182 183 /** 184 * This method formats a ChildEntry object to a Json object and serialize it to a string. 185 * 186 * @param[in] aChildEntry A ChildEntry object. 187 * 188 * @returns A string of serialized Json object. 189 * 190 */ 191 std::string ChildTableEntry2JsonString(const otNetworkDiagChildEntry &aChildEntry); 192 193 /** 194 * This method formats an error code and an error message to a Json object and serialize it to a string. 195 * 196 * @param[in] aErrorCode An enum HttpStatusCode such as '404'. 197 * @param[in] aErrorMessage Error message such as '404 Not Found'. 198 * 199 * @returns A string of serialized Json object. 200 * 201 */ 202 std::string Error2JsonString(HttpStatusCode aErrorCode, std::string aErrorMessage); 203 204 }; // namespace Json 205 206 } // namespace rest 207 } // namespace otbr 208 209 #endif // OTBR_REST_JSON_HPP_ 210