Exemple #1
0
int print_fms(uint64_t fms, uint64_t bit_strength) {
    int a;
    int correct = 0;

    unsigned char crc = crc_check(fms);
    unsigned fzg1 = (fms >> 16) & 0xf;
    unsigned fzg2 = (fms >> 20) & 0xf;
    unsigned fzg3 = (fms >> 24) & 0xf;
    unsigned fzg4 = (fms >> 28) & 0xf;
    unsigned status = (fms >> 32) & 0xf;
    unsigned dir = (fms >> 37) & 1;
    //unsigned ort = (fms >> 8) & 0xff;
    //unsigned bos = (fms >> 4) & 0xf;
    //unsigned region = (fms & 0xf);

    //printf("header: %llx\n", (long long) header);

    if (crc != 0) {
        for ( a = 0; a < 48; a++) {
            if (!(bit_strength & 1ll << a)) {
                //printf("bit %d is weak\n", a);
                fms ^= 1ll << a;
                crc = crc_check(fms);
                if (crc == 0) {
                    //printf("corrected weak bit %d\n", a);
                    //XXX TODO check for 8f36 or kill
                    if ((fms & 0xfff) == FMS_REGION_BOS) correct = 2;
                    else correct = 0;
                    break;
                }
                fms ^= 1ll << a;
            }
        }
    } else {
       if ((fms & 0xfff) != FMS_REGION_BOS) correct = 0;
       else correct = 1;
    }

    if (correct > 0) {
        printf("FMS: %012llx ", (long long) fms);
        printf("FZG %x%x%x%x ", fzg1, fzg2, fzg3, fzg4);
        printf("Status: %x ", status);
        if (!dir) printf("FZG->LST ");
        else printf("LST->FZG ");
        if (correct == 1) printf("CRC correct\n");
        else if (correct == 2) printf("CRC corrected 1 weak bit\n");
        else printf("CRC INCORRECT\n");
    }

//    if (crc != 0) {
//        printf("%012llx\n", (long long) bit_strength);
//    }
    return correct;
}
__interrupt void USCI0RX_ISR(void)
{

	uint8_t rx_read;
	// Handle a UART Rx Interrupt
	if (IFG2 & UCA0RXIFG) {
		// Read in from UART peripheral and echo (for debug... will be removed)
		rx_read = UCA0RXBUF;

		switch ( uart_dev.state ) {
		case IDLE:
			if (rx_read == UART_MAGIC_FRAME_START) {
				uart_dev.state = MESSAGE;
				vector_uint8_clear(&uart_dev.rxbuf);
				crc_init(&(uart_dev.rxcrc));
			}
			break;
		case MESSAGE:
			if (rx_read == UART_MAGIC_ESCAPE) {
				uart_dev.state = ESCAPE;
				crc_add_byte( &(uart_dev.rxcrc), rx_read);
			} else if (rx_read == UART_MAGIC_FRAME_END) {
				if ( crc_check(uart_dev.rxcrc) || ignore_crc ) {
					// Remove CRC
				  uart_dev.rxbuf.end -= 2;
					// Find command
					switch (vector_uint8_get(&uart_dev.rxbuf, 0)) {
					case UART_COMMANDS_WR_REG: // Write Register command
						// rxbuf should be 4 long (command + addr + data)
						if (uart_dev.rxbuf.end == 4) {
						  uint8_t addr = vector_uint8_get(&uart_dev.rxbuf, 1);
							if (addr == 0x00) {
								settings_reg = (uint16_t)(((uint16_t)vector_uint8_get(&uart_dev.rxbuf, 2) << 8) | vector_uint8_get(&uart_dev.rxbuf, 3));
								uart_dev_send_ack(&uart_dev);
							} else if ((addr & 0xf0) == 0x10) {
							  // AD5504
                ad5504_value_reg[addr & 0x0f] = (uint16_t)(((uint16_t)vector_uint8_get(&uart_dev.rxbuf, 2) << 8) | vector_uint8_get(&uart_dev.rxbuf, 3));
                AD5504_send(&ad5504, (ad5504_addresses[addr&0x0f] & 0xff), ad5504_value_reg[addr&0x0f], AD5504_WRITE, ad5504_addresses[addr&0x0f]>>8);
                uart_dev_send_ack(&uart_dev);
							} else if ((addr & 0xf0) == 0x20) {
							  // DAC7512
                dac7512_value_reg[addr & 0x0f] = (uint16_t)(((uint16_t)vector_uint8_get(&uart_dev.rxbuf, 2) << 8) | vector_uint8_get(&uart_dev.rxbuf, 3));
                if ((dac7512_counter_reg[0] == 0) && ((addr & 0x0f) == 0)) {
                  DAC7512_send(&dac7512, dac7512_value_reg[addr & 0x0f], (addr>>2)&0x3, addr&0x3);
                }
                uart_dev_send_ack(&uart_dev);
              } else if ((addr & 0xf0) == 0x30) {
static void send_packet(int fd_out, int fd2, unsigned char buffer[], int buffer_length, unsigned char response[], int *response_length) {
    int i, bytes_read, bytes_written;
    unsigned char len, seq;
    const int *was_timeout;

    seq = unchar(buffer[2]);

    for(i = 0; i < MAX_RETRIES; i++) {
        bytes_written = write(fd_out, buffer, buffer_length);

        if(bytes_written != buffer_length) {
            fprintf(stderr, "sender: error writing packet to pipe (?!?)\n");
            continue;
        }

        was_timeout = set_alarm(TIMEOUT);

        bytes_read = read(fd2, response, 0x64);

        cancel_alarm();

        if(*was_timeout) {
            fprintf(stderr, "sender: we timed out while waiting for the ACK to the packet %d\n", seq);
            continue;
        }

        len = unchar(response[1]);

        if(!crc_check(tabel, response, len)) {
            fprintf(stderr, "sender: failed CRC check for the ACK/NAK to the packet %d\n", seq);
            continue;
        }

        if ( ( response[3] == 'Y' && buffer[2] == response[2] ) ||
             ( response[3] == 'N' && buffer[2] + 1 == response[2] ) ) {
            *response_length = bytes_read;
            return;
        }
    }

    fprintf(stderr, "sender: exceeded maximum number of retries... exiting...\n");

    close(fd_out);
    close(fd2);
    exit(0);
}
Exemple #4
0
void* myread(void* ptr)
{
	int sockfd=*((int*)ptr);
	int numbytes;
	char *buf=(char*)malloc(MAXDATASIZE);
	bzero(buf,MAXDATASIZE);
	int len;
	while(1){
		len=0;
		while((numbytes=MP1_read(sockfd,buf+len,MAXDATASIZE-len))!=-1){
			len+=numbytes;
			if(numbytes==0||len==MAXDATASIZE)
				break;
		}
		if(numbytes==0&&len<MAXDATASIZE){
			bzero(buf,MAXDATASIZE);
			pthread_mutex_lock(&m);
			dll_add_to_tail(&l,(void*)buf);
			pthread_mutex_unlock(&m);
			pthread_cond_broadcast(&c);
			break;
		}
		if(crc_check(buf)==0){
			pthread_mutex_lock(&m);
			dll_add_to_tail(&l,(void*)buf);
			pthread_mutex_unlock(&m);
			pthread_cond_broadcast(&c);

			buf=(char*)malloc(MAXDATASIZE);
		}
		else
			bzero(buf,MAXDATASIZE);
	}
	close(sockfd);
	return NULL;
}
Exemple #5
0
static unsigned int
readhead(T_HD *hp, unsigned char *ptr, unsigned int slen)
{
    struct headline	hd;
    unsigned int len
        = (*ptr == '!') ? decline_7((unsigned char *)&hd, ptr, slen) :
          decline_8((unsigned char *)&hd, ptr, slen);
    /* 上のちょっと危ないぞ */

    if(len > 60 && crc_check(0xffff, (unsigned char *)&hd, len) == 0x1d0f) {
        unsigned int	i;
        unsigned char	*s, *p;
        /*		memclr( sizeof(T_HD), hp );*/
        timecpy(&hp->fsize, (unsigned int *)hd.fs); /* 危ないかも WDN */
        if((int)(hp->vol = hd.volume)) {
            timecpy(&hp->volsize, (unsigned int *)hd.vbyte); /* 危かも WDN */
            hp->fsize= (hp->volsize*hd.volume > hp->fsize ?
                        hp->fsize-(hp->volsize*(hd.volume-1)) : hp->volsize);
        }
        hp->byte= hd.line[0]+(hd.line[1]<<8);
        hp->tcrc= (hd.crc16[0]<<8)+hd.crc16[1];
        hp->body= hp->byte-3;
        hp->blockbyte= hp->body*hp->body;
        hp->date= hp->perm= 0;
        if( hd.flag & 1 )
            timecpy(&hp->date, (unsigned int *)hd.date); /* 危かも WDN */
        hp->initflag= TRUE;
        hp->exec= ztype[hp->ztype= ishbit2z[hd.ishbit]].decexec;
        hp->block= hp->fsize/hp->blockbyte +1;
        hp->fsize+= 2;
        hp->dline= hp->fsize/hp->body+1;
        for( i= 0, s= hp->fname, p= hd.node ; *p != ' ' && i<8 ; i++ )
            *s++= *p++;
        if( *hd.ext != ' ' )
            for( *s++= '.', i= 0, p= hd.ext ; *p!=' '&&i<3 ; i++ )
                *s++= *p++;
        *s= '\0';

        if( !(hp->mode & mADJNAME) ) {
            unsigned int	ic;
            for( ic= 0, s= hp->fname, p= hp->adjname ;
                    *s && *s == *p && ic<8 ; s++, p++, ic++ );
            if( (!*s || *s == '.' || ic == 8) && *p )
                p_strcpy( hp->fname, hp->adjname );
        }

        /* debug */
        {
            if( !(hp->mode & mLIST) ) {
                Merr( (char *)hp->fname );
                Merrch( '(' );
                Numerr( hp->fsize-2 );
                if( hd.volume ) {
                    Merrch( '/' );
                    Numerr( hd.volume );
                }
                Merrch( ')' );
            } else {
                char	buf[28];
                Mprint( (char *)hp->fname );
                Mputchar( ' ' );
                Mputchar( '(' );
                Numput( hp->fsize-2 );
                if( hd.volume ) {
                    Mputchar( '/' );
                    Numput( hd.volume );
                }
                Mputchar( ')' );
                Mputchar( ' ' );
                DATESTR(
                    ((hp->date & 0xfe000000)>>9)+
                    ((hp->date & 0x01e00000)>>13)+
                    ((hp->date & 0x001f0000)>>16)+
                    0x27bc0000
                    , buf );
                Mprint( buf );
                Mputchar( ' ' );
                TIMESTR(
                    ((hp->date & 0x0000f800)<<5)+
                    ((hp->date & 0x000007e0)<<3)+
                    ((hp->date<<1) & 0x3e)
                    , buf );
                Mprint( buf );
                Mputchar( ' ' );
                Mprint( (char *)ztype[hp->ztype].name );
                Mprint( " os:" );
                Numput( hd.os );
#if 0
                if( hd.flag & 12 ) {
                    unsigned	i,j;
                    i= (hd.crc16[0]<<8)+hd.crc16[1];
                    timecpy( &j, hd.crc32 );
                    if( filecrc( hp->fname, i, j ) ) {
                        Mprint( "<crcOK!>" );
                    }
                }
#endif
                Mprint( " line:" );
            }
        }
        return	TRUE;
    }