int spi_writeBytes(uint8_t *data, unsigned int len) {
/*
    uint8_t v[16];

    data[len] = CRC8((unsigned char*)(data),len);
    len++;

    bcm2835_spi_transfernb((char*)data,(char*)v,len);

    for (unsigned int i=0;i<len;i++) {
	if (!t_start && v[i]!=0) t_start=true; //looking for the first non-zero byte which indicates start of a packet
	
	if (t_start) //we have found begining of a transfer
		if (_spi_addByte(v[i])) t_start = false;
	//we have consumed the packet hence we will be waiting for a new start byte 
    }
*/

    uint8_t v;
    
    data[len] = CRC8((unsigned char*)(data),len);
    len++;

    for (unsigned int i=0;i<len;i++) {
	v = bcm2835_spi_transfer(data[i]);

	if (!t_start && v!=0) t_start=true; //looking for the first non-zero byte which indicates start of a packet
	
	if (t_start) //we have found begining of a transfer
		if (_spi_addByte(v)) t_start = false;
	//we have consumed the packet hence we will be waiting for a new start byte 
	bcm2835_delay(1);
    }
    return ret;
}
int _spi_addByte(uint8_t b) {

    buf[p++] = b;

    if (p==4) {
        int16_t v = 0;
        v = buf[2] << 8 | buf [1]; 
        //uint8_t c = CRC8(buf,3);
        if (CRC8(buf,3) == buf[3]) {
	    if (spi_buf_c == SPI_BUF_SIZE) {
		printf("SPI input buffer full. Resetting.");
		spi_buf_c = 0;
	    }
	    spi_buf[spi_buf_c].t = buf[0];
	    spi_buf[spi_buf_c].v = v;
	    spi_buf_c++;
	    p = 0;
	    if (verbose>1) printf("Received from AVR msg %i %i\n",buf[0],v);
        } else {
		spi_crc_err++;
		if (verbose) printf("Received CRC failed msg %i %i %i %i\n",buf[0],buf[1],buf[2],buf[3]);
		buf[0]=buf[1];
		buf[1]=buf[2];
		buf[2]=buf[3];
		p--;
	}
	return 1;
    }
    return 0;
}
u8 BBFrame::CalCRC8(u8 *IN, u32 len)
{

	u32 idx = 0 ;
	u8 crc = 0;

	for(idx = 0; idx < len; idx++)
	{
		crc = CRC8(IN[idx], crc);
	}
	return crc;

}
Esempio n. 4
0
// Work around for omap hdq driver bug which reverses the slave address
// We check the CRC8 and use reversed if forward is incorrect.
static void search_callback( struct netlink_parse * nlp, void * v, const struct parsedname * pn )
{
	int i ;
	struct connection_in * in = pn->selected_connection ;
	struct device_search *ds = v ;
	BYTE sn[SERIAL_NUMBER_SIZE] ;
	BYTE * sn_pointer ;

	for ( i = 0 ; i < nlp->w1c->len ; i += SERIAL_NUMBER_SIZE ) {
		switch( in->master.w1.w1_slave_order ) {
			case w1_slave_order_forward:
				sn_pointer = &nlp->data[i] ;
				break ;
			case w1_slave_order_reversed:
				// reverse bytes
				sn[0] = nlp->data[i+7] ;
				sn[1] = nlp->data[i+6] ;
				sn[2] = nlp->data[i+5] ;
				sn[3] = nlp->data[i+4] ;
				sn[4] = nlp->data[i+3] ;
				sn[5] = nlp->data[i+2] ;
				sn[6] = nlp->data[i+1] ;
				sn[7] = nlp->data[i+0] ;
				sn_pointer = sn ;
				break ;
			case w1_slave_order_unknown:
			default:
				sn_pointer = &nlp->data[i] ;
				if ( CRC8(sn_pointer, SERIAL_NUMBER_SIZE) == 0 ) {
					in->master.w1.w1_slave_order = w1_slave_order_forward ;
					break ;
				}
				// reverse bytes
				sn[0] = nlp->data[i+7] ;
				sn[1] = nlp->data[i+6] ;
				sn[2] = nlp->data[i+5] ;
				sn[3] = nlp->data[i+4] ;
				sn[4] = nlp->data[i+3] ;
				sn[5] = nlp->data[i+2] ;
				sn[6] = nlp->data[i+1] ;
				sn[7] = nlp->data[i+0] ;
				sn_pointer = sn ;
				in->master.w1.w1_slave_order = w1_slave_order_reversed ;
				LEVEL_DEBUG( "w1 bus master%d uses reversed slave order", in->master.w1.id ) ;
				break ;
		}
		DirblobAdd(sn_pointer, &(ds->gulp) );
	}
}
uint8_t writeClientConfiguration(client_configuration_t *config)
{
	uint8_t flag = false;
	uint16_t address = CONFIG_START_ADDRESS;

	config->crc = CRC8((const uint8_t *)config, 2);
	EEPROM.write(address++, config->version);
	EEPROM.write(address++, config->nodeId);
	EEPROM.write(address, config->crc);

	flag = ( EEPROM.read(address) == config->crc );

	return flag;

}
Esempio n. 6
0
static GOOD_OR_BAD OW_w_config(BYTE config0, BYTE config1, struct parsedname *pn)
{
	BYTE p[4] ;
	struct transaction_log t[] = {
		TRXN_START,
		TRXN_WRITE(p,4),
		TRXN_END,
	};

	p[0] = _1W_WRITE_CONFIG ;
	p[1] = config0 ;
	p[2] = config1 ;
	p[3] = CRC8( &p[1], 2 ) ;

	return BUS_transaction(t, pn) ;
}
uint8_t readClientConfiguration(client_configuration_t *config)
{
	uint8_t flag = false;
	uint8_t crc = 0;
	int address = CONFIG_START_ADDRESS;

	config->version = EEPROM.read(address++);
	config->nodeId = EEPROM.read(address++);;
	config->crc = EEPROM.read(address++);;

	crc = CRC8((const uint8_t *)config, 2);

	flag = (crc == config->crc);

	return flag;

}
Esempio n. 8
0
    void CTCPProfile::CreateIR(const ROHC::iphdr *ip, const ROHC::tcphdr *tcp, data_t &output) {
        size_t headerStartIdx = output.size();
        
        if (!largeCID && cid)
        {
            output.push_back(CreateShortCID(cid));
        }
        
        output.push_back(IRv2Packet);
        
        if (largeCID)
        {
            SDVLEncode(back_inserter(output), cid);
        }
        
        output.push_back(static_cast<uint8_t>(ProfileID()));
        
        size_t crcPos = output.size();
        
        // Add zero crc for now
        output.push_back(0);
        
        create_ipv4_static(ip, output);
        
        create_tcp_static(tcp, output);
        
        create_ipv4_regular_innermost_dynamic(ip, output);
        
        //create_tcp_dynamic(msn, reorder_ratio, udp, output);
        
        // Calculate CRC
        uint8_t crc = CRC8(output.begin() + headerStartIdx, output.end());
        output[crcPos] = crc;
        
		IncreasePacketCount(PT_IR);
        
        ++numberOfIRPacketsSent;
        ++numberOfIRPacketsSinceReset;
    }
