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 DebugLogControlInitialize();
56 DebugLogStatusInitialize();
57 DebugLogEntryInitialize();
58 FlightStatusInitialize();
59 PIOS_DEBUGLOG_Initialize();
60 entry
= pios_malloc(sizeof(DebugLogEntryData
));
68 int32_t LoggingStart(void)
70 DebugLogSettingsConnectCallback(SettingsUpdatedCb
);
71 DebugLogControlConnectCallback(ControlUpdatedCb
);
72 FlightStatusConnectCallback(FlightStatusUpdatedCb
);
73 SettingsUpdatedCb(DebugLogSettingsHandle());
76 .obj
= DebugLogSettingsHandle(),
78 .event
= EV_UPDATED_PERIODIC
,
81 EventPeriodicCallbackCreate(&ev
, StatusUpdatedCb
, 1000);
82 // invoke a periodic dispatcher callback - the event struct is a dummy, it could be filled with anything!
87 MODULE_INITCALL(LoggingInitialize
, LoggingStart
);
89 static void StatusUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
91 PIOS_DEBUGLOG_Info(&status
.Flight
, &status
.Entry
, &status
.FreeSlots
, &status
.UsedSlots
);
92 DebugLogStatusSet(&status
);
95 static void FlightStatusUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
97 FlightStatusGet(&flightstatus
);
98 if (settings
.LoggingEnabled
== DEBUGLOGSETTINGS_LOGGINGENABLED_ONLYWHENARMED
) {
99 if (flightstatus
.Armed
!= FLIGHTSTATUS_ARMED_ARMED
) {
100 PIOS_DEBUGLOG_Printf("FlightStatus Disarmed: On board Logging disabled.");
101 PIOS_DEBUGLOG_Enable(0);
103 PIOS_DEBUGLOG_Enable(1);
104 PIOS_DEBUGLOG_Printf("FlightStatus Armed: On board logging enabled.");
109 static void SettingsUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
111 DebugLogSettingsGet(&settings
);
112 if (settings
.LoggingEnabled
== DEBUGLOGSETTINGS_LOGGINGENABLED_ALWAYS
) {
113 PIOS_DEBUGLOG_Enable(1);
114 PIOS_DEBUGLOG_Printf("On board logging enabled.");
115 } else if (settings
.LoggingEnabled
== DEBUGLOGSETTINGS_LOGGINGENABLED_DISABLED
) {
116 PIOS_DEBUGLOG_Printf("On board logging disabled.");
117 PIOS_DEBUGLOG_Enable(0);
119 FlightStatusUpdatedCb(NULL
);
123 static void ControlUpdatedCb(__attribute__((unused
)) UAVObjEvent
*ev
)
125 DebugLogControlGet(&control
);
126 if (control
.Operation
== DEBUGLOGCONTROL_OPERATION_RETRIEVE
) {
127 memset(entry
, 0, sizeof(DebugLogEntryData
));
128 if (PIOS_DEBUGLOG_Read(entry
, control
.Flight
, control
.Entry
) != 0) {
129 // reading from log failed, mark as non existent in output
130 entry
->Flight
= control
.Flight
;
131 entry
->Entry
= control
.Entry
;
132 entry
->Type
= DEBUGLOGENTRY_TYPE_EMPTY
;
134 DebugLogEntrySet(entry
);
135 } else if (control
.Operation
== DEBUGLOGCONTROL_OPERATION_FORMATFLASH
) {
136 FlightStatusArmedOptions armed
;
137 FlightStatusArmedGet(&armed
);
138 if (armed
== FLIGHTSTATUS_ARMED_DISARMED
) {
139 PIOS_DEBUGLOG_Format();