| 1 |
#ifndef arq_H |
| 2 |
#define arq_H |
| 3 |
|
| 4 |
// ---------------------------------------------------------------------------- |
| 5 |
// arq module arq.h |
| 6 |
// Copyright (c) 2007-2009, Dave Freese, W1HKJ |
| 7 |
// |
| 8 |
// |
| 9 |
// This file is part of flmsg. |
| 10 |
// |
| 11 |
// Fldigi is free software: you can redistribute it and/or modify |
| 12 |
// it under the terms of the GNU General Public License as published by |
| 13 |
// the Free Software Foundation, either version 3 of the License, or |
| 14 |
// (at your option) any later version. |
| 15 |
// |
| 16 |
// Fldigi is distributed in the hope that it will be useful, |
| 17 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
// GNU General Public License for more details. |
| 20 |
// |
| 21 |
// You should have received a copy of the GNU General Public License |
| 22 |
// along with flmsg. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
// ---------------------------------------------------------------------------- |
| 24 |
|
| 25 |
#include <string> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <iostream> |
| 29 |
#include <vector> |
| 30 |
#include <list> |
| 31 |
#include <cctype> |
| 32 |
|
| 33 |
#include "threads.h" |
| 34 |
#include "util.h" |
| 35 |
#include "debug.h" |
| 36 |
#include "crc16.h" |
| 37 |
|
| 38 |
#include <FL/Fl.H> |
| 39 |
|
| 40 |
#define arq_Version "arq 0.2" |
| 41 |
|
| 42 |
extern const char *ASCII[]; |
| 43 |
extern std::string noCR(std::string s); |
| 44 |
extern std::string noCTL(std::string s); |
| 45 |
|
| 46 |
// link layer spec for flmsg_arq |
| 47 |
// |
| 48 |
// generic Frame format: |
| 49 |
// \x01bMYCALL~URCALL~[info])12EF\x04 |
| 50 |
// | | | | | | | |
| 51 |
// | | | | | | +--ASCII <ARQ_EOF> (0x04) character |
| 52 |
// | | | | | +------checksum (4xAlphaNum) |
| 53 |
// | | | | +-----------Payload |
| 54 |
// | | | +-------------------Your callsign |
| 55 |
// | | +--------------------------My callsign |
| 56 |
// | +-----------------------------Block type |
| 57 |
// +-------------------------------ASCII <ARQ_SOF> (0x01) character |
| 58 |
// callsigns max of 12 characters each |
| 59 |
// payload size, 16, 32, 64, 128, or 256 characters |
| 60 |
// frame overhead = 7 |
| 61 |
|
| 62 |
class arq { |
| 63 |
public: |
| 64 |
//===================================================================== |
| 65 |
// Block Types |
| 66 |
#define CONREQ 'c' // Connection request |
| 67 |
#define CONACK 'C' // Connection acknowledge |
| 68 |
#define DISREQ 'd' // Disconnect request |
| 69 |
#define DISACK 'D' // Disconnect acknowledge |
| 70 |
#define POLL 'p' // Poll for status |
| 71 |
#define ACK 'A' // Frame OK |
| 72 |
#define NAK 'N' // Frame BAD |
| 73 |
#define DATA 'B' // Data block |
| 74 |
//===================================================================== |
| 75 |
#define ESC_CHR '~' |
| 76 |
#define ARQ_SOF "~1" // ESC_CHR 1 |
| 77 |
#define ARQ_EOF "~4" // ESC_CHR 4 |
| 78 |
#define ARQ_DLE ";" // '~' ARQ_DLE must be a single character |
| 79 |
//===================================================================== |
| 80 |
//ARQ defaults |
| 81 |
#define EXPONENT 7 // buffer_length = 2 ^ EXPONENT = 128 |
| 82 |
//===================================================================== |
| 83 |
//link timing defaults |
| 84 |
#define RETRIES 2 |
| 85 |
#define ARQLOOPTIME 100 // # msec for loop timing |
| 86 |
//===================================================================== |
| 87 |
//link states |
| 88 |
|
| 89 |
enum LINK_STATES { |
| 90 |
ARQ_DOWN = 0, |
| 91 |
ARQ_CONNECTING, |
| 92 |
ARQ_CONNECTED, |
| 93 |
ARQ_DISCONNECT, |
| 94 |
ARQ_DISCONNECTING |
| 95 |
}; |
| 96 |
|
| 97 |
//===================================================================== |
| 98 |
|
| 99 |
private: |
| 100 |
bool arqreset; |
| 101 |
bool sender; |
| 102 |
|
| 103 |
std::string MyCall; |
| 104 |
std::string UrCall; |
| 105 |
std::string arqfname; |
| 106 |
|
| 107 |
int blknbr; |
| 108 |
size_t xfrsize; |
| 109 |
|
| 110 |
std::string str_xfrsize; |
| 111 |
std::string str_blknbr; |
| 112 |
|
| 113 |
// queues // |
| 114 |
std::string TxTextQueue; |
| 115 |
std::string RxTextQueue; |
| 116 |
std::string RxFrameQueue; |
| 117 |
std::string rcvPayLoad; |
| 118 |
std::string xmttext; |
| 119 |
std::string xmtsent; |
| 120 |
std::string payload; |
| 121 |
std::string nomFrame; |
| 122 |
|
| 123 |
int buffer_length; |
| 124 |
int last_buffer_length; |
| 125 |
int maxheaders; |
| 126 |
int exponent; |
| 127 |
int lastblock; |
| 128 |
|
| 129 |
// status variables |
| 130 |
|
| 131 |
int retrytime; // time to wait before resending a Frame |
| 132 |
int retries; // number of times to retry sending Frame |
| 133 |
int timeout; // absolute time before declaring ARQ connection broken |
| 134 |
int loopcount; |
| 135 |
int Retries; |
| 136 |
|
| 137 |
Ccrc16 framecrc; |
| 138 |
|
| 139 |
int LinkState; // status of ARQ link |
| 140 |
|
| 141 |
bool arq_exit; |
| 142 |
bool arq_running; |
| 143 |
|
| 144 |
// Link quality for sending *** used for testing only !! *** |
| 145 |
// double sendquality; |
| 146 |
|
| 147 |
void reset(); |
| 148 |
void resetTx(); |
| 149 |
void resetRx(); |
| 150 |
int rtry(); |
| 151 |
|
| 152 |
void setbuffer_length(); |
| 153 |
|
| 154 |
void checkblocks(); |
| 155 |
std::string upcase(std::string s); |
| 156 |
|
| 157 |
std::string calls(); |
| 158 |
void connectFrame(); |
| 159 |
void conackFrame(); |
| 160 |
void disackFrame(); |
| 161 |
void ackFrame(); |
| 162 |
void nakFrame(); |
| 163 |
void dataFrame(); |
| 164 |
void resendFrame(); |
| 165 |
void pollFrame(); |
| 166 |
void disconnectFrame(); |
| 167 |
|
| 168 |
void addToTxQue(std::string s); |
| 169 |
|
| 170 |
bool rightCALLS(); |
| 171 |
|
| 172 |
void parseCONREQ(); |
| 173 |
void parseCONACK(); |
| 174 |
void parseDISREQ(); |
| 175 |
void parseDISACK(); |
| 176 |
void parsePOLL(); |
| 177 |
void parseDATA(); |
| 178 |
void parseACK(); |
| 179 |
void parseNAK(); |
| 180 |
|
| 181 |
void parseFrame(std::string txt); |
| 182 |
|
| 183 |
//---------------------------------------------------------------------- |
| 184 |
// external functions called by arq class must use Fl::wait(fn,...) |
| 185 |
// to isolate the UI thread from the arq thread |
| 186 |
//---------------------------------------------------------------------- |
| 187 |
|
| 188 |
std::string (*do_trx)(); |
| 189 |
int trx() { |
| 190 |
if (do_trx) { |
| 191 |
if (do_trx() == "TX") return 1; |
| 192 |
return 0; |
| 193 |
} |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
void (*do_sendfnc)(const std::string& s); |
| 198 |
void sendfnc(const std::string &s) { |
| 199 |
if (do_sendfnc != NULL) do_sendfnc(s); |
| 200 |
} |
| 201 |
|
| 202 |
bool (*do_getc1)(char &); |
| 203 |
bool getc1(char &c) { |
| 204 |
if (do_getc1 != NULL) return do_getc1(c); |
| 205 |
c = ' '; |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
void (*do_rcvfnc)(); |
| 210 |
void rcvfnc() { |
| 211 |
if (do_rcvfnc != NULL) do_rcvfnc(); |
| 212 |
} |
| 213 |
|
| 214 |
void (*do_printRX)(std::string s); |
| 215 |
void printRX(std::string s) { |
| 216 |
if (do_printRX != NULL) do_printRX(s); |
| 217 |
} |
| 218 |
|
| 219 |
void (*do_printTX)(std::string s); |
| 220 |
void printTX(std::string s) { |
| 221 |
if (do_printTX != NULL) do_printTX(s); |
| 222 |
} |
| 223 |
|
| 224 |
void (*do_printLOG)(std::string s1, std::string s2); |
| 225 |
void printLOG(std::string s1, std::string s2) { |
| 226 |
if (do_printLOG) do_printLOG(s1, s2); |
| 227 |
} |
| 228 |
|
| 229 |
void (*do_printTALK)(std::string s); |
| 230 |
void printTALK(std::string s) { |
| 231 |
if (do_printTALK != NULL) do_printTALK(s); |
| 232 |
} |
| 233 |
|
| 234 |
void (*do_abortfnc)(); |
| 235 |
void abortfnc() { |
| 236 |
if (do_abortfnc != NULL) do_abortfnc(); |
| 237 |
} |
| 238 |
|
| 239 |
void (*do_disconnectfnc)(); |
| 240 |
void disconnectfnc() { |
| 241 |
if (do_disconnectfnc != NULL) do_disconnectfnc(); |
| 242 |
} |
| 243 |
|
| 244 |
void (*do_rxUrCall)(std::string s); |
| 245 |
void rxUrCall(std::string s) { |
| 246 |
if (do_rxUrCall != NULL) do_rxUrCall(s); |
| 247 |
} |
| 248 |
|
| 249 |
void (*do_qualityfnc)(float pcnt); |
| 250 |
void qualityfnc(float pcnt) { |
| 251 |
if (do_qualityfnc != NULL) do_qualityfnc(pcnt); |
| 252 |
} |
| 253 |
|
| 254 |
void (*do_printSTATUS)(std::string s); |
| 255 |
void printSTATUS(std::string s) { |
| 256 |
if (do_printSTATUS != NULL) do_printSTATUS(s); |
| 257 |
} |
| 258 |
|
| 259 |
float (*do_transmit_time)(std::string s); |
| 260 |
float transmit_time(std::string s) { |
| 261 |
if (do_transmit_time) return do_transmit_time(s); |
| 262 |
else return 0; |
| 263 |
} |
| 264 |
|
| 265 |
bool (*do_OK_to_transmit)(); |
| 266 |
bool OK_to_transmit() { |
| 267 |
if (do_OK_to_transmit()) return do_OK_to_transmit(); |
| 268 |
else return false; |
| 269 |
} |
| 270 |
|
| 271 |
// execution loop functions |
| 272 |
void rcv_chars(); |
| 273 |
void exec_arqreset(); |
| 274 |
void sendchars(); |
| 275 |
|
| 276 |
public: |
| 277 |
arq(); |
| 278 |
~arq() { exit_arq(); } |
| 279 |
|
| 280 |
friend void *arqloop(void *); |
| 281 |
friend void sked_reset(void *); |
| 282 |
|
| 283 |
void start_arq(); |
| 284 |
void reset_arq(); |
| 285 |
void stop_arq(); |
| 286 |
void exit_arq(); |
| 287 |
|
| 288 |
std::string checksum(std::string &s); |
| 289 |
|
| 290 |
void myCall(std::string s) { |
| 291 |
MyCall = upcase(s); |
| 292 |
for (size_t n = 0; n < MyCall.length(); n++) |
| 293 |
if (MyCall[n] == ';') MyCall[n] = '-'; |
| 294 |
} |
| 295 |
std::string myCall() { return MyCall;} |
| 296 |
|
| 297 |
void urCall(std::string s) { |
| 298 |
UrCall = s; |
| 299 |
for (size_t n = 0; n < UrCall.length(); n++) |
| 300 |
if (UrCall[n] == ';') UrCall[n] = '-'; |
| 301 |
} |
| 302 |
|
| 303 |
std::string urCall() { return UrCall;} |
| 304 |
|
| 305 |
void escape(bool, std::string &); |
| 306 |
|
| 307 |
void setSendFunc( void (*f)(const std::string& s)) { do_sendfnc = f;} |
| 308 |
void setGetCFunc( bool (*f)(char &)) { do_getc1 = f;} |
| 309 |
void setRcvFunc( void (*f)()) { do_rcvfnc = f;} |
| 310 |
|
| 311 |
void setPrintRX( void (*f)(std::string s)) { do_printRX = f;} |
| 312 |
void setPrintTX( void (*f)(std::string s)) { do_printTX = f;} |
| 313 |
void setPrintTALK (void (*f)(std::string s)) {do_printTALK = f;} |
| 314 |
void setPrintLOG (void (*f)(std::string, std::string)){do_printLOG = f;} |
| 315 |
void setPrintSTATUS (void (*f)(std::string s)) { do_printSTATUS = f;} |
| 316 |
void setTRX(std::string (*f)()) { do_trx = f;} |
| 317 |
void setTransmitTime(float (*f)(std::string s)) { do_transmit_time = f; } |
| 318 |
void setOK_to_transmit(bool (*f)()) { do_OK_to_transmit = f; } |
| 319 |
|
| 320 |
void setExponent( int exp ) { exponent = exp; setbuffer_length(); } |
| 321 |
int getExponent() { return (int) exponent;} |
| 322 |
void setRetries ( int rtries ) { |
| 323 |
Retries = rtries; } |
| 324 |
int getRetries() { return (int) Retries; } |
| 325 |
void setrxUrCall( void (*f)(std::string s)) { do_rxUrCall = f;} |
| 326 |
void setQualityValue( void (*f)(float)) { do_qualityfnc = f;} |
| 327 |
void setDisconnected( void (*f)()) { do_disconnectfnc = f;}; |
| 328 |
|
| 329 |
void connect(); |
| 330 |
|
| 331 |
void sendtext(std::string call, std::string txt); |
| 332 |
|
| 333 |
bool connected() { return (LinkState == ARQ_CONNECTED); } |
| 334 |
void disconnect(); |
| 335 |
|
| 336 |
int state() { return LinkState;} |
| 337 |
|
| 338 |
double quality() { |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
}; |
| 343 |
|
| 344 |
#endif |
| 345 |
|