Tezzeret  1
Tezzeret
GmLoggingApp.hpp
1 /*
2  * GmTivaMFAMApp.hpp
3  *
4  * Created on: May 11, 2016
5  * Author: rcram
6  */
7 
8 #ifndef GMLOGGINGAPP_HPP_
9 #define GMLOGGINGAPP_HPP_
10 
11 
12 #if GPS_ENABLED == GEO_TRUE || GEO_PPS_EVENTS == GEO_TRUE
13 #include <ti/sysbios/hal/Seconds.h>
14 #endif
15 
16 
17 #include <GmMFAMDataSource.hpp>
18 #if GEO_PPS_EVENTS == GEO_TRUE
19 #include <GmPpsEvent.hpp>
20 #endif
21 #include <GmAppEvent.hpp>
22 
23 #if UNMIRRORED_STORAGE == GEO_TRUE
24 #include <GmCharRingBufferStorage.hpp>
25 #else
26 #if GEO_SDCARD == GEO_TRUE
27 #include <GmTivaSDStorage.hpp>
28 #endif
29 #if GEO_USB_STORAGE == GEO_TRUE
30 #include <GmTivaUsbStorage.hpp>
31 #endif
32 #endif
33 
34 #if GEO_MQTT == GEO_TRUE
35 #include <GmMQTT.hpp>
36 #endif
37 
38 #if GPS_ENABLED == GEO_TRUE
39 #include <GmTivaGpsUartSensor.hpp>
40 #endif
41 
42 #include "GmMqttCfg.hpp"
43 #include "GmAppCfg.hpp"
44 
45 //#include "Analogs.hpp"
46 #include "GmApp.hpp"
47 //#include "GmMQTT.hpp"
48 #if GEO_LEGACY_MAG_SERIAL_OUT == GEO_TRUE
49 #include "GmUartOut.hpp"
50 #endif
51 #ifdef acs
52 #include <ti/sysbios/knl/Semaphore.h>
53 #endif
54 #include <GmRingBuffer.hpp>
55 #include <GmManagedArrayRingBuffer.hpp>
56 #include <GmStatus.hpp>
57 
58 #ifdef linux
59 #include <semaphore.h>
60 #endif
61 
62 #include <functional>
63 
64 #if GEO_CONFIGPROVISION_EEPROM == GEO_TRUE
65 #include <GmProvision.hpp>
66 #endif
67 
68 namespace Geometrics
69 {
71  {
72  public:
73  UInt32 m_iCommand;
74  UInt32 m_iMessageId;
75  UInt8 m_iCommandSource;
76  UInt8 *m_pui8AddedInfo;
77  UInt32 m_iAddedInfoSize;
78  };
79 
80 #define GEO_NO_PENDING_MODE -1
81 
82 #define STORAGE_STATUS_ALL_OK 0
83 #define STORAGE_STATUS_USB_FAILED 0x01
84 #define STORAGE_STATUS_SD_FAILED 0x02
85 #define STORAGE_STATUS_TEST_FAILED 0x04 /* Couldn't run the test for some reason */
86  // GmLoggingAppState is for states that can have external visibilty, or
87  // can be reported to the user. Some of them also have internal logical roles, of course.
89  {
90  public:
92  int m_iGpsStatus,
93  m_iMqttStatus,
94  mFileSystemStatus, // Should be file system status
95  m_iLoggingMode,
96  m_iMagStatus,
97  m_iWriteLatency;
98  Bool m_bLogToSerial,
99  m_bSendMqtt,
100  m_bLogToFile,
101  m_bGpsInitialized,
102  m_bGpsReceiving,
103  m_bClockIsGpsBased,
104  m_bClockIsSet,
105  m_bSystemClockSetByGps,
106  m_bSystemClockSet,
107  m_bSystemClockSetByNetwork,
108  m_bMissedData;
109 
110 
111  UInt32 m_iRecsWritten;
112 
113  UInt32 TotalRecordsWritten ();
114  Bool IsSavingData ();
115  };
116 
122  {
123  public:
124  #ifdef acs
132  GmLoggingApp (Semaphore_Handle shCommandAvailable,
133  Semaphore_Handle shCommandQueueAvailable,
134  Semaphore_Handle appEventsAvailable,
135  GmLoggingAppState *loggingAppState);
136  #else
137  GmLoggingApp();
138  #endif
139  virtual int Initialize ();
140  void MainLoop ();
141  int SetupAppQueues ();
142  void DequeueCommands ();
143  int QueueCommand (UInt32 iCommand,
144  UInt8 commandSource,
145  UInt8 *pui8AddedInfo,
146  UInt32 uiAddedInfoSize);
147  #if GEO_MQTT_SYNCH_REQUESTS == GEO_TRUE
148  int HandleMqttSynchCommand (int messageId, char *strTmp);
149  #endif
150  /***********************************************************/
151  /* ProcessCommand is called as commands are */
152  /* dequeued from the command fifo. */
153  /* A key entrypoint for application functionality. */
154  /***********************************************************/
155  virtual UInt32 ProcessCommand (UInt32 iCommand,
156  UInt8 commandSource,
157  UInt8 *pui8AddedInfo,
158  UInt32 uiAddedInfoSize);
159 
160  #if (GEO_SDCARD == GEO_TRUE) || \
161  (GEO_USB_STORAGE == GEO_TRUE)
162  int ListSurveys (GmSessionNames *psnSessions);
163  #endif
164 
165  public:
166 
167  /**********************************************************
168  * Functions and variables relating to date and time
169  **********************************************************/
170  // =======================================================
171  // Internal state. These are not the state variables for
172  // external consumption. Those are located in
173  // the GmLoggingAppState object.
174  // =======================================================
175  Bool m_bPendingSecondRecord;
176 
177  /*********************************************************/
178  /* Signal that the logging mode is changing. Non-zero */
179  /* means that a new mode is queued. */
180  /*********************************************************/
181  int m_iPendingMode;
182  double m_dTimeAtNextSecond;
183 
184  #if HOST_CONNECTED == GEO_TRUE
185  #if KNOWS_HOST_TIME == GEO_TRUE
186  /* Connecting tablet or other host time, and embedded time */
187  int64_t mHostTimeAtSyncInMilliseconds,
188  mThisTimeAtSyncInMilliseconds;
189  bool mHostTimeSynced;
190  int SetHostSyncTimes (int64_t hostTimeInMilliseconds,
191  int64_t thisTimeInMilliseconds);
192 
193  #endif
194  #endif
195 
196 
197  public:
198  Bool LogToFile () { return GetLoggingAppState()->m_bLogToFile; };
199  Bool LogToSerial ();
200 
201  #if GEO_PPS_EVENTS == GEO_TRUE
202  double OnPps (Seconds_Time *timestamp);
203  int SetApproximateTimeInfo (double dSeconds);
204  #endif
205 
206  /********************************/
207  /* This class is a singleton. */
208  /********************************/
209  static GmLoggingApp *m_static_plaTheSingleApp;
210 
211  virtual GmLoggingAppState *GetLoggingAppState () = 0;
212  CGmAppConfig *GetConfigInfo () { return m_cfgAppSetup; };
213  #if UNMIRRORED_STORAGE == GEO_TRUE
214  GmRingBufferFileStorage *GetStorage();
215  #else
216  #if (GEO_SDCARD == GEO_TRUE) || \
217  (GEO_USB_STORAGE == GEO_TRUE)
218  GeoFatFsSurveyStorage *GetStorage();
219  #endif
220  #endif
221 
222  #if (GEO_SURVEY_STORAGE == GEO_TRUE)
223  int StartLogAnnotation ();
224  void EndLogAnnotation ();
225  #endif
226  int LogUnindexedAppEvent (GmAppEvent *appEvent,
227  const char *strTextData,
228  int loggingMode);
229 
230  #if GEO_MQTT == GEO_TRUE
231  Int RequestDeleteSegment (const int messageId, char *commandJson);
232  Int MarkTarget (const int messageId, char *commandJson);
233  Int RequestSynch (const int messageId);
234  int SelectAcquisition (const int messageId, char *commandJson);
235  int SendMqttOKMessage (const int messageId);
236  #if GEO_MQTT_SYNCH_REQUESTS == GEO_TRUE
237  int SendMqttSynchResponseMessage(const int messageId,const char *message);
238  #endif
239  #endif
240  int MqttSendDataFileSynch (const int messageId, char *commandJson);
241 
242 
243 
244  protected:
245  /*********************************************/
246  /* Functions called by ProcessCommand */
247  /*********************************************/
248  UInt32 StartLogging (),
249  StopLogging (),
250  SetFileLogging (Bool bSet),
251  SetMqttLogging (Bool bSet),
252  SetTextMode (),
253  SetBinaryMode ();
254  #if GEO_ALLOW_RUN_DISCONNECTED == GEO_TRUE
255  uint32_t DisconnectKeepLogging ();
256  uint32_t ReconnectKeepLogging ();
257  #endif
258 
266  virtual UInt32 CheckCommandContext (UInt32 iCommand);
267  virtual UInt32 FlushInputQueue () = 0;
268 
269  int PrepLogSession (),
270  StartSerialPort (int iUartPortEnum);
271  void StopLogSession ();
272 
273  #if GEO_SDCARD == GEO_TRUE
274  int SetupSDStorage (int iSpiEnum,
275  UInt32 uiBitRate);
276  #endif
277  #if GEO_USB_STORAGE == GEO_TRUE
278  int SetupUsbStorage (int iUsbEnum);
279  #endif
280 
281  #if GPS_ENABLED == GEO_TRUE
282  public: // TODO RHC bad. Doing for web server, hacky, fix it.
283  CTivaGpsUartSensor *mGps;
284  #endif
285  #if GEO_MQTT == GEO_TRUE
286  public: // TODO RHC bad. Doing for web server. Hacky, fix it.
287  GmMqttPublisher *mMqttPublisher;
288  CGmMqttConfig *m_pcmmMqttParams;
289  #endif
290 
291  protected:
297 
298  #ifndef MIRRORED_STORAGE
303  #else
304  #if (GEO_SDCARD == GEO_TRUE)||(GEO_USB_STORAGE == GEO_TRUE)
306  #endif
307  #endif
308 
309  #if GEO_PPS_EVENTS == GEO_TRUE
310  GmPpsEventSensor *mPpsEvents;
311  #endif
317  #if GEO_LEGACY_MAG_SERIAL_OUT == GEO_TRUE
318  CTivaUartOut *m_uoUartOut;
319  #endif
320  // AnalogConverter *m_acConverter;
321  CRingBuffer *mCommandQueue;
322 
328  CGmAppConfig *m_cfgAppSetup;
329  #if GEO_CONFIGPROVISION_EEPROM == GEO_TRUE
330  struct _GeoMachineProvisioning m_basicProvisioning;
331  #endif
332 
333  #ifdef acs
334  Semaphore_Handle m_shCommandAvailable,
335  m_shCommandQueueAvailable,
336  mAppEventsAvailable;
337  #endif
338 
339  /*************************************************************************************/
352  static SensorStorageModeInfo mStorageModeInfo[GEO_MAX_APP_STORAGE_MODES][GEO_MAX_APP_SENSORS][GEO_MAX_APP_STORAGES];
353 
354 
355  /************************************************************************************/
374  int AddStorageSensorMode (int iStorageMode,
375  GmEventSource *pSensor,
376  GmStorage *pStorage,
377  UInt32 iFormat,
378  UInt32 iTopic,
379  UInt32 iDownsampleRate,
380  UInt32 iOtherInfo);
382  *GetModeSensorLoggingInfo (GmEventSource *psSensor,
383  int iStorageMode);
384  int WriteSensorToStorageList (GmEventSource *psSensor,
385  SensorStorageModeInfo ssmiStorageList[],
386  char *pData,
387  UInt32 iSize);
388  int WriteSensorToStorageList (GmEventSource *psSensor,
389  SensorStorageModeInfo ssmiStorageList[],
390  char *pData,
391  UInt32 iSize,
392  std::function<Bool (SensorStorageModeInfo *pInfo)> storageFilter);
393  void SetSensorId (GmEventSource *psSensor);
394 
395 
396  protected:
397  int m_iCountSensorIds;
398 
399  public:
400  #if GEO_CONFIGPROVISION_EEPROM == GEO_TRUE
401  struct _GeoMachineProvisioning *GetBasicProvisioning() { return &m_basicProvisioning; };
402  #endif
403  int AddEvent (UInt32 uiEventCode,
404  UInt32 uiEventInfo,
405  UInt32 uiDataLength,
406  char *caData);
407  #ifdef linux
408  private:
409  sem_t m_commandAvailableSemaphore,
410  m_commandQueueAvailable;
411  pthread_t m_commandQueueThread;
412  #endif
413 
414 
415  };
416 
417  // TODO RHC move into separate clock/time class
418  extern double date2sec(long month, long day, long year, long hh, long mm, double dsec);
419 } // End of Geometrics namespace
420 #endif
Definition: GmAppCfg.hpp:29
Definition: GmMqttCfg.hpp:17
Definition: GmRingBuffer.hpp:33
Definition: GmTivaGpsUartSensor.hpp:57
Definition: GmUartOut.hpp:24
Definition: GmLoggingApp.hpp:71
Definition: GeoFatFsSurveyStorage.hpp:19
Definition: GmAppEvent.hpp:97
Definition: GmApp.hpp:28
The root class for sensors and event sources.
Definition: GmSensor.hpp:32
Definition: GmLoggingApp.hpp:89
Direct base class for Tezzeret applications. Includes the main event queue, general command logic,...
Definition: GmLoggingApp.hpp:122
GmLoggingAppState * mLoggingAppState
State information about the application at this level (Geometrics::GmLoggingApp); This state is norma...
Definition: GmLoggingApp.hpp:296
GmStorage * mSurveyStorage
At any particular time, the application has no more than one main local storage, into which survey da...
Definition: GmLoggingApp.hpp:302
GmManagedArrayRingBuffer< uint8_t, GM_SIZE_OF_APP_DATA_QUEUE > * mMainEventQueue
This is the queue through which almost all events and data in a Tezzeret application pass....
Definition: GmLoggingApp.hpp:327
GmLoggingApp(Semaphore_Handle shCommandAvailable, Semaphore_Handle shCommandQueueAvailable, Semaphore_Handle appEventsAvailable, GmLoggingAppState *loggingAppState)
Constructor, called only in the context of the constructor for the final application class.
Definition: GmLoggingApp.cpp:200
GmAppEventSource * mAppEvents
Every application include AppEvents, which can include such generic functionality as start/stop,...
Definition: GmLoggingApp.hpp:316
virtual UInt32 CheckCommandContext(UInt32 iCommand)
When a command is presented to the application, the application needs to check whether the event is l...
Definition: GmLoggingApp.cpp:1530
static SensorStorageModeInfo mStorageModeInfo[GEO_MAX_APP_STORAGE_MODES][GEO_MAX_APP_SENSORS][GEO_MAX_APP_STORAGES]
A list of storages x sensors x modes. "Mode" is an application-specific state; Tezzeret isn't really ...
Definition: GmLoggingApp.hpp:352
int AddStorageSensorMode(int iStorageMode, GmEventSource *pSensor, GmStorage *pStorage, UInt32 iFormat, UInt32 iTopic, UInt32 iDownsampleRate, UInt32 iOtherInfo)
AddStorageSensorMode : Record the system's intention to handle (usuall to store or send) event and se...
Definition: GmLoggingApp.cpp:1686
Definition: GmMQTT.hpp:36
Definition: GmPpsEvent.hpp:38
Definition: GmCharRingBufferStorage.hpp:11
Definition: GmStorage.hpp:81
This class stores metadata about how a sensor should record itself to a storage, when in a particular...
Definition: GmSensor.hpp:19
GmApp/GmLoggingApp.hpp>
Definition: Analogs.hpp:47
Definition: GmProvision.hpp:55