1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15
16 #include <cstdint>
17
18 #include "pw_tokenizer/tokenize.h"
19
20 namespace pw::sensor {
21
22 #define PW_SENSOR_UNIT_TYPE(_unit_name, _domain, _name_str, _symbol_str) \
23 struct _unit_name { \
24 [[maybe_unused]] static constexpr uint16_t kUnitName = \
25 PW_TOKENIZE_STRING_MASK(_domain, 0xFFFF, _name_str); \
26 [[maybe_unused]] static constexpr uint16_t kUnitSymbol = \
27 PW_TOKENIZE_STRING_MASK(_domain, 0xFFFF, _symbol_str); \
28 [[maybe_unused]] static constexpr uint32_t kUnitType = \
29 (static_cast<uint32_t>(kUnitName) << 16) | kUnitSymbol; \
30 }
31
32 #define PW_SENSOR_MEASUREMENT_TYPE( \
33 _measurement_name, _domain, _name_str, _unit_name) \
34 struct _measurement_name { \
35 [[maybe_unused]] static constexpr uint32_t kMeasurementName = \
36 PW_TOKENIZE_STRING_DOMAIN(_domain, _name_str); \
37 [[maybe_unused]] static constexpr uint32_t kUnitType = \
38 _unit_name::kUnitType; \
39 [[maybe_unused]] static constexpr uint64_t kMeasurementType = \
40 (static_cast<uint64_t>(kMeasurementName) << 32) | kUnitType; \
41 }
42
GetMeasurementNameFromType(uint64_t measurement_type)43 constexpr inline uint32_t GetMeasurementNameFromType(
44 uint64_t measurement_type) {
45 return measurement_type >> 32;
46 }
47
GetMeasurementUnitFromType(uint64_t measurement_type)48 constexpr inline uint32_t GetMeasurementUnitFromType(
49 uint64_t measurement_type) {
50 return measurement_type & 0xffffffff;
51 }
52
GetMeasurementUnitNameFromType(uint64_t measurement_type)53 constexpr inline uint32_t GetMeasurementUnitNameFromType(
54 uint64_t measurement_type) {
55 return GetMeasurementUnitFromType(measurement_type) >> 16;
56 }
57
58 #define PW_SENSOR_ATTRIBUTE_TYPE(_attribute_name, _domain, _name_str) \
59 struct _attribute_name { \
60 [[maybe_unused]] static constexpr uint32_t kAttributeType = \
61 PW_TOKENIZE_STRING_DOMAIN(_domain, _name_str); \
62 }
63
64 #define PW_SENSOR_ATTRIBUTE_INSTANCE( \
65 _inst_name, _measurement_name, _attribute_name, _unit_name) \
66 struct _inst_name { \
67 [[maybe_unused]] static constexpr auto kMeasurementType = \
68 _measurement_name::kMeasurementType; \
69 [[maybe_unused]] static constexpr auto kAttributeType = \
70 _attribute_name::kAttributeType; \
71 [[maybe_unused]] static constexpr auto kUnitType = _unit_name::kUnitType; \
72 }
73
74 #define PW_SENSOR_TRIGGER_TYPE(var, _domain, _name) \
75 struct var { \
76 [[maybe_unused]] static constexpr uint32_t kTriggerType = \
77 PW_TOKENIZE_STRING_DOMAIN(_domain, _name); \
78 }
79
80 } // namespace pw::sensor
81