Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DataTypes.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include <cstdint>
27#include <cinttypes>
28#include <cmath>
29
30#include <algorithm>
31
32#include <jau/basic_types.hpp>
33
35
36using namespace ieee11073;
37
38AbsoluteTime::AbsoluteTime(const uint8_t * data_le, const int size) {
39 if( nullptr == data_le || 0 == size ) {
40 return;
41 }
42 int i=0;
43 if( 2 > size ) return;
44 year = (int16_t) ( data_le[i+0] | data_le[i+1] << 8 );
45 i+=2;
46 if( 3 > size ) return;
47 month = static_cast<int8_t>(data_le[i++]);
48 if( 4 > size ) return;
49 day = static_cast<int8_t>(data_le[i++]);
50 if( 5 > size ) return;
51 hour = static_cast<int8_t>(data_le[i++]);
52 if( 6 > size ) return;
53 minute = static_cast<int8_t>(data_le[i++]);
54 if( 7 > size ) return;
55 second = static_cast<int8_t>(data_le[i++]);
56 if( 8 > size ) return;
57 second_fractions = static_cast<int8_t>(data_le[i++]);
58}
59
60std::string AbsoluteTime::toString() const {
61 char cbuf[24]; // '2020-04-04 10:58:59' 19 + second_fractions
62 const size_t count = snprintf(cbuf, sizeof(cbuf), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
63 (int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second);
64 if( count >= sizeof(cbuf) ) { // truncated?
65 throw jau::InternalError("snprintf date-format truncated "+std::to_string(count)+" >= "+std::to_string(sizeof(cbuf)), E_FILE_LINE);
66 }
67 std::string res(cbuf);
68 if( 0 != second_fractions ) {
69 res.append(".").append(std::to_string(second_fractions));
70 }
71 return res;
72}
73
74static const int32_t FIRST_RESERVED_VALUE = FloatTypes::ReservedFloatValues::MDER_POSITIVE_INFINITY;
75static const uint32_t FIRST_S_RESERVED_VALUE = FloatTypes::ReservedSFloatValues::MDER_S_POSITIVE_INFINITY;
76
77static const float reserved_float_values[5] = {INFINITY, NAN, NAN, NAN, -INFINITY};
78
79using namespace jau::int_literals;
80
81float FloatTypes::float16_IEEE11073_to_IEEE754(const uint16_t raw_bt_float16_le) {
82 uint16_t mantissa = raw_bt_float16_le & 0x0FFF;
83 int8_t exponent = (int8_t) ( ( raw_bt_float16_le >> 12 ) & 0xFF );
84
85 if( exponent >= 0x0008) {
86 exponent = (int8_t) ( - ( (0x000F + 1) - (int)exponent ) );
87 }
88
89 if( mantissa >= FIRST_S_RESERVED_VALUE &&
90 mantissa <= ReservedSFloatValues::MDER_S_NEGATIVE_INFINITY ) {
92 }
93 if ( mantissa >= 0x0800 ) {
94 mantissa = - ( ( 0x0FFF + 1 ) - mantissa );
95 }
96 return (float)mantissa * ::powf(10.0f, (float)exponent);
97}
98
99float FloatTypes::float32_IEEE11073_to_IEEE754(const uint32_t raw_bt_float32_le) {
100 int32_t mantissa = (int32_t) ( raw_bt_float32_le & 0xFFFFFF );
101 int8_t exponent = (int8_t) ( ( raw_bt_float32_le >> 24 ) & 0xFF );
102
103 if( mantissa >= FIRST_RESERVED_VALUE &&
104 mantissa <= ReservedFloatValues::MDER_NEGATIVE_INFINITY ) {
106 }
107 if( mantissa >= 0x800000 ) {
108 mantissa = - ( ( 0xFFFFFF + 1 ) - mantissa );
109 }
110 return (float)mantissa * ::powf(10.0f, (float)exponent);
111}
static const float reserved_float_values[5]
Definition: DataTypes.cpp:77
static const uint32_t FIRST_S_RESERVED_VALUE
Definition: DataTypes.cpp:75
static const int32_t FIRST_RESERVED_VALUE
Definition: DataTypes.cpp:74
#define E_FILE_LINE
std::string toString() const
Definition: DataTypes.cpp:60
static float float16_IEEE11073_to_IEEE754(const uint16_t raw_bt_float16_le)
Converts a 'IEEE-11073 16-bit SFLOAT' to std IEEE754 float.
Definition: DataTypes.cpp:81
static float float32_IEEE11073_to_IEEE754(const uint32_t raw_bt_float32_le)
Converts a 'IEEE-11073 32-bit FLOAT' to std IEEE754 float.
Definition: DataTypes.cpp:99
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
IEEE11073 Data Types.
Definition: DataTypes.hpp:47