AbstractInterface.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <memory>
27 #include <string>
28 #include "Types.h"
29 #include "BinaryLogger.h"
30 
31 
32 #define MSG_PREAMBLE_BYTE 0xaa
33 #define MSG_PREAMBLE_LEN 3
34 
35 // Combine bytes to different types
36 #define make_short( lowbyte, highbyte ) ( (unsigned short)lowbyte | ( (unsigned short)highbyte << 8 ) )
37 #define make_signed_short( lowbyte, highbyte ) ( (signed short) ( (unsigned short) lowbyte | ( (unsigned short) highbyte << 8 ) ) )
38 #define make_int( lowbyte, mid1, mid2, highbyte ) ( (unsigned int) lowbyte | ( (unsigned int) mid1 << 8 ) | ( (unsigned int) mid2 << 16 ) | ( (unsigned int) highbyte << 24 ) )
39 #define make_float( result, byteptr ) memcpy( &result, byteptr, sizeof( float ) )
40 
41 // Byte access
42 #define hi( x ) (unsigned char) ( ((x) >> 8) & 0xff ) // Returns the upper byte of the passed short
43 #define lo( x ) (unsigned char) ( (x) & 0xff ) // Returns the lower byte of the passed short
44 
45 
46 
47 struct Response;
48 
50 {
51 public:
53  virtual ~AbstractInterface();
54  virtual int open() = 0;
55  virtual void close() = 0;
56  int read(unsigned char* buf, unsigned int len);
57  int write(unsigned char* buf, unsigned int len);
58 
59  bool IsConnected() const
60  {
61  return connected;
62  }
63 
64  virtual std::string toString() const = 0;
65 
66  int send(unsigned char id, unsigned int len, unsigned char* data);
67  int receive(msg_t* msg);
68  Response submitCmd(unsigned char id, unsigned char* payload, unsigned int len, bool pending);
69  Response receive(bool pending, unsigned char expectedId);
71  void fireAndForgetCmd(unsigned char id, unsigned char* payload, unsigned int len, bool pending);
72 
73  void startLogging(std::string file);
74  void logText(std::string message);
75 
76 protected:
77  bool connected;
78 
79  virtual int readInternal(unsigned char* buf, unsigned int len) = 0;
80  virtual int writeInternal(unsigned char* buf, unsigned int len) = 0;
81 
82 private:
83  friend std::ostream& operator<<(std::ostream&, const AbstractInterface&);
84  std::shared_ptr<BinaryLogger> log;
85 };
86 
87 std::ostream& operator<<(std::ostream& strm, const AbstractInterface& a);
88 
89 
BinaryLogger.h
AbstractInterface::read
int read(unsigned char *buf, unsigned int len)
Definition: AbstractInterface.cpp:45
AbstractInterface::~AbstractInterface
virtual ~AbstractInterface()
Definition: AbstractInterface.cpp:41
AbstractInterface::toString
virtual std::string toString() const =0
operator<<
std::ostream & operator<<(std::ostream &strm, const AbstractInterface &a)
Definition: AbstractInterface.cpp:326
AbstractInterface::write
int write(unsigned char *buf, unsigned int len)
Definition: AbstractInterface.cpp:57
AbstractInterface::operator<<
friend std::ostream & operator<<(std::ostream &, const AbstractInterface &)
Definition: AbstractInterface.cpp:326
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
AbstractInterface::receiveWithoutChecks
Response receiveWithoutChecks()
Definition: AbstractInterface.cpp:107
AbstractInterface::AbstractInterface
AbstractInterface()
Definition: AbstractInterface.cpp:35
AbstractInterface::IsConnected
bool IsConnected() const
Definition: AbstractInterface.h:59
AbstractInterface::submitCmd
Response submitCmd(unsigned char id, unsigned char *payload, unsigned int len, bool pending)
Definition: AbstractInterface.cpp:68
AbstractInterface::send
int send(unsigned char id, unsigned int len, unsigned char *data)
Definition: AbstractInterface.cpp:275
AbstractInterface::open
virtual int open()=0
AbstractInterface::fireAndForgetCmd
void fireAndForgetCmd(unsigned char id, unsigned char *payload, unsigned int len, bool pending)
Definition: AbstractInterface.cpp:74
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
AbstractInterface
Definition: AbstractInterface.h:49
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
AbstractInterface::logText
void logText(std::string message)
Definition: AbstractInterface.cpp:99
AbstractInterface::startLogging
void startLogging(std::string file)
Definition: AbstractInterface.cpp:94
AbstractInterface::writeInternal
virtual int writeInternal(unsigned char *buf, unsigned int len)=0
AbstractInterface::readInternal
virtual int readInternal(unsigned char *buf, unsigned int len)=0
Response
Definition: Response.h:34
AbstractInterface::connected
bool connected
Definition: AbstractInterface.h:77
msg_t
Definition: Types.h:28
AbstractInterface::receive
int receive(msg_t *msg)
Definition: AbstractInterface.cpp:169
AbstractInterface::close
virtual void close()=0
Types.h