SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
wifi.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 "resource.hpp"
11 
12 bool isBssid(const std::string& str) {
13  if (str.length() != 17) return false; // BSSID length is 17, e.g. 00:11:22:33:44:55
14 
15  for (std::size_t i = 0; i < str.length(); ++i) {
16  if (i % 3 == 2) {
17  if (str[i] != ':' && str[i] != '-') return false; // BSSID delimiter is ':' or '-'
18  } else {
19  if (!std::isxdigit(str[i])) return false; // BSSID is hex string
20  }
21  }
22 
23  return true;
24 }
25 
26 #ifndef MA_HAS_NATTIVE_WIFI_SUPPORT
27  #define MA_HAS_NATTIVE_WIFI_SUPPORT 1
28 #endif
29 
30 #if MA_USE_EXTERNAL_WIFI_STATUS
31 extern ma::Mutex _net_sync_mutex;
32 extern in4_info_t _in4_info;
33 extern in6_info_t _in6_info;
34 extern volatile int _net_sta;
35 #else
36 static in4_info_t _in4_info{};
37 static in6_info_t _in6_info{};
38 #endif
39 
40 namespace ma::server::callback {
41 
42 static std::string _wifi_ver = "";
43 static int32_t _wifi_status = 0;
44 
45 #if !MA_HAS_NATTIVE_WIFI_SUPPORT
46 void setWifiVer(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
47  ma_err_t ret = MA_OK;
48 
49  if (argv.size() < 2) {
50  ret = MA_EINVAL;
51  goto exit;
52  }
53 
54  _wifi_ver = argv[1];
55 
56 exit:
57  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
58  encoder.write("ver", _wifi_ver);
59  encoder.end();
60  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
61 }
62 
63 void getWifiVer(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
64  ma_err_t ret = MA_OK;
65 
66  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
67  encoder.write("ver", _wifi_ver);
68  encoder.end();
69  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
70 }
71 #endif
72 
73 void configureWifi(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
74  ma_err_t ret = MA_OK;
75 
76  ma_wifi_config_t config{};
77  bool is_bssid = isBssid(argv[1]);
78 
79  if (argv.size() < 4) {
80  ret = MA_EINVAL;
81  goto exit;
82  }
83 
84  if (argv[3].length() < 8) {
85  ret = MA_EINVAL;
86  goto exit;
87  }
88 
89  if (is_bssid) {
90  std::strncpy(config.bssid, argv[1].c_str(), sizeof(config.bssid) - 1);
91  } else {
92  std::strncpy(config.ssid, argv[1].c_str(), sizeof(config.ssid) - 1);
93  }
94  std::strncpy(config.password, argv[3].c_str(), sizeof(config.password) - 1);
95  config.security = static_cast<ma_wifi_security_t>(std::atoi(argv[2].c_str()));
96 
97  MA_STORAGE_SET_ASTR(ret, static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_SSID, config.ssid);
98  MA_STORAGE_SET_ASTR(ret, static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_BSSID, config.bssid);
99  MA_STORAGE_SET_ASTR(ret, static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_PWD, config.password);
100  MA_STORAGE_SET_POD(ret, static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_SECURITY, config.security);
101 
102 exit:
103  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
104  encoder.write(config);
105  encoder.end();
106  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
107 }
108 
109 #if !MA_HAS_NATTIVE_WIFI_SUPPORT
110 void setWifiSta(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
111  ma_err_t ret = MA_OK;
112 
113  if (argv.size() < 2) {
114  ret = MA_EINVAL;
115  goto exit;
116  }
117 
118  _wifi_status = std::atoi(argv[1].c_str());
119 
120 exit:
121  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
122  encoder.write("status", _wifi_status);
123  encoder.end();
124  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
125 }
126 
127 void setWifiIn4Info(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
128  ma_err_t ret = MA_OK;
129 
130  if (argv.size() < 4) {
131  ret = MA_EINVAL;
132  goto exit;
133  }
134 
135  {
136  #if MA_USE_EXTERNAL_WIFI_STATUS
137  ma::Guard lock(_net_sync_mutex);
138  #endif
139  _in4_info.ip = ipv4_addr_t::from_str(argv[1]);
140  _in4_info.netmask = ipv4_addr_t::from_str(argv[2]);
141  _in4_info.gateway = ipv4_addr_t::from_str(argv[3]);
142  }
143 
144 exit:
145  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
146  {
147  #if MA_USE_EXTERNAL_WIFI_STATUS
148  ma::Guard lock(_net_sync_mutex);
149  #endif
150  encoder.write(_in4_info);
151  }
152  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
153 }
154 
155 void setWifiIn6Info(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
156  ma_err_t ret = MA_OK;
157 
158  if (argv.size() < 2) {
159  ret = MA_EINVAL;
160  goto exit;
161  }
162 
163  {
164  #if MA_USE_EXTERNAL_WIFI_STATUS
165  ma::Guard lock(_net_sync_mutex);
166  #endif
167  _in6_info.ip = ipv6_addr_t::from_str(argv[1]);
168  }
169 exit:
170  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
171  {
172  #if MA_USE_EXTERNAL_WIFI_STATUS
173  ma::Guard lock(_net_sync_mutex);
174  #endif
175  encoder.write(_in6_info);
176  }
177  encoder.end();
178  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
179 }
180 #endif
181 
182 void getWifiInfo(const std::vector<std::string>& argv, Transport& transport, Encoder& encoder) {
183  ma_err_t ret = MA_OK;
184 
185  ma_wifi_config_t config{};
186  MA_STORAGE_GET_ASTR(static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_SSID, config.ssid, "");
187  MA_STORAGE_GET_ASTR(static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_BSSID, config.bssid, "");
188  MA_STORAGE_GET_ASTR(static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_PWD, config.password, "");
189  MA_STORAGE_GET_POD(static_resource->device->getStorage(), MA_STORAGE_KEY_WIFI_SECURITY, config.security, 0);
190 
191  encoder.begin(MA_MSG_TYPE_RESP, ret, argv[0]);
192  {
193 #if MA_USE_EXTERNAL_WIFI_STATUS
194  ma::Guard lock(_net_sync_mutex);
195  _wifi_status = _net_sta > 2 ? 2 : _net_sta;
196 #endif
197  if (std::strlen(config.ssid) < 1) {
198  _wifi_status = 0;
199  }
200  encoder.write(_in4_info);
201  encoder.write(_in6_info);
202  }
203  encoder.write(config, (int*)&_wifi_status);
204  encoder.end();
205  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
206 }
207 
208 } // 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.
Definition: ma_osal.h:98
Definition: ma_osal.h:80
Definition: ma_transport.h:12
virtual size_t send(const char *data, size_t length) noexcept=0
#define MA_STORAGE_KEY_WIFI_PWD
Definition: ma_definations.h:27
#define MA_STORAGE_KEY_WIFI_SECURITY
Definition: ma_definations.h:28
#define MA_STORAGE_KEY_WIFI_SSID
Definition: ma_definations.h:25
#define MA_STORAGE_KEY_WIFI_BSSID
Definition: ma_definations.h:26
ma_wifi_security_t
Definition: ma_types.h:298
@ 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 getWifiInfo(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder)
Definition: wifi.hpp:182
void configureWifi(const std::vector< std::string > &argv, Transport &transport, Encoder &encoder)
Definition: wifi.hpp:73
#define static_resource
Definition: resource.hpp:64
Definition: ma_types.h:300
bool isBssid(const std::string &str)
Definition: wifi.hpp:12