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 #include <initializer_list>
17 #include <variant>
18 
19 namespace ifm3d
20 {
21  static const int NET_WAIT_O3R_SET =
22  std::getenv("IFM3D_NET_WAIT_O3R_SET") == nullptr ?
23  15000 :
24  std::stoi(std::getenv("IFM3D_NET_WAIT_O3R_SET"));
25 
27  struct IFM3D_EXPORT PortInfo
28  {
29  std::string port;
30  uint16_t pcic_port;
31  std::string type;
32  };
33 
34 #ifdef BUILD_MODULE_CRYPTO
35  class O3RSealedBox;
36 #endif
37 
42  class IFM3D_EXPORT O3R : public Device
43  {
44  public:
45  using Ptr = std::shared_ptr<O3R>;
46  O3R(const std::string& ip = ifm3d::DEFAULT_IP,
47  std::uint16_t xmlrpc_port = ifm3d::DEFAULT_XMLRPC_PORT);
48 
49  ~O3R() override;
50  O3R(O3R&&) = delete;
51  O3R& operator=(O3R&&) = delete;
52  O3R(O3R&) = delete;
53  O3R& operator=(O3R&) = delete;
54 
62  virtual void FactoryReset(bool keep_network_settings);
63 
70 
84  std::variant<std::monostate, std::string, std::vector<std::string>>
85  pointers);
86 
99  json GetSchema(std::initializer_list<std::string> pointers);
100 
111  const std::vector<std::string>& path = std::vector<std::string>());
112 
121  json ResolveConfig(const json::json_pointer& ptr);
122 
130  void Set(const json& j);
131 
141  void Remove(const std::string& json_pointer);
142 
152  void Reset(const std::string& json_pointer);
153 
160 
170  void SaveInit(const std::vector<std::string>& pointers = {});
171 
179  std::string GetInitStatus();
180 
190  void Lock(const std::string& password);
191 
199  void Unlock(const std::string& password);
200 
207  std::vector<PortInfo> Ports();
208 
216  PortInfo Port(const std::string& port);
217 
224 
232 
241 
242  void Reboot(
243  const BootMode& mode = ifm3d::Device::BootMode::PRODUCTIVE) override;
244 
249 
250  DeviceFamily WhoAmI() override;
251  ifm3d::Device::SWUVersion SwUpdateVersion() override;
252 
257  json ToJSON() override;
258 
263  void FromJSON(const json& j) override;
264 
265  void DownloadServiceReport(const std::string& out_file);
266 
267 #ifdef BUILD_MODULE_CRYPTO
272  std::shared_ptr<O3RSealedBox> SealedBox();
273 #endif
274 
276  {
277  enum Parameter : uint16_t
278  {
279  ODS_OVERHANGING_LOAD = 2003,
280  ODS_ZONE_SET = 2101,
281  ODS_MAXIMUM_HEIGHT = 2102,
282  ODS_MOTION_DATA = 2103,
283  PDS_GET_PALLET = 2200,
284  PDS_GET_ITEM = 2201,
285  PDS_GET_RACK = 2202,
286  PDS_VOL_CHECK = 2203,
287  };
288 
289  Parameter parameter;
290  std::vector<std::uint8_t> data;
291 
293  Parameter param,
294  std::vector<std::uint8_t> parameter_data)
295  : parameter(param),
296  data(std::move(parameter_data))
297  {}
298 
299  std::vector<std::uint8_t>
300  SerializeData() const override
301  {
302  std::vector<std::uint8_t> payload;
303 
304  // 'f' command prefix
305  payload.push_back(static_cast<uint8_t>('f'));
306 
307  // parameter ID as 5-digit ASCII string padded with zeros
308  std::ostringstream param;
309  param << std::setw(5) << std::setfill('0')
310  << static_cast<uint16_t>(parameter);
311  const std::string paramStr = param.str(); // e.g., "02103"
312  payload.insert(payload.end(), paramStr.begin(), paramStr.end());
313 
314  // reserved string "#00000" as bytes
315  const std::string reserved = "#00000";
316  payload.insert(payload.end(), reserved.begin(), reserved.end());
317 
318  // append user data bytes
319  payload.insert(payload.end(), data.begin(), data.end());
320 
321  return payload;
322  }
323  };
324 
325  private:
326  class Impl;
327  std::shared_ptr<Impl> _impl;
328 
329 #ifdef BUILD_MODULE_CRYPTO
330  friend class O3RSealedBox;
331 #endif
332  }; // end: class O3R
333 
334 #ifdef BUILD_MODULE_CRYPTO
342  class IFM3D_EXPORT O3RSealedBox
343  {
344  public:
345  using Ptr = std::shared_ptr<O3RSealedBox>;
346 
347  O3RSealedBox(const O3RSealedBox&) = default;
348  O3RSealedBox(O3RSealedBox&&) = delete;
349  O3RSealedBox& operator=(const O3RSealedBox&) = default;
350  O3RSealedBox& operator=(O3RSealedBox&&) = delete;
351  O3RSealedBox(std::shared_ptr<O3R::Impl> p_impl);
352  ~O3RSealedBox();
353 
361  void SetPassword(const std::string& new_password,
362  std::optional<std::string> old_password = std::nullopt);
363 
369  bool IsPasswordProtected();
370 
376  void RemovePassword(std::string password);
377 
385  void Set(const std::string& password, const json& configuration);
386 
393  std::vector<uint8_t> GetPublicKey();
394 
395  private:
396  std::shared_ptr<O3R::Impl> _impl;
397  };
398 #endif
399 }
400 
401 #endif // IFM3D_DEVICE_O3R_H
Software interface to an ifm 3D device.
Definition: device.h:134
BootMode
Device boot up modes:
Definition: device.h:145
Device specialization for O3R.
Definition: o3r.h:43
void Reset(const std::string &json_pointer)
Sets the default value of an object inside the JSON.
void Set(const json &j)
Overwrites parts of the temporary JSON configuration which is achieved by merging the provided JSON f...
json GetInit()
Return the initial JSON configuration.
json GetDiagnosticFilterSchema()
Returns the JSON schema for the filter expression provided to the getFiltered() method.
json GetSchema()
Return the current JSON schema configuration.
json GetSchema(std::initializer_list< std::string > pointers)
Returns the current JSON schema configuration or a subset of it.
void SaveInit(const std::vector< std::string > &pointers={})
Save to current temporary JSON configuration as initial JSON configuration, so it will be applied wit...
json ResolveConfig(const json::json_pointer &ptr)
Returns a part of the configuration formatted as JSON based on a JSON pointer.
void Remove(const std::string &json_pointer)
Removes an object from the JSON.
json Get(const std::vector< std::string > &path=std::vector< std::string >())
Returns the configuration formatted as JSON based on a path.
void Reboot(const BootMode &mode=ifm3d::Device::BootMode::PRODUCTIVE) override
Reboot the device.
void RebootToRecovery()
Reboot the device into Recovery Mode.
virtual void FactoryReset(bool keep_network_settings)
Sets the device configuration back to the state in which it shipped from the ifm factory.
json ToJSON() override
Serializes the state of the device to JSON.
DeviceFamily WhoAmI() override
This function can be used to retrieve the family of the connected device.
void FromJSON(const json &j) override
Configures the device based on the parameter values of the passed in JSON.
std::vector< PortInfo > Ports()
Returns a list containing information about all connected physical and application ports.
ifm3d::Device::SWUVersion SwUpdateVersion() override
Checks the swupdater version supported by device.
json GetDiagnosticFiltered(const json &filter)
Returns the content of the diagnostic memory formatted in JSON and filtered according to the JSON fil...
PortInfo Port(const std::string &port)
Returns information about a given physical or application port.
json GetDiagnostic()
Returns the content of the diagnostic memory formatted in JSON.
json GetSchema(std::variant< std::monostate, std::string, std::vector< std::string >> pointers)
Returns the current JSON schema configuration or a subset of it.
Definition: pcic_command.h:11
Definition: crypto.h:18
Definition: json.hpp:131
std::vector< std::uint8_t > SerializeData() const override
Serialize the command into a sequence of bytes for PCIC transmission.
Definition: o3r.h:300
Definition: o3r.h:28