Ejemplo n.º 1
0
void vscp_handlePreActiveState(void)
{

    if ( vscp_imsg.flags & VSCP_VALID_MSG ) { // incoming event?

        if ((VSCP_CLASS1_PROTOCOL == vscp_imsg.vscp_class) &&
                (VSCP_TYPE_PROTOCOL_SET_NICKNAME == vscp_imsg.vscp_type) &&
                (VSCP_ADDRESS_FREE == vscp_imsg.data[ 0 ])) {

            // Assign nickname
            vscp_nickname = vscp_imsg.data[ 1 ];
            vscp_writeNicknamePermanent(vscp_nickname);
            vscp_setSegmentCRC(0x40);

            // Go active state
            vscp_node_state = VSCP_STATE_ACTIVE;
        }
    } else {
        // Check for time out
        if (vscp_timer > VSCP_PROBE_TIMEOUT) {
            // Yes, we have a timeout
            vscp_nickname = VSCP_ADDRESS_FREE;
            vscp_writeNicknamePermanent(VSCP_ADDRESS_FREE);
            vscp_init();
        }
    }
}
Ejemplo n.º 2
0
///////////////////////////////////////////////////////////////////////////////
// vscp_preactive
//
void vscp_handleReactive( void )
{
	if ( E_OK == VSCP_deqMsgRx( &RxMsg)) {	// incoming message?
				// Yes, incoming message

		if (( VSCP_CLASS1_PROTOCOL == RxMsg.vscp_class)  && 
				( VSCP_TYPE_PROTOCOL_SET_NICKNAME == RxMsg.vscp_type) &&
				( VSCP_ADDRESS_FREE == RxMsg.data[0])) {
						
			// Assign nickname
			vscp_nickname = RxMsg.data[2];
			vscp_setNickname( vscp_nickname);
			vscp_setSegmentCRC( VSCP_CRC_DEFAULT);// segment code (non server segment )
			vscp_init(); // Clear Activation, with a Right Asigned NICKNAME 
		}		
	}
	else {
		// Check for time out
		if ( vscp_timer > VSCP_PROBE_TIMEOUT) {
			// Yes, we have a timeout
			vscp_nickname = VSCP_ADDRESS_FREE;
			vscp_setNickname( VSCP_ADDRESS_FREE);
			vscp_init();	//No answer from Server, Clear Restart with VSCP_ADDRESS_FREE
		}				
	}
}
Ejemplo n.º 3
0
///////////////////////////////////////////////////////////////////////////////
// vscp_rcv_set_nickname
//
void vscp_handleSetNickname( void)
{
	if (( 2 == RxMsg.length) && 
			( vscp_nickname == RxMsg.data[0])) {

		// Yes, we are addressed
		vscp_nickname = RxMsg.data[1];
		vscp_setNickname( vscp_nickname);		
		vscp_setSegmentCRC( VSCP_CRC_DEFAULT);
	}	
}
Ejemplo n.º 4
0
void vscp_handleSetNickname(void)
{
    if ((2 == (vscp_imsg.flags & 0x0f)) &&
            (vscp_nickname == vscp_imsg.data[ 0 ])) {

        // Yes, we are addressed
        vscp_nickname = vscp_imsg.data[ 1 ];
        vscp_writeNicknamePermanent(vscp_nickname);
        vscp_setSegmentCRC(0x40);
    }
}
Ejemplo n.º 5
0
void vscp_handleHeartbeat(void)
{
	if ( !vscp_getSegmentCRC() ) {
		if ( ( 5 == (vscp_imsg.flags & 0x0f ) ) &&
				(vscp_getSegmentCRC() != vscp_imsg.data[ 0 ])) {

			// Stored CRC are different than received
			// We must be on a different segment
			vscp_setSegmentCRC(vscp_imsg.data[ 0 ]);

			// Introduce ourself in the proper way and start from the beginning
			vscp_nickname = VSCP_ADDRESS_FREE;
			vscp_writeNicknamePermanent(VSCP_ADDRESS_FREE);
			vscp_node_state = VSCP_STATE_INIT;
		}
	}
	else {
		// First heartbeat seen by this node
		vscp_setSegmentCRC(vscp_imsg.data[ 0 ]);
	}
}
Ejemplo n.º 6
0
///////////////////////////////////////////////////////////////////////////////
// vscp_rcv_heartbeat
//
void vscp_handleHeartbeat( void )
{
	if ((5 == RxMsg.length)&&
		 ( vscp_getSegmentCRC()!= RxMsg.data[0])) {
		// Stored CRC are different than received
		// We must be on a different segment
		vscp_setSegmentCRC( RxMsg.data[0]);
		
		// Introduce ourself in the proper way and start from the beginning
		vscp_nickname = VSCP_ADDRESS_FREE;
		vscp_setNickname( VSCP_ADDRESS_FREE);
		vscp_init(); // Clear Restart with VSCP_ADDRESS_FREE
	}		
}
Ejemplo n.º 7
0
///////////////////////////////////////////////////////////////////////////////
// vscp_check_pstorage
//
// Check control position integrity and restore EEPROM
// if invalid.
//
int08_t vscp_check_pstorage( void )
{
	// controlbyte == 01xxxxxx means initialized
	// everything else is uninitialized
	if ( VSCP_CRC_FREE != vscp_getSegmentCRC()) {
		return TRUE;
	}
	// No nickname yet.
	vscp_setNickname( VSCP_ADDRESS_FREE);
	// No segment CRC yet.
	vscp_setSegmentCRC( VSCP_CRC_FREE);
	vscp_setControlByte( VSCP_CONTROL_STARTUP);		// Initial startup, write allowed

	return FALSE;
}
Ejemplo n.º 8
0
int8_t vscp_check_pstorage(void)
{
    // controlbyte == 01xxxxxx means initialized
    // everything else is uninitialized
    if ((vscp_getSegmentCRC() & 0xc0) == 0x40) {
        return TRUE;
    }

    // No nickname yet.
    vscp_writeNicknamePermanent(0xff);

    // No segment CRC yet.
    vscp_setSegmentCRC(0x00);

    // Initial startup
    // write allowed
    vscp_setControlByte(0xA0);

    return FALSE;
}
/*!
 * @brief verify functionality of the custom defined vscp functions
 *        vscp_firmware.h @ line 457
 */
