changeset 123:25d2ba77fe8b main

Command line tool to listen, or send messages, for the canbus.
author Bob Cook <bob@bobcookdev.com>
date Wed, 14 Dec 2011 11:46:20 -0800
parents 0448b2b7c530
children 9b41739be9c2
files main/robots/cantool/CanToolApp.cpp main/robots/cantool/CanToolApp.h main/robots/cantool/CmdSonarFront.cpp main/robots/cantool/Commands.h main/robots/cantool/jamfile main/robots/cantool/main.cpp
diffstat 6 files changed, 507 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cantool/CanToolApp.cpp	Wed Dec 14 11:46:20 2011 -0800
@@ -0,0 +1,182 @@
+// ----------------------------------------------------------------------------------------
+//
+//  robots/cantool/CanToolApp.cpp
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+//
+//  Application object implementation for demonstrating the CANSocket class.    
+//
+//  Copyright (c) 2011 Bob Cook
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <iostream>
+#include <memory>
+#include <stdio.h>
+#include <string>
+#include <vector>
+
+#include <Poco/AutoPtr.h>
+#include <Poco/Thread.h>
+#include <Poco/Util/Application.h>
+#include <Poco/Util/HelpFormatter.h>
+#include <Poco/Util/Option.h>
+#include <Poco/Util/OptionSet.h>
+
+#include "CanToolApp.h"
+#include "Commands.h"
+
+#include "packages/common/can/can_helpers.h"
+
+#include "packages/linux/can/CANMessage.h"
+#include "packages/linux/can/CANMsgProcessor.h"
+
+// ----------------------------------------------------------------------------------------
+
+void CommandListen( const std::vector< std::string >& args )
+{
+    std::cout << "listening:" << std::endl;
+
+    for ( ;; )
+    {
+        CANMessage* msg = CANMessage::WaitDequeueReceived( 250 ); // 250 ms
+
+        if ( msg == 0 )
+        {
+            continue;
+        }
+
+        std::cout << "received: " << CANMessage::asText( msg ) << std::endl;
+    }
+}
+
+// ----------------------------------------------------------------------------------------
+
+CanToolApp::CanToolApp()
+    : m_helpRequested( false ),
+      m_canInterfaceName( "can0" ),
+      m_commandDispatchTable()
+{
+}
+
+// ----------------------------------------------------------------------------------------
+
+CanToolApp::~CanToolApp()
+{
+}
+
+// ----------------------------------------------------------------------------------------
+
+void CanToolApp::initialize( Poco::Util::Application& self )
+{
+    loadConfiguration();
+    Poco::Util::Application::initialize( self );
+
+    m_commandDispatchTable[ "listen" ] = CommandListen;
+    m_commandDispatchTable[ "sonarfront" ] = CommandSonarFront;
+}
+
+// ----------------------------------------------------------------------------------------
+
+void CanToolApp::uninitialize()
+{
+    Poco::Util::Application::uninitialize();
+}
+
+// ----------------------------------------------------------------------------------------
+
+void CanToolApp::defineOptions( Poco::Util::OptionSet& options )
+{
+    Poco::Util::Application::defineOptions( options );
+
+    options.addOption(
+        Poco::Util::Option( "help", "h", "display argument help information" )
+            .required( false )
+            .repeatable( false )
+            .callback( Poco::Util::OptionCallback< CanToolApp >(
+                                                    this, &CanToolApp::handleHelp ) ) );
+
+    options.addOption(
+        Poco::Util::Option( "interface", "i", "specify the CAN interface" )
+            .required( false )
+            .repeatable( false )
+            .callback( Poco::Util::OptionCallback< CanToolApp >(
+                                               this, &CanToolApp::setCanInterface ) ) );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void CanToolApp::handleHelp( const std::string& name, const std::string& value )
+{
+    Poco::Util::HelpFormatter helpFormatter( options() );
+    helpFormatter.setCommand( commandName() );
+    helpFormatter.format( std::cout );
+    stopOptionsProcessing();
+    m_helpRequested = true;
+}
+
+// ----------------------------------------------------------------------------------------
+
+void CanToolApp::setCanInterface( const std::string& name, const std::string& value )
+{
+    m_canInterfaceName = value;
+}
+
+// ----------------------------------------------------------------------------------------
+
+int CanToolApp::main( const std::vector< std::string >& args )
+{
+    if ( m_helpRequested )
+    {
+        return Poco::Util::Application::EXIT_OK;
+    }
+
+    loadConfiguration();
+
+    Poco::Thread    canMsgProcessorThread;
+    CANMsgProcessor canMsgProcessor( m_canInterfaceName );
+    canMsgProcessorThread.start( canMsgProcessor );
+
+    if ( ! args.empty() )
+    {
+        if ( m_commandDispatchTable.find( args[ 0 ] ) != m_commandDispatchTable.end() )
+        {
+            ( m_commandDispatchTable[ args[ 0 ] ] )( args );
+        }
+        else
+        {
+            std::cerr << "command \"" << args[ 0 ] << "\" not recognized" << std::endl;
+        }
+    }
+    else
+    {
+        std::cerr << "error: no command given" << std::endl;
+    }
+
+    canMsgProcessor.timeToQuit();
+    canMsgProcessorThread.join();
+
+    return Poco::Util::Application::EXIT_OK;
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cantool/CanToolApp.h	Wed Dec 14 11:46:20 2011 -0800
@@ -0,0 +1,79 @@
+// ----------------------------------------------------------------------------------------
+//
+//  robots/odr/CanToolApp.h
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+//    
+//  Application object for the OutDoor Robot
+//
+//  Copyright (c) 2010 Bob Cook
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#ifndef BCDRL_ROBOTS_CANTOOL_CANTOOLAPP_H
+#define BCDRL_ROBOTS_CANTOOL_CANTOOLAPP_H
+
+#include <Poco/Util/Application.h>
+
+namespace Poco
+{
+    namespace Util
+    {
+        class OptionSet;
+    }
+}
+
+#include <map>
+#include <string>
+#include <vector>
+
+// ----------------------------------------------------------------------------------------
+
+class CanToolApp : public Poco::Util::Application
+{
+    typedef void (*commandFuncType)( const std::vector< std::string >& args );
+    typedef std::map< std::string, commandFuncType > commandMapType;
+
+    public:
+        CanToolApp();
+        virtual ~CanToolApp();
+
+    protected:
+        void initialize( Poco::Util::Application& self );
+        void uninitialize();
+        void defineOptions( Poco::Util::OptionSet& options );
+        void handleHelp( const std::string& name, const std::string& value );
+        int  main( const std::vector< std::string >& args );
+
+    private:
+        void setCanInterface( const std::string& name, const std::string& value );
+
+    private:
+        bool           m_helpRequested;
+        std::string    m_canInterfaceName;
+        commandMapType m_commandDispatchTable;
+};
+
+// ----------------------------------------------------------------------------------------
+#endif // #ifndef BCDRL_ROBOTS_CANTOOL_CANTOOLAPP_H
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cantool/CmdSonarFront.cpp	Wed Dec 14 11:46:20 2011 -0800
@@ -0,0 +1,82 @@
+// ----------------------------------------------------------------------------------------
+//
+//  robots/cantool/CmdSonarFront.cpp
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+//
+//  Command "sonarfront" implementation.
+//
+//  Copyright (c) 2011 Bob Cook
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <iostream>
+
+#include "Commands.h"
+
+#include "packages/common/can/can_helpers.h"
+#include "packages/common/can/can_messages.h"
+#include "packages/common/can/can_nodes.h"
+
+#include "packages/linux/can/CANMessage.h"
+#include "packages/linux/can/CANMsgProcessor.h"
+
+// ----------------------------------------------------------------------------------------
+
+void CommandSonarFront( const std::vector< std::string >& args )
+{
+    if ( args.size() < 2 )
+    {
+        std::cout << "error: sonarfront <on|off>" << std::endl;
+        return;
+    }
+
+    if ( args[ 1 ] != "on" && args[ 1 ] != "off" )
+    {
+    }
+
+    uint32_t msgid;
+
+    if ( args[ 1 ] == "on" )
+    {
+        msgid = can_build_message_id( can_node_odr_manager,
+                                      can_node_odr_sonar_front,
+                                      can_dataid_sonar_front_enable );
+    }
+    else if ( args[ 1 ] == "off" )
+    {
+        msgid = can_build_message_id( can_node_odr_manager,
+                                      can_node_odr_sonar_front,
+                                      can_dataid_sonar_front_disable );
+    }
+    else
+    {
+        std::cout << "error: sonarfront <on|off>" << std::endl;
+        return;
+    }
+
+    CANMessage::QueueToSend( new CANMessage( msgid ) );
+    std::cout << "sent " << args[ 1 ] << std::endl;
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cantool/Commands.h	Wed Dec 14 11:46:20 2011 -0800
@@ -0,0 +1,42 @@
+// ----------------------------------------------------------------------------------------
+//
+//  robots/cantool/Commands.h
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+//
+//  Command function prototypes.
+//
+//  Copyright (c) 2011 Bob Cook
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <string>
+#include <vector>
+
+// ----------------------------------------------------------------------------------------
+
+void CommandListen( const std::vector< std::string >& args );
+
+void CommandSonarFront( const std::vector< std::string >& args );
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cantool/jamfile	Wed Dec 14 11:46:20 2011 -0800
@@ -0,0 +1,54 @@
+# -----------------------------------------------------------------------------------------
+#
+#   robots/cantool/jamfile
+#
+#   Bob Cook Development, Robotics Library
+#   http://www.bobcookdev.com/rl/
+#    
+#   Copyright (c) 2011 Bob Cook
+#
+#   Permission is hereby granted, free of charge, to any person obtaining a copy
+#   of this software and associated documentation files (the "Software"), to deal
+#   in the Software without restriction, including without limitation the rights
+#   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+#   copies of the Software, and to permit persons to whom the Software is
+#   furnished to do so, subject to the following conditions:
+#
+#   The above copyright notice and this permission notice shall be included in
+#   all copies or substantial portions of the Software.
+#
+#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+#   THE SOFTWARE.
+#
+# -----------------------------------------------------------------------------------------
+
+
+if $(TRACE) { echo "trace /robots/cantool/jamfile" ; }
+
+SubDir TOP robots cantool ;
+
+# -----------------------------------------------------------------------------------------
+
+COMMON_SOURCES = 
+    main.cpp
+    CanToolApp.cpp
+    CmdSonarFront.cpp
+    packages.common.can.pkg
+    packages.linux.can.pkg
+    ;
+
+POCO_SHARED = PocoFoundation.so PocoNet.so PocoUtil.so PocoXML.so ;
+POCO_STATIC = PocoFoundation.a  PocoNet.a  PocoUtil.a  PocoXML.a ;
+
+# -----------------------------------------------------------------------------------------
+
+ubuntu_executable cantool_ubuntu : $(COMMON_SOURCES) $(POCO_SHARED) ;
+overo_executable  cantool        : $(COMMON_SOURCES) $(POCO_STATIC) ;
+
+# -----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/robots/cantool/main.cpp	Wed Dec 14 11:46:20 2011 -0800
@@ -0,0 +1,68 @@
+// ----------------------------------------------------------------------------------------
+//
+//  robots/cantool/main.cpp
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+//    
+//  Main entry point for the CanTool software.
+//
+//  Copyright (c) 2011 Bob Cook
+//
+//  Permission is hereby granted, free of charge, to any person obtaining a copy
+//  of this software and associated documentation files (the "Software"), to deal
+//  in the Software without restriction, including without limitation the rights
+//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+//  copies of the Software, and to permit persons to whom the Software is
+//  furnished to do so, subject to the following conditions:
+//
+//  The above copyright notice and this permission notice shall be included in
+//  all copies or substantial portions of the Software.
+//
+//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+//  THE SOFTWARE.
+//
+// ----------------------------------------------------------------------------------------
+
+#include "CanToolApp.h"
+
+#include <iostream>
+#include <stdexcept>
+
+// ----------------------------------------------------------------------------------------
+
+int main( int argc, char** argv )
+{
+    int result = -1;
+
+    try
+    {
+        CanToolApp app;
+        app.init( argc, argv );
+        result = app.run();
+    }
+    catch ( const Poco::Exception& ex )
+    {
+        std::cerr << "Failed to start application, Poco::Exception: "
+            << ex.displayText() << std::endl;
+    }
+    catch ( const std::exception& ex )
+    {
+        std::cerr << "Failed to start application, std::exception: "
+            << ex.what() << std::endl;
+    }
+    catch ( ... )
+    {
+        std::cerr << "Failed to start application, unknown exception" << std::endl;
+    }
+
+    return result;
+}
+
+// ----------------------------------------------------------------------------------------
+