Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Deprecated/old_18mar_diff/nto/BoardServer.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__BoardServer_CPP__
00002 #define __J2K__BoardServer_CPP__
00003 
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <inttypes.h>
00007 #include <sys/mman.h>
00008 #include <sys/neutrino.h>
00009 #include <hw/inout.h>
00010 
00011 #include "BoardDef.hpp"
00012 
00013 uintptr_t setHardware( uint64_t addr ) 
00014 {
00015   ThreadCtl( _NTO_TCTL_IO, 0 );
00016   uintptr_t ptr = mmap_device_io( 2, addr );
00017 }
00018 
00019 static uintptr_t       ib_state = NULL;
00020 static uintptr_t       ob_state = NULL;
00021 static uintptr_t       ib_ctrl  = NULL;
00022 static uintptr_t       ob_ctrl  = NULL;
00023 static name_attach_t*  attach   = (name_attach_t*)(~0);
00024 
00025 static USHORT          ob_val = 0xFFFF;
00026 static USHORT          ib_val = 0xFFFF;
00027 static int             done   = 0;
00028 
00029 void SetupIO() 
00030 {
00031   ob_state = setHardware( BOARD_OUT16_ADDR );
00032   ib_state = setHardware( BOARD_IN16_ADDR  );
00033   ob_ctrl  = setHardware( BOARD_OUT16_ADDR + BOARD_CONTROL_OFFSET );
00034   ib_ctrl  = setHardware( BOARD_IN16_ADDR  + BOARD_CONTROL_OFFSET );
00035 
00036   out16( ob_state, ob_val );      // Set to default
00037   out8(  ob_ctrl,  BOARD_OUT16_ENABLE ); // Set Array 0 & 1 ON (Controllable)
00038   ib_val = in16( ib_state ); 
00039 }
00040 
00041 void CleanUp() 
00042 {
00043   /* Remove the name from the space */
00044   printf( "Removing the attach name from the local name space.\n" );
00045   name_detach(attach, 0);
00046 }
00047 
00048 /*** Server Side of the code ***/
00049 int server() 
00050 {
00051     board_data_t msg, rmsg;
00052     int rcvid;
00053 
00054     /* Create a local name (/dev/name/local/...) */
00055     attach = name_attach( NULL, ATTACH_POINT, NAME_FLAG_ATTACH_GLOBAL );
00056     if ( attach == NULL )
00057     {
00058    printf( "Could not attach a name to the local name space.\n" );
00059         return EXIT_FAILURE;
00060     }
00061 
00062     SetupIO();
00063 
00064     /* Do your MsgReceive's here now with the chid */
00065     while( !done ) 
00066     {
00067         memset(  msg.string, 0, 80 );
00068         memset( rmsg.string, 0, 80 );
00069         rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
00070 
00071 // printf( "Received [%d]\n", rcvid );
00072 
00073         if (rcvid == -1)   /* Error condition, exit */
00074         {
00075             break;
00076         }
00077 
00078         if (rcvid == 0)    /* Pulse received */
00079         {
00080        printf( "Pulse received.\n" );
00081             switch (msg.hdr.code) {
00082             case _PULSE_CODE_DISCONNECT:
00083                 /*
00084                  * A client disconnected all its connections (called
00085                  * name_close() for each name_open() of our name) or
00086                  * terminated
00087                  */
00088       printf( "Client disconnected.\n" );
00089                 fflush( stdout );
00090                 ConnectDetach(msg.hdr.scoid);
00091                 break;
00092 
00093             case _PULSE_CODE_UNBLOCK:
00094                 /*
00095                  * REPLY blocked client wants to unblock (was hit by
00096                  * a signal or timed out).  It's up to you if you
00097                  * reply now or later.
00098                  */
00099       printf( "Client wants to unblock.\n");
00100                 break;
00101             default:
00102                 /* A pulse sent by one of your processes? */
00103       printf( "Unknown pulse.\n" );
00104                 fflush( stdout );
00105                 break;
00106             }
00107             continue;
00108         }
00109 
00110         /* A QNX IO message received, reject */
00111         if (msg.hdr.type >= _IO_BASE && msg.hdr.type <= _IO_MAX) {
00112             MsgError( rcvid, ENOSYS );
00113        printf( "Rejected.\n" );
00114             continue;
00115         }
00116 
00117         if ( msg.serialMsgID != BOARD_SerialMsgID ) {
00118      printf( "Wrong Serial ID...\n" );
00119      continue;
00120         }
00121 
00122         /* A message (presumable ours) received, handle */
00123 //        printf( "Server receive %d \n", msg.data );
00124 
00125         memcpy( &rmsg, &msg, sizeof( msg ) );
00126 
00127    register UCHAR cmd = msg.command;
00128 
00129    if ( (cmd & BOARD_CONTROL_IN) == BOARD_CONTROL_IN )
00130      out8( ib_ctrl, msg.control_in );
00131 
00132    if ( (cmd & BOARD_CONTROL_OUT) == BOARD_CONTROL_OUT )
00133      out8( ob_ctrl, msg.control_out );
00134 
00135    if ( (cmd & BOARD_IN16_READ ) == BOARD_IN16_READ ) {
00136      ib_val = in16( ib_state );
00137      rmsg.input = ~ib_val;
00138    }
00139  
00140    if ( (cmd & BOARD_OUT16_WRITE ) == BOARD_OUT16_WRITE ) {
00141      ob_val = ~( msg.output );
00142      out16( ob_state, ob_val );
00143    }
00144 
00145    if ( (cmd & BOARD_GET_STATE) == BOARD_GET_STATE ) {
00146      rmsg.control_in  = in8( ib_ctrl );
00147      rmsg.control_out = in8( ib_ctrl );
00148      rmsg.output      = ~ob_val;
00149      // input processed already.
00150    }
00151 
00152    if ( (cmd & BOARD_TEST_SERVER) == BOARD_TEST_SERVER ) {
00153      memcpy( &rmsg, &msg, sizeof( board_data_t ) );
00154    }
00155 
00156    if ( (cmd & BOARD_KILL_SERVER) == BOARD_KILL_SERVER ) {
00157      done++;
00158    }
00159 
00160    rmsg.command = BOARD_REPLY_DONE;
00161 
00162         rmsg.data++;
00163         strcpy( rmsg.string, "ACK" );
00164         MsgReply(rcvid, EOK, &rmsg, sizeof( rmsg ) );
00165     }
00166 
00167     CleanUp();
00168 
00169     return EXIT_SUCCESS;
00170 }
00171 
00172 int main(int argc, char **argv) {
00173     return server();
00174 }
00175 
00176 #endif

Generated on Sun Oct 14 18:46:21 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001