void test_vscp_externals(){

	uint8_t FlashValue8; 		   /*! gets an 8-bit vscp data field from permanent storage */
	uint8_t val;				   /*! assign a value to write to buffer -> permanent storage */

	printf("Testing vscp_functions\r\n");

	val = 0x11;
	vscp_writeNicknamePermanent(val);
	FlashValue8 = vscp_readNicknamePermanent();
	printf("Read %d dec, %x hex\r\n", FlashValue8, FlashValue8);

	val = 0x22;
	vscp_setSegmentCRC(val);
	FlashValue8 = vscp_getSegmentCRC();
	printf("Read %d dec, %x hex\r\n", FlashValue8, FlashValue8);

	val = 0x33;
	vscp_setControlByte(val);
	FlashValue8 = vscp_getControlByte();
	printf("Read %d dec, %x hex\r\n", FlashValue8, FlashValue8);
}
Ejemplo n.º 10
0
///////////////////////////////////////////////////////////////////////////////
// vscp_handleProbeState
//
void vscp_handleProbeState( void )
{
	switch ( vscp_node_substate ) {
		case VSCP_SUBSTATE_NONE:
			if ( VSCP_ADDRESS_FREE != vscp_probe_address ) {
				SendMsg.priority = VSCP_PRIORITY_HIGH;	
				SendMsg.vscp_class = VSCP_CLASS1_PROTOCOL;
				SendMsg.vscp_type = VSCP_TYPE_PROTOCOL_NEW_NODE_ONLINE;
				SendMsg.length =  1 ;	
				SendMsg.data[ 0 ] = vscp_probe_address;			
				// send the probe
				VSCP_enqMsgTx( &SendMsg, FALSE);
				vscp_node_substate = VSCP_SUBSTATE_INIT_PROBE_SENT;
				vscp_timer = 0;	
			}
			else {
				// No free address -> error
				vscp_node_state = VSCP_STATE_ERROR;
				// Tell system we are giving up
				SendMsg.priority = VSCP_PRIORITY_HIGH;	
				SendMsg.vscp_class = VSCP_CLASS1_PROTOCOL;
				SendMsg.vscp_type = VSCP_TYPE_PROTOCOL_PROBE_ACK;
				SendMsg.data[ 0 ] = 0xff;				// we are unassigned			
				SendMsg.length =  1 ;
				// send the error event
				VSCP_enqMsgTx( &SendMsg, FALSE);
			}
			break;	

		case VSCP_SUBSTATE_INIT_PROBE_SENT:
			if ( E_OK == VSCP_deqMsgRx( &RxMsg)) {	// incoming message?
				// Yes, incoming message
				if ( ( VSCP_CLASS1_PROTOCOL == RxMsg.vscp_class ) && 
					( VSCP_TYPE_PROTOCOL_PROBE_ACK == RxMsg.vscp_type ) ) {
					// Yes it was an ack from the segment master or a node
					if ( !vscp_probe_address ) {
						// Master controller answered
						// wait for address
						vscp_node_state = VSCP_STATE_PREACTIVE;
						vscp_timer = 0; // reset timer
					}
					else {
						// node answered, try next address
						vscp_probe_address++;
						vscp_node_substate = VSCP_SUBSTATE_NONE;
						vscp_probe_cnt = 0;
					}	
				}
			}
			else {
				if ( vscp_timer > VSCP_PROBE_TIMEOUT ) {	// Check for timeout
 					vscp_probe_cnt++;	// Another timeout. Repet each probe four times ( 4 * 500ms = 2 S)
					if ( vscp_probe_cnt >= 4) { 
						// Yes we have a timeout, no answer received
						if ( !vscp_probe_address) {			// master controller probe?
							// No master controler on segment, try next node
							vscp_probe_address++;
							vscp_node_substate = VSCP_SUBSTATE_NONE;
						}
						else {
							// No answer, We have found a free address - use it
							vscp_nickname = vscp_probe_address;
							vscp_setNickname( vscp_nickname);
							vscp_setSegmentCRC( VSCP_CRC_DEFAULT);// segment code (non server segment )
							vscp_init(); // Clear Activation, with a Right Discovered NICKNAME 
						}
					}
					else {
						vscp_node_substate = VSCP_SUBSTATE_NONE;
					}
				} // Timeout
			}			
			break;

		case VSCP_SUBSTATE_INIT_PROBE_ACK:
			// Developed in function before
			break;

		default: 
			vscp_node_substate = VSCP_SUBSTATE_NONE;
			break;
	}
}
Ejemplo n.º 11
0
void vscp_handleProbeState(void)
{
    switch (vscp_node_substate) {

        case VSCP_SUBSTATE_NONE:

            if (VSCP_ADDRESS_FREE != vscp_probe_address) {

                vscp_omsg.flags = VSCP_VALID_MSG + 1; // one databyte
                vscp_omsg.priority = VSCP_PRIORITY_HIGH;
                vscp_omsg.vscp_class = VSCP_CLASS1_PROTOCOL;
                vscp_omsg.vscp_type = VSCP_TYPE_PROTOCOL_NEW_NODE_ONLINE;
                vscp_omsg.data[ 0 ] = vscp_probe_address;

                // send the probe
                vscp_sendEvent();

                vscp_node_substate = VSCP_SUBSTATE_INIT_PROBE_SENT;
                vscp_timer = 0;

            } else {

                // No free address -> error
                vscp_node_state = VSCP_STATE_ERROR;

                // Tell system we are giving up
                vscp_omsg.flags = VSCP_VALID_MSG + 1; // one databyte
                vscp_omsg.data[ 0 ] = 0xff; // we are unassigned
                vscp_omsg.priority = VSCP_PRIORITY_LOW;
                vscp_omsg.vscp_class = VSCP_CLASS1_PROTOCOL;
                vscp_omsg.vscp_type = VSCP_TYPE_PROTOCOL_PROBE_ACK;

                // send the error event
                vscp_sendEvent();

            }
            break;

        case VSCP_SUBSTATE_INIT_PROBE_SENT:

            if (vscp_imsg.flags & VSCP_VALID_MSG) { // incoming event?

                // Yes, incoming event
                if ((VSCP_CLASS1_PROTOCOL == vscp_imsg.vscp_class) &&
                        (VSCP_TYPE_PROTOCOL_PROBE_ACK == vscp_imsg.vscp_type)) {

                    // Yes it was an ack from the segment master or a node
                    if (0 == vscp_probe_address) {

                        // Master controller answered
                        // wait for address
                        vscp_node_state = VSCP_STATE_PREACTIVE;
                        vscp_timer = 0; // reset timer

                    } else {

                        // node answered, try next address
                        vscp_probe_address++;
                        vscp_node_substate = VSCP_SUBSTATE_NONE;
                        vscp_probe_cnt = 0;

                    }
                }
            } else {

                if (vscp_timer > VSCP_PROBE_TIMEOUT) { // Check for timeout

                    vscp_probe_cnt++; // Another timeout

                    if (vscp_probe_cnt >= VSCP_PROBE_TIMEOUT_COUNT) {

                        // Yes we have a timeout
                        if (0 == vscp_probe_address) { // master controller probe?

                            // No master controler on segment, try next node
                            vscp_probe_address++;
                            vscp_node_substate = VSCP_SUBSTATE_NONE;
                            vscp_timer = 0;
                            vscp_probe_cnt = 0;

                        } else {

                            // We have found a free address - use it
                            vscp_nickname = vscp_probe_address;
                            vscp_node_state = VSCP_STATE_ACTIVE;
                            vscp_node_substate = VSCP_SUBSTATE_NONE;
                            vscp_writeNicknamePermanent(vscp_nickname);
                            vscp_setSegmentCRC(0x40); // segment code (non server segment )

                            // Report success
                            vscp_probe_cnt = 0;
                            vscp_goActiveState();

                        }
                    } else {
                        vscp_node_substate = VSCP_SUBSTATE_NONE;
                    }
                } // Timeout

            }
            break;

        case VSCP_SUBSTATE_INIT_PROBE_ACK:
            break;

        default:
            vscp_node_substate = VSCP_SUBSTATE_NONE;
            break;
    }

    vscp_imsg.flags = 0;

}