SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
algorithm.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include "core/ma_core.h"
6 #include "porting/ma_porting.h"
7 #include "resource.hpp"
8 
9 #define MA_STORAGE_KEY_ALGORITHM_ID "ma#algorithm_id"
10 
12 
13 void getAvailableAlgorithms(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
14  // Need to refactor the algorithms
15  // - A registry of algorithms is needed and the registry should be able to provide the list of available algorithms
16  // - Each algorithm in the registry should be able to provide the information about itself
17 }
18 
19 void configureAlgorithm(const std::vector<std::string>& argv,
20  Transport& transport,
21  Encoder& encoder,
22  bool called_by_event = false) {
23  MA_ASSERT(argv.size() >= 2);
24  const auto& cmd = argv[0];
25  const size_t algorithm_id = std::atoi(argv[1].c_str());
26 
27  ma_err_t ret = MA_OK;
28  static_resource->current_algorithm_id = algorithm_id;
29 
30  if (!called_by_event) {
31  MA_STORAGE_SET_POD(ret,
32  static_resource->device->getStorage(),
34  static_resource->current_algorithm_id);
35  }
36 
37  encoder.begin(MA_MSG_TYPE_RESP, ret, cmd);
38  encoder.write("algorithm", static_cast<uint32_t>(static_resource->current_algorithm_id));
39  encoder.end();
40  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
41 }
42 
43 void initDefaultAlgorithm(Encoder& encoder) {
44  if (static_resource->device->getTransports().empty()) {
45  MA_LOGD(MA_TAG, "No transport available");
46  return;
47  }
48  auto& transport = static_resource->device->getTransports().front();
49  if (!transport || !*transport) {
50  MA_LOGD(MA_TAG, "Transport not available");
51  return;
52  }
53 
54  size_t algorithm_id = 0;
55 
56  MA_STORAGE_GET_POD(static_resource->device->getStorage(), MA_STORAGE_KEY_ALGORITHM_ID, algorithm_id, 0);
57 
58  std::vector<std::string> args{"INIT@ALGORITHM", std::to_string(static_cast<int>(algorithm_id))};
59  configureAlgorithm(args, *transport, encoder, true);
60 }
61 
62 void getAlgorithmInfo(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
63  MA_ASSERT(argv.size() >= 1);
64  const auto& cmd = argv[0];
65 
66  ma_err_t ret = MA_OK;
67  encoder.begin(MA_MSG_TYPE_RESP, ret, cmd);
68  encoder.write("algorithm", static_cast<uint32_t>(static_resource->current_algorithm_id));
69  encoder.end();
70  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
71 }
72 
73 } // namespace ma::server::callback
#define MA_STORAGE_KEY_ALGORITHM_ID
Definition: algorithm.hpp:9
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.
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_ASSERT(expr)
Definition: ma_debug.h:119
#define MA_TAG
Definition: ma_debug.h:9
@ MA_MSG_TYPE_RESP
Definition: ma_types.h:252
ma_err_t
Definition: ma_types.h:21
@ MA_OK
Definition: ma_types.h:23
Definition: algorithm.hpp:11
void getAvailableAlgorithms(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder)
Definition: algorithm.hpp:13
void getAlgorithmInfo(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder)
Definition: algorithm.hpp:62
void configureAlgorithm(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder, bool called_by_event=false)
Definition: algorithm.hpp:19
void initDefaultAlgorithm(Encoder &encoder)
Definition: algorithm.hpp:43
#define static_resource
Definition: resource.hpp:64