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
50struct Response;
51
53{
54public:
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
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
80protected:
82
83 virtual int readInternal(unsigned char* buf, unsigned int len) = 0;
84 virtual int writeInternal(unsigned char* buf, unsigned int len) = 0;
85
86private:
87 friend std::ostream& operator<<(std::ostream&, const AbstractInterface&);
88 std::shared_ptr<BinaryLogger> log;
89};
90
91std::ostream& operator<<(std::ostream& strm, const AbstractInterface& a);
std::ostream & operator<<(std::ostream &strm, const AbstractInterface &a)
uint8_t data[1]
virtual int open()=0
void startLogging(std::string file)
int receive(msg_t *msg)
int write(unsigned char *buf, unsigned int len)
virtual int readInternal(unsigned char *buf, unsigned int len)=0
Response submitCmd(unsigned char id, unsigned char *payload, unsigned int len, bool pending)
int read(unsigned char *buf, unsigned int len)
int send(unsigned char id, unsigned int len, unsigned char *data)
virtual std::string toString() const =0
void logText(std::string message)
void fireAndForgetCmd(unsigned char id, unsigned char *payload, unsigned int len, bool pending)
bool IsConnected() const
friend std::ostream & operator<<(std::ostream &, const AbstractInterface &)
virtual int writeInternal(unsigned char *buf, unsigned int len)=0
virtual void close()=0
Definition Types.h:29