SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
trigger.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <cstring>
5 #include <functional>
6 #include <string>
7 
8 #include "core/ma_core.h"
9 #include "porting/ma_porting.h"
10 #include "refactor_required.hpp"
11 #include "resource.hpp"
12 
13 namespace ma::server::callback {
14 
15 static bool parseAndSetTriggerRules(const std::string& trigger) {
16 
17  std::forward_list<std::shared_ptr<TriggerRule>> rules;
18 
19  for (size_t i = 0, j = 0; i < trigger.size(); i = j + 1) {
20  j = trigger.find('|', i);
21  if (j == std::string::npos) {
22  j = trigger.size();
23  }
24  auto token = trigger.substr(i, j - i);
25  auto ptr = TriggerRule::create(token);
26  if (ptr) {
27  rules.push_front(std::move(ptr));
28  } else {
29  return false;
30  }
31  }
32 
34  trigger_rules = std::move(rules);
36 
37  return true;
38 }
39 
40 void configureTrigger(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder, bool called_by_event = false) {
41  // [argv] 0: cmd, 1: trigger string
42  // [trigger string] 0: class id, 1: condition, 2: threshold, 3: gpio pin, 4: default level, 5: trigger level | ...
43  // [condition] 0: >, 1: <, 2: >=, 3: <=, 4: ==, 5: !=
44  // [threshold] [0-100]: integer
45  // [gpio pin] {1,2,3,21,41,42}: integer
46  // [default level] {0,1}: integer
47  // [trigger level] {0,1}: integer
48  // example: "0,1,50,1,0,1|1,1,50,2,0,1"
49 
50  ma_err_t ret = MA_OK;
51  if (argv.size() < 2) {
52  ret = MA_EINVAL;
53  goto exit;
54  }
55 
56  if (!parseAndSetTriggerRules(argv[1])) {
57  ret = MA_EINVAL;
58  goto exit;
59  }
60 
61  if (!called_by_event) {
62  MA_STORAGE_SET_STR(ret, static_resource->device->getStorage(), MA_STORAGE_KEY_TRIGGER_RULES, argv[1]);
63  }
64 
65 exit:
66  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
67  encoder.write("trigger_rules", argv[1]);
68  encoder.end();
69  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
70 }
71 
72 void getTrigger(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
73  ma_err_t ret = MA_OK;
74 
75  std::string trigger;
76  MA_STORAGE_GET_STR(static_resource->device->getStorage(), MA_STORAGE_KEY_TRIGGER_RULES, trigger, "");
77 
78  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
79  encoder.write("trigger_rules", trigger);
80  encoder.end();
81  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
82 }
83 
84 void initDefaultTrigger(Encoder& encoder) {
85  if (static_resource->device->getTransports().empty()) {
86  MA_LOGD(MA_TAG, "No transport available");
87  return;
88  }
89  auto& transport = static_resource->device->getTransports().front();
90  if (!transport || !*transport) {
91  MA_LOGD(MA_TAG, "Transport not available");
92  return;
93  }
94 
95  std::string trigger;
96  MA_STORAGE_GET_STR(static_resource->device->getStorage(), MA_STORAGE_KEY_TRIGGER_RULES, trigger, "");
97 
98  std::vector<std::string> args{"INIT@TRIGGER", std::move(trigger)};
99  configureTrigger(args, *transport, encoder, true);
100 }
101 
102 } // namespace ma::server::callback
Definition: ma_codec_base.h:14
virtual ma_err_t end()=0
Encoder type for end.
virtual const void * data() const =0
Encoder type for get data.
virtual ma_err_t begin()=0
Encoder type for begin.
virtual ma_err_t write(const std::string &key, int8_t value)=0
Encoder type for write int8_t value.
virtual const size_t size() const =0
Encoder type for get size.
bool lock() const
bool unlock() const
Definition: ma_transport.h:12
virtual size_t send(const char *data, size_t length) noexcept=0
#define MA_LOGD(TAG,...)
Definition: ma_debug.h:79
#define MA_TAG
Definition: ma_debug.h:9
#define MA_STORAGE_KEY_TRIGGER_RULES
Definition: ma_definations.h:43
@ MA_MSG_TYPE_RESP
Definition: ma_types.h:252
ma_err_t
Definition: ma_types.h:21
@ MA_OK
Definition: ma_types.h:23
@ MA_EINVAL
Definition: ma_types.h:28
Definition: algorithm.hpp:11
void initDefaultTrigger(Encoder &encoder)
Definition: trigger.hpp:84
void getTrigger(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder)
Definition: trigger.hpp:72
void configureTrigger(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder, bool called_by_event=false)
Definition: trigger.hpp:40
std::forward_list< std::shared_ptr< TriggerRule > > trigger_rules
Definition: refactor_required.hpp:296
ma::Mutex trigger_rules_mutex
Definition: refactor_required.hpp:297
#define static_resource
Definition: resource.hpp:64
static std::shared_ptr< TriggerRule > create(const std::string &rule_str)
Definition: refactor_required.hpp:147