示例#1
0
/* this probes for the device on all serial ports and at all baud rates */
int probe(struct polhemus *ph, int *devicenum_p, int *baud_p, int *mode_p)
{
  static int baudrates[2] = { 9600,
                              38400 };
  int mode = PH_THREAD;
  int nbaud, baud, devicenum;
  int errnum;
  char *devicename;
  char reply[256];

  phSetThreadMode(ph,mode);
  errnum = phGetError(ph);
  if (errnum == PH_MODE_ERROR && mode != PH_NOTHREAD) {
    mode = PH_NOTHREAD;
    phSetThreadMode(ph,mode);
  }

  for (devicenum = 0; devicenum < 4; devicenum++) {
    devicename = phDeviceName(devicenum);
    for (nbaud = 0; nbaud < 2; nbaud++) {
      baud = baudrates[nbaud];
      phSetInitialComm(ph,baud,'N',8,0);
      fprintf(stderr, "trying %s baud %i\n", devicename, baud);
      phOpen(ph,devicename);
      errnum = phGetError(ph);
      /* if we can't open the serial port, go on to next serial port */
      if (errnum == PH_OPEN_ERROR) {
        break;
      }

      phSendCommand(ph, "\r\n");
      phReceiveReply(ph, reply, 256);
 
      errnum = phGetError(ph);
      /* if no error, then we're done switching baud rates */
      if (!errnum) {
        break;
      }
    }
    /* if no error, then we're done switching ports */
    if (!errnum) {
      break;
    }
  }

  if (!errnum) {
    phReset(ph);
    phClose(ph);
    *devicenum_p = devicenum;
    *baud_p = baud;
    *mode_p = mode;
  }
 
  return !errnum;
}
示例#2
0
int main(int argc, char *argv[])
{
  float pos[3],ang[3];
  struct polhemus *ph;
  int i;
  char status[128];
  int devicenum = 2;
  int baud = PH_38400;
  int mode = PH_NOTHREAD;
  char *devicename;
  double starttime;
  int count = 10;

  if (argc > 1) {
    count = atoi(argv[1]);
  }

  /* return a polhemus object */
  ph = phNew();

  /* probe for port/baud rate */
  if (!probe(ph,&devicenum,&baud,&mode)) {
    fprintf(stderr,"error %s\n",phGetErrorMessage(ph));
    exit(0);
  }

  /* print out probed information */
  fprintf(stderr,"found ph on port %s at baud rate %d in mode %d\n",
          phDeviceName(devicenum),baud,mode);

  /* you can set an error callback if you want */
  phSetErrorCallback(ph,callback,ph); 

  /* get the serial port name, e.g. "COM1:" or "COM2:" */
  devicename = phDeviceName(devicenum);

  /* args are ph, serial port, baud, flags */
  fprintf(stderr, "opening device %s\n", devicename);
  phSetInitialComm(ph, baud, 'N', 8, 0); 
  phSetThreadMode(ph, mode);
  phOpen(ph,devicename);
  /* ph->file = ndiSerialOpen(devicename);
     ndiSerialComm(ph->file, 38400, "8N1", 0); */
  fprintf(stderr, "device opened\n");

  /*
  fprintf(stderr,"system status\n");
  fbExamineValueBytes(ph,FB_FBB_STATUS,status);
  fprintf(stderr,"1: %2.2x 2: %2.2x",status[0],status[1]);
  fprintf(stderr,"\n");
  */

  fprintf(stderr,"sending: P\r\n");
  /*
  write(ph->file, "P\r\n", 3);
  usleep(500000);
  read(ph->file, status, 128);
  read(ph->file, status, 128);
  */
  phSetReplyFormat(ph, 1, PH_POSITION | PH_ANGLES);
  phSendRaw(ph,"P\n",2);
  phReceiveRaw(ph, status, 45, 0);
  fprintf(stderr, "received: \'%.*s\'\n", 45, status);
  phSetReplyFormat(ph, 1, PH_POSITION | PH_ANGLES);

  /* grab a single record */
  
  starttime = phGetTime(ph);

  fprintf(stderr, "phStream\n");
  phStream(ph);
  for (i = 0;i<count;i++) {
    fprintf(stderr, "phUpdate %i\n", i);
    phUpdate(ph);
    phGetPosition(ph,pos);
    phGetAngles(ph,ang);
    fprintf(stderr,"ph %d time %10.3f pos %+6.2f %+6.2f %+6.2f ang %+6.2f %+6.2f %+6.2f\n",
	    phGetStation(ph),phGetTime(ph)-starttime,
	    pos[0],pos[1],pos[2],
	    ang[0],ang[1],ang[2]);
  }
  
  phClose(ph);

  return 0;
}
示例#3
0
BOOL DestroySockets(PHGlobal *phglobal)
{
    phClose(&phglobal->m_tcpsocket);
    phClose(&phglobal->m_udpsocket);
    return TRUE;
}
示例#4
0
int ExecuteUpdate(PHGlobal *phglobal)
{
    char buffer[1024];

    char username[128] = "";
    char key[128] = "";
    char sendbuffer[256];

    char domains[255][255];
    char regicommand[255];
    int i,len, totaldomains;
    long challengetime = 0;

    char *chatid = NULL;
    char *startid = NULL;
    char *xmldata = NULL;
    int buflen = 0;

    LOG(1) ("ExecuteUpdate Connecting %s.\n",phglobal->szHost);

    if (!phConnect(phglobal->m_tcpsocket, phglobal->szHost,phglobal->nPort,&phglobal->nAddressIndex,phglobal->szTcpConnectAddress))
    {
        LOG(1) ("ExecuteUpdate errorConnectFailed.\n");
        phglobal->nAddressIndex++;
        return errorConnectFailed;
    }
    //////////////////////////////////////////////////////////////////////////
    //Recv server hello string
    memset(buffer, 0, 128);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    if (len <=0 )
    {
        LOG(1) ("ExecuteUpdate Recv server hello string failed.\n");
        phClose(&phglobal->m_tcpsocket);
        phglobal->nAddressIndex++;
        return errorConnectFailed;
    }

    LOG(1) (("SEND AUTH REQUEST COMMAND..."));
    phSend(phglobal->m_tcpsocket, (char*)COMMAND_AUTH,sizeof(COMMAND_AUTH),0);
    LOG(1) (("OK.\n"));

    //////////////////////////////////////////////////////////////////////////
    //Recv server key string
    memset(buffer, 0, 128);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    if (len <=0 )
    {
        LOG(1) (("ExecuteUpdate Recv server key string failed.\n"));
        phClose(&phglobal->m_tcpsocket);
        return errorConnectFailed;
    }
    LOG(1) (("SERVER SIDE KEY \"%s\" RECEIVED.\n"),buffer);

    phglobal->nChallengeLen =  lutil_b64_pton(buffer+4, (unsigned char *)phglobal->szChallenge, 256);


    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //Generate encoded auth string
    len = GenerateCrypt(phglobal->szUserID, phglobal->szUserPWD, buffer+4, phglobal->clientinfo, phglobal->challengekey, sendbuffer);
    strcat(sendbuffer, "\r\n");
    //Generate ok.
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////////
    //send auth data
    LOG(1) (("SEND AUTH DATA..."));
    phSend(phglobal->m_tcpsocket, sendbuffer,strlen(sendbuffer),0);
    LOG(1) (("OK\n"));

    memset(buffer, 0, 128);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    buffer[3] = 0;

    if (len <=0 )
    {
        LOG(1) (("ExecuteUpdate Recv server auth response failed.\n"));
        phClose(&phglobal->m_tcpsocket);
        //modified skyvense 2005/10/08, for server db conn lost bug
        //return errorAuthFailed;
        return errorConnectFailed;
    }
    if (strcmp(buffer,"250")!=0 && strcmp(buffer,"536")!=0)
    {
        LOG(1) ("CTcpThread::ExecuteUpdate auth failed.\n");
        phClose(&phglobal->m_tcpsocket);

        if (strstr(buffer + 4, "Busy.") != NULL) return errorAuthBusy;
        return errorAuthFailed;
    }
    if (strcmp(buffer,"536") == 0) //find redirected server address, and let us connect again to new server
    {
        char *pos0 = strchr(buffer + 4, '<');
        if (pos0)
        {
            char *pos1 = strchr(pos0 + 1, '>');
            if (pos1)
            {
                *pos1 = '\0';
                strcpy(phglobal->szHost, pos0 + 1);

                phClose(&phglobal->m_tcpsocket);
                return okRedirecting;
            }
        }
        return errorAuthFailed;
    }
    if (strcmp(buffer,"250") == 0) //get user type level, 0(free),1(pro),2(biz)
    {
        char *pos0 = strchr(buffer + 4, '<');
        if (pos0)
        {
            char *pos1 = strchr(pos0 + 1, '>');
            if (pos1)
            {
                *pos1 = '\0';
                phglobal->nUserType = atoi(pos0 + 1);
            }
        }
    }

    //////////////////////////////////////////////////////////////////////////
    //list domains
    for (i=0,totaldomains=0; i<255; i++)
    {
        memset(domains[i], 0, 255);
        phReadOneLine(phglobal->m_tcpsocket, domains[i],255);
        LOG(1) (("ExecuteUpdate domain \"%s\"\n"),domains[i]);
        totaldomains++;
        strcpy(phglobal->szActiveDomains[i],domains[i]);
        if (domains[i][0] == '.') break;
    }
    if (totaldomains<=0)
    {
        LOG(1) (("ExecuteUpdate Domain List Failed.\n"));
        phClose(&phglobal->m_tcpsocket);
        return errorDomainListFailed;
    }

    phglobal->cLastResult = okDomainListed;
    if (phglobal->cbOnStatusChanged) phglobal->cbOnStatusChanged(phglobal->cLastResult, 0);
    //::SendMessage(theApp.m_hWndController,WM_DOMAIN_UPDATEMSG,okDomainListed,(long)domains);
    //////////////////////////////////////////////////////////////////////////
    //send domain regi commands list
    for (i=0;; i++)
    {
        if (domains[i][0] == '.') break;
        memset(regicommand, 0, 128);
        strcpy(regicommand, COMMAND_REGI);
        strcat(regicommand, " ");
        strcat(regicommand, domains[i]);
        strcat(regicommand, "\r\n");
        //printf("%s",regicommand);
        phSend(phglobal->m_tcpsocket,regicommand,strlen(regicommand),0);
    }

    //////////////////////////////////////////////////////////////////////////
    //send confirm
    LOG(1) (("SEND CNFM DATA..."));
    phSend(phglobal->m_tcpsocket,(char*)COMMAND_CNFM,strlen(COMMAND_CNFM),0);
    LOG(1) (("OK\n"));

    for (i=0; i<totaldomains-1; i++)
    {
        memset(buffer, 0, 128);
        len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
        if (len <= 0)
        {
            LOG(1) (("ExecuteUpdate Recv server confirm response failed.\n"));
            phClose(&phglobal->m_tcpsocket);
            return errorDomainRegisterFailed;
        }
        LOG(1) (("ExecuteUpdate %s\n"),buffer);
        if (phglobal->cbOnDomainRegistered) phglobal->cbOnDomainRegistered(domains[i]);
    }

    memset(buffer, 0, 128);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    if (len <= 0)
    {
        LOG(1) (("ExecuteUpdate Recv server confirmed chatID response failed.\n"));
        phClose(&phglobal->m_tcpsocket);
        return errorDomainRegisterFailed;
    }
    LOG(1) (("%s\n"),buffer);

    //////////////////////////////////////////////////////////////////////////
    //find chatid & startid
    chatid = buffer + 4;
    startid = NULL;

    for (i=4; i<strlen(buffer); i++)
    {
        if (buffer[i] == ' ')
        {
            buffer[i] = 0;
            startid = buffer + i + 1;
            break;
        }
    }
    phglobal->nChatID = atoi(chatid);
    if (startid) phglobal->nStartID = atoi(startid);
    LOG(1) (("ExecuteUpdate nChatID:%d, nStartID:%d\n"),phglobal->nChatID,phglobal->nStartID);
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    //after confirm domain register, we begin to get user information
    phSend(phglobal->m_tcpsocket,(void *)COMMAND_STAT_USER,sizeof(COMMAND_STAT_USER),0);
    memset(buffer, 0, 1024);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    buffer[3] = 0;
    if (len <= 0 || strcmp(buffer,"250")!=0)
    {
        LOG(1) ("CTcpThread::ExecuteUpdate Recv server confirmed stat user response failed.\n");
        phClose(&phglobal->m_tcpsocket);
        return errorStatDetailInfoFailed;
    }

    buflen = MAX_PATH;
    xmldata = (char *)malloc(buflen);
    memset(xmldata, 0, buflen);

    for (;;)
    {
        memset(buffer, 0, 1024);
        len = phReadOneLine(phglobal->m_tcpsocket, buffer,1024);
        if (buffer[0] == '.' || len <= 0) break;
        if (buflen < strlen(xmldata) + len)
        {
            buflen += MAX_PATH;
            xmldata = realloc(xmldata, buflen);
            memset((xmldata + buflen) - MAX_PATH, 0, MAX_PATH);
        }
        strncat(xmldata, buffer, len);
    }
    LOG(1) ("userinfo: \r\n%s\r\n", xmldata);
    if (phglobal->cbOnUserInfo) phglobal->cbOnUserInfo(xmldata, strlen(xmldata));
    free(xmldata);
    buflen = 0;


    phSend(phglobal->m_tcpsocket,(void *)COMMAND_STAT_DOM,sizeof(COMMAND_STAT_DOM),0);
    memset(buffer, 0, 1024);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    buffer[3] = 0;
    if (len <= 0 || strcmp(buffer,"250")!=0)
    {
        LOG(1) ("CTcpThread::ExecuteUpdate Recv server confirmed stat user response failed.\n");
        phClose(&phglobal->m_tcpsocket);
        return errorStatDetailInfoFailed;
    }

    buflen = MAX_PATH;
    xmldata = (char *)malloc(buflen);
    memset(xmldata, 0, buflen);

    for (;;)
    {
        memset(buffer, 0, 1024);
        len = phReadOneLine(phglobal->m_tcpsocket, buffer,1024);
        if (buffer[0] == '.' || len <= 0) break;
        if (buflen < strlen(xmldata) + len)
        {
            buflen += MAX_PATH;
            xmldata = realloc(xmldata, buflen);
            memset((xmldata + buflen) - MAX_PATH, 0, MAX_PATH);
        }
        strncat(xmldata, buffer, len);
    }
    LOG(1) ("domaininfo: \r\n%s\r\n", xmldata);
    if (phglobal->cbOnAccountDomainInfo) phglobal->cbOnAccountDomainInfo(xmldata, strlen(xmldata));
    free(xmldata);
    buflen = 0;

    //////////////////////////////////////////////////////////////////////////
    //good bye!
    LOG(1) (("SEND QUIT COMMAND..."));
    phSend(phglobal->m_tcpsocket,(char*)COMMAND_QUIT,sizeof(COMMAND_QUIT),0);
    LOG(1) (("OK.\n"));

    memset(buffer, 0, 128);
    len = phReadOneLine(phglobal->m_tcpsocket, buffer,sizeof(buffer));
    if (len <= 0)
    {
        LOG(1) (("ExecuteUpdate Recv server goodbye response failed.\n"));
        phClose(&phglobal->m_tcpsocket);
        return okDomainsRegistered;
    }
    LOG(1) (("%s\n"),buffer);
    phClose(&phglobal->m_tcpsocket);
    return okDomainsRegistered;
}