1 | /* |
2 | SPDX-FileCopyrightText: 2012-2013 Jan Grulich <jgrulich@redhat.com> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "serialsetting.h" |
8 | #include "serialsetting_p.h" |
9 | |
10 | #include <QDebug> |
11 | |
12 | NetworkManager::SerialSettingPrivate::SerialSettingPrivate() |
13 | : name(NM_SETTING_SERIAL_SETTING_NAME) |
14 | , baud(57600) |
15 | , bits(8) |
16 | , parity(SerialSetting::NoParity) |
17 | , stopbits(1) |
18 | , sendDelay(0) |
19 | { |
20 | } |
21 | |
22 | NetworkManager::SerialSetting::SerialSetting() |
23 | : Setting(Setting::Serial) |
24 | , d_ptr(new SerialSettingPrivate()) |
25 | { |
26 | } |
27 | |
28 | NetworkManager::SerialSetting::SerialSetting(const Ptr &other) |
29 | : Setting(other) |
30 | , d_ptr(new SerialSettingPrivate()) |
31 | { |
32 | setBaud(other->baud()); |
33 | setBits(other->bits()); |
34 | setParity(other->parity()); |
35 | setStopbits(other->stopbits()); |
36 | setSendDelay(other->sendDelay()); |
37 | } |
38 | |
39 | NetworkManager::SerialSetting::~SerialSetting() |
40 | { |
41 | delete d_ptr; |
42 | } |
43 | |
44 | QString NetworkManager::SerialSetting::name() const |
45 | { |
46 | Q_D(const SerialSetting); |
47 | |
48 | return d->name; |
49 | } |
50 | |
51 | void NetworkManager::SerialSetting::setBaud(quint32 speed) |
52 | { |
53 | Q_D(SerialSetting); |
54 | |
55 | d->baud = speed; |
56 | } |
57 | |
58 | quint32 NetworkManager::SerialSetting::baud() const |
59 | { |
60 | Q_D(const SerialSetting); |
61 | |
62 | return d->baud; |
63 | } |
64 | |
65 | void NetworkManager::SerialSetting::setBits(quint32 byteWidgh) |
66 | { |
67 | Q_D(SerialSetting); |
68 | |
69 | d->bits = byteWidgh; |
70 | } |
71 | |
72 | quint32 NetworkManager::SerialSetting::bits() const |
73 | { |
74 | Q_D(const SerialSetting); |
75 | |
76 | return d->bits; |
77 | } |
78 | |
79 | void NetworkManager::SerialSetting::setParity(NetworkManager::SerialSetting::Parity paritysetting) |
80 | { |
81 | Q_D(SerialSetting); |
82 | |
83 | d->parity = paritysetting; |
84 | } |
85 | |
86 | NetworkManager::SerialSetting::Parity NetworkManager::SerialSetting::parity() const |
87 | { |
88 | Q_D(const SerialSetting); |
89 | |
90 | return d->parity; |
91 | } |
92 | |
93 | void NetworkManager::SerialSetting::setStopbits(quint32 number) |
94 | { |
95 | Q_D(SerialSetting); |
96 | |
97 | d->stopbits = number; |
98 | } |
99 | |
100 | quint32 NetworkManager::SerialSetting::stopbits() const |
101 | { |
102 | Q_D(const SerialSetting); |
103 | |
104 | return d->stopbits; |
105 | } |
106 | |
107 | void NetworkManager::SerialSetting::setSendDelay(quint64 delay) |
108 | { |
109 | Q_D(SerialSetting); |
110 | |
111 | d->sendDelay = delay; |
112 | } |
113 | |
114 | quint64 NetworkManager::SerialSetting::sendDelay() const |
115 | { |
116 | Q_D(const SerialSetting); |
117 | |
118 | return d->sendDelay; |
119 | } |
120 | |
121 | void NetworkManager::SerialSetting::fromMap(const QVariantMap &setting) |
122 | { |
123 | if (setting.contains(key: QLatin1String(NM_SETTING_SERIAL_BAUD))) { |
124 | setBaud(setting.value(key: QLatin1String(NM_SETTING_SERIAL_BAUD)).toUInt()); |
125 | } |
126 | |
127 | if (setting.contains(key: QLatin1String(NM_SETTING_SERIAL_BITS))) { |
128 | setBits(setting.value(key: QLatin1String(NM_SETTING_SERIAL_BITS)).toUInt()); |
129 | } |
130 | |
131 | if (setting.contains(key: QLatin1String(NM_SETTING_SERIAL_PARITY))) { |
132 | QChar character = setting.value(key: QLatin1String(NM_SETTING_SERIAL_PARITY)).toChar(); |
133 | |
134 | if (character == 'n') { |
135 | setParity(NoParity); |
136 | } else if (character == 'E') { |
137 | setParity(EvenParity); |
138 | } else if (character == 'o') { |
139 | setParity(OddParity); |
140 | } |
141 | } |
142 | |
143 | if (setting.contains(key: QLatin1String(NM_SETTING_SERIAL_STOPBITS))) { |
144 | setStopbits(setting.value(key: QLatin1String(NM_SETTING_SERIAL_STOPBITS)).toUInt()); |
145 | } |
146 | |
147 | if (setting.contains(key: QLatin1String(NM_SETTING_SERIAL_SEND_DELAY))) { |
148 | setSendDelay((Setting::SecretFlagType)setting.value(key: QLatin1String(NM_SETTING_SERIAL_SEND_DELAY)).toULongLong()); |
149 | } |
150 | } |
151 | |
152 | QVariantMap NetworkManager::SerialSetting::toMap() const |
153 | { |
154 | QVariantMap setting; |
155 | |
156 | if (baud() != 57600) { |
157 | setting.insert(key: QLatin1String(NM_SETTING_SERIAL_BAUD), value: baud()); |
158 | } |
159 | |
160 | if (bits() != 8) { |
161 | setting.insert(key: QLatin1String(NM_SETTING_SERIAL_BITS), value: bits()); |
162 | } |
163 | |
164 | if (parity() != NoParity) { |
165 | if (parity() == EvenParity) { |
166 | setting.insert(key: QLatin1String(NM_SETTING_SERIAL_PARITY), value: QChar('E')); |
167 | } else if (parity() == OddParity) { |
168 | setting.insert(key: QLatin1String(NM_SETTING_SERIAL_PARITY), value: QChar('o')); |
169 | } |
170 | } |
171 | |
172 | if (stopbits() != 1) { |
173 | setting.insert(key: QLatin1String(NM_SETTING_SERIAL_STOPBITS), value: stopbits()); |
174 | } |
175 | |
176 | if (sendDelay()) { |
177 | setting.insert(key: QLatin1String(NM_SETTING_SERIAL_SEND_DELAY), value: sendDelay()); |
178 | } |
179 | |
180 | return setting; |
181 | } |
182 | |
183 | QDebug NetworkManager::operator<<(QDebug dbg, const NetworkManager::SerialSetting &setting) |
184 | { |
185 | dbg.nospace() << "type: " << setting.typeAsString(type: setting.type()) << '\n'; |
186 | dbg.nospace() << "initialized: " << !setting.isNull() << '\n'; |
187 | |
188 | dbg.nospace() << NM_SETTING_SERIAL_BAUD << ": " << setting.baud() << '\n'; |
189 | dbg.nospace() << NM_SETTING_SERIAL_BITS << ": " << setting.bits() << '\n'; |
190 | dbg.nospace() << NM_SETTING_SERIAL_PARITY << ": " << setting.parity() << '\n'; |
191 | dbg.nospace() << NM_SETTING_SERIAL_STOPBITS << ": " << setting.stopbits() << '\n'; |
192 | dbg.nospace() << NM_SETTING_SERIAL_SEND_DELAY << ": " << setting.sendDelay() << '\n'; |
193 | |
194 | return dbg.maybeSpace(); |
195 | } |
196 | |