| 1 | // Copyright (C) 2017 Witekio. |
| 2 | // Copyright (C) 2018 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #include "qcoapresource_p.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \class QCoapResource |
| 11 | \inmodule QtCoap |
| 12 | |
| 13 | \brief The QCoapResource class holds information about a discovered |
| 14 | resource. |
| 15 | |
| 16 | \reentrant |
| 17 | |
| 18 | The QCoapRequest contains data as the path and title of the resource |
| 19 | and other ancillary information. |
| 20 | |
| 21 | \sa QCoapResourceDiscoveryReply |
| 22 | */ |
| 23 | |
| 24 | /*! |
| 25 | Constructs a new QCoapResource. |
| 26 | */ |
| 27 | QCoapResource::QCoapResource() : |
| 28 | d(new QCoapResourcePrivate) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | /*! |
| 33 | Constructs a new CoAP resource as a copy of \a other, making the two |
| 34 | resources identical. |
| 35 | */ |
| 36 | QCoapResource::QCoapResource(const QCoapResource &other) : |
| 37 | d(other.d) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | /*! |
| 42 | Destroy the QCoapResource. |
| 43 | */ |
| 44 | QCoapResource::~QCoapResource() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /*! |
| 49 | Copies \a other into this resource, making the two resources identical. |
| 50 | Returns a reference to this QCoapResource. |
| 51 | */ |
| 52 | QCoapResource &QCoapResource::operator=(const QCoapResource &other) |
| 53 | { |
| 54 | d = other.d; |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | /*! |
| 59 | Swaps this resource with \a other. This operation is very fast and never fails. |
| 60 | */ |
| 61 | void QCoapResource::swap(QCoapResource &other) noexcept |
| 62 | { |
| 63 | d.swap(other&: other.d); |
| 64 | } |
| 65 | |
| 66 | /*! |
| 67 | Returns the host of the resource. |
| 68 | |
| 69 | \sa setHost() |
| 70 | */ |
| 71 | QHostAddress QCoapResource::host() const |
| 72 | { |
| 73 | return d->host; |
| 74 | } |
| 75 | |
| 76 | /*! |
| 77 | Returns the path of the resource. |
| 78 | |
| 79 | \sa setPath() |
| 80 | */ |
| 81 | QString QCoapResource::path() const |
| 82 | { |
| 83 | return d->path; |
| 84 | } |
| 85 | |
| 86 | /*! |
| 87 | Returns the title of the resource. |
| 88 | |
| 89 | \sa setTitle() |
| 90 | */ |
| 91 | QString QCoapResource::title() const |
| 92 | { |
| 93 | return d->title; |
| 94 | } |
| 95 | |
| 96 | /*! |
| 97 | Returns \c true if the resource is observable |
| 98 | |
| 99 | \sa setObservable() |
| 100 | */ |
| 101 | bool QCoapResource::observable() const |
| 102 | { |
| 103 | return d->observable; |
| 104 | } |
| 105 | |
| 106 | /*! |
| 107 | Returns the type of the resource. |
| 108 | |
| 109 | \sa setResourceType() |
| 110 | */ |
| 111 | QString QCoapResource::resourceType() const |
| 112 | { |
| 113 | return d->resourceType; |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | Returns the interface description of the resource. |
| 118 | |
| 119 | The Interface Description 'if' attribute is an opaque string used to |
| 120 | provide a name or URI indicating a specific interface definition used |
| 121 | to interact with the target resource. It is specified in |
| 122 | \l{https://tools.ietf.org/html/rfc6690#section-3.2}{RFC 6690}. |
| 123 | |
| 124 | \sa setInterface() |
| 125 | */ |
| 126 | QString QCoapResource::interface() const |
| 127 | { |
| 128 | return d->interface; |
| 129 | } |
| 130 | |
| 131 | /*! |
| 132 | Returns the maximum size of the resource. |
| 133 | |
| 134 | The maximum size estimate attribute 'sz' gives an indication of the |
| 135 | maximum size of the resource representation returned by performing a |
| 136 | GET on the target URI. It is specified in |
| 137 | \l{https://tools.ietf.org/html/rfc6690#section-3.3}{RFC 6690}. |
| 138 | |
| 139 | \sa setMaximumSize() |
| 140 | */ |
| 141 | int QCoapResource::maximumSize() const |
| 142 | { |
| 143 | return d->maximumSize; |
| 144 | } |
| 145 | |
| 146 | /*! |
| 147 | Returns the Content-Format code of the resource. |
| 148 | |
| 149 | The Content-Format code corresponds to the 'ct' attribute and provides a |
| 150 | hint about the Content-Formats this resource returns. It is specified |
| 151 | in \l{https://tools.ietf.org/html/rfc7252#section-7.2.1}{RFC 7252}. |
| 152 | |
| 153 | \sa setContentFormat() |
| 154 | */ |
| 155 | uint QCoapResource::contentFormat() const |
| 156 | { |
| 157 | return d->contentFormat; |
| 158 | } |
| 159 | |
| 160 | /*! |
| 161 | Sets the host of the resource to \a host. |
| 162 | |
| 163 | \sa host() |
| 164 | */ |
| 165 | void QCoapResource::setHost(const QHostAddress &host) |
| 166 | { |
| 167 | d->host = host; |
| 168 | } |
| 169 | |
| 170 | /*! |
| 171 | Sets the path of the resource to \a path. |
| 172 | |
| 173 | \sa path() |
| 174 | */ |
| 175 | void QCoapResource::setPath(const QString &path) |
| 176 | { |
| 177 | d->path = path; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | Sets the title of the resource to \a title. |
| 182 | |
| 183 | \sa title() |
| 184 | */ |
| 185 | void QCoapResource::setTitle(const QString &title) |
| 186 | { |
| 187 | d->title = title; |
| 188 | } |
| 189 | |
| 190 | /*! |
| 191 | Makes the resource observable if the \a observable |
| 192 | parameter is \c true. |
| 193 | |
| 194 | \sa observable() |
| 195 | */ |
| 196 | void QCoapResource::setObservable(bool observable) |
| 197 | { |
| 198 | d->observable = observable; |
| 199 | } |
| 200 | |
| 201 | /*! |
| 202 | Sets the resource type to \a resourceType. |
| 203 | |
| 204 | \sa resourceType() |
| 205 | */ |
| 206 | void QCoapResource::setResourceType(const QString &resourceType) |
| 207 | { |
| 208 | d->resourceType = resourceType; |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | Sets the interface of the resource to \a interface. |
| 213 | |
| 214 | \sa interface() |
| 215 | */ |
| 216 | void QCoapResource::setInterface(const QString &interface) |
| 217 | { |
| 218 | d->interface = interface; |
| 219 | } |
| 220 | |
| 221 | /*! |
| 222 | Sets the maximum size of the resource to \a maximumSize. |
| 223 | |
| 224 | \sa maximumSize() |
| 225 | */ |
| 226 | void QCoapResource::setMaximumSize(int maximumSize) |
| 227 | { |
| 228 | d->maximumSize = maximumSize; |
| 229 | } |
| 230 | |
| 231 | /*! |
| 232 | Sets the content format of the resource to \a contentFormat. The content |
| 233 | format can be one of the content formats defined in \l {CoAP Content-Formats Registry}. |
| 234 | |
| 235 | \note CoAP supports common content formats such as XML, JSON, and so on, but |
| 236 | these are text based and consequently heavy both in payload and in processing. |
| 237 | One of the recommended content formats to use with CoAP is CBOR, which is |
| 238 | designed to be used in such contexts. |
| 239 | |
| 240 | \sa contentFormat(), QCborStreamWriter, QCborStreamReader |
| 241 | */ |
| 242 | void QCoapResource::setContentFormat(uint contentFormat) |
| 243 | { |
| 244 | d->contentFormat = contentFormat; |
| 245 | } |
| 246 | |
| 247 | QT_END_NAMESPACE |
| 248 | |