Skip to content

Commit 1f20cdd

Browse files
TMRh20256dpi
authored andcommitted
Fixes for initial commit
- Add private _begin function that both begin functions call for hostname and ipaddress usage - Use IPAddress directly on client call - Add _to _ipAddr and _port on setHost call
1 parent e0a1506 commit 1f20cdd

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

src/MQTTClient.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,18 @@ MQTTClient::~MQTTClient() {
143143
void MQTTClient::begin(IPAddress ipAddr, int port, Client &client) {
144144
// set hostname and port
145145
this->setHost(ipAddr, port);
146-
147-
// set client
148-
this->netClient = &client;
149-
150-
// initialize client
151-
lwmqtt_init(&this->client, this->writeBuf, this->bufSize, this->readBuf, this->bufSize);
152-
153-
// set timers
154-
lwmqtt_set_timers(&this->client, &this->timer1, &this->timer2, lwmqtt_arduino_timer_set, lwmqtt_arduino_timer_get);
155-
156-
// set network
157-
lwmqtt_set_network(&this->client, &this->network, lwmqtt_arduino_network_read, lwmqtt_arduino_network_write);
158-
159-
// set callback
160-
lwmqtt_set_callback(&this->client, (void *)&this->callback, MQTTClientHandler);
146+
// finish up begin() call
147+
_begin(port, client);
161148
}
162149

163150
void MQTTClient::begin(const char hostname[], int port, Client &client) {
164151
// set hostname and port
165152
this->setHost(hostname, port);
153+
// finish up begin() call
154+
_begin(port, client);
155+
}
166156

157+
void MQTTClient::_begin(int port, Client &client){
167158
// set client
168159
this->netClient = &client;
169160

@@ -199,11 +190,11 @@ void MQTTClient::setClockSource(MQTTClientClockSource cb) {
199190
this->timer2.millis = cb;
200191
}
201192

202-
void MQTTClient::setHost(IPAddress ipAddr, int port) {
193+
void MQTTClient::setHost(IPAddress _ipAddr, int _port) {
203194
// set hostname and port
204-
this->ipaddress = ipAddr;
195+
this->ipaddress = _ipAddr;
205196

206-
this->port = port;
197+
this->port = _port;
207198
}
208199

209200
void MQTTClient::setHost(const char _hostname[], int _port) {
@@ -316,13 +307,7 @@ bool MQTTClient::connect(const char clientId[], const char username[], const cha
316307
if(this->hostname != nullptr){
317308
ret = this->netClient->connect(this->hostname, (uint16_t)this->port);
318309
}else{
319-
uint32_t thisIP = 0;
320-
thisIP = ipaddress[0];
321-
thisIP |= uint32_t(ipaddress[1] << 8);
322-
thisIP |= uint32_t(ipaddress[2] << 16);
323-
thisIP |= uint32_t(ipaddress[3] << 24);
324-
325-
ret = this->netClient->connect(thisIP, (uint16_t)this->port);
310+
ret = this->netClient->connect(this->ipaddress, (uint16_t)this->port);
326311
}
327312

328313
if (ret <= 0) {

src/MQTTClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class MQTTClient {
136136

137137
private:
138138
void close();
139+
void _begin(int port, Client &client);
139140
};
140141

141142
#endif

0 commit comments

Comments
 (0)