Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBGattValue.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2021 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
26package org.direct_bt;
27
28import java.nio.charset.StandardCharsets;
29import java.util.Arrays;
30
31import org.jau.util.BasicTypes;
32
33/**
34 * A copy of the native GATT value of DBGattChar or DBGattDesc.
35 *
36 * Its {@link #capacity()} defines the maximum writable variable length
37 * and its {@link #size()} defines the maximum writable fixed length.
38 * See {@link #hasVariableLength()}.
39 *
40 * @since 2.4.0
41 */
42public class DBGattValue
43{
44 private final byte[] value_;
45
46 private final int capacity_;
47
48 private boolean variable_length_;
49
50 /** Convenience {@link DBGattValue} ctor function. */
51 public static DBGattValue make(final String name) {
52 final byte[] p = name.getBytes(StandardCharsets.UTF_8);
53 return new DBGattValue(p, p.length);
54 }
55 /** Convenience {@link DBGattValue} ctor function. */
56 public static DBGattValue make(final String name, final int capacity) {
57 final byte[] p = name.getBytes(StandardCharsets.UTF_8);
58 return new DBGattValue(p, Math.max(capacity, p.length), capacity > p.length/* variable_length */);
59 }
60 /** Convenience {@link DBGattValue} ctor function. */
61 public static DBGattValue make(final short v) {
62 final byte[] p = { (byte)0, (byte)0 };
63 p[0] = (byte)(v);
64 p[1] = (byte)(v >> 8);
65 return new DBGattValue(p, p.length);
66 }
67 /** Convenience {@link DBGattValue} ctor function. */
68 public static DBGattValue make(final int capacity, final int size) {
69 final byte[] p = new byte[size];
70 Arrays.fill(p, (byte)0);
71 return new DBGattValue(p, capacity, true /* variable_length */);
72 }
73
74 /** Convenience {@link DBGattValue} ctor function. */
75 public static DBGattValue make(final byte[] p) {
76 return new DBGattValue(p, p.length);
77 }
78
79 /** Convenience {@link DBGattValue} ctor function. */
80 public static DBGattValue make(final int capacity, final int size, final byte[] s) {
81 final byte[] p = new byte[size];
82 final int max_size = Math.min(size, s.length);
83 System.arraycopy(s, 0, p, 0, max_size);
84 return new DBGattValue(p, capacity, true /* variable_length */);
85 }
86
87 /**
88 * Constructor
89 * @param value the data, which length defines the maximum writable fixed length if variable length is false.
90 * If variable length is true, capacity limits the maximum writable length.
91 * @param capacity_ defines the maximum writable variable length, if variable length is true
92 * @param variable_length_ defaults to false.
93 */
94 public DBGattValue(final byte[] value, final int capacity, final boolean variable_length)
95 {
96 this.value_ = value;
97 this.capacity_ = capacity;
98 this.variable_length_ = variable_length;
99 }
100
101 /**
102 * Constructor, using default variable_length = false.
103 * @param value the data, which length defines the maximum writable fixed length, if variable length is disabled.
104 * @param capacity_ defines the maximum writable variable length, if variable length is true
105 */
106 public DBGattValue(final byte[] value, final int capacity)
107 {
108 this(value, capacity, false /* variable_length*/);
109 }
110
111 /**
112 * Returns true if this value has variable length.
113 *
114 * Variable length impacts GATT value write behavior.
115 *
116 * @see #capacity()
117 * @see #size()
118 */
119 public boolean hasVariableLength() { return variable_length_; }
120
121 public void setVariableLength(final boolean v) { variable_length_=v; }
122
123 /**
124 * Return the set capacity for this value.
125 *
126 * Capacity defines the maximum writable variable length,
127 * if variable length is enabled.
128 *
129 * @see #hasVariableLength()
130 */
131 public int capacity() { return capacity_; }
132
133 /**
134 * Return the size of this value, i.e. byte[] length.
135 *
136 * Size defines the maximum writable fixed length,
137 * if variable length is disabled.
138 *
139 * @see #hasVariableLength()
140 */
141 public int size() { return value_.length; }
142
143 /**
144 * Returns the actual data of this value.
145 */
146 public byte[] data() { return value_; };
147
148 /** Fill value with zero bytes. */
149 public void bzero() {
150 for(int i=value_.length-1; 0 <= i; --i) { // anything more efficient?
151 value_[i]=0;
152 }
153 }
154
155 /**
156 * Only compares the actual value, not {@link #hasVariableLength()} nor {@link #capacity()}.
157 * <p>
158 * Parent description:
159 * </p>
160 * {@inheritDoc}
161 */
162 @Override
163 public boolean equals(final Object other) {
164 if( this == other ) {
165 return true;
166 }
167 if( !(other instanceof DBGattValue) ) {
168 return false;
169 }
170 final DBGattValue o = (DBGattValue)other;
171 if( value_.length != o.value_.length ) {
172 return false;
173 }
174 for(int i=0; i<value_.length; i++) {
175 if( value_[i] != o.value_[i] ) {
176 return false;
177 }
178 }
179 // we intentionally ignore capacity
180 return true;
181 }
182
183 @Override
184 public String toString() {
185 final String len = hasVariableLength() ? "var" : "fixed";
186 return "len "+len+", size "+size()+", capacity "+capacity()+", "+
187 BasicTypes.bytesHexString(value_, 0, size(), true /* lsbFirst */) +
188 " '"+BTUtils.decodeUTF8String(value_, 0, size())+"'";
189 }
190}
static String decodeUTF8String(final byte[] buffer, final int offset, final int size)
Decodes the given consecutive UTF-8 characters within buffer to String.
Definition: BTUtils.java:91
A copy of the native GATT value of DBGattChar or DBGattDesc.
static DBGattValue make(final int capacity, final int size, final byte[] s)
Convenience DBGattValue ctor function.
int capacity()
Return the set capacity for this value.
int size()
Return the size of this value, i.e.
static DBGattValue make(final byte[] p)
Convenience DBGattValue ctor function.
static DBGattValue make(final String name, final int capacity)
Convenience DBGattValue ctor function.
static DBGattValue make(final String name)
Convenience DBGattValue ctor function.
boolean equals(final Object other)
Only compares the actual value, not hasVariableLength() nor capacity().
DBGattValue(final byte[] value, final int capacity)
Constructor, using default variable_length = false.
void setVariableLength(final boolean v)
DBGattValue(final byte[] value, final int capacity, final boolean variable_length)
Constructor.
byte[] data()
Returns the actual data of this value.
static DBGattValue make(final int capacity, final int size)
Convenience DBGattValue ctor function.
boolean hasVariableLength()
Returns true if this value has variable length.
void bzero()
Fill value with zero bytes.
static DBGattValue make(final short v)
Convenience DBGattValue ctor function.