Exemplo n.º 1
0
	bool CIRCContextWithCommands::UserInput(const std::string& txt) {
		try {
			if(txt[0] == '/') {
				// Is a command.
				std::istringstream iss;
				iss.str(txt.substr(1));

				std::string cmnd;
				iss >> cmnd;
				std::transform(cmnd.begin(), cmnd.end(), cmnd.begin(), ::tolower);

				if(!ProcessUserCommand(cmnd, iss))
					throw std::runtime_error("Command does not exist.");
				return true;
			}
			return false;
		}
void ProcessInput(ADKAccessory &acc, byte *inMsg, int len)
{
  int pin, mode, value;
  int shield_id;
  int command;
  long d;

  CommandPacket pktRead(inMsg, len);

  shield_id = pktRead.readInt(); // shield id for response
  command = pktRead.readInt();
  switch(command) {
  case COMMAND_PIN_MODE :
      pin = pktRead.readByte();
      mode = pktRead.readByte();
      DebugPrintln("ProcessInput: Pinmode ");
      DebugPrintln("pin = ");
      DebugPrintln(pin);
      DebugPrintln(" mode = ");
      DebugPrintln(mode);
      pinMode(pin, mode);
      break;

  case COMMAND_DIGITAL_READ:
      pin = pktRead.readByte();
      value = digitalRead(pin);
      // send data to mobile
      DebugPrintln("ProcessInput: DigitalRead ");
      DebugPrintln("pin = ");
      DebugPrintln(pin);
      DebugPrintln(" value = ");
      DebugPrintln(value);
      SendReplyInt(acc, shield_id, REPLY_OK, value);
      break;

  case COMMAND_DIGITAL_WRITE:
      pin = pktRead.readByte();
      value = pktRead.readByte();
      digitalWrite(pin, value);
      DebugPrintln("ProcessInput: DigitalWrite ");
      DebugPrintln("pin = ");
      DebugPrintln(pin);
      DebugPrintln(" value = ");
      DebugPrintln(value);
      break;

  case COMMAND_ANALOG_READ:
       pin = pktRead.readByte();
       value = analogRead(pin);
       // send data to mobile
       DebugPrintln("ProcessInput: AnalogRead ");
       DebugPrintln("pin = ");
       DebugPrintln(pin);
       DebugPrintln(" value = ");
       DebugPrintln(value);
       // 0~1023
       SendReplyInt(acc, shield_id, REPLY_OK, value);
       break;

  case COMMAND_ANALOG_WRITE:
       pin = pktRead.readByte();
       value = pktRead.readByte(); // 0~255
       DebugPrintln("ProcessInput: AnalogWrite ");
       DebugPrintln("pin = ");
       DebugPrintln(pin);
       DebugPrintln(" value = ");
       DebugPrintln(value);
       analogWrite(pin, value);
       break;

  case COMMAND_DELAY:
       d = pktRead.readLong();
       DebugPrintln("ProcessInput: Delay ");
       DebugPrintln(" value = ");
       DebugPrintln(d); 
       delay(d);
       break;

  case COMMAND_DELAYMICROS:
       value = pktRead.readInt();
       DebugPrintln("ProcessInput: DelayMicroseconds ");
       DebugPrintln(" value = ");
       DebugPrintln(value); 
       delayMicroseconds(value);
       break;

  case COMMAND_MILLIS:
       DebugPrintln("ProcessInput: Millis ");
       d = millis();
       SendReplyLong(acc, shield_id, REPLY_OK, d);
       break;

  case COMMAND_MICROS:
       DebugPrintln("ProcessInput: Micros ");
       d = micros();
       SendReplyLong(acc, shield_id, REPLY_OK, d);
       break;

  case COMMAND_USER:
       DebugPrintln("ProcessInput: User Command command id = ");
       DebugPrintln(command);
       ProcessUserCommand(pktRead, acc, shield_id);
       break;

  default:
       DebugPrintln("ProcessInput: Unknown command:");
       DebugPrintln(command);
       break;
  }
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
  int  len=0,
       ret=0;

  //command line argument check
  if (CheckParams(argc, argv) < 0)
      exit(0);

  printf("\033[1;32m\n");
  printf("** %s: a simple SIP based talk program **\n", argv[0]);
  printf("** Please enter help to see the command options **\n\n");

  if (oSocSIP.Initialize(SIP_PORT) < 0)
  {
    printf("Error on SIP Socket Initialize\n");
    printf("\033[0m");
    exit(1);
  }

  fd_set fds;
  memset(g_rxBuffer, 0, BUFFER_LEN);
  memset(g_txBuffer, 0, BUFFER_LEN);

  g_sip_done  = false;
  g_1stSampleDone = false;
  g_sip_done   = false;

  g_sip_state = SIP_IDLE;
  g_sip_mode  = SIP_NONE;

  double oldTime=0;
  double newTime=0;
 
  timeval tim;
  gettimeofday(&tim, NULL);
  oldTime = tim.tv_sec;

  printf("\ntcsip> ");
  fflush(stdout); 
  while (!g_sip_done)
  {
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    FD_SET(oSocSIP.m_socket, &fds);

    ret = select(1+oSocSIP.m_socket, &fds, NULL, NULL, NULL);

    //some activity has happened on the socket
    if(ret < 0) 
    {
      printf("Error on on select\n");
      break;
    }
    //dump_state();
    //User entry, coming in through stdin
    if (FD_ISSET(0, &fds))
    {
      memset(g_iBuffer, 0, MAX_STR_LEN);
      memset(g_oBuffer, 0, MAX_STR_LEN);
      
      fgets(g_iBuffer, MAX_STR_LEN, stdin);
      len = strlen(g_iBuffer);
      g_iBuffer[--len] = '\0';
      printf("tcsip> "); fflush(stdout);

      gettimeofday(&tim, NULL);
      newTime = tim.tv_sec;
      if (newTime - oldTime > TIMEOUTTIME)
      {
        if (g_sip_state==SIP_CONX_PENDING || g_sip_state==SIP_DISCONX_PENDING)
        {
          printf("Timing out...\n");  
          g_sip_done = true;
          continue;
        }
      }
      else
      {
         oldTime = newTime;
      }
      
      if (len>0)
      {
         ProcessUserCommand(g_iBuffer, g_oBuffer); 
      }
      if (strlen((char*)g_oBuffer))
         printf("%s\ntcsip> ", g_oBuffer); fflush(stdout);
    }

    // Something arrived at the listening socket. read the packet, check if it is DATA type or ACK type,
    // and do the necessary.
    if (FD_ISSET(oSocSIP.m_socket, &fds))
    {

      memset(g_rxBuffer, 0, BUFFER_LEN);
      len = BUFFER_LEN;
      if (oSocSIP.RecvFrom(g_rxBuffer, &len) < 0)
      {
        printf("Error on SIP socket\n");
        return 0;
      }
      g_rxBuffer[len]='\0';
      //printf("____________________\nReceived from socket:\n%s\n_____\n", (char*)g_rxBuffer);

      int prompt=0;
      prompt = ProcessSipMsg(g_rxBuffer, g_oBuffer);
      if (strlen((char*)g_oBuffer))
      {
        if (prompt!=5)
           printf("%s\ntcsip> ", g_oBuffer); fflush(stdout);
      }
    }
  }

  // in case the audio thread did not terminate...
  if (g_au_thread_running)
  {
    pthread_join(g_au_thread, NULL);
  }

  if (g_audio_fd > 0)  
     close(g_audio_fd);

  printf("\033[0m\n");
  return 0;
}