/**************************************************************************** Module DOG_RX pseudo code Description This is the receiver service for the DOG which receives messages from the farmer /*----------------------------- Module Defines ----------------------------*/ these times assume a 1.000mS/tick timing: #define ONE_SEC 1000 #define HALF_SEC (ONE_SEC/2) #define TWO_SEC (ONE_SEC*2) #define FIVE_SEC (ONE_SEC*5) #define LARGEST_FRAME_DATA 50 packet byte definition: #define CHECKSUM_OFFSET 0xFF #define START_BYTE 0x7E #define API_TX_ID 0x01 #define OPTION_BYTE 0x00 #define MSB_LEN 0x00 #define MAX_XBEE_PACKET_SIZE 9 #define API_RX_ID 0x81 #define API_TX_STATUS_ID 0x89 #define ACK_STATUS 0 #define NACK_STATUS 1 #define AD_CHANNEL_NUM 1 /*---------------------------- Module Functions ---------------------------*/ /* prototypes for private functions for this service.They should be functions relevant to the behavior of this service */ static void ClearDataPacket(void) static void AnalyzeRXPacket(void) static void ClearEncryption(void) static void PrintOutReceivedPacket(void) /*---------------------------- Module Variables ---------------------------*/ init a priority variable init variables to store the packet lengths init variables for check sum and received check sum init variables for the API and frame IDs init variable to store the transmission status init variable to store the status of a byte remaining init variables for length of MSB and LSB init variables to store MSB and LSB addresses for receiving init variable to store the DOG ID init variable to store the FARMER address init variables to store MSB and LSB addresses for transmitting init variable for encryption index init an array for the data packet init an array for the Xbee packet init a variable for the encryption key length init an array for the encryption key init variable for the rx index init variable for the length of the control message init variable for the control message init boolean to track whether we're in control mode init array for AD data for dog tag reading init variables for tolerances and set points of the dog tag voltages /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function InitDogRX Parameters uint8_t : the priorty of this service Returns bool, false if error in initialization, true otherwise Description Saves away the priority, and does any other required initialization for this service ****************************************************************************/ bool InitDogRX ( uint8_t Priority ) { declare ThisEvent variable set MyPriority to Priority /******************************************** Initialization Code *******************************************/ initialize current state to waiting to pair Initialize data and Xbee packet lengths to 0 enable RX interrupt call ReadID() print "Finished initializing Dog RX Service" post the initial transition event } /**************************************************************************** Function PostDogRX Parameters EF_Event ThisEvent ,the event to post to the queue Returns bool false if the Enqueue operation failed, true otherwise Description Posts an event to this state machine's queue ****************************************************************************/ bool PostDogRX( ES_Event ThisEvent ) { post ThisEvent to this service using MyPriority } /**************************************************************************** Function RunDogRX Parameters ES_Event : the event to process Returns ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise ****************************************************************************/ ES_Event RunDogRX( ES_Event ThisEvent ) { declare ReturnEvent variable set ReturnEvent type to ES_NO_EVENT -- assume no errors if ThisEvent type is VALID_PACKET_RECEIVED set DataPacket_Length to the parameter in ThisEvent for all elements in DataPacket set DataPacket[i] to corresponding value in the buffer call PrintOutReceivedPacket() for debugging call AnalyzeRXPacket() to process the data return ReturnEvent } /*************************************************************************** private functions ***************************************************************************/ /* * This function print out the received packet content */ static void PrintOutReceivedPacket(){ (prints the received element for debugging only) } /* * This function updates dog id from the attached dog tag */ void ReadID(){ call ADC_MultiRead(AD_data) to store AD value in AD_data declare raw_id_value as AD data value print "Raw id reading: " and raw_id_value if raw_id_value is in range for dog tag 1 set dog_id to 0x01 else if raw_id_value is in range for dog tag 2 set dog_id to 0x02 else set dog_id to 0x03 print "Set dog id to " and the dog ID } /* * This function clears data packet content */ static void ClearDataPacket(){ for all set bits in data packet clear dat shit } /* * This function clears encryption key array */ static void ClearEncryption(){ for all bits in encryption key clear it, yo } /* * This function reset the encryption key index */ void ResetEncryptKeyIndex(){ reset encryption key index to 0 } /* * This function decrypts the data packet */ static void DecryptMessage(int message_len){ print "Decrypt key: " and the encryption key, "index: " and the index for every bit in the message xor the bit by the correct encryption key bit increment the encryption key index if index is 32 reset it to 0x00 } /* * This function analyzes the recieved data packet */ static void AnalyzeRXPacket(){ set API_id to the 0 bit in the data packet if api id is API_TX_STATUS_ID(this is transmit status message) set frame_id to the 1 bit in the packet set tx_status to the 2 bit in the packet init event ThisEvent if tx_status is ACK_STATUS (acknowledge) set event type to ACK_RECEIVED set event parameter to frame_id post ThisEvent to DOG_TX else if tx_status is NACK_STATUS (not acknowledge) set event type to NACK_RECEIVED set event parameter to frame_id post ThisEvent to DOG_TX else if API_id is API_RX_ID (this is a receive data message) save source address to Source_Address_LSB/MSB variables set farmer address to the source address if rx is not in control mode (message will not be decrypted) if data packet type is request to pair call ReadID() to read the ID if received dog id is the same as on board one post get valid dog id response message to farmer top SM else if data packet is encryption key message update encryption key from DataPacket using for loop Save Farmer Address to Paired_Farmer_Address variable post encryption key received event with param Farmer_Address else (rx is in control command, we need to decrypt data packet) if Farmer_Address equals the saved Paired_Farmer_Address first decrypt the data packet if the decrypted data header is CTRL post CTRL_COMMAND_RECEIVED to farmer top SM -> event param is the control message from DataPacket data -> (get valid dog id response) else (received header is not CTRL) post INVALID_CTRL_COMMAND_RECEIVED to farmer top SM else print "Received message from other unpaired farmer: (Farmer_Address), ignore it" /* * This function updates control mode according to the input */ void SetInControlMode(bool input){ set InControlMode to input value } uint32_t GetControlMessage(){ return ControlMessage } /*------------------------------- Footnotes -------------------------------*/ /*------------------------------ End of file ------------------------------*/