Clean up "serval.h" a bit

Move some defs to more appropriate header file
Remove unused function prototypes
Remove unused SLIP and CRC32 code
This commit is contained in:
Andrew Bettison 2014-04-07 13:57:47 +09:30
parent 81d349211a
commit 97850835da
7 changed files with 17 additions and 673 deletions

View File

@ -1679,49 +1679,6 @@ int app_rhizome_add_file(const struct cli_parsed *parsed, struct cli_context *co
return status;
}
int app_slip_test(const struct cli_parsed *parsed, struct cli_context *context)
{
const char *seed = NULL;
const char *iterations = NULL;
const char *duration = NULL;
if ( cli_arg(parsed, "--seed", &seed, cli_uint, NULL) == -1
|| cli_arg(parsed, "--duration", &duration, cli_uint, NULL) == -1
|| cli_arg(parsed, "--iterations", &iterations, cli_uint, NULL) == -1)
return -1;
if (seed)
srandom(atoi(seed));
int maxcount = iterations ? atoi(iterations) : duration ? 0 : 1000;
time_ms_t start = duration ? gettime_ms() : 0;
time_ms_t end = duration ? start + atoi(duration) * (time_ms_t) 1000 : 0;
int count;
for (count = 0; maxcount == 0 || count < maxcount; ++count) {
if (end && gettime_ms() >= end)
break;
unsigned char bufin[8192];
unsigned char bufout[8192];
int len=1+random()%1500;
int i;
for(i=0;i<len;i++) bufin[i]=random()&0xff;
struct slip_decode_state state;
bzero(&state,sizeof state);
int outlen=slip_encode(SLIP_FORMAT_UPPER7,bufin,len,bufout,8192);
for(i=0;i<outlen;i++) upper7_decode(&state,bufout[i]);
uint32_t crc=Crc32_ComputeBuf( 0, state.dst, state.packet_length);
if (crc!=state.crc) {
WHYF("CRC error (%08x vs %08x)",crc,state.crc);
dump("input",bufin,len);
dump("encoded",bufout,outlen);
dump("decoded",state.dst,state.packet_length);
return 1;
} else {
if (!(count%1000))
cli_printf(context, "."); cli_flush(context);
}
}
cli_printf(context, "Test passed.\n");
return 0;
}
int app_rhizome_import_bundle(const struct cli_parsed *parsed, struct cli_context *context)
{
if (config.debug.verbose)
@ -3174,8 +3131,6 @@ struct cli_schema command_line_options[]={
"Run memory speed test"},
{app_byteorder_test,{"test","byteorder",NULL}, 0,
"Run byte order handling test"},
{app_slip_test,{"test","slip","[--seed=<N>]","[--duration=<seconds>|--iterations=<N>]",NULL}, 0,
"Run serial encapsulation test"},
{app_msp_connection,{"msp", "listen", "[--once]", "[--forward=<local_port>]", "<port>", NULL}, 0,
"Listen for incoming connections"},
{app_msp_connection,{"msp", "connect", "[--once]", "[--forward=<local_port>]", "<sid>", "<port>", NULL}, 0,

104
crc32.c
View File

@ -1,104 +0,0 @@
/*----------------------------------------------------------------------------*\
* CRC-32 version 2.0.0 by Craig Bruce, 2006-04-29.
*
* This program generates the CRC-32 values for the files named in the
* command-line arguments. These are the same CRC-32 values used by GZIP,
* PKZIP, and ZMODEM. The Crc32_ComputeBuf() can also be detached and
* used independently.
*
* THIS PROGRAM IS PUBLIC-DOMAIN SOFTWARE.
*
* Based on the byte-oriented implementation "File Verification Using CRC"
* by Mark R. Nelson in Dr. Dobb's Journal, May 1992, pp. 64-67.
*
* v1.0.0: original release.
* v1.0.1: fixed printf formats.
* v1.0.2: fixed something else.
* v1.0.3: replaced CRC constant table by generator function.
* v1.0.4: reformatted code, made ANSI C. 1994-12-05.
* v2.0.0: rewrote to use memory buffer & static table, 2006-04-29.
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdint.h>
/*----------------------------------------------------------------------------*\
* Local functions
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
* NAME:
* Crc32_ComputeBuf() - computes the CRC-32 value of a memory buffer
* DESCRIPTION:
* Computes or accumulates the CRC-32 value for a memory buffer.
* The 'inCrc32' gives a previously accumulated CRC-32 value to allow
* a CRC to be generated for multiple sequential buffer-fuls of data.
* The 'inCrc32' for the first buffer must be zero.
* ARGUMENTS:
* inCrc32 - accumulated CRC-32 value, must be 0 on first call
* buf - buffer to compute CRC-32 value for
* bufLen - number of bytes in buffer
* RETURNS:
* crc32 - computed CRC-32 value
* ERRORS:
* (no errors are possible)
\*----------------------------------------------------------------------------*/
uint32_t Crc32_ComputeBuf( uint32_t inCrc32, const void *buf,
size_t bufLen )
{
static const uint32_t crcTable[256] = {
0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,
0x9E6495A3,0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,
0xE7B82D07,0x90BF1D91,0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,
0x6DDDE4EB,0xF4D4B551,0x83D385C7,0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,
0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5,0x3B6E20C8,0x4C69105E,0xD56041E4,
0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,0x35B5A8FA,0x42B2986C,
0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59,0x26D930AC,
0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F,
0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,
0xB6662D3D,0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,
0x9FBFE4A5,0xE8B8D433,0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,
0x086D3D2D,0x91646C97,0xE6635C01,0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,
0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,0x65B0D9C6,0x12B7E950,0x8BBEB8EA,
0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65,0x4DB26158,0x3AB551CE,
0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB,0x4369E96A,
0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9,
0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,
0xCE61E49F,0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,
0xB7BD5C3B,0xC0BA6CAD,0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,
0x9DD277AF,0x04DB2615,0x73DC1683,0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,
0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1,0xF00F9344,0x8708A3D2,0x1E01F268,
0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7,0xFED41B76,0x89D32BE0,
0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5,0xD6D6A3E8,
0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B,
0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,
0x4669BE79,0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,
0x220216B9,0x5505262F,0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,
0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D,0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,
0x9C0906A9,0xEB0E363F,0x72076785,0x05005713,0x95BF4A82,0xE2B87A14,0x7BB12BAE,
0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21,0x86D3D2D4,0xF1D4E242,
0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,0x88085AE6,
0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45,
0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,
0x3E6E77DB,0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,
0x47B2CF7F,0x30B5FFE9,0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,
0xCDD70693,0x54DE5729,0x23D967BF,0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,
0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D };
uint32_t crc32;
unsigned char *byteBuf;
size_t i;
/** accumulate crc32 for buffer **/
crc32 = inCrc32 ^ 0xFFFFFFFF;
byteBuf = (unsigned char*) buf;
for (i=0; i < bufLen; i++) {
crc32 = (crc32 >> 8) ^ crcTable[ (crc32 ^ byteBuf[i]) & 0xFF ];
}
return( crc32 ^ 0xFFFFFFFF );
}
/*----------------------------------------------------------------------------*\
* END OF MODULE: crc32.c
\*----------------------------------------------------------------------------*/

View File

@ -91,6 +91,8 @@ struct sched_ent{
int _poll_index;
};
#define STRUCT_SCHED_ENT_UNUSED {.poll={.fd=-1}, ._poll_index=-1,}
int is_scheduled(const struct sched_ent *alarm);
int is_watching(struct sched_ent *alarm);
int _schedule(struct __sourceloc, struct sched_ent *alarm);

View File

@ -7,6 +7,20 @@
#define INTERFACE_STATE_UP 1
#define INTERFACE_STATE_DETECTING 2
struct limit_state{
// length of time for a burst
time_ms_t burst_length;
// how many in a burst
int burst_size;
// how many have we sent in this burst so far
int sent;
// when can we allow another burst
time_ms_t next_interval;
};
time_ms_t limit_next_allowed(struct limit_state *state);
int limit_is_allowed(struct limit_state *state);
int limit_init(struct limit_state *state, int rate_micro_seconds);
struct overlay_interface;

View File

@ -202,8 +202,6 @@ struct cli_parsed;
extern int servalShutdown;
extern char *gatewayspec;
int rhizome_enabled();
int rhizome_http_server_running();
@ -211,11 +209,6 @@ int rhizome_http_server_running();
extern int peer_count;
extern struct in_addr peers[MAX_PEERS];
extern char *outputtemplate;
extern char *instrumentation_file;
extern char *batman_socket;
extern char *batman_peerfile;
struct subscriber;
struct decode_context;
struct socket_address;
@ -223,27 +216,10 @@ struct overlay_interface;
struct network_destination;
struct internal_mdp_header;
/* Make sure we have space to put bytes of the packet as we go along */
#define CHECK_PACKET_LEN(B) {if (((*packet_len)+(B))>=packet_maxlen) { return WHY("Packet composition ran out of space."); } }
struct limit_state{
// length of time for a burst
time_ms_t burst_length;
// how many in a burst
int burst_size;
// how many have we sent in this burst so far
int sent;
// when can we allow another burst
time_ms_t next_interval;
};
struct overlay_buffer;
struct overlay_frame;
struct broadcast;
#define STRUCT_SCHED_ENT_UNUSED {.poll={.fd=-1}, ._poll_index=-1,}
extern int overlayMode;
// Specify the size of the receive buffer.
@ -290,8 +266,6 @@ int server_remove_stopfile();
int server_check_stopfile();
void overlay_mdp_clean_socket_files();
void serverCleanUp();
int isTransactionInCache(unsigned char *transaction_id);
void insertTransactionInCache(unsigned char *transaction_id);
int overlay_forward_payload(struct overlay_frame *f);
int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, size_t len,
@ -373,6 +347,7 @@ int directory_registration();
int directory_service_init();
int app_nonce_test(const struct cli_parsed *parsed, struct cli_context *context);
int app_slip_test(const struct cli_parsed *parsed, struct cli_context *context);
int app_rhizome_direct_sync(const struct cli_parsed *parsed, struct cli_context *context);
int app_monitor_cli(const struct cli_parsed *parsed, struct cli_context *context);
int app_vomp_console(const struct cli_parsed *parsed, struct cli_context *context);
@ -397,12 +372,6 @@ int scrapeProcNetRoute();
int lsif();
int doifaddrs();
#define SERVER_UNKNOWN 1
#define SERVER_NOTRESPONDING 2
#define SERVER_NOTRUNNING 3
#define SERVER_RUNNING 4
int server_probe(int *pid);
int dna_helper_start();
int dna_helper_shutdown();
int dna_helper_enqueue(struct subscriber *source, mdp_port_t source_port, const char *did);
@ -436,10 +405,6 @@ int overlay_mdp_service_stun_req(struct internal_mdp_header *header, struct over
int overlay_mdp_service_stun(struct internal_mdp_header *header, struct overlay_buffer *payload);
int overlay_mdp_service_probe(struct internal_mdp_header *header, struct overlay_buffer *payload);
time_ms_t limit_next_allowed(struct limit_state *state);
int limit_is_allowed(struct limit_state *state);
int limit_init(struct limit_state *state, int rate_micro_seconds);
int olsr_init_socket(void);
int olsr_send(struct overlay_frame *frame);
@ -447,16 +412,9 @@ int pack_uint(unsigned char *buffer, uint64_t v);
int measure_packed_uint(uint64_t v);
int unpack_uint(unsigned char *buffer, int buff_size, uint64_t *v);
int slip_encode(int format,
const unsigned char *src, int src_bytes, unsigned char *dst, int dst_len);
int slip_decode(struct slip_decode_state *state);
int upper7_decode(struct slip_decode_state *state,unsigned char byte);
uint32_t Crc32_ComputeBuf( uint32_t inCrc32, const void *buf,
size_t bufLen );
void rhizome_fetch_log_short_status();
extern char crash_handler_clue[1024];
int link_received_duplicate(struct subscriber *subscriber, int previous_seq);
int link_received_packet(struct decode_context *context, int sender_seq, char unicast);
int link_receive(struct internal_mdp_header *header, struct overlay_buffer *payload);

479
slip.c
View File

@ -1,479 +0,0 @@
/*
Serval Distributed Numbering Architecture (DNA)
Copyright (C) 2012 Paul Gardner-Stephen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "serval.h"
#include "conf.h"
#include "log.h"
#include "dataformats.h"
#define DEBUG_packet_visualise(M,P,N) logServalPacket(LOG_LEVEL_DEBUG, __WHENCE__, (M), (P), (N))
/* SLIP-style escape characters used for serial packet radio interfaces */
#define SLIP_END 0xc0
#define SLIP_ESC 0xdb
#define SLIP_0a 0x0a
#define SLIP_0d 0x0d
#define SLIP_0f 0x0f
#define SLIP_1b 0x1b
#define SLIP_ESC_END 0xdc
#define SLIP_ESC_ESC 0xdd
#define SLIP_ESC_0a 0x7a
#define SLIP_ESC_0d 0x7d
#define SLIP_ESC_0f 0x7f
#define SLIP_ESC_1b 0x6b
/* interface decoder state bits */
#define DC_VALID 1
#define DC_ESC 2
static int encode_slip(const unsigned char *src, int src_bytes, unsigned char *dst, int dst_len)
{
int i, offset=0;
for (i=0;i<src_bytes;i++){
if (offset+3>dst_len)
return WHY("Dest buffer full");
switch(src[i]) {
case SLIP_END:
dst[offset++]=SLIP_ESC;
dst[offset++]=SLIP_ESC_END;
break;
case SLIP_ESC:
dst[offset++]=SLIP_ESC;
dst[offset++]=SLIP_ESC_ESC;
break;
case SLIP_0a:
dst[offset++]=SLIP_ESC;
dst[offset++]=SLIP_ESC_0a;
break;
case SLIP_0d:
dst[offset++]=SLIP_ESC;
dst[offset++]=SLIP_ESC_0d;
break;
case SLIP_0f:
dst[offset++]=SLIP_ESC;
dst[offset++]=SLIP_ESC_0f;
break;
case SLIP_1b:
dst[offset++]=SLIP_ESC;
dst[offset++]=SLIP_ESC_1b;
break;
default:
dst[offset++]=src[i];
}
}
return offset;
}
int slip_encode(int format,
const unsigned char *src, int src_bytes, unsigned char *dst, int dst_len)
{
switch(format) {
case SLIP_FORMAT_SLIP:
{
int offset=0;
if (offset+2>dst_len)
return WHY("Dest buffer full");
dst[offset++]=SLIP_END;
int ret=encode_slip(src, src_bytes, dst + offset, dst_len - offset);
if (ret<0)
return ret;
offset+=ret;
unsigned char crc[4];
write_uint32(crc, Crc32_ComputeBuf( 0, src, src_bytes));
ret=encode_slip(crc, 4, dst + offset, dst_len - offset);
if (ret<0)
return ret;
offset+=ret;
dst[offset++]=SLIP_END;
return offset;
}
case SLIP_FORMAT_UPPER7:
/*
The purpose of this encoder is to work nicely with the RFD900 radios,
including allowing the reception of RSSI information in the middle of
packets.
RSSI reports look like:
L/R RSSI: 48/0 L/R noise: 62/0 pkts: 0 txe=0 rxe=0 stx=0 srx=0 ecc=0/0 temp=21 dco=0
So we are using 0x80-0xff to hold data, and { and } to frame packets.
*/
if (config.debug.slip)
dump("pre-slipped packet",src,src_bytes);
{
if (src_bytes<1) return 0;
if (src_bytes>0x3fff)
return WHYF("UPPER7 SLIP encoder packets must be <=0x3fff bytes");
if (dst_len<(9+src_bytes+(src_bytes/7)+1))
return WHYF("UPPER7 SLIP encoder requires 9+(8/7)*bytes to encode");
int i,j;
int out_len=0;
// Start of packet marker
dst[out_len++]='{';
// Length of (unencoded) packet
dst[out_len++]=0x80+((src_bytes>>7)&0x7f);
dst[out_len++]=0x80+((src_bytes>>0)&0x7f);
// Add 32-bit CRC
// (putting the CRC at the front allows it to be calculated progressively
// on the receiver side, if we decide to support that)
uint32_t crc=Crc32_ComputeBuf( 0, src, src_bytes);
dst[out_len++]=0x80|((crc>>25)&0x7f);
dst[out_len++]=0x80|((crc>>(25-7))&0x7f);
dst[out_len++]=0x80|((crc>>(25-7-7))&0x7f);
dst[out_len++]=0x80|((crc>>(25-7-7-7))&0x7f);
dst[out_len++]=0x80|((crc>>0)&0x7f);
for(i=0;i<src_bytes;i+=7)
{
// Create 8 bytes of output consisting of 8x7 bits
// Generate vector of 7 bytes to encode
unsigned char v[7];
for(j=0;j<7&&i+j<src_bytes;j++) v[j]=src[i+j];
for(;j<7;j++) v[j]=0;
if (out_len+8>dst_len)
return WHYF("Ran out of space in UPPER7 SLIP encoder (used all %d bytes after encoding %d of %d bytes)",
dst_len,i,src_bytes);
// We could use a nice for loop to do this, but for 8 bytes, let's
// just do it explicitly.
dst[out_len++]=0x80| (v[0]>>1);
dst[out_len++]=0x80|((v[0]&0x01)<<6)|(v[1]>>2);
dst[out_len++]=0x80|((v[1]&0x03)<<5)|(v[2]>>3);
dst[out_len++]=0x80|((v[2]&0x07)<<4)|(v[3]>>4);
dst[out_len++]=0x80|((v[3]&0x0f)<<3)|(v[4]>>5);
dst[out_len++]=0x80|((v[4]&0x1f)<<2)|(v[5]>>6);
dst[out_len++]=0x80|((v[5]&0x3f)<<1)|(v[6]>>7);
dst[out_len++]=0x80|((v[6]&0x7f)<<0);
}
// Mark end of packet
dst[out_len++]='}';
// Detect fatal miscalculations on byte counts
if (out_len>dst_len) {
FATALF("overran output buffer in SLIP UPPER7 encapsulation of packet (used %d of %d bytes)",out_len,dst_len);
}
return out_len;
}
default:
return WHYF("Unsupported slip encoding #%d",format);
}
}
time_ms_t last_rssi_time=0;
int last_radio_rssi=-999;
int last_radio_temperature=-999;
int last_radio_rxpackets=0;
int parse_rfd900_rssi(char *s)
{
int lrssi,rrssi,lnoise,rnoise,rxpackets,temp;
// L/R RSSI: 48/0 L/R noise: 62/0 pkts: 0 txe=0 rxe=0 stx=0 srx=0 ecc=0/0 temp=21 dco=0
if (sscanf(s,"L/R RSSI: %d/%d L/R noise: %d/%d pkts: %d txe=%*d rxe=%*d stx=%*d srx=%*d ecc=%*d/%*d temp=%d dco=%*d",
&lrssi,&rrssi,&lnoise,&rnoise,&rxpackets, &temp)==6)
{
int lmargin=(lrssi-lnoise)/1.9;
int rmargin=(rrssi-rnoise)/1.9;
int maxmargin=lmargin; if (rmargin>maxmargin) maxmargin=rmargin;
last_radio_rssi=maxmargin;
last_radio_temperature=temp;
last_radio_rxpackets=rxpackets;
if (gettime_ms()-last_rssi_time>30000) {
INFOF("Link budget = %+ddB, temperature=%dC",maxmargin,temp);
last_rssi_time=gettime_ms();
}
}
return 0;
}
#define UPPER7_STATE_NOTINPACKET 0
#define UPPER7_STATE_L1 1
#define UPPER7_STATE_L2 2
#define UPPER7_STATE_C1 3
#define UPPER7_STATE_C2 4
#define UPPER7_STATE_C3 5
#define UPPER7_STATE_C4 6
#define UPPER7_STATE_C5 7
#define UPPER7_STATE_D0 8
#define UPPER7_STATE_D1 9
#define UPPER7_STATE_D2 10
#define UPPER7_STATE_D3 11
#define UPPER7_STATE_D4 12
#define UPPER7_STATE_D5 13
#define UPPER7_STATE_D6 14
#define UPPER7_STATE_D7 15
int u7d_calls=0;
int upper7_decode(struct slip_decode_state *state,unsigned char byte)
{
IN()
u7d_calls++;
if (config.debug.slipdecode)
snprintf(crash_handler_clue,1024,
"upper7_decode() call #%d: state=%d, byte=0x%02x, rssi_len=%u, dst_offset=%u",
u7d_calls,state->state,byte,state->rssi_len,state->dst_offset);
if (config.debug.slipbytestream)
WHYF("call #%d: state=%d, byte=0x%02x, rssi_len=%u, dst_offset=%u",
u7d_calls,state->state,byte,state->rssi_len,state->dst_offset);
// Parse out inline RSSI reports
if (byte=='{') {
state->state=UPPER7_STATE_L1;
state->packet_length=0;
RETURN(0);
} else if (byte=='}') {
// End of packet marker -- report end of received packet to caller
// for CRC verification etc.
state->state=UPPER7_STATE_NOTINPACKET; RETURN(1);
} else if (byte>=' '&&byte<=0x7f) {
if (state->rssi_len<RSSI_TEXT_SIZE)
state->rssi_text[state->rssi_len++]=byte;
RETURN(0);
} else if (byte=='\r'||byte=='\n') {
if (state->rssi_len>=RSSI_TEXT_SIZE) state->rssi_len=RSSI_TEXT_SIZE-1;
state->rssi_text[state->rssi_len]=0;
parse_rfd900_rssi(state->rssi_text);
state->rssi_len=0;
}
// Non-data bytes (none currently used, but we need to catch them before
// moving onto processing data bytes)
if (byte<0x80) {
RETURN(0);
}
// Data bytes and packet fields
byte&=0x7f;
if (state->packet_length>=OVERLAY_INTERFACE_RX_BUFFER_SIZE
||(state->dst_offset+7)>=OVERLAY_INTERFACE_RX_BUFFER_SIZE
) {
WARNF("state=%p, state->dst_offset=%u, ->packet_length=%u, ->state=%d. State reset.",
state,state->dst_offset,state->packet_length,state->state);
state->state=UPPER7_STATE_NOTINPACKET;
state->dst_offset=0;
state->packet_length=0;
RETURN(0);
}
switch(state->state) {
case UPPER7_STATE_NOTINPACKET: RETURN(0);
case UPPER7_STATE_L1: state->packet_length=byte<<7; state->state++; RETURN(0);
case UPPER7_STATE_L2: state->packet_length|=byte;
// Make sure packet length can fit in RX buffer, including that we might
// need upto 7 bytes extra temporary space due to blocking
if ((state->packet_length+7)<OVERLAY_INTERFACE_RX_BUFFER_SIZE) {
state->state++;
state->dst_offset=0;
} else {
if (config.debug.packetradio)
DEBUGF("Ignoring jumbo packet of %u bytes",state->packet_length);
state->state=UPPER7_STATE_NOTINPACKET;
}
RETURN(0);
case UPPER7_STATE_C1: state->crc=byte<<25; state->state++; RETURN(0);
case UPPER7_STATE_C2: state->crc|=byte<<(25-7); state->state++; RETURN(0);
case UPPER7_STATE_C3: state->crc|=byte<<(25-7-7); state->state++; RETURN(0);
case UPPER7_STATE_C4: state->crc|=byte<<(25-7-7-7); state->state++; RETURN(0);
case UPPER7_STATE_C5: state->crc|=byte<<0; state->state++; RETURN(0);
case UPPER7_STATE_D0:
if (state->packet_length>=OVERLAY_INTERFACE_RX_BUFFER_SIZE
||(state->dst_offset+7)>=OVERLAY_INTERFACE_RX_BUFFER_SIZE
) {
WARNF("state->dst_offset=%u, ->packet_length=%u, ->state=%d. State reset (again).",
state->dst_offset,state->packet_length,state->state);
state->state=UPPER7_STATE_NOTINPACKET;
state->dst_offset=0;
state->packet_length=0;
RETURN(0);
}
state->dst[state->dst_offset]=byte<<1;
state->state++;
RETURN(0);
case UPPER7_STATE_D1:
state->dst[state->dst_offset+0]|=(byte>>6)&0x01;
state->dst[state->dst_offset+1]=(byte<<2);
state->state++;
RETURN(0);
case UPPER7_STATE_D2:
state->dst[state->dst_offset+1]|=(byte>>5)&0x03;
state->dst[state->dst_offset+2]=(byte<<3);
state->state++;
RETURN(0);
case UPPER7_STATE_D3:
state->dst[state->dst_offset+2]|=(byte>>4)&0x07;
state->dst[state->dst_offset+3]=(byte<<4);
state->state++;
RETURN(0);
case UPPER7_STATE_D4:
state->dst[state->dst_offset+3]|=(byte>>3)&0x0f;
state->dst[state->dst_offset+4]=(byte<<5);
state->state++;
RETURN(0);
case UPPER7_STATE_D5:
state->dst[state->dst_offset+4]|=(byte>>2)&0x1f;
state->dst[state->dst_offset+5]=(byte<<6);
state->state++;
RETURN(0);
case UPPER7_STATE_D6:
state->dst[state->dst_offset+5]|=(byte>>1)&0x3f;
state->dst[state->dst_offset+6]=(byte<<7);
state->state++;
RETURN(0);
case UPPER7_STATE_D7:
state->dst[state->dst_offset+6]|=(byte>>0)&0x7f;
state->dst_offset+=7;
state->state=UPPER7_STATE_D0;
RETURN(0);
default:
state->state=UPPER7_STATE_NOTINPACKET;
RETURN(0);
}
OUT();
}
/* state->src and state->src_size contain the freshly read bytes
we must accumulate any partial state between calls.
*/
int slip_decode(struct slip_decode_state *state)
{
switch(state->encapsulator) {
case SLIP_FORMAT_SLIP:
{
/*
Examine received bytes for end of packet marker.
The challenge is that we need to make sure that the packet encapsulation
is self-synchronising in the event that a data error occurs (including
failure to receive an arbitrary number of bytes).
*/
while(state->src_offset < state->src_size){
// clear the valid bit flag if we hit the end of the destination buffer
if (state->dst_offset >= sizeof state->dst)
state->state&=~DC_VALID;
if (state->state&DC_ESC){
// clear escape bit
state->state&=~DC_ESC;
switch(state->src[state->src_offset]) {
case SLIP_ESC_END: // escaped END byte
state->dst[state->dst_offset++]=SLIP_END;
break;
case SLIP_ESC_ESC: // escaped escape character
state->dst[state->dst_offset++]=SLIP_ESC;
break;
case SLIP_ESC_0a:
state->dst[state->dst_offset++]=SLIP_0a;
break;
case SLIP_ESC_0d:
state->dst[state->dst_offset++]=SLIP_0d;
break;
case SLIP_ESC_0f:
state->dst[state->dst_offset++]=SLIP_0f;
break;
case SLIP_ESC_1b:
state->dst[state->dst_offset++]=SLIP_1b;
break;
default: /* Unknown escape character. This is an error. */
if (config.debug.packetradio)
WARNF("Packet radio stream contained illegal escaped byte 0x%02x -- resetting parser.",state->src[state->src_offset]);
state->dst_offset=0;
// skip everything until the next SLIP_END
state->state=0;
}
}else{
// non-escape character
switch(state->src[state->src_offset]) {
case SLIP_ESC:
// set escape bit
state->state|=DC_ESC;
break;
case SLIP_END:
if (state->dst_offset>4){
uint32_t src_crc = read_uint32(state->dst + state->dst_offset -4);
uint32_t crc=Crc32_ComputeBuf( 0, state->dst, state->dst_offset -4);
if (src_crc != crc){
DEBUGF("Dropping frame due to CRC failure (%08x vs %08x)", src_crc, crc);
dump("frame", state->dst, state->dst_offset);
state->dst_offset=0;
state->state=0;
break;
}
// return once we've successfully parsed a valid packet that isn't empty
state->packet_length=state->dst_offset -4;
return 1;
}
// set the valid flag to begin parsing the next packet
state->state=DC_VALID;
break;
default:
if (state->state&DC_VALID)
state->dst[state->dst_offset++]=state->src[state->src_offset];
}
}
state->src_offset++;
}
return 0;
}
case SLIP_FORMAT_UPPER7:
{
if (config.debug.slip) {
if (state->rssi_len>=RSSI_TEXT_SIZE) state->rssi_len=RSSI_TEXT_SIZE-1;
state->rssi_text[state->rssi_len]=0;
DEBUGF("RX state=%d, rssi_len=%u, rssi_text='%s',src=%p, src_size=%u",
state->state,state->rssi_len,state->rssi_text,
state->src,state->src_size);
}
while(state->src_offset<state->src_size) {
if (upper7_decode(state,state->src[state->src_offset++])==1) {
if (config.debug.slip) {
dump("de-slipped packet",state->dst,state->packet_length);
}
// Check that CRC matches
uint32_t crc=Crc32_ComputeBuf( 0, state->dst, state->packet_length);
if (crc!=state->crc) {
if (config.debug.packetradio||config.debug.rejecteddata)
DEBUGF("Rejected packet of %u bytes due to CRC mis-match (%08x vs %08x)",
state->packet_length,crc,state->crc);
if (config.debug.rejecteddata) {
dump("bad packet",state->dst,state->packet_length);
}
} else {
if (config.debug.packetradio)
DEBUGF("Accepted packet of %u bytes (CRC ok)",state->packet_length);
return 1;
}
}
}
}
return 0;
default:
return WHYF("Unknown SLIP encapsulation format #%d",state->encapsulator);
}
}

View File

@ -35,7 +35,6 @@ SQLITE3_SOURCES = \
SERVAL_DAEMON_SOURCES = \
cli.c \
commandline.c \
crc32.c \
crypto.c \
directory_client.c \
dna_helper.c \
@ -89,7 +88,6 @@ SERVAL_DAEMON_SOURCES = \
server.c \
sha2.c \
sighandlers.c \
slip.c \
vomp.c \
vomp_console.c \
fec-3.0.1/ccsds_tables.c \