Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
udp.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2012-2013 Freek van Tienen and Dino Hensen
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 */
22 
24 #include "fms/fms_network.h"
25 #include <string.h>
26 
27 //Check if variables are set and else define them
28 #ifndef LINK_HOST
29 #define LINK_HOST "192.168.1.0"
30 #endif
31 #ifndef LINK_PORT
32 #define LINK_PORT 4242
33 #endif
34 #ifndef DATALINK_PORT
35 #define DATALINK_PORT 4243
36 #endif
37 #ifndef FMS_NETWORK_BROADCAST
38 #define FMS_NETWORK_BROADCAST TRUE
39 #endif
40 
41 //Define the buffer, check bytes and FmsNetwork
42 char udp_buffer[1496];
43 unsigned char udp_read_buffer[128];
46 struct FmsNetwork* network;
47 
48 void udp_init( void ) {
50  udp_buffer_id = 0;
51 }
52 
53 void udp_transmit( uint8_t data ) {
54  udp_buffer[udp_buffer_id] = data;
55  udp_buffer_id++;
56 }
57 
58 void udp_send( void ) {
59  network_write(network, udp_buffer, udp_buffer_id);
60  udp_buffer_id =0;
61 }
62 
63 void udp_receive( void ) {
64  //First check if it isn't already having a message
65  if(dl_msg_available == TRUE) {
66  return;
67  }
68 
69  //Read from the network
71 
72  //Parse the packet
73  if(udp_read_buffer[0] == STX) {
74  uint8_t size = udp_read_buffer[1]-4; // minus STX, LENGTH, CK_A, CK_B
75  uint8_t ck_aa, ck_bb;
76  ck_aa = ck_bb = size+4;
77 
78  // index-offset plus 2 for STX and LENGTH
79  for (int i = 2; i < size+2; i++) {
80  dl_buffer[i-2] = udp_read_buffer[i];
81  ck_aa += udp_read_buffer[i];
82  ck_bb += ck_aa;
83  }
84 
85  // if both checksums are good, tell datalink that the message is available
86  if (udp_read_buffer[2+size] == ck_aa && udp_read_buffer[2+size+1] == ck_bb) {
88  }
89 
90  }
91 
92  memset(&udp_read_buffer[0], 0, sizeof(udp_read_buffer));
93 }
94 
unsigned short uint16_t
Definition: types.h:16
#define DATALINK_PORT
Definition: udp.c:35
void udp_send(void)
Definition: udp.c:58
void udp_receive(void)
Definition: udp.c:63
unsigned char udp_read_buffer[128]
Definition: udp.c:43
#define LINK_HOST
Definition: udp.c:29
void udp_transmit(uint8_t data)
Definition: udp.c:53
char udp_buffer[1496]
Definition: udp.c:42
#define LINK_PORT
Definition: udp.c:32
uint8_t ck_b
Definition: udp.c:45
bool_t dl_msg_available
Definition: main_demo5.c:56
uint8_t ck_a
Definition: udp.c:45
#define TRUE
Definition: imu_chimu.h:144
unsigned char uint8_t
Definition: types.h:14
#define FMS_NETWORK_BROADCAST
Definition: udp.c:38
#define STX
uint16_t udp_buffer_id
Definition: udp.c:44
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:59
struct FmsNetwork * network
Definition: udp.c:46
void udp_init(void)
Definition: udp.c:48
#define TRANSPORT_PAYLOAD_LEN
Definition: transport.h:34