#include "WProgram.h"

/* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

   CONFIRM CHECKSUM

   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
int checksum_confirmed_in( byte *p )
{
   static byte my_checksum;
   static byte xbee_checksum;
   static byte full_checksum;
   static byte data_between_length_and_last;

   static int i;
   static int size;
   static int analog_sample;
   static int state;

   size = ( int ) p[ LENGTH_LSB ];

   for( i = 0, data_between_length_and_last = 0; i < size; i++ )
      data_between_length_and_last += p[ LENGTH_LSB + 1 + i ];

   my_checksum = 
      0xFF & ( 0xFF - data_between_length_and_last );

   xbee_checksum = 
      0xFF & p[ ( IO_DATA_SAMPLE_API_PACKET_SIZE - 1 ) ];

   full_checksum = 
      0xFF & 
      ( data_between_length_and_last + 
      p[ ( IO_DATA_SAMPLE_API_PACKET_SIZE - 1 ) ] );

   if( ( my_checksum == xbee_checksum ) && 
       ( full_checksum == CHECKSUM ) )
      state = VALID;
   else
      state = INVALID;

   return state;
}