Skip to content

Commit a3ede9e

Browse files
committed
load packet from phy now returniung packets rather then success code
1 parent e79cd9e commit a3ede9e

File tree

4 files changed

+100
-106
lines changed

4 files changed

+100
-106
lines changed

javasdk/NRSDK/addons/com/neuronrobotics/sdk/javaxusb/UsbCDCSerialConnection.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,11 @@ public void write(byte[] src) throws IOException {
590590
}
591591

592592
@Override
593-
public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer)
593+
public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer)
594594
throws NullPointerException, IOException {
595595

596596
if (dataInEndpoint == null)
597-
return false;
597+
return null;
598598
int got = 0;
599599
byte[] data = new byte[64];
600600
try {
@@ -628,22 +628,15 @@ public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer)
628628
| UsbException e) {
629629
e.printStackTrace();
630630
connect();
631-
return false;
631+
return null;
632632
}
633633
if (got > 0) {
634634
bytesToPacketBuffer.add(Arrays.copyOfRange(data, 0, got));
635-
BowlerDatagram bd = BowlerDatagramFactory
635+
return BowlerDatagramFactory
636636
.build(bytesToPacketBuffer);
637-
if (bd != null) {
638-
// Log.info("\nR<<"+bd);
639-
onDataReceived(bd);
640-
641-
// Packet found, break the loop and deal with it
642-
return true;
643-
}
644637
}
645638

646-
return false;
639+
return null;
647640
}
648641

649642
// /* (non-Javadoc)

javasdk/NRSDK/src/com/neuronrobotics/sdk/common/BowlerAbstractConnection.java

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -268,36 +268,7 @@ public long msSinceLastSend() {
268268
}
269269
return System.currentTimeMillis() - getLastWrite() ;
270270
}
271-
/**
272-
* Write.
273-
*
274-
* @param data the data
275-
* @throws IOException Signals that an I/O exception has occurred.
276-
*/
277-
//private ByteList outgoing = new ByteList();
278-
public void write(byte[] data) throws IOException {
279-
waitForConnectioToBeReady();
280-
setLastWrite(System.currentTimeMillis());
281-
if(dataOuts != null){
282-
try{
283-
//Log.info("Writing: "+data.length+" bytes");
284-
285-
//while(outgoing.size()>0){
286-
//byte[] b =outgoing.popList(getChunkSize());
287-
//System.out.println("Writing "+new ByteList(data));
288-
getDataOuts().write(data);
289-
getDataOuts().flush();
290-
//}
291-
}catch (Exception e){
292-
//e.printStackTrace();
293-
Log.error("Write failed. "+e.getMessage());
294-
//reconnect();
295-
}
296-
}else{
297-
Log.error("No data sent, stream closed");
298-
}
299-
300-
}
271+
301272

