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

al_receiving.c

Go to the documentation of this file.
00001 #include "standard.h"
00002 #include "al.h"
00003 #include "al_sending.h" 
00004 #include "al_receiving.h"
00005 #include "rcpc.h"
00006 #include "crc4.h"
00007 #include "arq.h"
00008 #include "uep_rcpcc.h"
00009 #include "LList.h"
00010 #include "between.h"
00011 
00012 #define DEF_MAX_ARQ 3
00013 
00014 /***************************************************
00015  * output buffer stuff                             *
00016  ***************************************************/
00017 
00018 extern int debug;
00019 
00020 
00021 al_receiving_entity *new_al_receiving_entity(channel_info *nfo)
00022      /*
00023       * Makes a new AL-Receiving Entity.
00024       */
00025 {
00026   al_receiving_entity *new_al;
00027   
00028   new_al = (al_receiving_entity *) malloc(sizeof(al_receiving_entity));
00029   /*  printf("new al_receiving entity has address %x\n",new_al); */
00030   if (new_al == NULL)
00031     error("new_al_receiving_entity", "can't malloc al_receiving_entity");
00032   else {
00033     new_al->outgoing_data = mkbuffer(MAX_AL_BUFFER_SIZE, sizeof(byte));
00034     new_al->incoming_data = mkbuffer(MAX_AL_BUFFER_SIZE, sizeof(byte));
00035 
00036     new_al->type = nfo->type;
00037     new_al->framed = nfo->framed;
00038     new_al->seq_num = nfo->seq_num;
00039 
00040     new_al->rcpc_code = nfo->rcpc_code;
00041     new_al->rcpc_rate = nfo->rcpc_rate;
00042 
00043     new_al->crc_bytes = nfo->crc_bytes;
00044 
00045     new_al->old_threshold = nfo->old_threshold;
00046 
00047     /* Initialize other parameters */
00048     new_al->max_arq = DEF_MAX_ARQ;
00049     new_al->last_recd = 31;
00050     new_al->last_written = 31;
00051     new_al->initialized = 0;
00052     new_al->bad_payloads = 0;
00053   }
00054   return new_al;
00055 }
00056 
00057 
00058 
00059 void send_to_al(al_receiving_entity *which, byte b)
00060      /* 
00061       * write the byte to the incoming data buffer.
00062       */
00063 {
00064   bwrite(which->incoming_data, &b);
00065 }
00066 
00067 
00068 void send_closing_flag(al_receiving_entity *which)
00069      /*
00070       * called by mux to signal the end of an AL-PDU.
00071       *
00072       * check the crc on the data in incoming buffer (incoming_data).  if it 
00073       * is good, transfers the data to the outgoing buffer (outgoing_data).  
00074       */
00075 {
00076   byte inbuf[MAX_AL_BUFFER_SIZE];
00077   byte inbyte;
00078   byte computedCRC,receivedCRC;
00079   unsigned short computedCRC16,receivedCRC16;
00080   int i;
00081   int len=blevel(which->incoming_data);
00082   
00083   if (len > 0) {
00084     if (which->type == AL1) {
00085     /* just transfer incoming buffer to outgoing buffer.. */
00086       for(i=0;i<len;i++) {
00087         bread(which->incoming_data,(void *) &inbyte);
00088         bwrite(which->outgoing_data,(void *) &inbyte);
00089       }
00090     }
00091     else if (which->type == AL2) {
00092     /* check one byte crc, if good transfer to outgoing.  if not, put back
00093        into incoming */
00094       computedCRC = 0;
00095       for(i=0;i<len-1;i++) {
00096         bread(which->incoming_data,(void *) inbuf+i);
00097         crc8(&computedCRC,inbuf[i]);
00098       }
00099       bread(which->incoming_data,(void *) inbuf+(len-1));
00100       receivedCRC = inbuf[len-1];
00101       if (computedCRC == receivedCRC) { /* put data without crc into outgoing buf */
00102         if (debug) printf("AL receiving good CRC\n");
00103         for(i=0;i<len-1;i++)
00104           bwrite(which->outgoing_data,(void *) inbuf + i);
00105       }
00106       else { /* put all data back into incoming_data, bad crc */
00107         if (debug) printf("AL receiving bad CRC\n");
00108         for(i=0;i<len;i++)
00109           bwrite(which->incoming_data,(void *) inbuf + i);
00110       }
00111     }
00112     else if (which->type == AL3) {
00113     /* check two byte crc, if good transfer to outgoing.  if not, put back into
00114        incoming */
00115       computedCRC16 = 0;
00116       for(i=0;i<len-2;i++) {
00117         bread(which->incoming_data,(void *) inbuf+i);
00118         crc16(&computedCRC16,inbuf[i]);
00119       }
00120       bread(which->incoming_data,(void *) inbuf+(len-2));
00121       bread(which->incoming_data,(void *) inbuf+(len-1));
00122       receivedCRC16 = inbuf[len-2];
00123       receivedCRC16 = (receivedCRC16 << 8) | (inbuf[len-1]);
00124       if (computedCRC16 == receivedCRC16) { /* put data without crc into outgoing buf */
00125         if (debug) printf("AL receiving good CRC\n");
00126         for(i=0;i<len-2;i++)
00127           bwrite(which->outgoing_data,(void *) inbuf + i);
00128       }
00129       else { /* bad crc, send it anyway. could modify to send with an error msg */
00130         if (debug) printf("AL receiving bad CRC\n");
00131         for(i=0;i<len-2;i++)
00132           bwrite(which->outgoing_data,(void *) inbuf + i);
00133       }
00134     }
00135   }
00136 }
00137 
00138 
00139 int al_indication(al_receiving_entity *which)
00140 /*
00141  * Called by AL user (receiver) to check if there is any data waiting for
00142  * it in the receive buffer.  Returns 1 if data is there, 0 if not
00143  */
00144 {
00145   if (blevel(which->outgoing_data))
00146     return 1;
00147   else
00148     return 0;
00149 }
00150 
00151 
00152 byte al_receive(al_receiving_entity *which)
00153 /* 
00154  * called by al_user, which must call al_indication before
00155  */
00156 {
00157   byte b;
00158 
00159   bread(which->outgoing_data, &b);
00160 
00161   return b;
00162 }
00163 
00164 

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