Dokit 0.5.3-pre
Internal development documentation
Loading...
Searching...
No Matches
infocommand.cpp
1// SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "infocommand.h"
5
8
9#include <QJsonDocument>
10#include <QJsonObject>
11
12#include <iostream>
13
14/*!
15 * \class InfoCommand
16 *
17 * The InfoCommand class implements the `info` CLI command.
18 */
19
20/*!
21 * Construct a new InfoCommand object with \a parent.
22 */
24{
25
26}
27
33
38
39/*!
40 * \copybrief DeviceCommand::processOptions
41 *
42 * This implementation extends DeviceCommand::processOptions to process additional CLI options
43 * supported (or required) by this command.
44 */
46{
48 if (!errors.isEmpty()) {
49 return errors;
50 }
51
52 return errors;
53}
54
55/*!
56 * \copybrief DeviceCommand::getService
57 *
58 * This override returns a pointer to a DeviceInfoService object.
59 */
61{
62 Q_ASSERT(device);
63 if (!service) {
65 Q_ASSERT(service);
66 }
67 return service;
68}
69
70/*!
71 * \copybrief DeviceCommand::serviceDetailsDiscovered
72 *
73 * This override fetches the current device's information, and outputs it in the selected format.
74 */
76{
77 DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
78 const QLowEnergyController * const controller = (device) ? device->controller() : nullptr;
79 const QString deviceName = (controller) ? controller->remoteName() : QString();
80 const QBluetoothAddress deviceAddress = (controller) ? controller->remoteAddress() : QBluetoothAddress();
81 const QBluetoothUuid deviceUuid = (controller) ? controller->remoteDeviceUuid() : QBluetoothUuid();
82 const QString serialNumber = service->serialNumber();
83 switch (format) {
85 std::cout << qUtf8Printable(tr("device_name,device_address,device_uuid,manufacturer_name,model_number,"
86 "hardware_revision,firmware_revision,software_revision,serial_number\n"));
87 std::cout << qUtf8Printable(QString::fromLatin1("%1,%2,%3,%4,%5,%6,%7,%8,%9\n").arg(
88 escapeCsvField(deviceName),
89 (deviceAddress.isNull()) ? QString() : deviceAddress.toString(),
90 (deviceUuid.isNull()) ? QString() : deviceUuid.toString(),
94 break;
95 case OutputFormat::Json: {
96 QJsonObject jsonObject{
97 { QLatin1String("manufacturerName"), service->manufacturer() },
98 { QLatin1String("modelNumber"), service->modelNumber() },
99 { QLatin1String("hardwareRevision"), service->hardwareRevision() },
100 { QLatin1String("firmwareRevision"), service->firmwareRevision() },
101 { QLatin1String("softwareRevision"), service->softwareRevision() },
102 };
103 if (!deviceName.isEmpty()) {
104 jsonObject.insert(QLatin1String("deviceName"), deviceName);
105 }
106 if (!deviceAddress.isNull()) {
107 jsonObject.insert(QLatin1String("deviceAddress"), deviceAddress.toString());
108 }
109 if (!deviceUuid.isNull()) {
110 jsonObject.insert(QLatin1String("deviceUuid"), deviceUuid.toString());
111 }
112 if (!serialNumber.isNull()) {
113 jsonObject.insert(QLatin1String("serialNumber"), serialNumber);
114 }
115 std::cout << QJsonDocument(jsonObject).toJson().toStdString();
116 } break;
118 if (!deviceName.isEmpty()) {
119 std::cout << qUtf8Printable(tr("Device name: %1\n").arg(deviceName));
120 }
121 if (!deviceAddress.isNull()) {
122 std::cout << qUtf8Printable(tr("Device addres: %1\n").arg(deviceAddress.toString()));
123 }
124 if (!deviceUuid.isNull()) {
125 std::cout << qUtf8Printable(tr("Device UUID: %1\n").arg(deviceUuid.toString()));
126 }
127 std::cout << qUtf8Printable(tr("Manufacturer name: %1\n").arg(service->manufacturer()));
128 std::cout << qUtf8Printable(tr("Model number: %1\n").arg(service->modelNumber()));
129 std::cout << qUtf8Printable(tr("Hardware revision: %1\n").arg(service->hardwareRevision()));
130 std::cout << qUtf8Printable(tr("Firmware revision: %1\n").arg(service->firmwareRevision()));
131 std::cout << qUtf8Printable(tr("Software revision: %1\n").arg(service->softwareRevision()));
132 if (!serialNumber.isNull()) {
133 std::cout << qUtf8Printable(tr("Serial number: %1\n").arg(serialNumber));
134 }
135 break;
136 }
137 if (device) disconnect(); // Will exit the application once disconnected.
138}
virtual QStringList supportedOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names supported by this command.
OutputFormat format
Selected output format.
@ Text
Plain unstructured text.
@ Csv
RFC 4180 compliant CSV text.
@ Json
RFC 8259 compliant JSON text.
virtual QStringList processOptions(const QCommandLineParser &parser)
Processes the relevant options from the command line parser.
static QString escapeCsvField(const QString &field)
Returns an RFC 4180 compliant version of field.
virtual QStringList requiredOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names required by this command.
The AbstractPokitService class provides a common base for Pokit services classes.
The AbstractCommand class extends AbstractCommand to add a PokitDevice instance.
PokitDevice * device
Pokit Bluetooth device (if any) this command interracts with.
virtual void serviceDetailsDiscovered()
Handles service detail discovery events.
void disconnect(int exitCode=EXIT_SUCCESS)
Disconnects the underlying Pokit device, and sets exitCode to be return to the OS once the disconnect...
QString serialNumber() const
Returns the most recent value of the Device Info service's (undocumented) Serial Number characteristi...
QString softwareRevision() const
Returns the most recent value of the Device Info service's Software Revision characteristic.
QString hardwareRevision() const
Returns the most recent value of the Device Info service's Hardware Revision characteristic.
QString modelNumber() const
Returns the most recent value of the Device Info service's Model Number characteristic.
QString manufacturer() const
Returns the most recent value of the Device Info service's Manufacturer Name characteristic.
QString firmwareRevision() const
Returns the most recent value of the Device Info service's Firmware Revision characteristic.
QStringList requiredOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names required by this command.
void serviceDetailsDiscovered() override
Handles service detail discovery events.
QStringList supportedOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names supported by this command.
DeviceInfoService * service
Bluetooth service this command interracts with.
Definition infocommand.h:28
AbstractPokitService * getService() override
Returns a Pokit service object for the derived command class.
QStringList processOptions(const QCommandLineParser &parser) override
Processes the relevant options from the command line parser.
InfoCommand(QObject *const parent=nullptr)
Construct a new InfoCommand object with parent.
DeviceInfoService * deviceInformation()
Returns a pointer to DeviceInformationService instance that uses this device's controller for access.
QLowEnergyController * controller()
Returns a non-const pointer to the controller used to access the Pokit device.
Declares the DeviceInfoService class.
Declares the PokitDevice class.
bool isNull() const const
QString toString() const const
std::string toStdString() const const
QByteArray toJson() const const
bool isEmpty() const const
QBluetoothAddress remoteAddress() const const
QBluetoothUuid remoteDeviceUuid() const const
QString remoteName() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString fromLatin1(const char *str, int size)
QString & insert(int position, QChar ch)
bool isEmpty() const const
bool isNull() const const
bool isNull() const const
QString toString() const const