111 lines
2.6 KiB
C
111 lines
2.6 KiB
C
/** @file Oxford protocol handler for script-context based controllers.
|
|
*
|
|
* If you 'send' commands to a oxford controller using this protocol you
|
|
* will get one of three possible responses,
|
|
* 1. A value
|
|
* eg
|
|
* 2. An acknowledgement (ie 'OK')
|
|
* eg
|
|
* sct_mc2 send "MOG"
|
|
* OK
|
|
* 3. An error message
|
|
* eg
|
|
* sct_mc2 send "BGG"
|
|
* ASCERR: 20 Begin not valid with motor off (during read finished)
|
|
*/
|
|
#include <errno.h>
|
|
#include <ascon.h>
|
|
#include <ascon.i>
|
|
#include <dynstring.h>
|
|
|
|
/** @brief Set line terminator before sending command
|
|
*/
|
|
int OxfordWriteStart(Ascon *a) {
|
|
DynStringConcat(a->wrBuffer,"\r");
|
|
a->state = AsconWriting;
|
|
a->wrPos = 0;
|
|
return 1;
|
|
}
|
|
|
|
/** @brief Map oxford replies to OK, ASCERR:..., value.
|
|
* You can use the first character to sort replies from a oxford controller
|
|
* into four categories
|
|
* First character is,
|
|
* 'SPACE' This is followed by a value
|
|
* '?' Error, use 'TC 1' to get error code and message.
|
|
* ':' Command received, in response to commands which don't request data.
|
|
* '1-9' First digit of error-code followed by error message. (in response to 'TC 1')
|
|
*/
|
|
int OxfordReading(Ascon *a) {
|
|
int ret;
|
|
char chr, ch[2];
|
|
char* cp = NULL;
|
|
|
|
ret = AsconReadChar(a->fd, &chr);
|
|
while (ret > 0) {
|
|
if (chr != '\r') {
|
|
DynStringConcatChar(a->rdBuffer, chr);
|
|
ret = AsconReadChar(a->fd, &chr);
|
|
}
|
|
else {
|
|
cp = GetCharArray(a->rdBuffer);
|
|
chr = *cp;
|
|
if (*cp == '?') { /* command was in error */
|
|
a->state = AsconIdle;
|
|
#if 0
|
|
AsconError(a, GetCharArray(a->rdBuffer), 0);
|
|
#else
|
|
DynStringInsert(a->rdBuffer, "ASCERR:", 0);
|
|
#endif
|
|
return 0;
|
|
}
|
|
else if (GetDynStringLength(a->rdBuffer) == 1) { /* command was successful */
|
|
DynStringReplace(a->rdBuffer, "OK", 0);
|
|
a->state = AsconReadDone;
|
|
return 1;
|
|
}
|
|
else {
|
|
a->state = AsconReadDone;
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/** brief Oxford protocol handler.
|
|
* This handler formats commands (ie adds cr line terminator) and
|
|
* sorts replies into standard responses of
|
|
* <value>
|
|
* OK
|
|
* ASCERR:...
|
|
*/
|
|
int OxfordProtHandler(Ascon *a) {
|
|
int ret;
|
|
|
|
switch(a->state){
|
|
case AsconWriteStart:
|
|
ret = OxfordWriteStart(a);
|
|
return ret;
|
|
break;
|
|
case AsconReading:
|
|
ret = OxfordReading(a);
|
|
return ret;
|
|
break;
|
|
default:
|
|
ret = AsconStdHandler(a);
|
|
return ret;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void AddOxfordProtocoll(){
|
|
AsconProtocol *prot = NULL;
|
|
|
|
prot = calloc(sizeof(AsconProtocol), 1);
|
|
prot->name = strdup("oxford");
|
|
prot->init = AsconStdInit;
|
|
prot->handler = OxfordProtHandler;
|
|
AsconInsertProtocol(prot);
|
|
}
|