Main Page | Class List | File List | Class Members | File Members

channel.h File Reference

#include "bytes.h"
#include "Buffer.h"

Go to the source code of this file.

Classes

struct  channel

Defines

#define MAX_FILENAME_LENGTH   256
#define DEF_PACKET_SIZE   136
#define DEF_CHANNEL_SPEED   30000
#define DEF_CHANNEL_DELAY   100
#define DEF_CHANNEL_ERROR   NoErrors
#define DEF_CHANNEL_BER   2.1e-2
#define DEF_CHANNEL_FILE   "./dect1.asc"

Enumerations

enum  errortype { NoErrors, Random, File }

Functions

void apply_errors (byte *data, channel *chan)
int channel_write_indication (channel *one)
int channel_write (channel *one, byte m)
int channel_read_indication (channel *one)
byte channel_read (channel *one)
channelnew_channel (char *name)
void close_channel (channel *c)


Define Documentation

#define DEF_CHANNEL_BER   2.1e-2
 

Definition at line 18 of file channel.h.

#define DEF_CHANNEL_DELAY   100
 

Definition at line 16 of file channel.h.

#define DEF_CHANNEL_ERROR   NoErrors
 

Definition at line 17 of file channel.h.

#define DEF_CHANNEL_FILE   "./dect1.asc"
 

Definition at line 19 of file channel.h.

Referenced by new_channel().

#define DEF_CHANNEL_SPEED   30000
 

Definition at line 15 of file channel.h.

#define DEF_PACKET_SIZE   136
 

Definition at line 14 of file channel.h.

#define MAX_FILENAME_LENGTH   256
 

Definition at line 13 of file channel.h.


Enumeration Type Documentation

enum errortype
 

Enumeration values:
NoErrors 
Random 
File 

Definition at line 10 of file channel.h.

00010 { NoErrors, Random, File } errortype;


Function Documentation

void apply_errors byte data,
channel chan
 

Definition at line 21 of file channel.c.

References channel::bit_count, channel::bit_error_rate, byte, data, debug, error(), channel::error, channel::error_count, channel::error_file, channel::error_offset, channel::file_open, channel::filename, channel::label, and warn().

Referenced by channel_read().

00022 {
00023   int i, j, count=0, num_bits;
00024   float r;
00025   float ber;
00026   byte b=*data;
00027 
00028   chan->bit_count += 8;
00029 
00030   if (chan->error==NoErrors)
00031     return;
00032 
00033   if (chan->error == Random) {
00034     if (chan->bit_error_rate == 0.0)
00035       return;
00036 
00037     if ((chan->bit_count/8) >= chan->error_offset) {
00038       for (j=0; j<8; j++) {
00039         r = (float) random() / MAX_RAND;
00040         if (r < chan->bit_error_rate) {
00041           b ^= (1 << j);
00042           count++;
00043           chan->error_count++;
00044           if (debug >= 2)
00045             printf("apply_errors() r=%e, bit %d flipped. \n", r, j);
00046         }
00047       }
00048     }
00049     else
00050       if (debug>=4) printf("apply_errors() - not applying errors yet\n");
00051   }
00052 
00053   else if (chan->error == File) { 
00054     if (chan->filename==NULL) {
00055       warn("apply_errors","channel's error file name is NULL");
00056       return;
00057     }
00058     if ((chan->bit_count/8) > chan->error_offset) {
00059       if (!chan->file_open) {
00060         chan->error_file = fopen(chan->filename,"r");
00061         if (chan->error_file == NULL) {
00062           error("apply_errors","Can't open error file");
00063         }
00064         chan->file_open = 1;
00065       }
00066 
00067       for (j=0; j<8; j++) {
00068         if (feof(chan->error_file))
00069           fseek(chan->error_file, 0, 0);
00070         if (fgetc(chan->error_file)=='1') {
00071           b ^= (1 << j);
00072           count++;
00073           chan->error_count++;
00074           if (debug >= 4)
00075             printf("apply_errors() bit %d flipped\n", j);
00076         }
00077       }
00078     }
00079     else
00080       if (debug>=4) printf("apply_errors() - not applying errors yet\n");
00081   } 
00082   if (debug >= 4) {
00083     printf("apply_errors() [%s] Applied %d errors to byte\n", 
00084            chan->label, count);
00085   }
00086   if (debug >= 1) {
00087     printf("apply_errors() [%s] Errors %d / %d bits (BER %e)\n",
00088            chan->label,
00089            chan->error_count, chan->bit_count, 
00090            (float) chan->error_count / chan->bit_count);
00091   }
00092 
00093 
00094   /* copy modified byte back to *data.  cheezy way of doing this. */
00095 
00096   *data = b;
00097 
00098   /*
00099    * Figure out the BER for this packet and log it.  
00100    */
00101 
00102 
00103   /* ber = (float) count / (float) num_bits;
00104   log_ber(ber, 0); */
00105 
00106   return;
00107 }

