Exemple #1
0
/**
 * @brief  Función que recibe el mensaje de zigbee.
 *
 * @return    El cuerpo del mensaje.
 *
 * Hace el tratamiento de la trama zigbee para quedarse con el
 * cuerpo del mensaje y reenviar la trama entera al resto de
 * controladores que se encuentren a su alcance.
*/
MENSAJEClass recibir_mensaje(void){
	MENSAJEClass mensaje; /*Estructura para guardar el cuerpo del mensaje*/
	uint8_t temporal[255]; /*Guarda el mensaje temporalmente*/
	uint8_t recibido[255]; /*Guarda la trama completa del mensaje*/
	int numero_recibido = 3; /*Numero de bytes a coger del buffer intermedio*/
	int checksum; /*Checksum del mensaje recibido*/
	int contador = 0; /*Contador para trasladar los datos de temporal a recibido*/

	UART_recv(gs_i_puerto_zigbee, temporal, &numero_recibido);
	for(contador = 0; contador < numero_recibido; contador++){
		recibido[contador] = temporal[contador];
	}
	numero_recibido = 0;
	numero_recibido += recibido[2] + 1;
	UART_recv(gs_i_puerto_zigbee, temporal, &numero_recibido);
	for(contador = 0; contador < numero_recibido; contador++){
		recibido[contador+3] = temporal[contador];
	}
	checksum = calcular_checksum(recibido);
	if(checksum == recibido[numero_recibido + 2]){
		mensaje = tratar_mensaje(recibido);
		if(mensaje.id == g_i_mi_id || recibido_anteriormente(mensaje)){
			mensaje.id = NULL;
		}
	}else{
		//TODO: ERROR!!!!!
	}

	return mensaje;
}
void METER_ISR ( void )
{
	uint8_t data[3];

	// Interrupt DO fired
	if (DOIFG & DO_PIN) {
		// we need access to the UART interrupts
		DOIE &= ~DO_PIN;
		enable_interrupts();

		// read power
		UART_clear();
		UART_send(METER_read_power, sizeof(METER_read_power));
		UART_recv(data, sizeof(data), '\0', USCI_BLOCKING);

		// convert the byte array to a 32bit value
		metered_power = (uint32_t)data[0];
		metered_power += ((uint32_t)data[1]<<8);
		metered_power += ((uint32_t)data[2]<<16);

		// clear our pending interrupt
		DOIFG &= ~DO_PIN;
		DOIE |= DO_PIN;

		// clear the interrupt from the power meter
		UART_send(METER_clear_drdy, sizeof(METER_clear_drdy));
	}
}