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 );
00037 out8( ob_ctrl, BOARD_OUT16_ENABLE );
00038 ib_val = in16( ib_state );
00039 }
00040
00041 void CleanUp()
00042 {
00043
00044 printf( "Removing the attach name from the local name space.\n" );
00045 name_detach(attach, 0);
00046 }
00047
00048
00049 int server()
00050 {
00051 board_data_t msg, rmsg;
00052 int rcvid;
00053
00054
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
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
00072
00073 if (rcvid == -1)
00074 {
00075 break;
00076 }
00077
00078 if (rcvid == 0)
00079 {
00080 printf( "Pulse received.\n" );
00081 switch (msg.hdr.code) {
00082 case _PULSE_CODE_DISCONNECT:
00083
00084
00085
00086
00087
00088 printf( "Client disconnected.\n" );
00089 fflush( stdout );
00090 ConnectDetach(msg.hdr.scoid);
00091 break;
00092
00093 case _PULSE_CODE_UNBLOCK:
00094
00095
00096
00097
00098
00099 printf( "Client wants to unblock.\n");
00100 break;
00101 default:
00102
00103 printf( "Unknown pulse.\n" );
00104 fflush( stdout );
00105 break;
00106 }
00107 continue;
00108 }
00109
00110
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
00123
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
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