aboutsummaryrefslogtreecommitdiffstats
path: root/api/direct_bt/DBTEnv.hpp
blob: c87a0e8c122d3a4eae701f39572066aecf1abf92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef DBT_ENV_HPP_
#define DBT_ENV_HPP_

#include <cstdint>
#include <cinttypes>
#include <cstring>
#include <string>
#include <cstdio>

extern "C" {
    #include <errno.h>
}

#include "BasicTypes.hpp"

namespace direct_bt {

    class DBTEnv {
        private:
            DBTEnv();

        public:
            /**
             * Module startup time t0 in monotonic time in milliseconds.
             */
            static const uint64_t startupTimeMilliseconds;

            /**
             * Returns current elapsed monotonic time in milliseconds since module startup, see {@link #startupTimeMilliseconds}.
             */
            static uint64_t getElapsedMillisecond() {
                return getCurrentMilliseconds() - startupTimeMilliseconds;
            }

            /**
             * Returns the value of the environment's variable 'name'.
             * <p>
             * If there is no environment variable 'name',
             * implementation will try the potentially Java JVM imported 'jvm_name'.
             * </p>
             * <p>
             * Note that all Java JVM system properties are passed to the environment
             * and all property names have replaced dots '.' to underscore '_'
             * and 'jvm_' prepended.
             * </p>
             */
            static std::string getProperty(const std::string & name);

            /**
             * Returns the value of the environment's variable 'name',
             * or the 'default_value' if the environment variable's value is null.
             * <p>
             * Implementation uses getProperty(const char *name).
             * </p>
             */
            static std::string getProperty(const std::string & name, const std::string & default_value);

            /**
             * Returns the boolean value of the environment's variable 'name',
             * or the 'default_value' if the environment variable's value is null.
             * <p>
             * If the environment variable is set (value != null),
             * true is determined if the value equals 'true'.
             * </p>
             * <p>
             * Implementation uses getProperty(const char *name).
             * </p>
             */
            static bool getBooleanProperty(const std::string & name, const bool default_value);

            /**
             * Returns the int32_t value of the environment's variable 'name',
             * or the 'default_value' if the environment variable's value is null
             * or not within int32_t value range.
             * <p>
             * Implementation uses getProperty(const char *name).
             * </p>
             */
            static int32_t getInt32Property(const std::string & name, const int32_t default_value,
                                            const int32_t min_allowed=INT32_MIN, const int32_t max_allowed=INT32_MAX);

            /**
             * Returns the uint32_t value of the environment's variable 'name',
             * or the 'default_value' if the environment variable's value is null
             * or not within uint32_t value range.
             * <p>
             * Implementation uses getProperty(const char *name).
             * </p>
             */
            static uint32_t getUint32Property(const std::string & name, const uint32_t default_value,
                                              const uint32_t min_allowed=0, const uint32_t max_allowed=UINT32_MAX);

            static DBTEnv& get() {
                /**
                 * Thread safe starting with C++11 6.7:
                 *
                 * If control enters the declaration concurrently while the variable is being initialized,
                 * the concurrent execution shall wait for completion of the initialization.
                 *
                 * (Magic Statics)
                 *
                 * Avoiding non-working double checked locking.
                 */
                static DBTEnv e;
                return e;
            }

            /**
             * Debug logging enabled or disabled.
             * <p>
             * Environment variable 'direct_bt.debug', boolean, default 'false'.
             * </p>
             * <p>
             * Exploding variable-name values</br>
             * If the value of variable 'direct_bt.debug' is neither 'true' or 'false',
             * it is treated as a list of sub-variable names separated by comma ','.</br>
             *
             * Each sub-variable name will be trimmed and if not zero-length
             * appended to the basename "direct_bt.debug" with a dot '.'.</br>
             *
             * The new variable name will be set in the environment with value 'true'.</br>
             *
             * The boolean variable value having an exploded value is considered true.</br>
             *
             * This is supported for DEBUG 'direct_bt.debug' and VERBOSE 'direct_bt.verbose'.</br>
             *
             * Example:
             * <pre>
               Example:
                   "direct_bt.debug" = "hci.event,manager.event,gatt.data".

               This leads DBTEnv to set the following environment variables:
                  "direct_bt.debug.hci.event"  = "true"
                  "direct_bt.debug.manager.event"  = "true"
                  "direct_bt.debug.gatt.data" = "true"

               In this case, DEBUG "direct_bt.debug" is also considered true!
             * </pre>
             * </p>
             */
            const bool DEBUG;

            /**
             * Verbose info logging enabled or disabled.
             * <p>
             * Environment variable 'direct_bt.verbose', boolean, default 'false'.
             * </p>
             * <p>
             * VERBOSE is also enabled if DEBUG is enabled!
             * </p>
             * <p>
             * See 'Exploding variable-name values' in DEBUG above.
             * </p>
             */
            const bool VERBOSE;
    };

} // namespace direct_bt

#endif /* DBT_ENV_HPP_ */