/*
This software is subject to the license described in the license.txt file included with this software distribution.
You may not use this file except in compliance with this license.
Copyright � Dynastream Innovations Inc. 2012
All rights reserved.
*/
#include "antfs.h"
#include <string.h>
#include "defines.h"
#include "app_error.h"
#include "app_timer.h"
#include "ant_error.h"
#include "ant_parameters.h"
#include "ant_interface.h"
#include "crc.h"
#ifdef LEDDRIVER_ACTIVE
#include "leddriver.h"
#endif // LEDDRIVER_ACTIVE
#define BURST_PACKET_SIZE 8u /**< The burst packet size. */
#define ANTFS_NETWORK_KEY {0,0,0,0,0,0,0,0} /**< The used ANT-FS network key. */
#define ANTFS_CONNECTION_TYPE_OFFSET 0x00u /**< The connection type offset within ANT-FS message. */
#define ANTFS_COMMAND_OFFSET 0x01u /**< The command offset within ANT-FS message. */
#define ANTFS_RESPONSE_OFFSET 0x01u /**< The response offset within ANT-FS message. */
#define ANTFS_CONTROL_OFFSET 0x02u /**< The control offset within ANT-FS message. */
#define ANTFS_DATA_OFFSET 0x03u /**< The data offset within ANT-FS message. */
#define ANTFS_BEACON_ID 0x43u /**< The ANT-FS beacon ID. */
#define ANTFS_COMMAND_ID 0x44u /**< The ANT-FS command ID. */
// Beacon definitions.
#define STATUS1_OFFSET 0x01u /**< The beacon status1 field offset. */
#define STATUS2_OFFSET 0x02u /**< The beacon status2 field offset. */
#define DEVICE_STATE_SHIFT 0x00u /**< Shift value for device state bitfield. */
#define DEVICE_STATE_MASK (0x0Fu << DEVICE_STATE_SHIFT) /**< Device state bitmask. */
#define DEVICE_STATE_LINK (0x00u << DEVICE_STATE_SHIFT) /**< Device is in link state. */
#define DEVICE_STATE_AUTHENTICATE (0x01u << DEVICE_STATE_SHIFT) /**< Device is in authenticate state. */
#define DEVICE_STATE_TRANSPORT (0x02u << DEVICE_STATE_SHIFT) /**< Device is in transport state. */
#define DEVICE_STATE_BUSY (0x03u << DEVICE_STATE_SHIFT) /**< Device is in busy state. */
#define DEVICE_DESCRIPTOR_OFFSET_0 0x04u /**< Beacon ANT-FS device descriptor LSB position. */
#define DEVICE_DESCRIPTOR_OFFSET_1 0x05u /**< Beacon ANT-FS device descriptor LSB + 1 position. */
#define DEVICE_DESCRIPTOR_OFFSET_2 0x06u /**< Beacon ANT-FS device descriptor LSB + 2 position. */
#define DEVICE_DESCRIPTOR_OFFSET_3 0x07u /**< Beacon ANT-FS device descriptor MSB position. */
// Commands.
#define ANTFS_CMD_NONE 0x00u /**< Used to identify that no ANT-FS command is in progress. */
#define ANTFS_CMD_LINK_ID 0x02u /**< ANT-FS link command ID. */
#define ANTFS_CMD_DISCONNECT_ID 0x03u /**< ANT-FS disconnect command ID. */
#define ANTFS_CMD_AUTHENTICATE_ID 0x04u /**< ANT-FS authenticate command ID. */
#define ANTFS_CMD_PING_ID 0x05u /**< ANT-FS ping command ID. */
#define ANTFS_CMD_DOWNLOAD_ID 0x09u /**< ANT-FS download request command ID. */
#define ANTFS_CMD_UPLOAD_REQUEST_ID 0x0Au /**< ANT-FS upload request command ID. */
#define ANTFS_CMD_ERASE_ID 0x0Bu /**< ANT-FS erase request command ID. */
#define ANTFS_CMD_UPLOAD_DATA_ID 0x0Cu /**< ANT-FS upload command ID. */
// Responses.
#define ANTFS_RSP_AUTHENTICATE_ID 0x84u /**< ANT-FS authenticate response command ID. */
#define ANTFS_RSP_DOWNLOAD_ID 0x89u /**< ANT-FS download request response command ID. */
#define ANTFS_RSP_UPLOAD_REQ_ID 0x8Au /**< ANT-FS upload request response command ID. */
#define ANTFS_RSP_ERASE_ID 0x8Bu /**< ANT-FS erase response command ID. */
#define ANTFS_RSP_UPLOAD_DATA_ID 0x8Cu /**< ANT-FS upload data response command ID. */
// Link command.
#define TRANSPORT_CHANNEL_FREQUENCY_OFFSET 0x02u /**< Channel frequency field offset within link command. */
#define TRANSPORT_MESSAGE_PERIOD_OFFSET 0x03u /**< Channel period field offset within link command. */
#define HOST_ID_OFFSET_0 0x04u /**< Host serial number period field LSB offset within link command. */
#define HOST_ID_OFFSET_1 0x05u /**< Host serial number period field LSB + 1 offset within link command. */
#define HOST_ID_OFFSET_2 0x06u /**< Host serial number period field LSB + 2 offset within link command. */
#define HOST_ID_OFFSET_3 0x07u /**< Host serial number period field MSB offset within link command. */
// Authenticate command.
#define COMMAND_TYPE_OFFSET 0x02u /**< Command type field offset within authenticate command. */
#define COMMAND_TYPE_PROCEED 0x00u /**< Command type proceed to transport in the authenticate command. */
#define COMMAND_TYPE_REQUEST_SERIAL 0x01u /**< Command type request client device serial number in the authenticate command. */
#define COMMAND_TYPE_REQUEST_PAIR 0x02u /**< Command type request pairing in the authenticate command. */
#define COMMAND_TYPE_REQUEST_PASSKEY 0x03u /**< Command type request passkey exchange in the authenticate command. */
// Authenticate response.
#define RESPONSE_TYPE_OFFSET 0x02u /**< Command type field offset within authenticate response command. */
#define AUTH_RESPONSE_N_A 0x00u /**< Command response type N/A (response for client serial number request). */
#define AUTH_RESPONSE_ACCEPT 0x01u /**< Command response type accept. */
#define AUTH_RESPONSE_REJECT 0x02u /**< Command response type reject. */
// Authenticate command/response.
#define AUTH_STRING_LENGTH_OFFSET 0x03u /**< Authenticate Command/Response authentication string length offset. */
#define SERIAL_NUMBER_OFFSET_0 0x04u /**< Authenticate Command/Response client/host serial number LSB offset. */
#define SERIAL_NUMBER_OFFSET_1 0x05u /**< Authenticate Command/Response client/host serial number LSB + 1 offset. */
#define SERIAL_NUMBER_OFFSET_2 0x06u /**< Authenticate Command/Response client/host serial number LSB + 2 offset. */
#define SERIAL_NUMBER_OFFSET_3 0x07u /**< Authenticate Command/Response client/host serial number MSB offset. */
// Download/Upload/Erase commands.
#define INITIAL_REQUEST_OFFSET 0x01u /**< Download/Upload/Erase command initial request command offset. */
#define DATA_INDEX_OFFSET_LOW