| AVR Z-LINKŪ |

# RadioInterface.java

Go to the documentation of this file.

    00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
    00002 package radioInterface;
    00003
    00004 import java.util.Timer;
    00005
    00006 import javax.comm.SerialPort;
    00007 import javax.swing.event.EventListenerList;
    00008
    00009 import serialInterface.SerialStream;
    00010 import serialInterface.SerialStreamEvent;
    00011 import serialInterface.SerialStreamException;
    00012 import serialInterface.SerialStreamListener;
    00013
    00014
    00032 public class RadioInterface implements SerialStreamListener{
    00033
    00034         private SerialStream io;
    00035         private SerialPort openPort;
    00036         private boolean IndicationsEnabled;
    00037         private boolean CommandTimedOut;
    00038
    00039         private EventListenerList NewAssociationIndicationListeners = new EventListenerList( );
    00040         private EventListenerList NewDisassociationIndicationListeners = new EventListenerList( );
    00041         private EventListenerList NewDataIndicationListeners = new EventListenerList( );
    00042
    00043
    00044         /*
    00045          * Time to wait blocking until a response should be available from the radio platform.
    00046          */
    00047         private int ResponseTimeOut = 5000;
    00048
    00049
    00063         public RadioInterface( SerialPort portToUse ) throws RadioInterfaceStartException{
    00064
    00065                 try{
    00066                         this.io = new SerialStream( portToUse );
    00067                 }catch( SerialStreamException sse ){
    00068
    00069                         sse.printStackTrace( );
    00070                         throw new RadioInterfaceStartException( "ERROR: Could not start the Radio Interface." );
    00071                 }
    00072
    00073                 this.openPort = portToUse;
    00074                 io.addSerialStreamEventListener( this );
    00075                 IndicationsEnabled = false;
    00076                 CommandTimedOut = false;
    00077         }
    00078
    00079
    00085         public void terminateCommunication( ){
    00086
    00087                 this.openPort.close( );
    00088         }
    00089
    00090
    00100         public boolean sendReset( ){
    00101
    00102                 return sendCommandToRadio( "AT+R", "OK" );
    00103         }
    00104
    00105
    00115         public boolean sendConfigure( String channel, String panID, String deviceType ) {
    00116
    00117                 //Coordinator
    00118                 if( deviceType.equals( "Coordinator" ) ){
    00119
    00120                         return sendCommandToRadio( "AT+C: " + channel + "," + panID + "," + 'C', "OK" );
    00121                 }
    00122                 //End Device
    00123                 else{
    00124
    00125                         return sendCommandToRadio( "AT+C: " + channel + "," + panID + "," + 'E'
    00126                                          , "OK" );
    00127                 }
    00128         }
    00129
    00130
    00140         public boolean sendData( String data ){
    00141
    00142                 return sendCommandToRadio( "AT+T: " + Integer.toHexString( data.length( ) ).toUpperCase( ) + ',' +
    00143                                 data, "OK" );
    00144         }
    00145
    00146
    00156         public boolean sendDisAssociate( ){
    00157
    00158                 return sendCommandToRadio( "AT+D", "OK" );
    00159         }
    00160
    00161
    00171         public void enableIndications( ){
    00172
    00173                 io.enableSerialStreamEvents( );
    00174                 IndicationsEnabled = true;
    00175         }
    00176
    00177
    00187         public void disableIndications( ){
    00188
    00189                 io.disableSerialStreamEvents( );
    00190                 IndicationsEnabled = false;
    00191         }
    00192
    00193
    00214         private boolean sendCommandToRadio( String cmd, String commandResponse ){
    00215
    00216                 CommandTimedOut = false;
    00217
    00218                 //Setup timer.
    00219                 Timer timer = new Timer( );
    00220                 TimeOut timerThread = new TimeOut( );
    00221                 timerThread.setCaller( this );
    00222                 timer.schedule( timerThread, ResponseTimeOut, ResponseTimeOut );
    00223
    00224                 //Send command to radio.
    00225                 io.sendData( cmd );
    00226
    00227                 //Wait for response to arrive or time-out. Blocking loop.
    00228                 for( ; ( io.getData( ) == null ) && ( !CommandTimedOut ); );
    00229
    00230                 //Stop timer.
    00231                 timer.cancel( );
    00232
    00233                 //Command timed out.
    00234                 if( CommandTimedOut ){
    00235
    00236                         return false;
    00237                 }
    00238
    00239                 else{
    00240
    00241                         //System.out.println( "Received from radio platform: " + io.getData() );
    00242                         if( io.getData().equals( commandResponse ) ){
    00243
    00244                                 return true;
    00245                         }
    00246
    00247                         else{
    00248
    00249                                 return false;
    00250                         }
    00251                 }
    00252         }
    00253
    00254
    00263         public void setCommandTimedOut( ){
    00264                 CommandTimedOut = true;
    00265         }
    00266
    00267
    00284         public void SerialStreamEvent( SerialStreamEvent serialStreamEvent ){
    00285
    00286                 int i, ii;
    00287                 String response = serialStreamEvent.getResponse(  );
    00288                 String dataLength;
    00289
    00290                 //Events enabled.
    00291                 if( IndicationsEnabled ){
    00292
    00293                         //The received string has the correct preamble.
    00294                         if( response.charAt( 0 ) == '+' ){
    00295
    00296                                 //Read next character to find what type
    00297                                 //of indication this is.
    00298                                 switch( response.charAt( 1 ) ){
    00299
    00300                                         //New Association Indication.
    00301                                         case 'N':
    00302
    00303                                                 fireNewAssociationIndicationListeners( new NewAssociationIndication( ) );
    00304                                                 break;
    00305
    00306                                         //New Disassociation Indication.
    00307                                         case 'D':
    00308
    00309                                                 fireNewDisassociationIndicationListeners( new NewDisassociationIndication( ) );
    00310                                                 break;
    00311
    00312                                         //New Data Indication.
    00313                                         case 'T':
    00314
    00315                                                 //Loop through char 4 to 6 (length is worst case 3 chars).
    00316                                                 for( ii = 4; ( ii <= 6 );ii++ ){
    00317
    00318                                                         //Break as soon as the argument ends.
    00319                                                         if( response.charAt( ii ) == ',' ){
    00320
    00321                                                                 break;
    00322                                                         }
    00323                                                 }
    00324
    00325                                                 dataLength = response.substring( 4, ii );
    00326
    00327                                                 //Convert to integer representation
    00328                                                 i = Integer.parseInt( dataLength );
    00329
    00330                                                 //Extract "real" data
    00331                                                 ii++;
    00332                                                 String subMsg = response.substring( ii, ii+i );
    00333
    00334                                                 fireNewDataIndicationListeners( new NewDataIndication( subMsg, i ) );
    00335                                                 break;
    00336
    00337                                         default:
    00339                                                 break;
    00340                                 }
    00341                         }
    00342                 }
    00343         }
    00344
    00345
    00354         public void addNewAssociationIndicationListeners( NewAssociationIndicationListener naiListener ){
    00355
    00356                 NewAssociationIndicationListeners.add( NewAssociationIndicationListener.class, naiListener );
    00357         }
    00358
    00359
    00368         public void removeNewAssociationIndicationListeners( NewAssociationIndicationListener naiListener ){
    00369                 NewAssociationIndicationListeners.remove( NewAssociationIndicationListener.class, naiListener );
    00370         }
    00371
    00372
    00381         protected void fireNewAssociationIndicationListeners( NewAssociationIndication newDataEvent ){
    00382
    00383                 Object[ ] listeners = NewAssociationIndicationListeners.getListenerList( );
    00384                 int numberOfListeners = listeners.length;
    00385
    00386                 for( int i = 0; i < numberOfListeners; i+=2 ){
    00387
    00388                         if( listeners[ i ] == NewAssociationIndicationListener.class ){
    00389                                 ( (NewAssociationIndicationListener)listeners[ i + 1 ] ).NewAssociationIndicationEvent( newDataEvent );
    00390                         }
    00391                 }
    00392         }
    00393
    00394
    00403         public void addNewDisassociationIndicationListeners( NewDisassociationIndicationListener ndiListener ){
    00404
    00405                 NewDisassociationIndicationListeners.add( NewDisassociationIndicationListener.class, ndiListener );
    00406         }
    00407
    00408
    00417         public void removeNewDisassociationIndicationListeners( NewDisassociationIndicationListener ndiListener ){
    00418                 NewDisassociationIndicationListeners.remove( NewDisassociationIndicationListener.class, ndiListener );
    00419         }
    00420
    00421
    00430         protected void fireNewDisassociationIndicationListeners( NewDisassociationIndication newDataEvent ){
    00431
    00432                 Object[ ] listeners = NewAssociationIndicationListeners.getListenerList( );
    00433                 int numberOfListeners = listeners.length;
    00434
    00435                 for( int i = 0; i < numberOfListeners; i+=2 ){
    00436
    00437                         if( listeners[ i ] == NewAssociationIndicationListener.class ){
    00438                                 ( (NewDisassociationIndicationListener)listeners[ i + 1 ] ).NewDisassociationIndicationEvent( newDataEvent );
    00439                         }
    00440                 }
    00441         }
    00442
    00443
    00452         public void addNewDataIndicationListeners( NewDataIndicationListener ndiListener ){
    00453
    00454                 NewDataIndicationListeners.add( NewDataIndicationListener.class, ndiListener );
    00455         }
    00456
    00457
    00466         public void removeNewDataIndicationListeners( NewDataIndicationListener ndiListener ){
    00467                 NewDataIndicationListeners.remove( NewDataIndicationListener.class, ndiListener );
    00468         }
    00469
    00470
    00479         protected void fireNewDataIndicationListeners( NewDataIndication newDataEvent ){
    00480
    00481                 Object[ ] listeners = NewAssociationIndicationListeners.getListenerList( );
    00482                 int numberOfListeners = listeners.length;
    00483
    00484                 for( int i = 0; i < numberOfListeners; i+=2 ){
    00485
    00486                         if( listeners[ i ] == NewAssociationIndicationListener.class ){
    00487                                 ( (NewDataIndicationListener)listeners[ i + 1 ] ).NewDataIndicationEvent( newDataEvent );
    00488                         }
    00489                 }
    00490         }
    00491 }

@DOC_TITLE@