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
urcHandler.h
Go to the documentation of this file.
1
46#pragma once
47
48#include "sfe_platform.h"
49#include "sfe_ip_address.h"
50#include "sfe_debug.h"
52#include <string.h>
53#include <vector>
54
56{
57
58 // The generic callback pointer type. Each handler casts this back to its own typed callback.
59 typedef void (*urcCallback_t)(void);
60
61 // The maximum number of user callbacks per URC (e.g. +CIEV has three: signal quality, service
62 // availability and SV/beam coordinates)
63 const uint8_t URC_MAX_CALLBACKS = 3;
64
70 {
71 public:
73
74 // Debug printing
75 virtual SfeDebugPrint &urcDebug(void) = 0;
76
77 // Sockets
78 virtual int urcGetLastSocketProtocol(int socket) = 0;
79 virtual IRIDIUM_9604_error_t urcSocketReadIndication(int socket, int length) = 0;
80 virtual IRIDIUM_9604_error_t urcSocketReadIndicationUDP(int socket, int length) = 0;
81 virtual void urcSocketListenIndication(sfe_ip_address_t localIP, sfe_ip_address_t remoteIP) = 0;
82
83 // GNSS NMEA: process one NMEA sentence ($ to checksum, no CR LF)
84 virtual void urcProcessNMEA(const char *sentence, size_t length) = 0;
85
86 // Satellite: record the latest +CIEV indicator value (getSatelliteSignalQuality() etc.)
87 virtual void urcIndicatorEvent(int indicator, int value) = 0;
88 };
89
92 {
93 public:
95 {
96 for (uint8_t i = 0; i < URC_MAX_CALLBACKS; i++)
97 _callbackPtr[i] = nullptr;
98 }
99 virtual ~urcHandler(void) {}
100
108 void addURC(const char *urcPrefix, bool keepInBacklog = true)
109 {
110 _urcPrefix = urcPrefix;
111 _keepInBacklog = keepInBacklog;
112 }
113
115 bool amI(const char *urcPrefix) const
116 {
117 return ((_urcPrefix != nullptr) && (urcPrefix != nullptr) && (strcmp(_urcPrefix, urcPrefix) == 0));
118 }
119
127 const char *match(const char *event) const
128 {
129 if ((_urcPrefix == nullptr) || (event == nullptr))
130 return nullptr;
131 const char *searchPtr = strstr(event, _urcPrefix);
132 if (searchPtr == nullptr)
133 return nullptr;
134 searchPtr += strlen(_urcPrefix); // Move searchPtr to first character - probably a space
135 while (*searchPtr == ' ')
136 searchPtr++; // Skip spaces
137 return searchPtr;
138 }
139
149 virtual bool process(urcHandlerInterface &device, const char *params) = 0;
150
152 bool setCallback(urcCallback_t callback, uint8_t index = 0)
153 {
154 if (index >= URC_MAX_CALLBACKS)
155 return false;
156 _callbackPtr[index] = callback;
157 return true;
158 }
159
161 bool hasCallback(uint8_t index = 0) const
162 {
163 return ((index < URC_MAX_CALLBACKS) && (_callbackPtr[index] != nullptr));
164 }
165
166 const char *_urcPrefix = nullptr;
167 bool _keepInBacklog = true;
169 };
170
171 // ---------------------------------------------------------------------------------------------
172 // Self-registration - mirrors nmeaMessageRegistry (and GNSS v4 ubxMessageRegistry)
173 // ---------------------------------------------------------------------------------------------
174
177 {
178 public:
179 typedef urcHandler *(*urcHandlerFactoryFn)(void);
180
183 {
184 static urcHandlerRegistry instance;
185 return instance;
186 }
187
196 {
197 for (auto existing : _factories)
198 if (existing == factory)
199 return; // Already registered (e.g. from another translation unit) - skip
200 _factories.push_back(factory);
201 }
202
204 void buildAll(std::vector<urcHandler *> &handlers) const
205 {
206 for (auto factory : _factories)
207 handlers.push_back(factory());
208 }
209
210 private:
211 urcHandlerRegistry(void) {}
212 std::vector<urcHandlerFactoryFn> _factories;
213 };
214
215 template <typename HandlerType>
218 {
219 public:
221 {
223 }
224
226 static urcHandler *create(void)
227 {
228 return new HandlerType();
229 }
230 };
231
238#define urcRegisterHandler(kHandler) static urcHandlerBuilder<kHandler> global_##kHandler##Builder;
239
240} // namespace SparkFun_Iridium_9604
IRIDIUM_9604_error_t
The library error codes.
Definition: Iridium_9604_typedefs.h:129
Debug printing: replaces the SARA-R5 library _debugPort prints. A base class of DevIridium9604 and th...
Definition: sfe_debug.h:38
Declared (by urcRegisterHandler) at the bottom of each URC handler header. Registers the handler clas...
Definition: urcHandler.h:218
static urcHandler * create(void)
Create one instance of the handler.
Definition: urcHandler.h:226
urcHandlerBuilder(void)
Definition: urcHandler.h:220
The base class for the URC handlers. See the file description for how to add a new URC.
Definition: urcHandler.h:92
const char * _urcPrefix
e.g. "+UUSORD:"
Definition: urcHandler.h:166
urcCallback_t _callbackPtr[URC_MAX_CALLBACKS]
The user's callbacks, stored generically.
Definition: urcHandler.h:168
bool _keepInBacklog
Keep this URC in the backlog for the next bufferedPoll.
Definition: urcHandler.h:167
bool amI(const char *urcPrefix) const
Does this object handle this URC prefix? (Exact match, e.g. "+UUSORD:")
Definition: urcHandler.h:115
bool setCallback(urcCallback_t callback, uint8_t index=0)
Set (or clear, with nullptr) callback 'index' for this URC.
Definition: urcHandler.h:152
const char * match(const char *event) const
Does this event contain this handler's URC?
Definition: urcHandler.h:127
virtual ~urcHandler(void)
Definition: urcHandler.h:99
urcHandler(void)
Definition: urcHandler.h:94
bool hasCallback(uint8_t index=0) const
true if callback 'index' has been registered.
Definition: urcHandler.h:161
virtual bool process(urcHandlerInterface &device, const char *params)=0
Parse the URC parameters and act on them.
void addURC(const char *urcPrefix, bool keepInBacklog=true)
Register this handler's identity. Called once, from the subclass's own constructor.
Definition: urcHandler.h:108
urcHandlerInterface: the services DevIridium9604 provides to the URC handlers. DevIridium9604 inherit...
Definition: urcHandler.h:70
virtual ~urcHandlerInterface()
Definition: urcHandler.h:72
virtual IRIDIUM_9604_error_t urcSocketReadIndicationUDP(int socket, int length)=0
Read UDP data and pass it to the socket read callback(s)
virtual SfeDebugPrint & urcDebug(void)=0
The debug printer.
virtual void urcProcessNMEA(const char *sentence, size_t length)=0
Process one NMEA sentence.
virtual int urcGetLastSocketProtocol(int socket)=0
IRIDIUM_9604_TCP, IRIDIUM_9604_UDP or 0.
virtual void urcIndicatorEvent(int indicator, int value)=0
Record the latest +CIEV indicator value.
virtual void urcSocketListenIndication(sfe_ip_address_t localIP, sfe_ip_address_t remoteIP)=0
Record the IP addresses (lastRemoteIP())
virtual IRIDIUM_9604_error_t urcSocketReadIndication(int socket, int length)=0
Read TCP data and pass it to the socket read callback(s)
The registry of URC handler classes - mirrors the GNSS v4 message registries.
Definition: urcHandler.h:177
urcHandler *(* urcHandlerFactoryFn)(void)
The factory function type.
Definition: urcHandler.h:179
static urcHandlerRegistry & get(void)
Get the single, lazily-constructed urcHandlerRegistry instance.
Definition: urcHandler.h:182
void registerHandler(urcHandlerFactoryFn factory)
Register one URC handler class's factory function, if not already registered.
Definition: urcHandler.h:195
void buildAll(std::vector< urcHandler * > &handlers) const
Construct one instance of every currently-registered URC handler class.
Definition: urcHandler.h:204
Definition: nmeaMessage.h:30
void(* urcCallback_t)(void)
The generic URC callback type. Cast your callback to this for setURCCallback().
Definition: urcHandler.h:59
const uint8_t URC_MAX_CALLBACKS
The maximum number of user callbacks per URC.
Definition: urcHandler.h:63