/* * Returns: 0 on failure * 1 on success */ unsigned char send_packet(unsigned char to_addr, unsigned char *data, unsigned char data_len) { // TODO: Wait for timeout to tick down (in multi-station mode) // Too much data if (data_len > PACKET_DATA_MAX) { return 0; } unsigned char crc; crc8_init(0xFF); rfm12_EnableTx(); // TODO: turn on transmitter rfm12_Tx_Byte(0xAA); // Preamble for RFM12 rfm12_Tx_Byte(0xAA); rfm12_Tx_Byte(0xAA); rfm12_Tx_Byte(0x2D); // RFM12 sync pattern rfm12_Tx_Byte(0xD4); rfm12_Tx_Byte(HEADER_LENGTH + data_len); // Total packet length crc = crc8_byte(HEADER_LENGTH + data_len); rfm12_Tx_Byte(PACKET_MAGIC); // Magic identification bytes crc = crc8_byte(PACKET_MAGIC); rfm12_Tx_Byte(NODE_ADDRESS); // Sender ID crc = crc8_byte(NODE_ADDRESS); rfm12_Tx_Byte(to_addr); // Destination ID crc = crc8_byte(to_addr); rfm12_Tx_Byte(sequence_number); // Packet sequence number crc = crc8_byte(sequence_number); sequence_number++; rfm12_Tx_Byte(0); // Reserved for future use crc = crc8_byte(0); rfm12_Tx_Byte(0); // Reserved for future use crc = crc8_byte(0); for (int n = 0; n < data_len; n++) { rfm12_Tx_Byte(data[n]); crc = crc8_byte(data[n]); } rfm12_Tx_Byte(crc); // CRC rfm12_Tx_Byte(0xAA); // Trailer rfm12_Tx_Byte(0xAA); rfm12_DisableTx(); // Turn off Tx return 1; }
/* * Given offset in flash area, fill in rest of the fcb_entry, and crc8 over * the data. */ int fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, uint8_t *c8p) { uint8_t tmp_str[FCB_TMP_BUF_SZ]; int cnt; int blk_sz; uint8_t crc8; uint16_t len; uint32_t off; uint32_t end; int rc; if (loc->fe_elem_off + 2 > loc->fe_area->fa_size) { return FCB_ERR_NOVAR; } rc = flash_area_read(loc->fe_area, loc->fe_elem_off, tmp_str, 2); if (rc) { return FCB_ERR_FLASH; } cnt = fcb_get_len(tmp_str, &len); if (cnt < 0) { return cnt; } loc->fe_data_off = loc->fe_elem_off + fcb_len_in_flash(fcb, cnt); loc->fe_data_len = len; crc8 = crc8_init(); crc8 = crc8_calc(crc8, tmp_str, cnt); off = loc->fe_data_off; end = loc->fe_data_off + len; for (; off < end; off += blk_sz) { blk_sz = end - off; if (blk_sz > sizeof(tmp_str)) { blk_sz = sizeof(tmp_str); } rc = flash_area_read(loc->fe_area, off, tmp_str, blk_sz); if (rc) { return FCB_ERR_FLASH; } crc8 = crc8_calc(crc8, tmp_str, blk_sz); } *c8p = crc8; return 0; }
unsigned char _process_packet(unsigned char bytes_received) { unsigned char length = 0; unsigned char crc = 0; receive_count++; // Check for the start-of-packet byte if (packet_buffer[0] != PACKET_MAGIC) { return 0; } // Check that the packet is for us (or, in multicast mode, is for // the multicast address) #ifdef PACKET_MULTICAST if (packet_buffer[2] != NODE_ADDRESS && packet_buffer[2] != MULTICAST_ADDRESS) { #else if (packet_buffer[2] != NODE_ADDRESS) { #endif return 0; } // Extract the length length = packet_buffer[3]; // Calculate the CRC for the whole packet, minus the CRC // which is at the end crc8_init(0xff); for (int n = 0; n < length - 1; n++) { crc = crc8_byte(packet_buffer[n]); } // Check the CRC against the packet if (crc != packet_buffer[length - 1]) { return 0; } // Copy the data to the buffer for (int n = 0; n < length - HEADER_LENGTH; n++) { data_buffer[n] = packet_buffer[7 + n]; } data_arrived = length - HEADER_LENGTH; return 1; }