OOKwiz
on/off-keying for ESP32 and a variety of supported radio modules
Device.cpp
Go to the documentation of this file.
1 #include "Device.h"
2 #include "serial_output.h"
3 #include "device_plugins/DEVICE_INDEX"
4 #include "tools.h"
5 #include "Settings.h"
6 
7 
8 // static members
9 decltype(Device::store) Device::store;
10 int Device::len = 0;
11 
12 bool Device::setup() {
13  INFO("Device plugins loaded: %s\n", list().c_str());
14  return true;
15 }
16 
17 /// @brief Static, passes all 3 forms of an incoming packet to each non-disabled device plugin
18 /// @param raw incoming packet
19 /// @param train incoming packet
20 /// @param meaning incoming packet
21 /// @return `true` as soon as one of the plugin rx functions returns `true`, `false` otherwise
22 bool Device::new_packet(RawTimings &raw, Pulsetrain &train, Meaning &meaning) {
23  for (int n = 0; n < len; n++) {
24  String name = store[n].name;
25  if (!Settings::isSet("device_" + name + "_disable")) {
26  DEBUG("Trying device plugin '%s'.\n", name.c_str());
27  if(store[n].pointer->receive(raw, train, meaning)) {
28  DEBUG("Device plugin '%s' understood it!\n", name.c_str());
29  return true;
30  }
31  }
32  }
33  return false;
34 }
35 
36 /// @brief Registers an instance of Device, i.e. a devuce plugin in the static `store`
37 /// @param name (char*) Name of plugin, maximum MAX_DEVICE_NAME_LEN characters
38 /// @param pointer Pointer to the plugin instance
39 /// @return `false` if store already holds info on MAX_DEVICES plugins
40 bool Device::add(const char* name, Device *pointer) {
41  // Uses char*, and does not DEBUG or INFO because this is ran by the contructor
42  // of the AutoRegister trick, so String and Serial are not available yet.
43  if (len == MAX_DEVICES) {
44  return false;
45  }
46  strncpy(store[len].name, name, MAX_DEVICE_NAME_LEN);
47  store[len].name[MAX_DEVICE_NAME_LEN] = 0; // just in case
48  store[len].pointer = pointer;
49  len++;
50  return true;
51 }
52 
53 /// @brief Returns a String with alist of registered device plugins, appending " [disabled]" if needed
54 /// @param separator Between the names, e.g. ", "
55 /// @return The list
56 String Device::list(String separator) {
57  String ret;
58  for (int n = 0; n < len; n++) {
59  ret += store[n].name;
60  String disable_key;
61  snprintf_append(disable_key, 50, "device_%s_disable", store[n].name);
62  if (Settings::isSet(disable_key)) {
63  ret += " [disabled]";
64  }
65  if (n < len - 1) {
66  ret += separator;
67  }
68  }
69  return ret;
70 }
71 
72 /// @brief static, passes a String to a named device's `tx()` function
73 /// @param plugin_name name of plugin
74 /// @param toTransmit String to be passed to `tx()`
75 /// @return
76 bool Device::transmit(const String &plugin_name, const String &toTransmit) {
77  for (int n = 0; n < len; n++) {
78  if (String(store[n].name) == plugin_name) {
79  INFO("Device '%s': transmitting '%s'\n", plugin_name.c_str(), toTransmit.c_str());
80  return store[n].pointer->transmit(toTransmit);
81  }
82  }
83  ERROR("ERROR: cannot transmit. Device '%s' not found.")
84  return false;
85 }
86 
87 /// @brief virtual, to be overridden in de individual plugins
88 /// @param raw incoming packet
89 /// @param train incoming packet
90 /// @param meaning incoming packet
91 /// @return `false` if not overridden
92 bool Device::receive(const RawTimings &raw, const Pulsetrain &train, const Meaning &meaning) {
93  return false;
94 }
95 
96 /// @brief virtual, to be overridden in de individual plugins
97 /// @param toTransmit String for plugin to make sense of and be transmitted
98 /// @return `false` if not overridden
99 bool Device::transmit(const String &toTransmit) {
100  return false;
101 }