2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
5 * @addtogroup LoggingModule Logging Module
6 * @brief Features for on board logging
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013.
11 * @brief Logging module, provides features for on board logging
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "openpilot.h"
34 #include "debuglogsettings.h"
35 #include "debuglogcontrol.h"
36 #include "debuglogstatus.h"
37 #include "debuglogentry.h"
38 #include "flightstatus.h"
41 static DebugLogSettingsData settings
;
42 static DebugLogControlData control
;
43 static DebugLogStatusData status
;
44 static FlightStatusData flightstatus
;
45 static DebugLogEntryData
*entry
; // would be better on stack but event dispatcher stack might be insufficient
48 static void SettingsUpdatedCb(UAVObjEvent
*ev
);
49 static void ControlUpdatedCb(UAVObjEvent
*ev
);
50 static void StatusUpdatedCb(UAVObjEvent
*ev
);
51 static void FlightStatusUpdatedCb(UAVObjEvent
*ev
);
53 int32_t LoggingInitialize(void)
55 DebugLogSettingsInitialize();
56 DebugLogControlInitialize();
57 DebugLogStatusInitialize();
58 DebugLogEntryInitialize();
59 FlightStatusInitialize();
60 PIOS_DEBUGLOG_Initialize();
61 entry
= pios_malloc(sizeof(DebugLogEntryData
));
69 int32_t LoggingStart(void)
71 DebugLogSettingsConnectCallback(SettingsUpdatedCb
);
72 DebugLogControlConnectCallback(ControlUpdatedCb
);
73 FlightStatusConnectCallback(FlightStatusUpdatedCb
);
74 SettingsUpdatedCb(DebugLogSettingsHandle());
77 .obj
= DebugLogSettingsHandle(),
79 .event
= EV_UPDATED_PERIODIC
,
82 EventPeriodicCallbackCreate(&ev
, StatusUpdatedCb
, 1000);
83 // invoke a periodic dispatcher callback - the event struct is a dummy, it could be filled with anything!
88 MODULE_INITCALL(LoggingInitialize
, LoggingStart
);
90 static void StatusUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
92 PIOS_DEBUGLOG_Info(&status
.Flight
, &status
.Entry
, &status
.FreeSlots
, &status
.UsedSlots
);
93 DebugLogStatusSet(&status
);
96 static void FlightStatusUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
98 FlightStatusGet(&flightstatus
);
99 if (settings
.LoggingEnabled
== DEBUGLOGSETTINGS_LOGGINGENABLED_ONLYWHENARMED
) {
100 if (flightstatus
.Armed
!= FLIGHTSTATUS_ARMED_ARMED
) {
101 PIOS_DEBUGLOG_Printf("FlightStatus Disarmed: On board Logging disabled.");
102 PIOS_DEBUGLOG_Enable(0);
104 PIOS_DEBUGLOG_Enable(1);
105 PIOS_DEBUGLOG_Printf("FlightStatus Armed: On board logging enabled.");
110 static void SettingsUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
112 DebugLogSettingsGet(&settings
);
113 if (settings
.LoggingEnabled
== DEBUGLOGSETTINGS_LOGGINGENABLED_ALWAYS
) {
114 PIOS_DEBUGLOG_Enable(1);
115 PIOS_DEBUGLOG_Printf("On board logging enabled.");
116 } else if (settings
.LoggingEnabled
== DEBUGLOGSETTINGS_LOGGINGENABLED_DISABLED
) {
117 PIOS_DEBUGLOG_Printf("On board logging disabled.");
118 PIOS_DEBUGLOG_Enable(0);
120 FlightStatusUpdatedCb(NULL
);
124 static void ControlUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
126 DebugLogControlGet(&control
);
127 if (control
.Operation
== DEBUGLOGCONTROL_OPERATION_RETRIEVE
) {
128 memset(entry
, 0, sizeof(DebugLogEntryData
));
129 if (PIOS_DEBUGLOG_Read(entry
, control
.Flight
, control
.Entry
) != 0) {
130 // reading from log failed, mark as non existent in output
131 entry
->Flight
= control
.Flight
;
132 entry
->Entry
= control
.Entry
;
133 entry
->Type
= DEBUGLOGENTRY_TYPE_EMPTY
;
135 DebugLogEntrySet(entry
);
136 } else if (control
.Operation
== DEBUGLOGCONTROL_OPERATION_FORMATFLASH
) {
137 FlightStatusArmedOptions armed
;
138 FlightStatusArmedGet(&armed
);
139 if (armed
== FLIGHTSTATUS_ARMED_DISARMED
) {
140 PIOS_DEBUGLOG_Format();