Esempio n. 9
0
int main(int argc, char *argv[]) {

	uint8_t data[MAXSIZE] = {0};
	FILE * pFile;

    if (argc < 3) {
        printf("\nCRC8 calculator for configuration file\nUsage: %s filename length\n", argv[0]);
        exit(1);
    }
    uint16_t bytes = atoi(argv[2]);
    printf("Filename: %s, array length: %d\n", argv[1], bytes);

    if (bytes > MAXSIZE-1) {
    	printf("Max array length error: %d\n", MAXSIZE);
    	return 1;
    }

    pFile = fopen (argv[1] , "r");
    if (pFile == NULL) {
    	perror ("Error opening file");
    	return 1;
    }

    if (fread (data, 1, bytes, pFile) != bytes) {
    	printf("File read error\n");
       	fclose (pFile);
    	return 1;
    }
   	fclose (pFile);

    uint8_t crc = CRC8(data, bytes);
    printf("Array size %d, CRC8: 0x%02X\n", bytes, crc);

    return 0;

}
Esempio n. 10
0
/* search = normal and alarm */
static enum search_status K1WM_next_both(struct device_search *ds, const struct parsedname *pn)
{
	LEVEL_DEBUG("[%s] BUS search", __FUNCTION__);
	int mismatched;
	BYTE sn[SERIAL_NUMBER_SIZE];
	BYTE bitpairs[SERIAL_NUMBER_SIZE*2];
	BYTE dummy ;
	struct connection_in * in = pn->selected_connection ;
	int i;

	LEVEL_DEBUG("[%s] ds->LastDevice == true ?", __FUNCTION__);
	if (ds->LastDevice) {
		LEVEL_DEBUG("[%s] ds->LastDevice == true -> search_done", __FUNCTION__);
		return search_done;
	}

	LEVEL_DEBUG("[%s] BUS_select failed ?", __FUNCTION__);
	if ( BAD( BUS_select(pn) ) ) {
		LEVEL_DEBUG("[%s] BUS_select failed -> search_error", __FUNCTION__);
		return search_error;
	}

	// Standard SEARCH ROM using SRA
	// need the reset done in BUS-select to set AnyDevices
	if ( in->AnyDevices == anydevices_no ) {
		LEVEL_DEBUG("[%s] in->AnyDevices == anydevices_no -> search_done", __FUNCTION__);
		ds->LastDevice = 1;
		return search_done;
	}

	// clear sn to satisfy static error checking (Coverity)
	memset( sn, 0, SERIAL_NUMBER_SIZE ) ;

	// build the command stream
	// call a function that may add the change mode command to the buff
	// check if correct mode
	// issue the search command
	// change back to command mode
	// search mode on
	// change back to data mode

	// set the temp Last Descrep to none
	mismatched = -1;

	// add the 16 bytes of the search
	memset(bitpairs, 0, SERIAL_NUMBER_SIZE*2);

	LEVEL_DEBUG("[%s] ds->LastDiscrepancy == %i", __FUNCTION__, ds->LastDiscrepancy);
	// set the bits in the added buffer
	for (i = 0; i < ds->LastDiscrepancy; i++) {
		// before last discrepancy
		UT_set2bit(bitpairs, i, UT_getbit(ds->sn, i) << 1);
	}
	// at last discrepancy
	if (ds->LastDiscrepancy > -1) {
		UT_set2bit(bitpairs, ds->LastDiscrepancy, 1 << 1);
	}
	// after last discrepancy so leave zeros

	// search ON
	// Send search rom or conditional search byte
	if ( BAD( K1WM_sendback_byte(&(ds->search), &dummy, in) ) ) {
		LEVEL_DEBUG("[%s] Sending SearchROM/SearchByte '0x%x' failed -> search_error", __FUNCTION__, ds->search);
		return search_error;
	}

