Skip to content

Commit 2fd250d

Browse files
committed
moved begin variants to header
1 parent 1f20cdd commit 2fd250d

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ void loop() {
110110
Initialize the object using the hostname of the broker, the brokers port (default: `1883`) and the underlying Client class for network transport:
111111
112112
```c++
113+
void begin(Client &client);
113114
void begin(const char hostname[], Client &client);
114115
void begin(const char hostname[], int port, Client &client);
116+
void begin(IPAddress address, Client &client);
117+
void begin(IPAddress address, int port, Client &client);
115118
```
116119

117120
- Specify port `8883` when using secure clients for encrypted connections.
@@ -122,6 +125,8 @@ The hostname and port can also be changed after calling `begin()`:
122125
```c++
123126
void setHost(const char hostname[]);
124127
void setHost(const char hostname[], int port);
128+
void setHost(IPAddress address);
129+
void setHost(IPAddress address, int port);
125130
```
126131
127132
Set a will message (last testament) that gets registered on the broker after connecting. `setWill()` has to be called before calling `connect()`:
@@ -171,9 +176,9 @@ void setClockSource(MQTTClientClockSource);
171176
Connect to broker using the supplied client id and an optional username and password:
172177
173178
```c++
174-
bool connect(const char clientId[], bool skip = false);
175-
bool connect(const char clientId[], const char username[], bool skip = false);
176-
bool connect(const char clientId[], const char username[], const char password[], bool skip = false);
179+
bool connect(const char clientID[], bool skip = false);
180+
bool connect(const char clientID[], const char username[], bool skip = false);
181+
bool connect(const char clientID[], const char username[], const char password[], bool skip = false);
177182
```
178183

179184
- If the `skip` option is set to true, the client will skip the network level connection and jump to the MQTT level connection. This option can be used in order to establish and verify TLS connections manually before giving control to the MQTT client.

src/MQTTClient.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,9 @@ MQTTClient::~MQTTClient() {
140140
free(this->writeBuf);
141141
}
142142

143-
void MQTTClient::begin(IPAddress ipAddr, int port, Client &client) {
144-
// set hostname and port
145-
this->setHost(ipAddr, port);
146-
// finish up begin() call
147-
_begin(port, client);
148-
}
149-
150-
void MQTTClient::begin(const char hostname[], int port, Client &client) {
151-
// set hostname and port
152-
this->setHost(hostname, port);
153-
// finish up begin() call
154-
_begin(port, client);
155-
}
156-
157-
void MQTTClient::_begin(int port, Client &client){
143+
void MQTTClient::begin(Client &_client) {
158144
// set client
159-
this->netClient = &client;
145+
this->netClient = &_client;
160146

161147
// initialize client
162148
lwmqtt_init(&this->client, this->writeBuf, this->bufSize, this->readBuf, this->bufSize);
@@ -190,10 +176,9 @@ void MQTTClient::setClockSource(MQTTClientClockSource cb) {
190176
this->timer2.millis = cb;
191177
}
192178

193-
void MQTTClient::setHost(IPAddress _ipAddr, int _port) {
194-
// set hostname and port
195-
this->ipaddress = _ipAddr;
196-
179+
void MQTTClient::setHost(IPAddress _address, int _port) {
180+
// set address and port
181+
this->address = _address;
197182
this->port = _port;
198183
}
199184

@@ -292,7 +277,7 @@ bool MQTTClient::publish(const char topic[], const char payload[], int length, b
292277
return true;
293278
}
294279

295-
bool MQTTClient::connect(const char clientId[], const char username[], const char password[], bool skip) {
280+
bool MQTTClient::connect(const char clientID[], const char username[], const char password[], bool skip) {
296281
// close left open connection if still connected
297282
if (!skip && this->connected()) {
298283
this->close();
@@ -303,13 +288,12 @@ bool MQTTClient::connect(const char clientId[], const char username[], const cha
303288

304289
// connect to host
305290
if (!skip) {
306-
int ret = 0;
307-
if(this->hostname != nullptr){
308-
ret = this->netClient->connect(this->hostname, (uint16_t)this->port);
309-
}else{
310-
ret = this->netClient->connect(this->ipaddress, (uint16_t)this->port);
311-
}
312-
291+
int ret;
292+
if (this->hostname != nullptr) {
293+
ret = this->netClient->connect(this->hostname, (uint16_t)this->port);
294+
} else {
295+
ret = this->netClient->connect(this->address, (uint16_t)this->port);
296+
}
313297
if (ret <= 0) {
314298
this->_lastError = LWMQTT_NETWORK_FAILED_CONNECT;
315299
return false;
@@ -320,7 +304,7 @@ bool MQTTClient::connect(const char clientId[], const char username[], const cha
320304
lwmqtt_options_t options = lwmqtt_default_options;
321305
options.keep_alive = this->keepAlive;
322306
options.clean_session = this->cleanSession;
323-
options.client_id = lwmqtt_string(clientId);
307+
options.client_id = lwmqtt_string(clientID);
324308

325309
// set username and password if available
326310
if (username != nullptr) {

src/MQTTClient.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MQTTClient {
4343

4444
Client *netClient = nullptr;
4545
const char *hostname = nullptr;
46-
IPAddress ipaddress;
46+
IPAddress address;
4747
int port = 0;
4848
lwmqtt_will_t *will = nullptr;
4949
MQTTClientCallback callback;
@@ -64,11 +64,17 @@ class MQTTClient {
6464

6565
~MQTTClient();
6666

67+
void begin(Client &_client);
6768
void begin(const char _hostname[], Client &_client) { this->begin(_hostname, 1883, _client); }
68-
void begin(const char hostname[], int port, Client &client);
69-
70-
void begin(IPAddress _ipAddr, Client &client) { this->begin(_ipAddr, 1883, client); }
71-
void begin(IPAddress ipAddr, int port, Client &client);
69+
void begin(const char _hostname[], int _port, Client &_client) {
70+
this->begin(_client);
71+
this->setHost(_hostname, _port);
72+
}
73+
void begin(IPAddress _address, Client &_client) { this->begin(_address, 1883, _client); }
74+
void begin(IPAddress _address, int _port, Client &_client) {
75+
this->begin(_client);
76+
this->setHost(_address, _port);
77+
}
7278

7379
void onMessage(MQTTClientCallbackSimple cb);
7480
void onMessageAdvanced(MQTTClientCallbackAdvanced cb);
@@ -77,9 +83,8 @@ class MQTTClient {
7783

7884
void setHost(const char _hostname[]) { this->setHost(_hostname, 1883); }
7985
void setHost(const char hostname[], int port);
80-
81-
void setHost(IPAddress _ipAddr) { this->setHost(_ipAddr, 1883); }
82-
void setHost(IPAddress ipAddr, int port);
86+
void setHost(IPAddress _address) { this->setHost(_address, 1883); }
87+
void setHost(IPAddress _address, int port);
8388

8489
void setWill(const char topic[]) { this->setWill(topic, ""); }
8590
void setWill(const char topic[], const char payload[]) { this->setWill(topic, payload, false, 0); }
@@ -95,7 +100,7 @@ class MQTTClient {
95100
bool connect(const char clientId[], const char username[], bool skip = false) {
96101
return this->connect(clientId, username, nullptr, skip);
97102
}
98-
bool connect(const char clientId[], const char username[], const char password[], bool skip = false);
103+
bool connect(const char clientID[], const char username[], const char password[], bool skip = false);
99104

100105
bool publish(const String &topic) { return this->publish(topic.c_str(), ""); }
101106
bool publish(const char topic[]) { return this->publish(topic, ""); }
@@ -136,7 +141,6 @@ class MQTTClient {
136141

137142
private:
138143
void close();
139-
void _begin(int port, Client &client);
140144
};
141145

142146
#endif

0 commit comments

Comments
 (0)