Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_WireFormat.h
Go to the documentation of this file.
1
8#pragma once
9
10// #include <behaviour/cs_Behaviour.h>
11// #include <behaviour/cs_SwitchBehaviour.h>
12// #include <behaviour/cs_TwilightBehaviour.h>
13
14#include <logging/cs_Logger.h>
17#include <time/cs_TimeOfDay.h>
18
19#include <algorithm>
20#include <cstddef>
21#include <cstdint>
22#include <typeinfo>
23
24#define LOGWireFormat(...) LOGnone(__VA_ARGS__)
25
26class Behaviour;
27class SwitchBehaviour;
30
31namespace WireFormat {
32
33// a uniform serialization mechanism for both object and fundamental
34// types. For each serializable class simply implement a member method called
35// serialize that takes no arguments, and create an in class typedef
36// that is equal to the return type of this method.
37template <class T>
38typename T::SerializedDataType serialize(T& obj) {
39 LOGWireFormat("serialize %s", typeid(T).name());
40 return obj.serialize();
41}
42
43// returns the bytecount of the array to which a type is serialized by WireFormat.
44// (specialized to return sizeof(..) for some fundamental types to gain a uniform interface)
45template <class T>
46constexpr size_t size(T* = nullptr) {
47 return std::tuple_size<typename T::SerializedDataType>::value;
48}
49
59template <class T>
60T deserialize(uint8_t* data, size_t len) {
61 typename T::SerializedDataType d;
62 if (len >= size<T>()) {
63 std::copy_n(data, size<T>(), d.begin());
64 }
65 return T(d);
66}
67
68// ========== Specializations for deserialize =========
69
70template <>
71uint8_t WireFormat::deserialize(uint8_t* data, size_t len);
72
73template <>
74uint32_t WireFormat::deserialize(uint8_t* data, size_t len);
75
76template <>
77int32_t WireFormat::deserialize(uint8_t* data, size_t len);
78
79template <>
80uint64_t WireFormat::deserialize(uint8_t* data, size_t len);
81
82// template<>
83// TimeOfDay WireFormat::deserialize(uint8_t* data, size_t len);
84
85// template<>
86// PresencePredicate WireFormat::deserialize(uint8_t* data, size_t len);
87
88// template<>
89// PresenceCondition WireFormat::deserialize(uint8_t* data, size_t len);
90
91// template<>
92// Behaviour WireFormat::deserialize(uint8_t* data, size_t len);
93
94// template<>
95// SwitchBehaviour WireFormat::deserialize(uint8_t* data, size_t len);
96
97// template<>
98// TwilightBehaviour WireFormat::deserialize(uint8_t* data, size_t len);
99
100// ========== Specializations/overloads for serialize =========
101
102std::array<uint8_t, 1> serialize(const uint8_t& obj);
103std::array<uint8_t, 4> serialize(const uint32_t& obj);
104std::array<uint8_t, 4> serialize(const int32_t& obj);
105std::array<uint8_t, 8> serialize(const uint64_t& obj);
106
107template <>
108constexpr size_t size<uint8_t>(uint8_t*) {
109 return sizeof(uint8_t);
110}
111template <>
112constexpr size_t size<int32_t>(int32_t*) {
113 return sizeof(int32_t);
114}
115template <>
116constexpr size_t size<uint32_t>(uint32_t*) {
117 return sizeof(uint32_t);
118}
119template <>
120constexpr size_t size<uint64_t>(uint64_t*) {
121 return sizeof(uint64_t);
122}
123
124template <>
125constexpr size_t size<const uint8_t>(const uint8_t*) {
126 return sizeof(uint8_t);
127}
128template <>
129constexpr size_t size<const int32_t>(const int32_t*) {
130 return sizeof(int32_t);
131}
132template <>
133constexpr size_t size<const uint32_t>(const uint32_t*) {
134 return sizeof(uint32_t);
135}
136template <>
137constexpr size_t size<const uint64_t>(const uint64_t*) {
138 return sizeof(uint64_t);
139}
140
141} // namespace WireFormat
Class to derrive behaviours from, centralizing common variables such as from and until times.
Definition: cs_Behaviour.h:20
A smart timer behaviour is a switch behaviour that is allowed to extend itself passed the until time ...
Definition: cs_ExtendedSwitchBehaviour.h:20
Object that defines when a state transition should occur.
Definition: cs_SwitchBehaviour.h:26
Definition: cs_TwilightBehaviour.h:26
#define LOGWireFormat(...)
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 24,...
Definition: cs_WireFormat.h:24
Definition: cs_WireFormat.h:31
T::SerializedDataType serialize(T &obj)
Definition: cs_WireFormat.h:38
T deserialize(uint8_t *data, size_t len)
data will be copied as few times as possible, but the constructed object is not emplaced over the [da...
Definition: cs_WireFormat.h:60
constexpr size_t size< uint64_t >(uint64_t *)
Definition: cs_WireFormat.h:120
constexpr size_t size< const uint8_t >(const uint8_t *)
Definition: cs_WireFormat.h:125
constexpr size_t size< uint32_t >(uint32_t *)
Definition: cs_WireFormat.h:116
constexpr size_t size< int32_t >(int32_t *)
Definition: cs_WireFormat.h:112
constexpr size_t size< const int32_t >(const int32_t *)
Definition: cs_WireFormat.h:129
constexpr size_t size(T *=nullptr)
Definition: cs_WireFormat.h:46
constexpr size_t size< const uint64_t >(const uint64_t *)
Definition: cs_WireFormat.h:137
constexpr size_t size< const uint32_t >(const uint32_t *)
Definition: cs_WireFormat.h:133
constexpr size_t size< uint8_t >(uint8_t *)
Definition: cs_WireFormat.h:108