16
16
17
17
#include < Arduino.h>
18
18
#include < vector>
19
+ #include < memory>
19
20
20
21
#if defined __has_include
21
22
#if !__has_include (<ArduinoIoTCloud.h>)
30
31
31
32
#include < ModemInterface.h>
32
33
#include < TimeUtils.h>
34
+ #include " ManagedTinyGsmClient.h"
33
35
34
36
/* *
35
37
* @enum ModemModel
@@ -100,6 +102,20 @@ class ArduinoCellular {
100
102
*/
101
103
ArduinoCellular ();
102
104
105
+ /* *
106
+ * @brief Destructor for the ArduinoCellular class.
107
+ * Cleans up any resources used by the class.
108
+ */
109
+ ~ArduinoCellular ();
110
+
111
+ // Delete copy operations (since unique_ptr can't be copied)
112
+ ArduinoCellular (const ArduinoCellular&) = delete ;
113
+ ArduinoCellular& operator =(const ArduinoCellular&) = delete ;
114
+
115
+ // Enable move operations
116
+ ArduinoCellular (ArduinoCellular&&) = default ;
117
+ ArduinoCellular& operator =(ArduinoCellular&&) = default ;
118
+
103
119
/* *
104
120
* @brief Initializes the modem.
105
121
* This function must be called before using any other functions in the library.
@@ -238,6 +254,7 @@ class ArduinoCellular {
238
254
/* *
239
255
* @brief Gets a HTTP client for the specified server and port.
240
256
* The maximum number of HTTP clients is limited by the number of sockets available.
257
+ * Call `cleanup()` to release the resources used by the clients once you are done with them.
241
258
* @param server The server address.
242
259
* @param port The server port.
243
260
* @return The HTTP client.
@@ -247,12 +264,26 @@ class ArduinoCellular {
247
264
/* *
248
265
* @brief Gets a HTTPS client for the specified server and port.
249
266
* The maximum number of HTTP clients is limited by the number of sockets available.
267
+ * Call `cleanup()` to release the resources used by the clients once you are done with them.
250
268
* @param server The server address.
251
269
* @param port The server port.
252
270
* @return The HTTPS client.
253
271
*/
254
272
HttpClient getHTTPSClient (const char * server, const int port);
255
273
274
+ /* *
275
+ * @brief Cleans up the clients and releases the resources used by them.
276
+ * It's necessary to call this function to free up the memory used by the client
277
+ * objects that are created by the library internally.
278
+ */
279
+ void cleanup ();
280
+
281
+ /* *
282
+ * @brief Gets the number of managed clients.
283
+ * The clients are managed in the sense of memory management.
284
+ */
285
+ size_t getManagedClientCount () const ;
286
+
256
287
/* *
257
288
* @brief Gets the local IP address.
258
289
* @return The local IP address.
@@ -282,6 +313,10 @@ class ArduinoCellular {
282
313
SimStatus getSimStatus ();
283
314
284
315
private:
316
+ // Each instance manages its own connections
317
+ std::vector<std::unique_ptr<ManagedTinyGsmClient>> managedGsmClients;
318
+ std::vector<std::unique_ptr<BearSSLClient>> managedSslClients;
319
+
285
320
bool connectToGPRS (const char * apn, const char * gprsUser, const char * gprsPass);
286
321
287
322
0 commit comments