Yate
yatemodem.h
1
21
22#ifndef __YATEMODEM_H
23#define __YATEMODEM_H
24
25#include <yateclass.h>
26
27#ifdef _WINDOWS
28
29#ifdef LIBYMODEM_EXPORTS
30#define YMODEM_API __declspec(dllexport)
31#else
32#ifndef LIBYMODEM_STATIC
33#define YMODEM_API __declspec(dllimport)
34#endif
35#endif
36
37#endif /* _WINDOWS */
38
39#ifndef YMODEM_API
40#define YMODEM_API
41#endif
42
43
47namespace TelEngine {
48
49class BitAccumulator; // 1-byte length bit accumulator
50class FSKModem; // Frequency Shift Keying modulator/demodulator
51class UART; // UART receiver/transmitter
52class UARTBuffer; // A byte accumulator used by an UART
53class ETSIModem; // An analog signal processor as defined by ETSI
54// Internal forward declarations
55class BitBuffer; // Used to accumulate all bits to be printed to output
56class FSKFilter; // The internal signal filter
57
58
63class YMODEM_API BitAccumulator
64{
65public:
70 inline BitAccumulator(unsigned char dataBits)
71 : m_crtByte(0), m_crtPos(0), m_dataBits(dataBits), m_oddParity(false)
72 {}
73
78 inline unsigned char dataBits() const
79 { return m_dataBits; }
80
85 inline void dataBits(unsigned char value) {
86 m_dataBits = value;
87 reset();
88 }
89
95 inline unsigned char reset(bool* oddParity = 0) {
96 unsigned char tmp = m_crtByte;
97 m_crtByte = m_crtPos = 0;
98 if (oddParity)
99 *oddParity = m_oddParity;
100 m_oddParity = false;
101 return tmp;
102 }
103
110 inline unsigned int accumulate(bool bit, bool* oddParity = 0) {
111 if (bit) {
112 m_crtByte |= (1 << m_crtPos);
113 m_oddParity = !m_oddParity;
114 }
115 m_crtPos++;
116 if (m_crtPos != m_dataBits)
117 return 0xffff;
118 return reset(oddParity);
119 }
120
121private:
122 unsigned char m_crtByte; // Current partial byte
123 unsigned char m_crtPos; // Current free bit position
124 unsigned char m_dataBits; // The length of a data byte (interval: 1..8)
125 bool m_oddParity; // The parity of the current byte value (true: odd)
126};
127
128
134class YMODEM_API FSKModem
135{
136public:
140 enum Type {
141 ETSI = 0, // ETSI caller id signal: MARK:1200 SPACE:2200 BAUDRATE:1200
142 // SAMPLERATE:8000 SAMPLES/BIT:7 STOPBITS:1 PARITY:NONE
143 TypeCount = 1
144 // NOTE: Don't change these values: they are used as array indexes
145 };
146
152 FSKModem(const NamedList& params, UART* uart);
153
158
164 inline bool terminated() const
165 { return m_terminated; }
166
171 inline int type() const
172 { return m_type; }
173
177 void reset();
178
184 bool demodulate(const DataBlock& data);
185
194 void modulate(DataBlock& dest, const DataBlock& data);
195
202 static inline void addRaw(DataBlock& dest, void* buf, unsigned int len) {
203 DataBlock tmp(buf,len,false);
204 dest += tmp;
205 tmp.clear(false);
206 }
207
212
213private:
214 int m_type; // Modem type
215 bool m_terminated; // Terminated flag (need reset if true)
216 FSKFilter* m_filter; // Internal filter used to demodulate received data
217 UART* m_uart; // The UART using this modem's services
218 DataBlock m_buffer; // Partial input buffer when used to demodulate or modulate data
219 BitBuffer* m_bits; // Bit buffer used when debugging
220};
221
222
227class YMODEM_API UART : public DebugEnabler
228{
229public:
233 enum State {
234 Idle, // Not started
235 BitStart, // Waiting for start bit (SPACE)
236 BitData, // Accumulate data bits
237 BitParity, // Waiting for parity bit(s)
238 BitStop, // Waiting for stop bit (MARK)
239 UARTError, // Error
240 };
241
245 enum Error {
246 EFraming, // Frame error: invalid stop bit(s)
247 EParity, // Parity error
248 EChksum, // Message checksum error
249 EInvalidData, // Invalid (inconsistent) data
250 EUnknown, // Unknown error
251 EStopped, // Aborted by descendants
252 ENone
253 };
254
261 UART(State state, const NamedList& params, const char* name = 0);
262
266 virtual ~UART()
267 {}
268
273 inline State state() const
274 { return m_state; }
275
280 inline Error error() const
281 { return m_error; }
282
287 inline int modemType() const
288 { return m_modem.type(); }
289
294 inline const BitAccumulator& accumulator() const
295 { return m_accumulator; }
296
301 virtual void reset(State newState = Idle);
302
308 inline bool demodulate(const DataBlock& data)
309 { return m_modem.demodulate(data); }
310
317 inline bool modulate(DataBlock& dest, NamedList& params) {
318 DataBlock data;
319 if (!createMsg(params,data))
320 return false;
321 m_modem.modulate(dest,data);
322 return true;
323 }
324
330 inline void modulate(DataBlock& dest, const DataBlock& src)
331 { m_modem.modulate(dest,src); }
332
338 bool recvBit(bool value);
339
345 virtual bool recvByte(unsigned char data)
346 { return false; }
347
352 virtual bool fskStarted()
353 { return true; }
354
359
360protected:
366 virtual int idleRecvByte(unsigned char data)
367 { return false; }
368
375 virtual bool createMsg(NamedList& params, DataBlock& data)
376 { return false; }
377
383 bool error(Error e);
384
385private:
386 // Change this UART's state
387 void changeState(State newState);
388
389 FSKModem m_modem; // The modem used by this UART
390 State m_state; // The state of this UART
391 Error m_error; // The error type if state is error
392 int m_parity; // Used parity: 0=none, -1=odd, 1=even
393 bool m_expectedParity; // The expected value of the parity bit if used
394 BitAccumulator m_accumulator; // The data bits accumulator
395};
396
397
402class YMODEM_API UARTBuffer
403{
404public:
409 inline UARTBuffer(UART* client)
410 : m_client(client)
411 { reset(); }
412
417 inline const DataBlock& buffer() const
418 { return m_buffer; }
419
424 inline unsigned int free() const
425 { return m_free; }
426
431 inline void reset(unsigned int len = 0) {
432 m_buffer.clear();
433 m_crtIdx = m_free = 0;
434 if (len) {
435 m_buffer.assign(0,len);
436 m_free = len;
437 }
438 }
439
445 inline bool accumulate(unsigned char value) {
446 if (m_free) {
447 ((unsigned char*)m_buffer.data())[m_crtIdx++] = value;
448 m_free--;
449 return true;
450 }
451 Debug(m_client,DebugNote,"Buffer overflow");
452 return false;
453 }
454
455private:
456 UART* m_client; // The client
457 unsigned int m_crtIdx; // Current index n buffer
458 unsigned int m_free; // Free buffer length
459 DataBlock m_buffer; // The buffer
460};
461
462
468class YMODEM_API ETSIModem : public UART
469{
470public:
474 enum State {
475 StateError, // Error encountered: need reset
476 WaitFSKStart, // Waiting for data start pattern
477 WaitMark, // Waiting for mark pattern
478 WaitMsg, // Wait a message
479 WaitMsgLen, // Received message: wait length
480 WaitParam, // Wait a parameter
481 WaitParamLen, // Received parameter: wait length
482 WaitData, // Received parameter length: wait data
483 WaitChksum, // Wait checksum
484 };
485
489 enum MsgType {
490 MsgCallSetup = 0x80, // Call setup
491 MsgMWI = 0x82, // Message waiting indicator
492 MsgCharge = 0x86, // Advise of charge
493 MsgSMS = 0x89, // Short message service
494 };
495
499 enum MsgParam {
500 DateTime = 0x01, // 8 Date and Time
501 CallerId = 0x02, // max. 20 Calling Line Identity
502 CalledId = 0x03, // max. 20 Called Line Identity
503 CallerIdReason = 0x04, // 1 Reason for Absence of Calling Line Identity
504 CallerName = 0x07, // max. 50 Calling Party Name
505 CallerNameReason = 0x08, // 1 Reason for absence of Calling Party Name
506 VisualIndicator = 0x0B, // 1 Visual Indicator
507 MessageId = 0x0D, // 3 Message Identification
508 LastMsgCLI = 0x0E, // max. 20 Last Message CLI
509 CompDateTime = 0x0F, // 8 or 10 Complementary Date and Time
510 CompCallerId = 0x10, // max. 20 Complementary Calling Line Identity
511 CallType = 0x11, // 1 Call type
512 FirstCalledId = 0x12, // max. 20 First Called Line Identity
513 MWICount = 0x13, // 1 Number of Messages
514 FwdCallType = 0x15, // 1 Type of Forwarded call
515 CallerType = 0x16, // 1 Type of Calling user
516 RedirNumber = 0x1A, // max. 20 Redirecting Number
517 Charge = 0x20, // 14 Charge
518 AdditionalCharge = 0x21, // 14 Additional Charge
519 Duration = 0x23, // 6 Duration of the Call
520 NetworkID = 0x30, // max. 20 Network Provider Identity
521 CarrierId = 0x31, // max. 20 Carrier Identity
522 SelectFunction = 0x40, // 2-21 Selection of Terminal Function
523 Display = 0x50, // max. 253 Display Information
524 ServiceInfo = 0x55, // 1 Service Information
525 Extension = 0xE0, // 10 Extension for network operator use
526 Unknown
527 };
528
534 ETSIModem(const NamedList& params, const char* name = 0);
535
539 virtual ~ETSIModem();
540
544 virtual void reset();
545
551 virtual bool recvByte(unsigned char data);
552
556 static TokenDict s_msg[];
557
562
563protected:
569 virtual int idleRecvByte(unsigned char data);
570
577 virtual bool recvParams(MsgType msg, const NamedList& params)
578 { return false; }
579
586 virtual bool decode(MsgType msg, const DataBlock& buffer);
587
595 virtual bool createMsg(NamedList& params, DataBlock& data);
596
597private:
598 // Change decoder's state
599 void changeState(State newState);
600
601 UARTBuffer m_buffer; // The buffer used to accumulate messages
602 State m_state; // Decoder state
603 unsigned char m_waitSeizureCount; // Expected number of channel seizure bytes in a row
604 unsigned char m_crtSeizureCount; // Current number of channel seizure bytes in a row
605 unsigned char m_crtMsg; // Current message id
606 unsigned char m_crtParamLen; // Current receiving parameter length
607 unsigned int m_chksum; // Current calculated checksum
608};
609
610}
611
612#endif /* __YATEMODEM_H */
613
614/* vi: set ts=8 sw=4 sts=4 noet: */
A 1-byte length bit accumulator.
Definition yatemodem.h:64
unsigned int accumulate(bool bit, bool *oddParity=0)
Definition yatemodem.h:110
void dataBits(unsigned char value)
Definition yatemodem.h:85
BitAccumulator(unsigned char dataBits)
Definition yatemodem.h:70
unsigned char dataBits() const
Definition yatemodem.h:78
unsigned char reset(bool *oddParity=0)
Definition yatemodem.h:95
A class that holds just a block of raw data.
Definition yateclass.h:4237
void clear(bool deleteData=true)
DebugEnabler(int level=TelEngine::debugLevel(), bool enabled=true)
Definition yateclass.h:319
MsgParam
Definition yatemodem.h:499
virtual bool recvByte(unsigned char data)
virtual ~ETSIModem()
MsgType
Definition yatemodem.h:489
virtual bool recvParams(MsgType msg, const NamedList &params)
Definition yatemodem.h:577
virtual bool decode(MsgType msg, const DataBlock &buffer)
static TokenDict s_msg[]
Definition yatemodem.h:556
State
Definition yatemodem.h:474
virtual bool createMsg(NamedList &params, DataBlock &data)
virtual void reset()
virtual int idleRecvByte(unsigned char data)
ETSIModem(const NamedList &params, const char *name=0)
static TokenDict s_msgParams[]
Definition yatemodem.h:561
A Frequency Shift Keying modem.
Definition yatemodem.h:135
Type
Definition yatemodem.h:140
static void addRaw(DataBlock &dest, void *buf, unsigned int len)
Definition yatemodem.h:202
bool demodulate(const DataBlock &data)
bool terminated() const
Definition yatemodem.h:164
int type() const
Definition yatemodem.h:171
void reset()
FSKModem(const NamedList &params, UART *uart)
void modulate(DataBlock &dest, const DataBlock &data)
static TokenDict s_typeName[]
Definition yatemodem.h:211
A named string container class.
Definition yateclass.h:5016
A fixed length byte accumulator used by an UART.
Definition yatemodem.h:403
const DataBlock & buffer() const
Definition yatemodem.h:417
unsigned int free() const
Definition yatemodem.h:424
UARTBuffer(UART *client)
Definition yatemodem.h:409
bool accumulate(unsigned char value)
Definition yatemodem.h:445
void reset(unsigned int len=0)
Definition yatemodem.h:431
An UART receiver/transmitter.
Definition yatemodem.h:228
virtual bool recvByte(unsigned char data)
Definition yatemodem.h:345
Error
Definition yatemodem.h:245
virtual void reset(State newState=Idle)
Error error() const
Definition yatemodem.h:280
bool demodulate(const DataBlock &data)
Definition yatemodem.h:308
State
Definition yatemodem.h:233
bool error(Error e)
virtual bool fskStarted()
Definition yatemodem.h:352
static TokenDict s_errors[]
Definition yatemodem.h:358
virtual bool createMsg(NamedList &params, DataBlock &data)
Definition yatemodem.h:375
const BitAccumulator & accumulator() const
Definition yatemodem.h:294
virtual int idleRecvByte(unsigned char data)
Definition yatemodem.h:366
virtual ~UART()
Definition yatemodem.h:266
UART(State state, const NamedList &params, const char *name=0)
bool recvBit(bool value)
int modemType() const
Definition yatemodem.h:287
bool modulate(DataBlock &dest, NamedList &params)
Definition yatemodem.h:317
State state() const
Definition yatemodem.h:273
void modulate(DataBlock &dest, const DataBlock &src)
Definition yatemodem.h:330
Definition yatemime.h:34
Definition yateclass.h:848