-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventQueueExampleNatives.java
More file actions
235 lines (202 loc) · 8.16 KB
/
Copy pathEventQueueExampleNatives.java
File metadata and controls
235 lines (202 loc) · 8.16 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Java
*
* Copyright 2023 MicroEJ Corp. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be found with this software.
*/
package com.microej.example;
import java.nio.ByteOrder;
import java.util.Random;
import ej.event.EventQueueMock;
/**
* Native methods of the Event Queue Example.
*/
public class EventQueueExampleNatives {
private static final Random random = new Random();
private static final int GPIO_ID_MASK = 0xFF;
private static final int GPIO_ID_SHIFT = 16;
private static final int GPIO_STATE_MASK = 0xFFFF;
private static final int ACCELEROMETER_MAX_VALUE = 1000;
private static final int ACCELEROMETER_ARRAY_DATA_SIZE = 12;
private static final int ACCELEROMETER_ARRAY_Y_INDEX = 4;
private static final int ACCELEROMETER_ARRAY_Z_INDEX = 8;
private static final int WEATHER_ARRAY_DATA_SIZE = 17;
private static final int WEATHER_MAX_VALUE = 100;
private static final int WEATHER_WIND_INDEX = 8;
private static final int WEATHER_RAIN_INDEX = 16;
private static final int DOUBLE_ARRAY_SIZE = 8;
private static final int FLOAT_ARRAY_SIZE = 4;
private static final int INTEGER_ARRAY_SIZE = 4;
private static final int DEFAULT_EVENT_MAX_VALUE = 100;
private static final int DEFAULT_EXTENDED_EVENT_DATA_LENGTH = 10;
private static final int SHIFT_8 = 8;
private static final int SHIFT_16 = 16;
private static final int SHIFT_24 = 24;
private static final int SHIFT_32 = 32;
private static final int SHIFT_40 = 40;
private static final int SHIFT_48 = 48;
private static final int SHIFT_56 = 56;
/**
* Private constructor.
*/
private EventQueueExampleNatives() {
}
/**
* Offer an GPIO event to the Event Queue.
*
* @param type
* the type of the event.
* @return 0 success, -1 failed: illegal arguments, -2 failed: the FIFO is full
*/
public static int offerGpioEvent(int type) {
return EventQueueMock.offerEvent(type, getGpioData());
}
/**
* Offer an accelerometer event to the Event Queue.
*
* @param type
* the type of the event.
* @return 0 success, -1 failed: illegal arguments, -2 failed: the FIFO is full
*/
public static int offerAccelerometerEvent(int type) {
byte[] accelerometerData = getAccelerometerData();
return EventQueueMock.offerExtendedEvent(type, accelerometerData, accelerometerData.length);
}
/**
* Offer a weather event to the Event Queue.
*
* @param type
* the type of the event.
* @return 0 success, -1 failed: illegal arguments, -2 failed: the FIFO is full
*/
public static int offerWeatherEvent(int type) {
byte[] weatherData = getWeatherData();
return EventQueueMock.offerExtendedEvent(type, weatherData, weatherData.length);
}
/**
* Offer a default basic event to the Event Queue.
*
* @param type
* the type of the event.
* @return 0 success, -1 failed: illegal arguments, -2 failed: the FIFO is full
*/
public static int offerDefaultBasicEvent(int type) {
return EventQueueMock.offerEvent(type, random.nextInt(DEFAULT_EVENT_MAX_VALUE));
}
/**
* Offer a default extended event to the Event Queue.
*
* @param type
* the type of the event.
* @return 0 success, -1 failed: illegal arguments, -2 failed: the FIFO is full
*/
public static int offerDefaultExtendedEvent(int type) {
int dataSize = random.nextInt(DEFAULT_EXTENDED_EVENT_DATA_LENGTH);
return EventQueueMock.offerExtendedEvent(type, new byte[dataSize], dataSize);
}
/**
* The GPIO data are composed of the Id and the State of the GPIO:
*
* ID (8 bits) | STATE (16 bits)
*
* @return the randomized GPIO data.
*/
private static int getGpioData() {
int gpioId = random.nextInt(GPIO_ID_MASK);
int gpioState = random.nextInt(GPIO_STATE_MASK);
System.out.println( // NOSONAR Use System.out for the readability of the example.
"[EventQueueExampleNatives] GPIO data sent through the Queue: Id=" + gpioId + ", state=" + gpioState); //$NON-NLS-1$ //$NON-NLS-2$
return ((gpioId & GPIO_ID_MASK) << GPIO_ID_SHIFT) | (gpioState & GPIO_STATE_MASK);
}
/**
* Accelerometer data are composed of three Integers corresponding to the X, Y and Z values of an accelerometer.
*
* @return the randomized accelerometer data.
*/
private static byte[] getAccelerometerData() {
int x = random.nextInt(ACCELEROMETER_MAX_VALUE);
int y = random.nextInt(ACCELEROMETER_MAX_VALUE);
int z = random.nextInt(ACCELEROMETER_MAX_VALUE);
System.out.println( // NOSONAR Use System.out for the readability of the example.
"[EventQueueExampleNatives] Accelerometer data sent through the Queue: x = " + x + ", y=" + y //$NON-NLS-1$ //$NON-NLS-2$
+ ", z=" + z); //$NON-NLS-1$
byte[] accelerometerData = new byte[ACCELEROMETER_ARRAY_DATA_SIZE];
byte[] xArray = intToByteArray(x);
byte[] yArray = intToByteArray(y);
byte[] zArray = intToByteArray(z);
System.arraycopy(xArray, 0, accelerometerData, 0, INTEGER_ARRAY_SIZE);
System.arraycopy(yArray, 0, accelerometerData, ACCELEROMETER_ARRAY_Y_INDEX, INTEGER_ARRAY_SIZE);
System.arraycopy(zArray, 0, accelerometerData, ACCELEROMETER_ARRAY_Z_INDEX, INTEGER_ARRAY_SIZE);
return accelerometerData;
}
/**
* This listener is used to handle weather extended data received. The alignment of the data must be the same than
* the C structures.
*
* The weather extended data are composed of:
*
* - The temperature (float value). -> 4 bytes aligned, data[0:3].
*
* - The wind (double value). -> 8 bytes aligned, data[8:15].
*
* - The rain (boolean value). -> 1 byte aligned, data[16];
*
* @return the randomized weather data.
*/
private static byte[] getWeatherData() {
float temperature = random.nextFloat() * WEATHER_MAX_VALUE;
double wind = random.nextDouble() * WEATHER_MAX_VALUE;
byte rain = (byte) random.nextInt(2);
byte[] weatherValues = new byte[WEATHER_ARRAY_DATA_SIZE];
// Get the array of bytes from the float value:
byte[] floatArray = intToByteArray(Float.floatToIntBits(temperature));
// Get the array of bytes from the double value:
byte[] doubleArray = longToByteArray(Double.doubleToLongBits(wind));
System.arraycopy(floatArray, 0, weatherValues, 0, FLOAT_ARRAY_SIZE);// 4 bytes aligned -> byte[0:4]
System.arraycopy(doubleArray, 0, weatherValues, WEATHER_WIND_INDEX, DOUBLE_ARRAY_SIZE);// 8 bytes aligned ->
// byte[8:15]
weatherValues[WEATHER_RAIN_INDEX] = rain;
System.out.println( // NOSONAR Use System.out for the readability of the example.
"[EventQueueExampleNatives] Weather data sent through the Queue: temperature = " //$NON-NLS-1$
+ temperature + ", wind =" + wind + ", rain =" + rain); //$NON-NLS-1$ //$NON-NLS-2$
return weatherValues;
}
/**
* Converts an integer value into a byte array depending on the endianness of the VEE Port.
*
* @param value
* the integer value to convert.
* @return the byte array corresponding to the integer value.
*/
public static byte[] intToByteArray(int value) {
byte[] returnInt;
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
returnInt = new byte[] { (byte) (value), (byte) (value >> SHIFT_8), (byte) (value >> SHIFT_16),
(byte) (value >> SHIFT_24) };
} else {
returnInt = new byte[] { (byte) (value >> SHIFT_24), (byte) (value >> SHIFT_16), (byte) (value >> SHIFT_8),
(byte) (value) };
}
return returnInt;
}
/**
* Converts a long value into a byte array depending on the endianness of the VEE Port.
*
* @param value
* the long value to convert.
* @return the byte array corresponding to the long value.
*/
public static byte[] longToByteArray(long value) {
byte[] returnDouble;
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
returnDouble = new byte[] { (byte) (value), (byte) (value >> SHIFT_8), (byte) (value >> SHIFT_16),
(byte) (value >> SHIFT_24), (byte) (value >> SHIFT_32), (byte) (value >> SHIFT_40),
(byte) (value >> SHIFT_48), (byte) (value >> SHIFT_56) };
} else {
returnDouble = new byte[] { (byte) (value >> SHIFT_56), (byte) (value >> SHIFT_48),
(byte) (value >> SHIFT_40), (byte) (value >> SHIFT_32), (byte) (value >> SHIFT_24),
(byte) (value >> SHIFT_16), (byte) (value >> SHIFT_8), (byte) (value) };
}
return returnDouble;
}
}