VivicoreSerial library
VivicoreSerialDataCode.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021 VIVIWARE JAPAN, Inc. All right reserved.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  as published by the Free Software Foundation; either version 2
7  of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
24 #ifndef VIVICORESERIAL_DATACODE_H
25 #define VIVICORESERIAL_DATACODE_H
26 
27 #include "CommunicationProtocol.h"
28 
35 struct dcInfo_t {
36  DcGroup_t group_no;
37  DcNature_t data_nature;
38  DcType_t data_type;
39  int16_t data_min;
40  int16_t data_max;
41  int16_t data_ini;
42 };
43 
45 #define NUM_DECODED_BUFF (NUM_MAX_UART_PKT_BODY_DATA * 2) // maximum buffer for input of encode and output of decode
46 #define NUM_DATA_PKT_STRUCT_LEN (NUM_DECODED_BUFF > NUM_MAX_UART_PKT ? NUM_DECODED_BUFF : NUM_MAX_UART_PKT)
47 
48 struct data_pkt {
49  uint8_t data[NUM_DATA_PKT_STRUCT_LEN];
50  uint8_t datalen;
51 };
58 public:
63 
71  DataCodeTranslator(const dcInfo_t *dc_info, const uint8_t dc_num, bool *initialized);
72 
74  virtual ~DataCodeTranslator(void) {}
75  DataCodeTranslator(const DataCodeTranslator &) = delete;
76  DataCodeTranslator &operator=(const DataCodeTranslator &) = delete;
84  inline uint8_t getDcNum(void) {
85  return _dc_num;
86  }
87 
93  inline const dcInfo_t *getDcInfo(void) {
94  return _dc_info;
95  }
96 
102  inline bool hasBinary(void) {
103  return _has_binary;
104  }
105 
117  inline bool hasError(void) {
118  return !(_is_dc_num_in_max && _is_dc_info_in_packet_length && _is_dc_minmax_valid && !_has_truncated_data &&
119  (_has_binary ? (_dc_num == 1) : true));
120  };
121 
129  inline bool hasFatalError(void) {
130  return !_is_known_dc_type;
131  };
132 
140  bool init(const dcInfo_t *dc_info, const uint8_t dc_num);
141 
156  bool encode(const int16_t *encoding_values, data_pkt *out);
157 
177  bool encode(const int16_t *encoding_values, const bool *is_set, data_pkt *out, const bool is_initial_data = false);
178 
198  bool decode(const data_pkt *in, int16_t *values, uint8_t *dc_nums, uint8_t *dc_nums_count);
199 
200 private:
201  struct Range_t {
202  int32_t min;
203  int32_t max;
204  };
205 
206  uint8_t _dc_num = 0;
207  bool _is_dc_num_in_max = false;
208  bool _is_dc_minmax_valid = false;
209  bool _is_dc_info_in_packet_length = false;
210  bool _is_known_dc_type = false;
211  bool _has_truncated_data = false;
212  bool _has_binary = false;
213  uint8_t _max_data_body_length = 0;
214  dcInfo_t _dc_info[NUM_MAX_DC] = {};
215 
216  uint8_t getDataBodyLength(void);
217  Range_t getDataRange(const DcType_t dc_type);
218 
219  inline size_t getDataSize(const DcType_t dc_type) {
220  if (dc_type == DcType_t::DC_TYPE_ANALOG_2BYTES) {
221  return 2;
222  } else if ((dc_type == DcType_t::DC_TYPE_ANALOG_1BYTE) || (dc_type == DcType_t::DC_TYPE_BOOLEAN) ||
223  (dc_type == DcType_t::DC_TYPE_BINARY)) {
224  return 1;
225  }
226  // unknown type and size
227  return 0;
228  };
229 };
230 
231 #endif // VIVICORESERIAL_DATACODE_H
Communication protocol definitions for VIVIWARE Cell Branch and Custom.
#define NUM_MAX_DC
Maximum number of DC info in a Branch.
Definition: CommunicationProtocol.h:265
This class is to encode and decode DCDT format data which is used between VIVIWARE Cell App,...
Definition: VivicoreSerialDataCode.h:57
bool hasBinary(void)
Definition: VivicoreSerialDataCode.h:102
bool init(const dcInfo_t *dc_info, const uint8_t dc_num)
Definition: VivicoreSerialDataCode.cpp:34
bool hasFatalError(void)
Definition: VivicoreSerialDataCode.h:129
uint8_t getDcNum(void)
Definition: VivicoreSerialDataCode.h:84
bool hasError(void)
Definition: VivicoreSerialDataCode.h:117
bool decode(const data_pkt *in, int16_t *values, uint8_t *dc_nums, uint8_t *dc_nums_count)
Definition: VivicoreSerialDataCode.cpp:218
const dcInfo_t * getDcInfo(void)
Definition: VivicoreSerialDataCode.h:93
bool encode(const int16_t *encoding_values, data_pkt *out)
Definition: VivicoreSerialDataCode.cpp:104
DataCodeTranslator(void)
Definition: VivicoreSerialDataCode.h:62
DC information structure for each input or output of Branch.
Definition: VivicoreSerialDataCode.h:35
DcNature_t data_nature
Definition: VivicoreSerialDataCode.h:37
int16_t data_min
Definition: VivicoreSerialDataCode.h:39
int16_t data_max
Definition: VivicoreSerialDataCode.h:40
DcType_t data_type
Definition: VivicoreSerialDataCode.h:38
DcGroup_t group_no
Definition: VivicoreSerialDataCode.h:36
int16_t data_ini
Definition: VivicoreSerialDataCode.h:41