Revision 940
Prototype JNI (Java Native Interface) for libwireless. Compiles, and
java loads the library, but untested with a real scenario or hardware.
trunk/code/projects/libwireless/jni/test/CWirelessTest.java | ||
---|---|---|
1 |
import org.roboticsclub.colony.*; |
|
2 |
import java.util.Scanner; |
|
3 |
import java.util.Arrays; |
|
4 |
|
|
5 |
public class CWirelessTest |
|
6 |
{ |
|
7 |
|
|
8 |
private static final byte TEST_PACKET_GROUP_ID = 15; |
|
9 |
|
|
10 |
public static void main(String[] args) throws Exception |
|
11 |
{ |
|
12 |
if (args.length!=1) |
|
13 |
{ |
|
14 |
System.err.println("Usage: java CWirelessTest <port name>"); |
|
15 |
System.exit(1); |
|
16 |
} |
|
17 |
|
|
18 |
try { |
|
19 |
CWirelessTest ctest = new CWirelessTest(args[0]); |
|
20 |
} catch (Exception e) { |
|
21 |
e.printStackTrace(); |
|
22 |
System.exit(1); |
|
23 |
} |
|
24 |
|
|
25 |
Scanner in = new Scanner(System.in); |
|
26 |
|
|
27 |
while(true) |
|
28 |
{ |
|
29 |
String command = in.nextLine(); |
|
30 |
|
|
31 |
if (command.charAt(0)=='\\') |
|
32 |
{ |
|
33 |
//command mode |
|
34 |
switch(command.charAt(1)) |
|
35 |
{ |
|
36 |
case 'i': |
|
37 |
System.out.println("XBee IDs: " + Arrays.toString(get_xbee_ids())); |
|
38 |
break; |
|
39 |
case 'n': |
|
40 |
System.out.println("Number of robots: " + get_num_robots()); |
|
41 |
break; |
|
42 |
case 'm': |
|
43 |
SensorReading[][] matrix = get_sensor_matrix(); |
|
44 |
System.out.println("BOM Matrix:"); |
|
45 |
for (int i = 0; i < matrix.length; i++) |
|
46 |
{ |
|
47 |
System.out.println(" From robot " + matrix[i][0].botid + ":"); |
|
48 |
for (int j = 0; j < matrix.length; j++) |
|
49 |
{ |
|
50 |
System.out.print(" " + matrix[i][j].target + ": " + matrix[i][j].reading); |
|
51 |
} |
|
52 |
} |
|
53 |
break; |
|
54 |
case 's': |
|
55 |
char dest = command.charAt(2); |
|
56 |
String message = command.substring(3); |
|
57 |
|
|
58 |
test_wl_send((short)dest, (short)1, command.getBytes()); |
|
59 |
break; |
|
60 |
default: |
|
61 |
System.out.println("Command not recognized"); |
|
62 |
break; |
|
63 |
} |
|
64 |
} |
|
65 |
else |
|
66 |
{ |
|
67 |
//send message |
|
68 |
test_wl_send((short)-1, (short)1, command.getBytes()); |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
public CWirelessTest(String wl_port) throws Exception |
|
74 |
{ |
|
75 |
Wireless.wl_set_com_port(wl_port); |
|
76 |
|
|
77 |
System.out.println("Calling wl_init(" + wl_port + ")..."); |
|
78 |
if (Wireless.wl_init() != 0) { |
|
79 |
throw new Exception("wl_init failed.\n"); |
|
80 |
} |
|
81 |
|
|
82 |
Wireless.wl_set_channel(0xF); |
|
83 |
|
|
84 |
TokenRing.wl_token_ring_register(); |
|
85 |
|
|
86 |
System.out.println("Joining token ring..."); |
|
87 |
if (TokenRing.wl_token_ring_join() != 0) { |
|
88 |
throw new Exception("Failed to join token ring."); |
|
89 |
} |
|
90 |
System.out.println("Joined token ring."); |
|
91 |
|
|
92 |
System.out.println("Spawning listener thread..."); |
|
93 |
|
|
94 |
Wireless.PacketGroupHandler pgh = new TestPacketGroupHandler(); |
|
95 |
Wireless.wl_register_packet_group(pgh); |
|
96 |
|
|
97 |
Wireless.startWirelessThread(1); |
|
98 |
} |
|
99 |
|
|
100 |
public static void test_wl_send(short dest, short msg_type, byte[] data) throws Exception |
|
101 |
{ |
|
102 |
System.out.println("test_wl_send: dest:" + dest + ", msg_type:" + msg_type + ", data:" + new String(data)); |
|
103 |
|
|
104 |
if (dest < 0) { |
|
105 |
System.out.println("sending to global dest"); |
|
106 |
int ret = Wireless.wl_send_global_packet(TEST_PACKET_GROUP_ID, (byte)msg_type, data, (byte)0); |
|
107 |
if (ret != 0) { |
|
108 |
throw new Exception("test_wl_send: wl_send_global_packet failed."); |
|
109 |
} |
|
110 |
} else { |
|
111 |
System.out.println("sending to specific robot: " + dest); |
|
112 |
int ret = Wireless.wl_send_robot_to_robot_global_packet(TEST_PACKET_GROUP_ID, (byte)msg_type, data, dest, (byte)0); |
|
113 |
if (ret != 0) { |
|
114 |
throw new Exception("test_wl_send: wl_send_robot_to_robot_global_packet failed."); |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
public static int get_num_robots() { |
|
120 |
return TokenRing.wl_token_get_num_robots(); |
|
121 |
} |
|
122 |
|
|
123 |
public static int[] get_xbee_ids() { |
|
124 |
int num_robots = TokenRing.wl_token_get_num_robots(); |
|
125 |
int[] ids = new int[num_robots]; |
|
126 |
|
|
127 |
TokenRing.wl_token_iterator_begin(); |
|
128 |
|
|
129 |
int i = 0; |
|
130 |
while (TokenRing.wl_token_iterator_has_next()!=0) { |
|
131 |
ids[i] = TokenRing.wl_token_iterator_next(); |
|
132 |
i++; |
|
133 |
} |
|
134 |
|
|
135 |
return ids; |
|
136 |
} |
|
137 |
|
|
138 |
public static class SensorReading |
|
139 |
{ |
|
140 |
public int botid; |
|
141 |
public int target; |
|
142 |
public int reading; |
|
143 |
|
|
144 |
public SensorReading(int botid, int target, int reading) |
|
145 |
{ |
|
146 |
this.botid = botid; |
|
147 |
this.target = target; |
|
148 |
this.reading = reading; |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
public static SensorReading[][] get_sensor_matrix() { |
|
153 |
int[] ids = get_xbee_ids(); |
|
154 |
int num_robots = ids.length; |
|
155 |
|
|
156 |
SensorReading[][] m = new SensorReading[num_robots][num_robots]; |
|
157 |
|
|
158 |
for (int i = 0; i < num_robots; i++) { |
|
159 |
for (int j = 0; j < num_robots; j++) { |
|
160 |
m[i][j] = new SensorReading(ids[i], ids[j], TokenRing.wl_token_get_sensor_reading(ids[i], ids[j]) ); |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
return m; |
|
165 |
} |
|
166 |
|
|
167 |
public static class TestPacketGroupHandler implements Wireless.PacketGroupHandler |
|
168 |
{ |
|
169 |
public int getGroupCode() { |
|
170 |
return TEST_PACKET_GROUP_ID; |
|
171 |
} |
|
172 |
|
|
173 |
public void timeout_handler() { |
|
174 |
System.err.println("wireless - timeout!"); |
|
175 |
} |
|
176 |
|
|
177 |
public void handle_response(int frame, int received) { |
|
178 |
} |
|
179 |
|
|
180 |
public void handle_received(byte type, int source_robot, byte[] data) { |
|
181 |
System.out.println("***Got packet from robot***"); |
|
182 |
|
|
183 |
System.out.println("Received message " + type + " from " + source_robot + ": " + new String(data)); |
|
184 |
} |
|
185 |
|
|
186 |
public void unregister() { |
|
187 |
System.out.println("unregister"); |
|
188 |
} |
|
189 |
} |
|
190 |
|
|
191 |
} |
trunk/code/projects/libwireless/jni/test/Makefile | ||
---|---|---|
1 |
all : |
|
2 |
javac -cp .:../lib/colonywireless.jar CWirelessTest.java |
|
3 |
|
|
4 |
run : |
|
5 |
java -cp .:../lib/colonywireless.jar -Djava.library.path=../lib CWirelessTest /dev/ttyUSB0 |
|
6 |
|
|
7 |
clean : |
|
8 |
rm *.class |
trunk/code/projects/libwireless/jni/robot_test/main.c | ||
---|---|---|
1 |
#include <dragonfly_lib.h> |
|
2 |
#include <wireless.h> |
|
3 |
#include <wl_token_ring.h> |
|
4 |
#include <xbee.h> |
|
5 |
|
|
6 |
#define TEST_PACKET_GROUP_ID 15 |
|
7 |
|
|
8 |
void handle_receive(char type, int source, unsigned char* packet, int length) |
|
9 |
{ |
|
10 |
wl_send_robot_to_robot_global_packet(TEST_PACKET_GROUP_ID, type, (char *)packet, length, source, 0); |
|
11 |
} |
|
12 |
|
|
13 |
int main(void) |
|
14 |
{ |
|
15 |
//usb_puts("Turned on.\n"); |
|
16 |
dragonfly_init(ALL_ON); |
|
17 |
usb_puts("Dragonfly initialized.\n"); |
|
18 |
|
|
19 |
wl_init(); |
|
20 |
usb_puts("Wireless initialized.\n"); |
|
21 |
|
|
22 |
PacketGroupHandler pgh = { TEST_PACKET_GROUP_ID, NULL, NULL, handle_receive, NULL }; |
|
23 |
wl_register_packet_group(&pgh); |
|
24 |
|
|
25 |
wl_set_channel(0xF); |
|
26 |
usb_puts("Wireless channel set.\n"); |
|
27 |
wl_token_ring_register(); |
|
28 |
usb_puts("Token ring initialized.\n"); |
|
29 |
wl_token_ring_join(); |
|
30 |
usb_puts("Token ring joined.\n"); |
|
31 |
|
|
32 |
while (1) |
|
33 |
{ |
|
34 |
wl_do(); |
|
35 |
} |
|
36 |
} |
|
37 |
|
trunk/code/projects/libwireless/jni/robot_test/Makefile | ||
---|---|---|
1 |
# Hey Emacs, this is a -*- makefile -*- |
|
2 |
#---------------------------------------------------------------------------- |
|
3 |
# WinAVR Makefile Template written by Eric B. Weddington, J?rg Wunsch, et al. |
|
4 |
# |
|
5 |
# Released to the Public Domain |
|
6 |
# |
|
7 |
# Additional material for this makefile was written by: |
|
8 |
# Peter Fleury |
|
9 |
# Tim Henigan |
|
10 |
# Colin O'Flynn |
|
11 |
# Reiner Patommel |
|
12 |
# Markus Pfaff |
|
13 |
# Sander Pool |
|
14 |
# Frederik Rouleau |
|
15 |
# |
|
16 |
#---------------------------------------------------------------------------- |
|
17 |
# On command line: |
|
18 |
# |
|
19 |
# make all = Make software. |
|
20 |
# |
|
21 |
# make clean = Clean out built project files. |
|
22 |
# |
|
23 |
# make coff = Convert ELF to AVR COFF. |
|
24 |
# |
|
25 |
# make extcoff = Convert ELF to AVR Extended COFF. |
|
26 |
# |
|
27 |
# make program = Download the hex file to the device, using avrdude. |
|
28 |
# Please customize the avrdude settings below first! |
|
29 |
# |
|
30 |
# make debug = Start either simulavr or avarice as specified for debugging, |
|
31 |
# with avr-gdb or avr-insight as the front end for debugging. |
|
32 |
# |
|
33 |
# make filename.s = Just compile filename.c into the assembler code only. |
|
34 |
# |
|
35 |
# make filename.i = Create a preprocessed source file for use in submitting |
|
36 |
# bug reports to the GCC project. |
|
37 |
# |
|
38 |
# To rebuild project do "make clean" then "make all". |
|
39 |
#---------------------------------------------------------------------------- |
|
40 |
|
|
41 |
#if you want your code to work on the Firefly++ and not Firefly+ |
|
42 |
#then add the -DFFPP line to CDEFS |
|
43 |
COLONYROOT = y: |
|
44 |
CDEFS = |
|
45 |
#-DFFPP |
|
46 |
|
|
47 |
# MCU name |
|
48 |
MCU = atmega128 |
|
49 |
|
|
50 |
|
|
51 |
# Processor frequency. |
|
52 |
# This will define a symbol, F_CPU, in all source code files equal to the |
|
53 |
# processor frequency. You can then use this symbol in your source code to |
|
54 |
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done |
|
55 |
# automatically to create a 32-bit value in your source code. |
|
56 |
F_CPU = 8000000 |
|
57 |
|
|
58 |
|
|
59 |
# Output format. (can be srec, ihex, binary) |
|
60 |
FORMAT = ihex |
|
61 |
|
|
62 |
|
|
63 |
# Target file name (without extension). |
|
64 |
TARGET = main |
|
65 |
|
|
66 |
|
|
67 |
# List C source files here. (C dependencies are automatically generated.) |
|
68 |
SRC = $(wildcard *.c) |
|
69 |
#$(TARGET).c |
|
70 |
|
|
71 |
|
|
72 |
# List Assembler source files here. |
|
73 |
# Make them always end in a capital .S. Files ending in a lowercase .s |
|
74 |
# will not be considered source files but generated files (assembler |
|
75 |
# output from the compiler), and will be deleted upon "make clean"! |
|
76 |
# Even though the DOS/Win* filesystem matches both .s and .S the same, |
|
77 |
# it will preserve the spelling of the filenames, and gcc itself does |
|
78 |
# care about how the name is spelled on its command-line. |
|
79 |
ASRC = |
|
80 |
|
|
81 |
|
|
82 |
# Optimization level, can be [0, 1, 2, 3, s]. |
|
83 |
# 0 = turn off optimization. s = optimize for size. |
|
84 |
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.) |
|
85 |
OPT = s |
|
86 |
|
|
87 |
|
|
88 |
# Debugging format. |
|
89 |
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. |
|
90 |
# AVR Studio 4.10 requires dwarf-2. |
|
91 |
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. |
|
92 |
DEBUG = |
|
93 |
|
|
94 |
|
|
95 |
# List any extra directories to look for include files here. |
|
96 |
# Each directory must be seperated by a space. |
|
97 |
# Use forward slashes for directory separators. |
|
98 |
# For a directory that has spaces, enclose it in quotes. |
|
99 |
EXTRAINCDIRS = $(COLONYROOT)/code/firefly_plus_lib |
|
100 |
#C:\WinAVR\include\fwr |
|
101 |
|
|
102 |
|
|
103 |
# Compiler flag to set the C Standard level. |
|
104 |
# c89 = "ANSI" C |
|
105 |
# gnu89 = c89 plus GCC extensions |
|
106 |
# c99 = ISO C99 standard (not yet fully implemented) |
|
107 |
# gnu99 = c99 plus GCC extensions |
|
108 |
CSTANDARD = -std=gnu99 |
|
109 |
|
|
110 |
|
|
111 |
# Place -D or -U options here |
|
112 |
CDEFS += -DF_CPU=$(F_CPU)UL |
|
113 |
CDEFS += -DFFP |
|
114 |
|
|
115 |
|
|
116 |
# Place -I options here |
|
117 |
CINCS = -I../../lib -I../../../libdragonfly -L../../lib -L../../../libdragonfly |
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
#---------------- Compiler Options ---------------- |
|
122 |
# -g*: generate debugging information |
|
123 |
# -O*: optimization level |
|
124 |
# -f...: tuning, see GCC manual and avr-libc documentation |
|
125 |
# -Wall...: warning level |
|
126 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
127 |
# -adhlns...: create assembler listing |
|
128 |
CFLAGS = -g$(DEBUG) |
|
129 |
CFLAGS += $(CDEFS) $(CINCS) |
|
130 |
CFLAGS += -O$(OPT) |
|
131 |
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums |
|
132 |
CFLAGS += -Wall -Wstrict-prototypes |
|
133 |
CFLAGS += -Wa,-adhlns=$(<:.c=.lst) |
|
134 |
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) |
|
135 |
CFLAGS += $(CSTANDARD) |
|
136 |
CFLAGS += -DROBOT |
|
137 |
|
|
138 |
|
|
139 |
#---------------- Assembler Options ---------------- |
|
140 |
# -Wa,...: tell GCC to pass this to the assembler. |
|
141 |
# -ahlms: create listing |
|
142 |
# -gstabs: have the assembler create line number information; note that |
|
143 |
# for use in COFF files, additional information about filenames |
|
144 |
# and function names needs to be present in the assembler source |
|
145 |
# files -- see avr-libc docs [FIXME: not yet described there] |
|
146 |
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs |
|
147 |
|
|
148 |
|
|
149 |
#---------------- Library Options ---------------- |
|
150 |
# Minimalistic printf version |
|
151 |
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min |
|
152 |
|
|
153 |
# Floating point printf version (requires MATH_LIB = -lm below) |
|
154 |
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt |
|
155 |
|
|
156 |
# If this is left blank, then it will use the Standard printf version. |
|
157 |
PRINTF_LIB = |
|
158 |
#PRINTF_LIB = $(PRINTF_LIB_MIN) |
|
159 |
#PRINTF_LIB = $(PRINTF_LIB_FLOAT) |
|
160 |
|
|
161 |
|
|
162 |
# Minimalistic scanf version |
|
163 |
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min |
|
164 |
|
|
165 |
# Floating point + %[ scanf version (requires MATH_LIB = -lm below) |
|
166 |
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt |
|
167 |
|
|
168 |
# If this is left blank, then it will use the Standard scanf version. |
|
169 |
SCANF_LIB = |
|
170 |
#SCANF_LIB = $(SCANF_LIB_MIN) |
|
171 |
#SCANF_LIB = $(SCANF_LIB_FLOAT) |
|
172 |
|
|
173 |
|
|
174 |
MATH_LIB = -lm |
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
#---------------- External Memory Options ---------------- |
|
179 |
|
|
180 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
181 |
# used for variables (.data/.bss) and heap (malloc()). |
|
182 |
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff |
|
183 |
|
|
184 |
# 64 KB of external RAM, starting after internal RAM (ATmega128!), |
|
185 |
# only used for heap (malloc()). |
|
186 |
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff |
|
187 |
|
|
188 |
EXTMEMOPTS = |
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
#---------------- Linker Options ---------------- |
|
193 |
# -Wl,...: tell GCC to pass this to linker. |
|
194 |
# -Map: create map file |
|
195 |
# --cref: add cross reference to map file |
|
196 |
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref |
|
197 |
LDFLAGS += $(EXTMEMOPTS) |
|
198 |
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) |
|
199 |
LDFLAGS += -lwireless -ldragonfly |
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
#---------------- Programming Options (avrdude) ---------------- |
|
204 |
|
|
205 |
# Programming hardware: alf avr910 avrisp bascom bsd |
|
206 |
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500 |
|
207 |
# |
|
208 |
# Type: avrdude -c ? |
|
209 |
# to get a full listing. |
|
210 |
# |
|
211 |
AVRDUDE_PROGRAMMER = avrisp |
|
212 |
|
|
213 |
# com1 = serial port. Use lpt1 to connect to parallel port. |
|
214 |
AVRDUDE_PORT = /dev/ttyUSB1 |
|
215 |
# programmer connected to serial device |
|
216 |
|
|
217 |
AVRDUDE_WRITE_FLASH = -b 57600 -U flash:w:$(TARGET).hex |
|
218 |
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep |
|
219 |
|
|
220 |
|
|
221 |
# Uncomment the following if you want avrdude's erase cycle counter. |
|
222 |
# Note that this counter needs to be initialized first using -Yn, |
|
223 |
# see avrdude manual. |
|
224 |
#AVRDUDE_ERASE_COUNTER = -y |
|
225 |
|
|
226 |
# Uncomment the following if you do /not/ wish a verification to be |
|
227 |
# performed after programming the device. |
|
228 |
#AVRDUDE_NO_VERIFY = -V |
|
229 |
|
|
230 |
# Increase verbosity level. Please use this when submitting bug |
|
231 |
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> |
|
232 |
# to submit bug reports. |
|
233 |
#AVRDUDE_VERBOSE = -v -v |
|
234 |
|
|
235 |
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) |
|
236 |
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY) |
|
237 |
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE) |
|
238 |
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER) |
|
239 |
|
|
240 |
#don't check for device signature |
|
241 |
AVRDUDE_FLAGS += -F |
|
242 |
AVRDUDE_FLAGS += -D |
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
#---------------- Debugging Options ---------------- |
|
247 |
|
|
248 |
# For simulavr only - target MCU frequency. |
|
249 |
DEBUG_MFREQ = $(F_CPU) |
|
250 |
|
|
251 |
# Set the DEBUG_UI to either gdb or insight. |
|
252 |
# DEBUG_UI = gdb |
|
253 |
DEBUG_UI = insight |
|
254 |
|
|
255 |
# Set the debugging back-end to either avarice, simulavr. |
|
256 |
DEBUG_BACKEND = avarice |
|
257 |
#DEBUG_BACKEND = simulavr |
|
258 |
|
|
259 |
# GDB Init Filename. |
|
260 |
GDBINIT_FILE = __avr_gdbinit |
|
261 |
|
|
262 |
# When using avarice settings for the JTAG |
|
263 |
JTAG_DEV = /dev/com1 |
|
264 |
|
|
265 |
# Debugging port used to communicate between GDB / avarice / simulavr. |
|
266 |
DEBUG_PORT = 4242 |
|
267 |
|
|
268 |
# Debugging host used to communicate between GDB / avarice / simulavr, normally |
|
269 |
# just set to localhost unless doing some sort of crazy debugging when |
|
270 |
# avarice is running on a different computer. |
|
271 |
DEBUG_HOST = localhost |
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
#============================================================================ |
|
276 |
|
|
277 |
|
|
278 |
# Define programs and commands. |
|
279 |
SHELL = sh |
|
280 |
CC = avr-gcc |
|
281 |
OBJCOPY = avr-objcopy |
|
282 |
OBJDUMP = avr-objdump |
|
283 |
SIZE = avr-size |
|
284 |
NM = avr-nm |
|
285 |
AVRDUDE = avrdude |
|
286 |
REMOVE = rm -f |
|
287 |
REMOVEDIR = rm -rf |
|
288 |
COPY = cp |
|
289 |
WINSHELL = cmd |
|
290 |
|
|
291 |
|
|
292 |
# Define Messages |
|
293 |
# English |
|
294 |
MSG_ERRORS_NONE = Errors: none |
|
295 |
MSG_BEGIN = -------- begin -------- |
|
296 |
MSG_END = -------- end -------- |
|
297 |
MSG_SIZE_BEFORE = Size before: |
|
298 |
MSG_SIZE_AFTER = Size after: |
|
299 |
MSG_COFF = Converting to AVR COFF: |
|
300 |
MSG_EXTENDED_COFF = Converting to AVR Extended COFF: |
|
301 |
MSG_FLASH = Creating load file for Flash: |
|
302 |
MSG_EEPROM = Creating load file for EEPROM: |
|
303 |
MSG_EXTENDED_LISTING = Creating Extended Listing: |
|
304 |
MSG_SYMBOL_TABLE = Creating Symbol Table: |
|
305 |
MSG_LINKING = Linking: |
|
306 |
MSG_COMPILING = Compiling: |
|
307 |
MSG_ASSEMBLING = Assembling: |
|
308 |
MSG_CLEANING = Cleaning project: |
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
# Define all object files. |
|
314 |
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) |
|
315 |
|
|
316 |
# Define all listing files. |
|
317 |
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) |
|
318 |
|
|
319 |
|
|
320 |
# Compiler flags to generate dependency files. |
|
321 |
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d |
|
322 |
|
|
323 |
|
|
324 |
# Combine all necessary flags and optional flags. |
|
325 |
# Add target processor to flags. |
|
326 |
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) |
|
327 |
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) |
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
# Default target. |
|
334 |
all: begin gccversion sizebefore build sizeafter end |
|
335 |
|
|
336 |
build: elf hex eep lss sym |
|
337 |
|
|
338 |
elf: $(TARGET).elf |
|
339 |
hex: $(TARGET).hex |
|
340 |
eep: $(TARGET).eep |
|
341 |
lss: $(TARGET).lss |
|
342 |
sym: $(TARGET).sym |
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
# Eye candy. |
|
347 |
# AVR Studio 3.x does not check make's exit code but relies on |
|
348 |
# the following magic strings to be generated by the compile job. |
|
349 |
begin: |
|
350 |
@echo |
|
351 |
@echo $(MSG_BEGIN) |
|
352 |
|
|
353 |
end: |
|
354 |
@echo $(MSG_END) |
|
355 |
@echo |
|
356 |
|
|
357 |
|
|
358 |
# Display size of file. |
|
359 |
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex |
|
360 |
ELFSIZE = $(SIZE) -A $(TARGET).elf |
|
361 |
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU) |
|
362 |
|
|
363 |
sizebefore: |
|
364 |
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \ |
|
365 |
$(AVRMEM) 2>/dev/null; echo; fi |
|
366 |
|
|
367 |
sizeafter: |
|
368 |
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \ |
|
369 |
$(AVRMEM) 2>/dev/null; echo; fi |
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
# Display compiler version information. |
|
374 |
gccversion : |
|
375 |
@$(CC) --version |
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
# Program the device. |
|
380 |
program: $(TARGET).hex $(TARGET).eep |
|
381 |
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) |
|
382 |
|
|
383 |
|
|
384 |
# Generate avr-gdb config/init file which does the following: |
|
385 |
# define the reset signal, load the target file, connect to target, and set |
|
386 |
# a breakpoint at main(). |
|
387 |
gdb-config: |
|
388 |
@$(REMOVE) $(GDBINIT_FILE) |
|
389 |
@echo define reset >> $(GDBINIT_FILE) |
|
390 |
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE) |
|
391 |
@echo end >> $(GDBINIT_FILE) |
|
392 |
@echo file $(TARGET).elf >> $(GDBINIT_FILE) |
|
393 |
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE) |
|
394 |
ifeq ($(DEBUG_BACKEND),simulavr) |
|
395 |
@echo load >> $(GDBINIT_FILE) |
|
396 |
endif |
|
397 |
@echo break main >> $(GDBINIT_FILE) |
|
398 |
|
|
399 |
debug: gdb-config $(TARGET).elf |
|
400 |
ifeq ($(DEBUG_BACKEND), avarice) |
|
401 |
@echo Starting AVaRICE - Press enter when "waiting to connect" message displays. |
|
402 |
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \ |
|
403 |
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT) |
|
404 |
@$(WINSHELL) /c pause |
|
405 |
|
|
406 |
else |
|
407 |
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \ |
|
408 |
$(DEBUG_MFREQ) --port $(DEBUG_PORT) |
|
409 |
endif |
|
410 |
@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE) |
|
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
|
415 |
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. |
|
416 |
COFFCONVERT=$(OBJCOPY) --debugging \ |
|
417 |
--change-section-address .data-0x800000 \ |
|
418 |
--change-section-address .bss-0x800000 \ |
|
419 |
--change-section-address .noinit-0x800000 \ |
|
420 |
--change-section-address .eeprom-0x810000 |
|
421 |
|
|
422 |
|
|
423 |
coff: $(TARGET).elf |
|
424 |
@echo |
|
425 |
@echo $(MSG_COFF) $(TARGET).cof |
|
426 |
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof |
|
427 |
|
|
428 |
|
|
429 |
extcoff: $(TARGET).elf |
|
430 |
@echo |
|
431 |
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof |
|
432 |
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof |
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
# Create final output files (.hex, .eep) from ELF output file. |
|
437 |
%.hex: %.elf |
|
438 |
@echo |
|
439 |
@echo $(MSG_FLASH) $@ |
|
440 |
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ |
|
441 |
|
|
442 |
%.eep: %.elf |
|
443 |
@echo |
|
444 |
@echo $(MSG_EEPROM) $@ |
|
445 |
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ |
|
446 |
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@ |
|
447 |
|
|
448 |
# Create extended listing file from ELF output file. |
|
449 |
%.lss: %.elf |
|
450 |
@echo |
|
451 |
@echo $(MSG_EXTENDED_LISTING) $@ |
|
452 |
$(OBJDUMP) -h -S $< > $@ |
|
453 |
|
|
454 |
# Create a symbol table from ELF output file. |
|
455 |
%.sym: %.elf |
|
456 |
@echo |
|
457 |
@echo $(MSG_SYMBOL_TABLE) $@ |
|
458 |
$(NM) -n $< > $@ |
|
459 |
|
|
460 |
|
|
461 |
|
|
462 |
# Link: create ELF output file from object files. |
|
463 |
.SECONDARY : $(TARGET).elf |
|
464 |
.PRECIOUS : $(OBJ) |
|
465 |
%.elf: $(OBJ) |
|
466 |
@echo |
|
467 |
@echo $(MSG_LINKING) $@ |
|
468 |
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) |
|
469 |
|
|
470 |
|
|
471 |
# Compile: create object files from C source files. |
|
472 |
%.o : %.c |
|
473 |
@echo |
|
474 |
@echo $(MSG_COMPILING) $< |
|
475 |
$(CC) -c $(ALL_CFLAGS) $< -o $@ |
|
476 |
|
|
477 |
|
|
478 |
# Compile: create assembler files from C source files. |
|
479 |
%.s : %.c |
|
480 |
$(CC) -S $(ALL_CFLAGS) $< -o $@ |
|
481 |
|
|
482 |
|
|
483 |
# Assemble: create object files from assembler source files. |
|
484 |
%.o : %.S |
|
485 |
@echo |
|
486 |
@echo $(MSG_ASSEMBLING) $< |
|
487 |
$(CC) -c $(ALL_ASFLAGS) $< -o $@ |
|
488 |
|
|
489 |
# Create preprocessed source for use in sending a bug report. |
|
490 |
%.i : %.c |
|
491 |
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ |
|
492 |
|
|
493 |
|
|
494 |
# Target: clean project. |
|
495 |
clean: begin clean_list end |
|
496 |
|
|
497 |
clean_list : |
|
498 |
@echo |
|
499 |
@echo $(MSG_CLEANING) |
|
500 |
$(REMOVE) $(TARGET).hex |
|
501 |
$(REMOVE) $(TARGET).eep |
|
502 |
$(REMOVE) $(TARGET).cof |
|
503 |
$(REMOVE) $(TARGET).elf |
|
504 |
$(REMOVE) $(TARGET).map |
|
505 |
$(REMOVE) $(TARGET).sym |
|
506 |
$(REMOVE) $(TARGET).lss |
|
507 |
$(REMOVE) $(OBJ) |
|
508 |
$(REMOVE) $(LST) |
|
509 |
$(REMOVE) $(SRC:.c=.s) |
|
510 |
$(REMOVE) $(SRC:.c=.d) |
|
511 |
$(REMOVEDIR) .dep |
|
512 |
|
|
513 |
|
|
514 |
|
|
515 |
# Include the dependency files. |
|
516 |
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) |
|
517 |
|
|
518 |
|
|
519 |
# Listing of phony targets. |
|
520 |
.PHONY : all begin finish end sizebefore sizeafter gccversion \ |
|
521 |
build elf hex eep lss sym coff extcoff \ |
|
522 |
clean clean_list program debug gdb-config |
|
523 |
|
trunk/code/projects/libwireless/jni/lib/handlers.c | ||
---|---|---|
1 |
void callUnregister(int num); |
|
2 |
void callHandleReceived(int num, char type, int source, unsigned char *packet, int length); |
|
3 |
void callHandleResponse(int num, int frame, int received); |
|
4 |
void callTimeout(int num); |
|
5 |
|
|
6 |
#define HANDLER(num) void jni_timeout_handler##num() \ |
|
7 |
{ \ |
|
8 |
callTimeout(num); \ |
|
9 |
} \ |
|
10 |
void jni_handle_response##num(int frame, int received) \ |
|
11 |
{ \ |
|
12 |
callHandleResponse(num, frame, received); \ |
|
13 |
} \ |
|
14 |
void jni_handle_receive##num(char type, int source, unsigned char* packet, int length) \ |
|
15 |
{ \ |
|
16 |
callHandleReceived(num, type, source, packet, length); \ |
|
17 |
} \ |
|
18 |
void jni_unregister##num() \ |
|
19 |
{ \ |
|
20 |
callUnregister(num); \ |
|
21 |
} |
|
22 |
|
|
23 |
HANDLER(0) |
|
24 |
HANDLER(1) |
|
25 |
HANDLER(2) |
|
26 |
HANDLER(3) |
|
27 |
HANDLER(4) |
|
28 |
HANDLER(5) |
|
29 |
HANDLER(6) |
|
30 |
HANDLER(7) |
|
31 |
HANDLER(8) |
|
32 |
HANDLER(9) |
|
33 |
HANDLER(10) |
|
34 |
HANDLER(11) |
|
35 |
HANDLER(12) |
|
36 |
HANDLER(13) |
|
37 |
HANDLER(14) |
|
38 |
HANDLER(15) |
|
39 |
|
|
40 |
#define DEC_HANDLER(num) {num, jni_timeout_handler##num, jni_handle_response##num, jni_handle_receive##num, jni_unregister##num } |
|
41 |
|
|
42 |
PacketGroupHandler packet_groups[WL_MAX_PACKET_GROUPS] = { |
|
43 |
DEC_HANDLER(0), DEC_HANDLER(1), DEC_HANDLER(2), DEC_HANDLER(3), |
|
44 |
DEC_HANDLER(4), DEC_HANDLER(5), DEC_HANDLER(6), DEC_HANDLER(7), |
|
45 |
DEC_HANDLER(8), DEC_HANDLER(9), DEC_HANDLER(10),DEC_HANDLER(11), |
|
46 |
DEC_HANDLER(12),DEC_HANDLER(13),DEC_HANDLER(14),DEC_HANDLER(15) |
|
47 |
}; |
trunk/code/projects/libwireless/jni/lib/org_roboticsclub_colony_Wireless.c | ||
---|---|---|
1 |
#include "org_roboticsclub_colony_Wireless.h" |
|
2 |
#include "wireless.h" |
|
3 |
#include <string.h> |
|
4 |
|
|
5 |
/* Memory used to hold XBee port name */ |
|
6 |
char xbeeport[128]; |
|
7 |
|
|
8 |
/* Flag to track whether wl has been initialized */ |
|
9 |
static int _init = 0; |
|
10 |
|
|
11 |
JavaVM *jvm; |
|
12 |
|
|
13 |
#include "handlers.c" |
|
14 |
jobject handlers[WL_MAX_PACKET_GROUPS] = {0}; |
|
15 |
|
|
16 |
void callTimeout(int num) |
|
17 |
{ |
|
18 |
jobject pgh = handlers[num]; |
|
19 |
JNIEnv *env; |
|
20 |
int bAttached = 0; |
|
21 |
|
|
22 |
if (jvm==NULL) /* JVM wasn't set - there was an error */ |
|
23 |
return; |
|
24 |
|
|
25 |
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1)) |
|
26 |
{ |
|
27 |
/* Aren't attached to a JVM, so try to attach */ |
|
28 |
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL)) |
|
29 |
return; /* Error getting JNI Environment, can't continue */ |
|
30 |
else |
|
31 |
bAttached = 1; |
|
32 |
} |
|
33 |
|
|
34 |
jclass cls = (*env)->GetObjectClass(env, pgh); |
|
35 |
jmethodID mid = (*env)->GetMethodID(env, cls, "timeout_handler", "()V"); |
|
36 |
if (mid == NULL) |
|
37 |
return; /* method not found */ |
|
38 |
(*env)->CallVoidMethod(env, pgh, mid); |
|
39 |
|
|
40 |
if (bAttached) |
|
41 |
(*jvm)->DetachCurrentThread(jvm); |
|
42 |
} |
|
43 |
void callHandleResponse(int num, int frame, int received) |
|
44 |
{ |
|
45 |
jobject pgh = handlers[num]; |
|
46 |
JNIEnv *env; |
|
47 |
int bAttached = 0; |
|
48 |
|
|
49 |
if (jvm==NULL) /* JVM wasn't set - there was an error */ |
|
50 |
return; |
|
51 |
|
|
52 |
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1)) |
|
53 |
{ |
|
54 |
/* Aren't attached to a JVM, so try to attach */ |
|
55 |
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL)) |
|
56 |
return; /* Error getting JNI Environment, can't continue */ |
|
57 |
else |
|
58 |
bAttached = 1; |
|
59 |
} |
|
60 |
|
|
61 |
jclass cls = (*env)->GetObjectClass(env, pgh); |
|
62 |
jmethodID mid = (*env)->GetMethodID(env, cls, "handle_response", "(II)V"); |
|
63 |
if (mid == NULL) |
|
64 |
return; /* method not found */ |
|
65 |
(*env)->CallVoidMethod(env, pgh, mid, (jint)frame, (jint)received); |
|
66 |
|
|
67 |
if (bAttached) |
|
68 |
(*jvm)->DetachCurrentThread(jvm); |
|
69 |
} |
|
70 |
void callHandleReceived(int num, char type, int source, unsigned char *packet, int length) |
|
71 |
{ |
|
72 |
jobject pgh = handlers[num]; |
|
73 |
JNIEnv *env; |
|
74 |
int bAttached = 0; |
|
75 |
|
|
76 |
if (jvm==NULL) /* JVM wasn't set - there was an error */ |
|
77 |
return; |
|
78 |
|
|
79 |
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1)) |
|
80 |
{ |
|
81 |
/* Aren't attached to a JVM, so try to attach */ |
|
82 |
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL)) |
|
83 |
return; /* Error getting JNI Environment, can't continue */ |
|
84 |
else |
|
85 |
bAttached = 1; |
|
86 |
} |
|
87 |
|
|
88 |
jclass cls = (*env)->GetObjectClass(env, pgh); |
|
89 |
jmethodID mid = (*env)->GetMethodID(env, cls, "handle_received", "(BI[B)V"); |
|
90 |
if (mid == NULL) |
|
91 |
return; /* method not found */ |
|
92 |
|
|
93 |
jbyteArray arr = (*env)->NewByteArray(env, length); |
|
94 |
(*env)->SetByteArrayRegion(env, arr, 0, length, (jbyte *)packet); |
|
95 |
|
|
96 |
(*env)->CallVoidMethod(env, pgh, mid, (jbyte)type, (jint)source, packet); |
|
97 |
|
|
98 |
if (bAttached) |
|
99 |
(*jvm)->DetachCurrentThread(jvm); |
|
100 |
} |
|
101 |
void callUnregister(int num) |
|
102 |
{ |
|
103 |
jobject pgh = handlers[num]; |
|
104 |
JNIEnv *env; |
|
105 |
int bAttached = 0; |
|
106 |
|
|
107 |
if (jvm==NULL) /* JVM wasn't set - there was an error */ |
|
108 |
return; |
|
109 |
|
|
110 |
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_1)) |
|
111 |
{ |
|
112 |
/* Aren't attached to a JVM, so try to attach */ |
|
113 |
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL)) |
|
114 |
return; /* Error getting JNI Environment, can't continue */ |
|
115 |
else |
|
116 |
bAttached = 1; |
|
117 |
} |
|
118 |
|
|
119 |
jclass cls = (*env)->GetObjectClass(env, pgh); |
|
120 |
jmethodID mid = (*env)->GetMethodID(env, cls, "unregister", "()V"); |
|
121 |
if (mid == NULL) |
|
122 |
return; /* method not found */ |
|
123 |
(*env)->CallVoidMethod(env, pgh, mid); |
|
124 |
|
|
125 |
(*env)->DeleteGlobalRef(env, pgh); |
|
126 |
|
|
127 |
if (bAttached) |
|
128 |
(*jvm)->DetachCurrentThread(jvm); |
|
129 |
} |
|
130 |
|
|
131 |
/* |
|
132 |
* Class: org_roboticsclub_colony_Wireless |
|
133 |
* Method: wl_init |
|
134 |
* Signature: ()I |
|
135 |
*/ |
|
136 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1init |
|
137 |
(JNIEnv *env, jclass class) |
|
138 |
{ |
|
139 |
int ret; |
|
140 |
|
|
141 |
//init_pga(); |
|
142 |
|
|
143 |
(*env)->GetJavaVM(env, &jvm); |
|
144 |
|
|
145 |
ret = wl_init(); |
|
146 |
|
|
147 |
if (!ret) |
|
148 |
_init = 1; |
|
149 |
|
|
150 |
return ret; |
|
151 |
} |
|
152 |
|
|
153 |
/* |
|
154 |
* Class: org_roboticsclub_colony_Wireless |
|
155 |
* Method: wl_terminate |
|
156 |
* Signature: ()V |
|
157 |
*/ |
|
158 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1terminate |
|
159 |
(JNIEnv *env, jclass class) |
|
160 |
{ |
|
161 |
if (_init) |
|
162 |
wl_terminate(); |
|
163 |
} |
|
164 |
|
|
165 |
/* |
|
166 |
* Class: org_roboticsclub_colony_Wireless |
|
167 |
* Method: wl_do |
|
168 |
* Signature: ()V |
|
169 |
*/ |
|
170 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1do |
|
171 |
(JNIEnv *env, jclass class) |
|
172 |
{ |
|
173 |
wl_do(); |
|
174 |
} |
|
175 |
|
|
176 |
/* |
|
177 |
* Class: org_roboticsclub_colony_Wireless |
|
178 |
* Method: wl_register_packet_group |
|
179 |
* Signature: (Lorg/roboticsclub/colony/Wireless/PacketGroupHandler;)V |
|
180 |
*/ |
|
181 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1register_1packet_1group |
|
182 |
(JNIEnv *env, jclass class, jobject pgh) |
|
183 |
{ |
|
184 |
jint groupId; |
|
185 |
jobject gPgh; |
|
186 |
|
|
187 |
jclass cls = (*env)->GetObjectClass(env, pgh); |
|
188 |
jmethodID mid = (*env)->GetMethodID(env, cls, "getGroupCode", "()I"); |
|
189 |
if (mid == NULL) |
|
190 |
return; /* method not found */ |
|
191 |
groupId = (*env)->CallIntMethod(env, pgh, mid); |
|
192 |
|
|
193 |
if (groupId > 15 || groupId < 0) |
|
194 |
return; |
|
195 |
|
|
196 |
gPgh = (*env)->NewGlobalRef(env, pgh); |
|
197 |
handlers[groupId] = gPgh; |
|
198 |
|
|
199 |
wl_register_packet_group(&packet_groups[groupId]); |
|
200 |
} |
|
201 |
|
|
202 |
/* |
|
203 |
* Class: org_roboticsclub_colony_Wireless |
|
204 |
* Method: wl_unregister_packet_group |
|
205 |
* Signature: (Lorg/roboticsclub/colony/Wireless/PacketGroupHandler;)V |
|
206 |
*/ |
|
207 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1unregister_1packet_1group |
|
208 |
(JNIEnv *env, jclass class, jobject pgh) |
|
209 |
{ |
|
210 |
jint groupId; |
|
211 |
|
|
212 |
jclass cls = (*env)->GetObjectClass(env, pgh); |
|
213 |
jmethodID mid = (*env)->GetMethodID(env, cls, "getGroupCode", "()I"); |
|
214 |
if (mid == NULL) |
|
215 |
return; /* method not found */ |
|
216 |
groupId = (*env)->CallIntMethod(env, pgh, mid); |
|
217 |
|
|
218 |
if (groupId < 0 || groupId > 15) |
|
219 |
return; |
|
220 |
|
|
221 |
wl_unregister_packet_group(&packet_groups[groupId]); |
|
222 |
} |
|
223 |
|
|
224 |
/* |
|
225 |
* Class: org_roboticsclub_colony_Wireless |
|
226 |
* Method: wl_send_robot_to_robot_global_packet |
|
227 |
* Signature: (BB[BIB)I |
|
228 |
*/ |
|
229 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1robot_1to_1robot_1global_1packet |
|
230 |
(JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jint dest, jbyte frame) |
|
231 |
{ |
|
232 |
jbyte buf[256]; |
|
233 |
jsize length = (*env)->GetArrayLength(env, data); |
|
234 |
(*env)->GetByteArrayRegion(env, data, 0, 256, buf); |
|
235 |
return wl_send_robot_to_robot_global_packet(group, type, (char *)buf, length, (int)dest, frame); |
|
236 |
} |
|
237 |
|
|
238 |
/* |
|
239 |
* Class: org_roboticsclub_colony_Wireless |
|
240 |
* Method: wl_send_robot_to_robot_packet |
|
241 |
* Signature: (BB[BIB)I |
|
242 |
*/ |
|
243 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1robot_1to_1robot_1packet |
|
244 |
(JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jint dest, jbyte frame) |
|
245 |
{ |
|
246 |
jbyte buf[256]; |
|
247 |
jsize length = (*env)->GetArrayLength(env, data); |
|
248 |
(*env)->GetByteArrayRegion(env, data, 0, 256, buf); |
|
249 |
return wl_send_robot_to_robot_packet(group, type, (char *)buf, length, (int)dest, frame); |
|
250 |
} |
|
251 |
|
|
252 |
/* |
|
253 |
* Class: org_roboticsclub_colony_Wireless |
|
254 |
* Method: wl_send_global_packet |
|
255 |
* Signature: (BB[BB)I |
|
256 |
*/ |
|
257 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1global_1packet |
|
258 |
(JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jbyte frame) |
|
259 |
{ |
|
260 |
jbyte buf[256]; |
|
261 |
jsize length = (*env)->GetArrayLength(env, data); |
|
262 |
(*env)->GetByteArrayRegion(env, data, 0, 256, buf); |
|
263 |
return wl_send_global_packet(group, type, (char *)buf, length, frame); |
|
264 |
} |
|
265 |
|
|
266 |
/* |
|
267 |
* Class: org_roboticsclub_colony_Wireless |
|
268 |
* Method: wl_send_pan_packet |
|
269 |
* Signature: (BB[BB)V |
|
270 |
*/ |
|
271 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1pan_1packet |
|
272 |
(JNIEnv *env, jclass class, jbyte group, jbyte type, jbyteArray data, jbyte frame) |
|
273 |
{ |
|
274 |
jbyte buf[256]; |
|
275 |
jsize length = (*env)->GetArrayLength(env, data); |
|
276 |
(*env)->GetByteArrayRegion(env, data, 0, 256, buf); |
|
277 |
wl_send_pan_packet(group, type, (char *)buf, length, frame); |
|
278 |
} |
|
279 |
|
|
280 |
/* |
|
281 |
* Class: org_roboticsclub_colony_Wireless |
|
282 |
* Method: wl_set_pan |
|
283 |
* Signature: (I)I |
|
284 |
*/ |
|
285 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1set_1pan |
|
286 |
(JNIEnv *env, jclass class, jint pan) |
|
287 |
{ |
|
288 |
return wl_set_pan((int)pan); |
|
289 |
} |
|
290 |
|
|
291 |
/* |
|
292 |
* Class: org_roboticsclub_colony_Wireless |
|
293 |
* Method: wl_get_pan |
|
294 |
* Signature: ()I |
|
295 |
*/ |
|
296 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1get_1pan |
|
297 |
(JNIEnv *env, jclass class) |
|
298 |
{ |
|
299 |
return wl_get_pan(); |
|
300 |
} |
|
301 |
|
|
302 |
/* |
|
303 |
* Class: org_roboticsclub_colony_Wireless |
|
304 |
* Method: wl_set_channel |
|
305 |
* Signature: (I)I |
|
306 |
*/ |
|
307 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1set_1channel |
|
308 |
(JNIEnv *env, jclass class, jint channel) |
|
309 |
{ |
|
310 |
return wl_set_channel((int)channel); |
|
311 |
} |
|
312 |
|
|
313 |
/* |
|
314 |
* Class: org_roboticsclub_colony_Wireless |
|
315 |
* Method: wl_get_channel |
|
316 |
* Signature: ()I |
|
317 |
*/ |
|
318 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1get_1channel |
|
319 |
(JNIEnv *env, jclass class) |
|
320 |
{ |
|
321 |
return wl_get_channel(); |
|
322 |
} |
|
323 |
|
|
324 |
/* |
|
325 |
* Class: org_roboticsclub_colony_Wireless |
|
326 |
* Method: wl_get_xbee_id |
|
327 |
* Signature: ()I |
|
328 |
*/ |
|
329 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1get_1xbee_1id |
|
330 |
(JNIEnv *env, jclass class) |
|
331 |
{ |
|
332 |
return wl_get_xbee_id(); |
|
333 |
} |
|
334 |
|
|
335 |
/* |
|
336 |
* Class: org_roboticsclub_colony_Wireless |
|
337 |
* Method: wl_set_com_port |
|
338 |
* Signature: (Ljava/lang/String;)V |
|
339 |
*/ |
|
340 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1set_1com_1port |
|
341 |
(JNIEnv *env, jclass class, jstring port) |
|
342 |
{ |
|
343 |
const jbyte *str = (*env)->GetStringUTFChars(env, port, NULL); |
|
344 |
if (str == NULL) |
|
345 |
return; /* OutOfMemoryError already thrown */ |
|
346 |
|
|
347 |
strcpy(xbeeport, (const char *)str); |
|
348 |
|
|
349 |
(*env)->ReleaseStringUTFChars(env, port, str); |
|
350 |
|
|
351 |
wl_set_com_port(xbeeport); |
|
352 |
} |
|
353 |
|
trunk/code/projects/libwireless/jni/lib/org_roboticsclub_colony_TokenRing.c | ||
---|---|---|
1 |
#include "org_roboticsclub_colony_TokenRing.h" |
|
2 |
#include "wl_token_ring.h" |
|
3 |
|
|
4 |
/* |
|
5 |
* Class: org_roboticsclub_colony_TokenRing |
|
6 |
* Method: wl_token_ring_register |
|
7 |
* Signature: ()I |
|
8 |
*/ |
|
9 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1ring_1register |
|
10 |
(JNIEnv *env, jclass class) |
|
11 |
{ |
|
12 |
return wl_token_ring_register(); |
|
13 |
} |
|
14 |
|
|
15 |
/* |
|
16 |
* Class: org_roboticsclub_colony_TokenRing |
|
17 |
* Method: wl_token_ring_unregister |
|
18 |
* Signature: ()V |
|
19 |
*/ |
|
20 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1ring_1unregister |
|
21 |
(JNIEnv *env, jclass class) |
|
22 |
{ |
|
23 |
wl_token_ring_register(); |
|
24 |
} |
|
25 |
|
|
26 |
/* |
|
27 |
* Class: org_roboticsclub_colony_TokenRing |
|
28 |
* Method: wl_token_ring_join |
|
29 |
* Signature: ()I |
|
30 |
*/ |
|
31 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1ring_1join |
|
32 |
(JNIEnv *env, jclass class) |
|
33 |
{ |
|
34 |
return wl_token_ring_join(); |
|
35 |
} |
|
36 |
|
|
37 |
/* |
|
38 |
* Class: org_roboticsclub_colony_TokenRing |
|
39 |
* Method: wl_token_ring_leave |
|
40 |
* Signature: ()V |
|
41 |
*/ |
|
42 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1ring_1leave |
|
43 |
(JNIEnv *env, jclass class) |
|
44 |
{ |
|
45 |
wl_token_ring_leave(); |
|
46 |
} |
|
47 |
|
|
48 |
/* |
|
49 |
* Class: org_roboticsclub_colony_TokenRing |
|
50 |
* Method: wl_token_get_robots_in_ring |
|
51 |
* Signature: ()I |
|
52 |
*/ |
|
53 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1get_1robots_1in_1ring |
|
54 |
(JNIEnv *env, jclass class) |
|
55 |
{ |
|
56 |
return wl_token_get_robots_in_ring(); |
|
57 |
} |
|
58 |
|
|
59 |
/* |
|
60 |
* Class: org_roboticsclub_colony_TokenRing |
|
61 |
* Method: wl_token_is_robot_in_ring |
|
62 |
* Signature: (I)I |
|
63 |
*/ |
|
64 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1is_1robot_1in_1ring |
|
65 |
(JNIEnv *env, jclass class, jint robot) |
|
66 |
{ |
|
67 |
return wl_token_is_robot_in_ring((int)robot); |
|
68 |
} |
|
69 |
|
|
70 |
/* |
|
71 |
* Class: org_roboticsclub_colony_TokenRing |
|
72 |
* Method: wl_token_iterator_begin |
|
73 |
* Signature: ()V |
|
74 |
*/ |
|
75 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1iterator_1begin |
|
76 |
(JNIEnv *env, jclass class) |
|
77 |
{ |
|
78 |
wl_token_iterator_begin(); |
|
79 |
} |
|
80 |
|
|
81 |
/* |
|
82 |
* Class: org_roboticsclub_colony_TokenRing |
|
83 |
* Method: wl_token_iterator_has_next |
|
84 |
* Signature: ()I |
|
85 |
*/ |
|
86 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1iterator_1has_1next |
|
87 |
(JNIEnv *env, jclass class) |
|
88 |
{ |
|
89 |
return wl_token_iterator_has_next(); |
|
90 |
} |
|
91 |
|
|
92 |
/* |
|
93 |
* Class: org_roboticsclub_colony_TokenRing |
|
94 |
* Method: wl_token_iterator_next |
|
95 |
* Signature: ()I |
|
96 |
*/ |
|
97 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1iterator_1next |
|
98 |
(JNIEnv *env, jclass class) |
|
99 |
{ |
|
100 |
return wl_token_iterator_next(); |
|
101 |
} |
|
102 |
|
|
103 |
/* |
|
104 |
* Class: org_roboticsclub_colony_TokenRing |
|
105 |
* Method: wl_token_get_sensor_reading |
|
106 |
* Signature: (II)I |
|
107 |
*/ |
|
108 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1get_1sensor_1reading |
|
109 |
(JNIEnv *env, jclass class, jint source, jint dest) |
|
110 |
{ |
|
111 |
return wl_token_get_sensor_reading((int)source, (int)dest); |
|
112 |
} |
|
113 |
|
|
114 |
/* |
|
115 |
* Class: org_roboticsclub_colony_TokenRing |
|
116 |
* Method: wl_token_get_my_sensor_reading |
|
117 |
* Signature: (I)I |
|
118 |
*/ |
|
119 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1get_1my_1sensor_1reading |
|
120 |
(JNIEnv *env, jclass class, jint dest) |
|
121 |
{ |
|
122 |
return wl_token_get_my_sensor_reading(dest); |
|
123 |
} |
|
124 |
|
|
125 |
/* |
|
126 |
* Class: org_roboticsclub_colony_TokenRing |
|
127 |
* Method: wl_token_get_num_robots |
|
128 |
* Signature: ()I |
|
129 |
*/ |
|
130 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1get_1num_1robots |
|
131 |
(JNIEnv *env, jclass class) |
|
132 |
{ |
|
133 |
return wl_token_get_num_robots(); |
|
134 |
} |
|
135 |
|
|
136 |
/* |
|
137 |
* Class: org_roboticsclub_colony_TokenRing |
|
138 |
* Method: wl_token_get_matrix_size |
|
139 |
* Signature: ()I |
|
140 |
*/ |
|
141 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_TokenRing_wl_1token_1get_1matrix_1size |
|
142 |
(JNIEnv *env, jclass class) |
|
143 |
{ |
|
144 |
return wl_token_get_matrix_size(); |
|
145 |
} |
|
146 |
|
trunk/code/projects/libwireless/jni/lib/org/roboticsclub/colony/TokenRing.java | ||
---|---|---|
1 |
package org.roboticsclub.colony; |
|
2 |
|
|
3 |
public class TokenRing |
|
4 |
{ |
|
5 |
public static native int wl_token_ring_register(); |
|
6 |
public static native void wl_token_ring_unregister(); |
|
7 |
public static native int wl_token_ring_join(); |
|
8 |
public static native void wl_token_ring_leave(); |
|
9 |
public static native int wl_token_get_robots_in_ring(); |
|
10 |
public static native int wl_token_is_robot_in_ring(int robot); |
|
11 |
public static native void wl_token_iterator_begin(); |
|
12 |
public static native int wl_token_iterator_has_next(); |
|
13 |
public static native int wl_token_iterator_next(); |
|
14 |
public static native int wl_token_get_sensor_reading(int source, int dest); |
|
15 |
public static native int wl_token_get_my_sensor_reading(int dest); |
|
16 |
public static native int wl_token_get_num_robots(); |
|
17 |
public static native int wl_token_get_matrix_size(); |
|
18 |
} |
trunk/code/projects/libwireless/jni/lib/org/roboticsclub/colony/Wireless.java | ||
---|---|---|
1 |
package org.roboticsclub.colony; |
|
2 |
|
|
3 |
import java.util.Timer; |
|
4 |
import java.util.TimerTask; |
|
5 |
|
|
6 |
public class Wireless |
|
7 |
{ |
|
8 |
public static native int wl_init(); |
|
9 |
public static native void wl_terminate(); |
|
10 |
public static native void wl_do(); |
|
11 |
public static native void wl_register_packet_group(PacketGroupHandler pgh); |
|
12 |
public static native void wl_unregister_packet_group(PacketGroupHandler pgh); |
|
13 |
public static native int wl_send_robot_to_robot_global_packet(byte group, byte type, byte[] data, int dest, byte frame); |
|
14 |
public static native int wl_send_robot_to_robot_packet(byte group, byte type, byte[] data, int dest, byte frame); |
|
15 |
public static native int wl_send_global_packet(byte group, byte type, byte[] data, byte frame); |
|
16 |
public static native void wl_send_pan_packet(byte group, byte type, byte[] data, byte frame); |
|
17 |
public static native int wl_set_pan(int pan); |
|
18 |
public static native int wl_get_pan(); |
|
19 |
public static native int wl_set_channel(int channel); |
|
20 |
public static native int wl_get_channel(); |
|
21 |
public static native int wl_get_xbee_id(); |
|
22 |
public static native void wl_set_com_port(String port); |
|
23 |
|
|
24 |
public static TimerTask wireless_thread; |
|
25 |
|
|
26 |
public static void startWirelessThread(int rate) |
|
27 |
{ |
|
28 |
if (wireless_thread==null) |
|
29 |
{ |
|
30 |
wireless_thread = new TimerTask() { |
|
31 |
public void run() |
|
32 |
{ |
|
33 |
wl_do(); |
|
34 |
} |
|
35 |
}; |
|
36 |
} |
|
37 |
|
|
38 |
new Timer("Colony Wireless Monitor", true).scheduleAtFixedRate(wireless_thread, 0, rate); |
|
39 |
} |
|
40 |
|
|
41 |
static |
|
42 |
{ |
|
43 |
Runtime.getRuntime().addShutdownHook(new Thread() { |
|
44 |
public void run() |
|
45 |
{ |
|
46 |
wl_terminate(); |
|
47 |
System.out.println("Wireless terminated"); |
|
48 |
} |
|
49 |
}); |
|
50 |
} |
|
51 |
|
|
52 |
static |
|
53 |
{ |
|
54 |
System.loadLibrary("jniwireless"); |
|
55 |
} |
|
56 |
|
|
57 |
public static interface PacketGroupHandler |
|
58 |
{ |
|
59 |
public abstract int getGroupCode(); |
|
60 |
public abstract void timeout_handler(); |
|
61 |
public abstract void handle_response(int frame, int received); |
|
62 |
public abstract void handle_received(byte type, int source, byte[] packet); |
|
63 |
public abstract void unregister(); |
|
64 |
} |
|
65 |
} |
trunk/code/projects/libwireless/jni/lib/org_roboticsclub_colony_Wireless.h | ||
---|---|---|
1 |
/* DO NOT EDIT THIS FILE - it is machine generated */ |
|
2 |
#include <jni.h> |
|
3 |
/* Header for class org_roboticsclub_colony_Wireless */ |
|
4 |
|
|
5 |
#ifndef _Included_org_roboticsclub_colony_Wireless |
|
6 |
#define _Included_org_roboticsclub_colony_Wireless |
|
7 |
#ifdef __cplusplus |
|
8 |
extern "C" { |
|
9 |
#endif |
|
10 |
/* |
|
11 |
* Class: org_roboticsclub_colony_Wireless |
|
12 |
* Method: wl_init |
|
13 |
* Signature: ()I |
|
14 |
*/ |
|
15 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1init |
|
16 |
(JNIEnv *, jclass); |
|
17 |
|
|
18 |
/* |
|
19 |
* Class: org_roboticsclub_colony_Wireless |
|
20 |
* Method: wl_terminate |
|
21 |
* Signature: ()V |
|
22 |
*/ |
|
23 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1terminate |
|
24 |
(JNIEnv *, jclass); |
|
25 |
|
|
26 |
/* |
|
27 |
* Class: org_roboticsclub_colony_Wireless |
|
28 |
* Method: wl_do |
|
29 |
* Signature: ()V |
|
30 |
*/ |
|
31 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1do |
|
32 |
(JNIEnv *, jclass); |
|
33 |
|
|
34 |
/* |
|
35 |
* Class: org_roboticsclub_colony_Wireless |
|
36 |
* Method: wl_register_packet_group |
|
37 |
* Signature: (Lorg/roboticsclub/colony/Wireless/PacketGroupHandler;)V |
|
38 |
*/ |
|
39 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1register_1packet_1group |
|
40 |
(JNIEnv *, jclass, jobject); |
|
41 |
|
|
42 |
/* |
|
43 |
* Class: org_roboticsclub_colony_Wireless |
|
44 |
* Method: wl_unregister_packet_group |
|
45 |
* Signature: (Lorg/roboticsclub/colony/Wireless/PacketGroupHandler;)V |
|
46 |
*/ |
|
47 |
JNIEXPORT void JNICALL Java_org_roboticsclub_colony_Wireless_wl_1unregister_1packet_1group |
|
48 |
(JNIEnv *, jclass, jobject); |
|
49 |
|
|
50 |
/* |
|
51 |
* Class: org_roboticsclub_colony_Wireless |
|
52 |
* Method: wl_send_robot_to_robot_global_packet |
|
53 |
* Signature: (BB[BIB)I |
|
54 |
*/ |
|
55 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1robot_1to_1robot_1global_1packet |
|
56 |
(JNIEnv *, jclass, jbyte, jbyte, jbyteArray, jint, jbyte); |
|
57 |
|
|
58 |
/* |
|
59 |
* Class: org_roboticsclub_colony_Wireless |
|
60 |
* Method: wl_send_robot_to_robot_packet |
|
61 |
* Signature: (BB[BIB)I |
|
62 |
*/ |
|
63 |
JNIEXPORT jint JNICALL Java_org_roboticsclub_colony_Wireless_wl_1send_1robot_1to_1robot_1packet |
Also available in: Unified diff