	// Set search accelerator
	UT_setbit( &DS1WM_command(in), e_ds1wm_sra, 1 ) ;

	// send the packet
	// cannot use single-bit mode with search accerator
	// search OFF
	if ( BAD( K1WM_sendback_data(bitpairs, bitpairs, SERIAL_NUMBER_SIZE*2, pn) ) ) {
		LEVEL_DEBUG("[%s] Sending the packet (bitpairs) failed -> search_error", __FUNCTION__);
		return search_error;
	}

	// Turn off search accelerator
	UT_setbit( &DS1WM_command(in), e_ds1wm_sra, 0 ) ;

	// interpret the bit stream
	for (i = 0; i < SERIAL_NUMBER_BITS; i++) {
		// get the SerialNum bit
		UT_setbit(sn, i, UT_get2bit(bitpairs, i) >> 1);
		// check LastDiscrepancy
		if (UT_get2bit(bitpairs, i) == SEARCH_BIT_ON ) {
			mismatched = i;
		}
	}

	if ( sn[0]==0xFF && sn[1]==0xFF && sn[2]==0xFF && sn[3]==0xFF && sn[4]==0xFF && sn[5]==0xFF && sn[6]==0xFF && sn[7]==0xFF ) {
		// special case for no alarm present
		LEVEL_DEBUG("[%s] sn == 0xFFFFFFFFFFFFFFFF -> search_error", __FUNCTION__);
		return search_done ;
	}

	// CRC check
	if (CRC8(sn, SERIAL_NUMBER_SIZE) || (ds->LastDiscrepancy == SERIAL_NUMBER_BITS-1) || (sn[0] == 0)) {
		LEVEL_DEBUG("[%s] CRC check failed -> search_error", __FUNCTION__);
		return search_error;
	}

	// successful search
	// check for last one
	if ((mismatched == ds->LastDiscrepancy) || (mismatched == -1)) {
		ds->LastDevice = 1;
	}

	// copy the SerialNum to the buffer
	memcpy(ds->sn, sn, 8);

	// set the count
	ds->LastDiscrepancy = mismatched;

	LEVEL_DEBUG("SN found: " SNformat, SNvar(ds->sn));
	return search_good;
}
Esempio n. 11
0
// Read up to 7 (DS2490_DIR_GULP_ELEMENTS) at a time, and  place into
// a dirblob. Called from DS9490_next_both every 7 devices to fill.
static enum search_status DS9490_directory(struct device_search *ds, const struct parsedname *pn)
{
	BYTE status_buffer[ DS9490_getstatus_BUFFER_LENGTH + 1 ];
	BYTE EP2_data[SERIAL_NUMBER_SIZE] ; //USB endpoint 3 buffer
	union {
		BYTE b[DS2490_BULK_BUFFER_SIZE] ;
		BYTE sn[DS2490_BULK_BUFFER_SIZE/SERIAL_NUMBER_SIZE][SERIAL_NUMBER_SIZE];
	} EP3 ; //USB endpoint 3 buffer
	SIZE_OR_ERROR ret;
	int bytes_back;
	int devices_found;
	int device_index;
	int dir_gulp_elements = (pn->ds2409_depth==0) ? DS2490_DIR_GULP_ELEMENTS : 1 ;
	int readlen = 0 ;
	
	DirblobClear(&(ds->gulp));

	if ( BAD( BUS_select(pn) ) ) {
		LEVEL_DEBUG("Selection problem before a directory listing") ;
		return search_error ;
	}

	if ( pn->selected_connection->AnyDevices == anydevices_no ) {
		// empty bus detected, no presence pulse detected
		return search_done ;
	}
	
	SetupDiscrepancy(ds, EP2_data);

	// set the search start location
	ret = DS9490_write(EP2_data, SERIAL_NUMBER_SIZE, pn) ;
	if ( ret < SERIAL_NUMBER_SIZE ) {
		LEVEL_DATA("bulk write problem = %d", ret);
		return search_error;
	}
	// Send the search request
	if ( BAD( USB_Control_Msg(COMM_CMD, COMM_SEARCH_ACCESS | COMM_IM | COMM_SM | COMM_F | COMM_RTS, (dir_gulp_elements << 8) | (ds->search), pn) ) ) {
		LEVEL_DATA("control error");
		return search_error;
	}
	// read the search status
	if ( DS9490_getstatus(status_buffer, &readlen, pn)  != BUS_RESET_OK ) {
		return search_error;
	}

	// test the buffer size waiting for us
	bytes_back = status_buffer[13];
	LEVEL_DEBUG("Got %d bytes from USB search", bytes_back);
	if (bytes_back == 0) {
		/* Nothing found on the bus. Have to return something != search_good to avoid
		 * getting stuck in loop in FS_realdir() and FS_alarmdir()
		 * which ends when ret!=search_good */
		LEVEL_DATA("ReadBufferstatus == 0");
		return search_done;
	} else if ( bytes_back % SERIAL_NUMBER_SIZE != 0 ) {
		LEVEL_DATA("ReadBufferstatus size %d not a multiple of %d", bytes_back,SERIAL_NUMBER_SIZE);
		return search_error;
	} else if ( bytes_back > (dir_gulp_elements + 1) * SERIAL_NUMBER_SIZE ) {
		LEVEL_DATA("ReadBufferstatus size %d too large", bytes_back);
		return search_error;
	}
	devices_found = bytes_back / SERIAL_NUMBER_SIZE;
	if (devices_found > dir_gulp_elements) {
		devices_found = dir_gulp_elements;
	}

