communicator.h
Go to the documentation of this file.
1 //
2 // Created by sub on 18/12/17.
3 //
4 
5 #ifndef RIC_INTERFACE_COMMUNICATOR_H
6 #define RIC_INTERFACE_COMMUNICATOR_H
7 
10 #include <string.h>
11 #include <iostream>
12 #include "crc8.h"
13 
14 
15 
16 namespace ric_interface
17 {
18  typedef uint8_t byte;
20  {
21 
22  public:
23 
24  enum State
25  {
30  };
31 
32  void connect(std::string port, int baudrate)
33  {
34  serial_.connect(port, baudrate);
35  }
36 
37  /* return -1 for reading in process, or type of the incoming pkg */
38  int read(byte buff[])
39  {
40  switch (state_)
41  {
42  case HEADER_PART_A: //read header
43  {
44  if (tryReadHeader())
46  break;
47  }
48  case HEADER_PART_B: //read pkg size
49  {
51  if (pkg_size_ != -1)
52  state_ = PACKAGE;
53  break;
54  }
55  case PACKAGE:
56  {
57  int incoming = serial_.read();
58  if (incoming != -1)
59  buff[pkg_indx_++] = (byte)incoming;
60 
61  if (pkg_indx_ >= pkg_size_) //done reading pkg content
62  state_ = CHECKSUM;
63  break;
64  }
65  case CHECKSUM:
66  {
67 
68  int incoming = serial_.read();
69  if (incoming != -1)
70  {
71  byte incoming_checksum = (byte)incoming;
72  byte computed_checksum = crc_.get_crc(buff, pkg_size_);
73 
74  //fprintf(stderr, "got chksum: %d, comp chksum: %d\n", incoming_checksum, computed_checksum);
75 
76  if (incoming_checksum == computed_checksum)
77  {
79  fromBytes(buff, sizeof(protocol::package), pkg);
80  reset();
81  return (uint8_t)pkg.type;
82  }
83  else
84  return -2; //wrong checksum
85  }
86  break;
87  }
88  }
89  return -1;
90  }
91 
92  static void fromBytes(byte buff[], size_t pkg_size, protocol::package &pkg)
93  {
94  memcpy(&pkg, buff, pkg_size);
95  }
96 
97  static void toBytes(const protocol::package &pkg, size_t pkg_size, byte buff[])
98  {
99  memcpy(buff, &pkg, pkg_size);
100  }
101 
102  bool write(const protocol::package &pkg, size_t pkg_size)
103  {
104  /* send pkg header */
105  byte header_buff[2];
107  header_buff[protocol::PKG_SIZE_INDX] = pkg_size;
108  if (!serial_.send(header_buff, 2))
109  return false;
110  byte pkg_buff[pkg_size];
111  toBytes(pkg, pkg_size, pkg_buff);
112  byte checksum[1];
113  checksum[0] = crc_.get_crc(pkg_buff, pkg_size);
114  /* send pkg content and the checksum */
115  if (!serial_.send(pkg_buff, pkg_size))
116  return false;
117  if (!serial_.send(checksum, 1))
118  return false;
119  return true;
120  }
121 
122 
123  private:
126  int pkg_indx_ = 0;
127  int pkg_size_ = 0;
129 
130  void reset()
131  {
132  state_ = HEADER_PART_A;
133  pkg_indx_ = 0;
134  pkg_size_ = 0;
135  }
136 
137  /* try to read valid header start */
139  {
140  if (serial_.read() == protocol::HEADER_CODE)
141  return true;
142  return false;
143  }
144 
145  /* return -1 for failure and pkg size as success */
147  {
148  return serial_.read();
149  }
150  };
151 }
152 
153 #endif //RIC_INTERFACE_COMMUNICATOR_H
const uint8_t HEADER_INDX
Definition: protocol.h:24
uint8_t byte
Definition: communicator.h:18
const uint8_t PKG_SIZE_INDX
Definition: protocol.h:25
bool write(const protocol::package &pkg, size_t pkg_size)
Definition: communicator.h:102
bool send(const byte buff[], size_t size)
Definition: serial_com.cpp:85
const uint8_t HEADER_CODE
Definition: protocol.h:23
void connect(std::string port, int baudrate)
Definition: communicator.h:32
static void toBytes(const protocol::package &pkg, size_t pkg_size, byte buff[])
Definition: communicator.h:97
Definition: crc8.h:13
int read(byte buff[], size_t size)
Definition: serial_com.cpp:52
void connect(std::string port, int baudrate)
Definition: serial_com.cpp:6
static void fromBytes(byte buff[], size_t pkg_size, protocol::package &pkg)
Definition: communicator.h:92
byte get_crc(byte message[], size_t size)
Definition: crc8.h:45


ric_interface
Author(s):
autogenerated on Wed Jan 3 2018 03:48:20