Example #1
0
int send_file( const char *ip, const int port, const char *id,
               const char *content, const char *filename, char **result )
{
    int sockfd = -1;
    int timeout = TIMEOUT;

    if( ( sockfd = sock_connect( ip, port, timeout ) ) == -1 )
    {
        fprintf( stderr, "cann't connect to cms server(%s:%d)!\n", ip, port );
        return -1;
    }

    if( write( sockfd, NETPOWER, strlen( NETPOWER ) ) == -1 )
    {
        fprintf( stderr, "send signal failed!\n" );
        return -1;
    }

    if( write( sockfd, File, strlen( File ) ) == -1 )
    {
        fprintf( stderr, "send File command failed!\n" );
        return -1;
    }

    if( send_package( sockfd, id, strlen( id ) ) == -1 )
    {
        fprintf( stderr, "send ids File(%s) failed!\n", id );
        return -1;
    }

    if( send_package( sockfd, content, strlen( content ) ) == -1 )
    {
        fprintf( stderr, "send content(File:%s) failed!\n", content );
        return -1;
    }

    if( send_package( sockfd, filename, strlen( filename ) ) == -1 )
    {
        fprintf( stderr, "send filename(File:%s) failed!\n", filename );
        return -1;
    }

    if( recv_package( sockfd, result ) == -1 )
    {
        fprintf( stderr, "Send File recv failed!\n" );
        return -1;
    }

    sock_close( sockfd );
    return 0;
}
Example #2
0
static void send_gps_info(void)
{
	int32_t latitude, longitude, altitude;
	int16_t gps_vx, gps_vy, gps_vz;
	float true_yaw;

	/* Prepare the GPS data */
	read_global_data_value(GPS_LAT, DATA_POINTER_CAST(&latitude));
	read_global_data_value(GPS_LON, DATA_POINTER_CAST(&longitude));
	read_global_data_value(GPS_ALT, DATA_POINTER_CAST(&altitude));
	read_global_data_value(GPS_VX, DATA_POINTER_CAST(&gps_vx));
	read_global_data_value(GPS_VY, DATA_POINTER_CAST(&gps_vy));
	read_global_data_value(GPS_VZ, DATA_POINTER_CAST(&gps_vz));
	read_global_data_value(TRUE_YAW, DATA_POINTER_CAST(&true_yaw));

	mavlink_message_t msg;

	mavlink_msg_global_position_int_pack(1, 220, &msg, 
		get_system_time_ms(),   		       //time 
		latitude ,  //Latitude
		longitude ,  //Longitude
		altitude, //Altitude
		0,
		gps_vx * 1,   //Speed-Vx
		gps_vy * 1,   //Speed-Vy
		gps_vz * 1,   //Speed-Vz
		(uint16_t)true_yaw
	);

	send_package(&msg);
}
Example #3
0
	void connection::send_package(std::shared_ptr<package> pkg)
	{
		auto write_handler = [this](std::shared_ptr<package> pkg, const error_code& err, std::size_t bytes)
		{
			(void)pkg;
			(void)bytes;

			if (!check_error(err))
			{
				return;
			}

			std::lock_guard<std::mutex> locker(_mutex);

			if (!_packages.empty())
			{
				std::shared_ptr<package> pkg = _packages.front();

				_packages.pop();

				send_package(pkg);
			}
			else
			{
				_socket_locked = false;
			}
		};

		auto handler = std::bind(write_handler, pkg, std::placeholders::_1, std::placeholders::_2);

		_socket_locked = true;

		_socket->async_write(*(pkg->buffer.get()), handler);
	}
//向手机发送命令
void *send_pthread()
{
	//设定对于cancel请求的参数
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);

	//加锁成功后,等待信号量(send_info)
	while(end_flag == 0)
	{
		if(send_or_recv == 1)
		{
			pthread_mutex_lock(&ass_pthread_mutex);
			fprintf(stderr,"send_pthread lock\n");
			sem_wait(&ass_pthread_sem);
			pthread_mutex_lock(&send_flag_mutex);
			send_package();
			pthread_mutex_unlock(&send_flag_mutex);
			fprintf(stderr,"send_pthread unlock\n");
			pthread_mutex_unlock(&ass_pthread_mutex);
			send_or_recv = 0;
		}
		usleep(5000);
	}
	fprintf(stderr,"send_pthread exit\n");
	pthread_exit(NULL);
}
Example #5
0
int Spider_Http_Client_Base::surface_page(std::string host,Http_Request_Package& request, Http_Response_Package& response)
{
	int sock=connect_host(host);
	if ( sock<=0 )
	{
        LLOG(L_ERROR, "surface_page connect_host error.");
		return -1;
	}

	int ret=send_package(sock,request);
	if ( ret!=0 )
	{
		LLOG(L_ERROR,"surface_page:send_package error. ");
		return -1;
	}
	
	ret=recv_package(sock, response);
	if (ret!=0)
	{
		LLOG(L_ERROR,"surface_page:recv_package error. ");
		return -1;
	}

	int status_code=response.get_status_code();
	close_socket(sock);
	return status_code;
}
Example #6
0
void mission_set_new_current_waypoint(void)
{
	mavlink_mission_set_current_t mmst;
	mavlink_msg_mission_set_current_decode(&received_msg, &mmst);

	waypoint_t *wp;

	/* Clear the old current waypoint flag */
	wp = get_waypoint(waypoint_info.waypoint_list, waypoint_info.current_waypoint.number);
	wp->data.current = 0;

	/* Getting the seq of current waypoint */
  	
	/* Ming change : I think should set this too*/

	waypoint_info.current_waypoint.number = mmst.seq;
	Nav_update_current_wp_id(mmst.seq);

	/* Set the new waypoint flag */
	wp = get_waypoint(waypoint_info.waypoint_list, waypoint_info.current_waypoint.number);
	wp->data.current = 1;
	/* Send back the current waypoint seq as ack message */
	mavlink_msg_mission_current_pack(1, 0, &msg, waypoint_info.current_waypoint.number);
	send_package(&msg);
}
Example #7
0
void process_command()
{
  DEBUG_PRINTLN("process_command");
  DEBUG_PRINTLN2("command ", command.command);
  DEBUG_PRINTLN2H("com.addr  ", command.node_id.addr);
  DEBUG_PRINTLN2H("this.addr ", THIS_NODE);

  answer.node_id.addr = THIS_NODE;
  answer.command = command.command;
  answer.number = command.number;
  answer.answer = 0;

  if (command.node_id.addr == THIS_NODE ||
    command.node_id.addr == 0)
  {

    switch(command.command)
    {
      case ECommand_LIGHT_ON:
      {
        DEBUG_PRINTLN("ECommand_LIGHT_ON");
        //digitalWrite(ledPin, HIGH);
        // digitalWrite(ledPin, LOW);
        current_color = 7;
        set_led(true);

        answer.answer = 1;

        break;
      }
      case ECommand_LIGHT_OFF:
      {
        DEBUG_PRINTLN("ECommand_LIGHT_OFF");
        // digitalWrite(ledPin, LOW);
        // digitalWrite(ledPin, HIGH);
        set_led(false);


        answer.answer = 2;

        break;
      }
      case ECommand_BEACON:
      {
        // answer is ready for beacon, no processing needed
        DEBUG_PRINTLN("ECommand_BEACON");
        break;
      }
      default:
      {
        answer.answer = -1;
      }
    }

    send_package(SimpleAnswer_fields, &answer, packet_xb_writer);
  }

}
Example #8
0
int send_ids( const char *ip, const int port, const char *id,
              const char *cmd, char **result )
{
    int sockfd = -1;
    int timeout = TIMEOUT;

    if( ( sockfd = sock_connect( ip, port, timeout ) ) == -1 )
    {
        fprintf( stderr, "cann't connect to cms server(%s:%d)!\n", ip, port );
        return -1;
    }

    if( write( sockfd, NETPOWER, strlen( NETPOWER ) ) == -1 )
    {
        fprintf( stderr, "send signal failed!\n" );
        return -1;
    }

    if( write( sockfd, SEND, strlen( SEND ) ) == -1 )
    {
        fprintf( stderr, "send Send command failed!\n" );
        return -1;
    }

    if( send_package( sockfd, id, strlen( id ) ) == -1 )
    {
        fprintf( stderr, "send ids sid(%s) failed!\n", id );
        return -1;
    }

    if( send_package( sockfd, cmd, strlen( cmd ) ) == -1 )
    {
        fprintf( stderr, "send cmd(%s) failed!\n", cmd );
        return -1;
    }

    if( recv_package( sockfd, result ) == -1 )
    {
        fprintf( stderr, "Send cmd(%s) recv failed!\n", cmd );
        return -1;
    }

    sock_close( sockfd );
    return 0;
}
Example #9
0
static void send_heartbeat_info(void)
{
	mavlink_message_t msg;
	uint8_t current_flight_mode,current_safety_switch;
	uint8_t current_MAV_mode = MAV_MODE_PREFLIGHT;

	read_global_data_value(MODE_BUTTON, DATA_POINTER_CAST(&current_flight_mode));
	read_global_data_value(SAFTY_BUTTON, DATA_POINTER_CAST(&current_safety_switch));

	if(current_safety_switch == 0){
		/* ENGINE ON */

		if(current_flight_mode == 0){
			/* Mode 1 */
			current_MAV_mode = MAV_MODE_STABILIZE_ARMED;

		}else if(current_flight_mode == 1){
			/* Mode 2 */
			current_MAV_mode = MAV_MODE_GUIDED_ARMED;

		}else if(current_flight_mode == 2){
			/* Mode 3 */
			current_MAV_mode = MAV_MODE_AUTO_ARMED;

		}


	}else if(current_safety_switch == 1){
		/* ENGINE OFF */

		if(current_flight_mode == 0){
			/* Mode 1 */
			current_MAV_mode = MAV_MODE_STABILIZE_DISARMED;

		}else if(current_flight_mode == 1){
			/* Mode 2 */
			current_MAV_mode = MAV_MODE_GUIDED_DISARMED;

		}else if(current_flight_mode == 2){
			/* Mode 3 */
			current_MAV_mode = MAV_MODE_AUTO_DISARMED;

		}

	}


	mavlink_msg_heartbeat_pack(1, 200, &msg,
		MAV_TYPE_QUADROTOR, 
		MAV_AUTOPILOT_GENERIC, 
		current_MAV_mode, 
		0, MAV_STATE_ACTIVE
	);

	send_package(&msg);
}
Example #10
0
void main()
{
	
	int fifo_server,fifo_client;

	pthread_t thread[400];

	while (1){

		fifo_client=0;
		fifo_server=0;
		package packet;	
		int id_client;
		char *buf;

	 	
	 	/*
                *********************handshake**********************
	 	*/

		fifo_server = open_wendy(fifo_server);
		printf("\n Abriendo pipe de comunicaci贸n \n");
		packet =  read_package(packet, fifo_server);

		
	 	char direccion[] = "/tmp/i_";
	 	char st[32];
	 	sprintf(st, "%s%s", direccion,packet.payload);
		fifo_client = open_input_pipe(fifo_client,st);	 

	 	id_client = atoi(packet.payload);
		
		packet = build_package(packet, packet.payload, "F1", "10", "Conexion establecida");
		send_package(packet, fifo_client);

		close(fifo_server);
		close(fifo_client);

	 	/*
                *********************Fin del handshake**********************
	 	*/	 	
	
	 	thdata data;
	 	data.id = id_client;	 
	 	data.index = indice;
	 	data.input_pipe =  fifo_client; 
		
		pthread_create (&thread[indice], NULL, (void *) &player, (void *) &data);

		indice++;		

	}
}
Example #11
0
static void send_current_waypoint(void)
{
	if(waypoint_info.current_waypoint.is_update == true) {
		mavlink_message_t msg;		

		/* Update the new current waypoint */
		mavlink_msg_mission_current_pack(1, 0, &msg,
			waypoint_info.current_waypoint.number);
		send_package(&msg);

		waypoint_info.current_waypoint.is_update = false;
	}
}
Example #12
0
void mission_clear_waypoint(void)
{
	waypoint_info.is_busy = true;

	/* Free the waypoint list */
	free_waypoint_list(waypoint_info.waypoint_list);
	waypoint_info.waypoint_count = 0;

	waypoint_info.is_busy = false;
	nav_waypoint_list_is_updated = false;
	/* Send a mission ack Message at the end */
	mavlink_msg_mission_ack_pack(1, 0, &msg, 255, 0, 0);
	send_package(&msg);
}
Example #13
0
static void send_reached_waypoint(void)
{
	if(waypoint_info.reached_waypoint.is_update == true) {
		mavlink_message_t msg;		

		/* Notice the ground station that the vehicle is reached at the 
	   	waypoint */
		mavlink_msg_mission_item_reached_pack(1, 0, &msg,
			waypoint_info.reached_waypoint.number);
		send_package(&msg);

		waypoint_info.reached_waypoint.is_update = false;
	}
}
Example #14
0
int  Spider_Http_Client::send_request(UrlPtr url)
{
	if (url==NULL||url->ip==NULL)
	{
		LLOG2(L_ERROR,"send_request: url is invalid.");
		return -1;
	}
	int sock=connect_ip(url->ip, url->port);
	if ( sock<=0 )
	{
		LLOG2(L_ERROR,"send_request:failed  to connect.");
		return -1;
	}
	Http_Request_Package  request_package;
	std::string request_line="GET ";
	request_line+=url->res;
	request_line+=" HTTP/1.1";
	request_package.set_request_line(request_line);
	request_package.set_field("Accept","*/*");
	request_package.set_field("User-Agent","Mozilla/5.0");
	request_package.set_field("Host", std::string(url->domain));
	request_package.set_field("Connection","close");  //短链接
	if ( strstr(url->domain,"renren")!=NULL )
	{
		Cookie* cookie=Spider_Cookie::instance().get_cookie("renren");
		if ( cookie!=NULL )
		{
			request_package.set_field("Cookie",cookie->to_string() );
		}
	}
	else if(strstr(url->domain,"weibo")!=NULL )
	{
		Cookie* cookie=Spider_Cookie::instance().get_cookie("weibo");
		if ( cookie!=NULL )
		{
			request_package.set_field("Cookie",cookie->to_string() );
		}
	}

	int ret=send_package(sock, request_package);
	if ( ret!=0 )
	{
		LLOG2(L_WARN,"send count isn't equal to ret. Why???");
	}

	return sock;
}
Example #15
0
	connection::dispatch_slot connection::create_dispatch_slot()
	{
		dispatch_slot slot = [this](std::unique_ptr<stream_buffer> buffer)
		{
			std::shared_ptr<package> pkg = create_package(std::move(buffer));

			std::lock_guard<std::mutex> locker(_mutex);

			if (_socket_locked)
			{
				_packages.push(pkg);
			}
			else
			{
				send_package(pkg);
			}
		};

		return slot;
	}