	// read in the buffer that holds the devices found
	if ((ret = DS9490_read(EP3.b, bytes_back, pn)) <= 0) {
		LEVEL_DATA("bulk read problem ret=%d", ret);
		return search_error;
	}

	// analyze each device found
	for (device_index = 0; device_index < devices_found; ++device_index) {
		/* test for CRC error */
		LEVEL_DEBUG("gulp. Adding element %d:" SNformat, device_index, SNvar(EP3.sn[device_index]));
		if (CRC8(EP3.sn[device_index], SERIAL_NUMBER_SIZE) != 0 || EP3.sn[device_index][0] == 0) {
			LEVEL_DATA("CRC error");
			return search_error;
		}
	}
	// all ok, so add the devices
	for (device_index = 0; device_index < devices_found; ++device_index) {
		DirblobAdd(EP3.sn[device_index], &(ds->gulp));
	}
	ds->LastDiscrepancy = FindDiscrepancy(EP3.sn[devices_found-1], EP3.sn[devices_found]);
	ds->LastDevice = (bytes_back == devices_found * SERIAL_NUMBER_SIZE);	// no more to read

	return search_good;
}
Esempio n. 12
0
static void enumerator_handler(RETRANSLATOR *pRetranslator, void *ctx)
{
	if (pRetranslator->sock == -1)
		return;

	ENUMCONTEXT *pContext = (ENUMCONTEXT *)ctx;

	time_t now = time(NULL);

	if (FD_ISSET(pRetranslator->sock, &pContext->fdReadSet)) {

		api_log_printf("[EGTS-TATARSTAN] socket #%d is readble\r\n", pRetranslator->sock);
	
		for (;;) {
			
			int status = recv(pRetranslator->sock, (char *)&AckPacket, sizeof(AckPacket), 0);

			if (status <= 0) {

				api_log_printf("[EGTS-TATARSTAN] socket #%d is closed\r\n", pRetranslator->sock);
				closesocket(pRetranslator->sock);
				pRetranslator->sock = -1;
				pRetranslator->status = RETRANSLATOR_STATUS_INIT;

				return;
			}

			if (status > 0) {

				api_log_printf("[EGTS-TATARSTAN] Received #%d bytes from socket #%d\r\n", status, pRetranslator->sock);

				if (AckPacket.hcs != CRC8((unsigned char *)&AckPacket, 10)) {						
					api_log_printf("[EGTS-TATARSTAN] CRC8 invalid\r\n");
					break;
				}

				if (*((unsigned short *)((unsigned char *)&AckPacket.record_id + AckPacket.dataLength)) != Crc16((unsigned char *)&AckPacket.record_id, AckPacket.dataLength)) {
					api_log_printf("[EGTS-TATARSTAN] CRC16 invalid\r\n");
					break;
				}

				if (AckPacket.record_id != pRetranslator->packet_id) {
					api_log_printf("[EGTS-TATARSTAN] Wrong record_id\r\n");
					break;
				}

				if (AckPacket.result != 0) {
					api_log_printf("[EGTS-TATARSTAN] Wrong result code\r\n");
					break;
				}

				spinlock_lock(&pRetranslator->spinlock);
				pRetranslator->records_queue.pop();
				spinlock_unlock(&pRetranslator->spinlock);

				pRetranslator->status = RETRANSLATOR_STATUS_CONNECTED;
				pRetranslator->timeout = 0;
				pRetranslator->packet_id++;
			}

			break;
		}
	}

	if (FD_ISSET(pRetranslator->sock, &pContext->fdWriteSet)) {

		api_log_printf("[EGTS-TATARSTAN] socket #%d is writible\r\n", pRetranslator->sock);
		
		switch (pRetranslator->status) {

		case RETRANSLATOR_STATUS_INIT:

			api_log_printf("[EGTS-TATARSTAN] ERROR, socket #%d is in intial state\r\n", pRetranslator->sock);
			break;

		case RETRANSLATOR_STATUS_CONNECTING:

			api_log_printf("[EGTS-TATARSTAN] socket #%d connected\r\n", pRetranslator->sock);
			pRetranslator->packet_id = 0;
			pRetranslator->status = RETRANSLATOR_STATUS_CONNECTED;

		case RETRANSLATOR_STATUS_CONNECTED:

			spinlock_lock(&pRetranslator->spinlock);
			
			if (!pRetranslator->records_queue.empty()) {

				RETRANSLATOR_RECORD rr = pRetranslator->records_queue.front();

				spinlock_unlock(&pRetranslator->spinlock);

				TatarPacket.version			= 0x01;
				TatarPacket.securityKeyId	= 0x01;
				TatarPacket.packet_flags	= 0x01;
				TatarPacket.packet_length	= 0x0b;
				TatarPacket.encoding		= 0x00;
				TatarPacket.dataLength		= 35;
				TatarPacket.packet_id		= pRetranslator->packet_id;
				TatarPacket.type			= 1; // EGTS_PT_APPDATA

				TatarPacket.record_length	= 24;
				TatarPacket.record_num		= pRetranslator->packet_id;
				TatarPacket.record_flags	= 0x09;  // OID – (Object Identifier) | PRIORITY 01
				TatarPacket.record_oid		= pRetranslator->oid; // Last 8 digits of imei
				TatarPacket.record_sst		= 0x02; // EGTS_TELEDATA_SERVICE
				TatarPacket.record_rst		= 0x02; // EGTS_TELEDATA_SERVICE

				TatarPacket.subrecord_type	= 0x10; // EGTS_SR_POS_DATA
				TatarPacket.subrecord_length	= 0x15;

				TatarPacket.sr_pos_data_timestamp	= rr.t - 1262304000;

				float lat = (float)rr.latitude / 10000000;
				float lon = (float)rr.longitude / 10000000;

				TatarPacket.sr_pos_data_latitude	= (unsigned int)((fabs(lat) / 90) * 0xFFFFFFFF);
				TatarPacket.sr_pos_data_longitude	= (unsigned int)((fabs(lon) / 180) * 0xFFFFFFFF);

				TatarPacket.sr_pos_data_flags		= 0;

				if ((rr.latitude != 0)&&(rr.longitude != 0))
					TatarPacket.sr_pos_data_flags	|= 0x01;

				if (rr.flags1 & RECORD_FLAG1_MOVE)
					TatarPacket.sr_pos_data_flags	|= 0x10;

				if (lon < 0)
					TatarPacket.sr_pos_data_flags	|= 0x40;
				if (lat < 0)
					TatarPacket.sr_pos_data_flags	|= 0x20;

				TatarPacket.sr_pos_data_speed		= rr.speed & 0x3FFF;

				TatarPacket.sr_pos_data_direction	= (rr.cog & 0xFF);
				if (rr.cog > 255)
					TatarPacket.sr_pos_data_speed	|= 0x8000;
				
				TatarPacket.sr_pos_data_odometer[0]	= 0xcf;
				TatarPacket.sr_pos_data_odometer[1]	= 0xf6;
				TatarPacket.sr_pos_data_odometer[2]	= 0x27;
				TatarPacket.sr_pos_data_digitalInputs	= rr.flags2 & 0xF0;
				TatarPacket.sr_pos_data_source			= (rr.flags1 & RECORD_FLAG1_IGNITION) ? 0 : 5;

				TatarPacket.hcs = CRC8((unsigned char *)&TatarPacket, 10);
				TatarPacket.data_crc = Crc16((unsigned char *)&TatarPacket.record_length, TatarPacket.dataLength);

				send(pRetranslator->sock, (char *)&TatarPacket, sizeof(TatarPacket), 0);

				api_log_printf("[EGTS-TATARSTAN] socket #%d send record\r\n", pRetranslator->sock);

				pRetranslator->status = RETRANSLATOR_STATUS_WAITACK;
				pRetranslator->timeout = now + 30;

			}
			else {
				spinlock_unlock(&pRetranslator->spinlock);
			}

			break;

		case RETRANSLATOR_STATUS_WAITACK:

			api_log_printf("[EGTS-TATARSTAN] ERROR, socket #%d is in waitack state\r\n", pRetranslator->sock);
			break;

		}
	}
}
Esempio n. 13
0
/* uses the "Triple" primative for faster search */
static enum search_status DS2482_next_both(struct device_search *ds, const struct parsedname *pn)
{
    int search_direction = 0;	/* initialization just to forestall incorrect compiler warning */
    int bit_number;
    int last_zero = -1;
    FILE_DESCRIPTOR_OR_ERROR file_descriptor = pn->selected_connection->pown->file_descriptor;
    BYTE bits[3];

