Listing 1The main
thread starts the system off and then acts as the communication thread. It receives
commands from a remote host via a serial network connection over the wireless modem and
signals the gait-generator thread setting the global Command variable.
int cmdmutex;
int servomutex;
extern int Command;
main() {
int s,ns;
struct sockaddr_in sin;
int slen;
int GaitThread();
SpawnThread(GaitThread);
servomutex = InitMutex();
cmdmutex = InitMutex();
s = socket(AF_INET, SOCK_STREAM, 0); /* establish command port using TCP/IP */
sin.sin_addr.s_addr = htonl(MyIPAddress);
sin.sin_port = htons(MyPort);
bind(s,&sin,sizeof(sin));
while(1){ /* main loop; wait for connection */
slen = sizeof(sin);
ns = accept(s,&sin,&slen);
while(1){ /* do command loop */
if( (n = ReadLine(ns,buf,sizeof(buf))) < 1 )
break;
GetMutex(cmdmutex);
Command = DecodeCommand(buf);
ReleaseMutex(cmdmutex); }
GetMutex(cmdmutex); /* remote process has closed connection */
Command = CMD_STOP;
ReleaseMutex(cmdmutex);
close(ns); } }