302273
/**
303274
* Sets the connected.
@@ -1154,25 +1125,25 @@ public void run() {
11541125
//throw new RuntimeException();
11551126
}
11561127

1157-
11581128
private boolean runPacketUpdate() {
11591129
try {
1160-
if(loadPacketFromPhy(bytesToPacketBuffer))
1161-
bytesToPacketBuffer.clear();
1130+
BowlerDatagram bd = loadPacketFromPhy(bytesToPacketBuffer);
1131+
if(bd!=null){
1132+
Log.info("\nR<<"+bd);
1133+
onDataReceived(bd);
1134+
bytesToPacketBuffer=new ByteList();
1135+
}
11621136
} catch (Exception e) {
1137+
e.printStackTrace();
11631138
if(isConnected()){
11641139
Log.error("Data read failed "+e.getMessage());
11651140
e.printStackTrace();
1166-
// try {
1167-
// reconnect();
1168-
// } catch (IOException e1) {
1169-
// // TODO Auto-generated catch block
1170-
// e1.printStackTrace();
1171-
// }
1141+
11721142
}
11731143
}
11741144
return false;
11751145
}
1146+
11761147

11771148
/**
11781149
* Adds the datagram.
@@ -1192,43 +1163,6 @@ public void kill() {
11921163
}
11931164
}
11941165

1195-
1196-
public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
1197-
if(dataIns!=null){
1198-
if(getDataIns().available()==0)
1199-
return false;
1200-
byte [] buff = new byte[getDataIns().available()];
1201-
//we want to run this until the buffer is clear or a packet is found
1202-
int b = getDataIns().read(buff);
1203-
1204-
if(b<0 || buff.length != b){
1205-
Log.error("Stream is broken - unexpected: claimed to have "+getDataIns().available()+" bytes, read in "+b);
1206-
//reconnect();
1207-
//something went wrong
1208-
new RuntimeException(" Buffer attempted to read "+buff.length+" got "+b).printStackTrace();
1209-
return false;
1210-
}else{
1211-
bytesToPacketBuffer.add(buff);
1212-
BowlerDatagram bd=null;
1213-
try{
1214-
bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
1215-
}catch(Exception ex){
1216-
ex.printStackTrace();
1217-
}
1218-
if (bd!=null) {
1219-
Log.info("\nR<<"+bd);
1220-
onDataReceived(bd);
1221-
return true;
1222-
}
1223-
}
1224-
//ThreadUtil.wait(1);
1225-
1226-
}else{
1227-
Log.error("Data In is null");
1228-
}
1229-
return false;
1230-
}
1231-
12321166
public static boolean isUseThreadedStack() {
12331167
return useThreadedStack;
12341168
}
@@ -1245,5 +1179,77 @@ public void setBeater(boolean beater) {
12451179
this.beater = beater;
12461180
}
12471181

1182+
public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
1183+
BowlerDatagram bd=null;
1184+
if(dataIns!=null){
1185+
byte [] buff = new byte[getDataIns().available()];
1186+
if(buff.length==0)
1187+
return null;
1188+
int b,ret =0;
1189+
1190+
try{
1191+
for(b=0;b<buff.length;b++){
1192+
ret = getDataIns().read();
1193+
if(ret<0){
1194+
Log.error("Stream is broken - unexpected: claimed to have "+getDataIns().available()+" bytes, read in "+b);
1195+
//reconnect();
1196+
//something went wrong
1197+
new RuntimeException(" Buffer attempted to read "+buff.length+" got "+b).printStackTrace();
1198+
return null;
1199+
}else{
1200+
bytesToPacketBuffer.add(ret);
1201+
if(bd==null)
1202+
try{
1203+
bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
1204+
}catch(Exception ex){
1205+
ex.printStackTrace();
1206+
}
1207+
}
1208+
1209+
}
1210+
}catch(Exception e){
1211+
e.printStackTrace();
1212+
}
1213+
//ThreadUtil.wait(1);
1214+
1215+
}else{
1216+
Log.error("Data In is null");
1217+
}
1218+
return bd;
1219+
}
1220+
1221+
/**
1222+
* Write.
1223+
*
1224+
* @param data the data
1225+
* @throws IOException Signals that an I/O exception has occurred.
1226+
*/
1227+
//private ByteList outgoing = new ByteList();
1228+
public void write(byte[] data) throws IOException {
1229+
waitForConnectioToBeReady();
1230+
setLastWrite(System.currentTimeMillis());
1231+
if(dataOuts != null){
1232+
try{
1233+
//Log.info("Writing: "+data.length+" bytes");
1234+
ByteList outgoing = new ByteList(data);
1235+
1236+
while(outgoing.size()>0){
1237+
byte[] b =outgoing.popList(getChunkSize());
1238+
//System.out.println("Writing "+new ByteList(data));
1239+
getDataOuts().write( b );
1240+
getDataOuts().flush();
1241+
}
1242+
}catch (Exception e){
1243+
//e.printStackTrace();
1244+
Log.error("Write failed. "+e.getMessage());
1245+
//reconnect();
1246+
}
1247+
}else{
1248+
Log.error("No data sent, stream closed");
1249+
}
1250+
1251+
}
1252+
1253+
12481254

12491255
}

javasdk/NRSDK/src/com/neuronrobotics/sdk/network/BowlerUDPServer.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void write(byte[] data) throws IOException {
152152
}
153153

154154
@Override
155-
public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
155+
public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
156156
byte[] receiveData=new byte[4096];
157157

158158
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
@@ -163,7 +163,7 @@ public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointe
163163
}catch(SocketException ex){
164164
// disconnect called
165165
Log. warning("Receive bailed out because of close");
166-
return false;
166+
return null;
167167
}
168168

169169
IPAddressSet=(receivePacket.getAddress());
@@ -176,19 +176,16 @@ public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointe
176176
for (int i=0;i<receivePacket.getLength();i++){
177177
internalReceiveBuffer.add(data[i]);
178178
}
179+
BowlerDatagram bd =null;
179180

180181
while(internalReceiveBuffer.size()>0){
181182
bytesToPacketBuffer.add(internalReceiveBuffer.pop());
182-
BowlerDatagram bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
183-
if (bd!=null) {
184-
Log.info("\nR<<"+bd);
185-
onDataReceived(bd);
186-
187-
return true;
183+
if (bd==null) {
184+
bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
188185
}
189186
}
190187

191-
return false;
188+
return bd;
192189
}
193190

194191

javasdk/NRSDK/src/com/neuronrobotics/sdk/network/UDPBowlerConnection.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void write(byte[] data) throws IOException {
121121
}
122122

123123
@Override
124-
public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
124+
public BowlerDatagram loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointerException, IOException{
125125
byte[] receiveData=new byte[4096];
126126

127127
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
@@ -132,7 +132,7 @@ public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointe
132132
}catch(Exception ex){
133133
// disconnect called
134134
//Log. warning("Receive bailed out because of close");
135-
return false;
135+
return null;
136136
}
137137

138138
Log.info("Got UDP packet");
@@ -146,18 +146,16 @@ public boolean loadPacketFromPhy(ByteList bytesToPacketBuffer) throws NullPointe
146146
internalReceiveBuffer.add(data[i]);
147147
}
148148

149+
BowlerDatagram bd =null;
150+
149151
while(internalReceiveBuffer.size()>0){
150152
bytesToPacketBuffer.add(internalReceiveBuffer.pop());
151-
BowlerDatagram bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
152-
if (bd!=null) {
153-
Log.info("\nR<<"+bd);
154-
onDataReceived(bd);
155-
156-
return true;
153+
if (bd==null) {
154+
bd = BowlerDatagramFactory.build(bytesToPacketBuffer);
157155
}
158156
}
159157

160-
return false;
158+
return bd;
161159
}
162160

163161

0 commit comments

Comments
 (0)