Created
April 24, 2013 06:54
-
-
Save kg4sgp/5450149 to your computer and use it in GitHub Desktop.
ax.25 UI framer, non working crc. Ugly code...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Jimmy Carter - KG4SGP | |
// ax.25 UI framer | |
// compile with gcc aframe.c -o aframe | |
#include <stdio.h> | |
#include <string.h> | |
#define poly (0x1201) | |
unsigned char flag = 0x7e; //ax.25 start/stop flag | |
unsigned short crc = 0xffff; | |
unsigned char head[74]; //headder buffer | |
unsigned char scall[7]; //Source callsign | |
unsigned char dcall[7]; //Destination callsign | |
unsigned char sssid = 0xE0; // | |
unsigned char dssid = 0x60; // | |
unsigned char rxseq = 0; // | |
unsigned char txseq = 0; // | |
unsigned char poll = 0; //poll bit | |
unsigned char sfcf = 0; //Supervisory Frame Control Field | |
unsigned char ufcf = 0; //Unnumbered Frame Control Field | |
unsigned char control = 0; | |
unsigned char pid = 0; //protocol indentifier | |
int txbits = 0; //number of tx data bits | |
unsigned char txdata[307]; //txdata (info portion) | |
unsigned char packet[512]; //entire packet | |
int packetLen; | |
char type = 2; | |
int starting = 75; //number of starting flags | |
int ending = 20; //number of ending flags | |
int bytepos = 0; | |
int bitpos = 0; | |
int packbitpos = 0; | |
int bitstuff = 0; | |
int bitStuffCount = 0; | |
int oncrc = 0; | |
int writebit = 0; | |
int addToPacket(char* , int); | |
//int setTxCall(char* , int); | |
//int setRxCall(char* , int ); | |
void leftShift(char* , long unsigned int); | |
void docrc(unsigned short); | |
unsigned char revChar(char); | |
void charLeftShift(char *array, long unsigned int size){ | |
int i; | |
for(i = 0; i < size; i++){ | |
array[i] = array[i] << 1; | |
} | |
} | |
unsigned char revChar(char in){ | |
unsigned char temp = 0; | |
int i = 0; | |
for(i = 0; i < 8; i++){ | |
if ((in>>i & 0x01) == 0){ | |
temp &= (((0x01)<<(7-i))^(0xff)); | |
} else { | |
temp |= ((0x01)<<(7-i)); | |
} | |
} | |
return temp; | |
} | |
// this function is fed byte per byte of MSB first data | |
void updateCrc(unsigned short ch) | |
{ | |
} | |
int addToPacket(char* data, int size){ | |
int i , j = 0; | |
for(i = 0; i < size; i++){ | |
if(oncrc == 1){ | |
updateCrc(data[i]); | |
} | |
for(j = 0; j < 8; j++){ | |
int move = j; | |
// NRZI, if readbuff is 0 at i change state of write bit | |
if(((data[i]>>move) & 0x01) == 0){ | |
writebit = !writebit; | |
} | |
//printf(" %d", writebit); | |
if(writebit == 0){ | |
//printf("0"); | |
packet[bytepos] &= ((0x01)<<(7-packbitpos))^(0xff); | |
packbitpos++; | |
bitStuffCount = 0; | |
if(packbitpos > 7){ | |
packbitpos = 0; | |
bytepos++; | |
// printf("\n"); | |
} | |
} else { | |
if(bitstuff == 1 && bitStuffCount > 4){ | |
//printf("0"); | |
packet[bytepos] &= ((0x01)<<(7-packbitpos))^(0xff); | |
bitStuffCount++; | |
packbitpos++; | |
bitStuffCount = 0; | |
if(packbitpos > 7){ | |
packbitpos = 0; | |
bytepos++; | |
//printf("\n"); | |
} | |
} | |
//printf("1"); | |
packet[bytepos] |= (0x01)<<(7-packbitpos); | |
bitStuffCount++; | |
packbitpos++; | |
if(packbitpos > 7){ | |
bytepos++; | |
packbitpos = 0; | |
//printf("\n"); | |
} | |
} | |
} | |
} | |
} | |
int main(int argc[], char* argv[]){ | |
int i; | |
// open input file | |
FILE* fin; | |
if (strcmp(argv[1], "-") == 0){ | |
fin = stdin; | |
} else if( (fin = fopen(argv[1], "rb")) == NULL){ | |
printf("Error opening input file...\n"); | |
return; | |
} | |
// open output file | |
FILE* fout; | |
if (strcmp(argv[2], "-") == 0){ | |
fout = stdout; | |
} else if( (fout = fopen(argv[2], "wb")) == NULL){ | |
printf("Error opening input file...\n"); | |
return; | |
} | |
// flush buffers if piped | |
if (fin == stdin) fflush(stdin); | |
if (fout == stdin) fflush(stdout); | |
// set callsigns | |
char txc[6] = { 'A', 'P', 'R', 'S', ' ', ' '}; | |
char rxc[6] = { 'K', 'G', '4', 'S', 'G', 'P'}; | |
// shift callsign to conform to standard | |
charLeftShift(txc, sizeof(txc)); | |
charLeftShift(rxc, sizeof(rxc)); | |
// make lsb of ssid (last bit of address frame) 1 to conform to standard | |
dssid |= 0x01; | |
// turn bit stuffing off | |
bitstuff = 0; | |
bitStuffCount = 0; | |
// prepair crc | |
crc = 0xffff; | |
oncrc = 0; | |
//add starting flags | |
for(i = 0; i < starting; i++){ | |
addToPacket(&flag, sizeof(flag)); | |
} | |
// turn bit stuffing on | |
bitstuff = 1; | |
bitStuffCount = 0; | |
// turn on crc | |
oncrc = 1; | |
//add callsigns and ssids | |
addToPacket(&txc[0], sizeof(txc)); | |
addToPacket(&sssid, sizeof(dssid)); | |
addToPacket(&rxc[0], sizeof(rxc)); | |
addToPacket(&dssid, sizeof(sssid)); | |
// add un-numbered control field as per standard | |
control = 0x03; | |
addToPacket(&control, sizeof(control)); | |
// add protocol identifier, no layer 3 implemented | |
pid = 0xf0; | |
addToPacket(&pid, sizeof(pid)); | |
// file read buffer | |
// read one character at a time from a file and add it to the packet | |
char info = 0; | |
while(!feof(fin)){ | |
fread(&info, sizeof(char), 1, fin); | |
addToPacket(&info, (sizeof(info))); | |
} | |
//turn off crc | |
oncrc = 0; | |
// split crc into individual characters | |
// if we change the place of the high and low crc then flip them | |
// it flips the entire order of the 16bit sequence... its a hack and needs to | |
// be fixed at some point | |
// if this is fixed with our crc implementation it could be as simple as | |
// passing the crc high and low byte to addToPacket() | |
char highcrc = crc & 0x00ff; | |
char lowcrc = (crc & 0xff00)>>8; | |
highcrc = revChar(highcrc); | |
lowcrc = revChar(lowcrc); | |
addToPacket(&lowcrc, sizeof(lowcrc)); | |
addToPacket(&highcrc, sizeof(highcrc)); | |
// turn bit stuffing off | |
bitstuff = 0; | |
bitStuffCount = 0; | |
// add ending flags | |
for(i = 0; i < ending; i++){ | |
addToPacket(&flag, sizeof(flag)); | |
} | |
// write created packet to file (in binary form) | |
for(i = 0; i<bytepos; i++){ | |
fwrite(&packet[i], sizeof(char), 1, fout); | |
//printf("%c", packet[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment