Руслан Урядинский / libuavcan

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers transfer_sender.hpp Source File

transfer_sender.hpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #ifndef UAVCAN_TRANSPORT_TRANSFER_SENDER_HPP_INCLUDED
00006 #define UAVCAN_TRANSPORT_TRANSFER_SENDER_HPP_INCLUDED
00007 
00008 #include <cstdlib>
00009 #include <cassert>
00010 #include <uavcan/build_config.hpp>
00011 #include <uavcan/error.hpp>
00012 #include <uavcan/data_type.hpp>
00013 #include <uavcan/transport/crc.hpp>
00014 #include <uavcan/transport/transfer.hpp>
00015 #include <uavcan/transport/dispatcher.hpp>
00016 
00017 namespace uavcan
00018 {
00019 
00020 class UAVCAN_EXPORT TransferSender
00021 {
00022     const MonotonicDuration max_transfer_interval_;
00023 
00024     Dispatcher& dispatcher_;
00025 
00026     TransferPriority priority_;
00027     CanTxQueue::Qos qos_;
00028     TransferCRC crc_base_;
00029     DataTypeID data_type_id_;
00030     CanIOFlags flags_;
00031     uint8_t iface_mask_;
00032     bool allow_anonymous_transfers_;
00033 
00034     void registerError() const;
00035 
00036 public:
00037     enum { AllIfacesMask = 0xFF };
00038 
00039     static MonotonicDuration getDefaultMaxTransferInterval()
00040     {
00041         return MonotonicDuration::fromMSec(60 * 1000);
00042     }
00043 
00044     TransferSender(Dispatcher& dispatcher, const DataTypeDescriptor& data_type, CanTxQueue::Qos qos,
00045                    MonotonicDuration max_transfer_interval = getDefaultMaxTransferInterval())
00046         : max_transfer_interval_(max_transfer_interval)
00047         , dispatcher_(dispatcher)
00048         , priority_(TransferPriority::Default)
00049         , qos_(CanTxQueue::Qos())
00050         , flags_(CanIOFlags(0))
00051         , iface_mask_(AllIfacesMask)
00052         , allow_anonymous_transfers_(false)
00053     {
00054         init(data_type, qos);
00055     }
00056 
00057     TransferSender(Dispatcher& dispatcher, MonotonicDuration max_transfer_interval = getDefaultMaxTransferInterval())
00058         : max_transfer_interval_(max_transfer_interval)
00059         , dispatcher_(dispatcher)
00060         , priority_(TransferPriority::Default)
00061         , qos_(CanTxQueue::Qos())
00062         , flags_(CanIOFlags(0))
00063         , iface_mask_(AllIfacesMask)
00064         , allow_anonymous_transfers_(false)
00065     { }
00066 
00067     void init(const DataTypeDescriptor& dtid, CanTxQueue::Qos qos);
00068 
00069     bool isInitialized() const { return data_type_id_ != DataTypeID(); }
00070 
00071     CanIOFlags getCanIOFlags() const { return flags_; }
00072     void setCanIOFlags(CanIOFlags flags) { flags_ = flags; }
00073 
00074     uint8_t getIfaceMask() const { return iface_mask_; }
00075     void setIfaceMask(uint8_t iface_mask)
00076     {
00077         UAVCAN_ASSERT(iface_mask);
00078         iface_mask_ = iface_mask;
00079     }
00080 
00081     TransferPriority getPriority() const { return priority_; }
00082     void setPriority(TransferPriority prio) { priority_ = prio; }
00083 
00084     /**
00085      * Anonymous transfers (i.e. transfers that don't carry a valid Source Node ID) can be sent if
00086      * the local node is configured in passive mode (i.e. the node doesn't have a valid Node ID).
00087      * By default, this class will return an error if it is asked to send a transfer while the
00088      * node is configured in passive mode. However, if this option is enabled, it will be possible
00089      * to send anonymous transfers from passive mode.
00090      */
00091     void allowAnonymousTransfers() { allow_anonymous_transfers_ = true; }
00092 
00093     /**
00094      * Send with explicit Transfer ID.
00095      * Should be used only for service responses, where response TID should match request TID.
00096      */
00097     int send(const uint8_t* payload, unsigned payload_len, MonotonicTime tx_deadline,
00098              MonotonicTime blocking_deadline, TransferType transfer_type, NodeID dst_node_id,
00099              TransferID tid) const;
00100 
00101     /**
00102      * Send with automatic Transfer ID.
00103      *
00104      * Note that as long as the local node operates in passive mode, the
00105      * flag @ref CanIOFlagAbortOnError will be set implicitly for all outgoing frames.
00106      *
00107      * TID is managed by OutgoingTransferRegistry.
00108      */
00109     int send(const uint8_t* payload, unsigned payload_len, MonotonicTime tx_deadline,
00110              MonotonicTime blocking_deadline, TransferType transfer_type, NodeID dst_node_id) const;
00111 };
00112 
00113 }
00114 
00115 #endif // UAVCAN_TRANSPORT_TRANSFER_SENDER_HPP_INCLUDED