LoggerMessageWriter.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "AP_Common/AP_FWVersion.h"
  2. #include "LoggerMessageWriter.h"
  3. #define FORCE_VERSION_H_INCLUDE
  4. #include "ap_version.h"
  5. #undef FORCE_VERSION_H_INCLUDE
  6. extern const AP_HAL::HAL& hal;
  7. /* LogStartup - these are simple state machines which allow us to
  8. * trickle out messages to the log files
  9. */
  10. void LoggerMessageWriter::reset()
  11. {
  12. _finished = false;
  13. }
  14. void LoggerMessageWriter_DFLogStart::reset()
  15. {
  16. LoggerMessageWriter::reset();
  17. _fmt_done = false;
  18. _writesysinfo.reset();
  19. _writeentiremission.reset();
  20. _writeallrallypoints.reset();
  21. stage = ls_blockwriter_stage_formats;
  22. next_format_to_send = 0;
  23. _next_unit_to_send = 0;
  24. _next_multiplier_to_send = 0;
  25. _next_format_unit_to_send = 0;
  26. ap = AP_Param::first(&token, &type);
  27. }
  28. void LoggerMessageWriter_DFLogStart::process()
  29. {
  30. switch(stage) {
  31. case ls_blockwriter_stage_formats:
  32. // write log formats so the log is self-describing
  33. while (next_format_to_send < _logger_backend->num_types()) {
  34. if (!_logger_backend->Write_Format(_logger_backend->structure(next_format_to_send))) {
  35. return; // call me again!
  36. }
  37. next_format_to_send++;
  38. }
  39. _fmt_done = true;
  40. stage = ls_blockwriter_stage_units;
  41. FALLTHROUGH;
  42. case ls_blockwriter_stage_units:
  43. while (_next_unit_to_send < _logger_backend->num_units()) {
  44. if (!_logger_backend->Write_Unit(_logger_backend->unit(_next_unit_to_send))) {
  45. return; // call me again!
  46. }
  47. _next_unit_to_send++;
  48. }
  49. stage = ls_blockwriter_stage_multipliers;
  50. FALLTHROUGH;
  51. case ls_blockwriter_stage_multipliers:
  52. while (_next_multiplier_to_send < _logger_backend->num_multipliers()) {
  53. if (!_logger_backend->Write_Multiplier(_logger_backend->multiplier(_next_multiplier_to_send))) {
  54. return; // call me again!
  55. }
  56. _next_multiplier_to_send++;
  57. }
  58. stage = ls_blockwriter_stage_units;
  59. FALLTHROUGH;
  60. case ls_blockwriter_stage_format_units:
  61. while (_next_format_unit_to_send < _logger_backend->num_types()) {
  62. if (!_logger_backend->Write_Format_Units(_logger_backend->structure(_next_format_unit_to_send))) {
  63. return; // call me again!
  64. }
  65. _next_format_unit_to_send++;
  66. }
  67. stage = ls_blockwriter_stage_parms;
  68. FALLTHROUGH;
  69. case ls_blockwriter_stage_parms:
  70. while (ap) {
  71. if (!_logger_backend->Write_Parameter(ap, token, type)) {
  72. return;
  73. }
  74. ap = AP_Param::next_scalar(&token, &type);
  75. }
  76. stage = ls_blockwriter_stage_sysinfo;
  77. FALLTHROUGH;
  78. case ls_blockwriter_stage_sysinfo:
  79. _writesysinfo.process();
  80. if (!_writesysinfo.finished()) {
  81. return;
  82. }
  83. stage = ls_blockwriter_stage_write_entire_mission;
  84. FALLTHROUGH;
  85. case ls_blockwriter_stage_write_entire_mission:
  86. _writeentiremission.process();
  87. if (!_writeentiremission.finished()) {
  88. return;
  89. }
  90. stage = ls_blockwriter_stage_write_all_rally_points;
  91. FALLTHROUGH;
  92. case ls_blockwriter_stage_write_all_rally_points:
  93. _writeallrallypoints.process();
  94. if (!_writeallrallypoints.finished()) {
  95. return;
  96. }
  97. stage = ls_blockwriter_stage_vehicle_messages;
  98. FALLTHROUGH;
  99. case ls_blockwriter_stage_vehicle_messages:
  100. // we guarantee 200 bytes of space for the vehicle startup
  101. // messages. This allows them to be simple functions rather
  102. // than e.g. LoggerMessageWriter-based state machines
  103. if (_logger_backend->vehicle_message_writer()) {
  104. if (_logger_backend->bufferspace_available() < 200) {
  105. return;
  106. }
  107. (_logger_backend->vehicle_message_writer())();
  108. }
  109. stage = ls_blockwriter_stage_done;
  110. FALLTHROUGH;
  111. case ls_blockwriter_stage_done:
  112. break;
  113. }
  114. _finished = true;
  115. }
  116. void LoggerMessageWriter_WriteSysInfo::reset()
  117. {
  118. LoggerMessageWriter::reset();
  119. stage = ws_blockwriter_stage_formats;
  120. }
  121. void LoggerMessageWriter_WriteSysInfo::process() {
  122. const AP_FWVersion &fwver = AP::fwversion();
  123. switch(stage) {
  124. case ws_blockwriter_stage_formats:
  125. stage = ws_blockwriter_stage_firmware_string;
  126. FALLTHROUGH;
  127. case ws_blockwriter_stage_firmware_string:
  128. if (! _logger_backend->Write_Message(fwver.fw_string)) {
  129. return; // call me again
  130. }
  131. stage = ws_blockwriter_stage_git_versions;
  132. FALLTHROUGH;
  133. case ws_blockwriter_stage_git_versions:
  134. if (fwver.middleware_name && fwver.os_name) {
  135. if (! _logger_backend->Write_MessageF("%s: %s %s: %s",
  136. fwver.middleware_name,
  137. fwver.middleware_hash_str,
  138. fwver.os_name,
  139. fwver.os_hash_str)) {
  140. return; // call me again
  141. }
  142. } else if (fwver.os_name) {
  143. if (! _logger_backend->Write_MessageF("%s: %s",
  144. fwver.os_name,
  145. fwver.os_hash_str)) {
  146. return; // call me again
  147. }
  148. }
  149. stage = ws_blockwriter_stage_system_id;
  150. FALLTHROUGH;
  151. case ws_blockwriter_stage_system_id:
  152. char sysid[40];
  153. if (hal.util->get_system_id(sysid)) {
  154. if (! _logger_backend->Write_Message(sysid)) {
  155. return; // call me again
  156. }
  157. }
  158. stage = ws_blockwriter_stage_param_space_used;
  159. FALLTHROUGH;
  160. case ws_blockwriter_stage_param_space_used:
  161. if (! _logger_backend->Write_MessageF("Param space used: %u/%u", AP_Param::storage_used(), AP_Param::storage_size())) {
  162. return; // call me again
  163. }
  164. stage = ws_blockwriter_stage_rc_protocol;
  165. FALLTHROUGH;
  166. case ws_blockwriter_stage_rc_protocol:
  167. const char *prot = hal.rcin->protocol();
  168. if (prot == nullptr) {
  169. prot = "None";
  170. }
  171. if (! _logger_backend->Write_MessageF("RC Protocol: %s", prot)) {
  172. return; // call me again
  173. }
  174. }
  175. _finished = true; // all done!
  176. }
  177. void LoggerMessageWriter_WriteAllRallyPoints::process()
  178. {
  179. const AP_Rally *_rally = AP::rally();
  180. if (_rally == nullptr) {
  181. _finished = true;
  182. return;
  183. }
  184. switch(stage) {
  185. case ar_blockwriter_stage_write_new_rally_message:
  186. if (! _logger_backend->Write_Message("New rally")) {
  187. return; // call me again
  188. }
  189. stage = ar_blockwriter_stage_write_all_rally_points;
  190. FALLTHROUGH;
  191. case ar_blockwriter_stage_write_all_rally_points:
  192. while (_rally_number_to_send < _rally->get_rally_total()) {
  193. RallyLocation rallypoint;
  194. if (_rally->get_rally_point_with_index(_rally_number_to_send, rallypoint)) {
  195. if (!_logger_backend->Write_RallyPoint(
  196. _rally->get_rally_total(),
  197. _rally_number_to_send,
  198. rallypoint)) {
  199. return; // call me again
  200. }
  201. }
  202. _rally_number_to_send++;
  203. }
  204. stage = ar_blockwriter_stage_done;
  205. FALLTHROUGH;
  206. case ar_blockwriter_stage_done:
  207. break;
  208. }
  209. _finished = true;
  210. }
  211. void LoggerMessageWriter_WriteAllRallyPoints::reset()
  212. {
  213. LoggerMessageWriter::reset();
  214. stage = ar_blockwriter_stage_write_new_rally_message;
  215. _rally_number_to_send = 0;
  216. }
  217. void LoggerMessageWriter_WriteEntireMission::process() {
  218. const AP_Mission *_mission = AP::mission();
  219. if (_mission == nullptr) {
  220. _finished = true;
  221. return;
  222. }
  223. switch(stage) {
  224. case em_blockwriter_stage_write_new_mission_message:
  225. if (! _logger_backend->Write_Message("New mission")) {
  226. return; // call me again
  227. }
  228. stage = em_blockwriter_stage_write_mission_items;
  229. FALLTHROUGH;
  230. case em_blockwriter_stage_write_mission_items: {
  231. AP_Mission::Mission_Command cmd;
  232. while (_mission_number_to_send < _mission->num_commands()) {
  233. // upon failure to write the mission we will re-read from
  234. // storage; this could be improved.
  235. if (_mission->read_cmd_from_storage(_mission_number_to_send,cmd)) {
  236. if (!_logger_backend->Write_Mission_Cmd(*_mission, cmd)) {
  237. return; // call me again
  238. }
  239. }
  240. _mission_number_to_send++;
  241. }
  242. stage = em_blockwriter_stage_done;
  243. FALLTHROUGH;
  244. }
  245. case em_blockwriter_stage_done:
  246. break;
  247. }
  248. _finished = true;
  249. }
  250. void LoggerMessageWriter_WriteEntireMission::reset()
  251. {
  252. LoggerMessageWriter::reset();
  253. stage = em_blockwriter_stage_write_new_mission_message;
  254. _mission_number_to_send = 0;
  255. }