int main() { int ret; signal(SIGPIPE, CheckConnect); ret = CreateSocket(); if (ret == FALSE) { printf("Create socket faile.\n "); return FALSE; } NetServer(); usleep(10000); return 0; }
/* ====================================================================================== * * Ethernet to LeCroy HV modules Bridge via a Raspberry Pi (rPI) * * ======================================================================================= */ int main(void) { char LU_type[nLUTYP][L16] = {"1461PS0", "1461NS0", "1469PS0", "1469PS1", "1469NS0", "1469NS1", "1471PS0", "1471NS0"}; unsigned char ga, tbuff[L256], rbuff[L256], s1[L256], s2[L256], s3[L256], sdum[L256], *ps1; int imod, slot, nsm, sm, i1, i2, i3, i4; int fd; /*initialize */ for(i1 = 0; i1 < nLU; i1++) pLU[i1] = NULL; for(i1 = 0; i1 < nSLOTS; i1++) { /* negaitve # indicates empty slot/submodule */ for(i2 = 0; i2 < nSUBMOD; i2++) SS2LU[i1][i2] = -1; } /* Setup rPI serial to communicate with the HV modules * Options: * O_RDWR: read & write access to port * O_CTTY: prevent other input (e.g. keyboard) from affecting what we read * O_NONBLOCK: do not block waiting for a response * MAPPING REQUIRES ROOT PRIVILIGES OR CHANGING ACCESS PREVILIGES OF /dev/ttyUSB0 */ sio = open ("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); if(sio < 0) { printf("Unable to open /dev/ttyAMA0 ....\n"); exit(-1); } bzero(&sio_attr, sizeof(sio_attr)); sio_attr.c_iflag = IGNBRK | IGNPAR; sio_attr.c_oflag = 0; /* output mode flags - raw output */ /* 38400 baud, 8-bits, no parity, 1-stop bit, * no xonxoff, no rtscts */ sio_attr.c_cflag = B38400 | CS8 | CREAD | CLOCAL; /* control mode flags */ sio_attr.c_lflag = 0; /* local mode flags */ sio_attr.c_cc[VTIME]= 0; /* read - inter-character timer unused */ sio_attr.c_cc[VMIN] = 0; /* read - minimum number of characters */ tcflush (sio,TCIFLUSH); tcsetattr (sio, TCSANOW, &sio_attr); /* ATTN* signal from HV module is routed to GPIO23 * Setup system to access GPIO registers * Access GPIO23 as input, no interrupt, pull/down disabled (all are defaults) * MAPPING REQUIRES ROOT PRIVILIGES OR CHANGING ACCESS PREVILIGES OF /dev/mem */ fd = open("/dev/mem", O_RDWR | O_SYNC); /* needs to run as root!! */ if(fd < 0) { printf("Unable to open /dev/mem ....\n"); exit(-1); } gpioReg = mmap ( 0, 0xB4, /* length of GPIO registers */ PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_LOCKED, fd, 0x20200000); /* beginning of GPIO registers in memory */ close(fd); /* Send handshake message to every slot to determine which ones have a module. * This will also clear any module holding the ATTN* line (it has a pending response * in its output buffer) */ for(slot = 0; slot < nSLOTS; slot++) { ga = 255 - slot; /* geographical address of slot */ sio_TXbuff[0] = ga; sio_TXbuff[1] = 0x06; /* ACK */ sio_TXbuff[2] = '\n'; sio_TXbuff[3] = '\0'; write(sio,sio_TXbuff,strlen(sio_TXbuff)); if(msgget(50) > MSGstat_NONE) SLOTwMOD[nMOD++] = slot; /* 50 char wait */ } if(nMOD <= 0) { printf("i2lchv - no modules found.... exiting\n"); exit(0); } if(1) { printf("found %d modules\n",nMOD); /* get module ID */ for(imod = 0; imod < nMOD; imod++) { /* retrieve slot# of module */ slot = SLOTwMOD[imod]; /* get number of submodules in this module */ ga = 255 - slot; sio_TXbuff[0] = ga; sio_TXbuff[1] = 0x06; sio_TXbuff[2] = '\0'; /* Note: we choose to use the slot# as the transation ticket# */ sprintf(s1,"%d SM\n",slot); strcat(sio_TXbuff,s1); sio_TXlen = strlen(sio_TXbuff); write(sio,sio_TXbuff,sio_TXlen); /* send message */ if(msgget(sio_TXlen+50) != MSGstat_HNDSHK) goto skip_slot; /* get handshake */ /* wait up-to 2 sec for module to indicate is ready to send response to previous command */ if(IsGpioSet(23,2.0) != NORMAL) goto skip_slot; /* Module has response ready - send handshake sequence to start xfer * The slot geographical address & 0x06 (ACK) are * already in the buffer - we just add the LF and a NULL */ sio_TXbuff[2] = '\n'; sio_TXbuff[3] = '\0'; sio_TXlen = strlen(sio_TXbuff); write(sio,sio_TXbuff,strlen(sio_TXbuff)); /* send handshake */ if(msgget(50) != MSGstat_OK) goto skip_slot; /* 50 char wait (13ms) */ /* decode message */ sio_MSGbuff[sio_MSGlen] = '\0'; sio_MSGbuff[0] = ' '; /* convert ACK char into SPACE for decoding */ /* message terminates with CRLF */ ps1 = strchr(sio_MSGbuff,'\r'); /* search for CR */ *ps1 = '\0'; /* replace CR by NULL to terminate string */ sscanf(sio_MSGbuff,"%d %s %d",&i1,s1,&nsm); /* Check that the ticket-number field matches the slot# (we sent this value) * & the command is "SM" */ if((i1 != slot) || (strcmp(s1,"SM") != 0)) goto skip_slot; /* error */ /* get module ID & submodule information (e.g number of channels & properties) */ for(i3 = 0; i3 < nsm; i3++) { sio_TXbuff[2] = '\0'; if(nsm <= 1) { /* one sub-module, commands do not include sub-module address * Use the slot# as the transation ticket# */ sprintf(s1,"%d ID\n",slot); } else { /* sub-module i3 of [ 0 -> (nsm-1)] * Use the slot# as the transation ticket# */ sprintf(s1,"%d %d ID\n",slot,i3); } strcat(sio_TXbuff,s1); sio_TXlen = strlen(sio_TXbuff); write(sio,sio_TXbuff,sio_TXlen); /* send command */ if(msgget(sio_TXlen+50) != MSGstat_HNDSHK) goto skip_submodule; /* get handshake */ /* wait up-to 2 sec for module to indicate is ready to send response to previous command */ if(IsGpioSet(23,2.0) != NORMAL) goto skip_submodule; /* module ready - send handshake sequence to start xfer * The slot geographical address & 0x06 (ACK) are * already in the buffer - we just add the LF and a NULL */ sio_TXbuff[2] = '\n'; sio_TXbuff[3] = '\0'; sio_TXlen = strlen(sio_TXbuff); write(sio,sio_TXbuff,sio_TXlen); if(msgget(50) != MSGstat_OK) goto skip_submodule; /* 50 char wait (13ms) */ /* decode response - response should start with "YXX ID " where Y = 0x06 & * XX = transation ticket# = slot# (one or 2 characters) * Skip over the number of bytes taken by preamble (ACK, slot# & command) */ i1 = 1; if(slot > 9) i1 = 2; i1 = i1+5; /* 5 bytes = ACK + " ID " */ strcpy(s1,&sio_MSGbuff[i1]); ps1 = strchr(s1,'\r'); /* end-of-message sequence is CRLF. Search for CR */ *ps1 = '\0'; /* replace CR by NULL to terminate string */ sscanf(s1,"%s",s2); /* s2 should be the module type */ sprintf(s3,"S%d",i3); /*submodule number */ strcat(s2,s3); /* logic unit type */ /* search for logic unit type */ i2 = -1; for(i1 = 0; i1 < nLUTYP; i1++) { i4 = strcmp(&LU_type[i1][0],s2); if(i4 == 0) i2 = i1; } /* skip if no matching logic unit found */ if(i2 < 0) { printf("main - unknown logic unit type %s @ slot = %d\n",s2,slot); goto skip_submodule; } /* found logic unit type */ /* create structure to store this logic unit info */ pLUtmp = NULL; pLUtmp = (struct LUnit *)malloc(sizeof(struct LUnit)); if(pLUtmp == NULL) /* failed to allocate structure */ { printf("main - malloc failed!, slot = %d, smod = %d, LUTYP = %s\n\n",slot,i3,s2); goto skip_submodule; } lstLU = lstLU +1; pLU[lstLU] = pLUtmp; /* store - basic header for commands */ pLUtmp->hdr[0] = ga; pLUtmp->hdr[1] = 0x06; pLUtmp->hdr[2] = '\0'; if(nsm <= 1) { /* One sub-module, commands do not include sub-module address * The slot# is used as the transaction# */ sprintf(s3,"%d ",slot); } else { /*sub-module i3 of [ 0 -> (nsm-1)] * The slot# is used as the transaction# */ sprintf(s3,"%d %d ",slot,i3); } strcat(pLUtmp->hdr,s3); /* store - ack message */ pLUtmp->ack[0] = ga; pLUtmp->ack[1] = 0x06; pLUtmp->ack[2] = '\n'; pLUtmp->ack[3] = '\0'; /* store - */ pLUtmp->lutype = i2; /* logic unit type index */ pLUtmp->slot = slot; /* slot number */ pLUtmp->nsmod = nsm; /* number of submodules */ pLUtmp->smod = i3; /* submodule number */ strcpy(pLUtmp->id,s1); /* module/submodule id */ /* Map Slot/submodule to LU */ SS2LU[slot][i3] = lstLU; skip_submodule: continue; } /* bottom of loop over submodules */ skip_slot: continue; } /* bottom over slots */ } /* if(1)*/ /* Telnet server */ printf("Network server started\n"); NetServer(BASE_PORT); };