Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
protocol.c
Go to the documentation of this file.
1 
11  /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h> /* Standard input/output definitions */
14 #include <string.h> /* String function definitions */
15 #include <stdlib.h>
16 #include <math.h>
17 /* project libraries */
18 #include "protocol.h"
19 #include "config.h"
20 #include "def.h"
21 #include "rfid.h"
22 #include "openinterface.h"
23 #include "robot.h"
24 #include "pheromone.h"
25 #include "timelib.h"
26 
27 /* -- Defines -- */
28 
29 /* -- Functions -- */
30 
43 int protocol_encode(char *udp_packet,
44  int *len,
45  int recv_id,
46  int send_id,
47  int send_team,
48  char type,
49  int seqno,
50  int seqid,
51  int seq_lid,
52  int data_type,
53  void *data)
54 {
55  // Reset length
56  *len = 0;
57 
58 
59  int timestamp = (long long)timelib_unix_timestamp() % 60000;
60 
61  // Encode Reciever ID, Sender ID, Type, Timestamp, SequenceNumber, SequenceID, SequenceLastID
62  *len += sprintf(udp_packet,"%d,%d,%d,%c,%d,%d,%d,%d",recv_id, send_id, send_team, type,timestamp,seqno,seqid,seq_lid);
63 
64  // Continue depending on the packet type
65  switch(type)
66  {
67  // ACK
68  case s_PROTOCOL_TYPE_ACK :
69 
70  // Do nothing
71  break;
72 
73  //Go ahead
75  //This should not be done by the robot
76  break;
77 
78  // Data
80 
81  // Encode Data type
82  *len += sprintf(udp_packet + *len,",%d",data_type);
83 
84  // Continue depending on the data type
85  switch(data_type)
86  {
87  // Robot
89 
90  // Encode Estimated robot pose
91  *len += sprintf(udp_packet + *len,",%d,%d,%d",
92  ((robot_t *)data)->x,
93  ((robot_t *)data)->y,
94  (int)(((robot_t *)data)->a * (180/M_PI)));
95 
96  break;
97  // Victim
99 
100  // Encode Victim information
101  *len += sprintf(udp_packet + *len,",%d,%d,%s",
102  ((victim_t *)data)->x,
103  ((victim_t *)data)->y,
104  ((victim_t *)data)->id);
105 
106  break;
107  // Pheromone map
109 
110  // Encode pheromone information
111  *len += sprintf(udp_packet + *len,",%d,%d,%d,",
112  ((pheromone_map_sector_t *)data)->num,
113  ((pheromone_map_sector_t *)data)->size,
114  ((pheromone_map_sector_t *)data)->timestamp);
115 
116  // Encode pheromone map data
117  memcpy(udp_packet + *len, ((pheromone_map_sector_t *)data)->data, ((pheromone_map_sector_t *)data)->size);
118  *len += ((pheromone_map_sector_t *)data)->size;
119 
120  break;
121  // Command
123 
124  // Encode Command information
125  *len += sprintf(udp_packet + *len,",%d",
126  ((command_t *)data)->cmd);
127 
128  break;
130 
131  // Encode stream data information
132  *len += sprintf(udp_packet + *len,",%ld,",
133  ((stream_t *)data)->counter);
134 
135  // Encode stream data
136  memcpy(udp_packet + *len, ((stream_t *)data)->data, ((stream_t *)data)->size);
137  *len += ((stream_t *)data)->size;
138 
139  break;
140  // Other ?
141  default:
142  // Return error as unknown data type
143  return s_ERROR;
144  break;
145  }
146  break;
147  // Other ?
148  default:
149  // Return error as unknown packet type
150  return s_ERROR;
151  break;
152  }
153 
154  // End string
155  udp_packet[*len] = '\0';
156 
157  return s_OK;
158 }
159 
169 int protocol_decode(protocol_t *packet, char *udp_packet, int len, int robot_id, int robot_team)
170 {
171  // Local variables
172  char *end_ptr, *pch;
173 
174  // Process data into struct
175  // Save Reciever ID
176  pch = strtok(udp_packet, ",");
177  packet->recv_id = strtol(pch, &end_ptr, 10);
178  // Save Sender ID
179  pch = strtok(NULL, ",");
180  packet->send_id = strtol(pch, &end_ptr, 10);
181  // Check if it is not sent by yourself. If yes, drop.
182  if(packet->send_id == robot_id)
183  return s_ERROR;
184  // Save Sender Team
185  pch = strtok(NULL, ",");
186  packet->send_team = strtol(pch, &end_ptr, 10);
187  // Check if it is not sent by other team. If yes, drop.
188  // Or if send_team = 0, then ignore team ID
189  if(packet->send_team != robot_team && robot_team != 0)
190  return s_ERROR;
191 
192  // Save Packet type
193  pch = strtok(NULL, ",");
194  memcpy(&packet->type, pch, 1);
195 
196  //Save packet timestamp
197  pch = strtok(NULL, ",");
198  packet->send_time = strtol(pch, &end_ptr, 10);
199 
200  //Save the packet sequence number
201  pch = strtok(NULL, ",");
202  packet->pkt_seqno= strtol(pch, &end_ptr, 10);
203 
204  //Save the sequence id
205  pch = strtok(NULL, ",");
206  packet->pkt_seqid= strtol(pch, &end_ptr, 10);
207 
208  //Save the last sequence id
209  pch = strtok(NULL, ",");
210  packet->seq_last_id= strtol(pch, &end_ptr, 10);
211 
212  // Now decoding depends on the type of the packet
213  switch(packet->type)
214  {
215  // ACK
216  case s_PROTOCOL_TYPE_ACK :
217  // Do nothing
218  break;
219 
221  //There is no data
222 
223  break;
224 
225  // Data
226  case s_PROTOCOL_TYPE_DATA :
227  // Save Data type
228  pch = strtok(NULL, ",");
229  packet->data_type = strtol(pch, &end_ptr, 10);
230 
231  // Continue depending on the data type
232  switch(packet->data_type)
233  {
234  // Robot
236 
237  // Allocate memory for robot structure
238  packet->data = (void *)malloc(sizeof(robot_t));
239 
240  // Save data in structure
241  // Robot X
242  pch = strtok(NULL, ",");
243  ((robot_t *)packet->data)->x = strtol(pch, &end_ptr, 10);
244  // Robot Y
245  pch = strtok(NULL, ",");
246  ((robot_t *)packet->data)->y = strtol(pch, &end_ptr, 10);
247  // Robot heading angle
248  pch = strtok(NULL, ",");
249  ((robot_t *)packet->data)->a = (float)strtol(pch, &end_ptr, 10) * (float)(M_PI/180);
250 
251  break;
252  // Victim
254 
255  // Allocate memory for victim structure
256  packet->data = (void *)malloc(sizeof(victim_t));
257 
258  // Save data in structure
259  // Victims X
260  pch = strtok(NULL, ",");
261  ((victim_t *)packet->data)->x = strtol(pch, &end_ptr, 10);
262  // Victims Y
263  pch = strtok(NULL, ",");
264  ((victim_t *)packet->data)->y = strtol(pch, &end_ptr, 10);
265  // Victims ID
266  pch = strtok(NULL, ",");
267  memcpy(((victim_t *)packet->data)->id, pch, 11);
268 
269  break;
270  // Pheromone map
272 
273  // Allocate memory for pheromone map sector structure
274  packet->data = (void *)malloc(sizeof(pheromone_map_sector_t));
275 
276  // Save data in structure
277  // Map sector number
278  pch = strtok(NULL, ",");
279  ((pheromone_map_sector_t *)packet->data)->num = strtol(pch, &end_ptr, 10);
280  // Map sector size
281  pch = strtok(NULL, ",");
282  ((pheromone_map_sector_t *)packet->data)->size = strtol(pch, &end_ptr, 10);
283  // Map sector timestamp
284  pch = strtok(NULL, ",");
285  ((pheromone_map_sector_t *)packet->data)->timestamp = strtol(pch, &end_ptr, 10);
286 
287 
288  //printf("??? len: %d\n", len);
289  //printf("memcpy -|%c|%c|%x|%x|\n", *(end_ptr-2),*(end_ptr-1),*(end_ptr),*(end_ptr+1));
290  // Map sector data
291  pch = strtok(NULL, ",");
292  // Copy data
293  memcpy(((pheromone_map_sector_t *)packet->data)->data, pch, ((pheromone_map_sector_t *)packet->data)->size);
294  //printf("memcpy_end\n");
295 
296 
297  break;
298  // Command
300 
301  // Allocate memory for command structure
302  packet->data = (void *)malloc(sizeof(command_t));
303 
304  // Save data in structure
305  // Command
306  pch = strtok(NULL, " ,");
307  ((command_t *)packet->data)->cmd = strtol(pch, &end_ptr, 10);
308 
309  break;
310 
311 
313  // Allocate memory for stream structure
314  packet->data = (void *)malloc(sizeof(stream_t));
315 
316  // Save data in structure
317  // Stream counter
318  pch = strtok(NULL, ",");
319  ((stream_t *)packet->data)->counter = strtol(pch, &end_ptr, 10);
320  // Stream size
321  pch = strtok(NULL, ",");
322  ((stream_t *)packet->data)->size = strtol(pch, &end_ptr, 10);
323 
324  // Stream data
325  pch = strtok(NULL, ",");
326  // Copy data
327  memcpy(((stream_t *)packet->data)->data, pch, ((stream_t *)packet->data)->size);
328 
329  break;
330 
331  // Other ?
332  default:
333  // Return error as unknown data type
334  return s_ERROR;
335  break;
336  }
337 
338  break;
339  // Other ?
340  default:
341  // Return error as unknown packet type
342  return s_ERROR;
343  break;
344  }
345 
346  return s_OK;
347 }
348 
356 {
357  return 0;
358 }
#define s_DATA_STRUCT_TYPE_ROBOT
Definition: def.h:68
#define s_PROTOCOL_TYPE_DATA
Definition: protocol.h:45
Stream structure.
Definition: def.h:48
#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
#define s_OK
Definition: def.h:64
double timelib_unix_timestamp()
Definition: timelib.c:123
UDP structure.
Definition: protocol.h:22
#define s_PROTOCOL_TYPE_GO_AHEAD
Definition: protocol.h:46
int protocol_destroy()
Definition: protocol.c:355
#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
#define s_ERROR
Definition: def.h:65
#define s_DATA_STRUCT_TYPE_CMD
Definition: def.h:71
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
#define s_DATA_STRUCT_TYPE_VICTIM
Definition: def.h:69
Pheromone Map Sector structure.
Definition: pheromone.h:58