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 
29 #include "BinaryLogger.h"
30 #include "Types.h"
31 
32 
33 #define MSG_PREAMBLE_BYTE 0xaa
34 #define MSG_PREAMBLE_LEN 3
35 
36 // Combine bytes to different types
37 #define make_short(lowbyte, highbyte) ((unsigned short)lowbyte | ((unsigned short)highbyte << 8))
38 #define make_signed_short(lowbyte, highbyte) \
39  ((signed short)((unsigned short)lowbyte | ((unsigned short)highbyte << 8)))
40 #define make_int(lowbyte, mid1, mid2, highbyte) \
41  ((unsigned int)lowbyte | ((unsigned int)mid1 << 8) | ((unsigned int)mid2 << 16) | \
42  ((unsigned int)highbyte << 24))
43 #define make_float(result, byteptr) memcpy(&result, byteptr, sizeof(float))
44 
45 // Byte access
46 #define hi(x) (unsigned char)(((x) >> 8) & 0xff) // Returns the upper byte of the passed short
47 #define lo(x) (unsigned char)((x) & 0xff) // Returns the lower byte of the passed short
48 
49 
50 struct Response;
51 
53 {
54 public:
56  virtual ~AbstractInterface();
57  virtual int open() = 0;
58  virtual void close() = 0;
59  int read(unsigned char* buf, unsigned int len);
60  int write(unsigned char* buf, unsigned int len);
61 
62  bool
63  IsConnected() const
64  {
65  return connected;
66  }
67 
68  virtual std::string toString() const = 0;
69 
70  int send(unsigned char id, unsigned int len, unsigned char* data);
71  int receive(msg_t* msg);
72  Response submitCmd(unsigned char id, unsigned char* payload, unsigned int len, bool pending);
73  Response receive(bool pending, unsigned char expectedId);
75  void fireAndForgetCmd(unsigned char id, unsigned char* payload, unsigned int len, bool pending);
76 
77  void startLogging(std::string file);
78  void logText(std::string message);
79 
80 protected:
81  bool connected;
82 
83  virtual int readInternal(unsigned char* buf, unsigned int len) = 0;
84  virtual int writeInternal(unsigned char* buf, unsigned int len) = 0;
85 
86 private:
87  friend std::ostream& operator<<(std::ostream&, const AbstractInterface&);
88  std::shared_ptr<BinaryLogger> log;
89 };
90 
91 std::ostream& operator<<(std::ostream& strm, const AbstractInterface& a);
BinaryLogger.h
AbstractInterface::read
int read(unsigned char *buf, unsigned int len)
Definition: AbstractInterface.cpp:46
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:343
AbstractInterface::write
int write(unsigned char *buf, unsigned int len)
Definition: AbstractInterface.cpp:59
AbstractInterface::operator<<
friend std::ostream & operator<<(std::ostream &, const AbstractInterface &)
Definition: AbstractInterface.cpp:343
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
AbstractInterface::receiveWithoutChecks
Response receiveWithoutChecks()
Definition: AbstractInterface.cpp:119
AbstractInterface::AbstractInterface
AbstractInterface()
Definition: AbstractInterface.cpp:37
AbstractInterface::IsConnected
bool IsConnected() const
Definition: AbstractInterface.h:63
AbstractInterface::submitCmd
Response submitCmd(unsigned char id, unsigned char *payload, unsigned int len, bool pending)
Definition: AbstractInterface.cpp:70
AbstractInterface::send
int send(unsigned char id, unsigned int len, unsigned char *data)
Definition: AbstractInterface.cpp:294
AbstractInterface::open
virtual int open()=0
AbstractInterface::fireAndForgetCmd
void fireAndForgetCmd(unsigned char id, unsigned char *payload, unsigned int len, bool pending)
Definition: AbstractInterface.cpp:80
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
AbstractInterface
Definition: AbstractInterface.h:52
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
AbstractInterface::logText
void logText(std::string message)
Definition: AbstractInterface.cpp:110
AbstractInterface::startLogging
void startLogging(std::string file)
Definition: AbstractInterface.cpp:104
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:36
AbstractInterface::connected
bool connected
Definition: AbstractInterface.h:81
msg_t
Definition: Types.h:28
AbstractInterface::receive
int receive(msg_t *msg)
Definition: AbstractInterface.cpp:184
AbstractInterface::close
virtual void close()=0
Types.h