// Author: A.Visser@uva..nl & E.H.Steffens@uva.nl // Copyright: Universiteit van Amsterdam, September 2010. // The Lantronix webserver DSTni is visible as peer-to-peer connection SRV1. // It gives ip-adresses in range 169.254.4.*, and reserves for himself 169.254.0.10. // Typical usage is ConnectLantronix.exe 169.254.0.10 30704 // // Modifications Arnoud: // * added missing include-files to remove warnings // * added some comments // * default options for commandline arguments // * CSAPP style connection // * using MP_REPLY_LENGTH of 5 #include #include #include #include #include #include #include #include #include "csapp.h" #define MP_COMMAND_LENGTH 9 #define MP_REPLY_LENGTH 5 // http://www.lantronix.com/pdf/MatchPort_UG.pdf, p. 72 #define GET_CURRENT_STATE 0x13 /* function prototypes from shellex.c */ void eval(char*cmdline); int parseline(char *buf, char **argv); int builtin_command(char **argv); void error(char *msg) { perror(msg); exit(0); } static int first_time = 1; static int counter = 0; static char previous_state; int main(int argc, char **argv) { int clientfd, port; char *host; //char buf[MAXLINE]; char MP_command[MP_COMMAND_LENGTH]; char MP_reply[MP_REPLY_LENGTH]; bzero(MP_command, MP_COMMAND_LENGTH); MP_command[0] = 0x13; switch(argc) { case 3: port = atoi(argv[2]); host = argv[1]; break; case 1: port = 30704; host = "169.254.0.10"; break; case 2: port = 30704; host = argv[1]; break; default: fprintf(stderr,"usage %s hostname port\n", argv[0]); fprintf(stdout,"default usage %s 169.254.0.10 30704\n", argv[0]); fprintf(stdout,"\nMake sure that your notebook is connected peer-to-peer to SRV1\n"); exit(0); } fprintf(stdout,"open connection to %s:%d ....\n", host, port); clientfd = Open_clientfd(host, port); //fprintf(stdout,"initiating buffer of stream %d ....\n", clientfd); //Rio_readinitb(&rio, clientfd); //fprintf(stdout,"Hit return to read out the state of the switch\n"); while (1) { // while (Fgets(buf, MAXLINE, stdin) != NULL) { // buf not used, only used for testing, can be replaced by endless loop when ready //fprintf(stdout,"writing command\n"); Rio_writen(clientfd, MP_command, MP_COMMAND_LENGTH); //fprintf(stdout,"reading response\n"); Rio_readn(clientfd, MP_reply, MP_REPLY_LENGTH); //fprintf(stdout, "%x\n",MP_reply[0]); //fprintf(stdout, "%x\n",MP_reply[1]); if(first_time) { first_time = 0; previous_state = MP_reply[1]; fprintf(stdout, "initialized counter to %d\n",counter); } else { if (MP_reply[1] != previous_state) { previous_state = MP_reply[1]; counter++; fprintf(stdout, "raised counter to %d\n",counter); } else { //fprintf(stdout, "keep counter on %d\n",counter); } } switch(counter) { case 1: eval("/usr/bin/cp counter1.html home.html "); break; default: eval("/usr/bin/cp counter0.html home.html "); } } Close(clientfd); exit(0); }