aboutsummaryrefslogtreecommitdiffstats
path: root/examples/coap/consolecoapclient/main.cpp
blob: 13556e897cf153ad9479228bbaa13fac5beed866 (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
// Copyright (C) 2017 Witekio.
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "coaphandler.h"

#include <QCoreApplication>
#include <QCommandLineParser>
#include <QUrl>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QCommandLineParser parser;
    parser.setApplicationDescription("Qt CoAP test application");
    parser.addHelpOption();
    parser.addPositionalArgument("url",
                                 "The targeted coap resource URL, e.g. 'coap://10.10.10.10/myresource'");
    parser.addOption({"get", "Use GET method for the request. This is the default method"});
    parser.addOption({"post", "Use POST method for the request"});
    parser.addOption({"put", "Use PUT method for the request"});
    parser.addOption({"delete", "Use DELETE method for the request"});
    parser.addOption({"observe", "Observe a resource"});
    parser.addOption({"discover", "Discover available resources"});

    parser.process(a);

    if (parser.positionalArguments().isEmpty()) {
        qWarning("Please provide an url for the request");
        parser.showHelp(1);
    }

    QUrl url = parser.positionalArguments().first();
    if (!url.isValid()) {
        qWarning("The url provided is not valid.");
        parser.showHelp(1);
    }

    CoapHandler handler;
    if (parser.isSet("get"))        handler.runGet(url);
    if (parser.isSet("post"))       handler.runPost(url);
    if (parser.isSet("put"))        handler.runPut(url);
    if (parser.isSet("delete"))     handler.runDelete(url);
    if (parser.isSet("observe"))    handler.runObserve(url);
    if (parser.isSet("discover"))   handler.runDiscover(url);

    return a.exec();
}