Line data Source code
1 : /* 2 : AP_Logger logging - MAVLink variant 3 : 4 : - transfers blocks of the open log file to a client using MAVLink 5 : */ 6 : #pragma once 7 : 8 : #include "AP_Logger_Backend.h" 9 : 10 : #if HAL_LOGGING_MAVLINK_ENABLED 11 : 12 : #include <AP_HAL/Semaphores.h> 13 : 14 : #define DF_MAVLINK_DISABLE_INTERRUPTS 0 15 : 16 : class AP_Logger_MAVLink : public AP_Logger_Backend 17 : { 18 : public: 19 : // constructor 20 : AP_Logger_MAVLink(class AP_Logger &front, LoggerMessageWriter_DFLogStart *writer); 21 : 22 1 : static AP_Logger_Backend *probe(AP_Logger &front, 23 : LoggerMessageWriter_DFLogStart *ls) { 24 1 : return NEW_NOTHROW AP_Logger_MAVLink(front, ls); 25 : } 26 : 27 : // initialisation 28 : void Init() override; 29 : 30 : // in actual fact, we throw away everything until a client connects. 31 : // This stops calls to start_new_log from the vehicles 32 198733 : bool logging_started() const override { return _initialised; } 33 : 34 : void stop_logging() override; 35 : 36 : /* Write a block of data at current offset */ 37 : bool _WritePrioritisedBlock(const void *pBuffer, uint16_t size, 38 : bool is_critical) override; 39 : 40 : // initialisation 41 13 : bool CardInserted(void) const override { return true; } 42 : 43 : // erase handling 44 1 : void EraseAll() override {} 45 : 46 1 : void PrepForArming() override {} 47 : 48 : // high level interface 49 0 : uint16_t find_last_log(void) override { return 0; } 50 0 : void get_log_boundaries(uint16_t log_num, uint32_t & start_page, uint32_t & end_page) override {} 51 0 : void get_log_info(uint16_t log_num, uint32_t &size, uint32_t &time_utc) override {} 52 0 : int16_t get_log_data(uint16_t log_num, uint16_t page, uint32_t offset, uint16_t len, uint8_t *data) override { return 0; } 53 1 : void end_log_transfer() override { }; 54 2 : uint16_t get_num_logs(void) override { return 0; } 55 : 56 : void remote_log_block_status_msg(const GCS_MAVLINK &link, const mavlink_message_t& msg) override; 57 1 : void vehicle_was_disarmed() override {} 58 : 59 : protected: 60 : 61 : void push_log_blocks() override; 62 : bool WritesOK() const override; 63 : 64 : private: 65 : 66 : struct dm_block { 67 : uint32_t seqno; 68 : uint8_t buf[MAVLINK_MSG_REMOTE_LOG_DATA_BLOCK_FIELD_DATA_LEN]; 69 : uint32_t last_sent; 70 : struct dm_block *next; 71 : }; 72 : bool send_log_block(struct dm_block &block); 73 : void handle_ack(const GCS_MAVLINK &link, const mavlink_message_t &msg, uint32_t seqno); 74 : void handle_retry(uint32_t block_num); 75 : void do_resends(uint32_t now); 76 : void free_all_blocks(); 77 : 78 : // a stack for free blocks, queues for pending, sent, retries and sent 79 : struct dm_block_queue { 80 : uint32_t sent_count; 81 : struct dm_block *oldest; 82 : struct dm_block *youngest; 83 : }; 84 : typedef struct dm_block_queue dm_block_queue_t ; 85 : void enqueue_block(dm_block_queue_t &queue, struct dm_block *block); 86 : bool queue_has_block(dm_block_queue_t &queue, struct dm_block *block); 87 : struct dm_block *dequeue_seqno(dm_block_queue_t &queue, uint32_t seqno); 88 : bool free_seqno_from_queue(uint32_t seqno, dm_block_queue_t &queue); 89 : bool send_log_blocks_from_queue(dm_block_queue_t &queue); 90 : uint8_t stack_size(struct dm_block *stack); 91 : uint8_t queue_size(dm_block_queue_t queue); 92 : 93 : struct dm_block *_blocks_free; 94 : dm_block_queue_t _blocks_sent; 95 : dm_block_queue_t _blocks_pending; 96 : dm_block_queue_t _blocks_retry; 97 : 98 : struct _stats { 99 : // the following are reset any time we log stats (see "reset_stats") 100 : uint32_t resends; 101 : uint8_t collection_count; 102 : uint16_t state_free; // cumulative across collection period 103 : uint8_t state_free_min; 104 : uint8_t state_free_max; 105 : uint16_t state_pending; // cumulative across collection period 106 : uint8_t state_pending_min; 107 : uint8_t state_pending_max; 108 : uint16_t state_retry; // cumulative across collection period 109 : uint8_t state_retry_min; 110 : uint8_t state_retry_max; 111 : uint16_t state_sent; // cumulative across collection period 112 : uint8_t state_sent_min; 113 : uint8_t state_sent_max; 114 : } stats; 115 : 116 : // these methods are used for mavlink system status and arming checks 117 224034 : bool logging_enabled() const override { return true; } 118 : bool logging_failed() const override; 119 : 120 : const GCS_MAVLINK *_link; 121 : 122 : uint8_t _target_system_id; 123 : uint8_t _target_component_id; 124 : 125 : // this controls the maximum number of blocks we will push from 126 : // the pending and send queues in any call to push_log_blocks. 127 : // push_log_blocks is called by periodic_tasks. Each block is 200 128 : // bytes. In Plane, at 50Hz, a _max_blocks_per_send_blocks of 2 129 : // means we will push at most 2*50*200 == 20KB of logs per second 130 : // _max_blocks_per_send_blocks has to be high enough to push all 131 : // of the logs, but low enough that we don't spend way too much 132 : // time packing messages in any one loop 133 : const uint8_t _max_blocks_per_send_blocks; 134 : 135 : uint32_t _next_seq_num; 136 : uint16_t _latest_block_len; 137 : uint32_t _last_response_time; 138 : uint32_t _last_send_time; 139 : uint8_t _next_block_number_to_resend; 140 : bool _sending_to_client; 141 : 142 : void Write_DMS(AP_Logger_MAVLink &logger); 143 : 144 : uint32_t bufferspace_available() override; // in bytes 145 : uint8_t remaining_space_in_current_block() const; 146 : // write buffer 147 : uint8_t _blockcount_free; 148 : uint8_t _blockcount; 149 : struct dm_block *_blocks; 150 : struct dm_block *_current_block; 151 : struct dm_block *next_block(); 152 : 153 : void periodic_10Hz(uint32_t now) override; 154 : void periodic_1Hz() override; 155 : 156 : void stats_init(); 157 : void stats_reset(); 158 : void stats_collect(); 159 : void stats_log(); 160 : uint32_t _stats_last_collected_time; 161 : uint32_t _stats_last_logged_time; 162 : uint8_t mavlink_seq; 163 : 164 : /* we currently ignore requests to start a new log. Notionally we 165 : * could close the currently logging session and hope the client 166 : * re-opens one */ 167 0 : void start_new_log(void) override { 168 0 : return; 169 : } 170 : 171 : HAL_Semaphore semaphore; 172 : }; 173 : 174 : #endif // HAL_LOGGING_MAVLINK_ENABLED