OSDN Git Service

8c8530d99581e94092deab2f7bb9e6186b76a1ca
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / pccomms / lejos / pc / comm / NXTCommFantom.java
1 package lejos.pc.comm;
2
3 import java.io.*;
4
5 public class NXTCommFantom implements NXTComm {
6         private NXTInfo nxtInfo;
7         
8         public native String[] jfantom_find();
9         public native int jfantom_open(String nxt);
10         public native void jfantom_close(int nxt);
11         public native void jfantom_send_data(int nxt, byte [] message, int len, int replyLen);
12         public native byte[] jfantom_read_data(int nxt, int len);
13         
14         public NXTInfo[] search(String name, int protocol) {
15                 String[] nxtNames = jfantom_find();
16                 NXTInfo[] nxtInfo = new NXTInfo[nxtNames.length];
17                 for(int i=0;i<nxtNames.length;i++) {
18                         nxtInfo[i] = new NXTInfo();
19                         String nxtName = nxtNames[i];
20                         nxtInfo[i].btResourceString = nxtName;
21                         nxtInfo[i].name = "Unknown";
22                         nxtInfo[i].protocol = NXTCommFactory.USB;
23                         nxtInfo[i].btDeviceAddress = "";
24                         if (nxtName != null) {
25                             if (nxtName.length() >= 3 && nxtName.substring(0,3).equals("BTH"))
26                                 nxtInfo[i].protocol = NXTCommFactory.BLUETOOTH; 
27                             int startName = nxtName.indexOf("::");
28                             if (startName >= 0) startName +=2;
29                             int endName = -1;
30                             if (startName != -1) endName = nxtName.indexOf("::", startName);
31                             if (startName >= 0 && endName >= 0) {
32                                 nxtInfo[i].name = nxtName.substring(startName, endName);
33                             nxtInfo[i].btDeviceAddress = nxtName.substring(endName+2);
34                             }
35                         }
36                 }
37                 return nxtInfo;
38         }
39
40         public boolean open(NXTInfo nxtInfo) {
41                 this.nxtInfo = nxtInfo;
42                 nxtInfo.nxtPtr = jfantom_open(nxtInfo.btResourceString);
43                 return true;
44         }
45         
46         public void close() {
47                 jfantom_close(nxtInfo.nxtPtr);
48         }
49         
50         public byte [] sendRequest(byte [] data, int replyLen) {
51                 jfantom_send_data(nxtInfo.nxtPtr, data, data.length, replyLen-1);
52                 return jfantom_read_data(nxtInfo.nxtPtr, replyLen);
53         }
54         
55         public byte [] read() throws IOException {
56                 throw new IOException("Not implemented");
57         }
58         
59         public void write(byte [] data) throws IOException {
60                 throw new IOException("Not implemented");
61         }
62         
63         public OutputStream getOutputStream() {
64                 return null;            
65         }
66         
67         public InputStream getInputStream() {
68                 return null;            
69         }
70         
71         static {
72                 System.loadLibrary("jfantom");
73         }
74
75 }
76