Ejemplo n.º 1
0
static void * proc_control(void *args) {
	struct frame_manager *manager = get_frame_manager(CONTROL_MANAGER);
	struct record_manager * record_manager=get_record_manager();
	struct block_filter *filter = NULL;

	struct frame *pframe = NULL;
	struct block * pblock = NULL;
	char buffer[64];
	int length;
	char dest_addr = 0;
	char src_addr = 0;
	char operation = 0;
	char cmd = 0;
	char tmp;

	if (manager == NULL)
		return NULL;
	filter = get_frame_filter(manager);
	while (1) {
		pblock = get_block(filter, TIMEOUT_MAIN_UNIT, BLOCK_FULL);
		if (pblock != NULL) {
			set_sys_state(BIT5_MAIN_UNIT,STATE_MAINUNIT_OK);
			light_on(0);
			light_main_alarm(0);
			pframe = (struct frame*) pblock->data;
			pframe->length = pblock->data_length;
			dest_addr = destination_of_frame(pframe);
			src_addr = pframe->data[4];
			cmd = command_of_frame(pframe);
			operation = operation_of_frame(pframe);

			if (src_addr == MASTER_ADDRESS) {

				switch (dest_addr) {
				case RADIO_450M_ADDRESS:
					if ((operation == 0x03) && (cmd == 0x20)) { //向450M发送机车号
						char * cab_id = get_id();

						get_frame_real_data(pframe, buffer, &length);

						buffer[8] = 0;
						tmp = buffer[3];
						buffer[3] = 0;
						int cab_type = atoi(buffer);

						buffer[3] = tmp;

						sprintf(cab_id, "%s-%s", get_cab_code(cab_type),
								buffer + 3);

					}
					break;
				case ALL_MMI_ADDRESS:
					if ((operation == 0x03) && (cmd == 0x46)) { //关机命令

						flush_all_data(record_manager);
					}
					break;
				}

			}

			if (dest_addr == RECORD_ADDRESS) {

				switch (operation) {
				case 1: //维护信息
					// echo off
					tmp = 0;
					if (src_addr == 0x01) {
						tmp = 0x01;
					} else if ((src_addr == 0x03) || (src_addr == 0x04)) {
						tmp = 0x41;
					}
					send_frame(manager, RECORD_ADDRESS, src_addr, 1, tmp,
							pframe->data + 9, 2);

					switch (cmd) {
					case (char) 0x33: //播放控制
					{
						if (pframe->data[10] == (char) 0xff) {
							start_play(src_addr);
						} else if (pframe->data[10] == 0) {
							stop_play();
						}
					}
						break;
					case (char) 0xfa: //问询测试
						break;
					case (char) 0x34: //查询时钟
					{
						struct tm time;
						get_time(&time);

						buffer[0] = to_bcd(time.tm_year - 100); //year
						buffer[1] = to_bcd(time.tm_mon + 1);
						buffer[2] = to_bcd(time.tm_mday);
						buffer[3] = to_bcd(time.tm_hour);
						buffer[4] = to_bcd(time.tm_min);
						buffer[5] = to_bcd(time.tm_sec);
						send_frame(manager, RECORD_ADDRESS, src_addr, 1,
								(char) 0x91, buffer, 6);
					}
						break;
					case (char) 0x35: //设置时钟
						break;
					case (char) 0xa5: //查询软件版本
					{

						bcopy(version, buffer, 16);
						send_frame(manager, RECORD_ADDRESS, src_addr, 1,
								(char) 0xaa, buffer, 16);
					}
						break;

					}

					break;
				case 3: //不需要应答
					break;
				}
			}
			put_block(pblock, BLOCK_EMPTY);
		} else {
			set_sys_state(BIT5_MAIN_UNIT,STATE_MAINUNIT_FAIL);
			light_main_alarm(1);
		}
	}

	return NULL;
}
void scorer(mqd_t score_q)
{
	int rc;
	long msg[10000];
	int exit_called = FALSE;
	int match_over = FALSE;

	player_to_serve = flip_coin();
	start_play(player_to_serve);

	playerA_gamescore = 0;
	playerB_gamescore = 0;
	playerA_setscore = 0;
	playerB_setscore = 0;
	playerA_matchscore = 0;
	playerB_matchscore = 0;

	printf("\n\nMATCH IS BEGINNING\n\n");
	fflush(stdout);
	fflush(stdout);
	sleep(sleep_time_seconds);

	for (;;)
	{
		rc = mq_receive(score_q, (char *) &msg, sizeof(msg), NULL);
		if (rc == -1)
		{
			perror("error: scorer");
			exit(EXIT_FAILURE);
		}
		switch(msg[0])
		{
		case PLAYER_A:
			printf("POINT player %s\n", PLAYER_B_STR);
			fflush(stdout);

			playerB_gamescore++;
			volley_count = 0;
			match_over = eval_score(
				PLAYER_B_STR
				, &playerB_gamescore
				, &playerB_setscore
				, &playerB_matchscore
			);
			break;
		case PLAYER_B:
			printf("POINT player %s\n", PLAYER_A_STR);
			fflush(stdout);

			playerA_gamescore++;
			volley_count = 0;
			match_over = eval_score(
				PLAYER_A_STR
				, &playerA_gamescore
				, &playerA_setscore
				, &playerA_matchscore
			);
			break;
		case EXIT:
			printf("    scorer exit received\n");
			exit_called = TRUE;
			match_over = TRUE;
			break;
		default:
			break;
		}
		if (exit_called || match_over)
		{
			break;	// break from the for(;;)
		}

		start_play(player_to_serve);
	}
	kill(getpid(), SIGINT);
}
Ejemplo n.º 3
0
int
read_rtsp (sockets * s)
{
	char *arg[50];
	int cseq, la, i, rlen;
	char *proto, *transport = NULL, *useragent = NULL;
	int sess_id = 0;
	char buf[2000];
	streams *sid = get_sid(s->sid);

	if(s->buf[0]==0x24 && s->buf[1]<2)
	{
		if(sid)
			sid->rtime = s->rtime;

		int rtsp_len = s->buf[2]*256+s->buf[3];
		LOG("Received RTSP over tcp packet (sock_id %d, stream %d, rlen %d) packet len: %d, type %02X %02X discarding %s...", 
			s->id, s->sid, s->rlen, rtsp_len , s->buf[4], s->buf[5], (s->rlen == rtsp_len+4)?"complete":"fragment" );		
		if(s->rlen == rtsp_len+4){ // we did not receive the entire packet
			s->rlen = 0;			
			return 0;
		}
	}
	
	if (s->rlen < 4 || !end_of_header(s->buf + s->rlen - 4))
	{
		if( s->rlen > RBUF - 10 )
		{
			LOG("Discarding %d bytes from the socket buffer, request > %d, consider increasing  RBUF", s->rlen, RBUF);
			s->rlen = 0;
		}
		LOG("read_rtsp: read %d bytes from handle %d, sock_id %d, flags %d not ending with \\r\\n\\r\\n", s->rlen, s->sock, s->id, s->flags);
		if ( s->flags & 1 ) 
			return 0;		
		unsigned char *new_alloc = malloc1 (RBUF);
		memcpy(new_alloc, s->buf, s->rlen);
		s->buf = new_alloc;
		s->flags = s->flags | 1;
		return 0;
	}

	rlen = s->rlen;
	s->rlen = 0;

	LOG ("read RTSP (from handle %d sock_id %d, len: %d, sid %d):\n%s", s->sock, s->id, s->rlen, s->sid, s->buf);

	if( (s->type != TYPE_HTTP ) && (strncasecmp(s->buf, "GET", 3) == 0))
	{
		http_response (s , 404, NULL, NULL, 0, 0);
		return 0;
	}
	
	la = split (arg, s->buf, 50, ' ');
	cseq = 0;	
	if (la<2)
		LOG_AND_RETURN(0, "Most likely not an RTSP packet sock_id: %d sid: %d rlen: %d, dropping ....", s->id, s->sid, rlen); 
	
	if(s->sid<0)
		for (i = 0; i < la; i++)	
			if (strncasecmp ("Session:", arg[i], 8) == 0)
			{
				sess_id = map_int(header_parameter(arg, i), NULL);
				s->sid = find_session_id(sess_id);		
			}

	if(strstr(arg[1], "freq") || strstr(arg[1], "pids"))
	{
		int old_sid = s->sid;
		sid = (streams *) setup_stream (arg[1], s);
	}
	sid = get_sid(s->sid);
	if(sid)
		sid->rtime = s->rtime;

	if (sess_id)    
			set_session_id(s->sid, sess_id);
		
	
	for (i = 0; i < la; i++)
		if (strncasecmp ("CSeq:", arg[i], 5) == 0)
			cseq = map_int (header_parameter(arg, i), NULL);
		else if (strncasecmp ("Transport:", arg[i], 9) == 0){
			transport = header_parameter(arg, i);

			if( -1 == decode_transport (s, transport, opts.rrtp, opts.start_rtp))
			{
				http_response (s, 400, NULL, NULL, cseq, 0);
				return 0;
			}
		}
		else if (strstr (arg[i], "LIVE555"))
		{
			if(sid) sid->timeout = 0;
		}
		else if (strstr (arg[i], "Lavf"))
		{
			if(sid) sid->timeout = 0;
		}
		else if (strncasecmp ("User-Agent:", arg[i], 10) == 0)
			useragent = header_parameter(arg, i);
	
	if((strncasecmp (arg[0], "PLAY", 4) == 0) || (strncasecmp (arg[0], "GET", 3) == 0) || (strncasecmp (arg[0], "SETUP", 5) == 0)) 
	{
		char ra[100];
		int rv;
			
		if (!( sid = get_sid(s->sid)))
		{
			http_response (s, 454, NULL, NULL, cseq, 0);
			return 0;
		}

		if (useragent)
			strncpy(sid->useragent, useragent, 127);

		if ((strncasecmp (arg[0], "PLAY", 3) == 0) || (strncasecmp (arg[0], "GET", 3) == 0))
			if ((rv = start_play (sid, s)) < 0)
			{
				http_response (s, -rv , NULL, NULL, cseq, 0);
				return 0;
			}
		strcpy(ra, inet_ntoa (sid->sa.sin_addr));
		buf[0] = 0;
		if(transport)
		{
			int s_timeout = (sid->timeout ? sid->timeout : opts.timeout_sec) / 1000;
			switch (sid->type)
			{
				case STREAM_RTSP_UDP:
					if (atoi (ra) < 239)
						snprintf (buf, sizeof(buf), "Transport: RTP/AVP;unicast;destination=%s;source=%s;client_port=%d-%d;server_port=%d-%d\r\nSession: %010d;timeout=%d\r\ncom.ses.streamID: %d",
							ra, get_sock_host (s->sock), ntohs (sid->sa.sin_port), ntohs (sid->sa.sin_port) + 1,
//							opts.start_rtp, opts.start_rtp + 1, 
							get_sock_port(sid->rsock), get_sock_port(sid->rtcp),
							get_session_id (s->sid), s_timeout, sid->sid + 1);
					else
						snprintf (buf, sizeof(buf), "Transport: RTP/AVP;multicast;destination=%s;port=%d-%d\r\nSession: %010d;timeout=%d\r\ncom.ses.streamID: %d",
							ra, ntohs (sid->sa.sin_port), ntohs (sid->sa.sin_port) + 1,
							get_session_id (s->sid), s_timeout , sid->sid + 1);
					break;
				case STREAM_RTSP_TCP:
					snprintf(buf, sizeof(buf), "Transport: RTP/AVP/TCP;interleaved=0-1\r\nSession: %010d;timeout=%d\r\ncom.ses.streamID: %d", 
						get_session_id (s->sid), s_timeout, sid->sid + 1);
					break;
			}
		}
		
		if (strncasecmp(arg[0], "PLAY", 4) == 0)
		{
			char *qm = strchr(arg[1], '?');
			if(qm)
				*qm = 0;
			if(buf[0])
				strcat(buf, "\r\n");
			
			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,  "RTP-Info: url=%s;seq=%d;rtptime=%lld\r\nRange: npt=0.000-", arg[1], getTick(),(long long int)(getTickUs()/1000000));
		}
		if(buf[0]==0 && sid->type == STREAM_HTTP)
				snprintf(buf, sizeof(buf), "Content-Type: video/mp2t");
		http_response (s, 200, buf, NULL, cseq, 0);
	}
	else if (strncmp (arg[0], "TEARDOWN", 8) == 0)
	{
		streams *sid;
		buf[0] = 0;
		if(get_sid(s->sid))
			sprintf(buf, "Session: %010d", get_session_id(s->sid));
		close_stream (s->sid);		
		http_response (s, 200, buf, NULL, cseq, 0);
	}
	else
	{
		if (strncmp (arg[0], "DESCRIBE", 8) == 0)
		{
			char sbuf[1000];
			char *rv = NULL;
			rv = describe_streams(s, arg[1], sbuf, sizeof(sbuf));
			if (! rv)
			{
				http_response (s, 404, NULL, NULL, cseq, 0);
				return 0;
			}	
			snprintf(buf, sizeof(buf), "Content-type: application/sdp\r\nContent-Base: rtsp://%s/", get_sock_host(s->sock));
			http_response (s, 200, buf, sbuf, cseq, 0);
				
		}
		else if (strncmp (arg[0], "OPTIONS", 8) == 0)
		{
			http_response (s, 200, public, NULL, cseq, 0);
		}
	}