#pragma once
// from old comms proc
// cp.h
/*
RockBlock on Serial1
DataVault Serial 2
conair serial 3
Inbound Message format: type|length|payload
Outbound: RBStatus|length|payload type|payload
*/
#define RockBlock Serial1
#define DataVault Serial2
HardwareSerial* modemSerial;
// message types
#define OUTBOUND_MESSAGE 0x10
#define QUEUE_LENGTH 0x01
#define REQUEST_MODEM_STATE 0x02
#define SENDING_MESSAGE 0x03
#define INBOUND_MESSAGE 0x04
#define SENT_MESSAGE 0x05
#define CP_WAITING 0x06
#define DEVICE_STATUS 0x07
#define TRANSMISSION_ERROR 0x08
#define CONAIR_DATA 0x20
#define I2C_BUS_POWER_OFF 0x30
#define I2C_BUS_IS_ON 0x31
#define I2C_BUS_POWER_ON 0x32
#define AVIONICS_POWER_OFF 0x40
#define AVIONICS_POWER_ON 0x41
#define CLEAR_QUEUE 0x32
#define TEXT_MESSAGE 0x70
#define MODEM_TYPE 0xA0
#define DATE_TIME 0xA1
#define GPS_DATA 0xA2
#define MODEM_CN 0xA3
#define REQUEST_IMEI 0xf0
#define CP_BOOT 0xD0
#define AUX_SCAN_TIME 100 // analog scan interval
// main processor timeout
#define MAIN_PROC_TIMEOUT 40000 // 40 secs
boolean noMainProc = false;
// statuses
#define NO_MODEM 0x01
#define MODEM_OK 0x02
byte RBstatus;
#define RESET_MP_COMMS 0xaa
//IridiumSBD isbd(RockBlock, -1);
// commands for debugging
byte recIndex;
char serialrecbyte, strMonCmd[250];
// command list
const char monCmnds[] = "RBstop\0RBstart\0RBMM\0RBclearq\0RBLD";
const int monComndpositions[] = { 0, 7, 15, 20, 29 };
#define maxCommands 5
#define cmdRBstop 0
#define cmdRBstart 1
#define cmdRBMM 2
#define cmdRBclearq 3
#define cmdRBLD 4
static const int ledPin = 13;
const byte LF = 10;
const byte CR = 13;
char msgToSend[50];
int msgIndex = 0, msgcount = 0;
byte recbyte;
boolean sending;
long oldMillis, oldMicros;
// RockBlock
#define MAX_RBQLENGTH 100
#define MAX_MSG_LENGTH 200
#define MIN_MSG_LENGTH 20
//boolean useRockBlock = false, RBsending = false, RBpresent = false;
//int RBtransInterval = 60; // ****************************************
//int RBCheckInterval;
//int RBOutMessageCount; // *********************
//byte RBOutMessage[MAX_MSG_LENGTH], RBQueue[MAX_RBQLENGTH][MAX_MSG_LENGTH], RBMsgLengths[MAX_RBQLENGTH];
//int RBwriteIndex = 0, RBreadIndex = 0;
boolean distressMessage;
int DVIndex = 0, inCount = 0;
byte msgtype, DVMsgType;
int sigQuality;
int DVMessageNo;
uint8_t conaircoverindex;
int32_t maxval;
boolean sendConAirData = false;
#define MAX_RX_SIZE 255
#define MESSAGE_RECEIVED 0x04
#define SYS_DISTRESS 0x55
#define GPS_POS 0x60
#define MSG_RECEIVED 0x60
#define AC_ENG 0x31
#define MAX_RETRIES 10
int retries;
//boolean RBavailable = false;
byte RBInMessage[MAX_RX_SIZE];
size_t RBInBufferSize;
size_t txSize;
//boolean distressState = false;
//boolean cancelSending = false;
byte distressType;
byte DMRef[2];
// things for conair
#define GC_SERIAL Serial3
#define CP_I2C 0x11
#define CONAIR_SEND_VOLUME 0x10
#define CONAIR_SEND_ALL 0x11
#define CONAIR_FIELDS 11
#define CONAIR_DATA_BYTES 23
#define CONAIR_REC_BUFFER_SIZE 5
#define CONAIR_TIMEOUT 5000
#define CONAIR_MIN_HOPPER 80
#define CONAIR_MAX_HOPPER 800
#define CONAIR_RETRY_INTERVAL 50
const double conM = 0.09; // same as datavault defaults
const double conC = 45;
int iGals;
#define CR 13
byte conairMsgType, sendConairData = false, conairrecbyte, buffer[20];
byte conAirData[CONAIR_DATA_BYTES + 1];
int16_t rawData[CONAIR_FIELDS] = { 11,1052,2164,2165,1477,-1,-5,-4,-2,20,1237 };
unsigned long noOfChars, noOfMsgs;
boolean ConAirGatesOpen = false;
boolean gateControllerPresent = true;
int ConAirIndex;
const char conAirCoverLabels[][8] = { "200/0.5","200/1.0","200/2.0","200/3.0","200/MAX","267/0.5","267/1.0","267/2.0","267/3.0","267/MAX",
"400/0.5","400/1.0","400/2.0","400/3.0","400/4.0","400/5.0","400/MAX","800/0.5","800/1.0","800/2.0","800/3.0","800/4.0","800/5.0","800/6.0","800/MAX","CON-N/A" };
char strConAirCover[10];
// dc iridium stuff
#define iridiumSerial Serial1
// buffer
#define IRIDIUM_BUFFER_LENGTH 300
#define OUT_BUFFER_LENGTH 200
#define IN_BUFFER_LENGTH 300
char iridiumBuffer[IRIDIUM_BUFFER_LENGTH];
uint8_t iridiumOutBuffer[OUT_BUFFER_LENGTH];
uint8_t iridiumInBuffer[IN_BUFFER_LENGTH];
int iridiumBufferIndex, outboundMsgLength, inboundMsgLength;
// iridium states
byte modemState;
byte modemTimeOut;
byte sendingState;
byte signalStrength, signalStrengthCount;
#define MIN_SIGNAL_STRENGTH 2
#define SIGNAL_STRENGTH_INTERVAL 30
int SBDIXattempts = 1;
uint16_t SBDIXresponse[6];
#define MO_STATUS 0
#define MOMSN 1
#define MT_STATUS 2
#define MTMSN 3
#define MT_LENGTH 4
#define MT_QUEUED 5
boolean SBDIXResponseError, inBoundMessage, messageSent, signalStrengthAvailable, retrySBDIX;
boolean waitingForGoodSignal; // used to hold sending
boolean messageReadyToSend = false;
const struct {
byte WAITING = 0;
byte WAIT_FOR_AT = 1;
byte WAIT_FOR_ATE1 = 2;
byte WAIT_FOR_ATD0 = 3;
byte WAIT_FOR_ATK0 = 4;
byte WAIT_FOR_SBDWB_READY = 5;
byte WAIT_FOR_SBDWB_RECEIPT = 6;
byte WAIT_FOR_SBDIX = 7;
byte WAIT_FOR_SBDRB = 8;
byte WAIT_FOR_SBDD0 = 9;
byte WAIT_FOR_CSQ = 10;
byte WAIT_FOR_CSQ_RETRY = 11;
byte POWER_TOGGLE = 12;
byte WAIT_FOR_POWER_UP = 13;
byte WAIT_FOR_IMEI = 14;
} iridiumState;
// timeouts
const struct {
byte WAIT_FOR_AT = 10;
byte WAIT_FOR_SBDWD_READY = 5;
byte WAIT_FOR_SBDWB_RECEIPT = 5;
byte WAIT_FOR_SBDIX = 60;
byte WAIT_FOR_SBDRB = 10;
byte WAIT_FOR_SBDD0 = 10;
byte WAIT_FOR_CSQ = 60; // manual says up to 50
byte WAIT_FOR_CSQ_RETRY = 10; // seconds between retries when sending
byte FIRST_CSQ_TEST = 2;
byte WAIT_FOR_IMEI = 5;
} iridiumTimeOut;
// responses
const struct {
byte GOOD = 1;
byte SBDIX_PROTOCOL_ERROR = 2;
} iridiumResponse;
// sending state
const struct {
byte WAITING_FOR_MESSAGE = 1;
byte WAITING_FOR_GOOD_SIGNAL = 2;
byte SENDING = 3;
} iridiumSendingState;
// state
boolean modemAvailable = false;
// queue
#define MAX_MESSAGE_SIZE 250 // = 4 * 64
#define MAX_QUEUE_SIZE 20
uint8_t queueLength, msgQueue[MAX_QUEUE_SIZE][MAX_MESSAGE_SIZE + 5], queueWriteIndex, queueReadIndex, lastMessageWriteIndex;
// queue sizing
// msg bytes
// + 1 for msg length
// + 2 for rockair start bytes
// + 2 for rockair number
// message
uint8_t msgLength;
uint8_t msgContent[MAX_MESSAGE_SIZE];
// RockAir 400
//#define rockAirSerial Serial3 // *****************************************
// buffer
#define IN_BUFFER_LENGTH 300
char RockAirInBuffer[IN_BUFFER_LENGTH], V2Buffer[IN_BUFFER_LENGTH], SXBuffer[IN_BUFFER_LENGTH];
int RockAirBufferIndex, RockAirMsgRef, V2BufferIndex, V2MsgRef, SXBufferIndex, SXMsgRef;
float RockAirSpeed = 0.0, V2Speed = 0.0;;
// V2Track
//#define V2TrackSerial Serial1
// External GPS
int32_t encLat, encLon;
uint32_t encGPSTime, gpstime;
uint16_t gpsspeed, gpstrack, gpsaltitude;
uint8_t gpshdop, encGPSFixType, gpssats, gpspdop;
int hh, mm, ss, dd, yy, mth, secs;
uint16_t sentMsgRef, statusQueryRef;
int8_t statusRequestMsgIndex = -1;
#define TP_MESSAGE_TYPE 0x31
#define TP_MANUFACTURER 0x02
byte encBytes[300];
// RockAir states
const struct {
byte WAITING = 0;
byte WAITING_OK_FOR_BYTES = 1;
byte WAITING_FOR_REF_NO = 2;
byte WAITING_FOR_STATUS = 3;
} rockAirStates;
byte rockAirState = 0, emTimeOut = 0;
byte msgBytes[300];
#define KEEP_ALIVE_SECS 10
byte emKeepAlive = KEEP_ALIVE_SECS;
byte V2KeepAlive = KEEP_ALIVE_SECS;
int rockAirTickCount = 0;
// timeouts
const struct {
byte WAITING_OK_FOR_BYTES = 5;
byte WAITING_FOR_STATUS = 5;
} rockAirTimeOuts;
boolean measureResponse, lastMessageSent;
// distress mode
#define DISTRESS_ACTION_CODE 23;
byte DA[50], DC[50], LP[50], inMsgType, DAMsgRef[2];
const struct {
byte OFF = 0;
byte SENDING_DA = 1;
byte SENDING_DC = 2;
byte SENDING_LP = 3;
} distress;
byte distressState;
boolean distressMode = false, DASent = false, DCReceived = false;
// V2TrackStates
const struct {
byte READY_FOR_MSG = 0;
byte WAITING_GPS = 1;
byte WAITING_FOR_MSG_ID = 2;
byte WAITING_FOR_MSGSTATUS = 3;
byte WAITING_FOR_DISTRESS_ACK = 4;
byte WAITING_INBOUND_COUNT = 5;
byte WAITING_INBOUND_MSG_ID = 6;
byte WAITING_INBOUND_MSG_TEXT = 7;
byte WAITING_DISMISS = 8;
byte WAITING_STATE_ACK = 9;
} V2States;
#define QUEUE_CHECK_TIME 5
#define AC_STATE_SECS 10
byte V2State = 0, V2TimeOut = 0, queueCheckSecs, queueCheckIndex = 0, sendACStateSecs = AC_STATE_SECS;
char V2IDPrefix[20], V2Prefixes[5][20], V2InboundMsgId[20], V2InboundMessage[250];
#define V2_INBOUND_CHECK_INTERVAL 60
uint8_t V2InboundCheckSecs = V2_INBOUND_CHECK_INTERVAL;
#define MAX_V2_REQUESTS 5
uint8_t V2Requests = MAX_V2_REQUESTS;
uint64_t V2Prefix = 0;
char V2IDs[20][20];
boolean V2unknown = false, V2Ready = false, V2QEmpty = true, V2CancelAtStart = false;
uint8_t V2QueuedMessages = 0;
// timeouts
const struct {
byte WAITING_FOR_MSG_ID = 5;
byte WAITING_GPS = 5;
byte WAITING_FOR_MSGSTATUS = 5;
byte WAITING_FOR_DISTRESS_ACK = 5;
byte WAITING_INBOUND_COUNT = 5;
byte WAITING_INBOUND_MSG_ID = 5;
byte WAITING_INBOUND_MSG_TEXT = 5;
byte WAITING_DISMISS = 5;
byte WAITING_STATE_ACK = 5;
} V2TimeOuts;
// states
uint8_t aircraftStates;
uint startDelay = 0;
// spidertracks
//#define SXSerial Serial3
// sx states
const struct {
byte WAITING_FOR_BOOT = 0;
byte READY_FOR_MESSAGE = 1;
byte WAITING_FOR_MSG_ACK = 2;
byte WAITING_SOS_ACK = 3;
byte WAITING_SOS_ACTIVATE = 4;
} SXStates;
byte SXState, SXTimeout, SXProtocolVersion;
#define SX_DEFAULT_TIMEOUT 5
int SXQueueSize = 0, testCount = 0, SXTickCount = 0, csRetries = 0;
boolean SXReady = false, SXQEmpty, SXDebug = true, gpsNoFix = true;
// rtc clock to use epoch time
boolean RTCset = false;
time_t RTCTime;
char strSXFixType[10], strEpoch[20];
int dummySpeed = 0;
// for distance check
float fLat, fLon, lastLat, lastLon, fVal;
// handle a sx nak
boolean nacked = false;
boolean gotAnotherBoot = false;
// log output
boolean logOutput = true;