- update instruments configurations
- new six version with status display - bugfixes on TecsClient - some changed samp. env. devices
This commit is contained in:
206
tecs/six.c
206
tecs/six.c
@@ -46,51 +46,102 @@ int CocCreateSockAdr(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int readWrite(int fd, int skip) {
|
||||
char msg[128];
|
||||
int l, i, go, iret, ires, n;
|
||||
char* readWrite(int fd, int tmo, int skip, char *find) {
|
||||
char msg[256];
|
||||
int l, lbuf, pos, i, go, iret, n, ga;
|
||||
char *p, chr;
|
||||
static char result[256];
|
||||
int match;
|
||||
static int laststat=0;
|
||||
/*
|
||||
* read from fd and write to stdout until a key pressed,
|
||||
* a timeout occured (1 or 5 sec.), or a <go ahead> message
|
||||
* a timeout occurred (tmo [msec]), or a <go ahead> message
|
||||
* received.
|
||||
* skip=0: timeout 1 sec (normal behaviour)
|
||||
* skip>0: timeout 10 sec.
|
||||
* skip>1: swallow text
|
||||
* skip=1,2: return 0 if SICS not found in message text
|
||||
* skip=3: return 0 if not acknowledged
|
||||
* skip=0: skip response that does not match
|
||||
* skip=1: skip all responses
|
||||
*
|
||||
* if the response starts with the contents of find
|
||||
* the second part of the line is returned
|
||||
* if the response contain the contents of find, but not at the beginning
|
||||
* "1" is returned
|
||||
* if the text is not found, or find=NULL, an empty line is returned
|
||||
*/
|
||||
n=0;
|
||||
go=0;
|
||||
ires=0;
|
||||
iret=term_wait_fd(fd, skip?10000:1000);
|
||||
while (iret==1) {
|
||||
ERR_SI(l=recv(fd, msg, sizeof(msg)-1, 0));
|
||||
/* if (l==0) return(0); */
|
||||
msg[l]='\0';
|
||||
if (skip==3) {
|
||||
if (NULL!=strstr(msg, "Acknowledged")) ires=1;
|
||||
} else if (skip==1 || skip==2) {
|
||||
if (NULL!=strstr(msg, "SICS")) ires=1;
|
||||
}
|
||||
for (i=0; i<l-1; i++) {
|
||||
if (msg[i]==-1 && msg[i+1]==-7) { /* go ahead */
|
||||
go=1;
|
||||
msg[i]='<'; msg[i+1]='>';
|
||||
ga=0;
|
||||
str_copy(result, "");
|
||||
iret=term_wait_fd(fd, tmo);
|
||||
while (iret>0) {
|
||||
pos=0;
|
||||
ERR_SI(lbuf=recv(fd, &chr, 1, 0));
|
||||
if (lbuf==0) return "0";
|
||||
while (lbuf>0) {
|
||||
if (chr<' ') {
|
||||
if (chr=='\n') break;
|
||||
if (chr==-1) {
|
||||
ga=1;
|
||||
} else if (chr==-7 && ga) {
|
||||
go=1; break;
|
||||
} else {
|
||||
ga=0;
|
||||
if (chr!='\r') {
|
||||
msg[pos++]=chr;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
go=0;
|
||||
msg[pos++]=chr;
|
||||
}
|
||||
iret=term_wait_fd(fd, tmo);
|
||||
if (iret!=1) break;
|
||||
ERR_SI(lbuf=recv(fd, &chr, 1, 0));
|
||||
}
|
||||
msg[pos]='\0';
|
||||
/* printf("[%s]\n", msg); */
|
||||
match=0;
|
||||
if (find != NULL) {
|
||||
p=strstr(msg, find);
|
||||
if (p!=NULL) { /* copy message to result */
|
||||
if (p==msg) {
|
||||
str_copy(result, msg+strlen(find));
|
||||
p=strchr(result,'\r');
|
||||
if (p != NULL) *p='\0';
|
||||
} else {
|
||||
str_copy(result, "1");
|
||||
}
|
||||
match=1;
|
||||
if (find==strstr(find,"status")) {
|
||||
if (result[0]=='E') {
|
||||
tmo=100;
|
||||
} else if (laststat!=result[0]) {
|
||||
laststat=result[0];
|
||||
go=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (skip<2 || skip==2 && ires==0) {
|
||||
fputs(msg, stdout);
|
||||
} else {
|
||||
if (skip || match) { /* skip text */
|
||||
n+=strlen(msg);
|
||||
} else {
|
||||
term_clear();
|
||||
fputs(msg, stdout);
|
||||
if (chr=='\n') fputs("\n", stdout);
|
||||
}
|
||||
if (go) break;
|
||||
iret=term_wait_fd(fd, skip?10000:1000);
|
||||
iret=term_wait_fd(fd, tmo);
|
||||
}
|
||||
if (n==0 && ires==1) return 1;
|
||||
return ires*n;
|
||||
OnError: return -1;
|
||||
if (iret==0) laststat=0;
|
||||
return result;
|
||||
OnError: return NULL;
|
||||
}
|
||||
|
||||
int sendCmd(int fd, char *cmd) {
|
||||
int l;
|
||||
char buf[256];
|
||||
ERR_I(str_copy(buf, cmd));
|
||||
ERR_I(str_append(buf, "\r\n"));
|
||||
ERR_SI(l=send(fd, buf, strlen(buf), 0));
|
||||
return l;
|
||||
OnError: return -1;
|
||||
}
|
||||
|
||||
void Usage(void) {
|
||||
@@ -105,18 +156,20 @@ int main (int argc, char *argv[]) {
|
||||
fd_set mask;
|
||||
int fd, l, i, j, port, skip;
|
||||
char buf[128], lbuf[16], ibuf[64], ilow[64];
|
||||
char *rights, *instr, *sim="", *us, *ps;
|
||||
char stdPrompt[128], prompt[256], instr[32];
|
||||
char *rights, *sim="", *us, *ps;
|
||||
char *p, *statusMatch;
|
||||
|
||||
struct sockaddr_in sadr;
|
||||
|
||||
port=1301;
|
||||
skip=2;
|
||||
skip=1;
|
||||
j=0;
|
||||
for (i=1; i<argc; i++) {
|
||||
if (0==strcmp(argv[i], "-s")) {
|
||||
port=1303; sim="-sim";
|
||||
} else if (0==strcmp(argv[i], "-w")) {
|
||||
skip=1;
|
||||
skip=0;
|
||||
} else {
|
||||
if (strlen(argv[i])>=32) {
|
||||
printf("argument too long\n");
|
||||
@@ -139,23 +192,32 @@ int main (int argc, char *argv[]) {
|
||||
printf("missing password\n");
|
||||
Usage(); return 0;
|
||||
}
|
||||
/*
|
||||
instr=getenv("Instrument");
|
||||
if (instr==NULL || instr[0]=='\0') {
|
||||
printf("Instrument is undefined\n");
|
||||
Usage(); return 0;
|
||||
}
|
||||
*/
|
||||
ERR_I(CocCreateSockAdr(&sadr, "0", port));
|
||||
ERR_SI(fd=socket(AF_INET, SOCK_STREAM, 0));
|
||||
term_reg_socket(fd);
|
||||
ERR_SI(connect(fd, (struct sockaddr *)&sadr, sizeof(sadr)));
|
||||
strcpy(buf, "sicslogin Spy 007\r\n");
|
||||
ERR_SI(send(fd, buf, strlen(buf), 0));
|
||||
ERR_I(i=readWrite(fd,skip));
|
||||
if (!i) {
|
||||
ERR_I(sendCmd(fd, "sicslogin Spy 007"));
|
||||
ERR_P(p=readWrite(fd,10000,skip,"SICS"));
|
||||
if (*p=='\0') {
|
||||
printf("rejected\n");
|
||||
return 0;
|
||||
}
|
||||
if (skip==2) printf("reading welcome message ...\n");
|
||||
if (skip) printf("reading welcome message ...\n");
|
||||
|
||||
ERR_I(sendCmd(fd, "Instrument"));
|
||||
ERR_P(p=readWrite(fd,10000,0,"Instrument = "));
|
||||
str_copy(instr, p);
|
||||
if (*instr=='\0') {
|
||||
printf("can not detect instrument\n");
|
||||
return 0;
|
||||
}
|
||||
if (j==0) {
|
||||
rights=getenv("six_rights");
|
||||
if (rights!=NULL && 0==strcmp(rights, instr)) {
|
||||
@@ -168,62 +230,64 @@ int main (int argc, char *argv[]) {
|
||||
ps="03lns1";
|
||||
}
|
||||
}
|
||||
sprintf(buf, "config Rights %s %s\r\n", us, ps);
|
||||
ERR_SI(send(fd, buf, strlen(buf), 0));
|
||||
ERR_I(i=readWrite(fd,3));
|
||||
if (!i && 0 != strcmp(instr, "TASP")) {
|
||||
printf("password guess failed, logged in as Spy\n\n");
|
||||
Usage();
|
||||
if (NULL == strstr(instr, "TASP")) {
|
||||
sprintf(buf, "config Rights %s %s", us, ps);
|
||||
ERR_I(sendCmd(fd, buf));
|
||||
ERR_P(p=readWrite(fd,10000,0,"Acknowledged"));
|
||||
if (*p=='\0') {
|
||||
printf("password guess failed, logged in as Spy\n\n");
|
||||
Usage();
|
||||
us="Spy";
|
||||
}
|
||||
} else {
|
||||
us="Spy";
|
||||
}
|
||||
printf("\rlogged in to SICS as %s on %s%s\n", us, instr, sim);
|
||||
/*
|
||||
if (*sim!='\0') {
|
||||
strcpy(buf, "sync\r\n");
|
||||
ERR_SI(send(fd, buf, strlen(buf), 0));
|
||||
printf("\rsynchronize ...\n");
|
||||
ERR_I(i=readWrite(fd,1));
|
||||
}
|
||||
*/
|
||||
printf("\rlogged in to SICS as %s on %s\n", us, instr);
|
||||
|
||||
str_lowcase(ilow,instr);
|
||||
sprintf(stdPrompt, "six[%s] ", ilow);
|
||||
|
||||
ERR_I(sendCmd(fd, "status interest"));
|
||||
ERR_P(readWrite(fd,10000,0,"OK"));
|
||||
ERR_I(sendCmd(fd, "status"));
|
||||
ERR_P(p=readWrite(fd,10000,0,"status = "));
|
||||
|
||||
iret=1;
|
||||
buf[0]='\0';
|
||||
pos=0;
|
||||
str_lowcase(ilow,instr);
|
||||
sprintf(ibuf, "six[%s%s] ", ilow, sim);
|
||||
|
||||
while (1) {
|
||||
if (*p=='E') { /* Eager to ... */
|
||||
strcpy(prompt, stdPrompt);
|
||||
} else if (*p>'1') {
|
||||
sprintf(prompt, "%s(%s) ", stdPrompt, p);
|
||||
}
|
||||
FD_ZERO(&mask);
|
||||
FD_SET(fd, &mask);
|
||||
iret=term_get_line(buf, sizeof(buf)-2, &pos, ibuf, &mask);
|
||||
iret=term_get_line(buf, sizeof(buf)-2, &pos, prompt, &mask);
|
||||
if (iret==STDIN_FILENO) { /* input line terminated */
|
||||
str_lowcase(lbuf, buf);
|
||||
if (0==strcmp(lbuf,"quit")) break;
|
||||
if (0==strcmp(lbuf,"exit")) break;
|
||||
statusMatch="status = ";
|
||||
if (0==strcmp(lbuf,"stop")) {
|
||||
strcpy(buf, "INT1712 3");
|
||||
} else if (0==strcmp(buf, "")) {
|
||||
strcpy(buf, "status");
|
||||
statusMatch=NULL;
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
skip=0;
|
||||
if (0==strcmp(lbuf,"sync")) {
|
||||
printf("\rsynchronize ...\n");
|
||||
skip=1;
|
||||
}
|
||||
l=strlen(buf);
|
||||
/*
|
||||
printf("sent> %s[%d chars]\n", buf, l);
|
||||
*/
|
||||
buf[l]=13;
|
||||
buf[l+1]=10;
|
||||
ERR_SI(send(fd, buf, l+2, 0));
|
||||
ERR_I(i=readWrite(fd,skip));
|
||||
|
||||
ERR_I(sendCmd(fd, buf));
|
||||
ERR_P(p=readWrite(fd,1000,skip,statusMatch));
|
||||
buf[0]='\0';
|
||||
pos=0;
|
||||
} else if (iret<0) { /* ?? */
|
||||
printf("\nwhat?\n"); exit(1);
|
||||
} else { /* socket iret ready to read */
|
||||
assert(fd==iret);
|
||||
ERR_I(readWrite(fd,0));
|
||||
ERR_P(p=readWrite(fd,1000,0,"status = "));
|
||||
}
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
|
||||
Reference in New Issue
Block a user