SparkFun Iridium 9604  v1.0.0
Library for the Iridium 9604 Hybrid LTE-M Cellular + Iridium Satellite (SBD) + GNSS IoT module
Loading...
Searching...
No Matches
nmeaMessageVector.h
Go to the documentation of this file.
1
23#pragma once
24
25#include <vector>
26#include <string.h>
27
28#include "nmeaMessage.h"
29
30// ===========================
31
32// Support for individual NMEA messages is contained in these files.
33// Individual files can be commented out to save both program memory and RAM.
34// All thirteen messages supported by AT+UGNMEA are included by default.
35
49
50// ===========================
51
52#include "sfe_debug.h" // Shared base for debugPrint()/debugPrintln()
53
55{
56
59 {
60 public:
61 std::vector<nmeaMessage *> nmeaMessageVectors;
62
70 {
72 }
73
78 {
79 for (auto msg : nmeaMessageVectors)
80 delete msg;
81 }
82
89 nmeaMessage *find(const char *msgId)
90 {
91 if (msgId == nullptr)
92 return nullptr;
93 for (auto msg : nmeaMessageVectors)
94 {
95 if (msg->amI(msgId))
96 return msg;
97 }
98 return nullptr;
99 }
100
102 nmeaMessage *findByName(const char *msgId)
103 {
104 return find(msgId);
105 }
106
114 nmea_status_e initStorage(const char *msgId)
115 {
116 nmeaMessage *msg = find(msgId);
117 if (msg == nullptr)
120 }
121
123 nmea_status_e isURCEnabled(const char *msgId, bool *enabled)
124 {
125 nmeaMessage *msg = find(msgId);
126 if (msg == nullptr)
128 *enabled = msg->_urcEnabled;
129 return NMEA_STATUS_SUCCESS;
130 }
132 nmea_status_e setURCEnabled(const char *msgId, bool enabled)
133 {
134 nmeaMessage *msg = find(msgId);
135 if (msg == nullptr)
137 msg->_urcEnabled = enabled;
138 return NMEA_STATUS_SUCCESS;
139 }
140
142 nmea_status_e isMemoryEnabled(const char *msgId, bool *enabled)
143 {
144 nmeaMessage *msg = find(msgId);
145 if (msg == nullptr)
147 *enabled = msg->_memoryEnabled;
148 return NMEA_STATUS_SUCCESS;
149 }
151 nmea_status_e setMemoryEnabled(const char *msgId, bool enabled)
152 {
153 nmeaMessage *msg = find(msgId);
154 if (msg == nullptr)
156 msg->_memoryEnabled = enabled;
157 return NMEA_STATUS_SUCCESS;
158 }
159
160 // A single bool per message - has fresh data arrived since the last time it was reported?
168 nmea_status_e moduleQueried(const char *msgId, bool *queried)
169 {
170 nmeaMessage *msg = find(msgId);
171 if (msg == nullptr)
173 *queried = msg->_moduleQueried;
174 return NMEA_STATUS_SUCCESS;
175 }
177 nmea_status_e setModuleQueried(const char *msgId, bool queried)
178 {
179 nmeaMessage *msg = find(msgId);
180 if (msg == nullptr)
182 msg->_moduleQueried = queried;
183 return NMEA_STATUS_SUCCESS;
184 }
185
194 nmea_status_e setCallback(const char *msgId, void (*callbackPtr)(nmeaCallbackDataCommon_t *))
195 {
196 nmeaMessage *msg = find(msgId);
197 if (msg == nullptr)
199 msg->_callbackPtr = callbackPtr;
200 return NMEA_STATUS_SUCCESS;
201 }
202
221 nmea_status_e storePayload(const char *msgId, const uint8_t *payload, uint16_t len)
222 {
223 nmeaMessage *msg = find(msgId);
224 if (msg == nullptr)
226 if (!msg->initStorage())
227 return NMEA_STATUS_MEM_ERR;
228 if (len >= msg->_messageLength)
229 {
230 debugPrint("nmeaMessageVector::storePayload: msgId ");
231 debugPrint(msgId);
232 debugPrintln(" is too long! Truncating");
233 len = msg->_messageLength - 1;
234 }
235 memcpy(msg->_storage, payload, len);
236 msg->_storage[len] = 0; // NULL-terminate
237 msg->_moduleQueried = true; // Mark the data as fresh
238
239 if (msg->_callbackPtr != nullptr)
240 {
241 if (msg->initCallbackStorage())
242 {
243 if (msg->_numCallbackCopies <= 1)
244 {
245 memcpy(msg->_callbackStorage, payload, len);
246 msg->_callbackStorage[len] = 0;
247 msg->_callbackCount = 1; // head/tail stay at 0 - a degenerate 1-slot ring
248 }
249 else if (msg->_callbackCount < msg->_numCallbackCopies) // Ring has a free slot
250 {
251 uint8_t *slot = msg->_callbackStorage + ((uint32_t)msg->_callbackHead * msg->_messageLength);
252 memcpy(slot, payload, len);
253 slot[len] = 0;
254 msg->_callbackHead = (uint8_t)((msg->_callbackHead + 1) % msg->_numCallbackCopies);
255 msg->_callbackCount++;
256 }
257 else
258 {
259 // ring is full - drop this payload, keep what's already buffered
260 debugPrint("nmeaMessageVector::storePayload: msgId ", true); // Important
261 debugPrint(msgId, true);
262 debugPrintln(" _callbackStorage ring buffer full. Message lost.", true);
263 }
264 }
265 }
266
267 return NMEA_STATUS_SUCCESS;
268 }
269
270 // Look up one field of one message by name and fill in 'value'.
280 nmea_status_e extractValue(const char *msgId, const char *field, sfe_string_t &value)
281 {
282 nmeaMessage *msg = find(msgId);
283 if (msg == nullptr)
285 if (msg->_storage == nullptr)
286 return NMEA_STATUS_MEM_ERR; // No data has arrived for this message yet
287
288 return msg->extractFieldFrom(msg->_storage, field, value) ? NMEA_STATUS_SUCCESS : NMEA_STATUS_INVALID_ARG;
289 }
290 };
291
292} // namespace SparkFun_Iridium_9604
Debug printing: replaces the SARA-R5 library _debugPort prints. A base class of DevIridium9604 and th...
Definition: sfe_debug.h:38
void debugPrintln(void)
Print a blank debug line, if debugging is enabled and not in limited-debug mode.
Definition: sfe_debug.cpp:145
void debugPrint(const char *message, bool important=false)
Print a debug message (no trailing newline), if debugging is enabled.
Definition: sfe_debug.cpp:25
The base class for the NMEA messages. Each message stores its latest copy and a ring buffer of copies...
Definition: nmeaMessage.h:88
uint8_t _numCallbackCopies
Number of ring-buffer slots in _callbackStorage - 1 for most messages, 54 for GSV - see the GNSS v4 A...
Definition: nmeaMessage.h:510
void(* _callbackPtr)(nmeaCallbackDataCommon_t *)
The user callback.
Definition: nmeaMessage.h:539
uint8_t * _callbackStorage
Ring buffer of _numCallbackCopies slots, each _messageLength bytes - nullptr until initCallbackStorag...
Definition: nmeaMessage.h:526
bool initCallbackStorage(void)
Lazily allocate this message's callback ring-buffer storage (_callbackStorage), if not already done.
Definition: nmeaMessage.h:170
bool initStorage(void)
Lazily allocate this message's raw payload storage (_storage), if not already done.
Definition: nmeaMessage.h:150
uint8_t * _storage
Raw payload storage - nullptr until initStorage() is called.
Definition: nmeaMessage.h:511
bool _moduleQueried
true when fresh data has arrived. Cleared by getNMEA().
Definition: nmeaMessage.h:515
uint8_t _messageLength
The maximum message length in bytes. Messages could be less than this.
Definition: nmeaMessage.h:509
bool _urcEnabled
Has the module been told to output this message as a +UUGNMEA URC? (AT+UGNMEA <output> bit 1)
Definition: nmeaMessage.h:540
uint8_t _callbackHead
Next free slot the write side will write into.
Definition: nmeaMessage.h:534
bool _memoryEnabled
Has the module been told to store this message in its internal buffer? (AT+UGNMEA <output> bit 0)
Definition: nmeaMessage.h:541
bool extractFieldFrom(const uint8_t *buffer, const char *fieldName, sfe_string_t &value, const void *fieldsOverride=nullptr, uint8_t numFieldsOverride=0, uint16_t blockIndex=0) const
Look up one field of this message, by name, in a given NMEA sentence buffer.
Definition: nmeaMessage.h:218
uint8_t _callbackCount
How many slots currently hold fresh, undelivered data (replaces the old single _callbackDataValid boo...
Definition: nmeaMessage.h:536
void buildAll(std::vector< nmeaMessage * > &messages) const
Construct one instance of every currently-registered NMEA message class.
Definition: nmeaMessageRegistry.h:64
static nmeaMessageRegistry & get(void)
Get the single, lazily-constructed nmeaMessageRegistry instance.
Definition: nmeaMessageRegistry.h:35
The collection of NMEA message objects.
Definition: nmeaMessageVector.h:59
nmea_status_e extractValue(const char *msgId, const char *field, sfe_string_t &value)
Look up one named field of a registered message's live sentence.
Definition: nmeaMessageVector.h:280
nmea_status_e initStorage(const char *msgId)
Lazily allocate a registered message's raw payload storage.
Definition: nmeaMessageVector.h:114
std::vector< nmeaMessage * > nmeaMessageVectors
The NMEA message objects.
Definition: nmeaMessageVector.h:61
nmea_status_e isMemoryEnabled(const char *msgId, bool *enabled)
Get whether the module has been told to store this message in its internal (memory) buffer.
Definition: nmeaMessageVector.h:142
nmea_status_e setModuleQueried(const char *msgId, bool queried)
Set whether fresh data has arrived for a registered message since it was last reported.
Definition: nmeaMessageVector.h:177
nmea_status_e setURCEnabled(const char *msgId, bool enabled)
Record whether the module has been told to output this message as a +UUGNMEA URC.
Definition: nmeaMessageVector.h:132
nmea_status_e setMemoryEnabled(const char *msgId, bool enabled)
Record whether the module has been told to store this message in its internal (memory) buffer.
Definition: nmeaMessageVector.h:151
nmea_status_e isURCEnabled(const char *msgId, bool *enabled)
Get whether the module has been told to output this message as a +UUGNMEA URC.
Definition: nmeaMessageVector.h:123
nmeaMessage * findByName(const char *msgId)
Find the registered message object by its name. Equivalent to find().
Definition: nmeaMessageVector.h:102
nmeaMessageVector(void)
Construct the vector and populate it with one instance of every registered NMEA message.
Definition: nmeaMessageVector.h:69
nmea_status_e setCallback(const char *msgId, void(*callbackPtr)(nmeaCallbackDataCommon_t *))
Register (or clear) the user callback function for a registered message.
Definition: nmeaMessageVector.h:194
~nmeaMessageVector(void)
Destroy the vector, deleting every message object it owns.
Definition: nmeaMessageVector.h:77
nmea_status_e moduleQueried(const char *msgId, bool *queried)
Get whether fresh data has arrived for a registered message since it was last reported.
Definition: nmeaMessageVector.h:168
nmeaMessage * find(const char *msgId)
Find the registered message object for a given NMEA message identifier.
Definition: nmeaMessageVector.h:89
nmea_status_e storePayload(const char *msgId, const uint8_t *payload, uint16_t len)
Copy a freshly-received (and already checksum-validated) NMEA sentence into a message's storage.
Definition: nmeaMessageVector.h:221
Definition: nmeaMessage.h:30
nmea_status_e
Status codes returned by the nmeaMessageVector methods (replaces GNSS v4's sfe_ublox_status_e)
Definition: nmeaMessage.h:53
@ NMEA_STATUS_SUCCESS
Definition: nmeaMessage.h:54
@ NMEA_STATUS_INVALID_ARG
Definition: nmeaMessage.h:55
@ NMEA_STATUS_MEM_ERR
Definition: nmeaMessage.h:56
Forward declaration - see nmeaCallbackDataCommon_t below.
Definition: nmeaMessage.h:80