    // initialize for search
    // if the last call was not the last one
    if (ds->LastDevice) {
        return search_done;
    }

    if ( BAD( BUS_select(pn) ) ) {
        return search_error ;
    }

    // need the reset done in BUS-select to set AnyDevices
    if ( pn->selected_connection->AnyDevices == anydevices_no ) {
        ds->LastDevice = 1;
        return search_done;
    }

    /* Make sure we're using the correct channel */
    /* Appropriate search command */
    if ( BAD(BUS_send_data(&(ds->search), 1, pn)) ) {
        return search_error;
    }
    // loop to do the search
    for (bit_number = 0; bit_number < 64; ++bit_number) {
        LEVEL_DEBUG("bit number %d", bit_number);
        /* Set the direction bit */
        if (bit_number < ds->LastDiscrepancy) {
            search_direction = UT_getbit(ds->sn, bit_number);
        } else {
            search_direction = (bit_number == ds->LastDiscrepancy) ? 1 : 0;
        }
        /* Appropriate search command */
        if ( BAD( DS2482_triple(bits, search_direction, file_descriptor) ) )  {
            return search_error;
        }
        if (bits[0] || bits[1] || bits[2]) {
            if (bits[0] && bits[1]) {	/* 1,1 */
                /* No devices respond */
                ds->LastDevice = 1;
                return search_done;
            }
        } else {				/* 0,0,0 */
            last_zero = bit_number;
        }
        UT_setbit(ds->sn, bit_number, bits[2]);
    }							// loop until through serial number bits

