Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Utils.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <ble/cs_Nordic.h> // required for __get_IPSR()
10#include <logging/cs_Logger.h>
11
12#include <string>
13
16
21namespace CsUtils {
22
30inline uint16_t convertEndian16(uint16_t val) {
31 return ((val >> 8) & 0xFF) | ((val & 0xFF) << 8);
32}
33
41inline uint32_t convertEndian32(uint32_t val) {
42 return ((val >> 24) & 0xFF) | ((val >> 8) & 0xFF00) | ((val & 0xFF00) << 8) | ((val & 0xFF) << 24);
43}
44
50#define CS_ROUND_UP_TO_MULTIPLE_OF(num, multiple) (((num + multiple - 1) / multiple) * multiple)
56#define CS_ROUND_UP_TO_MULTIPLE_OF_POWER_OF_2(num, multiple) ((num + multiple - 1) & -multiple)
57
63template <class T, size_t N>
64constexpr auto ArraySize(T (&)[N]) {
65 return N;
66}
67
68template <typename T>
69void printAddress(T* arr, uint16_t len, uint8_t verbosity = SERIAL_DEBUG, bool addNewLine = true) {
70 _logArray(verbosity, addNewLine, arr, len, "", "", "%02X", ":", true);
71}
72
73inline void print_heap([[maybe_unused]] const std::string& msg) {
74 uint8_t* p = (uint8_t*)malloc(128);
75 LOGd("%s %p", msg.c_str(), p);
76 free(p);
77}
78
79inline void print_stack([[maybe_unused]] const std::string& msg) {
80 void* sp;
81 asm("mov %0, sp" : "=r"(sp) : :);
82 LOGd("%s %p", msg.c_str(), (uint8_t*)sp);
83}
84
85template <typename T>
86inline bool isBitSet(const T value, uint8_t bit) {
87 return value & (1 << bit);
88}
89
90template <typename T>
91inline bool setBit(T& value, uint8_t bit) {
92 return value |= (1 << bit);
93}
94
95template <typename T>
96inline bool clearBit(T& value, uint8_t bit) {
97 return value &= ~(1 << bit);
98}
99
111template <typename T>
112inline constexpr T lowestBitSet(T value) {
113 // TODO: there is a faster implementation for this.
114 T numBits = sizeof(T) * 8;
115 for (T i = 0; i < numBits; i++) {
116 if (value & 1) {
117 return i;
118 }
119 value >>= 1;
120 }
121 return numBits;
122}
123
127inline bool isNewer(uint8_t previousValue, uint8_t newValue) {
128 int8_t diff = newValue - previousValue;
129 return diff > 0;
130}
131
145inline static cs_ret_code_t findAdvType(uint8_t type, uint8_t* advData, uint8_t advLen, cs_data_t* foundData) {
146 int index = 0;
147 foundData->data = nullptr;
148 foundData->len = 0;
149 while (index < advLen - 1) {
150 uint8_t fieldLen = advData[index];
151 uint8_t fieldType = advData[index + 1];
152 // Check if length is not 0 or larger than remaining advertisement data.
153 if (fieldLen == 0 || index + 1 + fieldLen > advLen) {
154 return ERR_NOT_FOUND;
155 }
156
157 if (fieldType == type) {
158 foundData->data = &advData[index + 2];
159 foundData->len = fieldLen - 1;
160 return ERR_SUCCESS;
161 }
162 index += fieldLen + 1;
163 }
164 return ERR_NOT_FOUND;
165}
166
175constexpr uint16_t stringLen(const char* str, uint16_t maxSize) {
176 uint16_t size = 0;
177 while ((str[size] != '\0') && (size < maxSize)) {
178 ++size;
179 }
180 return size;
181}
182
183inline uint32_t getInterruptLevel() {
184 // return __get_IPSR() & 0x1FF;
185 return __get_IPSR();
186 // 0 = Thread mode
187 // 1 = Reserved
188 // 2 = NMI
189 // 3 = HardFault
190 // 4 = MemManage
191 // 5 = BusFault
192 // 6 = UsageFault
193 // 7-10 = Reserved
194 // 11 = SVCall
195 // 12 = Reserved for Debug
196 // 13 = Reserved
197 // 14 = PendSV
198 // 15 = SysTick
199 // 16 = IRQ0.
200 // 17 = IRQ1.
201 // 18 = IRQ2.
202 // ..
203 // n+16 = IRQn
204}
205
206} // namespace CsUtils
@ ERR_NOT_FOUND
Definition: cs_ErrorCodes.h:24
@ ERR_SUCCESS
Definition: cs_ErrorCodes.h:10
#define _logArray(level, addNewLine, pointer,...)
Definition: cs_LogBinaryProtocol.h:128
#define LOGd(fmt,...)
Definition: cs_Logger.h:90
#define SERIAL_DEBUG
Definition: cs_SerialTypes.h:24
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 29,...
uint16_t convertEndian16(uint16_t val)
Convert a short (uint16_t) from LSB (little-endian) to MSB (big-endian) and vice versa.
Definition: cs_Utils.h:30
bool isBitSet(const T value, uint8_t bit)
Definition: cs_Utils.h:86
bool isNewer(uint8_t previousValue, uint8_t newValue)
Returns true when newValue is newer than previousValue, for a value that is increased all the time an...
Definition: cs_Utils.h:127
void print_stack(const std::string &msg)
Definition: cs_Utils.h:79
constexpr uint16_t stringLen(const char *str, uint16_t maxSize)
Gets the string length of a null terminated constant string.
Definition: cs_Utils.h:175
void printAddress(T *arr, uint16_t len, uint8_t verbosity=SERIAL_DEBUG, bool addNewLine=true)
Definition: cs_Utils.h:69
uint32_t getInterruptLevel()
Definition: cs_Utils.h:183
constexpr T lowestBitSet(T value)
Returns the index of the lowest bit set in given value.
Definition: cs_Utils.h:112
bool setBit(T &value, uint8_t bit)
Definition: cs_Utils.h:91
void print_heap(const std::string &msg)
Definition: cs_Utils.h:73
bool clearBit(T &value, uint8_t bit)
Definition: cs_Utils.h:96
uint32_t convertEndian32(uint32_t val)
Convert an integer (uint32_t) from LSB (little-endian) to MSB (big-endian) and vice versa.
Definition: cs_Utils.h:41
static cs_ret_code_t findAdvType(uint8_t type, uint8_t *advData, uint8_t advLen, cs_data_t *foundData)
Parses advertisement data, providing length and location of the field in case matching data is found.
Definition: cs_Utils.h:145
constexpr auto ArraySize(T(&)[N])
Get number of items in an array.
Definition: cs_Utils.h:64
Packets (structs) that are used internally in the firmware, and can be changed freely.
Definition: cs_PacketsInternal.h:27
cs_buffer_size_t len
< Pointer to data.
Definition: cs_PacketsInternal.h:29
buffer_ptr_t data
Definition: cs_PacketsInternal.h:28