Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
task_communicate.c
Go to the documentation of this file.
1 
11 /* -- Includes -- */
12 /* system libraries */
13 
14 /* project libraries */
15 #include "task.h"
16 
20 void task_communicate(void)
21 {
22  // Check if task is enabled
24  {
25  // Loacal variables
26  void *data; // Void pointer for data
27  int data_type; // Data type
28 
29  // UDP Packet
30  char udp_packet[g_config.udp_packet_size];
31  int udp_packet_len;
32 
33  // Protocol
34  protocol_t packet;
35 
36  //Start the new sequence
37  int seq = 0; // Massi thing
38  //In principle I want to send all the data in the buffer
39  int last_id = g_list_send->count; // Massi thing
40 
41  /* ----- LAB 2 STARTS HERE ----- */
42 
43  /* --- DEFINE YOUR VARIABLES HERE ---
44  * WARNING:
45  * - remember to free the memory if you use dynamic allocation!
46  */
47 
48 
49  /* --- Send Data --- */
50  while(g_list_send->count != 0)
51  {
52  seq++;
53 
54  // Allocate memory for data structure
55  switch(g_list_send->first->data_type)
56  {
57  // Robot pose
59  data = (void *)malloc(sizeof(robot_t));
60  break;
61  // Victim information
63  data = (void *)malloc(sizeof(victim_t));
64  break;
65  // Pheromone map
67  data = (void *)malloc(sizeof(pheromone_map_sector_t));
68  break;
69  // Command
71  data = (void *)malloc(sizeof(command_t));
72  break;
74  data = (void *)malloc(sizeof(stream_t));
75  break;
76  // Other
77  default :
78  // Do nothing
79  continue;
80  break;
81  }
82 
83  // Get data from the list
85 
86  // Encode data into UDP packet
87  protocol_encode(udp_packet,
88  &udp_packet_len,
93  seq,
95  last_id,
96  data_type,
97  data);
98 
99  // Send data
100  udp_broadcast(g_udps, udp_packet, udp_packet_len);
101 
102  // Free memory
103  free(data);
104  }
105 
106 
107 
108  /* --- Receive Data --- */
109  // Receive packets, decode and forward to proper process
110 
111  // Receive UDP packet
112  while(udp_receive(g_udps, udp_packet, &udp_packet_len) == s_OK)
113  {
114  // Decode packet
115  //printf("%s\n",udp_packet);
116  if(protocol_decode(&packet, udp_packet, udp_packet_len, g_config.robot_id, g_config.robot_team) == s_OK)
117  {
118  // Now decoding depends on the type of the packet
119  switch(packet.type)
120  {
121  // ACK
122  case s_PROTOCOL_TYPE_ACK :
123  // Do nothing
124  break;
125 
126  //Massi: go_ahead packet
128  {
129  // Declare go ahead command
130  command_t go_ahead;
131  go_ahead.cmd = s_CMD_GO_AHEAD;
132  // Redirect to mission by adding it to the queue
134 
135  // Debuging stuff
136  debug_printf("GO_AHEAD RECEIVED for robot %d team %d\n",packet.recv_id,packet.send_team);
137  // Calculate time from packet (ms and s)
138  int send_time_s = floor(packet.send_time / 1000);
139  int send_time_ms = packet.send_time % 1000;
140  int now = floor(((long long)timelib_unix_timestamp() % 60000) / 1000);
141  debug_printf("GO_AHEAD_TIME: %d (%d)\n",send_time_s,now);
142 
143  break;
144  }
145  // Data
146  case s_PROTOCOL_TYPE_DATA :
147  // Continue depending on the data type
148  switch(packet.data_type)
149  {
150  // Robot pose
152  debug_printf("received robot\n");
153  // Do nothing
154  break;
155  // Victim information
157  debug_printf("received victim\n");
158  // Redirect to mission by adding it to the queue
160  break;
161  // Pheromone map
163  debug_printf("received pheromone\n");
164  // Redirect to navigate by adding it to the queue
166 
167  break;
168  // Command
170  debug_printf("received CMD\n");
171  // Redirect to mission by adding it to the queue
173  break;
175  debug_printf("received data stream item\n");
176  break;
177  // Other
178  default :
179  // Do nothing
180  break;
181  }
182  // Other ?
183  default:
184  // Do nothing
185  break;
186  }
187 
188  // Free memory (only if data packet was received!)
189  if(packet.type == s_PROTOCOL_TYPE_DATA)
190  free(packet.data);
191  }
192  }
193 
194  // Increase msg sequance id
196  }
197 }
void debug_printf(const char *format,...)
Definition: debug.c:25
#define s_DATA_STRUCT_TYPE_ROBOT
Definition: def.h:68
int udp_receive(udp_t *udp, char *packet, int *len)
Definition: udp.c:178
queue_t * g_queue_navigate
Definition: task.c:36
#define s_PROTOCOL_TYPE_DATA
Definition: protocol.h:45
doublylinkedlist_node_t * first
int g_message_sequence_id
Definition: task.c:57
task_t g_task_communicate
Definition: task.c:44
int enabled
Definition: task.h:46
config_t g_config
Definition: config.c:22
#define s_PROTOCOL_ADDR_BROADCAST
Definition: protocol.h:41
Stream structure.
Definition: def.h:48
int udp_packet_size
Definition: config.h:62
void queue_enqueue(queue_t *qs, void *data, int data_type)
Definition: queue.c:62
doublylinkedlist_t * g_list_send
Definition: task.c:32
#define s_PROTOCOL_TYPE_ACK
Definition: protocol.h:44
Victim structure.
Definition: def.h:28
int protocol_decode(protocol_t *packet, char *udp_packet, int len, int robot_id, int robot_team)
Definition: protocol.c:169
Command structure.
Definition: def.h:39
void task_communicate(void)
#define s_OK
Definition: def.h:64
#define s_CMD_GO_AHEAD
Definition: def.h:77
double timelib_unix_timestamp()
Definition: timelib.c:123
int robot_team
Definition: config.h:45
UDP structure.
Definition: protocol.h:22
#define s_PROTOCOL_TYPE_GO_AHEAD
Definition: protocol.h:46
udp_t * g_udps
Definition: task.c:29
#define s_DATA_STRUCT_TYPE_PHEROMONE
Definition: def.h:70
Robot structure.
Definition: robot.h:25
#define s_DATA_STRUCT_TYPE_STREAM
Definition: def.h:72
queue_t * g_queue_mission
Definition: task.c:35
int robot_id
Definition: config.h:44
#define s_DATA_STRUCT_TYPE_CMD
Definition: def.h:71
#define s_TRUE
Definition: def.h:60
int protocol_encode(char *udp_packet, int *len, int recv_id, int send_id, int send_team, char type, int seqno, int seqid, int seq_lid, int data_type, void *data)
Definition: protocol.c:43
int udp_broadcast(udp_t *udp, char *packet, int len)
Definition: udp.c:159
#define s_DATA_STRUCT_TYPE_VICTIM
Definition: def.h:69
void doublylinkedlist_remove(doublylinkedlist_t *dlls, doublylinkedlist_node_t *node, void *data, int *data_type)
Pheromone Map Sector structure.
Definition: pheromone.h:58