net_inbound.c
Go to the documentation of this file.
1 
5 #include "net.h"
6 #include <stdlib.h>
7 #include "main.h"
8 #include "UART2.h"
9 
10 #define START_DELIMITER 0x7E
11 
12 typedef enum {
13  EMPTY = 0, // Can write new data here
14  BUSY = 1, // Data being written here
15  READY = 2 // Data ready
16 } PacketStatus;
17 
19 int inbuff_start = 0;
20 int inbuff_end = 0;
21 
24 
25 static int packetPos = 0;
26 static int payloadPos = 0;
28 
29 // Clean up the command
30 void destroyCommand( struct command* cmd ) {
31  free( cmd );
32  cmd = 0;
33 }
34 
35 // Get the
36 struct command* popCommand() {
37  struct command* cmd = inBuffer[inbuff_start];
38  if ( ! cmd ) return 0;
39  inBuffer[inbuff_start] = 0;
41  return cmd;
42 }
43 
44 int pushCommand(struct command* cmd) {
46  return 0; // If no room for commands, fail
47  }
48  inBuffer[inbuff_end] = cmd; // Insert cmd at end of queue
49  inbuff_end = ( inbuff_end + 1 ) % INBOUND_QUEUE_SIZE; // increment handling wrap
50  return 1;
51 }
52 
53 struct command* createCommand( char* rawPacket ) {
54  struct command* cmd = malloc(sizeof(struct command));
55  if ( ! cmd ) { // Malloc failed ?
56  return 0;
57  }
58  int k;
59  char temp[128];
60  for (k = 0; k < 128; k++) temp[k] = rawPacket[k];
61  cmd->data_length = rawPacket[2];
62  cmd->cmd = rawPacket[15];
63  int i;
64  int j = 0;
65  for ( i = 16; i < 15 + cmd->data_length - 12; i++ ) { // Received packet payload starts at 15 and has 12 bytes before len
66  cmd->data[j++] = rawPacket[i];
67  }
68  cmd->data[j] = '\0'; // Null terminate the string so can use SendUart
69  return cmd;
70 }
71 
72 // Return 1 if packet is valid
73 int checkPacket(char* rawPacket) {
74  unsigned int i = 2;
75  char packetLength = rawPacket[i++];
76  char checksum = 0;
77  char packetChecksum = 0;
78 
79  for (i = 3; i < packetLength + 3; i++){
80  checksum += rawPacket[i];
81  }
82  packetChecksum = 0xFF - rawPacket[i];
83  if (checksum == packetChecksum){
84  return 1; // TODO: checksums are for suckers, because fuck you, thats why
85 
86  }
87  else{
88  return 0;
89  }
90 }
91 
93  int i;
94  for ( i = 0; i < RAW_PACKET_BUFFER_SIZE; i++ ) {
95  if ( rawPacketStatus[i] == READY && checkPacket(rawPackets[i]) ) {
96  struct command* cmd = createCommand( rawPackets[i] );
97  if ( cmd ) { // create command was successful ?
98  pushCommand( cmd ); // queue it up
99  rawPacketStatus[i] = EMPTY; // buffer is now good for writing another packet
100  }
101  }
102  }
103  if ( rawPacketStatus[0] == EMPTY ) {
104  rawPacketStatus[0] = BUSY;
105  }
106 }
107 
108 #if !PATH_MANAGER
109 void __attribute__((__interrupt__, no_auto_psv)) _U2RXInterrupt(void) {
110  unsigned char data = U2RXREG;
111  if ( rawPacketStatus[packetPos] != BUSY ) { // no buffer available to write
113  IFS1bits.U2RXIF = 0;
114  return;
115  }
116  switch ( payloadPos ) {
117  case 0:
118  if ( data != START_DELIMITER ) {
119 // if (U2STAbits.OERR == 1)
120 // U2STAbits.OERR = 0;
121  IFS1bits.U2RXIF = 0;
122  return;
123  }
124  break;
125  case 1:
126  if ( data != 0 ) {
127  payloadPos = 0;
128  IFS1bits.U2RXIF = 0;
129  return; // packet length < 100 bytes, so msb == 0
130  }
131  break;
132  case 2:
134  break;
135  default: // Normally, don't do anything special
136  break;
137  }
139  if ( payloadPos && payloadPos == payloadLength[packetPos] + 3 + 1) { // at end of packet
141  payloadPos = 0;
143  if ( rawPacketStatus[packetPos] == EMPTY ) {
145  }
146  }
147  IFS1bits.U2RXIF = 0;
148 }
149 #endif
unsigned int i
int inbuff_start
Definition: net_inbound.c:19
List of defines required for XBEE communication and telemetry.
char rawPackets[RAW_PACKET_BUFFER_SIZE][128]
Definition: net_inbound.c:22
Definition: net.h:149
static int payloadPos
Definition: net_inbound.c:26
#define RAW_PACKET_BUFFER_SIZE
Definition: net.h:50
Settings for UART2 communication used for telemetry link.
unsigned char data_length
Definition: net.h:151
#define INBOUND_QUEUE_SIZE
Definition: net.h:32
PacketStatus
Definition: net_inbound.c:12
int checkPacket(char *rawPacket)
Definition: net_inbound.c:73
void __attribute__((__interrupt__, no_auto_psv))
Definition: net_inbound.c:109
struct command * popCommand()
Definition: net_inbound.c:36
struct command * inBuffer[INBOUND_QUEUE_SIZE]
Definition: net_inbound.c:18
void destroyCommand(struct command *cmd)
Definition: net_inbound.c:30
unsigned char data
Definition: GPS.c:117
static int payloadLength[RAW_PACKET_BUFFER_SIZE]
Definition: net_inbound.c:27
int pushCommand(struct command *cmd)
Definition: net_inbound.c:44
unsigned char data[101]
Definition: net.h:152
PacketStatus rawPacketStatus[RAW_PACKET_BUFFER_SIZE]
Definition: net_inbound.c:23
static int packetPos
Definition: net_inbound.c:25
unsigned char cmd
Definition: net.h:150
#define START_DELIMITER
net_inbound.c
Definition: net_inbound.c:10
int inbuff_end
Definition: net_inbound.c:20
struct command * createCommand(char *rawPacket)
Definition: net_inbound.c:53
void inboundBufferMaintenance(void)
Definition: net_inbound.c:92