ifm3d
o3r.h
1 /*
2  * Copyright 2021-present ifm electronic, gmbh
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef IFM3D_DEVICE_O3R_H
7 #define IFM3D_DEVICE_O3R_H
8 
9 #include <ifm3d/common/features.h>
10 #include <ifm3d/device/device.h>
11 #include <sstream>
12 #ifdef BUILD_MODULE_CRYPTO
13 # include <ifm3d/crypto/crypto.h>
14 #endif
15 #include <ifm3d/device/pcic_command.h>
16 
17 namespace ifm3d
18 {
19  static const int NET_WAIT_O3R_SET =
20  std::getenv("IFM3D_NET_WAIT_O3R_SET") == nullptr ?
21  15000 :
22  std::stoi(std::getenv("IFM3D_NET_WAIT_O3R_SET"));
23 
25  struct IFM3D_EXPORT PortInfo
26  {
27  std::string port;
28  uint16_t pcic_port;
29  std::string type;
30  };
31 
32 #ifdef BUILD_MODULE_CRYPTO
33  class O3RSealedBox;
34 #endif
35 
40  class IFM3D_EXPORT O3R : public Device
41  {
42  public:
43  using Ptr = std::shared_ptr<O3R>;
44  O3R(const std::string& ip = ifm3d::DEFAULT_IP,
45  std::uint16_t xmlrpc_port = ifm3d::DEFAULT_XMLRPC_PORT);
46 
47  ~O3R() override;
48  O3R(O3R&&) = delete;
49  O3R& operator=(O3R&&) = delete;
50  O3R(O3R&) = delete;
51  O3R& operator=(O3R&) = delete;
52 
60  virtual void FactoryReset(bool keep_network_settings);
61 
67  json GetSchema();
68 
78  json Get(
79  const std::vector<std::string>& path = std::vector<std::string>());
80 
89  json ResolveConfig(const json::json_pointer& ptr);
90 
98  void Set(const json& j);
99 
109  void Remove(const std::string& json_pointer);
110 
120  void Reset(const std::string& json_pointer);
121 
127  json GetInit();
128 
138  void SaveInit(const std::vector<std::string>& pointers = {});
139 
147  std::string GetInitStatus();
148 
158  void Lock(const std::string& password);
159 
167  void Unlock(const std::string& password);
168 
175  std::vector<PortInfo> Ports();
176 
184  PortInfo Port(const std::string& port);
185 
191  json GetDiagnostic();
192 
199  json GetDiagnosticFilterSchema();
200 
208  json GetDiagnosticFiltered(const json& filter);
209 
210  void Reboot(
211  const BootMode& mode = ifm3d::Device::BootMode::PRODUCTIVE) override;
212 
216  void RebootToRecovery();
217 
218  DeviceFamily WhoAmI() override;
219  ifm3d::Device::SWUVersion SwUpdateVersion() override;
220 
225  json ToJSON() override;
226 
231  void FromJSON(const json& j) override;
232 
233  void DownloadServiceReport(const std::string& out_file);
234 
235 #ifdef BUILD_MODULE_CRYPTO
236 
240  std::shared_ptr<O3RSealedBox> SealedBox();
241 #endif
242 
244  {
245  enum Parameter : uint16_t
246  {
247  ODS_OVERHANGING_LOAD = 2003,
248  ODS_ZONE_SET = 2101,
249  ODS_MAXIMUM_HEIGHT = 2102,
250  ODS_MOTION_DATA = 2103,
251  PDS_GET_PALLET = 2200,
252  PDS_GET_ITEM = 2201,
253  PDS_GET_RACK = 2202,
254  PDS_VOL_CHECK = 2203,
255  };
256 
257  Parameter parameter;
258  std::vector<std::uint8_t> data;
259 
261  Parameter param,
262  std::vector<std::uint8_t> parameter_data)
263  : parameter(param),
264  data(std::move(parameter_data))
265  {}
266 
267  std::vector<std::uint8_t>
268  SerializeData() const override
269  {
270  std::vector<std::uint8_t> payload;
271 
272  // 'f' command prefix
273  payload.push_back(static_cast<uint8_t>('f'));
274 
275  // parameter ID as 5-digit ASCII string padded with zeros
276  std::ostringstream param;
277  param << std::setw(5) << std::setfill('0')
278  << static_cast<uint16_t>(parameter);
279  const std::string paramStr = param.str(); // e.g., "02103"
280  payload.insert(payload.end(), paramStr.begin(), paramStr.end());
281 
282  // reserved string "#00000" as bytes
283  const std::string reserved = "#00000";
284  payload.insert(payload.end(), reserved.begin(), reserved.end());
285 
286  // append user data bytes
287  payload.insert(payload.end(), data.begin(), data.end());
288 
289  return payload;
290  }
291  };
292 
293  private:
294  class Impl;
295  std::shared_ptr<Impl> _impl;
296 
297 #ifdef BUILD_MODULE_CRYPTO
298  friend class O3RSealedBox;
299 #endif
300  }; // end: class O3R
301 
302 #ifdef BUILD_MODULE_CRYPTO
303 
310  class IFM3D_EXPORT O3RSealedBox
311  {
312  public:
313  using Ptr = std::shared_ptr<O3RSealedBox>;
314 
315  O3RSealedBox(const O3RSealedBox&) = default;
316  O3RSealedBox(O3RSealedBox&&) = delete;
317  O3RSealedBox& operator=(const O3RSealedBox&) = default;
318  O3RSealedBox& operator=(O3RSealedBox&&) = delete;
319  O3RSealedBox(std::shared_ptr<O3R::Impl> p_impl);
320  ~O3RSealedBox();
321 
329  void SetPassword(const std::string& new_password,
330  std::optional<std::string> old_password = std::nullopt);
331 
337  bool IsPasswordProtected();
338 
344  void RemovePassword(std::string password);
345 
353  void Set(const std::string& password, const json& configuration);
354 
361  std::vector<uint8_t> GetPublicKey();
362 
363  private:
364  std::shared_ptr<O3R::Impl> _impl;
365  };
366 #endif
367 }
368 
369 #endif // IFM3D_DEVICE_O3R_H
ifm3d::O3R::SetTemporaryApplicationParameter::SerializeData
std::vector< std::uint8_t > SerializeData() const override
Serialize the command into a sequence of bytes for PCIC transmission.
Definition: o3r.h:268
ifm3d::Device::BootMode
BootMode
Device boot up modes:
Definition: device.h:143
ifm3d::json
Definition: json.hpp:130
ifm3d::SealedBox
Definition: crypto.h:17
ifm3d::Device
Definition: device.h:132
ifm3d::O3R::SetTemporaryApplicationParameter
Definition: o3r.h:243
ifm3d::PortInfo
Definition: o3r.h:25
ifm3d::O3R
Definition: o3r.h:40
ifm3d::PCICCommand
Definition: pcic_command.h:10