Skip to content

Commit 6a9f436

Browse files
abachmanbrentru
authored andcommitted
34 esp32 feed get malloc (#36)
* move /get string and object allocation into feed _init method addresses #34
1 parent aabe552 commit 6a9f436

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/AdafruitIO_Feed.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n):AdafruitIO_MQTT(
1818
name = n;
1919
_sub = 0;
2020
_pub = 0;
21+
_get_pub = 0;
2122
_dataCallback = 0;
2223

2324
_init();
@@ -111,20 +112,6 @@ bool AdafruitIO_Feed::save(double value, double lat, double lon, double ele, int
111112

112113
bool AdafruitIO_Feed::get()
113114
{
114-
if (!_get_topic)
115-
{
116-
_get_topic = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 12)); // 12 extra chars for /f/, /csv/get & null termination
117-
strcpy(_get_topic, _io->_username);
118-
strcat(_get_topic, "/f/");
119-
strcat(_get_topic, name);
120-
strcat(_get_topic, "/csv/get");
121-
}
122-
123-
if (!_get_pub)
124-
{
125-
_get_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _get_topic);
126-
}
127-
128115
return _get_pub->publish("\0");
129116
}
130117

@@ -225,6 +212,7 @@ void AdafruitIO_Feed::_init()
225212

226213
// dynamically allocate memory for mqtt topic and REST URLs
227214
_topic = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 8)); // 8 extra chars for /f/, /csv & null termination
215+
_get_topic = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 12)); // 12 extra chars for /f/, /csv/get & null termination
228216
_feed_url = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 16)); // 16 extra for api path & null term
229217
_create_url = (char *) malloc(sizeof(char) * (strlen(_io->_username) + 15)); // 15 extra for api path & null term
230218

@@ -250,9 +238,16 @@ void AdafruitIO_Feed::_init()
250238
strcat(_create_url, _io->_username);
251239
strcat(_create_url, "/feeds");
252240

241+
// build /get topic string
242+
strcpy(_get_topic, _io->_username);
243+
strcat(_get_topic, "/f/");
244+
strcat(_get_topic, name);
245+
strcat(_get_topic, "/csv/get");
246+
253247
// setup subscription
254248
_sub = new Adafruit_MQTT_Subscribe(_io->_mqtt, _topic);
255249
_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _topic);
250+
_get_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _get_topic);
256251
_io->_mqtt->subscribe(_sub);
257252

258253
_sub->setCallback(this, &AdafruitIO_MQTT::subCallback);
@@ -265,6 +260,7 @@ void AdafruitIO_Feed::_init()
265260
_feed_url = 0;
266261
_sub = 0;
267262
_pub = 0;
263+
_get_pub = 0;
268264
data = 0;
269265

270266
}

0 commit comments

Comments
 (0)