herald  2.0.0
simple_payload_data_supplier.h
1 // Copyright 2021 Herald Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #ifndef HERALD_SIMPLE_PAYLOAD_DATA_SUPPLIER_H
6 #define HERALD_SIMPLE_PAYLOAD_DATA_SUPPLIER_H
7 
8 #include "secret_key.h"
9 #include "k.h"
10 #include "../payload_data_supplier.h"
11 #include "../extended/extended_data.h"
12 #include "../../context.h"
13 #include "../../datatype/payload_timestamp.h"
14 #include "../../data/sensor_logger.h"
15 
16 #include <optional>
17 #include <cstdint>
18 
19 namespace herald {
20 namespace payload {
21 namespace simple {
22 
23 using namespace herald::datatype;
24 using namespace herald::payload::extended;
25 
26 using MYUINT32 = unsigned long;
27 
29 public:
30  SimplePayloadDataSupplier() = default;
31  virtual ~SimplePayloadDataSupplier() = default;
32 };
33 
34 template <typename ContextT>
36 public:
37  ConcreteSimplePayloadDataSupplierV1(ContextT& context, std::uint16_t countryCode, std::uint16_t stateCode,
38  SecretKey sk, K k)
40  ctx(context), country(countryCode), state(stateCode), secretKey(sk), k(k),
41  commonPayloadHeader(), extended()
42  HLOGGERINIT(ctx, "Sensor", "ConcreteSimplePayloadDataSupplierV1")
43  {
44  commonPayloadHeader.append(std::uint8_t(0x10)); // Simple payload V1
45  commonPayloadHeader.append(country);
46  commonPayloadHeader.append(state);
47 
48  HTDBG("About to call matching keys");
49  // matchingKeys = k.matchingKeys(sk);
50  HTDBG("Completed matching keys call");
51  }
52  ConcreteSimplePayloadDataSupplierV1(ContextT& context, std::uint16_t countryCode, std::uint16_t stateCode,
55  ctx(context), country(countryCode), state(stateCode), secretKey(sk), k(k),
56  commonPayloadHeader(), extended(ext)
57  HLOGGERINIT(ctx, "Sensor", "ConcreteSimplePayloadDataSupplierV1")
58  {
59  commonPayloadHeader.append(std::uint8_t(0x10)); // Simple payload V1
60  commonPayloadHeader.append(country);
61  commonPayloadHeader.append(state);
62  }
63  ConcreteSimplePayloadDataSupplierV1(const ConcreteSimplePayloadDataSupplierV1& from) = delete; // copy ctor deletion
64  ConcreteSimplePayloadDataSupplierV1(ConcreteSimplePayloadDataSupplierV1&& from) = delete; // move ctor deletion
66 
67  PayloadData legacyPayload(const PayloadTimestamp timestamp, const Device& device) {
68  return PayloadData();
69  }
70 
71  PayloadData payload(const PayloadTimestamp timestamp, const Device& device) {
72  const int day = k.day(timestamp.value);
73  const int period = k.period(timestamp.value);
74 
75  auto cid = k.contactIdentifier(secretKey,day,period);
76 
77  PayloadData p(commonPayloadHeader);
78  // length
79  if (extended.hasData()) {
80  p.append(std::uint16_t(2 + extended.payload().size()));
81  } else {
82  p.append(std::uint16_t(2));
83  }
84  // contact id
85  p.append(cid);
86  // extended data
87  if (extended.hasData()) {
88  p.append(extended.payload());
89  }
90 
91  return p;
92  }
93 
94  PayloadData payload(const PayloadTimestamp timestamp) {
95  const int day = k.day(timestamp.value);
96  const int period = k.period(timestamp.value);
97 
98  auto cid = k.contactIdentifier(secretKey,day,period);
99 
100  PayloadData p(commonPayloadHeader);
101  // length
102  if (extended.hasData()) {
103  p.append(std::uint16_t(2 + extended.payload().size()));
104  } else {
105  p.append(std::uint16_t(2));
106  }
107  // contact id
108  p.append(cid);
109  // extended data
110  if (extended.hasData()) {
111  p.append(extended.payload());
112  }
113 
114  return p;
115  }
116 
117  // std::vector<PayloadData> payload(const Data& data) {
118  // return std::vector<PayloadData>();
119  // }
120 
121 private:
122  ContextT& ctx;
123  const uint16_t country;
124  const uint16_t state;
125  const SecretKey secretKey;
126  K k;
127 
128  PayloadData commonPayloadHeader;
129 
130  ConcreteExtendedDataV1 extended;
131 
132  HLOGGER(ContextT);
133 };
134 
135 }
136 }
137 }
138 
139 #endif
Generic abstraction of a particular local proximate device type.
Definition: device.h:27
The main data workhorse class of the Herald API.
Definition: data.h:33
void append(const DataRef &rawData, std::size_t offset, std::size_t length)
Copies another DataRef into this instance, expanding if required.
Definition: data.h:201
Definition: payload_data.h:13
Definition: simple_payload_data_supplier.h:35
Definition: k.h:21
Definition: simple_payload_data_supplier.h:28
Contains all low-level Herald datatype implementations.
Definition: base64_string.h:14
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15
Definition: payload_timestamp.h:13