static void * blk_thread(void *arg) { do { msgblk_t *blk; acarsmsg_t msg; int i,k,pn; unsigned short crc; pthread_mutex_lock(&blkmtx); while(blkq_e==NULL) pthread_cond_wait(&blkwcd,&blkmtx); blk=blkq_e; blkq_e=blk->prev; if(blkq_e==NULL) blkq_s=NULL; pthread_mutex_unlock(&blkmtx); if(blk==NULL) return; if(blk->len<13) { if(verbose) fprintf(stderr,"#%d too short\n",blk->chn+1); free(blk); continue; } /* force STX/ETX */ blk->txt[12]&=(ETX|STX); blk->txt[12]|=(ETX&STX); msg.err=0; /* parity check */ pn=0; for(i=0;i<blk->len;i++) { if((numbits[blk->txt[i]]&1)==0) { pn++; } } if(pn>1) { if(verbose) fprintf(stderr,"#%d too much parity error : %d\n",blk->chn+1,pn); free(blk); continue; } /* crc check */ crc=0; for(i=0;i<blk->len;i++) { update_crc(crc,blk->txt[i]); } update_crc(crc,blk->crc[0]); update_crc(crc,blk->crc[1]); /* try to fix errors */ if(crc!=0) { int i,k,fx; if(verbose) fprintf(stderr,"#%d CRC error, try to recover 1 error\n",blk->chn+1); fx=0; for(i=0;i<242*8;i++) if(oneerr[i]==crc) { fixerr(blk,i/8,1<<(i%8)); fx=1; msg.err=1; break; } if(fx==0 && pn==0 && blk->len<142) { int i,k,l; unsigned char u,v; unsigned short n=0; if(verbose) fprintf(stderr,"#%d CRC error, try to recover 2 close errors\n",blk->chn+1); for(k=1,v=2;k<8;k++,v<<=1) { for(l=0,u=1;l<k;l++,u<<=1) { if(twoerr[n]==crc) { fixerr(blk,0,u|v); fx=1; msg.err=2; break; } n++; for(i=1;i<142;i++) { if(twoerr[n]==crc) { fixerr(blk,i,u|v); fx=1; msg.err=2; break; } n++; } if(i<142) break; } if(l<k) break; } } if(fx==0) { if(verbose) fprintf(stderr,"#%d not able to fix it\n",blk->chn+1); free(blk); continue; } else { if(verbose) fprintf(stderr,"#%d fix it\n",blk->chn+1); } } /* redo parity checking and remove parity */ pn=0; for(i=0;i<blk->len;i++) { if((numbits[blk->txt[i]]&1)==0) { pn++; } blk->txt[i]&=0x7f; } if(pn) { if(verbose) fprintf(stderr,"#%d parity error %d\n",blk->chn+1,pn); free(blk); continue; } /* fill msg struct */ msg.txt[0]= '\0'; msg.fid[0] = '\0'; msg.lvl = blk->lvl; k = 0; msg.mode = blk->txt[k]; k++; for (i = 0; i < 7; i++, k++) { msg.addr[i] = blk->txt[k]; } msg.addr[7] = '\0'; /* ACK/NAK */ msg.ack = blk->txt[k]; k++; msg.label[0] = blk->txt[k]; k++; msg.label[1] = blk->txt[k]; if(msg.label[1]==0x7f) msg.label[1]='d'; k++; msg.label[2] = '\0'; msg.bid = blk->txt[k]; k++; /* STX/ETX */ msg.bs=blk->txt[k]; k++; msg.no[0] = '\0'; msg.fid[0] = '\0'; if(msg.mode <= 0x5d) { /* donwlink */ if(k<blk->len) { for (i = 0; i < 4 && k<blk->len-1; i++, k++) { msg.no[i] = blk->txt[k]; } msg.no[i] = '\0'; msg.be=blk->txt[blk->len-1]; for (i = 0; i < 6 && k<blk->len-1; i++, k++) { msg.fid[i] = blk->txt[k]; } msg.fid[i] = '\0'; } } else { /* uplink */ if(airflt) { free(blk); continue; } } blk->txt[blk->len-1]='\0'; strncpy(msg.txt, &(blk->txt[k]),240);msg.txt[239]=0; if(outtype==0) printoneline(&msg,blk->chn,blk->t); else if (outtype==1) printmsg(&msg,blk->chn,blk->t); else if (outtype==2) send_udp(&msg,blk->chn,blk->t); free(blk); } while(1); }
void outputmsg(const msgblk_t * blk) { acarsmsg_t msg; int i, k; /* fill msg struct */ msg.lvl = blk->lvl; msg.err = blk->err; k = 0; msg.mode = blk->txt[k]; k++; for (i = 0; i < 7; i++, k++) { msg.addr[i] = blk->txt[k]; } msg.addr[7] = '\0'; /* ACK/NAK */ msg.ack = blk->txt[k]; if (msg.ack == 0x15) msg.ack = '!'; k++; msg.label[0] = blk->txt[k]; k++; msg.label[1] = blk->txt[k]; if (msg.label[1] == 0x7f) msg.label[1] = 'd'; k++; msg.label[2] = '\0'; /* * downlink: 0 - 9 * uplink: A - Z a - z 0x0(NULL) */ msg.bid = blk->txt[k]; if (msg.bid == 0) msg.bid = ' '; k++; /* txt start */ msg.bs = blk->txt[k]; k++; msg.no[0] = '\0'; msg.fid[0] = '\0'; msg.txt[0] = '\0'; /* Don't display uplink message */ if (((msg.bid >= 'A' && msg.bid <= 'Z') || (msg.bid >= 'a' && msg.bid <= 'z') || msg.bid == 0x0) && airflt) return; /* STX = 0x03 show that there is no application text */ if (msg.bs != 0x03) { /* downlink message, has MSN and Flight id */ if (msg.bid >= '0' && msg.bid <= '9') { /* message no */ for (i = 0; i < 4 && k < blk->len - 1; i++, k++) { msg.no[i] = blk->txt[k]; } msg.no[i] = '\0'; /* Flight id */ for (i = 0; i < 6 && k < blk->len - 1; i++, k++) { msg.fid[i] = blk->txt[k]; } msg.fid[i] = '\0'; } /* Message txt */ for (i = 0; k < blk->len - 1; i++, k++) msg.txt[i] = blk->txt[k]; msg.txt[i] = 0; } /* txt end */ msg.be = blk->txt[blk->len - 1]; if (sockfd > 0) outnet(&msg); switch (outtype) { case 0: break; case 1: printoneline(&msg, blk->chn, blk->t); break; case 2: printmsg(&msg, blk->chn, blk->t); break; } }
void outputmsg(const msgblk_t * blk) { acarsmsg_t msg; int i, k; /* fill msg struct */ msg.lvl = blk->lvl; msg.err = blk->err; k = 0; msg.mode = blk->txt[k]; k++; for (i = 0; i < 7; i++, k++) { msg.addr[i] = blk->txt[k]; } msg.addr[7] = '\0'; /* ACK/NAK */ msg.ack = blk->txt[k]; k++; msg.label[0] = blk->txt[k]; k++; msg.label[1] = blk->txt[k]; k++; msg.label[2] = '\0'; msg.bid = blk->txt[k]; if (msg.bid == 0) msg.bid = ' '; k++; /* txt start */ msg.bs = blk->txt[k]; k++; msg.no[0] = '\0'; msg.fid[0] = '\0'; msg.txt[0] = '\0'; if ((msg.bs == 0x03 || msg.mode > 'Z' || msg.bid > '9') && airflt) return; if (msg.bs != 0x03) { if (msg.mode <= 'Z' && msg.bid <= '9') { /* message no */ for (i = 0; i < 4 && k < blk->len - 1; i++, k++) { msg.no[i] = blk->txt[k]; } msg.no[i] = '\0'; /* Flight id */ for (i = 0; i < 6 && k < blk->len - 1; i++, k++) { msg.fid[i] = blk->txt[k]; } msg.fid[i] = '\0'; } /* Message txt */ for (i = 0; k < blk->len - 1; i++, k++) msg.txt[i] = blk->txt[k]; msg.txt[i] = 0; } /* txt end */ msg.be = blk->txt[blk->len - 1]; switch (outtype) { case 1: twiddle(&msg); printoneline(&msg, blk->chn, blk->tm); break; case 2: /* printmsg wants it un-twiddled */ printmsg(&msg, blk->chn, blk->tm); twiddle(&msg); break; default: twiddle(&msg); break; } if (sockfd > 0) { if (netout == 0) outpp(&msg); else outsv(&msg, blk->chn, blk->tm); } }