Example #16
0
static void send_attitude_info(void)
{
	mavlink_message_t msg;
	float attitude_roll, attitude_pitch, attitude_yaw;

	/* Prepare the attitude data */
	read_global_data_value(TRUE_ROLL, DATA_POINTER_CAST(&attitude_roll));
	read_global_data_value(TRUE_PITCH, DATA_POINTER_CAST(&attitude_pitch));
	read_global_data_value(TRUE_YAW, DATA_POINTER_CAST(&attitude_yaw));

	mavlink_msg_attitude_pack(1, 200, &msg,
		get_system_time_ms(),
		toRad(attitude_roll), 
		toRad(attitude_pitch), 
		toRad(attitude_yaw), 
		0.0, 0.0, 0.0
	);

	send_package(&msg);
}
Example #17
0
int list_ids( const char *ip, const int port, const char *params,
              char **result )
{
    int sockfd = -1;
    int timeout = TIMEOUT;

    if( ( sockfd = sock_connect( ip, port, timeout ) ) == -1 )
    {
        fprintf( stderr, "cann't connect to cms server(%s:%d)!\n", ip, port );
        return -1;
    }

    if( write( sockfd, NETPOWER, strlen( NETPOWER ) ) == -1 )
    {
        fprintf( stderr, "send signal failed!\n" );
        return -1;
    }

    if( write( sockfd, LIST, strlen( LIST ) ) == -1 )
    {
        fprintf( stderr, "send List command failed\n" );
        return -1;
    }

    if( send_package( sockfd, params, strlen( params ) ) == -1 )
    {
        fprintf( stderr, "send List params(%s) failed!\n", params );
        return -1;
    }

    if( recv_package( sockfd, result ) == -1 )
    {
        fprintf( stderr, "List recv failed!" );
        return -1;
    }

    sock_close( sockfd );

    return 0;
}
Example #18
0
static void send_system_info(void)
{
	mavlink_message_t msg;

	mavlink_msg_sys_status_pack(1, 0, &msg,
		0,
		0,
		0,
		0,
		12.5 * 1000, //Battery voltage
		-1,
		100,         //Battery remaining
		0,
		0,
		0,
		0,
		0,
		0
	);

	send_package(&msg);
}
Example #19
0
void mission_read_waypoint_list(void)
{
	uint32_t start_time, cur_time;

	waypoint_t *cur_wp = waypoint_info.waypoint_list; //First node of the waypoint list
	mavlink_mission_request_t mmrt;

	mavlink_msg_mission_count_pack(
		1, 0, &msg, 255, 0, waypoint_info.waypoint_count /* Waypoint count */
	);
	send_package(&msg);

	int i;
	for(i = 0; i < waypoint_info.waypoint_count; i++) {
		start_time = get_system_time_ms();

		/* Waiting for mission request command */
		while(received_msg.msgid != 40) {
			cur_time = get_system_time_ms();

			//Suspend the task to read the new message
			vTaskDelay(100);

			/* Time out, leave */
			if((cur_time - start_time) > TIMEOUT_CNT)
				return;
		}

		/* Waiting for mission request command */
		mavlink_msg_mission_request_decode(&received_msg, &mmrt);

		/* Clear the received message */
		clear_message_id(&received_msg);

		/* Send the waypoint to the ground station */
		mavlink_msg_mission_item_pack(
			1, 0, &msg, 255, 0,
			cur_wp->data.seq,
			cur_wp->data.frame,
			cur_wp->data.command,
			cur_wp->data.current,
			cur_wp->data.autocontinue,
			cur_wp->data.param1,
			cur_wp->data.param2,
			cur_wp->data.param3,
			cur_wp->data.param4,
			cur_wp->data.x,
			cur_wp->data.y,
			cur_wp->data.z
		);
		
		send_package(&msg);

		cur_wp = cur_wp->next;
	}


	/* Send a mission ack Message at the end */
	mavlink_msg_mission_ack_pack(1, 0, &msg, 255, 0, 0);
	send_package(&msg);
}
Example #20
0
void debug_msg(char *msg)
{
    xSemaphoreTake(debug_msg_mutex, portMAX_DELAY);
    send_package(msg, DEBUG_MODE);
    xSemaphoreGive(debug_msg_mutex);
}
Example #21
0
void send_all_vals()
{
    char command[MAX_COMMAND_LENGTH];
    status_get_var_str(command);
    send_package(command, UPDATE_MODE);
}
Example #22
0
void c_node::process_packet (t_osi3_packet &&packet) {
	// TODO!!!
	send_package(std::move(packet));
	_dbg1("***************************get apcket from " << packet.m_src);
	_dbg1("***************************data: " << packet.m_data);
}
Example #23
0
void player ( void *ptr )
{

    thdata *data;
    data = (thdata *) ptr;  /* type cast to a pointer to thdata */

    int fifo_output, fifo_input;
	 char address[25] = "/tmp/o_";
	 char address_input[25] = "/tmp/i_";
	 char stfile[8];
	 char stfile_i[8];
	 char *b;
	 char *respuesta;
	 int pipe_id = data->id;
	 int local_index = data->index;
	 int numero_juego;
	 char *token;
	 bool invitado;
	 players play;
	 package pack;

	 // Output pipe
	 sprintf(stfile, "%d", pipe_id);
	 strcat(address,stfile);

	 // Input pipe
	 sprintf(stfile_i, "%d", pipe_id);
	 strcat(address_input,stfile_i);

	 fifo_output = manage_output_pipe(address, fifo_output);
	 fifo_input =  data->input_pipe;
	 fifo_input =  open_input_pipe(fifo_input,address_input);
	 printf("Estamos en un nuevo thread \n");

	 /**                Comunicaci贸n con el cliente           **/

	 int rd;
	 int flag_out = 0;
	 int start_game = 0;

	 /* Opciones:
		FA: Crear un nuevo juego
		F6: Solicitar un juego
		F8: Unirse a un juego
	 */
	 while(flag_out != 1){

		pack = read_package(pack, fifo_output);
		fifo_input =  open_input_pipe(fifo_input, address_input);


		if(strcmp(pack.type,"FA") == 0){ // Crear juego

			play = PLAYER1; // Usted es jugador 1

			pthread_mutex_lock(&lock);	            // Proteger la variable juegos

			games[juegos].id_creator = pipe_id;
			numero_juego = juegos;
			games[numero_juego].state = STATE_A;
			juegos++;
			pthread_mutex_unlock(&lock);

			games[numero_juego].id = pipe_id;		// Fin de protecci贸n variable juegos

			token = strtok(pack.payload , ",");

			int counter_tokens = 0;
            while (token != NULL){
            	counter_tokens++;
                if(counter_tokens == 1)
						games[numero_juego].dimension = atoi(token);
					else if(counter_tokens == 2)
						strcpy(games[numero_juego].name, token);
					else if(counter_tokens == 3)
						games[numero_juego].t1 = atoi(token);
					else if(counter_tokens == 4)
						games[numero_juego].t2 = atoi(token);
					else if(counter_tokens == 5)
						games[numero_juego].t3 = atoi(token);


                token = strtok(NULL,",");
        	}

        	printf ("Juego creado con los siguientes datos: \nDim: %d, Nombre: %s, T1: %d, T2: %d, T3: %d \n",
        		games[numero_juego].dimension, games[numero_juego].name, games[numero_juego].t1,
        		games[numero_juego].t2, games[numero_juego]. t3);

        	flag_out = 1;
        	start_game = 3;

		}


		else if( strcmp(pack.type,"F6") == 0){ // Solicitar juegos
			int index_games;
			char list_games[255];
			char tmpID[32];
			sprintf(tmpID, "%d", games[numero_juego].id );


			for (index_games = 0; index_games < juegos ; index_games++){
				if(games[index_games].active != 1){
					char tmpPayload[255];
					sprintf(tmpPayload, "%d-%s-%dx%d\n",
						games[index_games].id, games[index_games].name,
						games[index_games].dimension,games[index_games].dimension);
					strcat(list_games, tmpPayload);
				}
			}
			// Env铆a lista de juegos
			pack = build_package(pack, tmpID , "F5", "10", list_games);
			send_package(pack, fifo_input);

			start_game = 2;
		}


		else if( strcmp(pack.type,"F8") == 0 ){ // Unirse a un juego
			int i=0;
			for(i=0;i<indice;i++){
				if((games[i].id == atoi(pack.payload)) && (flag_out != 1)){
					numero_juego = i;
					games[i].id_guest = pipe_id;
					games[i].state = STATE_A;
					games[i].active = 1;
					play = PLAYER2;
					printf("Juego con id: %d asignado! \n", games[i].id);

					start_game = 1;
					flag_out = 1;
				}
			}

		}

		/* Opciones:
			1: Confirmaci贸n de asignaci贸n de juego
			0: Recepci贸n de mensaje
		*/

		if(start_game == 0){

			pack = build_package(pack, "-" , "00", "10", "Mensaje recibido"); // Env铆a paquete de recepci贸n
			send_package(pack, fifo_input);

		}
		else if(start_game == 1){

			char tmpId[32];
			sprintf(tmpId, "%d", games[numero_juego].id);
			char info[255];
			sprintf(info,"%s,%d,%d,%d,%d",  games[numero_juego].name,
				games[numero_juego].dimension, games[numero_juego].t1,
				games[numero_juego].t2, games[numero_juego].t3);

			pack = build_package(pack, tmpId , "F9", "10", info); // Env铆a paquete de asignaci贸n
			send_package(pack, fifo_input);

		}
		else if(start_game == 3){
			char tmpId[32];
			sprintf(tmpId, "%d", games[numero_juego].id);
			char info[255];
			sprintf(info,"Se ha creado un juego exitosamente, con ID: %d",  games[numero_juego].id);

			pack = build_package(pack, "-" , "FB", "10", info); // Env铆a paquete de asignaci贸n
			send_package(pack, fifo_input);
		}

		close(fifo_input);
	}


	// Esperando a que el juego este activo para continuar

	while(games[numero_juego].active != 1){
		// do nothing
	}


	// Env铆a mensaje de Inicio de juego
	fifo_input =  open_input_pipe(fifo_input, address_input);
	pack = build_package(pack, pack.payload, "01", "10", "隆Inicia juego!");
	send_package(pack, fifo_input);


	//Leyendo posiciones de los barcos

	pack = read_package(pack, fifo_output);

	char *token_positions;
	token_positions = strtok(pack.payload , ",");

	int ctrtokens = 0;
    while (token_positions != NULL){

    	//Posicion tempPos = get_positions(token_positions);

    	if(play == PLAYER1){
	    	if ((ctrtokens % 2) == 0){
				games[numero_juego].posP1[ctrtokens].y =  atoi(token_positions);
				printf("PLAYER 1 VAL: %d \n",games[numero_juego].posP1[ctrtokens].y );
	    	}
	    	else{
	    		games[numero_juego].posP1[ctrtokens].x =  atoi(token_positions);
	    		printf("PLAYER 1 VAL: %d \n",games[numero_juego].posP1[ctrtokens].x );
	    	}
    	}
    	else{
	    	if ((ctrtokens % 2) == 0){
				games[numero_juego].posP2[ctrtokens].y =  atoi(token_positions);
				printf("PLAYER 2 VAL: %d \n",games[numero_juego].posP2[ctrtokens].y );
	    	}
	    	else{
	    		games[numero_juego].posP2[ctrtokens].x =  atoi(token_positions);
	    		printf("PLAYER 2 VAL: %d \n",games[numero_juego].posP2[ctrtokens].x );
	    	}
    	}
        ctrtokens++;
        token_positions = strtok(NULL,",");
    }

	// Fin de posicionamiento de barcos



	int jz = 0;
	while(jz != 5){
		if(play == PLAYER1){
			/* Esperar al estado A*/
		   	pthread_mutex_lock(&games[numero_juego].mutex);
		    while (games[numero_juego].state != STATE_A)
		        pthread_cond_wait(&games[numero_juego].condA, &games[numero_juego].mutex);
		    pthread_mutex_unlock(&games[numero_juego].mutex);


			pack = build_package(pack, pack.payload, "03", "10", "Tu turno!");
			send_package(pack, fifo_input);


			//rd = read (fifo_output,&jz,sizeof(int));
			pack = read_package(pack, fifo_output);
			printf("Respuesta del cliente 1: %d del juego %s \n", jz, pack.payload);

			/* Liberar al estado B*/
		    pthread_mutex_lock(&games[numero_juego].mutex);
	        games[numero_juego].state = STATE_B;
	        pthread_cond_signal(&games[numero_juego].condB);
	        pthread_mutex_unlock(&games[numero_juego].mutex);

		}
		else{
			/* Esperar al estado B*/
		   	pthread_mutex_lock(&games[numero_juego].mutex);
		    while (games[numero_juego].state != STATE_B)
		        pthread_cond_wait(&games[numero_juego].condB, &games[numero_juego].mutex);
		    pthread_mutex_unlock(&games[numero_juego].mutex);

	 		
			pack = build_package(pack, pack.payload, "03", "10", "Tu turno!");
			send_package(pack, fifo_input);

			
			//rd = read (fifo_output,&jz,sizeof(int));
			pack = read_package(pack, fifo_output);
			printf("Respuesta del cliente 2: %d del juego %s \n", jz, pack.payload);			

			/* Esperar al estado A*/
		    pthread_mutex_lock(&games[numero_juego].mutex);
	        games[numero_juego].state = STATE_A;
	        pthread_cond_signal(&games[numero_juego].condA);
	        pthread_mutex_unlock(&games[numero_juego].mutex);

		}

	}	

	close(fifo_input);
	close(fifo_output);
	printf("Fin de comunicaci贸n \n");
   
} 
Example #24
0
void mission_write_waypoint_list(void)
{
	uint32_t start_time, cur_time;

	waypoint_t *cur_wp = NULL;
	waypoint_t *new_waypoint;

	/* Getting the waypoint count */
	int new_waypoint_list_count = mavlink_msg_mission_count_get_count(&received_msg);

	waypoint_info.is_busy = true;

	int i;
	for(i = 0; i < new_waypoint_list_count; i++) {
		/* Generate the mission_request message */
		mavlink_msg_mission_request_pack(
			1, 0, &msg, 255, 0, i /* waypoint index */
		);

		send_package(&msg);

		/* Create a new node of waypoint */
		if (waypoint_info.waypoint_count > i) {
			new_waypoint = get_waypoint(waypoint_info.waypoint_list, i);
		} else { 
			/* Create a new node of waypoint */
			new_waypoint = create_waypoint_node();
		}

		start_time = get_system_time_ms();		

		/* Waiting for new message */
		while(received_msg.msgid != 39) {
			cur_time = get_system_time_ms();

			//Suspend the task to read the new message
			vTaskDelay(100);

			/* Time out, leave */
			if((cur_time - start_time) > TIMEOUT_CNT) {

				return;
			}
		}		

		/* Get the waypoint message */
		mavlink_msg_mission_item_decode(&received_msg, &(new_waypoint->data));

		/* Clear the received message */
		clear_message_id(&received_msg);

		/* insert the new waypoint at the end of the list */
		if(i == 0) {
			//First node of the list
			waypoint_info.waypoint_list = cur_wp = new_waypoint;
		} else {
			cur_wp->next = new_waypoint;
			cur_wp = cur_wp->next;
		}

	}

	/* Update the wayppoint, navigation manager */
	waypoint_info.waypoint_count = new_waypoint_list_count;
	waypoint_info.is_busy = false;
	nav_waypoint_list_is_updated = false; /* From navigation point of view */
	/* Send a mission ack Message at the end */
	mavlink_msg_mission_ack_pack(1, 0, &msg, 255, 0, 0);
	send_package(&msg);
}