libuav original

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers transport_stats_provider.hpp Source File

transport_stats_provider.hpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #ifndef UAVCAN_PROTOCOL_TRANSPORT_STATS_PROVIDER_HPP_INCLUDED
00006 #define UAVCAN_PROTOCOL_TRANSPORT_STATS_PROVIDER_HPP_INCLUDED
00007 
00008 #include <uavcan/build_config.hpp>
00009 #include <uavcan/node/service_server.hpp>
00010 #include <uavcan/util/method_binder.hpp>
00011 #include <uavcan/protocol/GetTransportStats.hpp>
00012 
00013 namespace uavcan
00014 {
00015 /**
00016  * This class provides statistics about the transport layer performance on the local node.
00017  * The user's application does not deal with this class directly because it's instantiated by the node class.
00018  */
00019 class UAVCAN_EXPORT TransportStatsProvider : Noncopyable
00020 {
00021     typedef MethodBinder<const TransportStatsProvider*,
00022                          void (TransportStatsProvider::*)(const protocol::GetTransportStats::Request&,
00023                                                           protocol::GetTransportStats::Response&) const>
00024             GetTransportStatsCallback;
00025 
00026     ServiceServer<protocol::GetTransportStats, GetTransportStatsCallback> srv_;
00027 
00028     void handleGetTransportStats(const protocol::GetTransportStats::Request&,
00029                                  protocol::GetTransportStats::Response& resp) const
00030     {
00031         const TransferPerfCounter& perf = srv_.getNode().getDispatcher().getTransferPerfCounter();
00032         resp.transfer_errors = perf.getErrorCount();
00033         resp.transfers_tx = perf.getTxTransferCount();
00034         resp.transfers_rx = perf.getRxTransferCount();
00035 
00036         const CanIOManager& canio = srv_.getNode().getDispatcher().getCanIOManager();
00037         for (uint8_t i = 0; i < canio.getNumIfaces(); i++)
00038         {
00039             const CanIfacePerfCounters can_perf = canio.getIfacePerfCounters(i);
00040             protocol::CANIfaceStats stats;
00041             stats.errors = can_perf.errors;
00042             stats.frames_tx = can_perf.frames_tx;
00043             stats.frames_rx = can_perf.frames_rx;
00044             resp.can_iface_stats.push_back(stats);
00045         }
00046     }
00047 
00048 public:
00049     explicit TransportStatsProvider(INode& node)
00050         : srv_(node)
00051     { }
00052 
00053     /**
00054      * Once started, this class requires no further attention.
00055      * Returns negative error code.
00056      */
00057     int start()
00058     {
00059         return srv_.start(GetTransportStatsCallback(this, &TransportStatsProvider::handleGetTransportStats));
00060     }
00061 };
00062 
00063 }
00064 
00065 #endif // UAVCAN_PROTOCOL_TRANSPORT_STATS_PROVIDER_HPP_INCLUDED