    if (CRC8(ds->sn, SERIAL_NUMBER_SIZE) || (bit_number < 64) || (ds->sn[0] == 0)) {
        /* Unsuccessful search or error -- possibly a device suddenly added */
        return search_error;
    }
    // if the search was successful then
    ds->LastDiscrepancy = last_zero;
    ds->LastDevice = (last_zero < 0);
    LEVEL_DEBUG("SN found: " SNformat "", SNvar(ds->sn));
    return search_good;
}
Esempio n. 14
0
/* Not used by more advanced adapters */
enum search_status BUS_next_both_bitbang(struct device_search *ds, const struct parsedname *pn)
{
	if ( BAD( BUS_select(pn) ) ) {
		return search_error ;
	} else {
		int search_direction = 0;	/* initialization just to forestall incorrect compiler warning */
		int bit_number;
		int last_zero = -1;
		BYTE bits[3];
		
		// initialize for search
		// if the last call was not the last one
		if (ds->LastDevice) {
			return search_done;
		}
		
		/* Appropriate search command */
		if ( BAD( BUS_send_data(&(ds->search), 1, pn)) ) {
			return search_error ;
		}

		// Need data from a reset for AnyDevices -- obtained from BUS_data_send above
		if (pn->selected_connection->AnyDevices == anydevices_no) {
			ds->LastDevice = 1;
			return search_done;
		}

		// loop to do the search
		for (bit_number = 0;; ++bit_number) {
			bits[1] = bits[2] = 0xFF;
			if (bit_number == 0) {	/* First bit */
				/* get two bits (AND'ed bit and AND'ed complement) */
				if ( BAD( BUS_sendback_bits(&bits[1], &bits[1], 2, pn) ) ) {
					return search_error;
				}
			} else {
				bits[0] = search_direction;
				if (bit_number < 64) {
					/* Send chosen bit path, then check match on next two */
					if ( BAD( BUS_sendback_bits(bits, bits, 3, pn) ) ) {
						return search_error;
					}
				} else {		/* last bit */
					if ( BAD( BUS_sendback_bits(bits, bits, 1, pn) ) ) {
						return search_error;
					}
					break;
				}
			}
			if (bits[1]) {
				if (bits[2]) {	/* 1,1 */
					/* No devices respond */
					ds->LastDevice = 1;
					return search_done;
				} else {		/* 1,0 */
					search_direction = 1;	// bit write value for search
				}
			} else if (bits[2]) {	/* 0,1 */
				search_direction = 0;	// bit write value for search
			} else if (bit_number > ds->LastDiscrepancy) {	/* 0,0 looking for last discrepancy in this new branch */
				// Past branch, select zeros for now
				search_direction = 0;
				last_zero = bit_number;
			} else if (bit_number == ds->LastDiscrepancy) {	/* 0,0 -- new branch */
				// at branch (again), select 1 this time
				search_direction = 1;	// if equal to last pick 1, if not then pick 0
			} else if (UT_getbit(ds->sn, bit_number)) {	/* 0,0 -- old news, use previous "1" bit */
				// this discrepancy is before the Last Discrepancy
				search_direction = 1;
			} else {			/* 0,0 -- old news, use previous "0" bit */
				// this discrepancy is before the Last Discrepancy
				search_direction = 0;
				last_zero = bit_number;
			}
			UT_setbit(ds->sn, bit_number, search_direction);
			
		}	// loop until through serial number bits
		
		if ( (CRC8(ds->sn, SERIAL_NUMBER_SIZE)!=0) || (bit_number < 64) || (ds->sn[0] == 0)) {
			/* A minor "error" */
			return search_error;
		}
		// if the search was successful then
		
		ds->LastDiscrepancy = last_zero;
		//    printf("Post, lastdiscrep=%d\n",si->LastDiscrepancy) ;
		ds->LastDevice = (last_zero < 0);
		return search_good;
	}
}
Esempio n. 15
0
static GOOD_OR_BAD BUS_transaction_single(const struct transaction_log *t, const struct parsedname *pn)
{
	GOOD_OR_BAD ret = gbGOOD;
	switch (t->type) {
	case trxn_select:			// select a 1-wire device (by unique ID)
		ret = BUS_select(pn);
		LEVEL_DEBUG("select = %d", ret);
		break;
	case trxn_compare:			// match two strings -- no actual 1-wire
		if ((t->in == NULL) || (t->out == NULL)
			|| (memcmp(t->in, t->out, t->size) != 0)) {
			ret = gbBAD;
		}
		LEVEL_DEBUG("compare = %d", ret);
		break;
	case trxn_bitcompare:			// match two strings -- no actual 1-wire
		if ((t->in == NULL) || (t->out == NULL)
			|| BAD( BUS_compare_bits( t->in, t->out, t->size)) ) {
			ret = gbBAD;
		}
		LEVEL_DEBUG("compare = %d", ret);
		break;
	case trxn_match:			// send data and compare response
		assert(t->in == NULL);	// else use trxn_compare
		if (t->size == 0) {		/* ignore for both cases */
			break;
		} else {
			ret = BUS_send_data(t->out, t->size, pn);
			LEVEL_DEBUG("send = %d", ret);
		}
		break;
	case trxn_bitmatch:			// send data and compare response
		assert(t->in == NULL);	// else use trxn_compare
		if (t->size == 0) {		/* ignore for both cases */
			break;
		} else {
			ret = BUS_send_bits(t->out, t->size, pn);
			LEVEL_DEBUG("send bits = %d", ret);
		}
		break;
	case trxn_read:
		assert(t->out == NULL);	// pure read
		if (t->size == 0) {		/* ignore for all cases */
			break;
		} else if (t->out == NULL) {
			ret = BUS_readin_data(t->in, t->size, pn);
			LEVEL_DEBUG("readin = %d", ret);
		}
		break;
	case trxn_bitread:
		assert(t->out == NULL);	// pure read
		if (t->size == 0) {		/* ignore for all cases */
			break;
		} else if (t->out == NULL) {
			ret = BUS_readin_bits(t->in, t->size, pn);
			LEVEL_DEBUG("readin bits = %d", ret);
		}
		break;
	case trxn_modify:			// write data and read response. No match needed
		ret = BUS_sendback_data(t->out, t->in, t->size, pn);
		LEVEL_DEBUG("modify = %d", ret);
		break;
	case trxn_bitmodify:			// write data and read response. No match needed
		ret = BUS_sendback_bits(t->out, t->in, t->size, pn);
		LEVEL_DEBUG("bit modify = %d", ret);
		break;
	case trxn_blind:			// write data ignore response
		{
			BYTE *dummy = owmalloc(t->size);
			if (dummy != NULL) {
				ret = BUS_sendback_data(t->out, dummy, t->size, pn);
				owfree(dummy);
			} else {
				ret = gbBAD;
			}
		}
		LEVEL_DEBUG("blind = %d", ret);
		break;
	case trxn_power:
		ret = BUS_PowerByte(t->out[0], t->in, t->size, pn);
		LEVEL_DEBUG("power (%d usec) = %d", t->size, ret);
		break;
	case trxn_bitpower:
		ret = BUS_PowerBit(t->out[0], t->in, t->size, pn);
		LEVEL_DEBUG("power bit (%d usec) = %d", t->size, ret);
		break;
	case trxn_program:
		ret = BUS_ProgramPulse(pn);
		LEVEL_DEBUG("program pulse = %d", ret);
		break;
	case trxn_crc8:
		ret = CRC8(t->out, t->size);
		LEVEL_DEBUG("CRC8 = %d", ret);
		break;
	case trxn_crc8seeded:
		ret = CRC8seeded(t->out, t->size, ((UINT *) (t->in))[0]);
		LEVEL_DEBUG("seeded CRC8 = %d", ret);
		break;
	case trxn_crc16:
		ret = CRC16(t->out, t->size);
		LEVEL_DEBUG("CRC16 = %d", ret);
		break;
	case trxn_crc16seeded:
		ret = CRC16seeded(t->out, t->size, ((UINT *) (t->in))[0]);
		LEVEL_DEBUG("seeded CRC16 = %d", ret);
		break;
	case trxn_delay:
		if (t->size > 0) {
			UT_delay(t->size);
		}
		LEVEL_DEBUG("Delay %d", t->size);
		break;
	case trxn_udelay:
		if (t->size > 0) {
			UT_delay_us(t->size);
		}
		LEVEL_DEBUG("Micro Delay %d", t->size);
		break;
	case trxn_reset:
		ret = BUS_reset(pn)==BUS_RESET_OK ? gbGOOD : gbBAD;
		LEVEL_DEBUG("reset = %d", ret);
		break;
	case trxn_end:
		LEVEL_DEBUG("end = %d", ret);
		return gbOTHER;			// special "end" flag
	case trxn_verify:
		{
			struct parsedname pn2;
			memcpy(&pn2, pn, sizeof(struct parsedname));	//shallow copy
			pn2.selected_device = NO_DEVICE;
			if ( BAD( BUS_select(&pn2) )) {
				ret = gbBAD ;
			} else if ( BAD(BUS_verify(t->size, pn) )) {
				ret = gbBAD ;
			} else {
				ret = gbGOOD;
			}
			LEVEL_DEBUG("verify = %d", ret);
		}
		break;
	case trxn_nop:
		ret = gbGOOD;
		break;
	}
	return ret;
}
Esempio n. 16
0
static GOOD_OR_BAD LINK_directory(struct device_search *ds, struct connection_in * in)
{
	char resp[DEVICE_LENGTH+COMMA_LENGTH+PLUS_LENGTH+in->CRLF_size];

	DirblobClear( &(ds->gulp) );

	// Send the configuration command and check response
	RETURN_BAD_IF_BAD( LINK_search_type( ds, in )) ;

	// send the first search
	RETURN_BAD_IF_BAD(LINK_write(LINK_string("f"), 1, in)) ;

	//One needs to check the first character returned.
	//If nothing is found, the link will timeout rather then have a quick
	//return.  This happens when looking at the alarm directory and
	//there are no alarms pending
	//So we grab the first character and check it.  If not an E leave it
	//in the resp buffer and get the rest of the response from the LINK
	//device

	RETURN_BAD_IF_BAD(LINK_read(LINK_string(resp), 1, in)) ;
	
	switch (resp[0]) {
		case 'E':
			LEVEL_DEBUG("LINK returned E: No devices in alarm");
			// pass through
		case 'N':
			// remove extra 2 bytes
			LEVEL_DEBUG("LINK returned E or N: Empty bus");
			if (ds->search != _1W_CONDITIONAL_SEARCH_ROM) {
				in->AnyDevices = anydevices_no;
			}
			return gbGOOD ;
		default:
			break ;
	}
	
	if ( BAD(LINK_read(LINK_string(&resp[1+in->CRLF_size]), DEVICE_LENGTH+COMMA_LENGTH+PLUS_LENGTH-1-in->CRLF_size, in)) ) {
		return gbBAD;
	}

	// Check if we should start scanning
	switch (resp[0]) {
	case '-':
	case '+':
		if (ds->search != _1W_CONDITIONAL_SEARCH_ROM) {
			in->AnyDevices = anydevices_yes;
		}
		break;
	default:
		LEVEL_DEBUG("LINK_search unrecognized case");
		return gbBAD;
	}

	/* Join the loop after the first query -- subsequent handled differently */
	while ((resp[0] == '+') || (resp[0] == '-')) {
		BYTE sn[SERIAL_NUMBER_SIZE];

		sn[7] = string2num(&resp[2]);
		sn[6] = string2num(&resp[4]);
		sn[5] = string2num(&resp[6]);
		sn[4] = string2num(&resp[8]);
		sn[3] = string2num(&resp[10]);
		sn[2] = string2num(&resp[12]);
		sn[1] = string2num(&resp[14]);
		sn[0] = string2num(&resp[16]);
		LEVEL_DEBUG("SN found: " SNformat, SNvar(sn));

		// CRC check
		if (CRC8(sn, SERIAL_NUMBER_SIZE) || (sn[0] == 0x00)) {
			LEVEL_DEBUG("BAD family or CRC8");
			return gbBAD;
		}

		DirblobAdd(sn,  &(ds->gulp) );

		switch (resp[0]) {
		case '+':
			// get next element
			if ( BAD(LINK_write(LINK_string("n"), 1, in))) {
				return gbBAD;
			}
			if ( BAD(LINK_read(LINK_string((resp)), DEVICE_LENGTH+COMMA_LENGTH+PLUS_LENGTH, in)) ) {
				return gbBAD;
			}
			break;
		case '-':
			return gbGOOD;
		default:
			break;
		}
	}

	return gbGOOD;
}
Esempio n. 17
0
static GOOD_OR_BAD Bundle_unpack(struct transaction_bundle *tb)
{
	int packet_index;
	const struct transaction_log *tl;
	BYTE *data = MemblobData(&(tb->mb));
	GOOD_OR_BAD ret = gbGOOD;

	LEVEL_DEBUG("unpacking");

	for (packet_index = 0, tl = tb->start; packet_index < tb->packets; ++packet_index, ++tl) {
		switch (tl->type) {
		case trxn_compare:		// match two strings -- no actual 1-wire
			LEVEL_DEBUG("unpacking #%d COMPARE", packet_index);
			if ((tl->in == NULL) || (tl->out == NULL)
				|| (memcmp(tl->in, tl->out, tl->size) != 0)) {
				ret = gbBAD;
			}
			break;
		case trxn_bitcompare:			// match two strings -- no actual 1-wire
			if ((tl->in == NULL) || (tl->out == NULL)
				|| BAD( BUS_compare_bits( tl->in, tl->out, tl->size)) ) {
				ret = gbBAD;
			}
			break ;
		case trxn_match:		// send data and compare response
			LEVEL_DEBUG("unpacking #%d MATCH", packet_index);
			if (memcmp(tl->out, data, tl->size) != 0) {
				ret = gbBAD;
			}
			data += tl->size;
			break;
		case trxn_read:
		case trxn_modify:		// write data and read response. No match needed
			LEVEL_DEBUG("unpacking #%d READ MODIFY", packet_index);
			memmove(tl->in, data, tl->size);
			data += tl->size;
			break;
		case trxn_blind:
			LEVEL_DEBUG("unpacking #%d BLIND", packet_index);
			data += tl->size;
			break;
		case trxn_power:
		case trxn_program:
			LEVEL_DEBUG("unpacking #%d POWER PROGRAM", packet_index);
			memmove(tl->in, data, 1);
			data += 1;
			UT_delay(tl->size);
			break;
		case trxn_crc8:
			LEVEL_DEBUG("unpacking #%d CRC8", packet_index);
			if (CRC8(tl->out, tl->size) != 0) {
				ret = gbBAD;
			}
			break;
		case trxn_crc8seeded:
			LEVEL_DEBUG("unpacking #%d CRC8 SEEDED", packet_index);
			if (CRC8seeded(tl->out, tl->size, ((UINT *) (tl->in))[0]) != 0) {
				ret = gbBAD;
			}
			break;
		case trxn_crc16:
			LEVEL_DEBUG("unpacking #%d CRC16", packet_index);
			if (CRC16(tl->out, tl->size) != 0) {
				ret = gbBAD;
			}
			break;
		case trxn_crc16seeded:
			LEVEL_DEBUG("unpacking #%d CRC16 SEEDED", packet_index);
			if (CRC16seeded(tl->out, tl->size, ((UINT *) (tl->in))[0]) != 0) {
				ret = gbBAD;
			}
			break;
		case trxn_delay:
			LEVEL_DEBUG("unpacking #%d DELAY", packet_index);
			UT_delay(tl->size);
			break;
		case trxn_udelay:
			LEVEL_DEBUG("unpacking #%d UDELAY", packet_index);
			UT_delay_us(tl->size);
			break;
		case trxn_reset:
		case trxn_end:
		case trxn_verify:
			// should never get here
			LEVEL_DEBUG("unpacking #%d RESET END VERIFY", packet_index);
			ret = gbBAD;
			break;
		case trxn_nop:
		case trxn_select:
			LEVEL_DEBUG("unpacking #%d NOP or SELECT", packet_index);
			break;
		case trxn_bitmatch:
			LEVEL_DEBUG("Unsupported transaction request trxn_bitmatch");
			break ;
		case trxn_bitmodify:
			LEVEL_DEBUG("Unsupported transaction request trxn_bitmodify");
			break ;
		case trxn_bitread:
			LEVEL_DEBUG("Unsupported transaction request trxn_bitread");
			break ;
		case trxn_bitpower:
			LEVEL_DEBUG("Unsupported transaction request trxn_bitpower");
			break ;
		}
		if ( BAD(ret) ) {
			break;
		}
	}

	// clear the bundle
	MemblobClear(&tb->mb);
	tb->packets = 0;
	tb->select_first = 0;

	return ret;
}