byte channel_read channel one  ) 
 

Definition at line 161 of file channel.c.

References apply_errors(), bread(), byte, channel::pdus, and channel::read_count.

Referenced by demultiplex().

00165 {
00166   static byte b;
00167 
00168   bread(one->pdus, &b);
00169 
00170   apply_errors(&b, one);
00171 
00172   one->read_count++;
00173 
00174   return(b);
00175 }

int channel_read_indication channel one  ) 
 

Definition at line 140 of file channel.c.

References blevel(), get_clock(), channel::pdus, channel::read_count, channel::transmission_delay, and channel::transmission_speed.

Referenced by demultiplex().

00150 {
00151 /*  long int pos = ((get_clock() * one->transmission_speed) / 8000) - one->transmission_delay; */
00152   long int allowed = ((get_clock() - one->transmission_delay) * one->transmission_speed) / 8000;
00153 
00154   if (blevel(one->pdus) > 0)
00155         return (one->read_count < allowed);
00156   else 
00157         return 0;
00158 }

int channel_write channel one,
byte  m
 

Definition at line 128 of file channel.c.

References bwrite(), channel::pdus, and channel::write_count.

Referenced by multiplex().

00134 {
00135   bwrite(one->pdus, &m);
00136   one->write_count++;
00137 }

int channel_write_indication channel one  ) 
 

Definition at line 110 of file channel.c.

References get_clock(), channel::transmission_speed, and channel::write_count.

Referenced by multiplex().

00120 {
00121   /* int level = (one->transmission_speed * one->transmission_delay) / 8000; */
00122   long int allowed = (one->transmission_speed * get_clock()) / 8000; 
00123   
00124   return (one->write_count < allowed);
00125 }

void close_channel channel c  ) 
 

Definition at line 205 of file channel.c.

References channel::bit_count, closebuffer(), channel::error_count, channel::error_file, channel::label, and channel::pdus.

Referenced by cleanup().

00206 {
00207   if (debug) {
00208     fprintf(stdout,"Channel %s:\n",c->label);
00209     fprintf(stdout,"Number of errors: %d\nNumber of bits:   %d\n",c->error_count,
00210             c->bit_count);
00211     fprintf(stdout,"BER:              %f\n", (float) c->error_count/c->bit_count);
00212   }
00213   if (c->error_file!=NULL)
00214     fclose(c->error_file);
00215   closebuffer(c->pdus);
00216   free(c);
00217 }

channel* new_channel char *  name  ) 
 

Definition at line 178 of file channel.c.

References channel::bit_count, channel::bit_error_rate, BUFSIZE, byte, DEF_CHANNEL_FILE, error(), channel::error, channel::error_count, channel::error_file, channel::error_offset, channel::file_open, channel::filename, channel::label, mkbuffer(), channel::pdus, channel::read_count, channel::transmission_delay, channel::transmission_speed, and channel::write_count.

Referenced by start_config().

00179 {
00180   char s[256];
00181   int x;
00182 
00183   channel *one = malloc(sizeof(channel));
00184   if (one != NULL) {
00185     strcpy(one->label, name);
00186     one->transmission_speed = DEF_CHANNEL_SPEED;
00187     one->transmission_delay = DEF_CHANNEL_DELAY;
00188     one->error = DEF_CHANNEL_ERROR;
00189     one->bit_error_rate = DEF_CHANNEL_BER;
00190     strcpy(one->filename, DEF_CHANNEL_FILE);
00191     one->pdus = mkbuffer(BUFSIZE, sizeof(byte));
00192     one->error_count = 0;
00193     one->bit_count = 0;
00194     one->write_count=0;
00195     one->read_count=0;
00196     one->error_offset=0;
00197     one->file_open=0;
00198     one->error_file=NULL;
00199   }
00200   else
00201     error("new_channel","Can't malloc new channel");
00202   return (one);
00203 }


Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1