PSI sics-cvs-psi-2006

This commit is contained in:
2006-05-08 02:00:00 +00:00
committed by Douglas Clowes
parent ae77364de2
commit 6e926b813f
388 changed files with 445529 additions and 14109 deletions

View File

@@ -13,6 +13,7 @@
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include "fortify.h"
#include "sics.h"
#include "splitter.h"
@@ -123,13 +124,21 @@ int writeRS232(prs232 self, void *data, int dataLen)
iRet = NETWrite(self->pSock,data,dataLen);
if(self->debug > 0)
{
printf("RS232 OUT: %s",(char *)data);
printf("RS232 OUT : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL)
{
puts("");
}
fflush(stdout);
}
if(pPtr != NULL)
free(pPtr);
if(iRet != 1){
return BADSEND;
}
return iRet;
}
/*----------------------------------------------------------------------*/
@@ -171,7 +180,11 @@ int readRS232(prs232 self, void *data, int *dataLen)
{
if(self->debug > 0)
{
printf("RS232 IN: %s",(char *)data);
printf("RS232 IN : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL)
{
puts("");
}
fflush(stdout);
}
*dataLen = lRead;
@@ -205,22 +218,65 @@ int readRS232TillTerm(prs232 self, void *data, int *datalen){
replylen = *datalen;
iRet = NETReadTillTerm(self->pSock,self->timeout,self->replyTerminator,
(char *)data, replylen);
if(self->debug > 0)
if(self->debug > 0 && iRet > 0)
{
printf("RS232 IN/TERM: %s",(char *)data);
printf("RS232 IN/TERM : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL)
{
puts("");
}
fflush(stdout);
}
if(iRet == 0)
{
if(self->debug > 0)
{
printf("RS232 IN/TERM : TIMEOUT:%s\n",(char *)data);
}
return TIMEOUT;
}
else if(iRet == -1)
{
printf("Incomplete read: %s\n", (char *)data);
return INCOMPLETE;
}
*datalen = strlen((char *)data);
else if (iRet < 0)
{
return iRet;
}
if (*self->replyTerminator != 0) {
*datalen = strlen((char *)data);
} else {
*datalen = iRet;
}
return 1;
}
/*----------------------------------------------------------------------*/
int readRS232UntilWord(prs232 self,
char *buffer, int buflen, char *word){
time_t endTime;
int status;
int bytesRead = 0;
endTime = time(NULL) + self->timeout;
memset(buffer,0,buflen);
while(time(NULL) < endTime){
if(availableRS232(self)){
bytesRead = recv(self->pSock->sockid,buffer + bytesRead,
buflen - bytesRead - 1,0);
if(bytesRead < 0){
return BADREAD;
}
if(strstr(buffer,word) != NULL) {
return 1;
}
} else {
SicsWait(1);
}
}
return 0;
}
/*-----------------------------------------------------------------------*/
int availableRS232(prs232 self)
{
@@ -234,7 +290,7 @@ int availableRS232(prs232 self)
return NOTCONNECTED;
}
return NETAvailable(self->pSock,3);
return NETAvailable(self->pSock,0);
}
/*-----------------------------------------------------------------------*/
int availableNetRS232(prs232 self)
@@ -265,7 +321,7 @@ int availableNetRS232(prs232 self)
int transactRS232(prs232 self, void *send, int sendLen,
void *reply, int replyLen)
{
int iRet;
int iRet, len;
assert(self);
@@ -277,13 +333,22 @@ int transactRS232(prs232 self, void *send, int sendLen,
return NOTCONNECTED;
}
/*
if there is still data on the socket: clear it
while(availableRS232(self)){
len = replyLen;
readRS232(self,reply,&len);
}
*/
/*
write data
*/
iRet = writeRS232(self,send,sendLen);
if(iRet < 0)
if(iRet <= 0)
{
return iRet;
return BADSEND;
}
/*
@@ -294,8 +359,23 @@ int transactRS232(prs232 self, void *send, int sendLen,
reply, replyLen);
if(self->debug > 0)
{
printf("RS232 IN/TRANS: %s",(char *)reply);
fflush(stdout);
if(iRet > 0)
{
printf("RS232 IN/TRANS: %s",(char *)reply);
if(strchr((char *)reply,'\n') == NULL)
{
puts("");
}
fflush(stdout);
}
else if(iRet == 0)
{
printf("RS232 IN/TRANS: TIMEOUT\n");
}
else
{
printf("RS232 IN/TRANS/INCOMPLETE: %s",(char *)reply);
}
}
if(iRet == 0)
{
@@ -332,7 +412,7 @@ void getRS232Error(int iCode, char *errorBuffer,
break;
case TIMEOUT:
strncpy(errorBuffer,
"Timeout reading data",
"Timeout or network error when reading data",
errorBufferLen);
break;
case FAILEDCONNECT:
@@ -344,6 +424,9 @@ void getRS232Error(int iCode, char *errorBuffer,
strncpy(errorBuffer,"Did not find terminator in read buffer",
errorBufferLen);
break;
case BADSEND:
strncpy(errorBuffer,"Network problem: failed to send",errorBufferLen);
break;
default:
strncpy(errorBuffer,strerror(errno),
errorBufferLen);
@@ -351,7 +434,11 @@ void getRS232Error(int iCode, char *errorBuffer,
}
}
/*--------------------------------------------------------------------*/
int initRS232(prs232 self)
int getRS232Timeout(prs232 self){
return self->timeout;
}
/*--------------------------------------------------------------------*/
int initRS232WithFlags(prs232 self, int flags)
{
int iRet;
@@ -363,9 +450,10 @@ int initRS232(prs232 self)
NetReadRemoveUserSocket(pServ->pReader,self->pSock->sockid);
}
NETClosePort(self->pSock);
free(self->pSock);
self->pSock = NULL;
}
self->pSock = NETConnect(self->pHost, self->iPort);
self->pSock = NETConnectWithFlags(self->pHost, self->iPort, flags);
if(!self->pSock){
return FAILEDCONNECT;
} else{
@@ -376,11 +464,45 @@ int initRS232(prs232 self)
return 1;
}
}
/*--------------------------------------------------------------------*/
int initRS232(prs232 self)
{
return initRS232WithFlags(self, 0);
}
/*--------------------------------------------------------------------*/
int initRS232Finished(prs232 self)
{
int iret;
iret = NETConnectFinished(self->pSock);
if (iret < 0) {
return FAILEDCONNECT;
} else {
return iret;
}
}
/*--------------------------------------------------------------------*/
void closeRS232(prs232 self)
{
assert(self);
if(self->pSock != NULL)
{
if(pServ->pReader != NULL){
NetReadRemoveUserSocket(pServ->pReader,self->pSock->sockid);
}
NETClosePort(self->pSock);
free(self->pSock);
self->pSock = NULL;
}
}
/*------------------------------------------------------------------*/
prs232 createRS232(char *host, int iPort)
{
prs232 pNew = NULL;
assert(iPort > 0);
/*
create data structure
*/
@@ -396,6 +518,7 @@ prs232 createRS232(char *host, int iPort)
pNew->sendTerminator = strdup("\r");
pNew->replyTerminator = strdup("\n");
pNew->timeout = 1000;
pNew->debug = 0;
pNew->pDes = CreateDescriptor("RS232 Controller");
if(!pNew->pDes || !pNew->pHost ||
!pNew->replyTerminator || !pNew->sendTerminator)
@@ -431,6 +554,7 @@ prs232 createRS232(char *host, int iPort)
NetReadRemoveUserSocket(pServ->pReader,self->pSock->sockid);
}
NETClosePort(self->pSock);
free(self->pSock);
}
if(self->pHost)
{
@@ -438,6 +562,11 @@ prs232 createRS232(char *host, int iPort)
}
}
/*-------------------------------------------------------------------*/
static void KillAndFreeRS232(void *pData) {
KillRS232(pData);
free(pData);
}
/*-------------------------------------------------------------------*/
static int checkSet(SConnection *pCon, int argc, int rights)
{
if(argc < 3)
@@ -559,9 +688,15 @@ int RS232Action(SConnection *pCon, SicsInterp *pSics,
return 1;
}
}
else if(strcmp(argv[1],"debug") == 0)
{
self->debug = atoi(argv[2]);
SCSendOK(pCon);
return 1;
}
else if(strcmp(argv[1],"timeout") == 0)
{
if(checkSet(pCon,argc,usMugger))
if(checkSet(pCon,argc,usUser))
{
setRS232Timeout(self,atoi(argv[2]));
SCSendOK(pCon);
@@ -628,6 +763,31 @@ int RS232Action(SConnection *pCon, SicsInterp *pSics,
SCWrite(pCon,pBuffer,eValue);
return 1;
}
else if(strcmp(argv[1],"readchar") == 0){
if(argc < 3){
SCWrite(pCon,"ERROR: need number of chars to read",eError);
return 0;
}
iRet = Tcl_GetInt(pSics->pTcl,argv[2],&iRead);
if(iRet != TCL_OK){
SCWrite(pCon,"ERROR: failed to convert argument to number",eError);
return 0;
}
if(!availableRS232(self))
{
SCWrite(pCon,"Nothing to read!",eError);
return 1;
}
iRet = readRS232(self,pBuffer,&iRead);
if(iRet < 0)
{
getRS232Error(iRet,pError,255);
SCWrite(pCon,pError,eError);
return 0;
}
SCWrite(pCon,pBuffer,eValue);
return 1;
}
else if(strcmp(argv[1],"available") == 0)
{
iRet = availableRS232(self);
@@ -729,26 +889,14 @@ int RS232Factory(SConnection *pCon, SicsInterp *pSics,
/*
create data structure and open port
*/
pNew = (prs232)malloc(sizeof(rs232));
pNew = createRS232(argv[2], atoi(argv[3]));
if(!pNew)
{
SCWrite(pCon,"ERROR: out of memory in RS232Factory",eError);
return 0;
}
memset(pNew, 0, sizeof(rs232));
pNew->pHost = strdup(argv[2]);
pNew->iPort = atoi(argv[3]);
pNew->sendTerminator = strdup("\r");
pNew->replyTerminator = strdup("\n");
pNew->timeout = 1000;
pNew->pDes = CreateDescriptor("RS232 Controller");
if(!pNew->pDes || !pNew->pHost ||
!pNew->replyTerminator || !pNew->sendTerminator)
{
SCWrite(pCon,"ERROR: out of memory in RS232Factory",eError);
return 0;
}
status = initRS232(pNew);
if(status != 1)
{
@@ -760,12 +908,12 @@ int RS232Factory(SConnection *pCon, SicsInterp *pSics,
/*
create the command
*/
iRet = AddCommand(pSics,argv[1],RS232Action, KillRS232, pNew);
iRet = AddCommand(pSics,argv[1],RS232Action, KillAndFreeRS232, pNew);
if(!iRet)
{
sprintf(pError,"ERROR: duplicate command %s not created", argv[1]);
SCWrite(pCon,pError,eError);
KillRS232(pNew);
KillAndFreeRS232(pNew);
return 0;
}
return 1;