26#include "state_handler.h"
28#include <core/exception.h>
29#include <logging/logger.h>
30#include <netcomm/socket/datagram.h>
33# include <interfaces/SoccerPenaltyInterface.h>
47static const uint32_t SPL_STRUCT_VERSION = 6;
49static const uint32_t SPL_STRUCT_VERSION = 7;
52static const uint8_t SPL_STATE_INITIAL = 0;
53static const uint8_t SPL_STATE_READY = 1;
54static const uint8_t SPL_STATE_SET = 2;
55static const uint8_t SPL_STATE_PLAYING = 3;
56static const uint8_t SPL_STATE_FINISHED = 4;
63static const uint8_t SPL_PENALTY_NONE = 0;
65static const uint8_t SPL_PENALTY_BALL_HOLDING = 1;
66static const uint8_t SPL_PENALTY_GOALIE_PUSHING = 2;
67static const uint8_t SPL_PENALTY_PLAYER_PUSHING = 3;
68static const uint8_t SPL_PENALTY_ILLEGAL_DEFENDER = 4;
69static const uint8_t SPL_PENALTY_ILLEGAL_DEFENSE = 5;
70static const uint8_t SPL_PENALTY_OBSTRUCTION = 6;
71static const uint8_t SPL_PENALTY_REQ_FOR_PICKUP = 7;
72static const uint8_t SPL_PENALTY_LEAVING = 8;
73static const uint8_t SPL_PENALTY_DAMAGE = 9;
74static const uint8_t SPL_PENALTY_MANUAL = 10;
90static const uint8_t SPL_TEAM_BLUE = 0;
91static const uint8_t SPL_TEAM_RED = 1;
98static const char SPL_GAMECONTROL_HEADER[SPL_HEADER_SIZE] = {
'R',
'G',
'm',
'e'};
114 unsigned short int broadcast_port,
115 unsigned int team_number,
116 unsigned int player_number)
118 player_number_ = player_number;
119 team_number_ = team_number;
123 s_->bind(broadcast_port);
125 penalty_ = SPL_PENALTY_NONE;
151 case SPL_TEAM_BLUE: our_team =
TEAM_CYAN;
break;
153 default: printf(
"Ignoring faulty packet\n");
return;
158 (our_team == TEAM_CYAN ? GOAL_BLUE
161 for (
unsigned int pl_num = 0; pl_num < SPL_MAX_NUM_PLAYERS; ++pl_num) {
162 if ((pl_num + 1) == player_number_) {
170 case SPL_PENALTY_BALL_HOLDING:
171 penalty_ = SoccerPenaltyInterface::SPL_PENALTY_BALL_HOLDING;
173 case SPL_PENALTY_GOALIE_PUSHING:
174 case SPL_PENALTY_PLAYER_PUSHING:
175 penalty_ = SoccerPenaltyInterface::SPL_PENALTY_PLAYER_PUSHING;
177 case SPL_PENALTY_ILLEGAL_DEFENDER:
178 case SPL_PENALTY_ILLEGAL_DEFENSE:
179 penalty_ = SoccerPenaltyInterface::SPL_PENALTY_ILLEGAL_DEFENDER;
181 case SPL_PENALTY_OBSTRUCTION:
182 penalty_ = SoccerPenaltyInterface::SPL_PENALTY_OBSTRUCTION;
184 case SPL_PENALTY_REQ_FOR_PICKUP:
185 penalty_ = SoccerPenaltyInterface::SPL_PENALTY_REQ_FOR_PICKUP;
187 case SPL_PENALTY_LEAVING:
188 penalty_ = SoccerPenaltyInterface::SPL_PENALTY_LEAVING_THE_FIELD;
190 case SPL_PENALTY_DAMAGE:
191 case SPL_PENALTY_MANUAL: penalty_ = SoccerPenaltyInterface::SPL_PENALTY_MANUAL;
break;
192 default: penalty_ = SoccerPenaltyInterface::SPL_PENALTY_NONE;
break;
202 switch (msg->
state) {
220 size_t bytes_read = s_->recv((
void *)&ctrlmsg,
sizeof(ctrlmsg));
221 if (bytes_read ==
sizeof(ctrlmsg)) {
222 if ((strncmp(ctrlmsg.
header, SPL_GAMECONTROL_HEADER, SPL_HEADER_SIZE) == 0)
223 && (ctrlmsg.
version == SPL_STRUCT_VERSION)) {
224 process_struct(&ctrlmsg);
229 logger_->
log_warn(
"SplRefBoxProcessor",
"Receiving failed, exception follows");
230 logger_->
log_warn(
"SplRefBoxProcessor", e);
249 size_t bytes_read = s_->recv((
void *)&ctrlmsg,
sizeof(ctrlmsg));
250 if (bytes_read ==
sizeof(ctrlmsg)) {
251 if ((strncmp(ctrlmsg.header, SPL_GAMECONTROL_HEADER, SPL_HEADER_SIZE) == 0)
252 && (ctrlmsg.version == SPL_STRUCT_VERSION)) {
253 process_struct(&ctrlmsg);
256 printf(
"Received illegal package\n");
RefBoxStateHandler * _rsh
Refbox state handler, set via set_handler()
virtual void set_gamestate(int game_state, fawkes::worldinfo_gamestate_team_t state_team)=0
Set current game state.
virtual void add_penalty(unsigned int penalty, unsigned int seconds_remaining)=0
Add penalty.
virtual void handle_refbox_state()=0
Process the information set up to now.
virtual void set_team_goal(fawkes::worldinfo_gamestate_team_t our_team, fawkes::worldinfo_gamestate_goalcolor_t goal_color)=0
Set team and goal info.
virtual void set_score(unsigned int score_cyan, unsigned int score_magenta)=0
Set score.
virtual void set_half(fawkes::worldinfo_gamestate_half_t half, bool kickoff=false)=0
Set current half of the game time.
~SplRefBoxProcessor()
Destructor.
bool check_connection()
Check if the connection is alive and reconnect.
void refbox_process()
Process incoming refbox communication.
SplRefBoxProcessor(fawkes::Logger *logger, unsigned short int broadcast_port, unsigned int team_number, unsigned int player_number)
Constructor.
Base class for exceptions in Fawkes.
int get_errno() noexcept
Get errno.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Fawkes library namespace.
worldinfo_gamestate_team_t
Team.
@ TEAM_MAGENTA
Magenta team.
SPL RefBox protocol game control struct.
uint8_t first_half
1 = game in first half, 0 otherwise
uint32_t version
version of the data structure
spl_teaminfo_t teams[2]
Info about the teams.
char header[SPL_HEADER_SIZE]
header to identify the structure
uint8_t state
state of the game (STATE_READY, STATE_PLAYING, etc.)
uint8_t kick_off_team
the next team to kick off
uint16_t secs_till_unpenalized
estimate of time till unpenalised
uint16_t penalty
penalty state of the player
uint8_t team_color
colour of the team
uint8_t score
team's score
spl_robotinfo_t players[SPL_MAX_NUM_PLAYERS]
the team's players
uint8_t team_number
unique team number