• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wirenboard / wb-mqtt-adc / 56

31 Jul 2026 10:38AM UTC coverage: 56.281% (-1.1%) from 57.332%
56

push

github

web-flow
Use exported vars instead of MAKEFLAGS for coverage options (#51)

282 of 466 branches covered (60.52%)

457 of 812 relevant lines covered (56.28%)

4.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main.cpp
1
#include <chrono>
2
#include <getopt.h>
3
#include <iostream>
4

5
#include <functional>
6
#include <wblib/log.h>
7
#include <wblib/signal_handling.h>
8
#include <wblib/wbmqtt.h>
9

10
#include "adc_driver.h"
11
#include "config.h"
12
#include "log.h"
13

14
using namespace std;
15
using namespace WBMQTT;
16

17
namespace
18
{
19
    //! Maximum timeout before forced application termination. Topic cleanup can take a lot of time
20
    const auto DRIVER_STOP_TIMEOUT_S = chrono::seconds(5);
21

22
    //! Maximun time to start application. Exceded timeout will case application termination.
23
    const auto DRIVER_INIT_TIMEOUT_S = chrono::seconds(5);
24

25
    const auto CONFIG_FILE = "/etc/wb-mqtt-adc.conf";
26
    const auto SYSTEM_CONFIGS_DIR = "/var/lib/wb-mqtt-adc/conf.d";
27
    const auto CONFIG_SCHEMA_TEMPLATE_FILE = "/usr/share/wb-mqtt-adc/wb-mqtt-adc-template.schema.json";
28
    const auto SCHEMA_FILE = "/var/lib/wb-mqtt-confed/schemas/wb-mqtt-adc.schema.json";
29

30
    void PrintUsage()
×
31
    {
32
        cout << "Usage:" << endl
×
33
             << " wb-mqtt-adc [options]" << endl
×
34
             << "Options:" << endl
×
35
             << "  -d level     enable debuging output:" << endl
×
36
             << "                 1 - adc only;" << endl
×
37
             << "                 2 - mqtt only;" << endl
×
38
             << "                 3 - both;" << endl
×
39
             << "                 negative values - silent mode (-1, -2, -3))" << endl
×
40
             << "  -c config    config file" << endl
×
41
             << "  -p port      MQTT broker port (default: 1883)" << endl
×
42
             << "  -h IP        MQTT broker IP (default: localhost)" << endl
×
43
             << "  -u user      MQTT user (optional)" << endl
×
44
             << "  -P password  MQTT user password (optional)" << endl
×
45
             << "  -T prefix    MQTT topic prefix (optional)" << endl
×
46
             << "  -g           Generate JSON Schema for wb-mqtt-confed" << endl
×
47
             << "  -j           Make JSON for wb-mqtt-confed from /etc/wb-mqtt-adc.conf" << endl
×
48
             << "  -J           Make /etc/wb-mqtt-adc.conf from wb-mqtt-confed output" << endl;
×
49
    }
×
50

51
    void ParseCommadLine(int argc, char* argv[], TMosquittoMqttConfig& mqttConfig, string& customConfig)
×
52
    {
53
        int debugLevel = 0;
×
54
        int c;
55

56
        while ((c = getopt(argc, argv, "d:c:h:p:u:P:T:jJg")) != -1) {
×
57
            switch (c) {
×
58
                case 'd':
×
59
                    debugLevel = stoi(optarg);
×
60
                    break;
×
61
                case 'c':
×
62
                    customConfig = optarg;
×
63
                    break;
×
64
                case 'p':
×
65
                    mqttConfig.Port = stoi(optarg);
×
66
                    break;
×
67
                case 'h':
×
68
                    mqttConfig.Host = optarg;
×
69
                    break;
×
70
                case 'T':
×
71
                    mqttConfig.Prefix = optarg;
×
72
                    break;
×
73
                case 'u':
×
74
                    mqttConfig.User = optarg;
×
75
                    break;
×
76
                case 'P':
×
77
                    mqttConfig.Password = optarg;
×
78
                    break;
×
79
                case 'j':
×
80
                    try {
81
                        MakeJsonForConfed(CONFIG_FILE, SYSTEM_CONFIGS_DIR, SCHEMA_FILE);
×
82
                        exit(0);
×
83
                    } catch (const std::exception& e) {
×
84
                        ErrorLogger.Log() << "FATAL: " << e.what();
×
85
                        exit(1);
×
86
                    }
×
87
                case 'J':
×
88
                    try {
89
                        MakeConfigFromConfed(SYSTEM_CONFIGS_DIR, SCHEMA_FILE);
×
90
                        exit(0);
×
91
                    } catch (const std::exception& e) {
×
92
                        ErrorLogger.Log() << "FATAL: " << e.what();
×
93
                        exit(1);
×
94
                    }
×
95
                case 'g':
×
96
                    try {
97
                        MakeSchemaForConfed(SYSTEM_CONFIGS_DIR, CONFIG_SCHEMA_TEMPLATE_FILE, SCHEMA_FILE);
×
98
                        exit(0);
×
99
                    } catch (const std::exception& e) {
×
100
                        ErrorLogger.Log() << "FATAL: " << e.what();
×
101
                        exit(1);
×
102
                    }
×
103
                case '?':
×
104
                default:
105
                    PrintUsage();
×
106
                    exit(2);
×
107
            }
108
        }
109

110
        switch (debugLevel) {
×
111
            case 0:
×
112
                break;
×
113
            case -1:
×
114
                Info.SetEnabled(false);
×
115
                break;
×
116

117
            case -2:
×
118
                WBMQTT::Info.SetEnabled(false);
×
119
                break;
×
120

121
            case -3:
×
122
                WBMQTT::Info.SetEnabled(false);
×
123
                Info.SetEnabled(false);
×
124
                break;
×
125

126
            case 1:
×
127
                DebugLogger.SetEnabled(true);
×
128
                break;
×
129

130
            case 2:
×
131
                WBMQTT::Debug.SetEnabled(true);
×
132
                break;
×
133

134
            case 3:
×
135
                WBMQTT::Debug.SetEnabled(true);
×
136
                DebugLogger.SetEnabled(true);
×
137
                break;
×
138

139
            default:
×
140
                cout << "Invalid -d parameter value " << debugLevel << endl;
×
141
                PrintUsage();
×
142
                exit(2);
×
143
        }
144

145
        if (optind < argc) {
×
146
            for (int index = optind; index < argc; ++index) {
×
147
                cout << "Skipping unknown argument " << argv[index] << endl;
×
148
            }
149
        }
150
    }
×
151

152
    void PrintStartupInfo(const TMosquittoMqttConfig& mqttConfig, const string& customConfig)
×
153
    {
154
        cout << "MQTT broker " << mqttConfig.Host << ':' << mqttConfig.Port << endl;
×
155
        if (!customConfig.empty()) {
×
156
            cout << "Custom config " << customConfig << endl;
×
157
        }
158
    }
×
159

160
} // namespace
161

162
int main(int argc, char* argv[])
×
163
{
164
    TMosquittoMqttConfig mqttConfig{};
×
165
    mqttConfig.Id = "wb-adc";
×
166

167
    string customConfig;
×
168
    ParseCommadLine(argc, argv, mqttConfig, customConfig);
×
169
    PrintStartupInfo(mqttConfig, customConfig);
×
170

171
    TPromise<void> initialized;
×
172
    SetThreadName("wb-mqtt-adc");
×
173
    SignalHandling::Handle({SIGINT, SIGTERM});
×
174
    SignalHandling::OnSignals({SIGINT, SIGTERM}, [&] { SignalHandling::Stop(); });
×
175

176
    /* if signal arrived before driver is initialized:
177
        wait some time to initialize and then exit gracefully
178
        else if timed out: exit with error
179
    */
180
    SignalHandling::SetWaitFor(DRIVER_INIT_TIMEOUT_S, initialized.GetFuture(), [&] {
×
181
        ErrorLogger.Log() << "Driver takes too long to initialize. Exiting.";
×
182
        cerr << "Error: DRIVER_INIT_TIMEOUT_S" << endl;
×
183
        exit(1);
×
184
    });
185

186
    /* if handling of signal takes too much time: exit with error */
187
    SignalHandling::SetOnTimeout(DRIVER_STOP_TIMEOUT_S, [&] {
×
188
        ErrorLogger.Log() << "Driver takes too long to stop. Exiting.";
×
189
        cerr << "Error: DRIVER_STOP_TIMEOUT_S" << endl;
×
190
        exit(2);
×
191
    });
192
    SignalHandling::Start();
×
193

194
    try {
195
        TConfig config =
196
            LoadConfig(CONFIG_FILE, customConfig, SYSTEM_CONFIGS_DIR, SCHEMA_FILE, &InfoLogger, &WarnLogger);
×
197

198
        if (config.EnableDebugMessages)
×
199
            DebugLogger.SetEnabled(true);
×
200

201
        auto publishParameters = TPublishParameters();
×
202
        publishParameters.Set(config.MaxUnchangedInterval.count());
×
203

204
        auto mqttDriver = NewDriver(TDriverArgs{}
×
205
                                        .SetBackend(NewDriverBackend(NewMosquittoMqttClient(mqttConfig)))
×
206
                                        .SetId(mqttConfig.Id)
×
207
                                        .SetUseStorage(false)
×
208
                                        .SetReownUnknownDevices(true),
209
                                    publishParameters);
×
210

211
        mqttDriver->StartLoop();
×
212
        SignalHandling::OnSignals({SIGINT, SIGTERM}, [&] {
×
213
            mqttDriver->StopLoop();
×
214
            mqttDriver->Close();
×
215
        });
×
216

217
        mqttDriver->WaitForReady();
×
218

219
        TADCDriver driver(mqttDriver, config, ErrorLogger, DebugLogger, InfoLogger);
×
220

221
        SignalHandling::OnSignals({SIGINT, SIGTERM}, [&] { driver.Stop(); });
×
222

223
        initialized.Complete();
×
224
        SignalHandling::Wait();
×
225

226
    } catch (const exception& e) {
×
227
        ErrorLogger.Log() << "FATAL: " << e.what();
×
228
        SignalHandling::Stop();
×
229
        return 1;
×
230
    }
×
231

232
    return 0;
×
233
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc