KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
entry.h
1 /*
2  * Copyright (c) 2015, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include <cstdint>
35 #include <string>
36 #include <vector>
37 #include <functional>
38 #include <mutex>
39 #include <memory>
40 
41 #include "types.h"
42 #include "value.h"
43 #include "access_method.h"
44 
45 namespace kaco {
46 
53  class Entry {
54 
55  public:
56 
60  using ValueChangedCallback = std::function< void(const Value& value) >;
61 
63  Entry();
64 
71  Entry(const uint16_t _index, const uint8_t _subindex, const std::string& _name, const Type _type, const AccessType _access_type);
72 
74  Entry(const Entry& other) = delete;
75 
77  Entry(Entry&& other) = default;
78 
80  Entry& operator=(const Entry& other) = delete;
81 
83  Entry& operator=(Entry&& other) = default;
84 
88  void set_value(const Value& value);
89 
93  const Value& get_value() const;
94 
97  bool valid() const;
98 
101  Type get_type() const;
102 
106 
110  void print() const;
111 
115  bool operator<(const Entry& other) const;
116 
118  uint16_t index;
119 
122  uint8_t subindex; // only used if is_array==false
123 
126  std::string name;
127 
129  Type type;
130 
132  AccessType access_type;
133 
136  ReadAccessMethod read_access_method = ReadAccessMethod::sdo;
137 
140  WriteAccessMethod write_access_method = WriteAccessMethod::sdo;
141 
144  bool disabled;
145 
146  // maybe supported in future:
147  //bool is_slice;
148  //uint8_t slice_first_bit;
149  //uint8_t slice_last_bit;
150 
151  // TODO: Add fields for default, max and min value, pdo mapping boolean, object flags, and object type.
152  // TODO: Maybe the array functionality is obsolete when using EDS files...
153 
157  bool is_generic = false;
158 
159  private:
160 
161  Value m_value;
162  bool m_valid = false;
163  Value m_dummy_value;
164 
165  std::vector<ValueChangedCallback> m_value_changed_callbacks;
166  std::unique_ptr<std::mutex> m_value_changed_callbacks_mutex;
167 
171  std::unique_ptr<std::recursive_mutex> m_read_write_mutex; // recursive_mutex because of valid() used by get_entry(), on heap because mutexes aren't movable.
172 
173 
174  };
175 
176 } // end namespace kaco
Type get_type() const
Returns the data type.
Definition: entry.cpp:106
void add_value_changed_callback(ValueChangedCallback callback)
Registers a given function to be called when the value is changed.
Definition: entry.cpp:110
Type type
Data type of the value.
Definition: entry.h:129
std::function< void(const Value &value) > ValueChangedCallback
type of a callback for a value changed event Important: Never call add_value_changed_callback() from ...
Definition: entry.h:60
const Value & get_value() const
Returns the value.
Definition: entry.cpp:94
Entry & operator=(const Entry &other)=delete
copy assignment
WriteAccessMethod write_access_method
Standard method for writing this entry. Used by Device::set_entry().
Definition: entry.h:140
void set_value(const Value &value)
Sets the value.
Definition: entry.cpp:63
AccessType access_type
Accessibility of the entry.
Definition: entry.h:132
uint16_t index
index in dictionary
Definition: entry.h:118
std::string name
Human-readable name Should be escaped for consitency using Utils::escape().
Definition: entry.h:126
uint8_t subindex
subindex in dictionary. if is_array==true, this variable is not used
Definition: entry.h:122
bool disabled
Disables this entry. This is used when a device reports "Object does not exist in the object dictiona...
Definition: entry.h:144
bool is_generic
This is set to true, if the entry has been created through a default CiA EDS file. This means that it's not guaranteed that the entry actually exists in the current device. For manually added entries and entries from manufacturer-specific EDS files, this is set to false.
Definition: entry.h:157
ReadAccessMethod read_access_method
Standard method for reading this entry. Used by Device::get_entry().
Definition: entry.h:136
bool valid() const
Returns if the value is set/valid.
Definition: entry.cpp:102
bool operator<(const Entry &other) const
Compares entries by index and subindex. This can be used for sorting the dictionary.
Definition: entry.cpp:136
This class represents an entry in the object dictionary of a device.
Definition: entry.h:53
void print() const
Prints relevant information concerning this entry on standard output - name, index, possibly value, ... This is used by Device::print_dictionary()
Definition: entry.cpp:115
Entry()
Constructs an empty entry.
Definition: entry.cpp:43
This class contains a value to be stored in the object dictionary. The value can have one of the type...
Definition: value.h:53