aboutsummaryrefslogtreecommitdiffstats
path: root/examples/coap/doc/simplecoapclient.qdoc
blob: 438442d1f693a66afeaab431ca88acb5f6fa8b4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example simplecoapclient
    \title Simple CoAP Client Example
    \ingroup qtcoap-examples
    \brief Creating an application that communicates with a CoAP server.

    \image simplecoapclient.png

    \e {Simple CoAP Client} demonstrates how to create a minimalistic CoAP client
    application to send and receive CoAP messages.

    \section1 Setting Up a CoAP Server

    To use the application, you need to specify a CoAP server. You have the following
    options:

    \list
        \li Use the CoAP test server located at \c {coap://coap.me}.
        \li Create a CoAP server using \l {https://github.com/obgm/libcoap} {libcoap},
        \l {https://github.com/keith-cullen/FreeCoAP} {FreeCoAP} or any other CoAP
        server implementation.
        \li Use the \l {https://github.com/eclipse/californium/} {Californium} plugtest
        server, which supports most of the CoAP features. You can build it manually or
        use a ready Docker image, which builds and starts the plugtest server. The steps
        for using the docker-based server are described below.

    \endlist

    \section2 Using the Docker-based Test Server

    The following command pulls the docker container for the CoAP server from the Docker
    Hub and starts it:

    \badcode
    docker run --name coap-test-server -d --rm -p 5683:5683/udp -p 5684:5684/udp sokurazy/coap-secure-test-server:californium.1.1.0
    \endcode

    To find out the IP address of the docker container, first retrieve the container ID
    by running \c {docker ps}, which will output something like:

    \badcode
    $ docker ps
    CONTAINER ID        IMAGE
    5e46502df88f        sonakur/coap-secure-test-server:californium.1.1.0
    \endcode

    Then you can obtain the IP address with the following command:

    \badcode
    docker inspect <container_id> | grep IPAddress
    \endcode

    For example:

    \badcode
    $ docker inspect 5e46502df88f | grep IPAddress
    ...
    "IPAddress": "172.17.0.2",
    ...
    \endcode

    The CoAP test server will be reachable by the retrieved IP address on ports
    \e 5683 (non-secure) and \e 5684 (secure).

    \section1 Creating a Client

    The first step is to create a CoAP client using the QCoapClient class. Then
    we need to connect its signals, to get notified when a CoAP reply is received
    or a request has failed:

    \quotefromfile simplecoapclient/mainwindow.cpp
    \skipto MainWindow::MainWindow
    \printuntil onError
    \dots

    \section1 Sending Requests
    We use the QCoapRequest class to create CoAP requests. This class provides methods
    for constructing CoAP frames.

    \skipto on_runButton_clicked
    \printuntil addOption
    \dots

    In this example, we set the URL, as well as the message type and add options
    to the request. It is also possible to set the payload, message ID, token,
    and so on, but we are using the default values here. Note that by default,
    the message ID and token are generated randomly.

    Based on the selected request method, we send a \c GET, \c PUT, \c POST or
    \c DELETE request to the server:

    \skipto switch
    \dots
    \printuntil }
    \dots

    For \c PUT and \c POST requests we also add \c m_currentData as a payload for
    the request.

    For browsing the contents of the server and discovering the resources available on
    it, a \e discovery request is used:

    \skipto on_discoverButton_clicked
    \printuntil {
    \dots
    \skipto discoverReply
    \printuntil onDiscovered
    \dots

    \note Instead of QCoapReply class, we use the QCoapResourceDiscoveryReply to keep
    the reply for a discovery request. It has the QCoapResourceDiscoveryReply::discovered
    signal, which returns the list of QCoapResources that has been discovered.

    If there are \e observable resources on the server (meaning that they have the
    resource type \c obs), we can subscribe to updates on that resource by running
    an \e observe request:

    \skipto on_observeButton_clicked
    \printuntil {
    \dots
    \skipto observeReply
    \printline observeReply
    \dots
    \skipto onNotified
    \printline onNotified
    \dots

    The client can unsubscribe from the resource observation by handling the
    \c clicked() signal of the \c cancelObserveButton:

    \skipto clicked
    \dots
    \printuntil })

    The responses coming from the server are displayed in the UI:

    \quotefromfile simplecoapclient/mainwindow.cpp
    \skipto MainWindow::addMessage
    \printto on_runButton_clicked
*/