Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTUtils.cxx
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 "org_direct_bt_BTUtils.h"
27 
28 #include <cstdint>
29 #include <cinttypes>
30 
31 #include <time.h>
32 
33 #include <jau/dfa_utf8_decode.hpp>
34 #include <jau/environment.hpp>
35 
36 #include "helper_base.hpp"
37 
38 static const uint64_t NanoPerMilli = 1000000UL;
39 static const uint64_t MilliPerOne = 1000UL;
40 
41 /**
42  * See <http://man7.org/linux/man-pages/man2/clock_gettime.2.html>
43  * <p>
44  * Regarding avoiding kernel via VDSO,
45  * see <http://man7.org/linux/man-pages/man7/vdso.7.html>,
46  * clock_gettime seems to be well supported at least on kernel >= 4.4.
47  * Only bfin and sh are missing, while ia64 seems to be complicated.
48  */
49 jlong Java_org_direct_1bt_BTUtils_currentTimeMillis(JNIEnv *env, jclass clazz) {
50  (void)env;
51  (void)clazz;
52 
53  struct timespec t;
54  clock_gettime(CLOCK_MONOTONIC, &t);
55  uint64_t res = static_cast<uint64_t>( t.tv_sec ) * MilliPerOne +
56  static_cast<uint64_t>( t.tv_nsec ) / NanoPerMilli;
57  return (jlong)res;
58 }
59 
60 jlong Java_org_direct_1bt_BTUtils_wallClockSeconds(JNIEnv *env, jclass clazz) {
61  (void)env;
62  (void)clazz;
63 
64  struct timespec t;
65  clock_gettime(CLOCK_REALTIME, &t);
66  return (jlong)( static_cast<uint64_t>( t.tv_sec ) );
67 }
68 
69 
70 jlong Java_org_direct_1bt_BTUtils_startupTimeMillisImpl(JNIEnv *env, jclass clazz) {
71  (void)env;
72  (void)clazz;
73 
75 }
76 
77 jstring Java_org_direct_1bt_BTUtils_decodeUTF8String(JNIEnv *env, jclass clazz, jbyteArray jbuffer, jint offset, jint size) {
78  (void)clazz;
79 
80  const int buffer_size = env->GetArrayLength(jbuffer);
81  if( 0 == buffer_size ) {
82  return env->NewStringUTF("");
83  }
84  if( buffer_size < offset+size ) {
85  std::string msg("buffer.length "+std::to_string(buffer_size)+
86  " < offset "+std::to_string(offset)+
87  " + size "+std::to_string(size));
89  }
90 
91  std::string sres;
92  {
93  JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
94  uint8_t * buffer_ptr = criticalArray.get(jbuffer, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
95  if( NULL == buffer_ptr ) {
96  throw jau::IllegalArgumentException("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE);
97  }
98  sres = jau::dfa_utf8_decode(buffer_ptr+offset, static_cast<size_t>(size));
99  }
100  return jau::from_string_to_jstring(env, sres);
101 }
Java_org_direct_1bt_BTUtils_currentTimeMillis
jlong Java_org_direct_1bt_BTUtils_currentTimeMillis(JNIEnv *env, jclass clazz)
See http://man7.org/linux/man-pages/man2/clock_gettime.2.html
Definition: BTUtils.cxx:49
Java_org_direct_1bt_BTUtils_decodeUTF8String
jstring Java_org_direct_1bt_BTUtils_decodeUTF8String(JNIEnv *env, jclass clazz, jbyteArray jbuffer, jint offset, jint size)
Definition: BTUtils.cxx:77
jau::IllegalArgumentException
Definition: basic_types.hpp:111
jau::from_string_to_jstring
jstring from_string_to_jstring(JNIEnv *env, const std::string &str)
Java_org_direct_1bt_BTUtils_startupTimeMillisImpl
jlong Java_org_direct_1bt_BTUtils_startupTimeMillisImpl(JNIEnv *env, jclass clazz)
Definition: BTUtils.cxx:70
helper_base.hpp
dfa_utf8_decode.hpp
Java_org_direct_1bt_BTUtils_wallClockSeconds
jlong Java_org_direct_1bt_BTUtils_wallClockSeconds(JNIEnv *env, jclass clazz)
Definition: BTUtils.cxx:60
jau::to_string
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
Definition: byte_util.hpp:198
E_FILE_LINE
#define E_FILE_LINE
Definition: basic_types.hpp:64
NanoPerMilli
static const uint64_t NanoPerMilli
Definition: BTUtils.cxx:38
JNICriticalArray::get
T * get(U jarray_val, Mode mode_val=UPDATE_AND_RELEASE)
Acquired the primitive array.
Definition: jni_mem.hpp:177
jau::dfa_utf8_decode
uint32_t dfa_utf8_decode(uint32_t &state, uint32_t &codep, const uint32_t byte_value)
Definition: dfa_utf8_decode.cpp:90
jau::environment::startupTimeMilliseconds
static const uint64_t startupTimeMilliseconds
Module startup time t0 in monotonic time in milliseconds.
Definition: environment.hpp:75
JNICriticalArray
Definition: jni_mem.hpp:126
MilliPerOne
static const uint64_t MilliPerOne
Definition: BTUtils.cxx:39
environment.hpp