Esempio n. 1
0
void Director::ReceiveSignal(ISystemModule *module, unsigned int signal, void *data)
{
	int from = module->GetSerial();
	if (m_World->GetSerial() != from) {
		return;
	}

	if (signal != 3) {
		return;
	}

	if (!m_World) {
		m_System->Printf("Director::ReceiveSignal: world == NULL\n");
		return;
	}

	frame_t frame;
	m_World->GetUncompressedFrame(m_World->GetLastFrame()->seqnr, &frame);
	AnalyseFrame(&frame);
}
/* main function is a recv-thread, it is used to receive messages   *
*  sending from the senrver.                                        * 
*  if it receives a control-message, it will set needsendACK = 1    *
*  (to tell send thread that he should send an ACK to the server    *
*   saying "got it").                                               *
*  if receive a ACK message, it will set haverecvACK = 1, to let    *
*  the send thread knows that the server had received a temperature *
*  message.                                                         */
int main()
{
  pthread_t sendthread, sendACKthread;
  char recvframe[MAX_FRAME_SIZE] = "ABCDEFG";
//  char *device = "/dev/ttyAMA0";
//  char *device = "/dev/ttyUSB0";
  char *device = "/dev/pts/4";
  fre  = 1;
  stop = 0;
  haverecvACK = 0;
  needsendACK = 0;
  pthread_mutex_init(&stop_mutex, NULL);
  pthread_mutex_init(&fre_mutex, NULL);
  pthread_mutex_init(&have_recv_ack_mutex, NULL);
  pthread_mutex_init(&need_send_ack_mutex, NULL);
  fd = OpenPort(device);
  if(fd < 0) return -1;
  pthread_create(&sendthread,    NULL, (void *)&SendThreadFun, NULL);
  pthread_create(&sendACKthread, NULL, (void *)&SendACKFun,    NULL);

  while(stop != 1)
  {

    /*receive a frame and analyse it*/
    RecvFrame(fd, recvframe);
    int recvmode = AnalyseFrame(recvframe);

    if(recvmode == 0)
    {
       printf("recv: ACK\n");
       pthread_mutex_lock(&have_recv_ack_mutex);
       haverecvACK = 1;
       show_state();
       pthread_mutex_unlock(&have_recv_ack_mutex);
    }

    /* if it receives a frequency-message, it will set needsendACK = 1 *
     *  (to tell send thread that he should send an ACK to the server  *
     *   saying "got it").                                             */
    if(recvmode == 2 ) 
    {
      printf("recv: change frequency %d\n", fre);
      char delayAscii[7];
      /*extract the number from the recv frame */
      memcpy(delayAscii, recvframe + 3, 6);
      delayAscii[6] = '\0';
      pthread_mutex_lock(&fre_mutex);
      fre = atol((const char *) delayAscii);
      pthread_mutex_unlock(&fre_mutex);

      pthread_mutex_lock(&need_send_ack_mutex);
      needsendACK = 1;
      show_state();
      pthread_mutex_unlock(&need_send_ack_mutex);
    }

    /* if it receives a stop-message, it will set needsendACK = 1      *
     *  (to tell send thread that he should send an ACK to the server  *
     *   saying "got it").                                             */
    if(recvmode == 3)
    {

      printf("recv: STOP!\n");
      pthread_mutex_lock(&stop_mutex);
      stop = 1;
      pthread_mutex_unlock(&stop_mutex);

      pthread_mutex_lock(&need_send_ack_mutex);
      needsendACK = 1;
      show_state();
      pthread_mutex_unlock(&need_send_ack_mutex);
    }

    memset(recvframe, '\0', MAX_FRAME_SIZE);
    //usleep(500000);
  }
  pthread_join(sendthread, NULL);
  pthread_join(sendACKthread, NULL);
  return 0;
}