OPAL  Version 3.18.8
dahdi_ep.h
Go to the documentation of this file.
1 /*
2  * dahdi_ep.h
3  *
4  * DAHDI Line Interface Device EndPoint
5  *
6  * Open Phone Abstraction Library
7  *
8  * Copyright (C) 2011 Post Increment
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Opal Library
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  */
27 
28 #ifndef OPAL_LIDS_DAHDI_EP_H
29 #define OPAL_LIDS_DAHDI_EP_H
30 
31 #ifdef P_USE_PRAGMA
32 #pragma interface
33 #endif
34 
35 #include <opal_config.h>
36 
37 #include <ptlib.h>
38 #include <ptlib/mutex.h>
39 #include <ptclib/inetmail.h>
40 #include <ptclib/dtmf.h>
41 
42 #include <lids/lid.h>
43 #include <codec/g711codec.h>
44 
45 #include <dahdi/user.h>
46 
47 #include <unistd.h>
48 #include <sys/ioctl.h>
49 #include <poll.h>
50 #include <signal.h>
51 
52 #include <fcntl.h>
53 #include <errno.h>
54 #include <string.h>
55 #include <stdlib.h>
56 
57 #include <iostream>
58 #include <vector>
59 #include <queue>
60 using namespace std;
61 
63 
65 {
67 
68  public:
69  static const char * DeviceName;
70 
72 
73  // inherited from OpalLineInterfaceDevice
74 
75  // these can be called before Open
76  virtual PString GetDeviceType() const { return "dahdi"; }
77  virtual PString GetDeviceName() const { return DeviceName; }
78  virtual PStringArray GetAllNames() const { PStringArray l; l.AppendString(DeviceName); return l; }
79  virtual PString GetDescription() const { return "DAHDI"; }
80 
82  {
84  l += OpalPCM16;
85  return l;
86  }
87 
88  // open the device
89  virtual bool Open(const PString & device);
90  virtual bool Close();
91 
92  // these can be called after Open
93  virtual unsigned GetLineCount() const
94  { return m_channelInfoList.size(); }
95 
96  virtual bool IsLineTerminal(unsigned line)
97  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->IsFXS(); }
98 
99  virtual bool IsLineOffHook(unsigned line)
100  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->IsOffHook(); }
101 
102  virtual bool SetLineOffHook(unsigned line, bool newState = true)
103  { if (!IsValidLine(line) || IsLineTerminal(line)) return false; return m_channelInfoList[line]->SetOffHook(newState); }
104 
105  virtual bool PlayTone(unsigned line, CallProgressTones tone) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->PlayTone(tone); }
106 
107  virtual bool IsTonePlaying(unsigned line) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->IsTonePlaying(); }
108 
109  virtual bool StopTone(unsigned line) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->StopTone(); }
110 
111  virtual char ReadDTMF(unsigned line) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->ReadDTMF(); }
112 
113  virtual PINDEX GetReadFrameSize(unsigned line) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->GetReadFrameSize(); }
114 
115  virtual PINDEX GetWriteFrameSize(unsigned line) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->GetWriteFrameSize(); }
116 
117  virtual bool SetReadFrameSize(unsigned line, PINDEX frameSize) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->SetReadFrameSize(frameSize); }
118  virtual bool SetWriteFrameSize(unsigned line, PINDEX frameSize) { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->SetWriteFrameSize(frameSize); }
119 
120  virtual bool SetReadFormat(unsigned line, const OpalMediaFormat & mediaFormat);
121  virtual bool SetWriteFormat(unsigned line, const OpalMediaFormat & mediaFormat);
122 
123  virtual bool StopReading(unsigned line);
124  virtual bool StopWriting(unsigned line);
125 
126  virtual OpalMediaFormat GetReadFormat(unsigned line)
127  { return OpalG711ALaw; }
128 
129  virtual OpalMediaFormat GetWriteFormat(unsigned line)
130  { return OpalG711ALaw; }
131 
132  virtual bool EnableAudio(unsigned line, bool enable = true)
133  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->EnableAudio(enable); }
134 
135  virtual bool ReadFrame(unsigned line, void * buf, PINDEX & count)
136  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->ReadFrame(buf, count); }
137 
138  virtual bool WriteFrame(unsigned line, const void * buf, PINDEX count, PINDEX & written)
139  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->WriteFrame(buf, count, written); }
140 
141  virtual bool SetRecordVolume(unsigned line, unsigned volume)
142  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->SetRecordVolume(volume); }
143 
144  virtual bool SetPlayVolume(unsigned line, unsigned volume)
145  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->SetPlayVolume(volume); }
146 
147  virtual bool GetRecordVolume(unsigned line, unsigned & volume)
148  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->GetRecordVolume(volume); }
149 
150  virtual bool GetPlayVolume(unsigned line, unsigned & volume)
151  { if (!IsValidLine(line)) return false; return m_channelInfoList[line]->GetPlayVolume(volume); }
152 
153  virtual bool IsAudioEnabled(unsigned line) const { return false; return m_channelInfoList[line]->IsAudioEnabled(); }
154 
155  virtual bool IsValidLine(unsigned line) const { return line < (unsigned)m_channelInfoList.size(); }
156 
157  struct ChannelInfo : public PObject {
158 
159  ChannelInfo(dahdi_params & parms);
160  virtual ~ChannelInfo();
161 
162  virtual bool Open(int samplesPerBlock);
163  virtual bool Close();
164  void ThreadMain();
165 
166  virtual bool IsFXS() { return false; }
167  virtual bool IsOffHook() { return false; }
168  virtual bool IsAudioEnabled() const { return m_audioEnable; }
169  virtual bool IsMediaRunning() const { return m_mediaStarted; }
170 
171  virtual bool IsTonePlaying();
172  virtual bool PlayTone(CallProgressTones tone);
173  virtual bool StopTone();
174 
175  virtual bool EnableAudio(bool enable);
176  virtual bool SetOffHook(bool newState) { return false; }
177 
178  virtual PINDEX GetReadFrameSize() { return m_samplesPerBlock*2; }
179  virtual PINDEX GetWriteFrameSize() { return m_samplesPerBlock*2; }
180  virtual bool SetReadFrameSize(PINDEX frameSize) { return frameSize == m_samplesPerBlock*2; }
181  virtual bool SetWriteFrameSize(PINDEX frameSize) { return frameSize == m_samplesPerBlock*2; }
182 
183  virtual bool ReadFrame(void * buf, PINDEX & count);
184  virtual bool WriteFrame(const void * buf, PINDEX count, PINDEX & written);
185  virtual bool InternalReadFrame(void * buf);
186 
187  virtual bool SetReadFormat(const OpalMediaFormat & mediaFormat);
188  virtual bool SetWriteFormat(const OpalMediaFormat & mediaFormat);
189 
190  virtual bool StopReading();
191  virtual bool StopWriting();
192 
193  virtual bool StartMedia();
194  virtual bool StopMedia();
195 
196  virtual char ReadDTMF();
197 
198  virtual bool LookForEvent();
199  virtual bool DetectTones(void * buffer, int len);
200 
201  virtual bool SetRecordVolume(unsigned volume)
202  { /* m_readVol = volume; */return true; }
203 
204  virtual bool SetPlayVolume(unsigned volume)
205  { /* m_writeVol = volume; */ return true; }
206 
207  virtual bool GetRecordVolume(unsigned & volume)
208  { volume = m_readVol; return true; }
209 
210  virtual bool GetPlayVolume(unsigned & volume)
211  { volume = m_writeVol; return true; }
212 
213  virtual void OnHook() { }
214  virtual void OffHook() { }
215 
216  virtual void Flush();
217 
218  short DecodeSample(BYTE sample);
219  BYTE EncodeSample(short sample);
220 
224 
226  PDTMFDecoder m_dtmfDecoder;
227 
228  PMutex m_mutex;
229  int m_fd;
233 
234  BYTE * m_toneBuffer;
237  bool m_isALaw;
238 
239  PMutex m_dtmfMutex;
240  std::queue<char> m_dtmfQueue;
241 
244 
245  std::vector<BYTE> m_readBuffer;
246  std::vector<BYTE> m_writeBuffer;
247  };
248 
249  struct FXSChannelInfo : public ChannelInfo {
250  FXSChannelInfo(dahdi_params & parms);
251  virtual ~FXSChannelInfo();
252 
253  virtual bool IsOffHook() { return m_hookState == eOffHook; }
254 
255  virtual void OnHook();
256  virtual void OffHook();
257  virtual bool IsFXS() { return true; }
258 
259  enum HookState {
261  eOffHook
262  } m_hookState;
263  };
264 
265  static bool IsDigitalSpan(dahdi_spaninfo & span)
266  { return span.linecompat > 0; }
267 
268  void BuildPollFDs();
269  void ThreadMain();
270 
271  protected:
273  PMutex m_mutex;
274  typedef std::vector<ChannelInfo *> ChannelInfoList;
276 
277  PThread * m_thread;
278  bool m_running;
279  std::vector<pollfd> m_pollFds;
282 };
283 
284 #endif // OPAL_LIDS_DAHDI_EP_H
285 
286 
virtual PINDEX GetReadFrameSize(unsigned line)
Definition: dahdi_ep.h:113
virtual PString GetDescription() const
Definition: dahdi_ep.h:79
int m_spanNumber
Definition: dahdi_ep.h:221
virtual PINDEX GetWriteFrameSize(unsigned line)
Definition: dahdi_ep.h:115
static bool IsDigitalSpan(dahdi_spaninfo &span)
Definition: dahdi_ep.h:265
virtual bool SetWriteFrameSize(unsigned line, PINDEX frameSize)
Definition: dahdi_ep.h:118
int m_chanPos
Definition: dahdi_ep.h:223
virtual bool GetPlayVolume(unsigned line, unsigned &volume)
Definition: dahdi_ep.h:150
PThread * m_thread
Definition: dahdi_ep.h:277
virtual bool IsMediaRunning() const
Definition: dahdi_ep.h:169
Definition: dahdi_ep.h:157
virtual bool IsFXS()
Definition: dahdi_ep.h:166
int m_writeVol
Definition: dahdi_ep.h:242
ChannelInfoList m_channelInfoList
Definition: dahdi_ep.h:275
virtual bool PlayTone(unsigned line, CallProgressTones tone)
Definition: dahdi_ep.h:105
PDTMFDecoder m_dtmfDecoder
Definition: dahdi_ep.h:226
bool m_audioEnable
Definition: dahdi_ep.h:231
bool m_running
Definition: dahdi_ep.h:278
PMutex m_mutex
Definition: dahdi_ep.h:228
std::vector< BYTE > m_readBuffer
Definition: dahdi_ep.h:245
virtual bool IsValidLine(unsigned line) const
Definition: dahdi_ep.h:155
std::queue< char > m_dtmfQueue
Definition: dahdi_ep.h:240
virtual PINDEX GetWriteFrameSize()
Definition: dahdi_ep.h:179
PMutex m_mutex
Definition: dahdi_ep.h:273
virtual bool SetPlayVolume(unsigned volume)
Definition: dahdi_ep.h:204
virtual bool EnableAudio(unsigned line, bool enable=true)
Definition: dahdi_ep.h:132
bool m_mediaStarted
Definition: dahdi_ep.h:232
#define OpalPCM16
Definition: mediafmt.h:1733
PMutex m_dtmfMutex
Definition: dahdi_ep.h:239
virtual OpalMediaFormatList GetMediaFormats() const
Definition: dahdi_ep.h:81
virtual bool SetRecordVolume(unsigned volume)
Definition: dahdi_ep.h:201
int m_readVol
Definition: dahdi_ep.h:243
virtual bool IsLineTerminal(unsigned line)
Definition: dahdi_ep.h:96
virtual bool IsTonePlaying(unsigned line)
Definition: dahdi_ep.h:107
Definition: dahdi_ep.h:64
PMutex m_pollListMutex
Definition: dahdi_ep.h:280
virtual bool WriteFrame(unsigned line, const void *buf, PINDEX count, PINDEX &written)
Definition: dahdi_ep.h:138
Definition: mediafmt.h:112
std::vector< ChannelInfo * > ChannelInfoList
Definition: dahdi_ep.h:274
virtual bool SetReadFrameSize(unsigned line, PINDEX frameSize)
Definition: dahdi_ep.h:117
virtual void OffHook()
Definition: dahdi_ep.h:214
virtual bool SetWriteFrameSize(PINDEX frameSize)
Definition: dahdi_ep.h:181
int m_fd
Definition: dahdi_ep.h:229
virtual bool IsLineOffHook(unsigned line)
Definition: dahdi_ep.h:99
Definition: mediafmt.h:806
bool m_isALaw
Definition: dahdi_ep.h:237
int m_toneBufferPos
Definition: dahdi_ep.h:236
virtual bool GetPlayVolume(unsigned &volume)
Definition: dahdi_ep.h:210
virtual OpalMediaFormat GetWriteFormat(unsigned line)
Definition: dahdi_ep.h:129
virtual bool SetLineOffHook(unsigned line, bool newState=true)
Definition: dahdi_ep.h:102
std::vector< BYTE > m_writeBuffer
Definition: dahdi_ep.h:246
virtual unsigned GetLineCount() const
Definition: dahdi_ep.h:93
virtual PStringArray GetAllNames() const
Definition: dahdi_ep.h:78
virtual bool IsOffHook()
Definition: dahdi_ep.h:253
virtual bool IsFXS()
Definition: dahdi_ep.h:257
virtual bool ReadFrame(unsigned line, void *buf, PINDEX &count)
Definition: dahdi_ep.h:135
virtual bool IsOffHook()
Definition: dahdi_ep.h:167
int m_toneBufferLen
Definition: dahdi_ep.h:235
virtual PString GetDeviceType() const
Definition: dahdi_ep.h:76
bool m_pollListDirty
Definition: dahdi_ep.h:281
virtual bool SetPlayVolume(unsigned line, unsigned volume)
Definition: dahdi_ep.h:144
int m_channelNumber
Definition: dahdi_ep.h:222
int m_samplesPerBlock
Definition: dahdi_ep.h:272
virtual bool SetOffHook(bool newState)
Definition: dahdi_ep.h:176
static const char * DeviceName
Definition: dahdi_ep.h:69
BYTE * m_toneBuffer
Definition: dahdi_ep.h:234
std::vector< pollfd > m_pollFds
Definition: dahdi_ep.h:279
virtual OpalMediaFormat GetReadFormat(unsigned line)
Definition: dahdi_ep.h:126
HookState
Definition: dahdi_ep.h:259
virtual char ReadDTMF(unsigned line)
Definition: dahdi_ep.h:111
Definition: lid.h:49
virtual bool GetRecordVolume(unsigned line, unsigned &volume)
Definition: dahdi_ep.h:147
virtual bool IsAudioEnabled(unsigned line) const
Definition: dahdi_ep.h:153
virtual void OnHook()
Definition: dahdi_ep.h:213
virtual PINDEX GetReadFrameSize()
Definition: dahdi_ep.h:178
int m_samplesPerBlock
Definition: dahdi_ep.h:230
virtual bool IsAudioEnabled() const
Definition: dahdi_ep.h:168
CallProgressTones
Definition: lid.h:582
virtual bool SetReadFrameSize(PINDEX frameSize)
Definition: dahdi_ep.h:180
virtual PString GetDeviceName() const
Definition: dahdi_ep.h:77
virtual bool GetRecordVolume(unsigned &volume)
Definition: dahdi_ep.h:207
virtual bool StopTone(unsigned line)
Definition: dahdi_ep.h:109
bool m_hasHardwareToneDetection
Definition: dahdi_ep.h:225
virtual bool SetRecordVolume(unsigned line, unsigned volume)
Definition: dahdi_ep.h:141
#define OpalG711ALaw
Definition: mediafmt.h:1812