00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __YATEMODEM_H
00026 #define __YATEMODEM_H
00027
00028 #include <yateclass.h>
00029
00030 #ifdef _WINDOWS
00031
00032 #ifdef LIBYMODEM_EXPORTS
00033 #define YMODEM_API __declspec(dllexport)
00034 #else
00035 #ifndef LIBYMODEM_STATIC
00036 #define YMODEM_API __declspec(dllimport)
00037 #endif
00038 #endif
00039
00040 #endif
00041
00042 #ifndef YMODEM_API
00043 #define YMODEM_API
00044 #endif
00045
00046
00050 namespace TelEngine {
00051
00052 class BitAccumulator;
00053 class FSKModem;
00054 class UART;
00055 class UARTBuffer;
00056 class ETSIModem;
00057
00058 class BitBuffer;
00059 class FSKFilter;
00060
00061
00066 class YMODEM_API BitAccumulator
00067 {
00068 public:
00073 inline BitAccumulator(unsigned char dataBits)
00074 : m_crtByte(0), m_crtPos(0), m_dataBits(dataBits), m_oddParity(false)
00075 {}
00076
00081 inline unsigned char dataBits() const
00082 { return m_dataBits; }
00083
00088 inline void dataBits(unsigned char value) {
00089 m_dataBits = value;
00090 reset();
00091 }
00092
00098 inline unsigned char reset(bool* oddParity = 0) {
00099 unsigned char tmp = m_crtByte;
00100 m_crtByte = m_crtPos = 0;
00101 if (oddParity)
00102 *oddParity = m_oddParity;
00103 m_oddParity = false;
00104 return tmp;
00105 }
00106
00113 inline unsigned int accumulate(bool bit, bool* oddParity = 0) {
00114 if (bit) {
00115 m_crtByte |= (1 << m_crtPos);
00116 m_oddParity = !m_oddParity;
00117 }
00118 m_crtPos++;
00119 if (m_crtPos != m_dataBits)
00120 return 0xffff;
00121 return reset(oddParity);
00122 }
00123
00124 private:
00125 unsigned char m_crtByte;
00126 unsigned char m_crtPos;
00127 unsigned char m_dataBits;
00128 bool m_oddParity;
00129 };
00130
00131
00137 class YMODEM_API FSKModem
00138 {
00139 public:
00143 enum Type {
00144 ETSI = 0,
00145
00146 TypeCount = 1
00147
00148 };
00149
00155 FSKModem(const NamedList& params, UART* uart);
00156
00160 ~FSKModem();
00161
00167 inline bool terminated() const
00168 { return m_terminated; }
00169
00174 inline int type() const
00175 { return m_type; }
00176
00180 void reset();
00181
00187 bool demodulate(const DataBlock& data);
00188
00197 void modulate(DataBlock& dest, const DataBlock& data);
00198
00205 static inline void addRaw(DataBlock& dest, void* buf, unsigned int len) {
00206 DataBlock tmp(buf,len,false);
00207 dest += tmp;
00208 tmp.clear(false);
00209 }
00210
00214 static TokenDict s_typeName[];
00215
00216 private:
00217 int m_type;
00218 bool m_terminated;
00219 FSKFilter* m_filter;
00220 UART* m_uart;
00221 DataBlock m_buffer;
00222 BitBuffer* m_bits;
00223 };
00224
00225
00230 class YMODEM_API UART : public DebugEnabler
00231 {
00232 public:
00236 enum State {
00237 Idle,
00238 BitStart,
00239 BitData,
00240 BitParity,
00241 BitStop,
00242 UARTError,
00243 };
00244
00248 enum Error {
00249 EFraming,
00250 EParity,
00251 EChksum,
00252 EInvalidData,
00253 EUnknown,
00254 EStopped,
00255 ENone
00256 };
00257
00264 UART(State state, const NamedList& params, const char* name = 0);
00265
00269 virtual ~UART()
00270 {}
00271
00276 inline State state() const
00277 { return m_state; }
00278
00283 inline Error error() const
00284 { return m_error; }
00285
00290 inline int modemType() const
00291 { return m_modem.type(); }
00292
00297 inline const BitAccumulator& accumulator() const
00298 { return m_accumulator; }
00299
00304 virtual void reset(State newState = Idle);
00305
00311 inline bool demodulate(const DataBlock& data)
00312 { return m_modem.demodulate(data); }
00313
00320 inline bool modulate(DataBlock& dest, NamedList& params) {
00321 DataBlock data;
00322 if (!createMsg(params,data))
00323 return false;
00324 m_modem.modulate(dest,data);
00325 return true;
00326 }
00327
00333 inline void modulate(DataBlock& dest, const DataBlock& src)
00334 { m_modem.modulate(dest,src); }
00335
00341 bool recvBit(bool value);
00342
00348 virtual bool recvByte(unsigned char data)
00349 { return false; }
00350
00355 virtual bool fskStarted()
00356 { return true; }
00357
00361 static TokenDict s_errors[];
00362
00363 protected:
00369 virtual int idleRecvByte(unsigned char data)
00370 { return false; }
00371
00378 virtual bool createMsg(NamedList& params, DataBlock& data)
00379 { return false; }
00380
00386 bool error(Error e);
00387
00388 private:
00389
00390 void changeState(State newState);
00391
00392 FSKModem m_modem;
00393 State m_state;
00394 Error m_error;
00395 int m_parity;
00396 bool m_expectedParity;
00397 BitAccumulator m_accumulator;
00398 };
00399
00400
00405 class YMODEM_API UARTBuffer
00406 {
00407 public:
00412 inline UARTBuffer(UART* client)
00413 : m_client(client)
00414 { reset(); }
00415
00420 inline const DataBlock& buffer() const
00421 { return m_buffer; }
00422
00427 inline unsigned int free() const
00428 { return m_free; }
00429
00434 inline void reset(unsigned int len = 0) {
00435 m_buffer.clear();
00436 m_crtIdx = m_free = 0;
00437 if (len) {
00438 m_buffer.assign(0,len);
00439 m_free = len;
00440 }
00441 }
00442
00448 inline bool accumulate(unsigned char value) {
00449 if (m_free) {
00450 ((unsigned char*)m_buffer.data())[m_crtIdx++] = value;
00451 m_free--;
00452 return true;
00453 }
00454 Debug(m_client,DebugNote,"Buffer overflow");
00455 return false;
00456 }
00457
00458 private:
00459 UART* m_client;
00460 unsigned int m_crtIdx;
00461 unsigned int m_free;
00462 DataBlock m_buffer;
00463 };
00464
00465
00471 class YMODEM_API ETSIModem : public UART
00472 {
00473 public:
00477 enum State {
00478 StateError,
00479 WaitFSKStart,
00480 WaitMark,
00481 WaitMsg,
00482 WaitMsgLen,
00483 WaitParam,
00484 WaitParamLen,
00485 WaitData,
00486 WaitChksum,
00487 };
00488
00492 enum MsgType {
00493 MsgCallSetup = 0x80,
00494 MsgMWI = 0x82,
00495 MsgCharge = 0x86,
00496 MsgSMS = 0x89,
00497 };
00498
00502 enum MsgParam {
00503 DateTime = 0x01,
00504 CallerId = 0x02,
00505 CalledId = 0x03,
00506 CallerIdReason = 0x04,
00507 CallerName = 0x07,
00508 CallerNameReason = 0x08,
00509 VisualIndicator = 0x0B,
00510 MessageId = 0x0D,
00511 LastMsgCLI = 0x0E,
00512 CompDateTime = 0x0F,
00513 CompCallerId = 0x10,
00514 CallType = 0x11,
00515 FirstCalledId = 0x12,
00516 MWICount = 0x13,
00517 FwdCallType = 0x15,
00518 CallerType = 0x16,
00519 RedirNumber = 0x1A,
00520 Charge = 0x20,
00521 AdditionalCharge = 0x21,
00522 Duration = 0x23,
00523 NetworkID = 0x30,
00524 CarrierId = 0x31,
00525 SelectFunction = 0x40,
00526 Display = 0x50,
00527 ServiceInfo = 0x55,
00528 Extension = 0xE0,
00529 Unknown
00530 };
00531
00537 ETSIModem(const NamedList& params, const char* name = 0);
00538
00542 virtual ~ETSIModem();
00543
00547 virtual void reset();
00548
00554 virtual bool recvByte(unsigned char data);
00555
00559 static TokenDict s_msg[];
00560
00564 static TokenDict s_msgParams[];
00565
00566 protected:
00572 virtual int idleRecvByte(unsigned char data);
00573
00580 virtual bool recvParams(MsgType msg, const NamedList& params)
00581 { return false; }
00582
00589 virtual bool decode(MsgType msg, const DataBlock& buffer);
00590
00598 virtual bool createMsg(NamedList& params, DataBlock& data);
00599
00600 private:
00601
00602 void changeState(State newState);
00603
00604 UARTBuffer m_buffer;
00605 State m_state;
00606 unsigned char m_waitSeizureCount;
00607 unsigned char m_crtSeizureCount;
00608 unsigned char m_crtMsg;
00609 unsigned char m_crtParamLen;
00610 unsigned int m_chksum;
00611 };
00612
00613 }
00614
00615 #endif
00616
00617