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
00016
00017
00018 extern int debug;
00019
00020
00021 al_receiving_entity *new_al_receiving_entity(channel_info *nfo)
00022
00023
00024
00025 {
00026 al_receiving_entity *new_al;
00027
00028 new_al = (al_receiving_entity *) malloc(sizeof(al_receiving_entity));
00029
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
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
00062
00063 {
00064 bwrite(which->incoming_data, &b);
00065 }
00066
00067
00068 void send_closing_flag(al_receiving_entity *which)
00069
00070
00071
00072
00073
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
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
00093
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) {
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 {
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
00114
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) {
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 {
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
00142
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
00155
00156 {
00157 byte b;
00158
00159 bread(which->outgoing_data, &b);
00160
00161 return b;
00162 }
00163
00164