mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-12-19 03:37:46 +00:00
Added mod_khomp Endpoint.
This module has been developed to make a nice, affordable brazilian board called Khomp (http://www.khomp.com.br) compatible with FreeSWITCH. Supported boards: - FXS - FXO - E1 - GSM (boards and usb devices) - Passive record - kommuter
This commit is contained in:
549
src/mod/endpoints/mod_khomp/Install/files/khomp.conf.xml
Normal file
549
src/mod/endpoints/mod_khomp/Install/files/khomp.conf.xml
Normal file
@@ -0,0 +1,549 @@
|
||||
<configuration name="khomp.conf" description="Khomp Configuration">
|
||||
<!-- Config for all boards -->
|
||||
<settings>
|
||||
|
||||
<!--
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;;;;;;;;;;; This is the configuration file for the Khomp ;;;;;;;;;;;;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Endpoint 1.0 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
-->
|
||||
|
||||
<channels>
|
||||
<!-- Section for main configurations about channels. -->
|
||||
|
||||
<!--
|
||||
Enable/disable generalized echo cancellation in the channel, for calls
|
||||
passing inside FreeSWITCH (disabled for bridged calls).
|
||||
(default = yes)
|
||||
|
||||
<param name="echo-canceller" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable/disable AGC (auto gain control). Should be used carefully, as it
|
||||
can make line noise really loud.
|
||||
(default = yes)
|
||||
|
||||
<param name="auto-gain-control" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable/disable sending DTMFs out-band as a way to pass detected DTMFs to
|
||||
FreeSWITCH. Needed if FreeSWITCH generates digits for us in bridge application
|
||||
or is being used for IVR ("URA", in pt_BR).
|
||||
(default = yes)
|
||||
|
||||
<param name="out-of-band-dtmfs" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable/disable DTMF suppression delay. WARNING: if you disable this, DTMFs
|
||||
will not be suppressed anymore! You should only use this option if
|
||||
"out-of-band-dtmfs" is "no".
|
||||
(default = yes)
|
||||
|
||||
<param name="suppression-delay" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Adjust connection automagically if a FAX tone is detected.
|
||||
(default = yes)
|
||||
|
||||
<param name="auto-fax-adjustment" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Time (is seconds) since connection, when we may detect FAX tone and perform
|
||||
automagical adjustment. After this, FAX tone detection is ignored.
|
||||
Possible values: 0 - 9999
|
||||
(default = 30).
|
||||
|
||||
<param name="fax-adjustment-timeout" value="30"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable/disable pulse detection (reported as DTMF digits).
|
||||
(default = yes)
|
||||
|
||||
<param name="pulse-forwarding" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable correct standard following for R2/MFC protocol. Disabled by default,
|
||||
as using FreeSWITCH behind a PBX may timeout all outgoing calls without this.
|
||||
However, you can set this to "yes" if FreeSWITCH is directly connected to a
|
||||
CO (central office) or to a fast signaling PBX, and have a correct
|
||||
behaviour for condition obtaining/reporting.
|
||||
(default = no)
|
||||
|
||||
<param name="r2-strict-behaviour" value="no"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set the delay (in ms) after sending ringback condition where audio stream
|
||||
should be opened for the channel. Limited to 25ms min, 500ms max.
|
||||
(default = 250)
|
||||
|
||||
<param name="r2-preconnect-wait" value="250"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable/disable native bridge mode (known in pt_BR as "trombone") for calls
|
||||
in the Khomp channel, passing the audio inside the board when both channels
|
||||
(incoming and outgoing) are of type Khomp. This reduces the echo and the
|
||||
audio delay, and frees the host from most audio processing.
|
||||
(default = yes)
|
||||
|
||||
<param name="native-bridge" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the incoming context for calls on E1 channels. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD-LL)
|
||||
|
||||
<param name="context-digital" value="khomp-DD-LL"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the incoming context for calls on FXS channels. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD-CC)
|
||||
|
||||
<param name="context-fxs" value="khomp-DD-CC"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the "alternative" context for calls on FXS channels, which will be
|
||||
checked if the main context does not match for a call. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD)
|
||||
|
||||
<param name="context-fxs-alt" value="khomp-DD"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the incoming context for calls on FXO channels. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD-CC)
|
||||
|
||||
<param name="context-fxo" value="khomp-DD-CC"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the "alternative" context for calls on FXO channels, which will be
|
||||
checked if the main context does not match for a call. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD)
|
||||
|
||||
<param name="context-fxo-alt" value="khomp-DD"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the incoming context for calls on GSM channels. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD-CC)
|
||||
|
||||
<param name="context-gsm-call" value="khomp-DD-CC"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the "alternative" context for calls on GSM channels, which will be
|
||||
checked if the main context does not match for a call. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD)
|
||||
|
||||
<param name="context-gsm-call-alt" value="khomp-DD"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the incoming context for messages on GSM channels. Some wildcards are
|
||||
accepted, and described in the bottom of this file.
|
||||
(default = khomp-sms-DD-CC)
|
||||
|
||||
<param name="context-gsm-sms" value="khomp-sms-DD-CC"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the incoming context for calls on Passive Record boards (KPR). Some
|
||||
wildcards are accepted, and described in the bottom of this file.
|
||||
(default = khomp-DD-CC)
|
||||
|
||||
<param name="context-pr" value="khomp-DD-CC"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set the logging of messages to console. Possible values (to set more than one,
|
||||
separate the values with comma):
|
||||
|
||||
errors - Error messages, when something goes really wrong.
|
||||
warnings - Warnings, used when something might not be going as expected.
|
||||
messages - Generic messages, used to indicate some information.
|
||||
events - Show received K3L events as console messages.
|
||||
commands - Show sent K3L commands as console messages.
|
||||
audio - Enable messages for K3L audio events (very verbose!).
|
||||
modem - Enable messages for data received from KGSM modems.
|
||||
link - Enable logging of link status changes.
|
||||
standard - Special, enable default messages (RECOMMENDED).
|
||||
all - Special, enable ALL messages (should not be used naively).
|
||||
|
||||
(default = standard)
|
||||
|
||||
<param name="log-to-console" value="standard"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set the logging of messages to disk. Possible values (to set more than one,
|
||||
separate the values with comma):
|
||||
|
||||
errors - Error messages, when something goes really wrong.
|
||||
warnings - Warnings, used when something might not be going as expected.
|
||||
messages - Generic messages, used to indicate some information.
|
||||
events - Record received K3L events as log messages.
|
||||
commands - Record sent K3L commands as log messages.
|
||||
audio - Enable messages for K3L audio events (very verbose!).
|
||||
modem - Enable messages for data received from KGSM modems.
|
||||
link - Enable logging of link status changes.
|
||||
functions - Enable debugging for functions. Should not be used naively!).
|
||||
threads - Enable debugging for threads. Should not be used naively!).
|
||||
locks - Enable debugging for locks. Should not be used naively!).
|
||||
streams - Enable debugging for streams. Should not be used naively!).
|
||||
standard - Special, enable default messages (RECOMMENDED).
|
||||
debugging - Special, enable debug messages (should not be used naively).
|
||||
all - Special, enable ALL messages (DO NOT USE THIS!).
|
||||
|
||||
(default = standard)
|
||||
|
||||
<param name="log-to-disk" value="standard"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set the low level tracing. DO NOT USE THIS UNLESS YOU WERE ADVISED TO DO SO.
|
||||
Possible values (to set more than one, separate the values with comma):
|
||||
|
||||
k3l - Enable the low level tracing of board's API.
|
||||
If you are using 'kserver', this option will take no effect. For
|
||||
the k3l tracing to proceed, you will need to (re)start kserver
|
||||
with 'debug' option. E.g. '#> kserver start -debug'.
|
||||
rdsi - Enable ISDN low level debugging.
|
||||
r2 - Enable r2 protocol low level debugging.
|
||||
|
||||
(default = <empty>)
|
||||
|
||||
<param name="trace" value=""/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set output volume level. Possible values:
|
||||
|
||||
* '+ N' = increase N times;
|
||||
* '- N' = decrease N times.
|
||||
* '0' = leave default.
|
||||
|
||||
(default = 0)
|
||||
|
||||
<param name="output-volume" value="0"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set input volume level. Can only be used if AGC (and not pulse detection)
|
||||
is enabled on the board configuration. Possible values:
|
||||
|
||||
* '+ N' = increase N times;
|
||||
* '- N' = decrease N times.
|
||||
* '0' = leave default.
|
||||
|
||||
(default = 0)
|
||||
|
||||
<param name="input-volume" value="0"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Sets the default AMA flags, affecting the categorization of entries in
|
||||
the call detail records.
|
||||
(default = default)
|
||||
|
||||
<param name="amaflags" value="default"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Sets the account code for calls placed on the channel. The account code may
|
||||
be any alphanumeric string
|
||||
(default = KhompBoard)
|
||||
|
||||
<param name="accountcode" value="KhompBoard"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set the language of the channel (useful for selecting audio messages of a
|
||||
specific language on answer).
|
||||
(default = <empty>)
|
||||
|
||||
<param name="language" value="pt_BR"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Set the music on hold class of the channel (useful for selecting a group of
|
||||
songs to play on hold).
|
||||
(default = default)
|
||||
|
||||
<param name="mohclass" value="default"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Sets the global orig (CALLERID) base for FXS boards. This number is added
|
||||
to a sequencial number, which is incremented for each FXS board and FXS
|
||||
channel in the system.
|
||||
|
||||
For more example of how to use this option, see channel README file,
|
||||
section 'Opcoes do application Bridge', item '<action application="bridge" data="Khomp/r304" />'.
|
||||
(default = 0)
|
||||
|
||||
<param name="fxs-global-orig" value="0200"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Sets the numbers (separated by comma) in which the 'pbx-dialtone' from
|
||||
FXS boards will be changed to 'co-dialtone' (public tone), when they are
|
||||
pressed.
|
||||
(default = <empty>)
|
||||
|
||||
<param name="fxs-co-dialtone" value="0,90"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable or disable sending number of A throught BINA DTMF signalization to
|
||||
a FXS branch.
|
||||
(default = yes)
|
||||
|
||||
<param name="fxs-bina" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enable/disable using CTbus for Khomp CTI boards in native bridge.
|
||||
(WARNING: just used for internal testings!)
|
||||
(default = no)
|
||||
|
||||
<param name="has-ctbus" value="no"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
This is the delay time to really disconnect a channel after the disconnect
|
||||
event arrive. If a connect event comes up in this interval, then the
|
||||
disconnection is ignored and the call keeps going on. Values in ms.
|
||||
(default = 0)
|
||||
|
||||
<param name="disconnect-delay" value="0"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
This timer controls the delay associated with ringback generation in the
|
||||
Khomp channel, when the other side *does not send audio* - in other words,
|
||||
this is used when calling peers located at VoIP channels.
|
||||
Values are in milliseconds.
|
||||
(default = 1500)
|
||||
|
||||
<param name="delay-ringback-co" value="1500"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
This timer controls the delay associated with ringback generation in the
|
||||
Khomp channel when the other side report a continuous stream of audio in
|
||||
silence - in other words, this is used when the audio is present but does
|
||||
not have any tone. Values are in milliseconds.
|
||||
(default = 2500)
|
||||
|
||||
<param name="delay-ringback-pbx" value="2500"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines if the channel should optimize audio delay by droping longstanding
|
||||
packets from audio buffer. This guarantees the minimal audio delay for the
|
||||
user, and avoid delays associated with miscoded SIP clients. However,
|
||||
depending on the system's scheduling policy (some 2.6 kernel releases),
|
||||
this may result on excessive drop of packets, and audible audio skipping.
|
||||
This should not be changed naively.
|
||||
(default = no)
|
||||
|
||||
<param name="optimize-audio-path" value="no"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines if the channel should ignore some uncommon DTMF digits detected by
|
||||
the board (A, B, C and D). This reduces the number of false positives which
|
||||
may happen sometimes, without affecting correctness on traditional
|
||||
scenarios. However, if you need to pass those digits througth the board,
|
||||
you may need to set this option to 'no'.
|
||||
(default = yes)
|
||||
|
||||
<param name="ignore-letter-dtmfs" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
For KFXO series boards, defines if audio will be allowed being sent into
|
||||
outgoing calls before it has been connected.
|
||||
(default = yes)
|
||||
|
||||
<param name="fxo-send-pre-audio" value="yes"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the timeout, in seconds, between digits of a FXS board's extension.
|
||||
(default = 7)
|
||||
|
||||
<param name="fxs-digit-timeout" value="7"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Enables/Disables the action of dropping collect calls. If enabled, all
|
||||
collect calls will be dropped no matter what KDropCollectCall is set to.
|
||||
(default = no)
|
||||
|
||||
<param name="drop-collect-call" value="no"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines if the activation and deactivation of Kommuter is done automatically by the channel,
|
||||
or manually by the user. Possible values:
|
||||
|
||||
auto - Khomp channel driver starts all kommuter devices at initialization and stops them if the module is unloaded.
|
||||
manual - The user must explicity call the CLI command < khomp kommuter on/off >, that starts or stops the kommuter devices connected to this machine.
|
||||
(default = auto)
|
||||
|
||||
<param name="kommuter-activation" value="auto"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
Defines the default value for the Kommuter watchdog in seconds.
|
||||
All kommuters conected to this machine will be initialized with this value,
|
||||
and will commute the links after reaching this time with no response of the channel.
|
||||
The minimum is 0 (will never commute the links), and maximum value is 255 seconds.
|
||||
(default = 10)
|
||||
|
||||
<param name="kommuter-timeout" value="10"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
When adjusted to some DTMF digit sequence, it will define those as the digits
|
||||
to be used to initialize a call transfer using PBX-to-PBX signaling.
|
||||
(default = empty)
|
||||
|
||||
<param name="user-transfer-digits" value="#2"/>
|
||||
-->
|
||||
|
||||
<!--
|
||||
;;;;;;;;;;;;;;;;;;;; CONTEXTS WILDCARDS ;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
For incoming contexts, you can use the following wildcards:
|
||||
|
||||
'DD' (device number): the sequence number of the board on the
|
||||
system (can be checked using "khomp summary", valid for all
|
||||
board models);
|
||||
|
||||
'LL' (link number): the sequence number of link where the call
|
||||
has arrived on the board. valid only for E1 boards.
|
||||
|
||||
'SSSS' (serial number): the serial number of the board (can
|
||||
be checked using "khomp summary", and it's valid for all
|
||||
board models);
|
||||
|
||||
'CC' (channel number): the channel number on which the call
|
||||
or message has arrived (valid for FXO, FXS and GSM boards);
|
||||
|
||||
'CCC' (channel number): same as above, but valid only for E1
|
||||
channels;
|
||||
-->
|
||||
|
||||
</channels>
|
||||
|
||||
<!-- Section for configuring allocation groups. -->
|
||||
<groups>
|
||||
<!--
|
||||
In this section, you should define options using the following syntax:
|
||||
|
||||
<groupname> = <allocation-string>[:<context>]
|
||||
|
||||
<param name="pstn" value="b0l0 + b1c38"/>
|
||||
<param name="pstn" value="b0l0 + b1c38:from-pstn"/>
|
||||
|
||||
You may define your own groups. In the example above, the group
|
||||
called pstn can be used in the bridge string as <action application="brigde" data="Khomp/Gpstn/..." />
|
||||
or <action application="brigde" data="Khomp/Gpstn/..." />. As a result, the allocation string will be
|
||||
replaced with the one associated with the group "pstn". This is the same
|
||||
of doing <action application="brigde" data="Khomp/b0l0 + b1c38/... />.
|
||||
In the second example, a context is also defined which can be used in
|
||||
extensions.conf for inbound calls on that allocation string's range.
|
||||
-->
|
||||
</groups>
|
||||
|
||||
<!--
|
||||
Section for configuring cadences used on FXS boards and the whole
|
||||
channel (fast busy, ringback tones, etc).
|
||||
-->
|
||||
<cadences>
|
||||
<!--
|
||||
Default value for cadences. You may define your own cadences, and
|
||||
also use them in the bridge arguments as "cadence=your_cadence_name".
|
||||
|
||||
"0,0" means a continuous dialtone.
|
||||
|
||||
(default as defined below)
|
||||
<param name="fast-busy" value="100,100" />
|
||||
<param name="ringback" value="1000,4000" />
|
||||
<param name="pbx-dialtone" value="1000,100" />
|
||||
<param name="vm-dialtone " value="1000,100,100,100" />
|
||||
<param name="co-dialtone " value="0,0" />
|
||||
-->
|
||||
</cadences>
|
||||
|
||||
|
||||
<!-- Section for configuring CALLERID's associated with FXS boards. -->
|
||||
<fxs-branches>
|
||||
<!--
|
||||
In this section, you should define options using the following syntax:
|
||||
|
||||
'orig_prefix = serial number 0, serial number 1, ...'
|
||||
|
||||
ex: <param name="880" value="111,222"/>
|
||||
|
||||
In the example above (assuming KFXS-SPX boards 1234 and 4535), the
|
||||
branches will be numbered from 800 to 829 in board 1234, and from
|
||||
830 to 859 in board 4535.
|
||||
-->
|
||||
</fxs-branches>
|
||||
|
||||
<!-- Section for configuring FXS hotlines. -->
|
||||
<fxs-hotlines>
|
||||
<!--
|
||||
In this section, you should define options using the following syntax:
|
||||
|
||||
'orig_prefix = destination_number'
|
||||
|
||||
ex: <param name="804" value="32332933" />
|
||||
|
||||
In the example above, the branch numbered 804 will call the number
|
||||
3233-2933 (Khomp support number) every time the FXS branch goes off hook.
|
||||
-->
|
||||
</fxs-hotlines>
|
||||
|
||||
<!-- Section for configuring specific options for FXS branches. -->
|
||||
<fxs-options>
|
||||
<!--
|
||||
In this section, you should define options using the following syntax:
|
||||
|
||||
'orig_prefix = option1:value | option2:value | option3:value' ...
|
||||
|
||||
ex: <param name="804" value="output-volume:+2' />
|
||||
|
||||
In the example above, the branch numbered 804 will have specific
|
||||
configuration for default output volume set to +2.
|
||||
|
||||
Possible values to options is:
|
||||
context, input-volume, output-volume language,
|
||||
mohclass, amaflags, accountcode, calleridnum, calleridname, mailbox.
|
||||
-->
|
||||
</fxs-options>
|
||||
|
||||
</settings>
|
||||
</configuration>
|
||||
41
src/mod/endpoints/mod_khomp/Makefile
Normal file
41
src/mod/endpoints/mod_khomp/Makefile
Normal file
@@ -0,0 +1,41 @@
|
||||
MODNAME := mod_khomp
|
||||
VERBOSE := 1
|
||||
|
||||
ifeq ($(strip $(FREESWITCH_PATH)),)
|
||||
BASE := ../../../../
|
||||
else
|
||||
BASE := $(FREESWITCH_PATH)
|
||||
endif
|
||||
|
||||
curr_dir := $(shell pwd)
|
||||
|
||||
versions := -DFS_VERSION_MAJOR=$(shell bash $(curr_dir)/tools/getversion.sh "SWITCH_VERSION_MAJOR" $(BASE)) -DFS_VERSION_MINOR=$(shell bash $(curr_dir)/tools/getversion.sh "SWITCH_VERSION_MINOR" $(BASE)) -DFS_VERSION_MICRO=$(shell bash $(curr_dir)/tools/getversion.sh "SWITCH_VERSION_MICRO" $(BASE))
|
||||
|
||||
LOCAL_CFLAGS = -I./ -I./include -I./commons -I./support -D_REENTRANT -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DK3L_HOSTSYSTEM -DCOMMONS_LIBRARY_USING_FREESWITCH -g -ggdb #-DDEBUG_FLAGS
|
||||
LOCAL_CFLAGS += $(versions)
|
||||
|
||||
LOCAL_LDFLAGS = -lk3l
|
||||
|
||||
LOCAL_OBJS = ./commons/k3lapi.o ./commons/k3lutil.o ./commons/config_options.o ./commons/format.o ./commons/strings.o ./commons/ringbuffer.o ./commons/verbose.o ./commons/saved_condition.o ./commons/regex.o ./commons/timer.o ./commons/configurator/configfile.o ./commons/configurator/option.o ./commons/configurator/section.o ./commons/configurator/restriction.o
|
||||
LOCAL_OBJS += ./support/klog-config.o ./support/klog-options.o ./support/config_defaults.o
|
||||
LOCAL_OBJS += ./src/globals.o ./src/opt.o ./src/frame.o ./src/utils.o ./src/lock.o ./src/spec.o ./src/applications.o ./src/khomp_pvt_fxo.o ./src/khomp_pvt_gsm.o ./src/khomp_pvt_kxe1.o ./src/khomp_pvt_passive.o ./src/khomp_pvt.o ./src/logger.o ./src/cli.o
|
||||
|
||||
conf_file_name := khomp.conf.xml
|
||||
conf_file_dir := $(curr_dir)/Install/files
|
||||
conf_file_dir_alt := $(curr_dir)/conf
|
||||
conf_file_install = $(sysconfdir)/autoload_configs
|
||||
|
||||
include $(BASE)/build/modmake.rules
|
||||
|
||||
depend_install:
|
||||
@echo "Copy $(conf_file_name)"
|
||||
@if test -d $(conf_file_install) ; then \
|
||||
if test -f $(conf_file_dir)/$(conf_file_name) ; then \
|
||||
cp $(conf_file_dir)/$(conf_file_name) $(conf_file_install)/$(conf_file_name).new ;\
|
||||
else \
|
||||
cp $(conf_file_dir_alt)/$(conf_file_name) $(conf_file_install)/$(conf_file_name).new ;\
|
||||
fi; \
|
||||
if test ! -f "$(conf_file_install)/$(conf_file_name)" ; then \
|
||||
mv $(conf_file_install)/$(conf_file_name).new $(conf_file_install)/$(conf_file_name) ;\
|
||||
fi; \
|
||||
fi;
|
||||
189
src/mod/endpoints/mod_khomp/commons/atomic.hpp
Normal file
189
src/mod/endpoints/mod_khomp/commons/atomic.hpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
|
||||
This code was based on FreeBSD 7.X SVN (sys/i386/include/atomic.h),
|
||||
with changes regarding optimizations and generalizations, and a
|
||||
remake of the interface to fit use C++ features.
|
||||
|
||||
Code is distributed under original license.
|
||||
Original copyright follows:
|
||||
|
||||
* Copyright (c) 1998 Doug Rabson
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _ATOMIC_HPP_
|
||||
#define _ATOMIC_HPP_
|
||||
|
||||
namespace Atomic
|
||||
{
|
||||
// Macros used to insert compare and exchange instructions easily into functions.
|
||||
|
||||
#define MAKE_CMPXCHG_FUNCTION(INS, PTR, EXP, VAL, TYPE) \
|
||||
PunnedType pexp; pexp.valtype = EXP; \
|
||||
PunnedType pval; pval.valtype = VAL; \
|
||||
TYPE vexp = *(pexp.podtype); \
|
||||
TYPE vval = *(pval.podtype); \
|
||||
TYPE res; \
|
||||
unsigned char chg = 0; \
|
||||
asm volatile("lock;" INS "sete %1;" \
|
||||
: "=a" (res), /* 0 */ \
|
||||
"=q" (chg), /* 1 */ \
|
||||
"=m" (*(unsigned char **)(PTR)) /* 2 */ \
|
||||
: "r" (vval), /* 3 */ \
|
||||
"a" (vexp), /* 4 */ \
|
||||
"m" (*(unsigned char **)(PTR)) /* 5 */ \
|
||||
: "memory"); \
|
||||
*(pexp.podtype) = res; \
|
||||
return (chg != 0 ? true : false);
|
||||
|
||||
#define MAKE_CMPXCHG8B_FUNCTION(PTR,EXP,VAL) \
|
||||
PunnedType pexp; pexp.valtype = EXP; \
|
||||
PunnedType pval; pval.valtype = VAL; \
|
||||
unsigned long long vexp = *(pexp.podtype); \
|
||||
unsigned long long vval = *(pval.podtype); \
|
||||
unsigned long long res = (unsigned long long)exp; \
|
||||
unsigned char chg = 0; \
|
||||
asm volatile("lock; cmpxchg8b %2; sete %1;" \
|
||||
: "+A" (vexp), /* 0 (result) */ \
|
||||
"=q" (chg) /* 1 */ \
|
||||
: "m" (*(unsigned char**)(PTR)), /* 2 */ \
|
||||
"b" ((unsigned long)(vval)), \
|
||||
"c" ((unsigned long)(vval >> 32))); \
|
||||
*(pexp.podtype) = vexp; \
|
||||
return (chg != 0 ? true : false);
|
||||
|
||||
// Types used for making CMPXCHG instructions independent from base type.
|
||||
|
||||
template < typename ValType, typename PodType >
|
||||
union PunnedTypeTemplate
|
||||
{
|
||||
ValType * valtype;
|
||||
PodType * podtype;
|
||||
};
|
||||
|
||||
template < int SizeOfType, typename ReturnType >
|
||||
struct HelperCreateCAS;
|
||||
|
||||
template < typename ValType >
|
||||
struct HelperCreateCAS<4, ValType>
|
||||
{
|
||||
#if !defined(__LP64__) && !defined(__LP64)
|
||||
typedef unsigned long BaseType;
|
||||
#else
|
||||
typedef unsigned int BaseType;
|
||||
#endif
|
||||
|
||||
typedef PunnedTypeTemplate< ValType, BaseType > PunnedType;
|
||||
|
||||
inline static bool apply(volatile void *p, ValType * exp, ValType now)
|
||||
{
|
||||
#if !defined(__LP64__) && !defined(__LP64)
|
||||
MAKE_CMPXCHG_FUNCTION("cmpxchgl %3,%5;", p, exp, &now, BaseType);
|
||||
#else
|
||||
MAKE_CMPXCHG_FUNCTION("cmpxchgl %k3,%5;", p, exp, &now, BaseType);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
template < typename ValType >
|
||||
struct HelperCreateCAS<8, ValType>
|
||||
{
|
||||
#if !defined(__LP64__) && !defined(__LP64)
|
||||
typedef unsigned long long BaseType;
|
||||
#else
|
||||
typedef unsigned long BaseType;
|
||||
#endif
|
||||
|
||||
typedef PunnedTypeTemplate< ValType, BaseType > PunnedType;
|
||||
|
||||
inline static volatile ValType apply(volatile void *p, ValType * exp, ValType now)
|
||||
{
|
||||
#if !defined(__LP64__) && !defined(__LP64)
|
||||
MAKE_CMPXCHG8B_FUNCTION(p, exp, &now);
|
||||
#else
|
||||
MAKE_CMPXCHG_FUNCTION("cmpxchgq %3,%5;", p, exp, &now, BaseType);
|
||||
#endif
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// The CAS function itself.
|
||||
|
||||
template < typename ValType >
|
||||
inline bool doCAS(volatile ValType * p, ValType * o, ValType n)
|
||||
{
|
||||
return HelperCreateCAS<sizeof(ValType), ValType>::apply(static_cast<volatile void *>(p), o, n);
|
||||
};
|
||||
|
||||
template < typename ValType >
|
||||
inline bool doCAS(volatile ValType * p, ValType o, ValType n)
|
||||
{
|
||||
return HelperCreateCAS<sizeof(ValType), ValType>::apply(static_cast<volatile void *>(p), &o, n);
|
||||
};
|
||||
|
||||
#undef MAKE_CMPXCHG_32_FUNCTION
|
||||
#undef MAKE_CMPXCHG_64_FUNCTION
|
||||
|
||||
#define MAKE_LOCKED_TEMPLATE(NAME) \
|
||||
template < typename ValType > inline void do##NAME(volatile ValType * p, ValType v); \
|
||||
template < typename ValType > inline void do##NAME(volatile ValType * p);
|
||||
|
||||
#define MAKE_LOCKED_FUNCTION(NAME, TYPE, INS, CONS, VAL) \
|
||||
template < > inline void do##NAME < TYPE > (volatile TYPE * p, TYPE v){ asm volatile("lock;" INS : "=m" (*p) : CONS (VAL), "m" (*p)); } \
|
||||
template < > inline void do##NAME < TYPE > (volatile TYPE * p) { asm volatile("lock;" INS : "=m" (*p) : CONS (1), "m" (*p)); }
|
||||
|
||||
#define MAKE_LOCKED_FUNCTIONS(NAME, TYPE, INS, CONS, VAL) \
|
||||
MAKE_LOCKED_FUNCTION(NAME, TYPE, INS, CONS, VAL) \
|
||||
MAKE_LOCKED_FUNCTION(NAME, unsigned TYPE, INS, CONS, VAL)
|
||||
|
||||
MAKE_LOCKED_TEMPLATE(Add);
|
||||
MAKE_LOCKED_TEMPLATE(Sub);
|
||||
MAKE_LOCKED_TEMPLATE(SetBits);
|
||||
MAKE_LOCKED_TEMPLATE(ClearBits);
|
||||
|
||||
MAKE_LOCKED_FUNCTIONS(Add, int, "addl %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(Sub, int, "subl %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(SetBits, int, "orl %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(ClearBits, int, "andl %1,%0", "ir", ~v);
|
||||
|
||||
#if !defined(__LP64__) && !defined(__LP64)
|
||||
|
||||
MAKE_LOCKED_FUNCTIONS(Add, long, "addl %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(Sub, long, "subl %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(SetBits, long, "orl %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(ClearBits, long, "andl %1,%0", "ir", ~v);
|
||||
|
||||
#else
|
||||
|
||||
MAKE_LOCKED_FUNCTIONS(Add, long, "addq %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(Sub, long, "subq %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(SetBits, long, "orq %1,%0", "ir", v);
|
||||
MAKE_LOCKED_FUNCTIONS(ClearBits, long, "andq %1,%0", "ir", ~v);
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif /* _ATOMIC_HPP_ */
|
||||
69
src/mod/endpoints/mod_khomp/commons/config_commons.hpp
Normal file
69
src/mod/endpoints/mod_khomp/commons/config_commons.hpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _CONFIG_COMMONS_HPP_
|
||||
#define _CONFIG_COMMONS_HPP_
|
||||
|
||||
/****************************************************************************/
|
||||
/* ASTERISK */
|
||||
#if defined(COMMONS_LIBRARY_USING_ASTERISK)
|
||||
#define COMMONS_IMPLEMENTATION asterisk
|
||||
/****************************************************************************/
|
||||
/* CALLWEAVER */
|
||||
#elif defined(COMMONS_LIBRARY_USING_CALLWEAVER)
|
||||
#define COMMONS_IMPLEMENTATION callweaver
|
||||
/****************************************************************************/
|
||||
/* FREESWITCH */
|
||||
#elif defined(COMMONS_LIBRARY_USING_FREESWITCH)
|
||||
#define COMMONS_IMPLEMENTATION freeswitch
|
||||
/****************************************************************************/
|
||||
/* GNU/LINUX (generic) */
|
||||
#elif defined(COMMONS_LIBRARY_USING_GNU_LINUX)
|
||||
#define COMMONS_IMPLEMENTATION gnulinux
|
||||
/****************************************************************************/
|
||||
#else
|
||||
#error Unknown implementation selected. Please define COMMONS_LIBRARY_USING_* correctly.
|
||||
#endif
|
||||
|
||||
#define COMMONS_INCLUDE(file) <COMMONS_IMPLEMENTATION/file>
|
||||
|
||||
#endif /* _CONFIG_COMMONS_HPP_ */
|
||||
|
||||
710
src/mod/endpoints/mod_khomp/commons/config_options.cpp
Normal file
710
src/mod/endpoints/mod_khomp/commons/config_options.cpp
Normal file
@@ -0,0 +1,710 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <config_options.hpp>
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, const ConfigOption::StringType & value, const ConfigOption::StringType defvalue, string_allowed_type allowed, bool list_me)
|
||||
: _my_name(name), _value_data(new StringData(const_cast<StringType &>(value), defvalue, allowed), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, const ConfigOption::StringType & value, const ConfigOption::StringType defvalue, bool list_me)
|
||||
: _my_name(name), _value_data(new StringData(const_cast<StringType &>(value), defvalue, string_allowed_type()), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, const ConfigOption::SignedIntType & value, const ConfigOption::SignedIntType defvalue,
|
||||
ConfigOption::SignedIntType min, ConfigOption::SignedIntType max, ConfigOption::SignedIntType step, bool list_me)
|
||||
: _my_name(name), _value_data(new SignedIntData(const_cast<SignedIntType &>(value), defvalue, Range<SignedIntType>(min, max, step)), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, const ConfigOption::UnsignedIntType & value, const ConfigOption::UnsignedIntType defvalue,
|
||||
ConfigOption::UnsignedIntType min, ConfigOption::UnsignedIntType max, ConfigOption::UnsignedIntType step, bool list_me)
|
||||
: _my_name(name), _value_data(new UnsignedIntData(const_cast<UnsignedIntType &>(value), defvalue, Range<UnsignedIntType>(min, max, step)), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, const ConfigOption::BooleanType & value, const ConfigOption::BooleanType defvalue, bool list_me)
|
||||
: _my_name(name), _value_data(new BooleanData(const_cast<BooleanType &>(value), defvalue), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, ConfigOption::FunctionType fun, std::string defvalue, string_allowed_type allowed, bool list_me)
|
||||
: _my_name(name), _value_data(new FunctionData(fun, defvalue, allowed), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(std::string name, ConfigOption::FunctionType fun, std::string defvalue, bool list_me)
|
||||
: _my_name(name), _value_data(new FunctionData(fun, defvalue, string_allowed_type()), true),
|
||||
_list_me(list_me), _values(NULL), _loaded(false)
|
||||
{};
|
||||
|
||||
ConfigOption::ConfigOption(const ConfigOption & o)
|
||||
: _my_name(o._my_name), _value_data(o._value_data),
|
||||
_list_me(o._list_me), _values(o._values), _loaded(o._loaded)
|
||||
{};
|
||||
|
||||
ConfigOption::~ConfigOption(void)
|
||||
{
|
||||
if (_values)
|
||||
{
|
||||
for (unsigned int i = 0; _values[i] != NULL; i++)
|
||||
delete _values[i];
|
||||
|
||||
delete[] _values;
|
||||
_values = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
void ConfigOption::set(ConfigOption::StringType value)
|
||||
{
|
||||
switch (_value_data.which())
|
||||
{
|
||||
case ID_STRING:
|
||||
{
|
||||
try
|
||||
{
|
||||
StringData & tmp = _value_data.get<StringData>();
|
||||
|
||||
if (tmp.string_allowed.empty())
|
||||
{
|
||||
tmp.string_val = value;
|
||||
_loaded = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tmp.string_allowed.find(value) != tmp.string_allowed.end())
|
||||
{
|
||||
tmp.string_val = value;
|
||||
_loaded = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string allowed_string;
|
||||
|
||||
for (string_allowed_type::iterator i = tmp.string_allowed.begin(); i != tmp.string_allowed.end(); i++)
|
||||
{
|
||||
allowed_string += " '";
|
||||
allowed_string += (*i);
|
||||
allowed_string += "'";
|
||||
}
|
||||
|
||||
throw ConfigProcessFailure(STG(FMT("value '%s' not allowed for option '%s' (allowed values:%s)")
|
||||
% value % _my_name % allowed_string));
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
case ID_FUN:
|
||||
{
|
||||
try
|
||||
{
|
||||
FunctionData & tmp = _value_data.get<FunctionData>();
|
||||
tmp.fun_val(value);
|
||||
_loaded = true;
|
||||
break;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
throw ConfigProcessFailure(STG(FMT("option '%s' is not of type string, nor function defined") % _my_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigOption::set(ConfigOption::SignedIntType value)
|
||||
{
|
||||
try
|
||||
{
|
||||
SignedIntData & tmp = _value_data.get<SignedIntData>();
|
||||
|
||||
if (value < tmp.sint_Range.minimum)
|
||||
throw ConfigProcessFailure(STG(FMT("value '%d' out-of-Range for option '%s' (too low)") % value % _my_name));
|
||||
|
||||
if (value > tmp.sint_Range.maximum)
|
||||
throw ConfigProcessFailure(STG(FMT("value '%d' out-of-Range for option '%s' (too high)") % value % _my_name));
|
||||
|
||||
if (((value - tmp.sint_Range.minimum) % tmp.sint_Range.step) != 0)
|
||||
throw ConfigProcessFailure(STG(FMT("value '%d' out-of-Range for option '%s' (outside allowed step)") % value % _my_name));
|
||||
|
||||
tmp.sint_val = value;
|
||||
_loaded = true;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigOption::set(ConfigOption::UnsignedIntType value)
|
||||
{
|
||||
try
|
||||
{
|
||||
UnsignedIntData & tmp = _value_data.get<UnsignedIntData>();
|
||||
|
||||
if (value < tmp.uint_Range.minimum)
|
||||
throw ConfigProcessFailure(STG(FMT("value '%d' out-of-Range for option '%s' (too low)") % value % _my_name));
|
||||
|
||||
if (value > tmp.uint_Range.maximum)
|
||||
throw ConfigProcessFailure(STG(FMT("value '%d' out-of-Range for option '%s' (too high)") % value % _my_name));
|
||||
|
||||
if (((value - tmp.uint_Range.minimum) % tmp.uint_Range.step) != 0)
|
||||
throw ConfigProcessFailure(STG(FMT("value '%d' out-of-Range for option '%s' (outside allowed step)") % value % _my_name));
|
||||
|
||||
tmp.uint_val = value;
|
||||
_loaded = true;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigOption::set(ConfigOption::BooleanType value)
|
||||
{
|
||||
try
|
||||
{
|
||||
BooleanData & tmp = _value_data.get<BooleanData>();
|
||||
tmp.bool_val = value;
|
||||
_loaded = true;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
std::string & ConfigOption::name(void) { return _my_name; };
|
||||
|
||||
ConfigOption::value_id_type ConfigOption::type(void)
|
||||
{
|
||||
return (value_id_type) _value_data.which();
|
||||
};
|
||||
|
||||
const char ** ConfigOption::values(void)
|
||||
{
|
||||
if (_values != NULL)
|
||||
return _values;
|
||||
|
||||
switch ((value_id_type) _value_data.which())
|
||||
{
|
||||
case ConfigOption::ID_BOOL:
|
||||
{
|
||||
_values = new const char*[3];
|
||||
|
||||
_values[0] = strdup("yes");
|
||||
_values[1] = strdup("no");
|
||||
_values[2] = NULL;
|
||||
|
||||
return _values;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_SINT:
|
||||
{
|
||||
try
|
||||
{
|
||||
SignedIntData & tmp = _value_data.get<SignedIntData>();
|
||||
|
||||
|
||||
unsigned int count = ((tmp.sint_Range.maximum - tmp.sint_Range.minimum) / tmp.sint_Range.step) + 1;
|
||||
unsigned int index = 0;
|
||||
|
||||
_values = new const char*[count + 1];
|
||||
|
||||
for (SignedIntType i = tmp.sint_Range.minimum; i <= tmp.sint_Range.maximum; i += tmp.sint_Range.step, index++)
|
||||
_values[index] = strdup(STG(FMT("%d") % i).c_str());
|
||||
|
||||
_values[index] = NULL;
|
||||
|
||||
return _values;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
case ConfigOption::ID_UINT:
|
||||
{
|
||||
try
|
||||
{
|
||||
UnsignedIntData & tmp = _value_data.get<UnsignedIntData>();
|
||||
|
||||
unsigned int count = ((tmp.uint_Range.maximum - tmp.uint_Range.minimum) / tmp.uint_Range.step) + 1;
|
||||
unsigned int index = 0;
|
||||
|
||||
_values = new const char*[count + 1];
|
||||
|
||||
for (UnsignedIntType i = tmp.uint_Range.minimum; i <= tmp.uint_Range.maximum; i += tmp.uint_Range.step, index++)
|
||||
_values[index] = strdup(STG(FMT("%d") % i).c_str());
|
||||
|
||||
_values[index] = NULL;
|
||||
|
||||
return _values;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
case ConfigOption::ID_STRING:
|
||||
{
|
||||
try
|
||||
{
|
||||
StringData & tmp = _value_data.get<StringData>();
|
||||
|
||||
_values = new const char*[ tmp.string_allowed.size() + 1 ];
|
||||
|
||||
unsigned int index = 0;
|
||||
|
||||
for (string_allowed_type::iterator i = tmp.string_allowed.begin(); i != tmp.string_allowed.end(); i++, index++)
|
||||
_values[index] = strdup((*i).c_str());
|
||||
|
||||
_values[index] = NULL;
|
||||
|
||||
return _values;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
case ConfigOption::ID_FUN:
|
||||
{
|
||||
try
|
||||
{
|
||||
FunctionData & tmp = _value_data.get<FunctionData>();
|
||||
|
||||
_values = new const char*[ tmp.fun_allowed.size() + 1 ];
|
||||
|
||||
unsigned int index = 0;
|
||||
|
||||
for (string_allowed_type::iterator i = tmp.fun_allowed.begin(); i != tmp.fun_allowed.end(); i++, index++)
|
||||
_values[index] = strdup((*i).c_str());
|
||||
|
||||
_values[index] = NULL;
|
||||
return _values;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
default:
|
||||
throw ConfigProcessFailure(STG(FMT("unknown type identifier '%d'") % _value_data.which()));
|
||||
}
|
||||
};
|
||||
|
||||
void ConfigOption::reset(void)
|
||||
{
|
||||
_loaded = false;
|
||||
};
|
||||
|
||||
void ConfigOption::commit(void)
|
||||
{
|
||||
if (_loaded)
|
||||
return;
|
||||
|
||||
switch ((value_id_type) _value_data.which())
|
||||
{
|
||||
case ConfigOption::ID_BOOL:
|
||||
{
|
||||
try
|
||||
{
|
||||
BooleanData & tmp = _value_data.get<BooleanData>();
|
||||
tmp.bool_val = tmp.bool_default;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_SINT:
|
||||
{
|
||||
try
|
||||
{
|
||||
SignedIntData & tmp = _value_data.get<SignedIntData>();
|
||||
tmp.sint_val = tmp.sint_default;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_UINT:
|
||||
{
|
||||
try
|
||||
{
|
||||
UnsignedIntData & tmp = _value_data.get<UnsignedIntData>();
|
||||
tmp.uint_val = tmp.uint_default;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_STRING:
|
||||
{
|
||||
try
|
||||
{
|
||||
StringData & tmp = _value_data.get<StringData>();
|
||||
tmp.string_val = tmp.string_default;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_FUN:
|
||||
{
|
||||
try
|
||||
{
|
||||
FunctionData & tmp = _value_data.get<FunctionData>();
|
||||
tmp.fun_val(tmp.fun_default);
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw ConfigProcessFailure(STG(FMT("unknown type identifier '%d'") % _value_data.which()));
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
};
|
||||
|
||||
void ConfigOption::copy_from(ConfigOption & src)
|
||||
{
|
||||
if (src._value_data.which() != _value_data.which())
|
||||
throw ConfigProcessFailure(STG(FMT("unable to copy options, source type differs from destination.")));
|
||||
|
||||
if (!src._loaded)
|
||||
return;
|
||||
|
||||
switch ((value_id_type) _value_data.which())
|
||||
{
|
||||
case ConfigOption::ID_BOOL:
|
||||
{
|
||||
try
|
||||
{
|
||||
BooleanData & stmp = src._value_data.get<BooleanData>();
|
||||
BooleanData & dtmp = _value_data.get<BooleanData>();
|
||||
/* do not copy references, but values.. */
|
||||
bool tmpval = stmp.bool_val;
|
||||
dtmp.bool_val = tmpval;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_SINT:
|
||||
{
|
||||
try
|
||||
{
|
||||
SignedIntData & stmp = src._value_data.get<SignedIntData>();
|
||||
SignedIntData & dtmp = _value_data.get<SignedIntData>();
|
||||
/* do not copy references, but values.. */
|
||||
int tmpval = stmp.sint_val;
|
||||
dtmp.sint_val = tmpval;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_UINT:
|
||||
{
|
||||
try
|
||||
{
|
||||
UnsignedIntData & stmp = src._value_data.get<UnsignedIntData>();
|
||||
UnsignedIntData & dtmp = _value_data.get<UnsignedIntData>();
|
||||
/* do not copy references, but values.. */
|
||||
unsigned int tmpval = stmp.uint_val;
|
||||
dtmp.uint_val = tmpval;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_STRING:
|
||||
{
|
||||
try
|
||||
{
|
||||
StringData & stmp = src._value_data.get<StringData>();
|
||||
StringData & dtmp = _value_data.get<StringData>();
|
||||
/* do not copy references, but values.. */
|
||||
std::string tmpval = stmp.string_val;
|
||||
dtmp.string_val = tmpval;
|
||||
}
|
||||
catch(ValueType::InvalidType & e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ConfigOption::ID_FUN:
|
||||
{
|
||||
/* TO IMPLEMENT (NEEDS ANOTHER METHOD ON FUNCTION FOR GETTING VALUE) */
|
||||
|
||||
// FunctionData & tmp = boost::get<FunctionData>(_value_data);
|
||||
//
|
||||
// if (!tmp.loaded)
|
||||
// {
|
||||
// tmp.fun_val(tmp.fun_default);
|
||||
// tmp.loaded = true;
|
||||
// }
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw ConfigProcessFailure(STG(FMT("unknown type identifier '%d'") % _value_data.which()));
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
};
|
||||
|
||||
/*********************************/
|
||||
|
||||
bool ConfigOptions::add(ConfigOption option)
|
||||
{
|
||||
//option_map_type::iterator iter2 = _map.begin();
|
||||
|
||||
//boost::tie(iter2, ok2)
|
||||
std::pair<option_map_type::iterator, bool> ret = _map.insert(option_pair_type(option.name(), option));
|
||||
|
||||
return ret.second;
|
||||
}
|
||||
|
||||
bool ConfigOptions::synonym(std::string equiv_opt, std::string main_opt)
|
||||
{
|
||||
//syn_option_map_type::iterator iter = _syn_map.begin();
|
||||
|
||||
//boost::tie(iter, ok)
|
||||
std::pair<syn_option_map_type::iterator, bool> ret = _syn_map.insert(syn_option_pair_type(equiv_opt, main_opt));
|
||||
|
||||
return ret.second;
|
||||
}
|
||||
|
||||
ConfigOptions::string_set ConfigOptions::options(void)
|
||||
{
|
||||
string_set res;
|
||||
|
||||
for (option_map_type::iterator i = _map.begin(); i != _map.end(); i++)
|
||||
res.insert((*i).first);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void ConfigOptions::process(const char * name, const char * value)
|
||||
{
|
||||
option_map_type::iterator iter = find_option(name);
|
||||
|
||||
if (iter == _map.end())
|
||||
throw ConfigProcessFailure(STG(FMT("unknown option '%s'") % name));
|
||||
|
||||
try
|
||||
{
|
||||
switch ((*iter).second.type())
|
||||
{
|
||||
case ConfigOption::ID_SINT:
|
||||
set<ConfigOption::SignedIntType>((*iter).first, Strings::toulong(value));
|
||||
return;
|
||||
case ConfigOption::ID_UINT:
|
||||
set<ConfigOption::UnsignedIntType>((*iter).first, Strings::tolong(value));
|
||||
return;
|
||||
case ConfigOption::ID_BOOL:
|
||||
set<ConfigOption::BooleanType>((*iter).first, Strings::toboolean(value));
|
||||
return;
|
||||
case ConfigOption::ID_STRING:
|
||||
case ConfigOption::ID_FUN:
|
||||
set<ConfigOption::StringType>((*iter).first, std::string(value));
|
||||
return;
|
||||
default:
|
||||
throw ConfigProcessFailure(STG(FMT("unknown type identifier '%d'") % (*iter).second.type()));
|
||||
}
|
||||
}
|
||||
catch (Strings::invalid_value & e)
|
||||
{
|
||||
throw ConfigProcessFailure(STG(FMT("invalid value '%s' for option '%s'") % value % name));
|
||||
}
|
||||
}
|
||||
|
||||
const char ** ConfigOptions::values(const char * name)
|
||||
{
|
||||
option_map_type::iterator iter = find_option(name);
|
||||
|
||||
if (iter == _map.end())
|
||||
throw ConfigProcessFailure(STG(FMT("unknown option '%s'") % name));
|
||||
|
||||
return (*iter).second.values();
|
||||
}
|
||||
|
||||
const char ** ConfigOptions::values(void)
|
||||
{
|
||||
if (_values != NULL)
|
||||
return _values;
|
||||
|
||||
unsigned int count = 0;
|
||||
|
||||
for (option_map_type::iterator i = _map.begin(); i != _map.end(); i++)
|
||||
if ((*i).second.list_me())
|
||||
++count;
|
||||
|
||||
_values = new const char*[ count + 1 ];
|
||||
|
||||
unsigned int index = 0;
|
||||
|
||||
for (option_map_type::iterator i = _map.begin(); i != _map.end(); i++)
|
||||
{
|
||||
if ((*i).second.list_me())
|
||||
{
|
||||
_values[index] = strdup((*i).first.c_str());
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
_values[index] = NULL;
|
||||
|
||||
return _values;
|
||||
}
|
||||
|
||||
void ConfigOptions::reset(void)
|
||||
{
|
||||
for (option_map_type::iterator i = _map.begin(); i != _map.end(); i++)
|
||||
(*i).second.reset();
|
||||
}
|
||||
|
||||
ConfigOptions::messages_type ConfigOptions::commit(void)
|
||||
{
|
||||
messages_type msgs;
|
||||
|
||||
for (option_map_type::iterator i = _map.begin(); i != _map.end(); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
(*i).second.commit();
|
||||
}
|
||||
catch (ConfigProcessFailure & e)
|
||||
{
|
||||
msgs.push_back(e.msg);
|
||||
}
|
||||
}
|
||||
|
||||
return msgs;
|
||||
}
|
||||
|
||||
bool ConfigOptions::loaded(std::string name)
|
||||
{
|
||||
option_map_type::iterator iter = find_option(name);
|
||||
|
||||
if (iter == _map.end())
|
||||
return false;
|
||||
|
||||
return iter->second.loaded();
|
||||
}
|
||||
|
||||
void ConfigOptions::copy_from(ConfigOptions & source, std::string name)
|
||||
{
|
||||
option_map_type::iterator iter_src = source.find_option(name);
|
||||
option_map_type::iterator iter_dst = find_option(name);
|
||||
|
||||
if (iter_src == source._map.end())
|
||||
throw ConfigProcessFailure(STG(FMT("unknown option '%s' on source") % name));
|
||||
|
||||
if (iter_dst == _map.end())
|
||||
throw ConfigProcessFailure(STG(FMT("unknown option '%s' on destination") % name));
|
||||
|
||||
iter_dst->second.copy_from(iter_src->second);
|
||||
}
|
||||
|
||||
ConfigOptions::option_map_type::iterator ConfigOptions::find_option(std::string name)
|
||||
{
|
||||
syn_option_map_type::iterator syn_iter = _syn_map.find(name);
|
||||
|
||||
if (syn_iter != _syn_map.end())
|
||||
name = syn_iter->second;
|
||||
|
||||
option_map_type::iterator iter = _map.find(name);
|
||||
|
||||
return iter;
|
||||
}
|
||||
286
src/mod/endpoints/mod_khomp/commons/config_options.hpp
Normal file
286
src/mod/endpoints/mod_khomp/commons/config_options.hpp
Normal file
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <strings.hpp>
|
||||
|
||||
#include <function.hpp>
|
||||
#include <variant.hpp>
|
||||
#include <format.hpp>
|
||||
|
||||
#ifndef _CONFIG_OPTIONS_HPP_
|
||||
#define _CONFIG_OPTIONS_HPP_
|
||||
|
||||
struct ConfigProcessFailure
|
||||
{
|
||||
ConfigProcessFailure(std::string _msg): msg(_msg) {};
|
||||
std::string msg;
|
||||
};
|
||||
|
||||
struct ConfigOption
|
||||
{
|
||||
typedef int SignedIntType;
|
||||
typedef unsigned int UnsignedIntType;
|
||||
typedef bool BooleanType;
|
||||
typedef std::string StringType;
|
||||
|
||||
typedef Function::Function1 < void, std::string > FunctionType;
|
||||
|
||||
typedef std::set < StringType > string_allowed_type;
|
||||
|
||||
/* this should reflect 'variant.which()'! */
|
||||
typedef enum
|
||||
{
|
||||
ID_SINT = 0,
|
||||
ID_UINT = 1,
|
||||
ID_BOOL = 2,
|
||||
ID_STRING = 3,
|
||||
ID_FUN = 4,
|
||||
}
|
||||
value_id_type;
|
||||
|
||||
template < typename number_type >
|
||||
struct Range
|
||||
{
|
||||
Range(number_type _minimum, number_type _maximum, number_type _step)
|
||||
: minimum(_minimum), maximum(_maximum), step(_step) {};
|
||||
|
||||
number_type minimum, maximum, step;
|
||||
};
|
||||
|
||||
struct SignedIntData : public VariantBaseType < void >
|
||||
{
|
||||
SignedIntData(SignedIntType & _sint_val, SignedIntType _sint_default, Range< SignedIntType > _sint_Range)
|
||||
: sint_val(_sint_val), sint_default(_sint_default), sint_Range(_sint_Range) {};
|
||||
|
||||
int which()
|
||||
{
|
||||
return ID_SINT;
|
||||
}
|
||||
|
||||
SignedIntType & sint_val;
|
||||
SignedIntType sint_default;
|
||||
Range<SignedIntType> sint_Range;
|
||||
};
|
||||
|
||||
struct UnsignedIntData : public VariantBaseType < void >
|
||||
{
|
||||
UnsignedIntData(UnsignedIntType & _uint_val, UnsignedIntType _uint_default, Range< UnsignedIntType > _uint_Range)
|
||||
: uint_val(_uint_val), uint_default(_uint_default), uint_Range(_uint_Range) {};
|
||||
|
||||
int which()
|
||||
{
|
||||
return ID_UINT;
|
||||
}
|
||||
|
||||
UnsignedIntType & uint_val;
|
||||
UnsignedIntType uint_default;
|
||||
Range<UnsignedIntType> uint_Range;
|
||||
};
|
||||
|
||||
struct BooleanData : public VariantBaseType < void >
|
||||
{
|
||||
BooleanData(BooleanType & _bool_val, BooleanType _bool_default)
|
||||
: bool_val(_bool_val), bool_default(_bool_default) {};
|
||||
|
||||
int which()
|
||||
{
|
||||
return ID_BOOL;
|
||||
}
|
||||
|
||||
BooleanType & bool_val;
|
||||
BooleanType bool_default;
|
||||
};
|
||||
|
||||
struct StringData : public VariantBaseType < void >
|
||||
{
|
||||
StringData(std::string & _string_val, std::string _string_default, string_allowed_type _string_allowed)
|
||||
: string_val(_string_val), string_default(_string_default), string_allowed(_string_allowed) {};
|
||||
|
||||
int which()
|
||||
{
|
||||
return ID_STRING;
|
||||
}
|
||||
|
||||
std::string & string_val;
|
||||
std::string string_default;
|
||||
string_allowed_type string_allowed;
|
||||
};
|
||||
|
||||
struct FunctionData : public VariantBaseType < void >
|
||||
{
|
||||
FunctionData(FunctionType _fun_val, std::string _fun_default, string_allowed_type _fun_allowed)
|
||||
: fun_val(_fun_val), fun_default(_fun_default), fun_allowed(_fun_allowed) {};
|
||||
|
||||
int which()
|
||||
{
|
||||
return ID_FUN;
|
||||
}
|
||||
|
||||
FunctionType fun_val;
|
||||
std::string fun_default;
|
||||
string_allowed_type fun_allowed;
|
||||
};
|
||||
|
||||
|
||||
typedef Variant < VariantBaseType < void > > ValueType;
|
||||
|
||||
ConfigOption(std::string, const StringType &, const StringType, string_allowed_type allowed, bool list_me = true);
|
||||
ConfigOption(std::string, const StringType &, const StringType = "", bool list_me = true);
|
||||
ConfigOption(std::string, const SignedIntType &, const SignedIntType = 0, SignedIntType min = -INT_MAX, SignedIntType max = INT_MAX, SignedIntType step = 1, bool list_me = true);
|
||||
ConfigOption(std::string, const UnsignedIntType &, const UnsignedIntType = 0, UnsignedIntType min = 0, UnsignedIntType max = UINT_MAX, UnsignedIntType step = 1, bool list_me = true);
|
||||
ConfigOption(std::string, const BooleanType &, const BooleanType = false, bool list_me = true);
|
||||
ConfigOption(std::string, FunctionType, std::string defvalue, string_allowed_type allowed, bool list_me = true);
|
||||
ConfigOption(std::string, FunctionType, std::string defvalue = "", bool list_me = true);
|
||||
|
||||
ConfigOption(const ConfigOption & o);
|
||||
|
||||
~ConfigOption(void);
|
||||
|
||||
void set(StringType value);
|
||||
|
||||
void set(SignedIntType value);
|
||||
void set(UnsignedIntType value);
|
||||
void set(BooleanType value);
|
||||
|
||||
BooleanType get_bool(){ return _value_data.get<BooleanData>().bool_val; }
|
||||
std::string get_str(){ return _value_data.get<StringData>().string_val; }
|
||||
SignedIntType get_sint(){ return _value_data.get<SignedIntData>().sint_val; }
|
||||
UnsignedIntType get_uint(){ return _value_data.get<UnsignedIntData>().uint_val; }
|
||||
|
||||
std::string & name(void);
|
||||
value_id_type type(void);
|
||||
|
||||
const char ** values(void);
|
||||
|
||||
void reset(void);
|
||||
void commit(void);
|
||||
|
||||
bool list_me(void) { return _list_me; };
|
||||
bool loaded(void) { return _loaded; };
|
||||
|
||||
void copy_from(ConfigOption &);
|
||||
|
||||
protected:
|
||||
std::string _my_name;
|
||||
ValueType _value_data;
|
||||
|
||||
bool _list_me;
|
||||
const char ** _values;
|
||||
bool _loaded;
|
||||
};
|
||||
|
||||
struct ConfigOptions
|
||||
{
|
||||
typedef std::vector < std::string > messages_type;
|
||||
|
||||
ConfigOptions(void): _values(NULL) {};
|
||||
|
||||
typedef std::set < std::string > string_set;
|
||||
|
||||
typedef std::map < std::string, ConfigOption > option_map_type;
|
||||
typedef std::pair < std::string, ConfigOption > option_pair_type;
|
||||
|
||||
typedef std::map < std::string, std::string > syn_option_map_type;
|
||||
typedef std::pair < std::string, std::string > syn_option_pair_type;
|
||||
|
||||
bool add(ConfigOption option);
|
||||
|
||||
/* only valid in "process" (for backwards compatibility config files) */
|
||||
bool synonym(std::string, std::string);
|
||||
|
||||
template <typename ValueType>
|
||||
void set(std::string name, ValueType value)
|
||||
{
|
||||
option_map_type::iterator iter = _map.find(name);
|
||||
|
||||
if (iter == _map.end())
|
||||
throw ConfigProcessFailure(STG(FMT("unknown option: %s") % name));
|
||||
|
||||
(*iter).second.set(value);
|
||||
}
|
||||
|
||||
std::string get(std::string name)
|
||||
{
|
||||
option_map_type::iterator iter = _map.find(name);
|
||||
|
||||
if (iter == _map.end())
|
||||
throw ConfigProcessFailure(STG(FMT("unknown option: %s") % name));
|
||||
|
||||
switch((*iter).second.type())
|
||||
{
|
||||
case ConfigOption::ID_BOOL: return (*iter).second.get_bool() ? "yes" : "no";
|
||||
case ConfigOption::ID_STRING: return (*iter).second.get_str();
|
||||
case ConfigOption::ID_UINT: return STG(FMT("%d") % (*iter).second.get_uint());
|
||||
case ConfigOption::ID_SINT: return STG(FMT("%d") % (*iter).second.get_sint());
|
||||
case ConfigOption::ID_FUN: return "";
|
||||
}
|
||||
}
|
||||
|
||||
string_set options(void);
|
||||
|
||||
void process(const char *, const char *); /* process option from file */
|
||||
|
||||
void reset(void); /* reset loaded opts */
|
||||
messages_type commit(void); /* set defaults */
|
||||
|
||||
const char ** values(const char *); /* option value */
|
||||
const char ** values(void); /* values from options */
|
||||
|
||||
bool loaded(std::string); /* return if config was loaded */
|
||||
|
||||
void copy_from(ConfigOptions &, std::string);
|
||||
|
||||
protected:
|
||||
option_map_type::iterator find_option(std::string);
|
||||
|
||||
protected:
|
||||
option_map_type _map;
|
||||
syn_option_map_type _syn_map;
|
||||
|
||||
const char ** _values;
|
||||
};
|
||||
|
||||
#endif /* _CONFIG_OPTIONS_HPP_ */
|
||||
|
||||
241
src/mod/endpoints/mod_khomp/commons/configurator/configfile.cpp
Normal file
241
src/mod/endpoints/mod_khomp/commons/configurator/configfile.cpp
Normal file
@@ -0,0 +1,241 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
#include <errno.h>
|
||||
|
||||
#include <configurator/configfile.hpp>
|
||||
|
||||
#if _MSC_VER >= 1400
|
||||
#undef close
|
||||
#endif
|
||||
|
||||
void Configfile::ignore(std::string str)
|
||||
{
|
||||
_ignores.insert(str);
|
||||
};
|
||||
|
||||
bool Configfile::select(Section **ptr, std::string str)
|
||||
{
|
||||
/* default section == this! */
|
||||
*ptr = this;
|
||||
|
||||
/* always success by default */
|
||||
return true;
|
||||
};
|
||||
|
||||
bool Configfile::adjust(Section * section, std::string & opt, std::string & val)
|
||||
{
|
||||
return section->load(opt, val);
|
||||
};
|
||||
|
||||
bool Configfile::deserialize(std::ifstream& fd)
|
||||
{
|
||||
Section * section = NULL;
|
||||
|
||||
/* default selection! */
|
||||
if (!select(§ion))
|
||||
{
|
||||
_errors.push_back("default selection has failed!");
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t count = 0;
|
||||
|
||||
while (fd.good())
|
||||
{
|
||||
std::string str;
|
||||
|
||||
/* read one line! */
|
||||
std::getline(fd, str);
|
||||
|
||||
size_t lst = str.size() - 1;
|
||||
|
||||
if (str.size() >= 1 && str[lst] == '\r') //cuida das quebras de linha do tipo \r\n
|
||||
{
|
||||
str.erase(lst,1);
|
||||
--lst;
|
||||
}
|
||||
|
||||
/* empty line! */
|
||||
if (str.size() == 0)
|
||||
continue;
|
||||
|
||||
/* comment! */
|
||||
if (str[0] == '#')
|
||||
continue;
|
||||
|
||||
++count;
|
||||
|
||||
if (str[0] == '[' && str[lst] == ']')
|
||||
{
|
||||
str.erase(0,1); --lst;
|
||||
str.erase(lst,1); --lst;
|
||||
|
||||
if (!select(§ion, str))
|
||||
{
|
||||
_errors.push_back(STG(FMT("erroneous section '%s'") % str));
|
||||
|
||||
/* ignore this section */
|
||||
section = NULL;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string::size_type pos = str.find('=');
|
||||
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
_errors.push_back(STG(FMT("erroneous separator '%s'") % str));
|
||||
continue;
|
||||
};
|
||||
|
||||
if (section == NULL)
|
||||
{
|
||||
_errors.push_back(STG(FMT("no section for option '%s'") % str));
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string opt(str.substr(0,pos));
|
||||
std::string val(str.substr(pos+1));
|
||||
|
||||
if (_ignores.find(opt) != _ignores.end())
|
||||
continue;
|
||||
|
||||
if (val == "@") val = "";
|
||||
|
||||
if (adjust(section, opt, val))
|
||||
continue;
|
||||
|
||||
_errors.push_back(STG(FMT("option '%s' does "
|
||||
"not exist or '%s' is not a valid value (at section '%s')")
|
||||
% opt % val % section->name()));
|
||||
}
|
||||
}
|
||||
|
||||
// retorna 'true' se arquivo tinha alguma coisa valida.
|
||||
return (count != 0);
|
||||
}
|
||||
|
||||
bool Configfile::obtain()
|
||||
{
|
||||
std::ifstream fd(_filename.c_str());
|
||||
|
||||
if (!fd.is_open())
|
||||
{
|
||||
_errors.push_back(STG(FMT("unable to open file '%s': %s")
|
||||
% _filename % strerror(errno)));
|
||||
return false;
|
||||
};
|
||||
|
||||
if (!deserialize(fd))
|
||||
{
|
||||
fd.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
fd.close();
|
||||
return true;
|
||||
};
|
||||
|
||||
void Configfile::recurse(std::ofstream& fd, Section * section)
|
||||
{
|
||||
typedef Section::SectionMap::iterator section_iter;
|
||||
typedef Section::OptionMap::iterator option_iter;
|
||||
|
||||
for (option_iter i = section->option_begin(); i != section->option_end(); i++)
|
||||
{
|
||||
std::string res;
|
||||
|
||||
if ((*i).second.store(res))
|
||||
{
|
||||
if (res == "") res = "@";
|
||||
fd << (*i).first << "=" << res << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (!section->recursive())
|
||||
return;
|
||||
|
||||
for (section_iter j = section->section_begin(); j != section->section_end(); j++)
|
||||
recurse(fd, (*j).second);
|
||||
}
|
||||
|
||||
bool Configfile::serialize(std::ofstream& fd)
|
||||
{
|
||||
recurse(fd, this);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Configfile::provide()
|
||||
{
|
||||
std::string tmp(_filename);
|
||||
tmp += ".new";
|
||||
|
||||
std::ofstream fd(tmp.c_str());
|
||||
|
||||
if (!fd.good())
|
||||
{
|
||||
_errors.push_back(STG(FMT("unable to open file '%s': %s")
|
||||
% tmp % strerror(errno)));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!serialize(fd))
|
||||
{
|
||||
fd.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
fd.close();
|
||||
|
||||
if (rename(tmp.c_str(), _filename.c_str()) != 0)
|
||||
{
|
||||
_errors.push_back(STG(FMT("unable to replace config file '%s': %s")
|
||||
% _filename % strerror(errno)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if _MSC_VER >= 1400
|
||||
#define close _close
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
|
||||
#include <format.hpp>
|
||||
|
||||
#include <configurator/section.hpp>
|
||||
|
||||
#ifndef _CONFIG_CONFIGFILE_HPP_
|
||||
#define _CONFIG_CONFIGFILE_HPP_
|
||||
|
||||
struct Configfile: public Section
|
||||
{
|
||||
typedef std::list < std::string > ErrorVector;
|
||||
typedef std::set < std::string > NameSet;
|
||||
|
||||
Configfile(std::string name, std::string desc)
|
||||
: Section(name, desc), _good(false) {};
|
||||
|
||||
virtual ~Configfile() {};
|
||||
|
||||
bool good() { return _good; };
|
||||
std::string & filename() { return _filename; };
|
||||
|
||||
ErrorVector & errors() { return _errors; };
|
||||
|
||||
void ignore(std::string);
|
||||
|
||||
virtual bool obtain();
|
||||
virtual bool provide();
|
||||
|
||||
protected:
|
||||
virtual bool select(Section **, std::string str = "");
|
||||
virtual bool adjust(Section *, std::string & opt, std::string & val);
|
||||
|
||||
virtual bool deserialize(std::ifstream &);
|
||||
virtual bool serialize(std::ofstream &);
|
||||
|
||||
void recurse(std::ofstream &, Section *);
|
||||
|
||||
protected:
|
||||
bool _good;
|
||||
ErrorVector _errors;
|
||||
NameSet _ignores;
|
||||
std::string _filename;
|
||||
};
|
||||
|
||||
#endif /* _CONFIG_CONFIGFILE_HPP_ */
|
||||
185
src/mod/endpoints/mod_khomp/commons/configurator/option.cpp
Normal file
185
src/mod/endpoints/mod_khomp/commons/configurator/option.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <configurator/option.hpp>
|
||||
|
||||
bool Option::equals(std::string & value)
|
||||
{
|
||||
switch (_restriction.numeral())
|
||||
{
|
||||
case Restriction::N_UNIQUE:
|
||||
{
|
||||
Restriction::Value my_value;
|
||||
|
||||
if (!_restriction.get(Restriction::F_USER, my_value))
|
||||
return false;
|
||||
|
||||
return (my_value == value);
|
||||
}
|
||||
case Restriction::N_MULTIPLE:
|
||||
{
|
||||
Restriction::Vector my_values;
|
||||
|
||||
if (!_restriction.get(Restriction::F_USER, my_values))
|
||||
return false;
|
||||
|
||||
for (Restriction::Vector::iterator i = my_values.begin(); i != my_values.end(); i++)
|
||||
{
|
||||
if ((*i) == value)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Option::load(std::string & value)
|
||||
{
|
||||
bool ret = _restriction.set(Restriction::F_FILE, value);
|
||||
|
||||
if (ret) _modified = false;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Option::change(std::string & value)
|
||||
{
|
||||
bool ret = _restriction.set(Restriction::F_FILE, value);
|
||||
|
||||
if (ret) _modified = true;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Option::store(std::string & value)
|
||||
{
|
||||
switch (_restriction.numeral())
|
||||
{
|
||||
case Restriction::N_UNIQUE:
|
||||
return _restriction.get(Restriction::F_FILE, value);
|
||||
|
||||
case Restriction::N_MULTIPLE:
|
||||
{
|
||||
Restriction::Vector values;
|
||||
|
||||
if (!_restriction.get(Restriction::F_FILE, values))
|
||||
return false;
|
||||
|
||||
Strings::Merger strs;
|
||||
|
||||
for (Restriction::Vector::iterator i = values.begin(); i != values.end(); i++)
|
||||
strs.add(*i);
|
||||
|
||||
value = strs.merge(",");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Option::Flags Option::set(const char * value)
|
||||
{
|
||||
std::string str_value(value);
|
||||
return set(str_value);
|
||||
}
|
||||
|
||||
|
||||
Option::Flags Option::set(Restriction::Value & value)
|
||||
{
|
||||
Restriction::Value last_value, curr_value;
|
||||
Flags flags;
|
||||
|
||||
bool ret1 = _restriction.get(Restriction::F_USER, last_value);
|
||||
|
||||
if (!_restriction.set(Restriction::F_USER, value))
|
||||
return flags;
|
||||
|
||||
flags[F_ADJUSTED] = true;
|
||||
|
||||
bool ret2 = _restriction.get(Restriction::F_USER, curr_value);
|
||||
|
||||
if (!ret1 || (ret2 && (last_value != curr_value)))
|
||||
{
|
||||
flags[F_MODIFIED] = true;
|
||||
_modified = true;
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
Option::Flags Option::set(Restriction::Vector & values)
|
||||
{
|
||||
Restriction::Vector last_values, curr_values;
|
||||
Flags flags;
|
||||
|
||||
bool ret1 = _restriction.get(Restriction::F_USER, last_values);
|
||||
|
||||
if (!_restriction.set(Restriction::F_USER, values))
|
||||
return flags;
|
||||
|
||||
flags[F_ADJUSTED] = true;
|
||||
|
||||
bool ret2 = _restriction.get(Restriction::F_USER, curr_values);
|
||||
|
||||
if (!ret1 || (ret2 && (last_values != curr_values)))
|
||||
{
|
||||
flags[F_MODIFIED] = true;
|
||||
_modified = true;
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
bool Option::get(Restriction::Value & value)
|
||||
{
|
||||
return _restriction.get(Restriction::F_USER, value);
|
||||
}
|
||||
|
||||
bool Option::get(Restriction::Vector & values)
|
||||
{
|
||||
return _restriction.get(Restriction::F_USER, values);
|
||||
}
|
||||
122
src/mod/endpoints/mod_khomp/commons/configurator/option.hpp
Normal file
122
src/mod/endpoints/mod_khomp/commons/configurator/option.hpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#include <strings.hpp>
|
||||
|
||||
#include <configurator/restriction.hpp>
|
||||
|
||||
#ifndef _CONFIG_OPTION_HPP_
|
||||
#define _CONFIG_OPTION_HPP_
|
||||
|
||||
struct Option
|
||||
{
|
||||
enum FlagTypes
|
||||
{
|
||||
F_MODIFIED = 0x0, /* if option was modified */
|
||||
F_ADJUSTED = 0x1, /* if option was correctly formated */
|
||||
};
|
||||
|
||||
struct Flags: public std::vector<bool>
|
||||
{
|
||||
Flags(): std::vector<bool>(2) {};
|
||||
};
|
||||
|
||||
typedef Restriction::Value Value;
|
||||
typedef Restriction::Vector Vector;
|
||||
|
||||
/* exception */
|
||||
struct InvalidDefaultValue
|
||||
{
|
||||
InvalidDefaultValue(std::string name, std::string value)
|
||||
: _name(name), _value(value) {};
|
||||
|
||||
std::string & name() { return _name; };
|
||||
std::string & value() { return _value; };
|
||||
|
||||
protected:
|
||||
std::string _name;
|
||||
std::string _value;
|
||||
};
|
||||
|
||||
Option(std::string name, std::string desc, std::string defvalue, Restriction restriction)
|
||||
: _name(name), _desc(desc), _restriction(restriction), _modified(true)
|
||||
{
|
||||
std::string value(defvalue);
|
||||
|
||||
if (!(set(value)[F_ADJUSTED]))
|
||||
throw InvalidDefaultValue(name, defvalue);
|
||||
}
|
||||
|
||||
const std::string & name() { return _name; };
|
||||
const std::string & description() { return _desc; };
|
||||
|
||||
Restriction & restriction() { return _restriction; };
|
||||
bool modified() { return _modified; };
|
||||
|
||||
public:
|
||||
bool load(std::string &);
|
||||
bool change(std::string &);
|
||||
bool store(std::string &);
|
||||
|
||||
Flags set(const char *);
|
||||
Flags set(Value &);
|
||||
Flags set(Vector &);
|
||||
|
||||
bool get(Value &);
|
||||
bool get(Vector &);
|
||||
|
||||
bool equals(std::string &);
|
||||
|
||||
protected:
|
||||
std::string _name;
|
||||
std::string _desc;
|
||||
|
||||
Restriction _restriction;
|
||||
bool _modified;
|
||||
};
|
||||
|
||||
#endif /* _CONFIG_OPTION_HPP_ */
|
||||
353
src/mod/endpoints/mod_khomp/commons/configurator/restriction.cpp
Normal file
353
src/mod/endpoints/mod_khomp/commons/configurator/restriction.cpp
Normal file
@@ -0,0 +1,353 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <strings.hpp>
|
||||
|
||||
#include <configurator/restriction.hpp>
|
||||
|
||||
/* internal helper! */
|
||||
bool Restriction::equalNumber(const double a, const double b)
|
||||
{
|
||||
char tmp1[64];
|
||||
char tmp2[64];
|
||||
|
||||
snprintf(tmp1, sizeof(tmp1), "%.3f", a);
|
||||
snprintf(tmp2, sizeof(tmp2), "%.3f", b);
|
||||
|
||||
if (strncmp(tmp1, tmp2, sizeof(tmp1)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* process value to our internal representation */
|
||||
|
||||
bool Restriction::process(Restriction::Format fmt,
|
||||
const Restriction::Value & value, Restriction::Value & final)
|
||||
{
|
||||
switch (_bounds)
|
||||
{
|
||||
case B_RANGE:
|
||||
{
|
||||
if (_kind != K_NUMBER)
|
||||
return false;
|
||||
|
||||
std::string tmpvalue;
|
||||
|
||||
Restriction::Value::const_iterator itr = value.begin();
|
||||
Restriction::Value::const_iterator end = value.end();
|
||||
|
||||
tmpvalue.reserve(value.size());
|
||||
|
||||
// f*cking dot/comma notation!
|
||||
for (; itr != end; ++itr)
|
||||
tmpvalue += ((*itr) != ',' ? (*itr) : '.');
|
||||
|
||||
try
|
||||
{
|
||||
double newvalue = Strings::todouble(tmpvalue);
|
||||
|
||||
if (newvalue < _init && newvalue > _fini)
|
||||
return false;
|
||||
|
||||
double res = (newvalue - _init) / _step;
|
||||
|
||||
if (!equalNumber(res, rint(res)))
|
||||
return false;
|
||||
|
||||
final = value;
|
||||
return true;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
case B_LIST:
|
||||
for (List::iterator i = _list.begin(); i != _list.end(); i++)
|
||||
{
|
||||
Value & tmp = (*i);
|
||||
|
||||
if (tmp == value)
|
||||
{
|
||||
final = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
case B_MAPS:
|
||||
switch (fmt)
|
||||
{
|
||||
case F_USER:
|
||||
{
|
||||
Map::iterator i = _map_from_usr.find(std::string(value));
|
||||
|
||||
if (i == _map_from_usr.end())
|
||||
return false;
|
||||
|
||||
Value & tmp = (*i).second;
|
||||
|
||||
final = tmp;
|
||||
return true;
|
||||
}
|
||||
|
||||
case F_FILE:
|
||||
{
|
||||
Map::iterator i = _map_from_cfg.find(std::string(value));
|
||||
|
||||
if (i == _map_from_cfg.end())
|
||||
return false;
|
||||
|
||||
final = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case B_FREE:
|
||||
final = value;
|
||||
return true;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* unprocess the value (outputs the external representation) */
|
||||
|
||||
bool Restriction::unprocess(Restriction::Format fmt,
|
||||
const Restriction::Value & value, Restriction::Value & final)
|
||||
{
|
||||
switch (_bounds)
|
||||
{
|
||||
case B_MAPS:
|
||||
|
||||
switch (fmt)
|
||||
{
|
||||
case F_USER:
|
||||
{
|
||||
Map::iterator i = _map_from_cfg.find(std::string(value));
|
||||
|
||||
if (i == _map_from_cfg.end())
|
||||
return false;
|
||||
|
||||
final = (*i).second;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
final = value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************** *****************************/
|
||||
|
||||
bool Restriction::get(Restriction::Format fmt, Restriction::Value & value)
|
||||
{
|
||||
if (_numeral != N_UNIQUE)
|
||||
return false;
|
||||
|
||||
if (!unprocess(fmt, _value._unique, value))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Restriction::get(Restriction::Format fmt, Restriction::Vector & values)
|
||||
{
|
||||
if (_numeral != N_MULTIPLE)
|
||||
return false;
|
||||
|
||||
List & my_values = _value._multiple;
|
||||
|
||||
for (List::iterator i = my_values.begin(); i != my_values.end(); i++)
|
||||
{
|
||||
Value & value = (*i);
|
||||
Value final;
|
||||
|
||||
if (!unprocess(fmt, value, final))
|
||||
return false;
|
||||
|
||||
values.push_back(final);
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/***************************** *****************************/
|
||||
|
||||
bool Restriction::set(Restriction::Format fmt, Restriction::Value &value)
|
||||
{
|
||||
switch (_numeral)
|
||||
{
|
||||
case N_UNIQUE:
|
||||
{
|
||||
Value final;
|
||||
|
||||
if (!process(fmt, value, final))
|
||||
return false;
|
||||
|
||||
_value._unique = final;
|
||||
return true;
|
||||
}
|
||||
|
||||
case N_MULTIPLE:
|
||||
{
|
||||
if (value == "@" || value == "#" || value == "")
|
||||
{
|
||||
_value._multiple.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
Strings::vector_type values;
|
||||
Strings::tokenize(value, values, ",");
|
||||
|
||||
return set(fmt, values);
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Restriction::set(Restriction::Format fmt, Restriction::Vector & values)
|
||||
{
|
||||
if (_numeral != N_MULTIPLE)
|
||||
return false;
|
||||
|
||||
if (values.empty())
|
||||
{
|
||||
_value._multiple.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* list needed to store temporary values */
|
||||
List finals;
|
||||
|
||||
for (Vector::iterator i = values.begin(); i != values.end(); i++)
|
||||
{
|
||||
Value & value = (*i);
|
||||
Value final;
|
||||
|
||||
if (!process(fmt, value, final))
|
||||
return false;
|
||||
|
||||
finals.push_back(final);
|
||||
}
|
||||
|
||||
List & lst = _value._multiple;
|
||||
|
||||
/* need to clear values set before */
|
||||
lst.clear();
|
||||
|
||||
for (List::iterator i = finals.begin(); i != finals.end(); i++)
|
||||
{
|
||||
Value value = (*i);
|
||||
lst.push_back(value);
|
||||
}
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/***************************** *****************************/
|
||||
|
||||
void Restriction::allowed(Restriction::Vector &vals)
|
||||
{
|
||||
switch (_bounds)
|
||||
{
|
||||
case B_FREE:
|
||||
return;
|
||||
|
||||
case B_LIST:
|
||||
for (List::iterator i = _list.begin(); i != _list.end(); i++)
|
||||
vals.push_back((*i));
|
||||
break;
|
||||
|
||||
case B_MAPS:
|
||||
for (Map::iterator i = _map_from_usr.begin(); i != _map_from_usr.end(); i++)
|
||||
vals.push_back((*i).first);
|
||||
break;
|
||||
|
||||
case B_RANGE:
|
||||
{
|
||||
if (_kind != K_NUMBER)
|
||||
return;
|
||||
|
||||
// is there any fraction?
|
||||
bool has_fraction = (!equalNumber(_init, rint(_init))) || (!equalNumber(_fini, rint(_fini))) || (!equalNumber(_step, rint(_step)));
|
||||
|
||||
const char * format = (has_fraction ? "%.2f" : "%02.0f");
|
||||
|
||||
for (double i = _init; i <= _fini; i += _step)
|
||||
{
|
||||
char tmp[32];
|
||||
snprintf(tmp, sizeof(tmp), format, i);
|
||||
vals.push_back(std::string(tmp));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Restriction::init_class()
|
||||
{
|
||||
_value._unique.clear();
|
||||
_value._multiple.clear();
|
||||
}
|
||||
321
src/mod/endpoints/mod_khomp/commons/configurator/restriction.hpp
Normal file
321
src/mod/endpoints/mod_khomp/commons/configurator/restriction.hpp
Normal file
@@ -0,0 +1,321 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#ifndef _CONFIG_RESTRICTION_HPP_
|
||||
#define _CONFIG_RESTRICTION_HPP_
|
||||
|
||||
struct Restriction
|
||||
{
|
||||
/* generic types */
|
||||
enum Format
|
||||
{
|
||||
F_USER,
|
||||
F_FILE
|
||||
};
|
||||
|
||||
enum Kind
|
||||
{
|
||||
// K_INTEGER,
|
||||
// K_FLOAT,
|
||||
K_STRING,
|
||||
K_NUMBER // = K_INTEGER // compatibility
|
||||
};
|
||||
|
||||
enum Bounds
|
||||
{
|
||||
B_FREE,
|
||||
B_RANGE,
|
||||
B_LIST,
|
||||
B_MAPS
|
||||
};
|
||||
|
||||
enum Numeral
|
||||
{
|
||||
N_UNIQUE,
|
||||
N_MULTIPLE
|
||||
};
|
||||
|
||||
typedef std::string Value;
|
||||
|
||||
/* types used for data input */
|
||||
struct Pair
|
||||
{
|
||||
const char *pretty;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
typedef std::pair < Value, Value > PairMap;
|
||||
typedef std::list < PairMap > ListMap;
|
||||
|
||||
/* types used internally */
|
||||
typedef std::map < Value, Value > Map;
|
||||
typedef std::vector < Value > Vector;
|
||||
|
||||
typedef std::list < Value > List;
|
||||
typedef std::pair < Value, Value > MapPair;
|
||||
|
||||
struct Generic
|
||||
{
|
||||
Value _unique;
|
||||
List _multiple;
|
||||
};
|
||||
|
||||
Restriction(Kind kind, Numeral num)
|
||||
: _kind(kind), _bounds(B_FREE), _numeral(num), _unit(""),
|
||||
_init(-1), _fini(-1), _step(-1)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
/*
|
||||
Restriction(Kind kind, Numeral num,
|
||||
int init, int fini, int step = 1)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_iinit(init), _ifini(fini), _istep(step),
|
||||
_finit(-1), _ffini(-1), _fstep(-1)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
const char *unit, int init, int fini, int step = 1)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_iinit(init), _ifini(fini), _istep(step),
|
||||
_finit(-1), _ffini(-1), _fstep(-1)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
std::string unit, int init, int fini, int step = 1)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_iinit(init), _ifini(fini), _istep(step),
|
||||
_finit(-1), _ffini(-1), _fstep(-1)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
float init, float fini, float step = 1)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_iinit(-1), _ifini(-1), _istep(-1)
|
||||
_finit(init), _ffini(fini), _fstep(step),
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
const char *unit, float init, float fini, float step = 1.0)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_iinit(-1), _ifini(-1), _istep(-1)
|
||||
_finit(init), _ffini(fini), _fstep(step),
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
std::string unit, float init, float fini, float step = 1.0)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_iinit(-1), _ifini(-1), _istep(-1)
|
||||
_finit(init), _ffini(fini), _fstep(step),
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
*/
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
double init, double fini, double step = 1)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(""),
|
||||
_init(init), _fini(fini), _step(step)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
const char *unit, double init, double fini, double step = 1.0)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_init(init), _fini(fini), _step(step)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
std::string unit, double init, double fini, double step = 1.0)
|
||||
: _kind(kind), _bounds(B_RANGE), _numeral(num), _unit(unit),
|
||||
_init(init), _fini(fini), _step(step)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
const char *first, ...)
|
||||
: _kind(kind), _bounds(B_LIST), _numeral(num), _unit(""),
|
||||
_init(-1), _fini(-1), _step(-1)
|
||||
{
|
||||
_list.push_back(std::string(first));
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, first);
|
||||
|
||||
while (true)
|
||||
{
|
||||
const char *arg = va_arg(ap, const char *);
|
||||
|
||||
if (arg == NULL) break;
|
||||
|
||||
_list.push_back(std::string(arg));
|
||||
}
|
||||
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, const char *unit, Numeral num,
|
||||
const char *first, ...)
|
||||
: _kind(kind), _bounds(B_LIST), _numeral(num), _unit(unit),
|
||||
_init(-1), _fini(-1), _step(-1)
|
||||
{
|
||||
_list.push_back(std::string(first));
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, first);
|
||||
|
||||
while (true)
|
||||
{
|
||||
const char *arg = va_arg(ap, const char *);
|
||||
|
||||
if (arg == NULL) break;
|
||||
|
||||
_list.push_back(std::string(arg));
|
||||
}
|
||||
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num,
|
||||
const struct Pair first, ...)
|
||||
: _kind(kind), _bounds(B_MAPS), _numeral(num), _unit(""),
|
||||
_init(-1), _fini(-1), _step(-1)
|
||||
{
|
||||
_map_from_usr.insert(MapPair(Value(first.pretty), Value(first.value)));
|
||||
_map_from_cfg.insert(MapPair(Value(first.value), Value(first.pretty)));
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, first);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Pair arg = va_arg(ap, Pair);
|
||||
|
||||
if (arg.pretty == NULL) break;
|
||||
|
||||
_map_from_usr.insert(MapPair(Value(arg.pretty), Value(arg.value)));
|
||||
_map_from_cfg.insert(MapPair(Value(arg.value), Value(arg.pretty)));
|
||||
}
|
||||
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num, List list)
|
||||
: _kind(kind), _bounds(B_LIST), _numeral(num), _unit(""),
|
||||
_init(-1), _fini(-1), _step(-1), _list(list)
|
||||
{
|
||||
init_class();
|
||||
}
|
||||
|
||||
Restriction(Kind kind, Numeral num, ListMap map)
|
||||
: _kind(kind), _bounds(B_MAPS), _numeral(num), _unit(""),
|
||||
_init(-1), _fini(-1), _step(-1)
|
||||
{
|
||||
for (ListMap::iterator i = map.begin(); i != map.end(); i++)
|
||||
{
|
||||
_map_from_usr.insert(MapPair(Value((*i).first), Value((*i).second)));
|
||||
_map_from_cfg.insert(MapPair(Value((*i).second), Value((*i).first)));
|
||||
}
|
||||
|
||||
init_class();
|
||||
}
|
||||
|
||||
Kind kind() { return _kind; };
|
||||
Bounds bounds() { return _bounds; };
|
||||
Numeral numeral() { return _numeral; };
|
||||
|
||||
Value unit() { return _unit; };
|
||||
|
||||
bool set(Format, Vector &);
|
||||
bool set(Format, Value &);
|
||||
|
||||
bool get(Format, Vector &);
|
||||
bool get(Format, Value &);
|
||||
|
||||
void allowed(Vector &);
|
||||
|
||||
private:
|
||||
bool process(Format, const Value &, Value &);
|
||||
bool unprocess(Format, const Value &, Value &);
|
||||
void init_class();
|
||||
|
||||
bool equalNumber(const double, const double);
|
||||
|
||||
protected:
|
||||
Kind _kind;
|
||||
Bounds _bounds;
|
||||
Numeral _numeral;
|
||||
|
||||
Value _unit;
|
||||
|
||||
double _init, _fini, _step;
|
||||
|
||||
Map _map_from_usr,
|
||||
_map_from_cfg;
|
||||
|
||||
List _list;
|
||||
|
||||
Generic _value;
|
||||
};
|
||||
|
||||
#endif /* _CONFIG_RESTRICTION_HPP_ */
|
||||
130
src/mod/endpoints/mod_khomp/commons/configurator/section.cpp
Normal file
130
src/mod/endpoints/mod_khomp/commons/configurator/section.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <configurator/section.hpp>
|
||||
|
||||
void Section::options(Section::OptionVector & vec)
|
||||
{
|
||||
for (OptionMap::iterator it = _options.begin(); it != _options.end();)
|
||||
{
|
||||
vec.push_back(&((*it).second));
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void Section::sections(Section::SectionVector & vec)
|
||||
{
|
||||
for (SectionMap::iterator it = _sections.begin(); it != _sections.end();)
|
||||
{
|
||||
vec.push_back((*it).second);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
/*********/
|
||||
|
||||
Option * Section::option_find(std::string & str, bool recurse)
|
||||
{
|
||||
OptionMap::iterator i = _options.find(str);
|
||||
|
||||
if (i == _options.end())
|
||||
{
|
||||
if (!recurse)
|
||||
throw not_found();
|
||||
|
||||
for (SectionMap::iterator i = _sections.begin(); i != _sections.end(); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return i->second->option_find(str, recurse);
|
||||
}
|
||||
catch (not_found & e)
|
||||
{
|
||||
/* keep looping! */
|
||||
};
|
||||
}
|
||||
|
||||
throw not_found();
|
||||
}
|
||||
|
||||
return &((*i).second);
|
||||
}
|
||||
|
||||
Option * Section::option_find(const char * str, bool recurse)
|
||||
{
|
||||
std::string sstr(str);
|
||||
return option_find(sstr, recurse);
|
||||
}
|
||||
|
||||
/*********/
|
||||
|
||||
Section * Section::section_find(std::string & str, bool recurse)
|
||||
{
|
||||
SectionMap::iterator i = _sections.find(str);
|
||||
|
||||
if (i == _sections.end())
|
||||
{
|
||||
if (!recurse)
|
||||
throw not_found();
|
||||
|
||||
for (SectionMap::iterator i = _sections.begin(); i != _sections.end(); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return i->second->section_find(str, recurse);
|
||||
}
|
||||
catch (not_found & e)
|
||||
{
|
||||
/* keep looping! */
|
||||
};
|
||||
}
|
||||
|
||||
throw not_found();
|
||||
}
|
||||
|
||||
return ((*i).second);
|
||||
}
|
||||
|
||||
Section * Section::section_find(const char * str, bool recurse)
|
||||
{
|
||||
std::string sstr(str);
|
||||
return section_find(sstr, recurse);
|
||||
}
|
||||
226
src/mod/endpoints/mod_khomp/commons/configurator/section.hpp
Normal file
226
src/mod/endpoints/mod_khomp/commons/configurator/section.hpp
Normal file
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#include <configurator/option.hpp>
|
||||
|
||||
#ifndef _CONFIG_SECTION_HPP_
|
||||
#define _CONFIG_SECTION_HPP_
|
||||
|
||||
struct Section
|
||||
{
|
||||
typedef std::map < std::string, Option > OptionMap;
|
||||
typedef std::vector< Option * > OptionVector;
|
||||
|
||||
typedef std::map < std::string, Section * > SectionMap;
|
||||
typedef std::vector < Section * > SectionVector;
|
||||
|
||||
struct not_found {}; /* exception */
|
||||
|
||||
// protected:
|
||||
Section(std::string name, std::string desc, bool recursive = true)
|
||||
: _name(name), _description(desc), _recursive(recursive) {};
|
||||
|
||||
void add(Option o)
|
||||
{
|
||||
_options.insert(std::pair<std::string,Option>(o.name(), o));
|
||||
};
|
||||
|
||||
void del(std::string name)
|
||||
{
|
||||
_options.erase(name);
|
||||
};
|
||||
|
||||
void add(Section *s)
|
||||
{
|
||||
_sections.insert(std::pair<std::string,Section*>(s->name(), s));
|
||||
};
|
||||
|
||||
public:
|
||||
const std::string & name() { return _name; };
|
||||
const std::string & description() { return _description; };
|
||||
const bool & recursive() { return _recursive; };
|
||||
|
||||
OptionMap::iterator option_begin() { return _options.begin(); };
|
||||
OptionMap::iterator option_end() { return _options.end(); };
|
||||
|
||||
SectionMap::iterator section_begin() { return _sections.begin(); };
|
||||
SectionMap::iterator section_end() { return _sections.end(); };
|
||||
|
||||
/**/
|
||||
|
||||
Option * option_find(const char *, bool recurse = false);
|
||||
Section * section_find(const char *, bool recurse = false);
|
||||
|
||||
Option * option_find(std::string &, bool recurse = false);
|
||||
Section * section_find(std::string &, bool recurse = false);
|
||||
|
||||
/**/
|
||||
|
||||
void options(OptionVector &);
|
||||
void sections(SectionVector &);
|
||||
|
||||
/**/
|
||||
|
||||
template <typename F>
|
||||
bool search_and_apply(std::string &key, std::string &value, F f)
|
||||
{
|
||||
OptionMap::iterator i = _options.find(key);
|
||||
|
||||
if (i != _options.end())
|
||||
return f((*i).second);
|
||||
|
||||
if (!_recursive)
|
||||
return false;
|
||||
|
||||
return (find_if(_sections.begin(), _sections.end(), f) != _sections.end());
|
||||
}
|
||||
|
||||
private:
|
||||
struct key_value
|
||||
{
|
||||
key_value(std::string &k, std::string &v): _k(k), _v(v) {};
|
||||
std::string & _k, & _v;
|
||||
};
|
||||
|
||||
struct load_section: protected key_value
|
||||
{
|
||||
load_section(std::string &k, std::string &v): key_value(k,v) {};
|
||||
|
||||
bool operator()(Option &o) { return o.load(_v); };
|
||||
bool operator()(SectionMap::value_type &v) { return v.second->load(_k,_v); };
|
||||
};
|
||||
|
||||
struct change_section: protected key_value
|
||||
{
|
||||
change_section(std::string &k, std::string &v): key_value(k,v) {};
|
||||
|
||||
bool operator()(Option &o) { return o.change(_v); };
|
||||
bool operator()(SectionMap::value_type &v) { return v.second->change(_k,_v); };
|
||||
};
|
||||
|
||||
struct store_section: protected key_value
|
||||
{
|
||||
store_section(std::string &k, std::string &v): key_value(k,v) {};
|
||||
|
||||
bool operator()(Option &o) { return o.store(_v); };
|
||||
bool operator()(SectionMap::value_type &v) { return v.second->store(_k,_v); };
|
||||
};
|
||||
|
||||
struct set_section: protected key_value
|
||||
{
|
||||
set_section(std::string &k, std::string &v): key_value(k,v) {};
|
||||
|
||||
bool operator()(Option &o) { return (o.set(_v))[Option::F_ADJUSTED]; };
|
||||
bool operator()(SectionMap::value_type &v) { return v.second->set(_k,_v); };
|
||||
};
|
||||
|
||||
struct get_section: protected key_value
|
||||
{
|
||||
get_section(std::string &k, std::string &v): key_value(k,v) {};
|
||||
|
||||
bool operator()(Option &o) { return o.get(_v); };
|
||||
bool operator()(SectionMap::value_type &v) { return v.second->get(_k,_v); };
|
||||
};
|
||||
|
||||
struct modified_section
|
||||
{
|
||||
bool operator()(OptionMap::value_type &v) { return v.second.modified(); };
|
||||
bool operator()(SectionMap::value_type &v) { return v.second->modified(); };
|
||||
};
|
||||
|
||||
public:
|
||||
bool load(const char * key, std::string value)
|
||||
{
|
||||
std::string skey(key);
|
||||
return search_and_apply(skey, value, load_section(skey, value));
|
||||
}
|
||||
|
||||
bool load(std::string &key, std::string &value)
|
||||
{
|
||||
return search_and_apply(key, value, load_section(key, value));
|
||||
}
|
||||
|
||||
bool change(std::string &key, std::string &value)
|
||||
{
|
||||
return search_and_apply(key, value, change_section(key, value));
|
||||
}
|
||||
|
||||
bool store(std::string &key, std::string &value)
|
||||
{
|
||||
return search_and_apply(key, value, store_section(key, value));
|
||||
}
|
||||
|
||||
bool set(std::string &key, std::string &value)
|
||||
{
|
||||
return search_and_apply(key, value, set_section(key, value));
|
||||
}
|
||||
|
||||
bool get(std::string &key, std::string &value)
|
||||
{
|
||||
return search_and_apply(key, value, get_section(key, value));
|
||||
}
|
||||
|
||||
bool modified()
|
||||
{
|
||||
return ((find_if(_options.begin(), _options.end(), modified_section()) != _options.end()) ||
|
||||
(find_if(_sections.begin(), _sections.end(), modified_section()) != _sections.end()));
|
||||
}
|
||||
|
||||
private:
|
||||
Section() {};
|
||||
|
||||
protected:
|
||||
std::string _name;
|
||||
std::string _description;
|
||||
|
||||
OptionMap _options;
|
||||
SectionMap _sections;
|
||||
|
||||
bool _recursive;
|
||||
};
|
||||
|
||||
#endif /* _CONFIG_SECTION_HPP_ */
|
||||
102
src/mod/endpoints/mod_khomp/commons/flagger.hpp
Normal file
102
src/mod/endpoints/mod_khomp/commons/flagger.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <initializer.hpp>
|
||||
|
||||
#ifndef _FLAGGER_HPP_
|
||||
#define _FLAGGER_HPP_
|
||||
|
||||
template < typename Flag >
|
||||
struct Flagger
|
||||
{
|
||||
protected:
|
||||
struct Bool
|
||||
{
|
||||
Bool(): value(false) {};
|
||||
Bool(bool &v): value(v) {};
|
||||
|
||||
bool value;
|
||||
};
|
||||
|
||||
typedef std::map< Flag, Bool > Map;
|
||||
|
||||
public:
|
||||
typedef Initializer< Flag > InitFlags;
|
||||
|
||||
Flagger() {};
|
||||
|
||||
Flagger(InitFlags flags)
|
||||
{
|
||||
for (typename InitFlags::iterator i = flags.begin(); i != flags.end(); i++)
|
||||
{
|
||||
Flag & flag = (*i);
|
||||
_map[flag].value = true;
|
||||
};
|
||||
};
|
||||
|
||||
void set(Flag elt, bool value = true)
|
||||
{
|
||||
_map[elt].value = value;
|
||||
}
|
||||
|
||||
bool is_set(Flag elt)
|
||||
{
|
||||
return _map[elt].value;
|
||||
}
|
||||
|
||||
Flagger & operator&(Flag elt)
|
||||
{
|
||||
set(elt);
|
||||
return *this;
|
||||
};
|
||||
|
||||
bool operator[](Flag elt)
|
||||
{
|
||||
return is_set(elt);
|
||||
};
|
||||
|
||||
protected:
|
||||
Map _map;
|
||||
};
|
||||
|
||||
#endif /* _FLAGGER_HPP_ */
|
||||
331
src/mod/endpoints/mod_khomp/commons/format.cpp
Normal file
331
src/mod/endpoints/mod_khomp/commons/format.cpp
Normal file
@@ -0,0 +1,331 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include "format.hpp"
|
||||
//#include <iostream>
|
||||
|
||||
Format::Format(const char * format_string, bool raise_exception)
|
||||
: _format(format_string), _valid(true), _raise(raise_exception)
|
||||
{
|
||||
initialize(format_string);
|
||||
}
|
||||
|
||||
/*
|
||||
Format::Format(std::string & format_string, bool raise_exception)
|
||||
: _format(NULL), _valid(true), _raise(raise_exception)
|
||||
{
|
||||
initialize(format_string.c_str());
|
||||
}
|
||||
*/
|
||||
|
||||
Format::Format(std::string format_string, bool raise_exception)
|
||||
: _format(format_string), _valid(true), _raise(raise_exception)
|
||||
{
|
||||
initialize(format_string.c_str());
|
||||
}
|
||||
|
||||
void Format::initialize(const char * format_string)
|
||||
{
|
||||
std::string txt;
|
||||
|
||||
const char * ptr = format_string;
|
||||
|
||||
while (*ptr != '\0')
|
||||
{
|
||||
if (*ptr != '%')
|
||||
{
|
||||
txt += *ptr;
|
||||
++ptr;
|
||||
continue;
|
||||
}
|
||||
|
||||
const char * ptr2 = ptr+1;
|
||||
|
||||
if (*ptr2 == '%')
|
||||
{
|
||||
txt += *ptr;
|
||||
|
||||
ptr += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!txt.empty())
|
||||
push_argument(txt, T_LITERAL);
|
||||
|
||||
std::string arg(1, *ptr);
|
||||
|
||||
++ptr;
|
||||
|
||||
bool finished = false;
|
||||
|
||||
short long_count = 0;
|
||||
short short_count = 0;
|
||||
|
||||
while(*ptr != '\0' && !finished)
|
||||
{
|
||||
switch (*ptr)
|
||||
{
|
||||
case ' ':
|
||||
// uncomplete format with ' ', make it a literal.
|
||||
arg += *ptr;
|
||||
push_argument(arg, T_LITERAL);
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case '%':
|
||||
// uncomplete format with '%', make it a literal and start a new format.
|
||||
push_argument(arg, T_LITERAL);
|
||||
arg += *ptr;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
short_count = std::min<short>(short_count+1, 2);
|
||||
long_count = 0;
|
||||
arg += *ptr;
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
long_count = std::min<short>(long_count+1, 2);
|
||||
short_count = 0;
|
||||
arg += *ptr;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
case 'i':
|
||||
arg += *ptr;
|
||||
switch (long_count - short_count)
|
||||
{
|
||||
case 2:
|
||||
push_argument(arg, T_SIGNED_LONG_LONG);
|
||||
break;
|
||||
case 1:
|
||||
push_argument(arg, T_SIGNED_LONG);
|
||||
break;
|
||||
case 0:
|
||||
push_argument(arg, T_SIGNED_INT);
|
||||
break;
|
||||
case -1:
|
||||
push_argument(arg, T_SIGNED_SHORT);
|
||||
break;
|
||||
case -2:
|
||||
push_argument(arg, T_SIGNED_SHORT_SHORT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
case 'u':
|
||||
case 'x':
|
||||
case 'X':
|
||||
arg += *ptr;
|
||||
switch (long_count - short_count)
|
||||
{
|
||||
case 2:
|
||||
push_argument(arg, T_UNSIGNED_LONG_LONG);
|
||||
break;
|
||||
case 1:
|
||||
push_argument(arg, T_UNSIGNED_LONG);
|
||||
break;
|
||||
case 0:
|
||||
push_argument(arg, T_UNSIGNED_INT);
|
||||
break;
|
||||
case -1:
|
||||
push_argument(arg, T_UNSIGNED_SHORT);
|
||||
break;
|
||||
case -2:
|
||||
push_argument(arg, T_UNSIGNED_SHORT_SHORT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'f':
|
||||
case 'F':
|
||||
case 'g':
|
||||
case 'G':
|
||||
case 'a':
|
||||
case 'A':
|
||||
arg += *ptr;
|
||||
push_argument(arg, T_FLOAT);
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
arg += *ptr;
|
||||
push_argument(arg, T_CHAR);
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
arg += *ptr;
|
||||
push_argument(arg, T_STRING);
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
arg += *ptr;
|
||||
push_argument(arg, T_POINTER);
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
case 'C':
|
||||
case 'S':
|
||||
case 'm':
|
||||
case 'n': // unsupported for now.
|
||||
arg += *ptr;
|
||||
push_argument(arg, T_ANYTHING);
|
||||
finished = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
arg += *ptr;
|
||||
break;
|
||||
}
|
||||
|
||||
++ptr;
|
||||
}
|
||||
|
||||
if (!arg.empty())
|
||||
push_argument(arg, T_LITERAL);
|
||||
}
|
||||
|
||||
if (!txt.empty())
|
||||
push_argument(txt, T_LITERAL);
|
||||
}
|
||||
|
||||
void Format::mark_invalid(std::string & msg)
|
||||
{
|
||||
if (_valid)
|
||||
{
|
||||
_valid = false;
|
||||
|
||||
std::string finalmsg;
|
||||
|
||||
finalmsg += "** INVALID FORMAT: ";
|
||||
finalmsg += msg;
|
||||
finalmsg += " **";
|
||||
|
||||
_result = finalmsg;
|
||||
}
|
||||
}
|
||||
|
||||
void Format::raise_check(void)
|
||||
{
|
||||
if (!_valid && _raise)
|
||||
throw InvalidFormat(_result);
|
||||
}
|
||||
|
||||
bool Format::validity_check(void)
|
||||
{
|
||||
raise_check();
|
||||
|
||||
return _valid;
|
||||
}
|
||||
|
||||
const Format::Argument * Format::next_argument(void)
|
||||
{
|
||||
// std::cerr << "size: " << _args.size() << std::endl;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// std::cerr << "loop size: " << _args.size() << std::endl;
|
||||
|
||||
if (_args.empty())
|
||||
return NULL; // throw NoArgumentLeft();
|
||||
|
||||
const Argument & top = _args.front();
|
||||
|
||||
if (top.type() == T_LITERAL)
|
||||
{
|
||||
// std::cerr << "top type == LITERAL, looping..." << std::endl;
|
||||
_result += top.fmts();
|
||||
pop_argument();
|
||||
}
|
||||
else
|
||||
{
|
||||
// std::cerr << "top type: " << top.type() << std::endl;
|
||||
return ⊤
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Format::pop_argument(void)
|
||||
{
|
||||
_args.pop();
|
||||
}
|
||||
|
||||
void Format::push_argument(std::string & data, Format::Type type)
|
||||
{
|
||||
// std::cerr << "pushing type (" << type << ") with format (" << data << ")" << std::endl;
|
||||
|
||||
_args.push(Argument(data, type));
|
||||
data.clear();
|
||||
}
|
||||
|
||||
std::string Format::str()
|
||||
{
|
||||
if (!validity_check())
|
||||
return _result;
|
||||
|
||||
if (next_argument() == NULL)
|
||||
return _result;
|
||||
|
||||
std::string msg;
|
||||
|
||||
msg += "too few arguments passed for format '";
|
||||
msg += _format;
|
||||
msg += "' (";
|
||||
msg += _format;
|
||||
msg += ")";
|
||||
|
||||
mark_invalid(msg);
|
||||
|
||||
return _result;
|
||||
}
|
||||
|
||||
|
||||
512
src/mod/endpoints/mod_khomp/commons/format.hpp
Normal file
512
src/mod/endpoints/mod_khomp/commons/format.hpp
Normal file
@@ -0,0 +1,512 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _FORMAT_H_
|
||||
#define _FORMAT_H_
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <typeinfo>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef WIN32 // WINDOWS
|
||||
#include <KHostSystem.h>
|
||||
#endif
|
||||
|
||||
|
||||
/* macros used for shortening lines and making the code clearer */
|
||||
#define STG(x) (x).str()
|
||||
#define FMT(x) Format(x)
|
||||
|
||||
struct Format
|
||||
{
|
||||
static const unsigned int strings_base_length = 64;
|
||||
static const unsigned int generic_base_length = 64;
|
||||
|
||||
struct InvalidFormat
|
||||
{
|
||||
InvalidFormat(std::string msg) : _msg(msg) {}
|
||||
|
||||
std::string _msg;
|
||||
};
|
||||
|
||||
explicit Format(const char * format_string, bool raise_exception = false);
|
||||
explicit Format(std::string format_string, bool raise_exception = false);
|
||||
|
||||
void initialize(const char *);
|
||||
|
||||
std::string str(void);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
protected:
|
||||
|
||||
enum Type
|
||||
{
|
||||
T_ANYTHING = 1,
|
||||
|
||||
T_SIGNED_SHORT,
|
||||
T_SIGNED_SHORT_SHORT,
|
||||
T_SIGNED_INT,
|
||||
T_SIGNED_LONG,
|
||||
T_SIGNED_LONG_LONG,
|
||||
|
||||
T_UNSIGNED_SHORT,
|
||||
T_UNSIGNED_SHORT_SHORT,
|
||||
T_UNSIGNED_INT,
|
||||
T_UNSIGNED_LONG,
|
||||
T_UNSIGNED_LONG_LONG,
|
||||
|
||||
T_FLOAT,
|
||||
T_CHAR,
|
||||
|
||||
T_POINTER,
|
||||
T_STRING,
|
||||
|
||||
T_LITERAL
|
||||
};
|
||||
|
||||
struct Argument
|
||||
{
|
||||
Argument(std::string fmts, Type type)
|
||||
: _fmts(fmts), _type(type) {};
|
||||
|
||||
Type type(void) const { return _type; }
|
||||
const std::string & fmts(void) const { return _fmts; }
|
||||
|
||||
protected:
|
||||
std::string _fmts;
|
||||
Type _type;
|
||||
};
|
||||
|
||||
typedef std::queue < Argument > ArgumentQueue;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
public:
|
||||
|
||||
template < typename V >
|
||||
Format & operator%( V value )
|
||||
{
|
||||
if (!validity_check())
|
||||
return *this;
|
||||
|
||||
const Argument * top = next_argument();
|
||||
|
||||
if (top == NULL)
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "too many arguments passed for format '";
|
||||
msg += _format;
|
||||
msg += "'";
|
||||
|
||||
mark_invalid(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
char temp[generic_base_length];
|
||||
|
||||
if (!generic_verify(value, top->type()))
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "type mismatch: got type '";
|
||||
msg += typeid(value).name();
|
||||
msg += "' in format '";
|
||||
msg += top->fmts();
|
||||
msg += "' (";
|
||||
msg += _format;
|
||||
msg += ")";
|
||||
|
||||
mark_invalid(msg);
|
||||
return *this;
|
||||
}
|
||||
|
||||
snprintf(temp, sizeof(temp), top->fmts().c_str(), value);
|
||||
_result += temp;
|
||||
|
||||
pop_argument();
|
||||
}
|
||||
|
||||
raise_check();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
Format & operator%( V * value )
|
||||
{
|
||||
if (!validity_check())
|
||||
return *this;
|
||||
|
||||
const Argument * top = next_argument();
|
||||
|
||||
if (top == NULL)
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "too many arguments passed for format '";
|
||||
msg += _format;
|
||||
msg += "'";
|
||||
|
||||
mark_invalid(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (top->type())
|
||||
{
|
||||
case T_POINTER:
|
||||
{
|
||||
char temp[generic_base_length];
|
||||
snprintf(temp, sizeof(temp), top->fmts().c_str(), value);
|
||||
_result += temp;
|
||||
break;
|
||||
}
|
||||
|
||||
case T_STRING:
|
||||
{
|
||||
if ((typeid(const char) == typeid(V)) ||
|
||||
(typeid(char) == typeid(V)) ||
|
||||
(typeid(const unsigned char) == typeid(V)) ||
|
||||
(typeid(unsigned char) == typeid(V)) ||
|
||||
(typeid(const void) == typeid(V)) ||
|
||||
(typeid(void) == typeid(V)))
|
||||
{
|
||||
int len = strlen((const char*)value)+strings_base_length+1;
|
||||
|
||||
char * temp = new char[len];
|
||||
|
||||
snprintf(temp, len, top->fmts().c_str(), value);
|
||||
_result += temp;
|
||||
|
||||
delete[] temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "type mismatch: got type '";
|
||||
msg += typeid(value).name();
|
||||
msg += "' in string format (";
|
||||
msg += _format;
|
||||
msg += ")";
|
||||
|
||||
mark_invalid(msg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "type mismatch: got pointer/string type in format '";
|
||||
msg += top->fmts();
|
||||
msg += "' (";
|
||||
msg += _format;
|
||||
msg += ")";
|
||||
|
||||
mark_invalid(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pop_argument();
|
||||
}
|
||||
|
||||
raise_check();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*
|
||||
Format & operator%( std::string value )
|
||||
{
|
||||
return operator%(value);
|
||||
}
|
||||
*/
|
||||
|
||||
Format & operator%( const std::string value )
|
||||
{
|
||||
if (!validity_check())
|
||||
return *this;
|
||||
|
||||
const Argument * top = next_argument();
|
||||
|
||||
if (top == NULL)
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "too many arguments passed for format '";
|
||||
msg += _format;
|
||||
msg += "'";
|
||||
|
||||
mark_invalid(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (top->type() == T_STRING)
|
||||
{
|
||||
int len = value.length()+strings_base_length+1;
|
||||
|
||||
char * temp = new char[len];
|
||||
|
||||
snprintf(temp, len, top->fmts().c_str(), value.c_str());
|
||||
_result += temp;
|
||||
|
||||
delete[] temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string msg;
|
||||
|
||||
msg += "type mismatch: got string type in format '";
|
||||
msg += top->fmts();
|
||||
msg += "' (";
|
||||
msg += _format;
|
||||
msg += ")";
|
||||
|
||||
mark_invalid(msg);
|
||||
}
|
||||
|
||||
pop_argument();
|
||||
}
|
||||
|
||||
raise_check();
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_signed_short( V value )
|
||||
{
|
||||
return
|
||||
((typeid(V) == typeid(short int) ||
|
||||
typeid(V) == typeid(short) ||
|
||||
typeid(V) == typeid(const short int) ||
|
||||
typeid(V) == typeid(const short) ||
|
||||
typeid(V) == typeid(volatile short int) ||
|
||||
typeid(V) == typeid(volatile short)) &&
|
||||
sizeof(V) == sizeof(short));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_unsigned_short( V value )
|
||||
{
|
||||
return
|
||||
((typeid(V) == typeid(unsigned short int) ||
|
||||
typeid(V) == typeid(unsigned short) ||
|
||||
typeid(V) == typeid(const unsigned short int) ||
|
||||
typeid(V) == typeid(const unsigned short) ||
|
||||
typeid(V) == typeid(volatile unsigned short int) ||
|
||||
typeid(V) == typeid(volatile unsigned short)) &&
|
||||
sizeof(V) == sizeof(unsigned short));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_signed_long( V value )
|
||||
{
|
||||
return
|
||||
((typeid(V) == typeid(long int) ||
|
||||
typeid(V) == typeid(long) ||
|
||||
typeid(V) == typeid(const long int) ||
|
||||
typeid(V) == typeid(const long) ||
|
||||
typeid(V) == typeid(volatile long int) ||
|
||||
typeid(V) == typeid(volatile long)) &&
|
||||
sizeof(V) == sizeof(long));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_unsigned_long( V value )
|
||||
{
|
||||
return
|
||||
((typeid(V) == typeid(unsigned long int) ||
|
||||
typeid(V) == typeid(unsigned long) ||
|
||||
typeid(V) == typeid(const unsigned long int) ||
|
||||
typeid(V) == typeid(const unsigned long) ||
|
||||
typeid(V) == typeid(volatile unsigned long int) ||
|
||||
typeid(V) == typeid(volatile unsigned long)) &&
|
||||
sizeof(V) == sizeof(long long));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_signed_long_long( V value )
|
||||
{
|
||||
return
|
||||
((typeid(V) == typeid(long long int) ||
|
||||
typeid(V) == typeid(long long) ||
|
||||
typeid(V) == typeid(const long long int) ||
|
||||
typeid(V) == typeid(const long long) ||
|
||||
typeid(V) == typeid(volatile long long) ||
|
||||
typeid(V) == typeid(volatile long long int)) &&
|
||||
sizeof(V) == sizeof(long long));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_unsigned_long_long( V value )
|
||||
{
|
||||
return
|
||||
((typeid(V) == typeid(unsigned long long int) ||
|
||||
typeid(V) == typeid(unsigned long long) ||
|
||||
typeid(V) == typeid(const unsigned long long int) ||
|
||||
typeid(V) == typeid(const unsigned long long) ||
|
||||
typeid(V) == typeid(volatile unsigned long long) ||
|
||||
typeid(V) == typeid(volatile unsigned long long int)) &&
|
||||
sizeof(V) == sizeof(unsigned long long));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_signed_int( V value )
|
||||
{
|
||||
return
|
||||
(sizeof(V) <= sizeof(int) ||
|
||||
typeid(V) == typeid(int) ||
|
||||
typeid(V) == typeid(const int) ||
|
||||
typeid(V) == typeid(volatile int));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool number_verify_unsigned_int( V value )
|
||||
{
|
||||
return
|
||||
(sizeof(V) <= sizeof(unsigned int) ||
|
||||
typeid(V) == typeid(unsigned int) ||
|
||||
typeid(V) == typeid(const unsigned int) ||
|
||||
typeid(V) == typeid(volatile unsigned int));
|
||||
}
|
||||
|
||||
template < typename V >
|
||||
bool generic_verify( V value, Type type )
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
/* EXCEPTION: consider any number an valid input. */
|
||||
case T_SIGNED_INT:
|
||||
case T_UNSIGNED_INT:
|
||||
return
|
||||
(number_verify_signed_int(value) ||
|
||||
number_verify_unsigned_int(value) ||
|
||||
number_verify_signed_long(value) ||
|
||||
number_verify_unsigned_long(value) ||
|
||||
number_verify_signed_short(value) ||
|
||||
number_verify_unsigned_short(value));
|
||||
|
||||
case T_SIGNED_SHORT_SHORT:
|
||||
return (typeid(V) == typeid(char) || typeid(V) == typeid(const char));
|
||||
|
||||
case T_SIGNED_SHORT:
|
||||
return number_verify_signed_short(value);
|
||||
|
||||
case T_SIGNED_LONG:
|
||||
return number_verify_signed_long(value);
|
||||
|
||||
case T_SIGNED_LONG_LONG:
|
||||
return number_verify_signed_long_long(value);
|
||||
|
||||
case T_UNSIGNED_SHORT_SHORT:
|
||||
return (typeid(V) == typeid(unsigned char) || typeid(V) == typeid(unsigned char));
|
||||
|
||||
case T_UNSIGNED_SHORT:
|
||||
return number_verify_unsigned_short(value);
|
||||
|
||||
case T_UNSIGNED_LONG:
|
||||
return number_verify_unsigned_long(value);
|
||||
|
||||
case T_UNSIGNED_LONG_LONG:
|
||||
return number_verify_unsigned_long_long(value);
|
||||
|
||||
case T_FLOAT:
|
||||
return (typeid(V) == typeid(float)) || (typeid(V) == typeid(double) ||
|
||||
typeid(V) == typeid(const float)) || (typeid(V) == typeid(const double));
|
||||
|
||||
case T_CHAR:
|
||||
return (typeid(V) == typeid(char)) || (typeid(V) == typeid(unsigned char) ||
|
||||
typeid(V) == typeid(const char)) || (typeid(V) == typeid(const unsigned char));
|
||||
|
||||
case T_POINTER:
|
||||
case T_STRING:
|
||||
return false;
|
||||
|
||||
case T_ANYTHING:
|
||||
return true;
|
||||
|
||||
case T_LITERAL:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
void mark_invalid(std::string &);
|
||||
|
||||
bool validity_check(void);
|
||||
void raise_check(void);
|
||||
|
||||
/*
|
||||
struct NoArgumentLeft
|
||||
{
|
||||
NoArgumentLeft(): empty(0) {};
|
||||
|
||||
unsigned int empty;
|
||||
};
|
||||
*/
|
||||
|
||||
const Argument * next_argument(void);
|
||||
|
||||
void pop_argument(void);
|
||||
void push_argument(std::string & data, Type type);
|
||||
|
||||
private:
|
||||
std::string _format;
|
||||
|
||||
bool _valid;
|
||||
bool _raise;
|
||||
|
||||
std::string _result;
|
||||
ArgumentQueue _args;
|
||||
};
|
||||
|
||||
#endif /* _FORMAT_H_ */
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include "saved_condition.hpp"
|
||||
|
||||
bool SavedCondition::wait(unsigned int msec)
|
||||
{
|
||||
bool ret = true;
|
||||
|
||||
switch_mutex_lock(_mutex);
|
||||
|
||||
if (!_signaled)
|
||||
{
|
||||
/* msec * 1000 = The amount of time in microseconds to wait. */
|
||||
if (switch_thread_cond_timedwait(_condition, _mutex, (switch_interval_time_t)msec * 1000) != 0)
|
||||
ret = false;
|
||||
}
|
||||
|
||||
_signaled = false;
|
||||
|
||||
switch_mutex_unlock(_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _SAVED_CONDITION_
|
||||
#define _SAVED_CONDITION_
|
||||
|
||||
#include <saved_condition.hpp>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <switch.h>
|
||||
}
|
||||
|
||||
struct SavedCondition : public SavedConditionCommon// : public RefCounter < SavedCondition >
|
||||
{
|
||||
typedef switch_thread_cond_t BaseConditionType;
|
||||
typedef switch_mutex_t BaseMutexType;
|
||||
|
||||
SavedCondition(switch_memory_pool_t *pool=NULL):
|
||||
_pool(pool),
|
||||
_can_delete_pool(false)
|
||||
{
|
||||
if(!_pool)
|
||||
{
|
||||
switch_core_new_memory_pool(&_pool);
|
||||
_can_delete_pool = true;
|
||||
}
|
||||
|
||||
switch_thread_cond_create(&_condition, _pool);
|
||||
switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool);
|
||||
}
|
||||
|
||||
//SavedCondition(const SavedCondition &);
|
||||
~SavedCondition()
|
||||
{
|
||||
switch_thread_cond_destroy(_condition);
|
||||
switch_mutex_destroy(_mutex);
|
||||
|
||||
if(_can_delete_pool)
|
||||
switch_core_destroy_memory_pool(&_pool);
|
||||
}
|
||||
|
||||
void signal(void)
|
||||
{
|
||||
switch_mutex_lock(_mutex);
|
||||
|
||||
_signaled = true;
|
||||
switch_thread_cond_signal(_condition);
|
||||
|
||||
switch_mutex_unlock(_mutex);
|
||||
}
|
||||
|
||||
void broadcast(void)
|
||||
{
|
||||
switch_mutex_lock(_mutex);
|
||||
|
||||
_signaled = true;
|
||||
switch_thread_cond_broadcast(_condition);
|
||||
|
||||
switch_mutex_unlock(_mutex);
|
||||
}
|
||||
|
||||
void wait(void)
|
||||
{
|
||||
switch_mutex_lock(_mutex);
|
||||
|
||||
if (!_signaled)
|
||||
switch_thread_cond_wait(_condition, _mutex);
|
||||
|
||||
_signaled = false;
|
||||
|
||||
switch_mutex_unlock(_mutex);
|
||||
}
|
||||
|
||||
bool wait(unsigned int);
|
||||
|
||||
void reset(void)
|
||||
{
|
||||
switch_mutex_lock(_mutex);
|
||||
|
||||
_signaled = false;
|
||||
|
||||
switch_mutex_unlock(_mutex);
|
||||
}
|
||||
|
||||
BaseMutexType * mutex() { return _mutex; };
|
||||
BaseConditionType * condition() { return _condition; };
|
||||
|
||||
protected:
|
||||
|
||||
BaseConditionType *_condition;
|
||||
BaseMutexType *_mutex;
|
||||
switch_memory_pool_t *_pool;
|
||||
bool _can_delete_pool;
|
||||
};
|
||||
|
||||
#endif /* _SAVED_CONDITION_ */
|
||||
|
||||
177
src/mod/endpoints/mod_khomp/commons/freeswitch/simple_lock.hpp
Normal file
177
src/mod/endpoints/mod_khomp/commons/freeswitch/simple_lock.hpp
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _SIMPLE_LOCK_HPP_
|
||||
#define _SIMPLE_LOCK_HPP_
|
||||
|
||||
#include <simple_lock.hpp>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <switch.h>
|
||||
}
|
||||
|
||||
template < typename Implementor >
|
||||
struct SimpleLockBasic: public SimpleLockCommon < Implementor >
|
||||
{
|
||||
typedef SimpleLockCommon < Implementor > Super;
|
||||
typedef typename Super::Result Result;
|
||||
|
||||
typedef switch_mutex_t BaseMutexType;
|
||||
|
||||
SimpleLockBasic(switch_memory_pool_t *pool = NULL)
|
||||
: _pool(pool), _can_delete_pool( (_pool == NULL) )
|
||||
|
||||
{
|
||||
if(!_pool)
|
||||
switch_core_new_memory_pool(&_pool);
|
||||
|
||||
//switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool);
|
||||
switch_mutex_init(&_mutex, SWITCH_MUTEX_NESTED, _pool);
|
||||
}
|
||||
|
||||
virtual ~SimpleLockBasic()
|
||||
{
|
||||
/* do nothing */
|
||||
};
|
||||
|
||||
void unreference()
|
||||
{
|
||||
switch_mutex_destroy(_mutex);
|
||||
|
||||
if (_can_delete_pool)
|
||||
switch_core_destroy_memory_pool(&_pool);
|
||||
}
|
||||
|
||||
Result trylock()
|
||||
{
|
||||
switch (switch_mutex_trylock(_mutex))
|
||||
{
|
||||
case SWITCH_STATUS_SUCCESS:
|
||||
return Super::SUCCESS;
|
||||
case SWITCH_STATUS_FALSE:
|
||||
case SWITCH_STATUS_TERM:
|
||||
case SWITCH_STATUS_NOTIMPL:
|
||||
case SWITCH_STATUS_MEMERR:
|
||||
case SWITCH_STATUS_GENERR:
|
||||
case SWITCH_STATUS_SOCKERR:
|
||||
case SWITCH_STATUS_NOTFOUND:
|
||||
case SWITCH_STATUS_UNLOAD:
|
||||
case SWITCH_STATUS_NOUNLOAD:
|
||||
case SWITCH_STATUS_NOT_INITALIZED:
|
||||
return Super::FAILURE;
|
||||
//case SWITCH_STATUS_INUSE:
|
||||
default:
|
||||
return Super::ISINUSE;
|
||||
}
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
switch_mutex_unlock(_mutex);
|
||||
}
|
||||
|
||||
BaseMutexType * mutex() { return _mutex; };
|
||||
|
||||
protected:
|
||||
BaseMutexType *_mutex;
|
||||
switch_memory_pool_t *_pool;
|
||||
bool _can_delete_pool;
|
||||
};
|
||||
|
||||
struct SimpleLock: public SimpleLockBasic < SimpleLock >
|
||||
{
|
||||
typedef SimpleLockBasic < SimpleLock > Super;
|
||||
typedef Super::Result Result;
|
||||
|
||||
SimpleLock(switch_memory_pool_t *pool = NULL)
|
||||
: Super(pool) {};
|
||||
|
||||
Result lock()
|
||||
{
|
||||
switch (switch_mutex_lock(_mutex))
|
||||
{
|
||||
case SWITCH_STATUS_SUCCESS:
|
||||
return Super::SUCCESS;
|
||||
case SWITCH_STATUS_FALSE:
|
||||
case SWITCH_STATUS_TERM:
|
||||
case SWITCH_STATUS_NOTIMPL:
|
||||
case SWITCH_STATUS_MEMERR:
|
||||
case SWITCH_STATUS_GENERR:
|
||||
case SWITCH_STATUS_SOCKERR:
|
||||
case SWITCH_STATUS_NOTFOUND:
|
||||
case SWITCH_STATUS_UNLOAD:
|
||||
case SWITCH_STATUS_NOUNLOAD:
|
||||
case SWITCH_STATUS_NOT_INITALIZED:
|
||||
return Super::FAILURE;
|
||||
//case SWITCH_STATUS_INUSE:
|
||||
default:
|
||||
return Super::ISINUSE;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template < unsigned int Retries = 10, unsigned int Interval = 50 >
|
||||
struct SimpleNonBlockLock: public SimpleLockBasic < SimpleNonBlockLock < Retries, Interval > >
|
||||
{
|
||||
typedef SimpleLockBasic < SimpleNonBlockLock < Retries, Interval > > Super;
|
||||
typedef typename Super::Result Result;
|
||||
|
||||
SimpleNonBlockLock(switch_memory_pool_t *pool = NULL)
|
||||
: Super(pool) {};
|
||||
|
||||
inline Result lock()
|
||||
{
|
||||
for (unsigned int i = 0; i < Retries; i++)
|
||||
{
|
||||
Result ret = Super::trylock();
|
||||
|
||||
if (ret != Super::ISINUSE)
|
||||
return ret;
|
||||
|
||||
usleep(Interval * 1000);
|
||||
}
|
||||
|
||||
return Super::ISINUSE;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* _SIMPLE_LOCK_HPP_ */
|
||||
327
src/mod/endpoints/mod_khomp/commons/freeswitch/thread.hpp
Normal file
327
src/mod/endpoints/mod_khomp/commons/freeswitch/thread.hpp
Normal file
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _THREAD_HPP_
|
||||
#define _THREAD_HPP_
|
||||
|
||||
#include <thread.hpp>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <switch.h>
|
||||
}
|
||||
|
||||
struct Thread : ThreadCommon
|
||||
{
|
||||
typedef switch_thread_t BaseThreadType;
|
||||
|
||||
template<typename T, typename R, typename A>
|
||||
struct ThreadData : ThreadDataCommon
|
||||
{
|
||||
ThreadData(T data, A arg) : _data(data), _arg(arg) {}
|
||||
|
||||
int run()
|
||||
{
|
||||
return _data(_arg);
|
||||
}
|
||||
|
||||
T _data;
|
||||
A _arg;
|
||||
};
|
||||
|
||||
template<typename T, typename R>
|
||||
struct ThreadData < T, R, void > : ThreadDataCommon
|
||||
{
|
||||
ThreadData(T data) : _data(data) {}
|
||||
|
||||
int run()
|
||||
{
|
||||
return _data();
|
||||
}
|
||||
|
||||
T _data;
|
||||
};
|
||||
|
||||
template<typename T, typename A>
|
||||
struct ThreadData < T, void, A > : ThreadDataCommon
|
||||
{
|
||||
ThreadData(T data, A arg) : _data(data), _arg(arg) {}
|
||||
|
||||
int run()
|
||||
{
|
||||
_data(_arg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
T _data;
|
||||
A _arg;
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct ThreadData < T, void, void > : ThreadDataCommon
|
||||
{
|
||||
ThreadData(T data) : _data(data) {}
|
||||
|
||||
int run()
|
||||
{
|
||||
_data();
|
||||
return 0;
|
||||
}
|
||||
|
||||
T _data;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Thread(T obj, switch_memory_pool_t *pool=NULL) :
|
||||
_thread_info(new ThreadData<T, typename DecomposeFunction<T>::Return, void>(obj)),
|
||||
_pool(pool),
|
||||
_can_delete_pool(false)
|
||||
{
|
||||
if(!_pool)
|
||||
{
|
||||
switch_core_new_memory_pool(&_pool);
|
||||
_can_delete_pool = true;
|
||||
}
|
||||
|
||||
_thread_info->_thread = this;
|
||||
_thread_info->_self = NULL;
|
||||
_thread_info->_attribute = NULL;
|
||||
|
||||
if(switch_threadattr_create(
|
||||
(switch_threadattr_t **)&_thread_info->_attribute, _pool) != 0)
|
||||
{
|
||||
_thread_info->_attribute = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
switch_threadattr_stacksize_set(
|
||||
(switch_threadattr_t *)_thread_info->_attribute,
|
||||
SWITCH_THREAD_STACKSIZE);
|
||||
|
||||
if(!priority())
|
||||
{
|
||||
_thread_info->_attribute = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T, typename A>
|
||||
Thread(T obj, A arg, switch_memory_pool_t *pool=NULL) :
|
||||
_thread_info(new ThreadData<T, typename DecomposeFunction<T>::Return, A>(obj, arg)),
|
||||
_pool(pool),
|
||||
_can_delete_pool(false)
|
||||
{
|
||||
if(!_pool)
|
||||
{
|
||||
switch_core_new_memory_pool(&_pool);
|
||||
_can_delete_pool = true;
|
||||
}
|
||||
|
||||
_thread_info->_thread = this;
|
||||
_thread_info->_self = NULL;
|
||||
_thread_info->_attribute = NULL;
|
||||
|
||||
if(switch_threadattr_create(
|
||||
(switch_threadattr_t **)&_thread_info->_attribute, _pool) != 0)
|
||||
{
|
||||
_thread_info->_attribute = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
switch_threadattr_stacksize_set(
|
||||
(switch_threadattr_t *)_thread_info->_attribute,
|
||||
SWITCH_THREAD_STACKSIZE);
|
||||
|
||||
if(!priority())
|
||||
{
|
||||
_thread_info->_attribute = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
~Thread()
|
||||
{
|
||||
if(_thread_info)
|
||||
delete _thread_info;
|
||||
|
||||
if (_can_delete_pool)
|
||||
switch_core_destroy_memory_pool(&_pool);
|
||||
}
|
||||
|
||||
void detach(bool d = true)
|
||||
{
|
||||
if(!_thread_info->_attribute)
|
||||
return;
|
||||
|
||||
/* Non-zero if detached threads should be created. */
|
||||
switch_threadattr_detach_set(
|
||||
(switch_threadattr_t *)_thread_info->_attribute, d ? 1 : 0);
|
||||
}
|
||||
|
||||
bool start()
|
||||
{
|
||||
if(!_pool || !_thread_info->_attribute)
|
||||
return false;
|
||||
|
||||
switch_thread_create((switch_thread_t**)&_thread_info->_self,
|
||||
(switch_threadattr_t *)_thread_info->_attribute,
|
||||
run,
|
||||
_thread_info,
|
||||
_pool);
|
||||
|
||||
if(!_thread_info->_self)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int join()
|
||||
{
|
||||
/*
|
||||
* block until the desired thread stops executing.
|
||||
* @param retval The return value from the dead thread.
|
||||
* @param thd The thread to join
|
||||
*
|
||||
* SWITCH_DECLARE(switch_status_t) switch_thread_join(switch_status_t *retval, switch_thread_t *thd);
|
||||
*/
|
||||
|
||||
if(!_thread_info->_self)
|
||||
return -2;
|
||||
|
||||
int retval = 0;
|
||||
|
||||
if(switch_thread_join((switch_status_t*)&retval,
|
||||
(switch_thread_t *)_thread_info->_self) != 0)
|
||||
return -1;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
BaseThreadType * self()
|
||||
{
|
||||
//switch_thread_self();
|
||||
//apr_os_thread_current();
|
||||
return (BaseThreadType *)_thread_info->_self;
|
||||
}
|
||||
|
||||
private:
|
||||
void exit(int status)
|
||||
{
|
||||
/**
|
||||
* stop the current thread
|
||||
* @param thd The thread to stop
|
||||
* @param retval The return value to pass back to any thread that cares
|
||||
*/
|
||||
//SWITCH_DECLARE(switch_status_t) switch_thread_exit(switch_thread_t *thd, switch_status_t retval);
|
||||
switch_thread_exit((switch_thread_t *)_thread_info->_self, (switch_status_t)status);
|
||||
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
struct apr_threadattr_t {
|
||||
apr_pool_t *pool;
|
||||
pthread_attr_t attr;
|
||||
};
|
||||
#endif
|
||||
|
||||
bool priority()
|
||||
{
|
||||
#ifndef WIN32
|
||||
struct sched_param param;
|
||||
|
||||
struct apr_threadattr_t *myattr = (struct apr_threadattr_t *)_thread_info->_attribute;
|
||||
|
||||
if (pthread_attr_setschedpolicy(
|
||||
(pthread_attr_t *)&myattr->attr, SCHED_RR) < 0)
|
||||
return false;
|
||||
|
||||
if (pthread_attr_getschedparam(
|
||||
(pthread_attr_t *)&myattr->attr, ¶m) < 0)
|
||||
return false;
|
||||
|
||||
param.sched_priority = sched_get_priority_max(SCHED_RR);
|
||||
|
||||
if (pthread_attr_setschedparam(
|
||||
(pthread_attr_t *)&myattr->attr, ¶m) < 0)
|
||||
return false;
|
||||
|
||||
#endif
|
||||
return true;
|
||||
|
||||
/*
|
||||
//BUG in Freeswitch
|
||||
if(switch_threadattr_priority_increase(
|
||||
(switch_threadattr_t *)_thread_info->_attribute) != 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
ThreadDataCommon * _thread_info;
|
||||
switch_memory_pool_t *_pool;
|
||||
bool _can_delete_pool;
|
||||
|
||||
protected:
|
||||
|
||||
static void *SWITCH_THREAD_FUNC run(BaseThreadType *thread, void * obj)
|
||||
{
|
||||
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
|
||||
// "Starting new Thread\n");
|
||||
|
||||
ThreadDataCommon * data = (ThreadDataCommon *)obj;
|
||||
int retval = data->run();
|
||||
|
||||
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
|
||||
// "Stopping new Thread = %d\n", retval);
|
||||
|
||||
((Thread *)(data->_thread))->exit(retval);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* _THREAD_HPP_ */
|
||||
337
src/mod/endpoints/mod_khomp/commons/function.hpp
Normal file
337
src/mod/endpoints/mod_khomp/commons/function.hpp
Normal file
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the "LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <refcounter.hpp>
|
||||
|
||||
#ifndef _FUNCTION_HPP_
|
||||
#define _FUNCTION_HPP_
|
||||
|
||||
namespace Function
|
||||
{
|
||||
struct EmptyFunction {};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename FunctionTraits >
|
||||
struct StorageBase: NEW_REFCOUNTER(StorageBase < FunctionTraits >)
|
||||
{
|
||||
typedef typename FunctionTraits::BaseType BaseType;
|
||||
|
||||
typedef typename FunctionTraits::FunType FunType;
|
||||
typedef typename FunctionTraits::ObjType ObjType;
|
||||
|
||||
template < typename Functor >
|
||||
StorageBase(Functor f)
|
||||
: _object(reinterpret_cast<ObjType>(new Functor(f))),
|
||||
_function(reinterpret_cast<FunType>(&(Functor::operator()))),
|
||||
_malloced(true)
|
||||
{};
|
||||
|
||||
template < typename Functor >
|
||||
StorageBase(Functor & f, bool malloced)
|
||||
: _object(reinterpret_cast<ObjType>((malloced ? new Functor(f) : &f))),
|
||||
_function(reinterpret_cast<FunType>(&(Functor::operator()))),
|
||||
_malloced(malloced)
|
||||
{};
|
||||
|
||||
StorageBase()
|
||||
: _object(reinterpret_cast<ObjType>(0)),
|
||||
_function(reinterpret_cast<FunType>(0)),
|
||||
_malloced(false)
|
||||
{};
|
||||
|
||||
StorageBase(const StorageBase & o)
|
||||
: INC_REFCOUNTER(o, StorageBase < FunctionTraits >),
|
||||
_object(o._object), _function(o._function), _malloced(o._malloced)
|
||||
{};
|
||||
|
||||
virtual ~StorageBase() {};
|
||||
|
||||
void unreference()
|
||||
{
|
||||
// TODO: will this work if we delete a different type? //
|
||||
if (_malloced)
|
||||
delete _object;
|
||||
};
|
||||
|
||||
template < typename Functor >
|
||||
void operator=(Functor f)
|
||||
{
|
||||
_object = reinterpret_cast<ObjType>(new Functor(f)),
|
||||
_function = reinterpret_cast<FunType>(&(Functor::operator()));
|
||||
_malloced = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
ObjType _object;
|
||||
FunType _function;
|
||||
bool _malloced;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename R >
|
||||
struct VTable0
|
||||
{
|
||||
R operator()(void) { return R(); };
|
||||
};
|
||||
|
||||
template < >
|
||||
struct VTable0< void >
|
||||
{
|
||||
void operator()(void) { return; };
|
||||
};
|
||||
|
||||
template < typename R >
|
||||
struct Function0Traits
|
||||
{
|
||||
typedef VTable0<R> BaseType;
|
||||
|
||||
typedef R (BaseType::* FunType)(void);
|
||||
typedef BaseType * ObjType;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename R, typename A0 >
|
||||
struct VTable1
|
||||
{
|
||||
R operator()(A0 a0) { return R(); };
|
||||
};
|
||||
|
||||
template < typename A0 >
|
||||
struct VTable1< void, A0 >
|
||||
{
|
||||
void operator()(A0 a0) { return; };
|
||||
};
|
||||
|
||||
template < typename R, typename A0 >
|
||||
struct Function1Traits
|
||||
{
|
||||
typedef VTable1<R, A0> BaseType;
|
||||
|
||||
typedef R (BaseType::* FunType)(A0);
|
||||
typedef BaseType * ObjType;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename R, typename A0, typename A1 >
|
||||
struct VTable2
|
||||
{
|
||||
R operator()(A0 a0, A1) { return R(); };
|
||||
};
|
||||
|
||||
template < typename A0, typename A1 >
|
||||
struct VTable2< void, A0, A1 >
|
||||
{
|
||||
void operator()(A0 a0, A1 a1) { return; };
|
||||
};
|
||||
|
||||
template < typename R, typename A0, typename A1 >
|
||||
struct Function2Traits
|
||||
{
|
||||
typedef VTable2<R, A0, A1> BaseType;
|
||||
|
||||
typedef R (BaseType::* FunType)(A0, A1);
|
||||
typedef BaseType * ObjType;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename R, typename A0, typename A1, typename A2 >
|
||||
struct VTable3
|
||||
{
|
||||
R operator()(A0 a0, A1 a1, A2 a2) { return R(); };
|
||||
};
|
||||
|
||||
template < typename A0, typename A1, typename A2 >
|
||||
struct VTable3< void, A0, A1, A2 >
|
||||
{
|
||||
void operator()(A0 a0, A1 a1, A2 a2) { return; };
|
||||
};
|
||||
|
||||
template < typename R, typename A0, typename A1, typename A2 >
|
||||
struct Function3Traits
|
||||
{
|
||||
typedef VTable3<R, A0, A1, A2> BaseType;
|
||||
|
||||
typedef R (BaseType::* FunType)(A0, A1, A2);
|
||||
typedef BaseType * ObjType;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename R, typename A0, typename A1, typename A2, typename A3 >
|
||||
struct VTable4
|
||||
{
|
||||
R operator()(A0 a0, A1 a1, A2 a2, A3 a3) { return R(); };
|
||||
};
|
||||
|
||||
template < typename A0, typename A1, typename A2, typename A3 >
|
||||
struct VTable4< void, A0, A1, A2, A3 >
|
||||
{
|
||||
void operator()(A0 a0, A1 a1, A2 a2, A3 a3) { return; };
|
||||
};
|
||||
|
||||
template < typename R, typename A0, typename A1, typename A2, typename A3 >
|
||||
struct Function4Traits
|
||||
{
|
||||
typedef VTable4<R, A0, A1, A2, A3> BaseType;
|
||||
|
||||
typedef R (BaseType::* FunType)(A0, A1, A2, A3);
|
||||
typedef BaseType * ObjType;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
template < typename R, typename A0 >
|
||||
struct Function0 : public StorageBase < Function0Traits < R > >
|
||||
{
|
||||
typedef StorageBase < Function0Traits < R > > Storage;
|
||||
|
||||
template < typename Functor >
|
||||
Function0(Functor f): Storage(f) {};
|
||||
|
||||
template < typename Functor >
|
||||
Function0(Functor & f, bool m): Storage(f, m) {};
|
||||
|
||||
Function0() {};
|
||||
|
||||
R operator()(void)
|
||||
{
|
||||
if (reinterpret_cast<void *>(Storage::_object) == 0)
|
||||
throw EmptyFunction();
|
||||
|
||||
return ((Storage::_object)->*(Storage::_function))();
|
||||
}
|
||||
};
|
||||
|
||||
template < typename R, typename A0 >
|
||||
struct Function1 : public StorageBase < Function1Traits < R, A0 > >
|
||||
{
|
||||
typedef StorageBase < Function1Traits < R, A0 > > Storage;
|
||||
|
||||
template < typename Functor >
|
||||
Function1(Functor f): Storage(f) {};
|
||||
|
||||
template < typename Functor >
|
||||
Function1(Functor & f, bool m): Storage(f, m) {};
|
||||
|
||||
Function1() {};
|
||||
|
||||
R operator()(A0 a0)
|
||||
{
|
||||
if (reinterpret_cast<void *>(Storage::_object) == 0)
|
||||
throw EmptyFunction();
|
||||
|
||||
return ((Storage::_object)->*(Storage::_function))(a0);
|
||||
}
|
||||
};
|
||||
|
||||
template < typename R, typename A0, typename A1 >
|
||||
struct Function2 : public StorageBase < Function2Traits < R, A0, A1 > >
|
||||
{
|
||||
typedef StorageBase < Function2Traits < R, A0, A1 > > Storage;
|
||||
|
||||
template < typename Functor >
|
||||
Function2(Functor f): Storage(f) {};
|
||||
|
||||
template < typename Functor >
|
||||
Function2(Functor & f, bool m): Storage(f, m) {};
|
||||
|
||||
Function2() {};
|
||||
|
||||
R operator()(A0 a0, A1 a1)
|
||||
{
|
||||
if (reinterpret_cast<void *>(Storage::_object) == 0)
|
||||
throw EmptyFunction();
|
||||
|
||||
return ((Storage::_object)->*(Storage::_function))(a0, a1);
|
||||
}
|
||||
};
|
||||
|
||||
template < typename R, typename A0, typename A1, typename A2 >
|
||||
struct Function3 : public StorageBase < Function3Traits < R, A0, A1, A2 > >
|
||||
{
|
||||
typedef StorageBase < Function3Traits < R, A0, A1, A2 > > Storage;
|
||||
|
||||
template < typename Functor >
|
||||
Function3(Functor f): Storage(f) {};
|
||||
|
||||
template < typename Functor >
|
||||
Function3(Functor & f, bool m): Storage(f, m) {};
|
||||
|
||||
Function3() {};
|
||||
|
||||
R operator()(A0 a0, A1 a1, A2 a2)
|
||||
{
|
||||
if (reinterpret_cast<void *>(Storage::_object) == 0)
|
||||
throw EmptyFunction();
|
||||
|
||||
return ((Storage::_object)->*(Storage::_function))(a0, a1, a2);
|
||||
}
|
||||
};
|
||||
|
||||
template < typename R, typename A0, typename A1, typename A2, typename A3 >
|
||||
struct Function4 : public StorageBase < Function4Traits < R, A0, A1, A2, A3 > >
|
||||
{
|
||||
typedef StorageBase < Function4Traits < R, A0, A1, A2, A3 > > Storage;
|
||||
|
||||
template < typename Functor >
|
||||
Function4(Functor f): Storage(f) {};
|
||||
|
||||
template < typename Functor >
|
||||
Function4(Functor & f, bool m): Storage(f, m) {};
|
||||
|
||||
Function4() {};
|
||||
|
||||
R operator()(A0 a0, A1 a1, A2 a2, A3 a3)
|
||||
{
|
||||
if (reinterpret_cast<void *>(Storage::_object) == 0)
|
||||
throw EmptyFunction();
|
||||
|
||||
return ((Storage::_object)->*(Storage::_function))(a0, a1, a2, a3);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* _FUNCTION_HPP_ */
|
||||
80
src/mod/endpoints/mod_khomp/commons/initializer.hpp
Normal file
80
src/mod/endpoints/mod_khomp/commons/initializer.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
|
||||
#ifndef _INITIALIZER_HPP_
|
||||
#define _INITIALIZER_HPP_
|
||||
|
||||
template < typename Type >
|
||||
struct Initializer: public std::vector< Type >
|
||||
{
|
||||
typedef std::vector< Type > super;
|
||||
|
||||
Initializer(Type e) { push_back(e); };
|
||||
Initializer(Type & e) { push_back(e); };
|
||||
|
||||
Initializer & operator&(Initializer v)
|
||||
{
|
||||
insert(super::end(), v.begin(), v.end());
|
||||
return *this;
|
||||
};
|
||||
|
||||
Initializer & operator&(Initializer & v)
|
||||
{
|
||||
insert(super::end(), v.begin(), v.end());
|
||||
return *this;
|
||||
};
|
||||
|
||||
Initializer & operator&(Type v)
|
||||
{
|
||||
insert(super::end(), v);
|
||||
return *this;
|
||||
};
|
||||
|
||||
Initializer & operator&(Type & v)
|
||||
{
|
||||
insert(super::end(), v);
|
||||
return *this;
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* _INITIALIZER_HPP_ */
|
||||
313
src/mod/endpoints/mod_khomp/commons/k3lapi.cpp
Normal file
313
src/mod/endpoints/mod_khomp/commons/k3lapi.cpp
Normal file
@@ -0,0 +1,313 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <k3lapi.hpp>
|
||||
|
||||
#include <format.hpp>
|
||||
#include <verbose.hpp>
|
||||
|
||||
K3LAPI::K3LAPI(bool has_exceptions)
|
||||
: _has_exceptions(has_exceptions),
|
||||
_device_count(0), _channel_count(0), _link_count(0),
|
||||
_device_config(0), _channel_config(0), _link_config(0)
|
||||
{};
|
||||
|
||||
/* initialize the whole thing! */
|
||||
|
||||
void K3LAPI::start(void)
|
||||
{
|
||||
/* tie the used k3l to the compiled k3l version */
|
||||
char *ret = k3lStart(k3lApiMajorVersion, k3lApiMinorVersion, 0); //k3lApiBuildVersion);
|
||||
|
||||
if (ret && *ret)
|
||||
throw start_failed(ret);
|
||||
|
||||
/* call init automagically */
|
||||
init();
|
||||
}
|
||||
|
||||
void K3LAPI::stop(void)
|
||||
{
|
||||
k3lStop();
|
||||
fini();
|
||||
}
|
||||
|
||||
/* envio de comandos para placa */
|
||||
|
||||
void K3LAPI::mixer(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
|
||||
{
|
||||
KMixerCommand mix;
|
||||
|
||||
mix.Track = track;
|
||||
mix.Source = src;
|
||||
mix.SourceIndex = index;
|
||||
|
||||
command(dev, obj, CM_MIXER, (const char *) &mix);
|
||||
}
|
||||
|
||||
void K3LAPI::mixerRecord(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
|
||||
{
|
||||
/* estes buffers *NAO PODEM SER ESTATICOS*! */
|
||||
char cmd[] = { 0x3f, 0x03, (char)obj, (char)track, 0xff, 0xff };
|
||||
|
||||
switch (src)
|
||||
{
|
||||
case kmsChannel:
|
||||
cmd[4] = 0x05;
|
||||
cmd[5] = (char)index;
|
||||
break;
|
||||
|
||||
case kmsNoDelayChannel:
|
||||
cmd[4] = 0x0a;
|
||||
cmd[5] = (char)index;
|
||||
break;
|
||||
|
||||
case kmsGenerator:
|
||||
cmd[4] = 0x09;
|
||||
|
||||
switch ((KMixerTone)index)
|
||||
{
|
||||
case kmtSilence:
|
||||
cmd[5] = 0x0F;
|
||||
break;
|
||||
case kmtDial:
|
||||
cmd[5] = 0x08;
|
||||
break;
|
||||
case kmtBusy:
|
||||
cmd[5] = 0x0D;
|
||||
break;
|
||||
|
||||
case kmtFax:
|
||||
case kmtVoice:
|
||||
case kmtEndOf425:
|
||||
case kmtCollect:
|
||||
case kmtEndOfDtmf:
|
||||
/* TODO: exception, unable to generate */
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case kmsCTbus:
|
||||
case kmsPlay:
|
||||
/* TODO: exception, not implemented! */
|
||||
break;
|
||||
}
|
||||
|
||||
int32 dsp = get_dsp(dev, DSP_AUDIO);
|
||||
|
||||
raw_command(dev, dsp, cmd, sizeof(cmd));
|
||||
}
|
||||
|
||||
void K3LAPI::mixerCTbus(int32 dev, int32 obj, byte track, KMixerSource src, int32 index)
|
||||
{
|
||||
KMixerCommand mix;
|
||||
|
||||
mix.Track = track;
|
||||
mix.Source = src;
|
||||
mix.SourceIndex = index;
|
||||
|
||||
command(dev, obj, CM_MIXER_CTBUS, (const char *) &mix);
|
||||
}
|
||||
|
||||
void K3LAPI::command(int32 dev, int32 obj, int32 code, std::string & str)
|
||||
{
|
||||
command(dev, obj, code, str.c_str());
|
||||
}
|
||||
|
||||
void K3LAPI::command (int32 dev, int32 obj, int32 code, const char * parms)
|
||||
{
|
||||
K3L_COMMAND cmd;
|
||||
|
||||
cmd.Cmd = code;
|
||||
cmd.Object = obj;
|
||||
cmd.Params = (byte *)parms;
|
||||
|
||||
int32 rc = k3lSendCommand(dev, &cmd);
|
||||
|
||||
if (rc != ksSuccess)
|
||||
throw failed_command(code, dev, obj, rc);
|
||||
}
|
||||
|
||||
void K3LAPI::raw_command(int32 dev, int32 dsp, std::string & str)
|
||||
{
|
||||
raw_command(dev, dsp, str.data(), str.size());
|
||||
}
|
||||
|
||||
void K3LAPI::raw_command(int32 dev, int32 dsp, const char * cmds, int32 size)
|
||||
{
|
||||
std::string str(cmds, size);
|
||||
|
||||
int32 rc = k3lSendRawCommand(dev, dsp, (void *)cmds, size);
|
||||
|
||||
if (rc != ksSuccess)
|
||||
throw failed_raw_command(dev, dsp, rc);
|
||||
}
|
||||
|
||||
KLibraryStatus K3LAPI::get_param(K3L_EVENT *ev, const char *name, std::string &res)
|
||||
{
|
||||
char tmp_param[256];
|
||||
memset((void*)tmp_param, 0, sizeof(tmp_param));
|
||||
|
||||
int32 rc = k3lGetEventParam (ev, (sbyte *)name, (sbyte *)tmp_param, sizeof(tmp_param)-1);
|
||||
|
||||
if (rc != ksSuccess)
|
||||
return (KLibraryStatus)rc;
|
||||
|
||||
res.append(tmp_param, strlen(tmp_param));
|
||||
return ksSuccess;
|
||||
}
|
||||
|
||||
std::string K3LAPI::get_param(K3L_EVENT *ev, const char *name)
|
||||
{
|
||||
std::string res;
|
||||
|
||||
KLibraryStatus rc = get_param(ev, name, res);
|
||||
|
||||
if (rc != ksSuccess)
|
||||
throw get_param_failed(name, rc);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void K3LAPI::init(void)
|
||||
{
|
||||
if (_device_count != 0) return;
|
||||
|
||||
_device_count = k3lGetDeviceCount();
|
||||
|
||||
_device_type = new KDeviceType[_device_count];
|
||||
_device_config = new device_conf_type[_device_count];
|
||||
_channel_config = new channel_ptr_conf_type[_device_count];
|
||||
_link_config = new link_ptr_conf_type[_device_count];
|
||||
_channel_count = new unsigned int[_device_count];
|
||||
_link_count = new unsigned int[_device_count];
|
||||
|
||||
for (unsigned int dev = 0; dev < _device_count; dev++)
|
||||
{
|
||||
KLibraryStatus ret = ksSuccess;
|
||||
|
||||
_device_type[dev] = (KDeviceType) k3lGetDeviceType(dev);
|
||||
|
||||
/* caches each device config */
|
||||
ret = (KLibraryStatus)k3lGetDeviceConfig(dev, ksoDevice + dev, &(_device_config[dev]), sizeof(_device_config[dev]));
|
||||
|
||||
if (ret != ksSuccess)
|
||||
throw start_failed(STG(FMT("k3lGetDeviceConfig(dev=%d): %s") % dev % Verbose::status(ret)));
|
||||
|
||||
/* adjust channel/link count for device */
|
||||
_channel_count[dev] = _device_config[dev].ChannelCount;
|
||||
_link_count[dev] = _device_config[dev].LinkCount;
|
||||
|
||||
/* caches each channel config */
|
||||
_channel_config[dev] = new channel_conf_type[_channel_count[dev]];
|
||||
|
||||
for (unsigned int obj = 0; obj < _channel_count[dev]; obj++)
|
||||
{
|
||||
ret = (KLibraryStatus)k3lGetDeviceConfig(dev, ksoChannel + obj, &(_channel_config[dev][obj]), sizeof(_channel_config[dev][obj]));
|
||||
|
||||
if (ret != ksSuccess)
|
||||
throw start_failed(STG(FMT("k3lGetDeviceConfig(dev=%d,chan=%d): %s") % dev % obj % Verbose::status(ret)));
|
||||
}
|
||||
|
||||
/* adjust link count for device */
|
||||
_link_count[dev] = _device_config[dev].LinkCount;
|
||||
|
||||
/* caches each link config */
|
||||
_link_config[dev] = new link_conf_type[_link_count[dev]];
|
||||
|
||||
for (unsigned int obj = 0; obj < _link_count[dev]; obj++)
|
||||
{
|
||||
ret = (KLibraryStatus)k3lGetDeviceConfig(dev, ksoLink + obj, &(_link_config[dev][obj]), sizeof(_link_config[dev][obj]));
|
||||
|
||||
if (ret != ksSuccess)
|
||||
throw start_failed(STG(FMT("k3lGetDeviceConfig(dev=%d,link=%d): %s") % dev % obj % Verbose::status(ret)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void K3LAPI::fini(void)
|
||||
{
|
||||
for (unsigned int dev = 0; dev < _device_count; dev++)
|
||||
{
|
||||
if (_channel_config[dev])
|
||||
{
|
||||
delete[] _channel_config[dev];
|
||||
_channel_config[dev] = NULL;
|
||||
}
|
||||
|
||||
if (_link_config[dev])
|
||||
{
|
||||
delete[] _link_config[dev];
|
||||
_link_config[dev] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
_device_count = 0;
|
||||
|
||||
if (_device_type) { delete[] _device_type; _device_type = NULL; }
|
||||
if (_device_config) { delete[] _device_config; _device_config = NULL; }
|
||||
if (_channel_config) { delete[] _channel_config; _channel_config = NULL; }
|
||||
if (_link_config) { delete[] _link_config; _link_config = NULL; }
|
||||
if (_channel_count) { delete[] _channel_count; _channel_count = NULL; }
|
||||
if (_link_count) { delete[] _link_count; _link_count = NULL; }
|
||||
}
|
||||
|
||||
int32 K3LAPI::get_dsp(int32 dev, K3LAPI::DspType type)
|
||||
{
|
||||
switch (device_type(dev))
|
||||
{
|
||||
case kdtFXO:
|
||||
case kdtFXOVoIP:
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
case kdtGSM:
|
||||
case kdtGSMSpx:
|
||||
#endif
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
case kdtGSMUSB:
|
||||
case kdtGSMUSBSpx:
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return (type == DSP_AUDIO ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
408
src/mod/endpoints/mod_khomp/commons/k3lapi.hpp
Normal file
408
src/mod/endpoints/mod_khomp/commons/k3lapi.hpp
Normal file
@@ -0,0 +1,408 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <k3l.h>
|
||||
|
||||
/* if using full k3l.h (for softpbx), version already defined. */
|
||||
#ifndef k3lApiMajorVersion
|
||||
# include <k3lVersion.h>
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC_PREREQ
|
||||
#if __GNUC_PREREQ(4,3)
|
||||
#include <cstring>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <types.hpp>
|
||||
|
||||
#ifndef _K3LAPI_HPP_
|
||||
#define _K3LAPI_HPP_
|
||||
|
||||
struct K3LAPI
|
||||
{
|
||||
/* exceptions */
|
||||
|
||||
struct start_failed
|
||||
{
|
||||
start_failed(const char * _msg) : msg(_msg) {};
|
||||
start_failed(std::string _msg) : msg(_msg) {};
|
||||
std::string msg;
|
||||
};
|
||||
|
||||
struct failed_command
|
||||
{
|
||||
failed_command(int32 _code, unsigned short _dev, unsigned short _obj, int32 _rc)
|
||||
: code(_code), dev(_dev), obj(_obj), rc(_rc) {};
|
||||
|
||||
int32 code;
|
||||
unsigned short dev;
|
||||
unsigned short obj;
|
||||
int32 rc;
|
||||
};
|
||||
|
||||
struct failed_raw_command
|
||||
{
|
||||
failed_raw_command(unsigned short _dev, unsigned short _dsp, int32 _rc)
|
||||
: dev(_dev), dsp(_dsp), rc(_rc) {};
|
||||
|
||||
unsigned short dev;
|
||||
unsigned short dsp;
|
||||
int32 rc;
|
||||
};
|
||||
|
||||
struct invalid_device
|
||||
{
|
||||
invalid_device(int32 _device)
|
||||
: device(_device) {};
|
||||
|
||||
int32 device;
|
||||
};
|
||||
|
||||
struct invalid_channel
|
||||
{
|
||||
invalid_channel(int32 _device, int32 _channel)
|
||||
: device(_device), channel(_channel) {};
|
||||
|
||||
int32 device, channel;
|
||||
};
|
||||
|
||||
struct invalid_link
|
||||
{
|
||||
invalid_link(unsigned int _device, unsigned int _link)
|
||||
: device(_device), link(_link) {};
|
||||
|
||||
int32 device, link;
|
||||
};
|
||||
|
||||
struct get_param_failed
|
||||
{
|
||||
get_param_failed(std::string _name, int32 _rc)
|
||||
: name(_name), rc((KLibraryStatus)_rc) {};
|
||||
|
||||
std::string name;
|
||||
KLibraryStatus rc;
|
||||
};
|
||||
|
||||
typedef K3L_DEVICE_CONFIG device_conf_type;
|
||||
typedef K3L_CHANNEL_CONFIG channel_conf_type;
|
||||
typedef K3L_CHANNEL_CONFIG * channel_ptr_conf_type;
|
||||
typedef K3L_LINK_CONFIG link_conf_type;
|
||||
typedef K3L_LINK_CONFIG * link_ptr_conf_type;
|
||||
|
||||
/* constructors/destructors */
|
||||
|
||||
K3LAPI(bool has_exceptions = false);
|
||||
virtual ~K3LAPI() {};
|
||||
|
||||
/* (init|final)ialize the whole thing! */
|
||||
|
||||
void start(void);
|
||||
void stop(void);
|
||||
|
||||
/* verificacao de intervalos */
|
||||
|
||||
inline bool valid_device(int32 dev)
|
||||
{
|
||||
return (dev >= 0 && dev < ((int32)_device_count));
|
||||
}
|
||||
|
||||
inline bool valid_channel(int32 dev, int32 obj)
|
||||
{
|
||||
return (valid_device(dev) && obj >= 0 && obj < ((int32)_channel_count[dev]));
|
||||
}
|
||||
|
||||
inline bool valid_link(int32 dev, int32 obj)
|
||||
{
|
||||
return (valid_device(dev) && obj >= 0 && obj < ((int32)_link_count[dev]));
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief High level object identifier
|
||||
Since Khomp works with an object concept, this is used to map the
|
||||
object id with its proper type.
|
||||
*/
|
||||
struct target
|
||||
{
|
||||
/*! The types a target can have */
|
||||
typedef enum { DEVICE, CHANNEL, MIXER, LINK } target_type;
|
||||
|
||||
target(K3LAPI & k3lapi, target_type type_init, int32 device_value, int32 object_value)
|
||||
: type(type_init),
|
||||
device((unsigned short)device_value),
|
||||
object((unsigned short)object_value)
|
||||
{
|
||||
switch (type_init)
|
||||
{
|
||||
case DEVICE:
|
||||
if (!k3lapi.valid_device(device_value))
|
||||
throw invalid_device(device_value);
|
||||
break;
|
||||
|
||||
case CHANNEL:
|
||||
case MIXER:
|
||||
if (!k3lapi.valid_channel(device_value, object_value))
|
||||
throw invalid_channel(device_value, object_value);
|
||||
break;
|
||||
|
||||
case LINK:
|
||||
if (!k3lapi.valid_link(device_value, object_value))
|
||||
throw invalid_link(device_value, object_value);
|
||||
break;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
const target_type type;
|
||||
|
||||
const unsigned short device;
|
||||
const unsigned short object;
|
||||
};
|
||||
|
||||
/* envio de comandos para placa (geral) */
|
||||
|
||||
void raw_command(int32 dev, int32 dsp, std::string & str);
|
||||
void raw_command(int32 dev, int32 dsp, const char * cmds, int32 size);
|
||||
|
||||
/* obter dados 'cacheados' (geral) */
|
||||
|
||||
inline unsigned int device_count(void)
|
||||
{
|
||||
return _device_count;
|
||||
}
|
||||
|
||||
/* envio de comandos para placa (sem identificadores) */
|
||||
|
||||
void mixer(int32 dev, int32 obj, byte track, KMixerSource src, int32 index);
|
||||
void mixerRecord(int32 dev, int32 obj, byte track, KMixerSource src, int32 index);
|
||||
void mixerCTbus(int32 dev, int32 obj, byte track, KMixerSource src, int32 index);
|
||||
|
||||
void command (int32 dev, int32 obj, int32 code, std::string & str);
|
||||
void command (int32 dev, int32 obj, int32 code, const char * parms = NULL);
|
||||
|
||||
/* obter dados 'cacheados' (sem identificadores) */
|
||||
|
||||
inline unsigned int channel_count(int32 dev)
|
||||
{
|
||||
if (!valid_device(dev))
|
||||
{
|
||||
if (_has_exceptions)
|
||||
throw invalid_device(dev);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _channel_count[dev];
|
||||
}
|
||||
|
||||
inline unsigned int link_count(int32 dev)
|
||||
{
|
||||
if (!valid_device(dev))
|
||||
{
|
||||
if (_has_exceptions)
|
||||
throw invalid_device(dev);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _link_count[dev];
|
||||
}
|
||||
|
||||
inline uint32 channel_stats(int32 dev, int32 obj, uint32 index)
|
||||
{
|
||||
if (!valid_channel(dev, obj))
|
||||
{
|
||||
if (_has_exceptions)
|
||||
throw invalid_channel(dev, obj);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32 res_value = (uint32)-1;
|
||||
stt_code stt_res = ksFail;
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
stt_res = k3lGetChannelStats(dev, obj, index, &res_value);
|
||||
#endif
|
||||
|
||||
if(stt_res != ksSuccess)
|
||||
{
|
||||
return (uint32)-1;
|
||||
}
|
||||
|
||||
return res_value;
|
||||
}
|
||||
|
||||
KDeviceType device_type(int32 dev)
|
||||
{
|
||||
if (!valid_device(dev))
|
||||
{
|
||||
if (_has_exceptions)
|
||||
throw invalid_device(dev);
|
||||
else
|
||||
return kdtDevTypeCount;
|
||||
}
|
||||
|
||||
return _device_type[dev];
|
||||
}
|
||||
|
||||
|
||||
K3L_DEVICE_CONFIG & device_config(int32 dev)
|
||||
{
|
||||
if (!valid_device(dev))
|
||||
throw invalid_device(dev);
|
||||
|
||||
return _device_config[dev];
|
||||
}
|
||||
|
||||
K3L_CHANNEL_CONFIG & channel_config(int32 dev, int32 obj)
|
||||
{
|
||||
if (!valid_channel(dev, obj))
|
||||
throw invalid_channel(dev, obj);
|
||||
|
||||
return _channel_config[dev][obj];
|
||||
}
|
||||
|
||||
K3L_LINK_CONFIG & link_config(int32 dev, int32 obj)
|
||||
{
|
||||
if (!valid_link(dev, obj))
|
||||
throw invalid_channel(dev, obj);
|
||||
|
||||
return _link_config[dev][obj];
|
||||
}
|
||||
|
||||
/* envio de comandos para placa (com identificadores) */
|
||||
|
||||
void mixer(target & tgt, byte track, KMixerSource src, int32 index)
|
||||
{
|
||||
mixer((int32)tgt.device, (int32)tgt.object, track, src, index);
|
||||
}
|
||||
|
||||
void mixerRecord(target & tgt, byte track, KMixerSource src, int32 index)
|
||||
{
|
||||
mixerRecord((int32)tgt.device, (int32)tgt.object, track, src, index);
|
||||
}
|
||||
|
||||
void mixerCTbus(target & tgt, byte track, KMixerSource src, int32 index)
|
||||
{
|
||||
mixerCTbus((int32)tgt.device, (int32)tgt.object, track, src, index);
|
||||
}
|
||||
|
||||
void command (target & tgt, int32 code, std::string & str)
|
||||
{
|
||||
command((int32)tgt.device, (int32)tgt.object, code, str);
|
||||
};
|
||||
|
||||
void command (target & tgt, int32 code, const char * parms = NULL)
|
||||
{
|
||||
command((int32)tgt.device, (int32)tgt.object, code, parms);
|
||||
};
|
||||
|
||||
/* obter dados 'cacheados' (com indentificadores) */
|
||||
|
||||
inline unsigned int channel_count(target & tgt)
|
||||
{
|
||||
return _channel_count[tgt.device];
|
||||
}
|
||||
|
||||
inline unsigned int link_count(target & tgt)
|
||||
{
|
||||
return _link_count[tgt.device];
|
||||
}
|
||||
|
||||
KDeviceType device_type(target & tgt)
|
||||
{
|
||||
return _device_type[tgt.device];
|
||||
}
|
||||
|
||||
|
||||
K3L_DEVICE_CONFIG & device_config(target & tgt)
|
||||
{
|
||||
return _device_config[tgt.device];
|
||||
}
|
||||
|
||||
K3L_CHANNEL_CONFIG & channel_config(target & tgt)
|
||||
{
|
||||
return _channel_config[tgt.device][tgt.object];
|
||||
}
|
||||
|
||||
K3L_LINK_CONFIG & link_config(target & tgt)
|
||||
{
|
||||
return _link_config[tgt.device][tgt.object];
|
||||
}
|
||||
|
||||
/* pega valores em strings de eventos */
|
||||
|
||||
KLibraryStatus get_param(K3L_EVENT *ev, const char *name, std::string &res);
|
||||
std::string get_param(K3L_EVENT *ev, const char *name);
|
||||
|
||||
/* inicializa valores em cache */
|
||||
|
||||
void init(void);
|
||||
void fini(void);
|
||||
|
||||
/* utilidades diversas e informacoes */
|
||||
|
||||
enum DspType
|
||||
{
|
||||
DSP_AUDIO,
|
||||
DSP_SIGNALING,
|
||||
};
|
||||
|
||||
int32 get_dsp(int32, DspType);
|
||||
|
||||
protected:
|
||||
|
||||
const bool _has_exceptions;
|
||||
|
||||
unsigned int _device_count;
|
||||
unsigned int * _channel_count;
|
||||
unsigned int * _link_count;
|
||||
|
||||
device_conf_type * _device_config;
|
||||
channel_ptr_conf_type * _channel_config;
|
||||
link_ptr_conf_type * _link_config;
|
||||
KDeviceType * _device_type;
|
||||
};
|
||||
|
||||
#endif /* _K3LAPI_HPP_ */
|
||||
234
src/mod/endpoints/mod_khomp/commons/k3lutil.cpp
Normal file
234
src/mod/endpoints/mod_khomp/commons/k3lutil.cpp
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <k3lutil.hpp>
|
||||
|
||||
std::string K3LUtil::channelStatus(int32 dev, int32 channel,
|
||||
Verbose::Presentation fmt)
|
||||
{
|
||||
try
|
||||
{
|
||||
K3L_CHANNEL_CONFIG & config = _k3lapi.channel_config(dev, channel);
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
KLibraryStatus ret = (KLibraryStatus) k3lGetDeviceStatus (dev,
|
||||
channel + ksoChannel, &status, sizeof(status));
|
||||
|
||||
switch (ret)
|
||||
{
|
||||
case ksSuccess: return Verbose::channelStatus(config.Signaling,
|
||||
status.AddInfo, fmt);
|
||||
default: return (fmt == Verbose::EXACT ? "<unknown[fail]>"
|
||||
: "Unknown (fail)");
|
||||
}
|
||||
}
|
||||
catch(K3LAPI::invalid_channel & e)
|
||||
{
|
||||
return (fmt == Verbose::EXACT ? "<unknown[fail]>" : "Unknown (fail)");
|
||||
}
|
||||
}
|
||||
|
||||
std::string K3LUtil::callStatus(int32 dev, int32 channel,
|
||||
Verbose::Presentation fmt)
|
||||
{
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
KLibraryStatus ret = (KLibraryStatus) k3lGetDeviceStatus(dev,
|
||||
channel + ksoChannel, &status, sizeof(status));
|
||||
|
||||
switch (ret)
|
||||
{
|
||||
case ksSuccess: return Verbose::callStatus(status.CallStatus, fmt);
|
||||
default: return (fmt == Verbose::EXACT ? "<unknown[fail]>"
|
||||
: "Unknown (fail)");
|
||||
}
|
||||
}
|
||||
|
||||
std::string K3LUtil::linkStatus(int32 dev, int32 link,
|
||||
Verbose::Presentation fmt, KSignaling sig)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (sig == ksigInactive)
|
||||
{
|
||||
K3L_LINK_CONFIG & config = _k3lapi.link_config(dev, link);
|
||||
sig = config.Signaling;
|
||||
}
|
||||
|
||||
K3L_LINK_STATUS status;
|
||||
|
||||
KLibraryStatus ret = (KLibraryStatus) k3lGetDeviceStatus (dev,
|
||||
link + ksoLink, &status, sizeof(status));
|
||||
|
||||
switch (ret)
|
||||
{
|
||||
case ksSuccess: return Verbose::linkStatus(sig, status.E1, fmt);
|
||||
default: return (fmt == Verbose::EXACT ?
|
||||
"<unknown[failure]>" : "Unknown (failure)");
|
||||
}
|
||||
}
|
||||
catch(K3LAPI::invalid_channel & e)
|
||||
{
|
||||
return (fmt == Verbose::EXACT ? "<unknown[failure]>"
|
||||
: "Unknown (failure)");
|
||||
}
|
||||
}
|
||||
|
||||
std::string K3LUtil::getLinkStatus(int32 dev, int32 link,
|
||||
Verbose::Presentation fmt)
|
||||
{
|
||||
switch (_k3lapi.device_type(dev))
|
||||
{
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
case kdtFXS:
|
||||
case kdtFXSSpx:
|
||||
return linkStatus(dev, link, fmt, ksigAnalogTerminal);
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
case kdtE1FXSSpx:
|
||||
if (link == 1)
|
||||
return linkStatus(dev, link, fmt, ksigAnalogTerminal);
|
||||
#endif
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
K3L_LINK_CONFIG & conf = _k3lapi.link_config(dev, link);
|
||||
|
||||
std::string res = linkStatus(dev, link, fmt);
|
||||
|
||||
if (conf.ReceivingClock & 0x01)
|
||||
res += (fmt == Verbose::EXACT ? ",sync" : " (sync)");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned int K3LUtil::physicalLinkCount(int32 dev, bool count_virtual)
|
||||
{
|
||||
unsigned int number = 0;
|
||||
|
||||
try
|
||||
{
|
||||
switch (_k3lapi.device_type(dev))
|
||||
{
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
case kdtFXS:
|
||||
number = (count_virtual ? (_k3lapi.channel_count(dev) < 50 ? 1 : 2) : 0);
|
||||
break;
|
||||
|
||||
case kdtFXSSpx:
|
||||
number = (count_virtual ? (_k3lapi.channel_count(dev) < 30 ? 1 : 2) : 0);
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
case kdtE1FXSSpx:
|
||||
number = (count_virtual ? 2 : 1);
|
||||
break;
|
||||
#endif
|
||||
|
||||
/* E1 boards */
|
||||
case kdtE1:
|
||||
case kdtE1Spx:
|
||||
case kdtE1IP:
|
||||
number = _k3lapi.link_count(dev);
|
||||
break;
|
||||
|
||||
case kdtPR:
|
||||
case kdtE1GW:
|
||||
number = 1;
|
||||
break;
|
||||
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
case kdtFXO:
|
||||
case kdtFXOVoIP:
|
||||
case kdtGSM:
|
||||
case kdtGSMSpx:
|
||||
#else
|
||||
case kdtFX:
|
||||
case kdtFXVoIP:
|
||||
#endif
|
||||
case kdtConf:
|
||||
case kdtGWIP:
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
case kdtGSMUSB:
|
||||
case kdtGSMUSBSpx:
|
||||
case kdtReserved1: // just to avoid warnings.
|
||||
case kdtDevTypeCount: // just to avoid warnings.
|
||||
#endif
|
||||
number = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(K3LAPI::invalid_device & e)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
|
||||
K3LUtil::ErrorCountType K3LUtil::linkErrorCount(int32 dev, int32 link,
|
||||
Verbose::Presentation fmt)
|
||||
{
|
||||
ErrorCountType result;
|
||||
K3L_LINK_ERROR_COUNTER status;
|
||||
|
||||
KLibraryStatus ret = (KLibraryStatus) k3lGetDeviceStatus (dev,
|
||||
link + ksoLinkMon, &status, sizeof(status));
|
||||
|
||||
switch (ret)
|
||||
{
|
||||
case ksSuccess:
|
||||
for (unsigned int i = klecChangesToLock; i < klecCount; i++)
|
||||
{
|
||||
result.push_back(ErrorCountPairType(Verbose::linkErrorCounter
|
||||
((KLinkErrorCounter)i, fmt), status.ErrorCounters[i]));
|
||||
}
|
||||
/* fall */
|
||||
|
||||
default:
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
82
src/mod/endpoints/mod_khomp/commons/k3lutil.hpp
Normal file
82
src/mod/endpoints/mod_khomp/commons/k3lutil.hpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <list>
|
||||
|
||||
#include <k3lapi.hpp>
|
||||
#include <verbose.hpp>
|
||||
|
||||
#ifndef _K3LUTIL_HPP_
|
||||
#define _K3LUTIL_HPP_
|
||||
|
||||
struct K3LUtil
|
||||
{
|
||||
typedef std::pair < std::string, unsigned int > ErrorCountPairType;
|
||||
typedef std::list < ErrorCountPairType > ErrorCountType;
|
||||
|
||||
K3LUtil(K3LAPI & k3lapi): _k3lapi(k3lapi) {};
|
||||
|
||||
std::string callStatus(int32 dev, int32 channel,
|
||||
Verbose::Presentation fmt = Verbose::HUMAN);
|
||||
|
||||
std::string channelStatus(int32, int32,
|
||||
Verbose::Presentation fmt = Verbose::HUMAN);
|
||||
|
||||
std::string linkStatus(int32, int32,
|
||||
Verbose::Presentation fmt = Verbose::HUMAN,
|
||||
KSignaling sig = ksigInactive);
|
||||
|
||||
std::string getLinkStatus(int32, int32,
|
||||
Verbose::Presentation fmt = Verbose::HUMAN);
|
||||
|
||||
unsigned int physicalLinkCount(int32 dev, bool count_virtual = false);
|
||||
|
||||
ErrorCountType linkErrorCount(int32, int32,
|
||||
Verbose::Presentation fmt = Verbose::HUMAN);
|
||||
|
||||
protected:
|
||||
K3LAPI & _k3lapi;
|
||||
};
|
||||
|
||||
#endif /* _K3LUTIL_HPP_ */
|
||||
|
||||
644
src/mod/endpoints/mod_khomp/commons/logger.hpp
Normal file
644
src/mod/endpoints/mod_khomp/commons/logger.hpp
Normal file
@@ -0,0 +1,644 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <tagged_union.hpp>
|
||||
#include <format.hpp>
|
||||
#include <refcounter.hpp>
|
||||
#include <flagger.hpp>
|
||||
|
||||
#if defined(COMMONS_LIBRARY_USING_ASTERISK)
|
||||
extern "C"
|
||||
{
|
||||
#include <asterisk/localtime.h>
|
||||
}
|
||||
#elif defined(COMMONS_LIBRARY_USING_CALLWEAVER)
|
||||
extern "C"
|
||||
{
|
||||
#include <callweaver/localtime.h>
|
||||
}
|
||||
#elif defined(COMMONS_LIBRARY_USING_FREESWITCH)
|
||||
extern "C"
|
||||
{
|
||||
#include <time.h>
|
||||
}
|
||||
#endif
|
||||
/*
|
||||
|
||||
********************************************************************************
|
||||
***************************** 'Logger' user manual *****************************
|
||||
********************************************************************************
|
||||
|
||||
* Description:
|
||||
|
||||
This class does the management of log messages for applications. It works with
|
||||
the following axioms:
|
||||
|
||||
<*> There are several class of messages.
|
||||
<*> There are some outputs, which may be files, sockets, or a console device.
|
||||
<*> There are options for classes, for outputs and for the association of both.
|
||||
|
||||
The last rule also shows the order in which options are processed: first the
|
||||
'classes' options are processed, then 'output' options are processed, and then
|
||||
the options for the tuple '(class, output)' are processed.
|
||||
|
||||
The options are mapped like this:
|
||||
|
||||
<class-of-message> -> options [prefix, flags]
|
||||
<output-sink> -> options [prefix]
|
||||
( <class-of-message>, <output-sink> ) -> options [prefix, flags]
|
||||
|
||||
- "prefix" means a fixed string prefix before the real message.
|
||||
- "flags" means auxiliary flags (DATETIME, THREADID) which are
|
||||
used to add information based on OS or process context info.
|
||||
|
||||
* Example of use:
|
||||
|
||||
typedef enum
|
||||
{
|
||||
C_DBG_FUNC,
|
||||
C_DBG_LOCK,
|
||||
C_WARNING,
|
||||
C_ERROR,
|
||||
C_CLI,
|
||||
}
|
||||
AstClassId;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
F_CONSOLE,
|
||||
F_GENERIC,
|
||||
F_TRACE,
|
||||
}
|
||||
AstOutputId;
|
||||
|
||||
// used to indicate the console log //
|
||||
struct AstConsoleLog {};
|
||||
|
||||
struct AstPrinter: public Logger::DefaultPrinter
|
||||
{
|
||||
typedef Logger::DefaultPrinter Super;
|
||||
|
||||
using namespace Tagged;
|
||||
|
||||
using Super::operator()(int);
|
||||
|
||||
// just 2 type of descriptors //
|
||||
typedef Union < int, Union < AstConsoleLog > > Container;
|
||||
|
||||
ast_printer(std::string & msg): Super(msg) {};
|
||||
|
||||
bool operator()(const AstConsoleLog & log)
|
||||
{
|
||||
ast_console_puts(_msg.c_str());
|
||||
return true;
|
||||
};
|
||||
|
||||
#if 0
|
||||
bool operator()(int log)
|
||||
{
|
||||
return Super::operator()(log);
|
||||
};
|
||||
#endif
|
||||
};
|
||||
|
||||
bool start_log()
|
||||
{
|
||||
typedef Logger::Manager<AstClassId, AstOutputId, AstPrinter, SimpleLock> LogManager;
|
||||
|
||||
LogManager logger;
|
||||
|
||||
// shortcut definition //
|
||||
typedef LogManager::Option LogOption;
|
||||
|
||||
FILE * log_file = fopen( "output.log", "a");
|
||||
|
||||
if (!log_file)
|
||||
return false;
|
||||
|
||||
logger.add( F_CONSOLE, AstConsoleLog(), "chan_khomp: ");
|
||||
logger.add( F_GENERIC, log_file);
|
||||
|
||||
logger.classe( C_WARNING )
|
||||
& LogOption(F_CONSOLE, "WARNING: ", LogOption::Flags(LogOption::Flag(LogOption::DATETIME)))
|
||||
& LogOption(F_GENERIC, "W: ", LogOption::Flags(LogOption::Flag(LogOption::DATETIME)))
|
||||
|
||||
logger.classe( C_DBG_LOCK ).enabled(false);
|
||||
|
||||
logger.classe( C_DBG_LOCK )
|
||||
& LogOption(F_GENERIC, "L: ", LogOption::Flags
|
||||
(LogOption::flag_type(LogOption::ENABLED) &
|
||||
LogOption::flag_type(LogOption::DATETIME))
|
||||
|
||||
logger(C_WARNING, "eu sou uma mensagem de warning");
|
||||
|
||||
logger.classe(C_WARNING).set(F_GENERIC, LogOption::ENABLED, true);
|
||||
logger.classe(C_WARNING).set(F_CONSOLE, LogOption::ENABLED, false);
|
||||
|
||||
logger.classe(C_CLI).prefix("<K>");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void message_the_user(int fd)
|
||||
{
|
||||
logger(C_CLI, fd, "eu sou uma mensagem de cli!");
|
||||
logger(C_WARNING, "eu sou um warning");
|
||||
}
|
||||
|
||||
********************************************************************************
|
||||
********************************************************************************
|
||||
|
||||
Now, the code..!
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _LOGGER_HPP_
|
||||
#define _LOGGER_HPP_
|
||||
|
||||
#include <tagged_union.hpp>
|
||||
|
||||
struct Logger
|
||||
{
|
||||
/*** a base struct for printing messages in many ways ***/
|
||||
|
||||
struct DefaultPrinter
|
||||
{
|
||||
typedef Tagged::Union < int, Tagged::Union < FILE *, Tagged::Union < std::ostream * > > > BaseType;
|
||||
|
||||
typedef bool ReturnType;
|
||||
|
||||
DefaultPrinter(std::string & msg): _msg(msg) {};
|
||||
|
||||
bool operator()(std::ostream * out)
|
||||
{
|
||||
(*out) << _msg;
|
||||
out->flush();
|
||||
|
||||
return out->good();
|
||||
}
|
||||
|
||||
bool operator()(FILE * out)
|
||||
{
|
||||
if (fputs(_msg.c_str(), out) < 0)
|
||||
return false;
|
||||
|
||||
if (fflush(out) < 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator()(int out)
|
||||
{
|
||||
#ifndef KWIN32
|
||||
return (write(out, _msg.c_str(), _msg.size()) == (int)_msg.size());
|
||||
#else
|
||||
// no need for file descriptors on windows
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string & msg() { return _msg; }
|
||||
|
||||
protected:
|
||||
std::string & _msg;
|
||||
};
|
||||
|
||||
/*** manage the printing of messages ***/
|
||||
|
||||
template <class ClassId, class OutputId, class Printer, class LockType>
|
||||
struct Manager
|
||||
{
|
||||
typedef typename Printer::BaseType BaseType;
|
||||
|
||||
protected:
|
||||
/* holds a stream, and an optimal message prefix */
|
||||
struct OutputOptions
|
||||
{
|
||||
OutputOptions(BaseType & stream, std::string & prefix)
|
||||
: _stream(stream), _prefix(prefix) {};
|
||||
|
||||
BaseType _stream;
|
||||
std::string _prefix;
|
||||
LockType _lock;
|
||||
};
|
||||
|
||||
typedef std::map < OutputId, OutputOptions > OutputMap;
|
||||
|
||||
public:
|
||||
|
||||
/* print in a specific 'message class' */
|
||||
struct ClassType
|
||||
{
|
||||
ClassType(void)
|
||||
: _enabled(true)
|
||||
{};
|
||||
|
||||
// ClassType(ClassType & o)
|
||||
// : _stream_map(o._stream_map), _prefix(o.prefix),
|
||||
// _lock(o._lock),_enabled(o._enabled)
|
||||
// {};
|
||||
|
||||
/* initializes the options of the (class, stream) pair */
|
||||
struct Option
|
||||
{
|
||||
typedef enum { ENABLED, DATETIME, THREADID, DATETIMEMS } EnumType;
|
||||
|
||||
typedef Flagger< EnumType > Flags;
|
||||
typedef typename Flags::InitFlags InitFlags;
|
||||
|
||||
Option(OutputId output, const char * prefix,
|
||||
Flags flags = InitFlags(ENABLED))
|
||||
: _output(output), _prefix(prefix), _flags(flags) {};
|
||||
|
||||
Option(OutputId output, std::string prefix,
|
||||
Flags flags = InitFlags(ENABLED))
|
||||
: _output(output), _prefix(prefix), _flags(flags) {};
|
||||
|
||||
Option(OutputId output,
|
||||
Flags flags = InitFlags(ENABLED))
|
||||
: _output(output), _flags(flags) {};
|
||||
|
||||
OutputId _output;
|
||||
std::string _prefix;
|
||||
Flags _flags;
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
/* holds a prefix and a activation status */
|
||||
struct OptionContainer
|
||||
{
|
||||
OptionContainer(std::string prefix, typename Option::Flags flags)
|
||||
: _prefix(prefix), _flags(flags) {};
|
||||
|
||||
std::string _prefix;
|
||||
typename Option::Flags _flags;
|
||||
};
|
||||
|
||||
typedef std::multimap < OutputId, OptionContainer > OptionMap;
|
||||
|
||||
/* utility function for printing */
|
||||
bool print(std::string & msg, BaseType & stream, LockType & lock)
|
||||
{
|
||||
lock.lock();
|
||||
|
||||
Printer p(msg);
|
||||
bool ret = stream.visit(p);
|
||||
|
||||
lock.unlock();
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/*
|
||||
bool print(std::string & msg, BaseType & stream, LockType & lock)
|
||||
{
|
||||
lock.lock();
|
||||
|
||||
Printer p(msg);
|
||||
bool ret = stream.visit(p);
|
||||
|
||||
lock.unlock();
|
||||
|
||||
return ret;
|
||||
};
|
||||
*/
|
||||
|
||||
public:
|
||||
ClassType & operator&(const Option & value)
|
||||
{
|
||||
add(value._output, value._prefix, value._flags);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void add(OutputId output_id, std::string prefix,
|
||||
typename Option::Flags flags)
|
||||
{
|
||||
typedef std::pair < OutputId, OptionContainer > pair_type;
|
||||
_stream_map.insert(pair_type(output_id, OptionContainer(prefix, flags)));
|
||||
}
|
||||
|
||||
/* get and set methods for active mode */
|
||||
void set(OutputId id, typename Option::EnumType flag, bool value = true)
|
||||
{
|
||||
typename OptionMap::iterator iter = _stream_map.find(id);
|
||||
|
||||
if (iter == _stream_map.end())
|
||||
return;
|
||||
|
||||
(*iter).second._flags.set(flag, value);
|
||||
}
|
||||
|
||||
bool get(OutputId idx, typename Option::EnumType flag)
|
||||
{
|
||||
typename OptionMap::iterator iter = _stream_map.find(idx);
|
||||
|
||||
if (iter == _stream_map.end())
|
||||
return false;
|
||||
|
||||
return (*iter).second._flags.is_set(flag);
|
||||
}
|
||||
|
||||
/* get/adjust the enable/disable value for the class */
|
||||
void enabled(bool enabled) { _enabled = enabled; };
|
||||
bool enabled() { return _enabled; };
|
||||
|
||||
/* get/adjust the classe prefix */
|
||||
void prefix(const char * prefix) { _prefix = prefix; }
|
||||
void prefix(std::string & prefix) { _prefix = prefix; }
|
||||
std::string & prefix() { return _prefix; }
|
||||
|
||||
/* printing function (operator, actually) */
|
||||
bool operator()(OutputMap & out_map, std::string & msg)
|
||||
{
|
||||
if (!_enabled)
|
||||
return true;
|
||||
|
||||
typedef typename OptionMap::iterator Iter;
|
||||
|
||||
bool ret = true;
|
||||
|
||||
for (Iter iter = _stream_map.begin(); iter != _stream_map.end(); iter++)
|
||||
{
|
||||
OptionContainer & opt = (*iter).second;
|
||||
|
||||
if (!opt._flags[Option::ENABLED])
|
||||
continue;
|
||||
|
||||
typename OutputMap::iterator out_iter = out_map.find((*iter).first);
|
||||
|
||||
/* this stream have been added already? if not, skip! */
|
||||
if (out_iter == out_map.end())
|
||||
continue;
|
||||
|
||||
/* final message */
|
||||
std::string out_msg;
|
||||
|
||||
if (opt._flags[Option::DATETIME])
|
||||
{
|
||||
#if defined(COMMONS_LIBRARY_USING_ASTERISK)
|
||||
#if ASTERISK_AT_LEAST(1,6,0)
|
||||
struct timeval tv;
|
||||
struct ast_tm lt;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
#else
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
time (&tv);
|
||||
#endif
|
||||
|
||||
#if ASTERISK_AT_LEAST(1,4,5)
|
||||
ast_localtime (&tv, <, NULL);
|
||||
#else
|
||||
localtime_r (&tv, <);
|
||||
#endif
|
||||
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour
|
||||
% lt.tm_min % lt.tm_sec);
|
||||
|
||||
#elif defined(COMMONS_LIBRARY_USING_CALLWEAVER)
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
time (&tv);
|
||||
|
||||
localtime_r (&tv, <);
|
||||
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour
|
||||
% lt.tm_min % lt.tm_sec);
|
||||
|
||||
#elif defined(COMMONS_LIBRARY_USING_FREESWITCH)
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
time (&tv);
|
||||
|
||||
localtime_r (&tv, <);
|
||||
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour
|
||||
% lt.tm_min % lt.tm_sec);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
if (opt._flags[Option::DATETIMEMS])
|
||||
{
|
||||
#if defined(COMMONS_LIBRARY_USING_ASTERISK)
|
||||
#if ASTERISK_AT_LEAST(1,6,0)
|
||||
struct timeval tv;
|
||||
struct ast_tm lt;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
#else
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
time (&tv);
|
||||
#endif
|
||||
|
||||
#if ASTERISK_AT_LEAST(1,4,5)
|
||||
ast_localtime (&tv, <, NULL);
|
||||
#else
|
||||
localtime_r (&tv, <);
|
||||
#endif
|
||||
|
||||
#if ASTERISK_AT_LEAST(1,6,0)
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d:%04d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour % lt.tm_min
|
||||
% lt.tm_sec % (tv.tv_usec / 1000));
|
||||
#else
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d:%04d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour % lt.tm_min
|
||||
% lt.tm_sec % (tv * 1000));
|
||||
#endif
|
||||
|
||||
#elif defined(COMMONS_LIBRARY_USING_CALLWEAVER)
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
time (&tv);
|
||||
|
||||
localtime_r (&tv, <);
|
||||
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d:%04d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour % lt.tm_min
|
||||
% lt.tm_sec % (tv * 1000));
|
||||
|
||||
#elif defined(COMMONS_LIBRARY_USING_FREESWITCH)
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
time (&tv);
|
||||
|
||||
localtime_r (&tv, <);
|
||||
|
||||
out_msg += STG(FMT("[%04d-%02d-%02d %02d:%02d:%02d:%04d] ")
|
||||
% (lt.tm_year + 1900) % (lt.tm_mon + 1) % lt.tm_mday % lt.tm_hour % lt.tm_min
|
||||
% lt.tm_sec % (tv * 1000));
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
OutputOptions & out_opt = (*out_iter).second;
|
||||
|
||||
if (opt._flags[Option::THREADID])
|
||||
{
|
||||
#if defined (COMMONS_LIBRARY_USING_ASTERISK) || defined(COMMONS_LIBRARY_USING_CALLWEAVER) || defined(COMMONS_LIBRARY_USING_FREESWITCH)
|
||||
out_msg += STG(FMT("t=%08p ") % ((void*)pthread_self()));
|
||||
#endif
|
||||
}
|
||||
|
||||
out_msg += _prefix;
|
||||
out_msg += out_opt._prefix;
|
||||
out_msg += opt._prefix;
|
||||
out_msg += msg;
|
||||
out_msg += "\n";
|
||||
|
||||
ret |= print(out_msg, out_opt._stream, out_opt._lock);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool operator()(BaseType & stream, std::string & msg)
|
||||
{
|
||||
std::string final_msg;
|
||||
|
||||
final_msg += _prefix;
|
||||
final_msg += msg;
|
||||
final_msg += "\n";
|
||||
|
||||
return print(final_msg, stream, _lock);
|
||||
}
|
||||
|
||||
protected:
|
||||
OptionMap _stream_map;
|
||||
std::string _prefix;
|
||||
LockType _lock;
|
||||
bool _enabled;
|
||||
};
|
||||
|
||||
/* util declaration */
|
||||
typedef typename ClassType::Option Option;
|
||||
|
||||
/* class_id_type -> ClassType mapper */
|
||||
typedef std::map < ClassId, ClassType > ClassMap;
|
||||
|
||||
/* local option pair */
|
||||
typedef std::pair < OutputId, OutputOptions > OutputOptionPair;
|
||||
|
||||
void add(OutputId output, BaseType stream, const char * prefix = "")
|
||||
{
|
||||
std::string str_prefix(prefix);
|
||||
|
||||
_output_map.insert(OutputOptionPair(output, OutputOptions(stream, str_prefix)));
|
||||
}
|
||||
|
||||
void add(OutputId output, BaseType stream, std::string prefix)
|
||||
{
|
||||
_output_map.insert(OutputOptionPair(output, OutputOptions(stream, prefix)));
|
||||
}
|
||||
|
||||
ClassType & classe(ClassId classeid)
|
||||
{
|
||||
return _classe_map[classeid];
|
||||
}
|
||||
|
||||
bool operator()(ClassId classeid, const char * msg)
|
||||
{
|
||||
std::string str_msg(msg);
|
||||
return _classe_map[classeid](_output_map, str_msg);
|
||||
}
|
||||
|
||||
bool operator()(ClassId classeid, std::string & msg)
|
||||
{
|
||||
return _classe_map[classeid](_output_map, msg);
|
||||
}
|
||||
|
||||
bool operator()(ClassId classeid, Format fmt)
|
||||
{
|
||||
std::string str_fmt = STG(fmt);
|
||||
return _classe_map[classeid](_output_map, str_fmt);
|
||||
}
|
||||
|
||||
bool operator()(ClassId classeid, BaseType stream, const char * msg)
|
||||
{
|
||||
std::string str_msg(msg);
|
||||
return _classe_map[classeid](stream, str_msg);
|
||||
}
|
||||
|
||||
bool operator()(ClassId classeid, BaseType stream, std::string & msg)
|
||||
{
|
||||
return _classe_map[classeid](stream, msg);
|
||||
}
|
||||
|
||||
bool operator()(ClassId classeid, BaseType stream, Format fmt)
|
||||
{
|
||||
std::string str_fmt = STG(fmt);
|
||||
return _classe_map[classeid](stream, str_fmt);
|
||||
}
|
||||
|
||||
protected:
|
||||
ClassMap _classe_map;
|
||||
OutputMap _output_map;
|
||||
};
|
||||
|
||||
private:
|
||||
Logger();
|
||||
};
|
||||
|
||||
#endif /* _LOGGER_HPP_ */
|
||||
54
src/mod/endpoints/mod_khomp/commons/noncopyable.hpp
Normal file
54
src/mod/endpoints/mod_khomp/commons/noncopyable.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _NONCOPYABLE_HPP_
|
||||
#define _NONCOPYABLE_HPP_
|
||||
|
||||
struct NonCopyable
|
||||
{
|
||||
NonCopyable() {};
|
||||
private:
|
||||
NonCopyable(NonCopyable const &) { };
|
||||
NonCopyable & operator=(NonCopyable const &) { return *this; };
|
||||
};
|
||||
|
||||
#endif /* _NONCOPYABLE_HPP_ */
|
||||
|
||||
263
src/mod/endpoints/mod_khomp/commons/refcounter.hpp
Normal file
263
src/mod/endpoints/mod_khomp/commons/refcounter.hpp
Normal file
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the "LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
/* This struct uses static polymorphism, and derived classes should implement *
|
||||
* the "unreference()" method, which should release all resources when called */
|
||||
|
||||
#ifndef _REFCOUNTER_HPP_
|
||||
#define _REFCOUNTER_HPP_
|
||||
|
||||
#define NEW_REFCOUNTER(...) public ReferenceCounter< __VA_ARGS__ >
|
||||
#define INC_REFCOUNTER(o, ...) ReferenceCounter< __VA_ARGS__ >(static_cast< const ReferenceCounter < __VA_ARGS__ > & >(o))
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <noncopyable.hpp>
|
||||
#include <atomic.hpp>
|
||||
|
||||
#ifdef DEBUG
|
||||
# include <iostream>
|
||||
#endif
|
||||
|
||||
struct ReferenceData: public NonCopyable
|
||||
{
|
||||
ReferenceData()
|
||||
: _data_count(1)
|
||||
{};
|
||||
|
||||
inline unsigned int increment(void)
|
||||
{
|
||||
if (!_data_count)
|
||||
abort();
|
||||
|
||||
Atomic::doAdd(&_data_count);
|
||||
|
||||
return _data_count;
|
||||
}
|
||||
|
||||
inline unsigned int decrement(void)
|
||||
{
|
||||
if (!_data_count)
|
||||
abort();
|
||||
|
||||
Atomic::doSub(&_data_count);
|
||||
return _data_count;
|
||||
}
|
||||
|
||||
volatile unsigned int _data_count;
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
struct ReferenceCounter
|
||||
{
|
||||
typedef T Type;
|
||||
|
||||
ReferenceCounter(bool create_counter = true)
|
||||
: _reference_count(0)
|
||||
{
|
||||
reference_restart(create_counter);
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cerr << ((void*)this) << ": ReferenceCounter() [ref_count="
|
||||
<< (_reference_count ? (*_reference_count) : -1) << "]" << std::endl;
|
||||
#endif
|
||||
};
|
||||
|
||||
ReferenceCounter(const ReferenceCounter & o)
|
||||
: _reference_count(0)
|
||||
{
|
||||
reference_reflect(o);
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cerr << ((void*)this) << ": ReferenceCounter(" << ((void*)(&o)) << ") [ref_count="
|
||||
<< (_reference_count ? (*_reference_count) : -1) << "]" << std::endl;
|
||||
#endif
|
||||
};
|
||||
|
||||
virtual ~ReferenceCounter()
|
||||
{
|
||||
#ifdef DEBUG
|
||||
std::cerr << ((void*)this) << ": ~ReferenceCounter() [ref_count="
|
||||
<< (_reference_count ? (*_reference_count) : -1) << "]" << std::endl;
|
||||
#endif
|
||||
reference_disconnect(_reference_count);
|
||||
}
|
||||
|
||||
ReferenceCounter & operator=(const ReferenceCounter & o)
|
||||
{
|
||||
reference_reflect(o);
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cerr << ((void*)this) << ": ReferenceCounter::operator=(" << ((void*)(&o)) << ") [ref_count="
|
||||
<< (_reference_count ? (*_reference_count) : -1) << "]" << std::endl;
|
||||
#endif
|
||||
|
||||
return *this;
|
||||
};
|
||||
|
||||
protected:
|
||||
inline void reference_restart(bool create_counter = false)
|
||||
{
|
||||
ReferenceData * oldref = _reference_count;
|
||||
|
||||
_reference_count = (create_counter ? new ReferenceData() : 0);
|
||||
|
||||
if (oldref) reference_disconnect(oldref);
|
||||
}
|
||||
|
||||
inline void reference_reflect(const ReferenceCounter & other)
|
||||
{
|
||||
ReferenceData * newref = other._reference_count;
|
||||
ReferenceData * oldref = _reference_count;
|
||||
|
||||
/* NOTE: increment before, avoid our reference being zero, even *
|
||||
* for a short period of time. */
|
||||
|
||||
if (newref) newref->increment();
|
||||
|
||||
_reference_count = newref;
|
||||
|
||||
if (oldref) reference_disconnect(oldref);
|
||||
};
|
||||
|
||||
inline void reference_disconnect(ReferenceData *& counter)
|
||||
{
|
||||
if (counter)
|
||||
{
|
||||
unsigned int result = counter->decrement();
|
||||
|
||||
if (!result)
|
||||
{
|
||||
static_cast< Type * >(this)->unreference();
|
||||
delete counter;
|
||||
}
|
||||
|
||||
counter = 0;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
ReferenceData * _reference_count;
|
||||
};
|
||||
|
||||
template < typename T >
|
||||
struct ReferenceContainer: NEW_REFCOUNTER(ReferenceContainer< T >)
|
||||
{
|
||||
/* type */
|
||||
typedef T Type;
|
||||
|
||||
/* shorthand */
|
||||
typedef ReferenceCounter < ReferenceContainer< Type > > Counter;
|
||||
|
||||
// TODO: make this a generic exception someday
|
||||
struct NotFound {};
|
||||
|
||||
ReferenceContainer()
|
||||
: Counter(false),
|
||||
_reference_value(0)
|
||||
{};
|
||||
|
||||
ReferenceContainer(Type * value)
|
||||
: _reference_value(value)
|
||||
{};
|
||||
|
||||
ReferenceContainer(const ReferenceContainer & value)
|
||||
: Counter(false),
|
||||
_reference_value(0)
|
||||
{
|
||||
operator()(value);
|
||||
};
|
||||
|
||||
virtual ~ReferenceContainer()
|
||||
{};
|
||||
|
||||
ReferenceContainer operator=(const ReferenceContainer & value)
|
||||
{
|
||||
operator()(value);
|
||||
return *this;
|
||||
};
|
||||
|
||||
/**/
|
||||
|
||||
void unreference()
|
||||
{
|
||||
if (_reference_value)
|
||||
{
|
||||
delete _reference_value;
|
||||
_reference_value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// simulates a copy constructor
|
||||
void operator()(const ReferenceContainer & value)
|
||||
{
|
||||
Counter::reference_reflect(value);
|
||||
|
||||
_reference_value = const_cast<Type *>(value._reference_value);
|
||||
};
|
||||
|
||||
// shortcut for operator below
|
||||
void operator=(const Type * value)
|
||||
{
|
||||
operator()(value);
|
||||
};
|
||||
|
||||
// accept value (pointer)!
|
||||
void operator()(const Type * value)
|
||||
{
|
||||
Counter::reference_restart((value != 0));
|
||||
|
||||
_reference_value = const_cast<Type *>(value);
|
||||
};
|
||||
|
||||
// return value (pointer)!
|
||||
Type * operator()(void)
|
||||
{
|
||||
return _reference_value;
|
||||
};
|
||||
|
||||
protected:
|
||||
Type * _reference_value;
|
||||
|
||||
protected:
|
||||
};
|
||||
|
||||
#endif /* _REFCOUNTER_HPP_ */
|
||||
148
src/mod/endpoints/mod_khomp/commons/regex.cpp
Normal file
148
src/mod/endpoints/mod_khomp/commons/regex.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <regex.hpp>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void Regex::Expression::initialize(void)
|
||||
{
|
||||
unsigned int tmplen = strlen(_expression);
|
||||
|
||||
bool extflag = (_flags & E_EXTENDED);
|
||||
|
||||
for (unsigned int i = 0; i < tmplen; ++i)
|
||||
{
|
||||
switch (_expression[i])
|
||||
{
|
||||
case '\\':
|
||||
++i;
|
||||
|
||||
if (!extflag && i < tmplen)
|
||||
if (_expression[i] == '(')
|
||||
++_subcounter;
|
||||
|
||||
break;
|
||||
|
||||
case '(':
|
||||
if (extflag)
|
||||
++_subcounter;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_errorstate = regcomp(&_comp_regex, _expression, _flags);
|
||||
}
|
||||
|
||||
std::string Regex::Expression::regerror_as_string(void)
|
||||
{
|
||||
unsigned int count = regerror(_errorstate, &_comp_regex, 0, 0) + 1;
|
||||
|
||||
char * msg = new char[count];
|
||||
|
||||
regerror(_errorstate, &_comp_regex, msg, count);
|
||||
|
||||
std::string tmp(msg, count);
|
||||
|
||||
delete[] msg;
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void Regex::Match::initialize(void)
|
||||
{
|
||||
if (_expression.valid())
|
||||
{
|
||||
_subcounter = (_expression.subcount() + 2); // 0 + N.. + invalid
|
||||
_submatches = new regmatch_t[_subcounter];
|
||||
_have_match = (regexec(_expression.repr(), _basestring.c_str(),
|
||||
_subcounter, _submatches, _flags) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
std::string Regex::Match::replace(std::string rep, unsigned int index)
|
||||
{
|
||||
ReplaceMap tmp;
|
||||
tmp.insert(ReplacePair(index,rep));
|
||||
return replace(tmp);
|
||||
}
|
||||
|
||||
std::string Regex::Match::replace(Regex::ReplaceMap & map)
|
||||
{
|
||||
if (!_have_match)
|
||||
return _basestring;
|
||||
|
||||
std::string buffer = _basestring;
|
||||
|
||||
try
|
||||
{
|
||||
if (_submatches[0].rm_so != 0 && (map.find(0) != map.end()))
|
||||
return _basestring.replace(_submatches[0].rm_so, _submatches[0].rm_eo - _submatches[0].rm_so, map.find(0)->second);
|
||||
|
||||
for (unsigned int n = 1; (_submatches[n].rm_so != -1) && (n < _subcounter); n++)
|
||||
{
|
||||
//// s f RRR s f RRR s f RRRR s f
|
||||
//// XXXYYY(ZZZ)AAA(BBB)CCCEEE(FFFF)GGGG
|
||||
|
||||
bool globalsubs = false;
|
||||
|
||||
if (map.find(n) == map.end())
|
||||
{
|
||||
if (map.find(UINT_MAX) == map.end())
|
||||
continue;
|
||||
|
||||
globalsubs = true;
|
||||
}
|
||||
|
||||
buffer = buffer.replace(_submatches[n].rm_so,
|
||||
_submatches[n].rm_eo - _submatches[n].rm_so,
|
||||
map.find((globalsubs ? UINT_MAX : n))->second);
|
||||
}
|
||||
}
|
||||
catch (std::out_of_range e)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
252
src/mod/endpoints/mod_khomp/commons/regex.hpp
Normal file
252
src/mod/endpoints/mod_khomp/commons/regex.hpp
Normal file
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <regex.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <map>
|
||||
|
||||
#include <refcounter.hpp>
|
||||
#include <noncopyable.hpp>
|
||||
|
||||
#ifndef _REGEX_HPP_
|
||||
#define _REGEX_HPP_
|
||||
|
||||
struct Regex
|
||||
{
|
||||
enum
|
||||
{
|
||||
E_EXTENDED = REG_EXTENDED,
|
||||
E_IGNORE_CASE = REG_ICASE,
|
||||
E_NO_SUB_MATCH = REG_NOSUB,
|
||||
E_EXPLICIT_NEWLINE = REG_NEWLINE,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
M_NO_BEGIN_OF_LINE = REG_NOTBOL,
|
||||
M_NO_END_OF_LINE = REG_NOTEOL,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
/* mark replace for full match (submatch "0"). */
|
||||
REP_BASE = 0u,
|
||||
/* mark global string for replace. */
|
||||
REP_ALL = UINT_MAX,
|
||||
};
|
||||
|
||||
typedef std::pair < unsigned int, std::string > ReplacePair;
|
||||
typedef std::map < unsigned int, std::string > ReplaceMap;
|
||||
|
||||
struct Expression : public NonCopyable
|
||||
{
|
||||
Expression(const char * expression, unsigned int flags = 0)
|
||||
: _expression(expression), _alloced(false),
|
||||
_subcounter(0), _errorstate(INT_MAX), _flags(flags)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
Expression(std::string & expression, unsigned int flags = 0)
|
||||
: _expression(strdup(expression.c_str())), _alloced(true),
|
||||
_subcounter(0), _errorstate(INT_MAX), _flags(flags)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
~Expression()
|
||||
{
|
||||
if (_errorstate != INT_MAX)
|
||||
regfree(&_comp_regex);
|
||||
|
||||
if (_alloced)
|
||||
{
|
||||
delete _expression;
|
||||
_expression = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool valid(void) { return (_errorstate == 0); }
|
||||
|
||||
unsigned int subcount(void) { return _subcounter; }
|
||||
const regex_t * repr(void) { return &_comp_regex; }
|
||||
|
||||
std::string error(void)
|
||||
{
|
||||
switch (_errorstate)
|
||||
{
|
||||
case 0: return "";
|
||||
case INT_MAX: return "uninitialized";
|
||||
default: return regerror_as_string();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void initialize(void);
|
||||
std::string regerror_as_string(void);
|
||||
|
||||
protected:
|
||||
const char * _expression;
|
||||
bool _alloced;
|
||||
|
||||
unsigned int _subcounter;
|
||||
|
||||
int _errorstate;
|
||||
regex_t _comp_regex;
|
||||
|
||||
unsigned int _flags;
|
||||
};
|
||||
|
||||
struct Match: NEW_REFCOUNTER(Match)
|
||||
{
|
||||
Match(const char * basestring, Expression & expression, unsigned int flags = 0)
|
||||
: _basestring(basestring), _expression(expression), _subcounter(0), _submatches(0),
|
||||
_have_match(false), _flags(flags)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
Match(std::string & basestring, Expression & expression, unsigned int flags = 0)
|
||||
: _basestring(basestring), _expression(expression), _subcounter(0), _submatches(0),
|
||||
_have_match(false), _flags(flags)
|
||||
{
|
||||
initialize();
|
||||
}
|
||||
|
||||
Match(const Match & o)
|
||||
: INC_REFCOUNTER(o, Match),
|
||||
_basestring(o._basestring), _expression(o._expression),
|
||||
_subcounter(o._subcounter), _submatches(o._submatches),
|
||||
_have_match(o._have_match), _flags(o._flags)
|
||||
{
|
||||
}
|
||||
|
||||
void unreference()
|
||||
{
|
||||
delete[] _submatches;
|
||||
}
|
||||
|
||||
bool matched(void)
|
||||
{
|
||||
return _have_match;
|
||||
}
|
||||
|
||||
bool matched(unsigned int number)
|
||||
{
|
||||
if (_have_match && number < _subcounter)
|
||||
return (_submatches[number].rm_so != -1);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string submatch(int number)
|
||||
{
|
||||
if (!matched(number))
|
||||
return "";
|
||||
|
||||
return _basestring.substr(_submatches[number].rm_so,
|
||||
_submatches[number].rm_eo - _submatches[number].rm_so);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief gets a map with all matches
|
||||
* \return std::map<int,std::string> with all matches
|
||||
* \note index 0 in map, is the complete string
|
||||
* \author Eduardo Nunes Pereira
|
||||
*
|
||||
* If fails the empty map is returned
|
||||
*/
|
||||
std::map<int, std::string> obtain_match_map()
|
||||
{
|
||||
int match_counter = 0;
|
||||
std::map<int,std::string> tmp_map;
|
||||
while(matched(match_counter))
|
||||
{
|
||||
tmp_map.insert(std::make_pair(match_counter,submatch(match_counter)));
|
||||
match_counter++;
|
||||
}
|
||||
|
||||
return tmp_map;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief replaces strings matched by parentesis
|
||||
* \param each item of the vector is a parentesis replaced
|
||||
* \return string replaced
|
||||
* \note The overload method match only one string in parentesis.
|
||||
* \author Eduardo Nunes Pereira
|
||||
*
|
||||
* If fails the empty string is returned.
|
||||
*/
|
||||
std::string replace(ReplaceMap &);
|
||||
std::string replace(std::string, unsigned int index = REP_BASE);
|
||||
|
||||
std::string operator[](int number)
|
||||
{
|
||||
return submatch(number);
|
||||
}
|
||||
|
||||
// NOTE: there is already a way to get subcount defined on EXPRESSION class!
|
||||
|
||||
private:
|
||||
void initialize(void);
|
||||
|
||||
protected:
|
||||
std::string _basestring;
|
||||
Expression & _expression;
|
||||
|
||||
unsigned int _subcounter;
|
||||
regmatch_t * _submatches;
|
||||
|
||||
bool _have_match;
|
||||
unsigned int _flags;
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* _REGEX_HPP_ */
|
||||
|
||||
575
src/mod/endpoints/mod_khomp/commons/ringbuffer.cpp
Normal file
575
src/mod/endpoints/mod_khomp/commons/ringbuffer.cpp
Normal file
@@ -0,0 +1,575 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <ringbuffer.hpp>
|
||||
|
||||
/********** BUFFER FUNCTIONS **********/
|
||||
|
||||
/* writes everything or nothing */
|
||||
bool Ringbuffer_traits::traits_provide(char * buffer, const char * value, unsigned int amount, bool skip_overwrite)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int reader = cache.reader.complete;
|
||||
const unsigned int writer = cache.writer.complete;
|
||||
|
||||
const unsigned int dest = cache.writer.complete - 1;
|
||||
|
||||
const bool reader_less = cache.reader.complete < cache.writer.complete;
|
||||
|
||||
if (amount >= _size)
|
||||
return false;
|
||||
|
||||
bool ret = true;
|
||||
|
||||
/* should we go around the buffer for writing? */
|
||||
if (((writer + amount) > _size) && (reader_less || !skip_overwrite))
|
||||
{
|
||||
/* Documentation of the formula used in the 'if' below.
|
||||
*
|
||||
* [0|1|2|3|4|5|6|7] => size=8
|
||||
* | |
|
||||
* reader |
|
||||
* writer
|
||||
*
|
||||
* => writer has places [5,6,7,0,1] to write (5 places).
|
||||
*
|
||||
* => 8 - (4-2+1) = 8 - (2+1) = 8 - 3 = 5
|
||||
*
|
||||
* > writer goes 1 up, amount goes 1 down.
|
||||
* > reader goes 1 up, amount goes 1 up.
|
||||
* > size goes 1 down, amount goes 1 down.
|
||||
*
|
||||
*/
|
||||
|
||||
if ((_size - (writer - reader + 1)) <= amount)
|
||||
{
|
||||
if (skip_overwrite)
|
||||
return false;
|
||||
|
||||
do
|
||||
{
|
||||
Buffer_pointer extra(cache.reader);
|
||||
extra.complete = (extra.complete + amount) % _size;
|
||||
|
||||
if (update(cache.reader, extra))
|
||||
break;
|
||||
}
|
||||
while (true);
|
||||
|
||||
ret = false;
|
||||
}
|
||||
|
||||
unsigned int wr1 = _size - writer + 1; /* writer is already 1 position after */
|
||||
unsigned int wr2 = amount - wr1;
|
||||
|
||||
// fprintf(stderr, "%p> partial write: (%d/%d) %d/%d [%d/%d]\n", this, wr1, wr2, amount, _size, reader, writer);
|
||||
|
||||
/* two partial writes (one at the end, another at the beginning) */
|
||||
memcpy((void *) &(buffer[dest]), (const void *) (value), _block * wr1);
|
||||
memcpy((void *) (buffer), (const void *) &(value[wr1]), _block * wr2);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!reader_less && ((reader - writer) <= amount))
|
||||
{
|
||||
if (skip_overwrite)
|
||||
return false;
|
||||
|
||||
do
|
||||
{
|
||||
Buffer_pointer extra(cache.reader);
|
||||
extra.complete = (extra.complete + amount) % _size;
|
||||
|
||||
if (update(cache.reader, extra))
|
||||
break;
|
||||
}
|
||||
while (true);
|
||||
|
||||
ret = false;
|
||||
}
|
||||
|
||||
// fprintf(stderr, "%p> full write: a=%d/s=%d [r=%d/w=%d]\n", this, amount, _size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
memcpy((void *) &(buffer[dest]), (const void *) value, _block * amount);
|
||||
}
|
||||
|
||||
_pointers.writer.complete = ((dest + amount) % _size) + 1;
|
||||
_pointers.writer.partial = 1;
|
||||
|
||||
// fprintf(stderr, "%p> write end: %d [block=%d]\n", this, writer, _block);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns the number of itens that have been read */
|
||||
unsigned int Ringbuffer_traits::traits_consume(const char * buffer, char * value, unsigned int amount, bool atomic_mode)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int writer = _pointers.writer.complete;
|
||||
const unsigned int reader = _pointers.reader.complete;
|
||||
|
||||
const bool writer_less = writer < reader;
|
||||
|
||||
unsigned int total = 0;
|
||||
|
||||
/* should we go around the buffer for reading? */
|
||||
if (writer_less && (reader + amount >= _size))
|
||||
{
|
||||
total = std::min(_size - (reader - writer + 1), amount);
|
||||
|
||||
if ((total == 0) || (atomic_mode && (total < amount)))
|
||||
return 0;
|
||||
|
||||
unsigned int rd1 = _size - reader;
|
||||
unsigned int rd2 = total - rd1;
|
||||
|
||||
// fprintf(stderr, "%p> partial read: (%d/%d) %d/%d [%d/%d]\n", this, rd1, rd2, total, _size, reader, writer);
|
||||
|
||||
/* two partial consumes (one at the end, another at the beginning) */
|
||||
memcpy((void *) (value), (const void *) &(buffer[reader]), _block * rd1);
|
||||
memcpy((void *) &(value[rd1]), (const void *) (buffer), _block * rd2);
|
||||
}
|
||||
else
|
||||
{
|
||||
total = std::min((!writer_less ? writer - (reader + 1) : amount), amount);
|
||||
|
||||
if ((total == 0) || (atomic_mode && (total < amount)))
|
||||
return 0;
|
||||
|
||||
// fprintf(stderr, "%p> full read: %d/%d [%d/%d]\n", this, total, _size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
memcpy((void *) value, (const void *) &(buffer[reader]), _block * total);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
/* jump the reader forward */
|
||||
Buffer_pointer index((cache.reader.complete + total) % _size);
|
||||
|
||||
if (update(cache.reader, index))
|
||||
break;
|
||||
}
|
||||
while (true);
|
||||
|
||||
// fprintf(stderr, "%p> read end: %d [block=%d]\n", this, reader, _block);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
/********** TWO-PHASE BUFFER FUNCTIONS ***********/
|
||||
|
||||
/* returns the number of itens that have been read */
|
||||
unsigned int Ringbuffer_traits::traits_consume_begins(const char * buffer, char * value, unsigned int amount, bool atomic_mode)
|
||||
{
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
/* avoid using different values */
|
||||
const unsigned int reader = cache.reader.complete;
|
||||
const unsigned int writer = cache.writer.complete;
|
||||
|
||||
const bool writer_less = writer < reader;
|
||||
|
||||
unsigned int total = 0;
|
||||
|
||||
/* should we go around the buffer for reading? */
|
||||
if (writer_less && (reader + amount >= _size))
|
||||
{
|
||||
total = std::min(_size - (reader - writer + 1), amount);
|
||||
|
||||
if ((total == 0) || (atomic_mode && (total < amount)))
|
||||
return 0;
|
||||
|
||||
unsigned int rd1 = _size - reader;
|
||||
unsigned int rd2 = total - rd1;
|
||||
|
||||
// fprintf(stderr, "%p> partial read: (%d/%d) %d/%d [%d/%d]\n", this, rd1, rd2, total, _size, reader, writer);
|
||||
|
||||
/* two partial consumes (one at the end, another at the beginning) */
|
||||
memcpy((void *) (value), (const void *) &(buffer[reader]), _block * rd1);
|
||||
memcpy((void *) &(value[rd1]), (const void *) (buffer), _block * rd2);
|
||||
}
|
||||
else
|
||||
{
|
||||
total = std::min((!writer_less ? writer - (reader + 1) : amount), amount);
|
||||
|
||||
if ((total == 0) || (atomic_mode && (total < amount)))
|
||||
return 0;
|
||||
|
||||
// fprintf(stderr, "%p> full read: %d/%d [%d/%d]\n", this, total, _size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
memcpy((void *) value, (const void *) &(buffer[reader]), _block * total);
|
||||
}
|
||||
|
||||
// fprintf(stderr, "%p> read end: %d [%d]\n", this, _reader, _reader_partial);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
bool Ringbuffer_traits::traits_consume_commit(unsigned int amount)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int writer = cache.writer.complete;
|
||||
const unsigned int reader = cache.reader.complete;
|
||||
|
||||
const bool writer_less = writer < reader;
|
||||
|
||||
unsigned int total = 0;
|
||||
|
||||
/* should we go around the buffer for reading? */
|
||||
if (writer_less && (reader + amount >= _size))
|
||||
{
|
||||
total = std::min(_size - (reader - writer + 1), amount);
|
||||
|
||||
if (total < amount)
|
||||
return false;
|
||||
|
||||
// fprintf(stderr, "%p> partial read: (%d/%d) %d/%d [%d/%d]\n", this, rd1, rd2, total, _size, reader, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
total = std::min((!writer_less ? writer - (reader + 1) : amount), amount);
|
||||
|
||||
if (total < amount)
|
||||
return false;
|
||||
|
||||
// fprintf(stderr, "%p> full read: %d/%d [%d/%d]\n", this, total, _size, reader, writer);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
/* jump the reader forward */
|
||||
Buffer_pointer index(cache.reader);
|
||||
index.complete = (index.complete + total) % _size;
|
||||
|
||||
if (update(cache.reader, index))
|
||||
break;
|
||||
}
|
||||
while (true);
|
||||
|
||||
// fprintf(stderr, "%p> read end: %d [%d]\n", this, _reader, _reader_partial);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/********** PARTIAL BUFFER FUNCTIONS (bytes) ***********/
|
||||
|
||||
/* writes everything or nothing */
|
||||
bool Ringbuffer_traits::traits_provide_partial(char * buffer, const char * value, unsigned int amount)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int reader = (cache.reader.complete * _block) + cache.reader.partial;
|
||||
const unsigned int writer = ((cache.writer.complete - 1) * _block) + cache.writer.partial;
|
||||
|
||||
const unsigned int size = _size * _block;
|
||||
const unsigned int dest = writer - 1;
|
||||
|
||||
// fprintf(stderr, "%p> provide partial: %d/%d [%d/%d]\n", this, reader, writer, amount, size);
|
||||
|
||||
const bool reader_less = reader < writer;
|
||||
|
||||
/* should we go around the buffer for writing? */
|
||||
if (reader_less && ((writer + amount) > size))
|
||||
{
|
||||
/* Documentation of the formula used in the 'if' below.
|
||||
*
|
||||
* [0|1|2|3|4|5|6|7] => size=8
|
||||
* | |
|
||||
* reader |
|
||||
* writer
|
||||
*
|
||||
* => writer has places [5,6,7,0,1] to write (5 places).
|
||||
*
|
||||
* => 8 - (4-2+1) = 8 - (2+1) = 8 - 3 = 5
|
||||
*
|
||||
* > writer goes 1 up, amount goes 1 down.
|
||||
* > reader goes 1 up, amount goes 1 up.
|
||||
* > size goes 1 down, amount goes 1 down.
|
||||
*
|
||||
*/
|
||||
|
||||
if ((size - (writer - reader + 1)) <= amount)
|
||||
return false;
|
||||
|
||||
unsigned int wr1 = size - writer + 1; /* writer is already 1 position after */
|
||||
unsigned int wr2 = amount - wr1;
|
||||
|
||||
// fprintf(stderr, "%p> p partial write: (%d/%d) %d/%d [%d/%d]\n", this, wr1, wr2, amount, size, reader, writer);
|
||||
|
||||
/* two partial writes (one at the end, another at the beginning) */
|
||||
memcpy((void *) &(buffer[dest]), (const void *) (value), wr1);
|
||||
memcpy((void *) (buffer), (const void *) &(value[wr1]), wr2);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!reader_less && ((reader - writer) <= amount))
|
||||
return false;
|
||||
|
||||
// fprintf(stderr, "%p> p full write: %d/%d [r=%d/w=%d]\n", this, amount, size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
memcpy((void *) &(buffer[dest]), (const void *) value, amount);
|
||||
}
|
||||
|
||||
unsigned int new_writer = ((dest + amount) % size) + 1;
|
||||
|
||||
/* update "full length position" */
|
||||
_pointers.writer.complete = (unsigned int)floor((double) new_writer / (double)_block)+1;
|
||||
_pointers.writer.partial = (unsigned short)(new_writer % _block);
|
||||
|
||||
// fprintf(stderr, "%p> p write end: %d [block=%d]\n", this, new_writer, _block);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* returns the number of bytes that have been read */
|
||||
unsigned int Ringbuffer_traits::traits_consume_partial(const char * buffer, char * value, unsigned int amount)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int writer = ((cache.writer.complete - 1) * _block) + cache.writer.partial;
|
||||
const unsigned int reader = (cache.reader.complete * _block) + cache.reader.partial;
|
||||
|
||||
const unsigned int size = _size * _block;
|
||||
|
||||
// fprintf(stderr, "%p> consume partial: %d/%d [%d/%d]\n", this, reader, writer, amount, size);
|
||||
|
||||
const bool writer_less = writer < reader;
|
||||
|
||||
unsigned int total = 0;
|
||||
|
||||
/* should we go around the buffer for reading? */
|
||||
if (writer_less && (reader + amount >= size))
|
||||
{
|
||||
total = std::min(size - (reader - writer + 1), amount);
|
||||
|
||||
if (total == 0)
|
||||
return 0;
|
||||
|
||||
unsigned int rd1 = size - reader;
|
||||
unsigned int rd2 = total - rd1;
|
||||
|
||||
// fprintf(stderr, "%p> p partial read: (%d/%d) %d/%d [%d/%d]\n", this, rd1, rd2, total, size, reader, writer);
|
||||
|
||||
/* two partial consumes (one at the end, another at the beginning) */
|
||||
memcpy((void *) (value), (const void *) &(buffer[reader]), rd1);
|
||||
memcpy((void *) &(value[rd1]), (const void *) (buffer), rd2);
|
||||
}
|
||||
else
|
||||
{
|
||||
total = std::min((writer_less ? amount : writer - (reader + 1)), amount);
|
||||
|
||||
if (total == 0)
|
||||
return 0;
|
||||
|
||||
// fprintf(stderr, "%p> p full read: %d/%d [r=%d/w=%d]\n", this, total, size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
memcpy((void *) value, (const void *) &(buffer[reader]), total);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
unsigned int new_reader = (((cache.reader.complete * _block) + cache.reader.partial) + total) % size;
|
||||
|
||||
/* jump the reader forward */
|
||||
Buffer_pointer index((unsigned int)floor((double)new_reader / (double)_block),
|
||||
(unsigned short)(new_reader % _block));
|
||||
|
||||
if (update(cache.reader, index))
|
||||
{
|
||||
// fprintf(stderr, "%p> p read end: %d [block=%d]\n", this, new_reader, _block);
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (true);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********** IO FUNCTIONS **********/
|
||||
|
||||
/* returns the number of items written to from buffer to stream */
|
||||
unsigned int Ringbuffer_traits::traits_put(const char * buffer, std::ostream &fd, unsigned int amount)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int reader = cache.reader.complete;
|
||||
const unsigned int writer = cache.writer.complete;
|
||||
|
||||
const bool writer_less = writer < reader;
|
||||
|
||||
unsigned int total = 0;
|
||||
|
||||
/* should we go around the buffer for reading? */
|
||||
if (writer_less && (reader + amount >= _size))
|
||||
{
|
||||
total = std::min(_size - (reader - writer + 1), amount);
|
||||
|
||||
if (total == 0)
|
||||
return 0;
|
||||
|
||||
unsigned int rd1 = _size - reader;
|
||||
unsigned int rd2 = total - rd1;
|
||||
|
||||
// fprintf(stderr, "%p> partial read: (%d/%d) %d/%d [%d/%d]\n", this, rd1, rd2, total, _size, reader, writer);
|
||||
|
||||
/* two partial consumes (one at the end, another at the beginning) */
|
||||
fd.write((const char *) &(buffer[reader]), _block * rd1);
|
||||
fd.write((const char *) (buffer), _block * rd2);
|
||||
}
|
||||
else
|
||||
{
|
||||
total = std::min((!writer_less ? writer - (reader + 1) : amount), amount);
|
||||
|
||||
if (total == 0)
|
||||
return 0;
|
||||
|
||||
// fprintf(stderr, "%p> full read: %d/%d [%d/%d]\n", this, total, _size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
fd.write((const char *) &(buffer[reader]), _block * total);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
/* jump the reader forward */
|
||||
Buffer_pointer index(cache.reader);
|
||||
index.complete = (index.complete + total) % _size;
|
||||
|
||||
if (update(cache.reader, index))
|
||||
break;
|
||||
}
|
||||
while (true);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
/* returns number of items read from stream to buffer */
|
||||
unsigned int Ringbuffer_traits::traits_get(char * buffer, std::istream &fd, unsigned int amount)
|
||||
{
|
||||
/* avoid using different values */
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
const unsigned int reader = cache.reader.complete;
|
||||
const unsigned int writer = cache.writer.complete;
|
||||
|
||||
const unsigned int dest = writer - 1;
|
||||
|
||||
const bool reader_less = reader < writer;
|
||||
|
||||
unsigned int real_amount = 0;
|
||||
|
||||
/* should we go around the buffer for writing? */
|
||||
if (reader_less && ((writer + amount) > _size))
|
||||
{
|
||||
/* Documentation of the formula used in the 'if' below.
|
||||
*
|
||||
* [0|1|2|3|4|5|6|7] => size=8
|
||||
* | |
|
||||
* reader |
|
||||
* writer
|
||||
*
|
||||
* => writer has places [5,6,7,0,1] to write (5 places).
|
||||
*
|
||||
* => 8 - (4-2+1) = 8 - (2+1) = 8 - 3 = 5
|
||||
*
|
||||
* > writer goes 1 up, amount goes 1 down.
|
||||
* > reader goes 1 up, amount goes 1 up.
|
||||
* > size goes 1 down, amount goes 1 down.
|
||||
*
|
||||
*/
|
||||
|
||||
if ((_size - (writer - reader + 1)) <= amount)
|
||||
return false;
|
||||
|
||||
unsigned int wr1 = _size - writer + 1; /* writer is already 1 position after */
|
||||
unsigned int wr2 = amount - wr1;
|
||||
|
||||
// fprintf(stderr, "%p> partial write: (%d/%d) %d/%d [%d/%d]\n", this, wr1, wr2, amount, _size, reader, writer);
|
||||
|
||||
unsigned int char_amount = 0;
|
||||
|
||||
/* one partial write on the buffer (at the end) */
|
||||
fd.read((char *) &(buffer[dest]), _block * wr1);
|
||||
char_amount += fd.gcount();
|
||||
|
||||
if (fd.gcount() == (int)(_block * wr1))
|
||||
{
|
||||
/* another partial write on the buffer (at the beginning) */
|
||||
fd.read((char *) (buffer), _block * wr2);
|
||||
char_amount += fd.gcount();
|
||||
}
|
||||
|
||||
real_amount = char_amount / _block;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!reader_less && ((reader - writer) <= amount))
|
||||
return false;
|
||||
|
||||
// fprintf(stderr, "%p> full write: %d/%d [%d/%d]\n", this, amount, _size, reader, writer);
|
||||
|
||||
/* we are talking about buffers here, man! */
|
||||
fd.read((char *) &(buffer[dest]), _block * amount);
|
||||
|
||||
real_amount = fd.gcount() / _block;
|
||||
}
|
||||
|
||||
_pointers.writer.complete = ((dest + real_amount) % _size) + 1;
|
||||
_pointers.writer.partial = 1;
|
||||
|
||||
return real_amount;
|
||||
}
|
||||
416
src/mod/endpoints/mod_khomp/commons/ringbuffer.hpp
Normal file
416
src/mod/endpoints/mod_khomp/commons/ringbuffer.hpp
Normal file
@@ -0,0 +1,416 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
/* WARNING: This is a generic ringbuffer abstraction, which works for single-sized elements,
|
||||
partial elements, single/multi-elements read/writes. It is not wise to mix some
|
||||
functions (partial element write / full element write), since it was not designed
|
||||
with this use in mind.
|
||||
|
||||
Also, it works only for single-reader + single-writer, since it does not depends
|
||||
on external mutex functions.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include <noncopyable.hpp>
|
||||
#include <atomic.hpp>
|
||||
|
||||
#ifndef _RINGBUFFER_HPP_
|
||||
#define _RINGBUFFER_HPP_
|
||||
|
||||
struct Buffer_pointer
|
||||
{
|
||||
Buffer_pointer(unsigned int _complete = 0u, unsigned short _partial = 0u)
|
||||
: complete(_complete), partial(_partial)
|
||||
{};
|
||||
|
||||
Buffer_pointer(const Buffer_pointer & o)
|
||||
: complete(o.complete), partial(o.partial)
|
||||
{};
|
||||
|
||||
Buffer_pointer(const volatile Buffer_pointer & o)
|
||||
: complete(o.complete), partial(o.partial)
|
||||
{};
|
||||
|
||||
void operator=(const volatile Buffer_pointer o)
|
||||
{
|
||||
complete = o.complete;
|
||||
partial = o.partial;
|
||||
}
|
||||
|
||||
void operator=(const Buffer_pointer o) volatile
|
||||
{
|
||||
complete = o.complete;
|
||||
partial = o.partial;
|
||||
}
|
||||
|
||||
bool operator==(const Buffer_pointer & o)
|
||||
{
|
||||
return (complete == o.complete && partial == o.partial);
|
||||
}
|
||||
|
||||
unsigned int complete:20;
|
||||
unsigned short partial:12;
|
||||
}
|
||||
__attribute__((packed));
|
||||
|
||||
struct Buffer_table
|
||||
{
|
||||
Buffer_table()
|
||||
: reader(0,0),
|
||||
writer(1,1)
|
||||
{};
|
||||
|
||||
Buffer_table(const Buffer_table & o)
|
||||
: reader(o.reader), writer(o.writer)
|
||||
{};
|
||||
|
||||
Buffer_table(const volatile Buffer_table & o)
|
||||
: reader(o.reader), writer(o.writer)
|
||||
{};
|
||||
|
||||
void operator=(const volatile Buffer_table o)
|
||||
{
|
||||
reader = o.reader;
|
||||
writer = o.writer;
|
||||
}
|
||||
|
||||
void operator=(const Buffer_table o) volatile
|
||||
{
|
||||
reader = o.reader;
|
||||
writer = o.writer;
|
||||
}
|
||||
|
||||
bool operator==(const Buffer_table & o)
|
||||
{
|
||||
return (reader == o.reader && writer == o.writer);
|
||||
}
|
||||
|
||||
Buffer_pointer reader;
|
||||
Buffer_pointer writer;
|
||||
}
|
||||
__attribute__((packed));
|
||||
|
||||
struct Ringbuffer_traits
|
||||
{
|
||||
Ringbuffer_traits(unsigned int block, unsigned int size)
|
||||
: _block(block), _size(size)
|
||||
{};
|
||||
|
||||
bool traits_provide( char *, const char *, unsigned int, bool);
|
||||
unsigned int traits_consume(const char *, char *, unsigned int, bool);
|
||||
|
||||
unsigned int traits_consume_begins(const char *, char *, unsigned int, bool);
|
||||
bool traits_consume_commit(unsigned int);
|
||||
|
||||
bool traits_provide_partial( char *, const char *, unsigned int);
|
||||
unsigned int traits_consume_partial(const char *, char *, unsigned int);
|
||||
|
||||
unsigned int traits_get( char *, std::istream &, unsigned int);
|
||||
unsigned int traits_put(const char *, std::ostream &, unsigned int);
|
||||
|
||||
bool update(Buffer_pointer & cache, Buffer_pointer & update)
|
||||
{
|
||||
return Atomic::doCAS(&(_pointers.reader), &cache, update);
|
||||
}
|
||||
|
||||
protected:
|
||||
const unsigned int _block;
|
||||
const unsigned int _size;
|
||||
|
||||
volatile Buffer_table _pointers;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Ringbuffer: protected Ringbuffer_traits, public NonCopyable
|
||||
{
|
||||
struct BufferFull {};
|
||||
struct BufferEmpty {};
|
||||
|
||||
Ringbuffer(unsigned int size)
|
||||
: Ringbuffer_traits(sizeof(T), size)
|
||||
{
|
||||
_buffer = new T[_size];
|
||||
_malloc = true;
|
||||
};
|
||||
|
||||
Ringbuffer(unsigned int size, T * buffer)
|
||||
: Ringbuffer_traits(sizeof(T), size)
|
||||
{
|
||||
_buffer = buffer;
|
||||
_malloc = false;
|
||||
};
|
||||
|
||||
~Ringbuffer()
|
||||
{
|
||||
if (_malloc)
|
||||
delete[] _buffer;
|
||||
}
|
||||
|
||||
/***** GENERIC RANGE/INDEX CALCULATION FUNCTIONS *****/
|
||||
bool may_write(Buffer_table & cache)
|
||||
{
|
||||
const unsigned int r = cache.reader.complete;
|
||||
const unsigned int w = cache.writer.complete;
|
||||
|
||||
return (((r - w) != 1) && (!(r == 0 && w == _size)));
|
||||
}
|
||||
|
||||
bool may_read(Buffer_table & cache)
|
||||
{
|
||||
if ((cache.writer.complete - cache.reader.complete) == 1)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned int writer_next(Buffer_pointer & cache, Buffer_pointer & index)
|
||||
{
|
||||
unsigned int dest = cache.complete - 1,
|
||||
temp = cache.complete + 1;
|
||||
|
||||
if (temp > _size) index.complete = 1;
|
||||
else index.complete = temp;
|
||||
|
||||
index.partial = 1;
|
||||
|
||||
return dest;
|
||||
};
|
||||
|
||||
void reader_next(Buffer_pointer & cache, Buffer_pointer & index)
|
||||
{
|
||||
unsigned int temp = cache.complete + 1;
|
||||
|
||||
if (temp == _size) index.complete = 0;
|
||||
else index.complete = temp;
|
||||
|
||||
index.partial = 0;
|
||||
}
|
||||
|
||||
/***** BUFFER FUNCTIONS *****/
|
||||
|
||||
bool provide(const T & value)
|
||||
{
|
||||
Buffer_table cache = _pointers;
|
||||
Buffer_pointer index = _pointers.writer;
|
||||
|
||||
if (!may_write(cache))
|
||||
return false;
|
||||
|
||||
// fprintf(stderr, "%p> provide %d/%d!\n", this, reader, writer);
|
||||
|
||||
unsigned int dest = writer_next(cache.writer, index);
|
||||
|
||||
_buffer[dest] = value;
|
||||
|
||||
_pointers.writer = index;
|
||||
|
||||
// fprintf(stderr, "%p> write: %d/%d [%d/%d]\n", this, _pointers.reader, _pointers.writer, _pointers.reader_partial, _pointers.writer_partial);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool consume(T & value)
|
||||
{
|
||||
Buffer_table cache = _pointers;
|
||||
Buffer_pointer index = _pointers.reader;
|
||||
|
||||
if (!may_read(cache))
|
||||
return false;
|
||||
|
||||
// fprintf(stderr, "%p> consume %d/%d!\n", this, reader, writer);
|
||||
|
||||
value = _buffer[index.complete];
|
||||
|
||||
do
|
||||
{
|
||||
reader_next(cache.reader, index);
|
||||
|
||||
if (update(cache.reader, index))
|
||||
break;
|
||||
|
||||
cache.reader = index;
|
||||
}
|
||||
while (true);
|
||||
|
||||
// fprintf(stderr, "%p> read: %d/%d [%d/%d]\n", this, _pointers.reader, _pointers.writer, _pointers.reader_partial, _pointers.writer_partial);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* writes everything or nothing */
|
||||
inline bool provide(const T * value, unsigned int amount, bool skip_overwrite = true)
|
||||
{
|
||||
return traits_provide((char *)_buffer, (const char *) value, amount, skip_overwrite);
|
||||
}
|
||||
|
||||
/* returns the number of items that have been read (atomic_mode == true means 'all or nothing') */
|
||||
inline unsigned int consume(T * value, unsigned int amount, bool atomic_mode = false)
|
||||
{
|
||||
return traits_consume((const char *)_buffer, (char *) value, amount, atomic_mode);
|
||||
}
|
||||
|
||||
/***** TWO-PHASE BUFFER FUNCTIONS *****/
|
||||
|
||||
/* returns the number of items that have been read (atomic_mode == true means 'all or nothing') */
|
||||
inline unsigned int consume_begins(T * value, unsigned int amount, bool atomic_mode = false)
|
||||
{
|
||||
return traits_consume_begins((const char *)_buffer, (char *) value, amount, atomic_mode);
|
||||
}
|
||||
|
||||
/* returns true if we could commit that much of buffer (use only after consume_begins). *
|
||||
* note: you may commit less bytes that have been read to keep some data inside the buffer */
|
||||
inline bool consume_commit(unsigned int amount)
|
||||
{
|
||||
return traits_consume_commit(amount);
|
||||
}
|
||||
|
||||
/***** TWO-PHASE SINGLE-ELEMENT BUFFER FUNCTIONS *****/
|
||||
|
||||
T & provider_start(void)
|
||||
{
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
if (!may_write(cache))
|
||||
throw BufferFull();
|
||||
|
||||
unsigned writer = _pointers.writer.complete - 1;
|
||||
|
||||
// fprintf(stderr, "%p> provider start %d/%d!\n", this, reader, writer);
|
||||
|
||||
return _buffer[writer];
|
||||
}
|
||||
|
||||
void provider_commit(void)
|
||||
{
|
||||
unsigned int temp = _pointers.writer.complete + 1;
|
||||
|
||||
// fprintf(stderr, "%p> provider commit %d!\n", this, temp);
|
||||
|
||||
if (temp > _size)
|
||||
temp = 1;
|
||||
|
||||
_pointers.writer.complete = temp;
|
||||
_pointers.writer.partial = 1;
|
||||
|
||||
// fprintf(stderr, "%p> write: %d/%d [%d/%d]\n", this, _pointers.reader, _pointers.writer, _pointers.reader_partial, _pointers.writer_partial);
|
||||
}
|
||||
|
||||
T & consumer_start(void)
|
||||
{
|
||||
Buffer_table cache = _pointers;
|
||||
|
||||
if (!may_read(cache))
|
||||
throw BufferEmpty();
|
||||
|
||||
unsigned int reader = _pointers.reader.complete;
|
||||
|
||||
// fprintf(stderr, "%p> consumer start %d/%d!\n", this, reader, writer);
|
||||
|
||||
return _buffer[reader];
|
||||
}
|
||||
|
||||
void consumer_commit(void)
|
||||
{
|
||||
Buffer_pointer cache = _pointers.reader;
|
||||
Buffer_pointer index(cache);
|
||||
|
||||
do
|
||||
{
|
||||
reader_next(cache, index);
|
||||
|
||||
if (update(cache, index))
|
||||
break;
|
||||
|
||||
cache = index;
|
||||
}
|
||||
while (true);
|
||||
|
||||
// fprintf(stderr, "%p> consumer commit %d!\n", this, temp);
|
||||
|
||||
// fprintf(stderr, "%p> read: %d/%d [%d/%d]\n", this, _pointers.reader, _pointers.writer, _pointers.reader_partial, _pointers.writer_partial);
|
||||
}
|
||||
|
||||
/* writes everything or nothing, but works on bytes (may write incomplete elements) */
|
||||
/* WARNING: do not mix this with full element provider */
|
||||
inline bool provider_partial(const char *buffer, unsigned int amount)
|
||||
{
|
||||
return traits_provide_partial((char *)_buffer, buffer, amount);
|
||||
}
|
||||
|
||||
/* returns the number of bytes that have been read (may read incomplete elements) */
|
||||
/* WARNING: do not mix this with full element consumer */
|
||||
inline unsigned int consumer_partial(char *buffer, unsigned int amount)
|
||||
{
|
||||
return traits_consume_partial((const char *)_buffer, buffer, amount);
|
||||
}
|
||||
|
||||
/***** IO FUNCTIONS *****/
|
||||
|
||||
/* returns the number of items written to from buffer to stream */
|
||||
inline unsigned int put(std::ostream &fd, unsigned int amount)
|
||||
{
|
||||
return traits_put((char *)_buffer, fd, amount);
|
||||
}
|
||||
|
||||
/* returns number of items read from stream to buffer */
|
||||
inline unsigned int get(std::istream &fd, unsigned int amount)
|
||||
{
|
||||
return traits_get((const char *)_buffer, fd, amount);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_pointers.reader.complete = 0;
|
||||
_pointers.reader.partial = 0;
|
||||
_pointers.writer.complete = 1;
|
||||
_pointers.writer.partial = 1;
|
||||
}
|
||||
|
||||
protected:
|
||||
T * _buffer;
|
||||
bool _malloc;
|
||||
};
|
||||
|
||||
#endif /* _RINGBUFFER_HPP_ */
|
||||
46
src/mod/endpoints/mod_khomp/commons/saved_condition.cpp
Normal file
46
src/mod/endpoints/mod_khomp/commons/saved_condition.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <config_commons.hpp>
|
||||
|
||||
#include COMMONS_INCLUDE(saved_condition.cpp)
|
||||
|
||||
61
src/mod/endpoints/mod_khomp/commons/saved_condition.hpp
Normal file
61
src/mod/endpoints/mod_khomp/commons/saved_condition.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _SAVED_CONDITION_COMMON_HPP_
|
||||
#define _SAVED_CONDITION_COMMON_HPP_
|
||||
|
||||
#include <config_commons.hpp>
|
||||
|
||||
//#include <refcounter.hpp>
|
||||
|
||||
struct SavedConditionCommon
|
||||
{
|
||||
SavedConditionCommon() : _signaled(false) {}
|
||||
|
||||
protected:
|
||||
bool _signaled;
|
||||
};
|
||||
|
||||
|
||||
#include COMMONS_INCLUDE(saved_condition.hpp)
|
||||
|
||||
#endif /* _SAVED_CONDITION_COMMON_HPP_ */
|
||||
|
||||
95
src/mod/endpoints/mod_khomp/commons/scoped_lock.hpp
Normal file
95
src/mod/endpoints/mod_khomp/commons/scoped_lock.hpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <simple_lock.hpp>
|
||||
|
||||
struct ScopedLockBasic
|
||||
{
|
||||
ScopedLockBasic(void)
|
||||
: _locked(true) {};
|
||||
|
||||
ScopedLockBasic(bool locked)
|
||||
: _locked(locked) {};
|
||||
|
||||
virtual ~ScopedLockBasic() {};
|
||||
|
||||
protected:
|
||||
bool _locked;
|
||||
};
|
||||
|
||||
|
||||
struct ScopedLock : public ScopedLockBasic
|
||||
{
|
||||
struct LockFailed {};
|
||||
|
||||
ScopedLock(SimpleLock & mutex)
|
||||
: ScopedLockBasic(false), _mutex(mutex)
|
||||
{
|
||||
switch (_mutex.lock())
|
||||
{
|
||||
case SimpleLock::ISINUSE:
|
||||
case SimpleLock::FAILURE:
|
||||
throw LockFailed();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
_locked = true;
|
||||
}
|
||||
|
||||
~ScopedLock()
|
||||
{
|
||||
unlock();
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
if (_locked)
|
||||
{
|
||||
_locked = false;
|
||||
_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
SimpleLock & _mutex;
|
||||
};
|
||||
|
||||
104
src/mod/endpoints/mod_khomp/commons/simple_lock.hpp
Normal file
104
src/mod/endpoints/mod_khomp/commons/simple_lock.hpp
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _SIMPLE_LOCK_COMMON_HPP_
|
||||
#define _SIMPLE_LOCK_COMMON_HPP_
|
||||
|
||||
#include <config_commons.hpp>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <noncopyable.hpp>
|
||||
|
||||
#include <refcounter.hpp>
|
||||
|
||||
/* This struct uses static polymorphism, and derived classes should implement *
|
||||
* the "lock/trylock/unlock()" methods for correct code compilation. *
|
||||
* The base class also features reference counting, so derived classes should *
|
||||
* implement the "unreference()" method for releasing resources. */
|
||||
|
||||
template < typename Implementor >
|
||||
struct SimpleLockCommon: NEW_REFCOUNTER( SimpleLockCommon < Implementor > )
|
||||
{
|
||||
friend class ReferenceCounter < SimpleLockCommon < Implementor > >;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ISINUSE = 0, // operation not succeded (no error)
|
||||
SUCCESS = 1, // operation succeded (no error)
|
||||
FAILURE = 2, // mutex or state is somehow invalid (error! run for your life!)
|
||||
}
|
||||
Result;
|
||||
|
||||
SimpleLockCommon()
|
||||
{};
|
||||
|
||||
SimpleLockCommon(const SimpleLockCommon & o)
|
||||
: INC_REFCOUNTER(o, SimpleLockCommon)
|
||||
{};
|
||||
|
||||
virtual ~SimpleLockCommon()
|
||||
{};
|
||||
|
||||
inline Result lock()
|
||||
{
|
||||
return static_cast<Implementor*>(this)->lock();
|
||||
}
|
||||
|
||||
inline Result trylock()
|
||||
{
|
||||
return static_cast<Implementor*>(this)->trylock();
|
||||
}
|
||||
|
||||
inline void unlock()
|
||||
{
|
||||
static_cast<Implementor*>(this)->unlock();
|
||||
}
|
||||
|
||||
protected:
|
||||
void unreference(void)
|
||||
{
|
||||
static_cast<Implementor*>(this)->unreference();
|
||||
}
|
||||
};
|
||||
|
||||
#include COMMONS_INCLUDE(simple_lock.hpp)
|
||||
|
||||
#endif /* _SIMPLE_LOCK_COMMON_HPP_ */
|
||||
247
src/mod/endpoints/mod_khomp/commons/strings.cpp
Normal file
247
src/mod/endpoints/mod_khomp/commons/strings.cpp
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <strings.hpp>
|
||||
|
||||
void Strings::Merger::add(std::string s)
|
||||
{
|
||||
_list.push_back(s);
|
||||
};
|
||||
|
||||
std::string Strings::Merger::merge(const std::string & sep)
|
||||
{
|
||||
list_type::iterator i = _list.begin();
|
||||
|
||||
std::string res;
|
||||
|
||||
if (i != _list.end())
|
||||
{
|
||||
res += (*i);
|
||||
++i;
|
||||
};
|
||||
|
||||
while (i != _list.end())
|
||||
{
|
||||
res += sep;
|
||||
res += (*i);
|
||||
++i;
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
std::string Strings::Merger::merge(const char *sep)
|
||||
{
|
||||
std::string ssep(sep);
|
||||
return merge(ssep);
|
||||
}
|
||||
|
||||
unsigned int Strings::tokenize(const std::string & str, Strings::vector_type & tokens,
|
||||
const std::string & delims, long int max_tokens, bool keep_empty)
|
||||
{
|
||||
std::string::size_type base = 0;
|
||||
|
||||
std::string::size_type init = str.find_first_not_of(delims, 0);
|
||||
std::string::size_type fini = str.find_first_of(delims, init);
|
||||
|
||||
long int cur_token = 1;
|
||||
|
||||
while (std::string::npos != init)
|
||||
{
|
||||
if (keep_empty && base < init)
|
||||
{
|
||||
std::string::size_type cur_empty = init - base;
|
||||
|
||||
while (cur_empty && cur_token < max_tokens)
|
||||
{
|
||||
tokens.push_back("");
|
||||
|
||||
++cur_token;
|
||||
--cur_empty;
|
||||
}
|
||||
}
|
||||
|
||||
if (std::string::npos != fini && cur_token < max_tokens)
|
||||
{
|
||||
base = fini + 1;
|
||||
|
||||
tokens.push_back(str.substr(init, fini - init));
|
||||
++cur_token;
|
||||
}
|
||||
else
|
||||
{
|
||||
base = str.size(); // find_first_of(delims, init);
|
||||
|
||||
tokens.push_back(str.substr(init, str.size() - init));
|
||||
break;
|
||||
}
|
||||
|
||||
init = str.find_first_not_of(delims, fini);
|
||||
fini = str.find_first_of(delims, init);
|
||||
}
|
||||
|
||||
if (keep_empty && base != str.size())
|
||||
{
|
||||
std::string::size_type cur_empty = str.size() - base + 1;
|
||||
|
||||
while (cur_empty && cur_token < max_tokens)
|
||||
{
|
||||
tokens.push_back("");
|
||||
|
||||
++cur_token;
|
||||
--cur_empty;
|
||||
}
|
||||
|
||||
if (cur_empty)
|
||||
{
|
||||
std::string::size_type pos = base + cur_empty - 1;
|
||||
tokens.push_back(str.substr(pos, str.size() - pos));
|
||||
++cur_token;
|
||||
}
|
||||
}
|
||||
|
||||
return (cur_token - 1);
|
||||
}
|
||||
|
||||
bool Strings::toboolean(std::string str)
|
||||
{
|
||||
std::string tmp(str);
|
||||
|
||||
Strings::lower(tmp);
|
||||
|
||||
if ((tmp == "true") || (tmp == "yes")) return true;
|
||||
if ((tmp == "false") || (tmp == "no")) return false;
|
||||
|
||||
throw invalid_value(str);
|
||||
}
|
||||
|
||||
long Strings::tolong(std::string str, int base)
|
||||
{
|
||||
char *str_end = 0;
|
||||
|
||||
unsigned long value = strtol(str.c_str(), &str_end, base);
|
||||
|
||||
if (str_end && *str_end == 0)
|
||||
return value;
|
||||
|
||||
throw invalid_value(str);
|
||||
}
|
||||
|
||||
unsigned long Strings::toulong(std::string str, int base)
|
||||
{
|
||||
char *str_end = 0;
|
||||
|
||||
unsigned long value = strtoul(str.c_str(), &str_end, base);
|
||||
|
||||
if (str_end && *str_end == 0)
|
||||
return value;
|
||||
|
||||
throw invalid_value(str);
|
||||
}
|
||||
|
||||
unsigned long long Strings::toulonglong(std::string str, int base)
|
||||
{
|
||||
#if defined(_WINDOWS) || defined(_Windows) || defined(_WIN32) || defined(WIN32)
|
||||
throw not_implemented();
|
||||
#else
|
||||
char *str_end = 0;
|
||||
|
||||
unsigned long long value = strtoull(str.c_str(), &str_end, base);
|
||||
|
||||
if (str_end && *str_end == 0)
|
||||
return value;
|
||||
|
||||
throw invalid_value(str);
|
||||
#endif
|
||||
}
|
||||
|
||||
double Strings::todouble(std::string str)
|
||||
{
|
||||
char *str_end = 0;
|
||||
|
||||
double value = strtod(str.c_str(), &str_end);
|
||||
|
||||
if (str_end && *str_end == 0)
|
||||
return value;
|
||||
|
||||
throw invalid_value(str);
|
||||
}
|
||||
|
||||
std::string Strings::fromboolean(bool value)
|
||||
{
|
||||
if (value) return "true";
|
||||
else return "false";
|
||||
}
|
||||
|
||||
std::string Strings::lower(std::string str)
|
||||
{
|
||||
std::string res;
|
||||
|
||||
for (std::string::iterator i = str.begin(); i != str.end(); i++)
|
||||
res += tolower((*i));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string Strings::hexadecimal(std::string value)
|
||||
{
|
||||
std::string result;
|
||||
|
||||
for (std::string::iterator i = value.begin(); i != value.end(); i++)
|
||||
{
|
||||
if (i != value.begin())
|
||||
result += " ";
|
||||
|
||||
result += STG(FMT("%2x") % (unsigned int)(*i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Strings::trim(const std::string& str, const std::string& trim_chars)
|
||||
{
|
||||
std::string result(str);
|
||||
|
||||
result.erase( result.find_last_not_of( trim_chars ) + 1 );
|
||||
result.erase( 0, result.find_first_not_of( trim_chars ) );
|
||||
|
||||
return result;
|
||||
}
|
||||
106
src/mod/endpoints/mod_khomp/commons/strings.hpp
Normal file
106
src/mod/endpoints/mod_khomp/commons/strings.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <types.hpp>
|
||||
|
||||
#include <format.hpp>
|
||||
|
||||
/* Generic string funcions */
|
||||
|
||||
#ifndef _STRINGS_HPP_
|
||||
#define _STRINGS_HPP_
|
||||
|
||||
struct Strings
|
||||
{
|
||||
typedef std::list<std::string> list_type;
|
||||
typedef std::vector<std::string> vector_type;
|
||||
|
||||
struct Merger
|
||||
{
|
||||
void add(std::string);
|
||||
|
||||
std::string merge(const std::string &);
|
||||
std::string merge(const char *);
|
||||
|
||||
bool empty() { return _list.empty(); };
|
||||
|
||||
protected:
|
||||
list_type _list;
|
||||
};
|
||||
|
||||
public:
|
||||
struct invalid_value
|
||||
{
|
||||
invalid_value(const char * value): _value(value) {};
|
||||
invalid_value(std::string value): _value(value) {};
|
||||
|
||||
std::string & value() { return _value; }
|
||||
|
||||
protected:
|
||||
std::string _value;
|
||||
};
|
||||
|
||||
struct not_implemented {};
|
||||
|
||||
static unsigned int tokenize(const std::string &, vector_type &, const std::string & delims = ",;:",
|
||||
long int max_toxens = LONG_MAX, bool keep_empty = true);
|
||||
|
||||
static bool toboolean(std::string);
|
||||
static std::string fromboolean(bool);
|
||||
|
||||
static long tolong(std::string, int base = 10);
|
||||
static unsigned long toulong(std::string, int base = 10);
|
||||
static unsigned long long toulonglong(std::string, int base = 10);
|
||||
static double todouble(std::string);
|
||||
|
||||
static std::string lower(std::string);
|
||||
static std::string hexadecimal(std::string);
|
||||
|
||||
static std::string trim(const std::string&, const std::string& trim_chars = " \f\n\r\t\v");
|
||||
};
|
||||
|
||||
#endif // _STRINGS_HPP_ //
|
||||
307
src/mod/endpoints/mod_khomp/commons/tagged_union.hpp
Normal file
307
src/mod/endpoints/mod_khomp/commons/tagged_union.hpp
Normal file
@@ -0,0 +1,307 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _TAGGED_UNION_HPP_
|
||||
#define _TAGGED_UNION_HPP_
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Tagged
|
||||
{
|
||||
struct EmptyUnion
|
||||
{
|
||||
friend class Value;
|
||||
|
||||
EmptyUnion()
|
||||
: _adjusted(false) {};
|
||||
|
||||
// copy constructor
|
||||
EmptyUnion(const EmptyUnion & o)
|
||||
: _adjusted(o._adjusted) {};
|
||||
|
||||
// copy assignment operator
|
||||
EmptyUnion & operator=(const EmptyUnion & o)
|
||||
{
|
||||
_adjusted = o._adjusted;
|
||||
return *this;
|
||||
};
|
||||
|
||||
~EmptyUnion() { _adjusted = false; };
|
||||
|
||||
bool operator==(const EmptyUnion & o)
|
||||
{
|
||||
return true;
|
||||
};
|
||||
|
||||
public:
|
||||
void clear(void) { _adjusted = false; };
|
||||
protected:
|
||||
void setup(void) { _adjusted = true; };
|
||||
|
||||
protected:
|
||||
bool value_set(void) { return false; };
|
||||
bool value_get(void) { return false; };
|
||||
|
||||
bool value_check(void) { return false; };
|
||||
|
||||
template < typename S >
|
||||
bool value_visit(S & visitor, typename S::ReturnType & ret)
|
||||
{
|
||||
return false;
|
||||
};
|
||||
|
||||
template < typename S >
|
||||
bool value_visit_void(S & visitor)
|
||||
{
|
||||
return false;
|
||||
};
|
||||
|
||||
bool adjusted() { return _adjusted; };
|
||||
|
||||
private:
|
||||
bool _adjusted;
|
||||
};
|
||||
|
||||
template < typename V, typename E = EmptyUnion >
|
||||
struct Union: public E
|
||||
{
|
||||
friend class Value;
|
||||
|
||||
// default constructor
|
||||
Union()
|
||||
: _value(0) {};
|
||||
|
||||
// constructor with initializer
|
||||
template < typename U >
|
||||
Union( U value )
|
||||
: _value(0)
|
||||
{
|
||||
set(value);
|
||||
};
|
||||
|
||||
// copy constructor
|
||||
Union(const Union & o)
|
||||
: E(static_cast<const E&>(o)),
|
||||
_value( (o._value ? new const V(*(o._value)) : 0) )
|
||||
{};
|
||||
|
||||
// copy assignment operator
|
||||
Union & operator=(const Union & o)
|
||||
{
|
||||
if (_value)
|
||||
{
|
||||
delete _value;
|
||||
_value = 0;
|
||||
}
|
||||
|
||||
if (o._value)
|
||||
{
|
||||
_value = new const V(*(o._value));
|
||||
}
|
||||
|
||||
E::operator=(static_cast<const E&>(o));
|
||||
|
||||
return *this;
|
||||
};
|
||||
|
||||
// destructor
|
||||
~Union()
|
||||
{
|
||||
if (_value)
|
||||
{
|
||||
delete _value;
|
||||
_value = 0;
|
||||
}
|
||||
};
|
||||
|
||||
// equal sign operator
|
||||
template < typename U >
|
||||
void operator=(U value)
|
||||
{
|
||||
set(value);
|
||||
}
|
||||
|
||||
template < typename U >
|
||||
bool check(void)
|
||||
{
|
||||
return value_check(static_cast<const U * const>(0));
|
||||
};
|
||||
|
||||
template < typename U >
|
||||
const U & get(void)
|
||||
{
|
||||
const U * res = 0;
|
||||
|
||||
if (!value_get(&res) || !res)
|
||||
throw std::runtime_error("type mismatch");
|
||||
|
||||
return *res;
|
||||
};
|
||||
|
||||
template < typename U >
|
||||
void set(U val)
|
||||
{
|
||||
if (E::adjusted())
|
||||
clear();
|
||||
|
||||
if (!value_set(val))
|
||||
throw std::runtime_error("unable to set value of invalid type");
|
||||
};
|
||||
|
||||
template < typename S >
|
||||
typename S::ReturnType visit(S visitor)
|
||||
{
|
||||
typename S::ReturnType ret;
|
||||
|
||||
if (!value_visit(visitor, ret))
|
||||
throw std::runtime_error("unable to visit empty value");
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
template < typename S >
|
||||
void visit_void(S visitor)
|
||||
{
|
||||
if (!value_visit_void(visitor))
|
||||
throw std::runtime_error("unable to visit empty value");
|
||||
};
|
||||
|
||||
void clear()
|
||||
{
|
||||
if (_value)
|
||||
{
|
||||
delete _value;
|
||||
_value = 0;
|
||||
}
|
||||
|
||||
E::clear();
|
||||
};
|
||||
|
||||
// compare (equal) operator
|
||||
bool operator==(const Union & o)
|
||||
{
|
||||
bool are_equal = false;
|
||||
|
||||
if (!_value && !(o._value))
|
||||
are_equal = true;
|
||||
|
||||
if (_value && o._value)
|
||||
are_equal = (*_value == *(o._value));
|
||||
|
||||
if (are_equal)
|
||||
return E::operator==(static_cast<const E&>(o));
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// compare types
|
||||
bool sameType(const Union & o)
|
||||
{
|
||||
if ((!(_value) && !(o._value)) || (_value && o._value))
|
||||
return E::operator==(static_cast<const E&>(o));
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
protected:
|
||||
using E::value_set;
|
||||
using E::value_get;
|
||||
|
||||
using E::value_check;
|
||||
using E::value_visit;
|
||||
using E::value_visit_void;
|
||||
|
||||
bool value_set(V val)
|
||||
{
|
||||
_value = new const V(val);
|
||||
E::setup();
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
bool value_get(const V ** val)
|
||||
{
|
||||
if (!_value)
|
||||
return false;
|
||||
|
||||
*val = _value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool value_check(const V * const junk)
|
||||
{
|
||||
(void)junk;
|
||||
return (_value != 0);
|
||||
};
|
||||
|
||||
template < typename S >
|
||||
bool value_visit(S & visitor, typename S::ReturnType & ret)
|
||||
{
|
||||
if (_value)
|
||||
{
|
||||
ret = visitor(*const_cast<V*>(_value));
|
||||
return true;
|
||||
};
|
||||
|
||||
return E::value_visit(visitor, ret);
|
||||
};
|
||||
|
||||
template < typename S >
|
||||
bool value_visit_void(S & visitor)
|
||||
{
|
||||
if (_value)
|
||||
{
|
||||
visitor(*const_cast<V*>(_value));
|
||||
return true;
|
||||
};
|
||||
|
||||
return E::value_visit_void(visitor);
|
||||
};
|
||||
|
||||
private:
|
||||
const V * _value;
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* _TAGGED_UNION_HPP_ */
|
||||
103
src/mod/endpoints/mod_khomp/commons/thread.hpp
Normal file
103
src/mod/endpoints/mod_khomp/commons/thread.hpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _THREAD_COMMON_HPP_
|
||||
#define _THREAD_COMMON_HPP_
|
||||
|
||||
#include <config_commons.hpp>
|
||||
|
||||
#include <types.hpp>
|
||||
//#include <function.hpp>
|
||||
|
||||
struct ThreadCommon
|
||||
{
|
||||
protected:
|
||||
template<typename FuncPtrType>
|
||||
struct DecomposeFuncPtr;
|
||||
|
||||
template<typename ReturnType, typename ArgType>
|
||||
struct DecomposeFuncPtr<ReturnType(*)(ArgType)>
|
||||
{
|
||||
typedef ReturnType Return;
|
||||
};
|
||||
|
||||
template<typename SomeClass>
|
||||
struct DecomposeFuncPtr
|
||||
{
|
||||
typedef void Return;
|
||||
};
|
||||
|
||||
template<typename ReturnType>
|
||||
struct DecomposeFuncPtr<ReturnType(*)()>
|
||||
{
|
||||
typedef ReturnType Return;
|
||||
};
|
||||
|
||||
template< typename FunctionType >
|
||||
struct DecomposeFunction
|
||||
{
|
||||
typedef typename Select < IsClass< FunctionType >::Result, int,
|
||||
typename DecomposeFuncPtr< FunctionType >::Return >::Result
|
||||
Return;
|
||||
};
|
||||
|
||||
public:
|
||||
struct ThreadDataCommon
|
||||
{
|
||||
ThreadDataCommon() {}
|
||||
|
||||
virtual ~ThreadDataCommon() {}
|
||||
|
||||
virtual int run() = 0;
|
||||
|
||||
ThreadCommon * _thread;
|
||||
|
||||
void * _self;
|
||||
void * _attribute;
|
||||
};
|
||||
|
||||
ThreadCommon() {}
|
||||
};
|
||||
|
||||
|
||||
#include COMMONS_INCLUDE(thread.hpp)
|
||||
|
||||
#endif /* _THREAD_COMMON_HPP_ */
|
||||
352
src/mod/endpoints/mod_khomp/commons/timer.cpp
Normal file
352
src/mod/endpoints/mod_khomp/commons/timer.cpp
Normal file
@@ -0,0 +1,352 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <timer.hpp>
|
||||
|
||||
TimerTraits::TimerTraits()
|
||||
: _thread((Thread*)0), _purify(false), _last_tick(0), _age_count(0), _shutdown(false)
|
||||
{};
|
||||
|
||||
bool TimerTraits::start (void)
|
||||
{
|
||||
_shutdown = false;
|
||||
|
||||
_condition.reset();
|
||||
_finalized.reset();
|
||||
|
||||
if (!_thread)
|
||||
{
|
||||
_thread = new Thread(TimerTraits::loop_thread(this));
|
||||
_purify = true;
|
||||
}
|
||||
|
||||
#if defined(_WINDOWS) || defined(_Windows) || defined(_WIN32) || defined(WIN32)
|
||||
// set priority ...
|
||||
#else
|
||||
pthread_attr_t attrs;
|
||||
sched_param param;
|
||||
|
||||
if (pthread_attr_init(&attrs) < 0)
|
||||
return false;
|
||||
|
||||
if (pthread_attr_setschedpolicy(&attrs, SCHED_RR) < 0)
|
||||
return false;
|
||||
|
||||
if (pthread_attr_getschedparam(&attrs, ¶m) < 0)
|
||||
return false;
|
||||
|
||||
param.sched_priority = sched_get_priority_max(SCHED_RR);
|
||||
|
||||
if (pthread_attr_setschedparam(&attrs, ¶m) < 0)
|
||||
return false;
|
||||
|
||||
// set priority...
|
||||
|
||||
// if (pthread_create(&_thread, &attrs, TimerTraits::loop_thread, NULL) < 0)
|
||||
// return false;
|
||||
|
||||
_thread->start();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TimerTraits::stop (void)
|
||||
{
|
||||
_shutdown = true;
|
||||
|
||||
_condition.signal();
|
||||
|
||||
_finalized.wait(10000); /* 10 seconds max wait */
|
||||
|
||||
if (_thread && _purify)
|
||||
{
|
||||
delete _thread;
|
||||
|
||||
_thread = (Thread *)0;
|
||||
_purify = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------
|
||||
|
||||
int TimerTraits::loop_thread::operator()(void)
|
||||
{
|
||||
try
|
||||
{
|
||||
_timer->loop();
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
/* something wrong happened! */
|
||||
}
|
||||
|
||||
_timer->_finalized.signal();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TimerTraits::execute(ControlSet::iterator init, const TimerTraits::Control & ctrl)
|
||||
{
|
||||
volatile CallbackFuncType func = (volatile CallbackFuncType) ctrl._func;
|
||||
volatile CallbackDataType data = (volatile CallbackDataType) ctrl._data;
|
||||
|
||||
_timer_set.erase(init);
|
||||
|
||||
_mutex.unlock();
|
||||
|
||||
func(data);
|
||||
}
|
||||
|
||||
void TimerTraits::loop (void)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (_shutdown) break;
|
||||
|
||||
_mutex.lock();
|
||||
|
||||
ControlSet::iterator init = _timer_set.begin();
|
||||
|
||||
if (init == _timer_set.end())
|
||||
{
|
||||
_mutex.unlock();
|
||||
_condition.wait();
|
||||
}
|
||||
else
|
||||
{
|
||||
const Control & ctrl = *init;
|
||||
|
||||
unsigned int ts_now = TimerTraits::tick();
|
||||
|
||||
if (_age_count == ctrl._age)
|
||||
{
|
||||
if (ts_now < ctrl._msecs)
|
||||
{
|
||||
/* age is right, but it is not time to expire yet... */
|
||||
volatile unsigned int wait_time = ctrl._msecs - ts_now;
|
||||
_mutex.unlock();
|
||||
_condition.wait(wait_time); /* expire - now */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* age is right, and we should expire! */
|
||||
execute(init, ctrl); /* called locked, return unlocked */
|
||||
}
|
||||
}
|
||||
else if (_age_count < ctrl._age)
|
||||
{
|
||||
/* age is not there yet (need some time to overlap)... */
|
||||
volatile unsigned int wait_time = (UINT_MAX - ts_now) + ctrl._msecs;
|
||||
_mutex.unlock();
|
||||
_condition.wait(wait_time); /* MAX - now + expire */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* age has passed, we should have expired before! */
|
||||
execute(init, ctrl); /* called locked, return unlocked */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_finalized.signal();
|
||||
}
|
||||
|
||||
unsigned int TimerTraits::tick()
|
||||
{
|
||||
#if defined(_WINDOWS) || defined(_Windows) || defined(_WIN32) || defined(WIN32)
|
||||
unsigned int tick = GetTickCount();
|
||||
#else
|
||||
struct timespec ticks;
|
||||
|
||||
// error condition, make the user notice this..
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ticks) < 0)
|
||||
return 0;
|
||||
|
||||
unsigned int tick = ( ticks.tv_sec * 1000 ) + ( ticks.tv_nsec / 1000000 );
|
||||
#endif
|
||||
|
||||
if (_last_tick > tick)
|
||||
++_age_count;
|
||||
|
||||
_last_tick = tick;
|
||||
|
||||
return tick;
|
||||
}
|
||||
|
||||
//-----------------------------------------
|
||||
|
||||
TimerTraits::Index TimerTraits::traits_add_unlocked (unsigned int msecs, const void * func, const void * data, unsigned int value)
|
||||
{
|
||||
unsigned int ms_tick = TimerTraits::tick();
|
||||
|
||||
unsigned int ms_left = UINT_MAX - ms_tick;
|
||||
unsigned int ms_real = msecs;
|
||||
|
||||
unsigned int age_num = _age_count;
|
||||
|
||||
if (ms_left < msecs)
|
||||
{
|
||||
ms_real -= ms_left;
|
||||
++age_num;
|
||||
}
|
||||
else
|
||||
{
|
||||
ms_real += ms_tick;
|
||||
}
|
||||
|
||||
ControlSet::iterator it = _timer_set.insert(Control(age_num,ms_real,func,data,value));
|
||||
|
||||
if (_timer_set.size() == 1 || _timer_set.begin() == it)
|
||||
{
|
||||
_condition.signal();
|
||||
};
|
||||
|
||||
return Index(age_num, ms_real, msecs, func, data, value);
|
||||
}
|
||||
|
||||
TimerTraits::Index TimerTraits::traits_add (unsigned int msecs, const void * func, const void * data, unsigned int value)
|
||||
{
|
||||
_mutex.lock();
|
||||
|
||||
Index idx = traits_add_unlocked(msecs, func, data, value);
|
||||
|
||||
_mutex.unlock();
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
bool TimerTraits::traits_restart (TimerTraits::Index & idx, bool force)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
_mutex.lock();
|
||||
|
||||
if (idx.valid)
|
||||
{
|
||||
if (traits_del_unlocked(idx) || force)
|
||||
{
|
||||
idx = traits_add_unlocked(idx.delta, idx.func, idx.data, idx.value);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
|
||||
_mutex.unlock();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool TimerTraits::traits_del_unlocked (TimerTraits::Index & idx)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
if (idx.valid)
|
||||
{
|
||||
ControlSet::iterator i = _timer_set.lower_bound(Control(idx.era, idx.msec));
|
||||
ControlSet::iterator j = _timer_set.upper_bound(Control(idx.era, idx.msec));
|
||||
|
||||
for (; i != j; i++)
|
||||
{
|
||||
const Control & ctrl = (*i);
|
||||
|
||||
if ((idx.value && !(ctrl._value & idx.value)))
|
||||
continue;
|
||||
|
||||
if (((idx.func && ctrl._func == idx.func) || !idx.func) && ((idx.data && ctrl._data == idx.data) || !idx.data))
|
||||
{
|
||||
if (_timer_set.begin() == i)
|
||||
_condition.signal();
|
||||
|
||||
_timer_set.erase(i);
|
||||
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
idx.valid = false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool TimerTraits::traits_del (TimerTraits::Index & idx)
|
||||
{
|
||||
_mutex.lock();
|
||||
|
||||
bool ret = traits_del_unlocked(idx);
|
||||
|
||||
_mutex.unlock();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool TimerTraits::traits_del (const void * func, const void * data, unsigned int value)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
_mutex.lock();
|
||||
|
||||
for (ControlSet::iterator i = _timer_set.begin(); i != _timer_set.end(); i++)
|
||||
{
|
||||
const Control & ctrl = (*i);
|
||||
|
||||
if ((value && !(ctrl._value & value)))
|
||||
continue;
|
||||
|
||||
if (((func && ctrl._func == func) || !func) && ((data && ctrl._data == data) || !data))
|
||||
{
|
||||
if (_timer_set.begin() == i)
|
||||
_condition.signal();
|
||||
|
||||
_timer_set.erase(i);
|
||||
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_mutex.unlock();
|
||||
|
||||
return ret;
|
||||
}
|
||||
224
src/mod/endpoints/mod_khomp/commons/timer.hpp
Normal file
224
src/mod/endpoints/mod_khomp/commons/timer.hpp
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#if defined(_WINDOWS) || defined(_Windows) || defined(_WIN32) || defined(WIN32)
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sched.h>
|
||||
#endif
|
||||
|
||||
#include <set>
|
||||
|
||||
#include <simple_lock.hpp>
|
||||
#include <saved_condition.hpp>
|
||||
#include <thread.hpp>
|
||||
#include <refcounter.hpp>
|
||||
|
||||
#ifndef _TIMER_HPP_
|
||||
#define _TIMER_HPP_
|
||||
|
||||
struct TimerTraits
|
||||
{
|
||||
typedef bool (* CallbackFuncType)(const void * volatile);
|
||||
typedef const void * CallbackDataType;
|
||||
|
||||
TimerTraits();
|
||||
|
||||
virtual ~TimerTraits() {};
|
||||
|
||||
protected:
|
||||
|
||||
/* pre-declaration, used below */
|
||||
struct ControlCompare;
|
||||
|
||||
struct Control
|
||||
{
|
||||
Control(unsigned int age, unsigned int msecs, const void * func = 0, const void * data = 0, unsigned int value = 0)
|
||||
: _age(age), _msecs(msecs), _func(func), _data(data), _value(value) {}
|
||||
|
||||
unsigned int _age;
|
||||
|
||||
unsigned int _msecs;
|
||||
|
||||
const void * _func;
|
||||
const void * _data;
|
||||
|
||||
unsigned int _value;
|
||||
};
|
||||
|
||||
struct ControlCompare
|
||||
{
|
||||
bool operator()(const Control & c1, const Control & c2) const
|
||||
{
|
||||
return (c1._age < c2._age ? true : c1._msecs < c2._msecs);
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::multiset < Control, ControlCompare > ControlSet;
|
||||
|
||||
public:
|
||||
struct Index
|
||||
{
|
||||
Index(): era(0), msec(0), valid(false) {};
|
||||
|
||||
Index(unsigned int _era, unsigned int _msec, unsigned int _delta, const void * _func, const void * _data, unsigned int _value)
|
||||
: era(_era), msec(_msec), delta(_delta), func(_func), data(_data), value(_value), valid(true) {};
|
||||
|
||||
unsigned int era;
|
||||
unsigned int msec;
|
||||
|
||||
unsigned int delta;
|
||||
|
||||
const void * func;
|
||||
const void * data;
|
||||
|
||||
unsigned int value;
|
||||
|
||||
bool valid;
|
||||
|
||||
void reset(void) { era = 0; msec = 0; valid = false; };
|
||||
};
|
||||
|
||||
/* timer add/remove functions */
|
||||
Index traits_add (unsigned int msecs, const void * func, const void * data = 0, unsigned int value = 0);
|
||||
|
||||
bool traits_restart (Index & idx, bool force);
|
||||
|
||||
bool traits_del (Index & idx);
|
||||
bool traits_del (const void * func, const void * data = 0, unsigned int value = 0);
|
||||
|
||||
/* timer start/stop functions */
|
||||
bool start(void);
|
||||
bool stop(void);
|
||||
|
||||
protected:
|
||||
Index traits_add_unlocked (unsigned int msecs, const void * func, const void * data, unsigned int value);
|
||||
|
||||
bool traits_del_unlocked (Index & idx);
|
||||
|
||||
protected:
|
||||
void execute(ControlSet::iterator, const Control &);
|
||||
|
||||
void loop(void);
|
||||
|
||||
struct loop_thread
|
||||
{
|
||||
loop_thread(TimerTraits *timer) : _timer(timer) {};
|
||||
|
||||
int operator()(void);
|
||||
|
||||
protected:
|
||||
TimerTraits * _timer;
|
||||
};
|
||||
|
||||
unsigned int tick();
|
||||
|
||||
/* variables */
|
||||
|
||||
SavedCondition _condition;
|
||||
|
||||
SimpleLock _mutex;
|
||||
Thread * _thread;
|
||||
bool _purify;
|
||||
|
||||
ControlSet _timer_set;
|
||||
|
||||
unsigned int _last_tick;
|
||||
unsigned int _age_count;
|
||||
|
||||
SavedCondition _finalized;
|
||||
bool _shutdown;
|
||||
};
|
||||
|
||||
template < typename F, typename D >
|
||||
struct TimerTemplate: NEW_REFCOUNTER(TimerTemplate< F, D >)
|
||||
{
|
||||
typedef TimerTraits::Index Index;
|
||||
typedef TimerTraits::Control Control;
|
||||
|
||||
TimerTemplate()
|
||||
: _timer(new TimerTraits())
|
||||
{};
|
||||
|
||||
TimerTemplate(const TimerTemplate< F, D > & o)
|
||||
: INC_REFCOUNTER(o, TimerTemplate< F, D >),
|
||||
_timer(o._timer)
|
||||
{};
|
||||
|
||||
void unreference(void)
|
||||
{
|
||||
if (_timer)
|
||||
delete _timer;
|
||||
};
|
||||
|
||||
bool start() { return _timer->start(); }
|
||||
bool stop() { return _timer->stop(); }
|
||||
|
||||
inline Index add(unsigned int msecs, F * func, D data = 0, unsigned int value = 0)
|
||||
{
|
||||
return _timer->traits_add(msecs, (const void *)func, (void *)(data), value);
|
||||
}
|
||||
|
||||
inline bool restart(Index & idx, bool force = false)
|
||||
{
|
||||
return _timer->traits_restart(idx, force);
|
||||
}
|
||||
|
||||
inline bool del(Index & idx)
|
||||
{
|
||||
return _timer->traits_del(idx);
|
||||
}
|
||||
|
||||
inline bool del(F * func, D data, unsigned int value = 0)
|
||||
{
|
||||
return _timer->traits_del((const void *)func, (void *)(data), value);
|
||||
}
|
||||
|
||||
inline bool del(unsigned int value)
|
||||
{
|
||||
return _timer->traits_del((const void *)0, (void *)0, value);
|
||||
}
|
||||
|
||||
protected:
|
||||
TimerTraits * _timer;
|
||||
};
|
||||
|
||||
#endif /* _TIMER_HPP_ */
|
||||
81
src/mod/endpoints/mod_khomp/commons/types.hpp
Normal file
81
src/mod/endpoints/mod_khomp/commons/types.hpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#ifndef _TYPES_HPP_
|
||||
#define _TYPES_HPP_
|
||||
|
||||
/*** Used for conditional compilation based on K3L version ***/
|
||||
|
||||
#define K3L_AT_LEAST(major,minor,build) \
|
||||
(((k3lApiMajorVersion == major) && ((k3lApiMinorVersion == minor) && (k3lApiBuildVersion >= build)) || \
|
||||
((k3lApiMajorVersion == major) && (k3lApiMinorVersion > minor))) || \
|
||||
(k3lApiMajorVersion > major))
|
||||
|
||||
#define K3L_EXACT(major,minor,build) \
|
||||
((k3lApiMajorVersion == major) && (k3lApiMinorVersion == minor) && (k3lApiBuildVersion >= build))
|
||||
|
||||
/*** Used for checking information on classes and stuff.. ***/
|
||||
|
||||
template< typename T >
|
||||
class IsClass
|
||||
{
|
||||
protected:
|
||||
template< typename X > static char ( &A( void(X::*)() ) )[1];
|
||||
template< typename X > static char ( &A( X ) )[2];
|
||||
public:
|
||||
static bool const Result = sizeof( A< T >(0) ) == 1;
|
||||
};
|
||||
|
||||
/*** Used for template metaprogramming ***/
|
||||
|
||||
template < bool Value, typename Then, typename Else >
|
||||
struct Select
|
||||
{
|
||||
typedef Then Result;
|
||||
};
|
||||
|
||||
template < typename Then, typename Else >
|
||||
struct Select < false, Then, Else >
|
||||
{
|
||||
typedef Else Result;
|
||||
};
|
||||
|
||||
#endif /* _TYPES_HPP_ */
|
||||
127
src/mod/endpoints/mod_khomp/commons/variant.hpp
Normal file
127
src/mod/endpoints/mod_khomp/commons/variant.hpp
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <refcounter.hpp>
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
#ifndef _VARIANT_H_
|
||||
#define _VARIANT_H_
|
||||
|
||||
/* this is internal, should not be used by the user */
|
||||
struct NoArgumentDefined {};
|
||||
|
||||
template < typename UserReturnType, typename UserArgumentType = NoArgumentDefined >
|
||||
struct VariantBaseType
|
||||
{
|
||||
typedef UserReturnType ReturnType;
|
||||
typedef UserArgumentType ArgumentType;
|
||||
|
||||
virtual ~VariantBaseType() {};
|
||||
|
||||
virtual int which() = 0;
|
||||
|
||||
virtual ReturnType visit(void) { return ReturnType(); };
|
||||
virtual ReturnType visit(ArgumentType) { return ReturnType(); };
|
||||
};
|
||||
|
||||
template < typename BaseType = VariantBaseType < void > >
|
||||
struct Variant: NEW_REFCOUNTER(Variant < BaseType >)
|
||||
{
|
||||
typedef typename BaseType::ReturnType ReturnType;
|
||||
typedef typename BaseType::ArgumentType ArgumentType;
|
||||
|
||||
struct InvalidType {};
|
||||
|
||||
Variant(BaseType * value, bool is_owner = false)
|
||||
: _value(value), _is_owner(is_owner) {};
|
||||
|
||||
Variant(const Variant & v)
|
||||
: INC_REFCOUNTER(v, Variant < BaseType >),
|
||||
_value(v._value), _is_owner(v._is_owner) {};
|
||||
|
||||
virtual ~Variant() {};
|
||||
|
||||
void unreference()
|
||||
{
|
||||
if (_is_owner && _value)
|
||||
{
|
||||
delete _value;
|
||||
_value = 0;
|
||||
}
|
||||
};
|
||||
|
||||
template < typename ValueType >
|
||||
ValueType & get(void)
|
||||
{
|
||||
try
|
||||
{
|
||||
ValueType & ret = dynamic_cast < ValueType & > (*_value);
|
||||
return ret;
|
||||
}
|
||||
catch (std::bad_cast & e)
|
||||
{
|
||||
throw InvalidType();
|
||||
}
|
||||
};
|
||||
|
||||
int which()
|
||||
{
|
||||
return _value->which();
|
||||
}
|
||||
|
||||
ReturnType visit(void)
|
||||
{
|
||||
return _value->visit();
|
||||
}
|
||||
|
||||
ReturnType visit(ArgumentType arg)
|
||||
{
|
||||
return _value->visit(arg);
|
||||
}
|
||||
|
||||
protected:
|
||||
BaseType * _value;
|
||||
bool _is_owner;
|
||||
};
|
||||
|
||||
#endif /* _VARIANT_H_ */
|
||||
|
||||
3188
src/mod/endpoints/mod_khomp/commons/verbose.cpp
Normal file
3188
src/mod/endpoints/mod_khomp/commons/verbose.cpp
Normal file
File diff suppressed because it is too large
Load Diff
505
src/mod/endpoints/mod_khomp/commons/verbose.hpp
Normal file
505
src/mod/endpoints/mod_khomp/commons/verbose.hpp
Normal file
@@ -0,0 +1,505 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
|
||||
#include <k3l.h>
|
||||
|
||||
// k3lApiMajorVersion
|
||||
#ifndef CM_PING
|
||||
# include <k3lVersion.h>
|
||||
# include <KTools.h>
|
||||
#endif
|
||||
|
||||
#include <types.hpp>
|
||||
#include <k3lapi.hpp>
|
||||
#include <format.hpp>
|
||||
|
||||
#ifndef _VERBOSE_HPP_
|
||||
#define _VERBOSE_HPP_
|
||||
|
||||
struct Verbose
|
||||
{
|
||||
typedef enum
|
||||
{
|
||||
K_CM_SEIZE = CM_SEIZE,
|
||||
K_CM_SYNC_SEIZE = CM_SYNC_SEIZE,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_CM_SIP_REGISTER = CM_SIP_REGISTER,
|
||||
#endif
|
||||
K_CM_DIAL_DTMF = CM_DIAL_DTMF,
|
||||
K_CM_DISCONNECT = CM_DISCONNECT,
|
||||
K_CM_CONNECT = CM_CONNECT,
|
||||
K_CM_PRE_CONNECT = CM_PRE_CONNECT,
|
||||
K_CM_CAS_CHANGE_LINE_STT = CM_CAS_CHANGE_LINE_STT,
|
||||
K_CM_CAS_SEND_MFC = CM_CAS_SEND_MFC,
|
||||
K_CM_SET_FORWARD_CHANNEL = CM_SET_FORWARD_CHANNEL,
|
||||
K_CM_CAS_SET_MFC_DETECT_MODE = CM_CAS_SET_MFC_DETECT_MODE,
|
||||
K_CM_DROP_COLLECT_CALL = CM_DROP_COLLECT_CALL,
|
||||
K_CM_MAKE_CALL = CM_MAKE_CALL,
|
||||
K_CM_RINGBACK = CM_RINGBACK,
|
||||
K_CM_USER_INFORMATION = CM_USER_INFORMATION,
|
||||
#if !K3L_AT_LEAST(2,2,0)
|
||||
K_CM_VOIP_SEIZE = CM_VOIP_SEIZE, //deprecated
|
||||
#endif
|
||||
K_CM_LOCK_INCOMING = CM_LOCK_INCOMING,
|
||||
K_CM_UNLOCK_INCOMING = CM_UNLOCK_INCOMING,
|
||||
K_CM_LOCK_OUTGOING = CM_LOCK_OUTGOING,
|
||||
K_CM_UNLOCK_OUTGOING = CM_UNLOCK_OUTGOING,
|
||||
K_CM_START_SEND_FAIL = CM_START_SEND_FAIL,
|
||||
K_CM_STOP_SEND_FAIL = CM_STOP_SEND_FAIL,
|
||||
K_CM_END_OF_NUMBER = CM_END_OF_NUMBER,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_CM_SS_TRANSFER = CM_SS_TRANSFER,
|
||||
K_CM_GET_SMS = CM_GET_SMS,
|
||||
K_CM_PREPARE_SMS = CM_PREPARE_SMS,
|
||||
K_CM_SEND_SMS = CM_SEND_SMS,
|
||||
#endif
|
||||
#if K3L_HAS_MPTY_SUPPORT
|
||||
K_CM_HOLD_SWITCH = CM_HOLD_SWITCH,
|
||||
K_CM_MPTY_CONF = CM_MPTY_CONF,
|
||||
K_CM_MPTY_SPLIT = CM_MPTY_SPLIT,
|
||||
#endif
|
||||
K_CM_ENABLE_DTMF_SUPPRESSION = CM_ENABLE_DTMF_SUPPRESSION,
|
||||
K_CM_DISABLE_DTMF_SUPPRESSION = CM_DISABLE_DTMF_SUPPRESSION,
|
||||
K_CM_ENABLE_AUDIO_EVENTS = CM_ENABLE_AUDIO_EVENTS,
|
||||
K_CM_DISABLE_AUDIO_EVENTS = CM_DISABLE_AUDIO_EVENTS,
|
||||
K_CM_ENABLE_CALL_PROGRESS = CM_ENABLE_CALL_PROGRESS,
|
||||
K_CM_DISABLE_CALL_PROGRESS = CM_DISABLE_CALL_PROGRESS,
|
||||
K_CM_FLASH = CM_FLASH,
|
||||
K_CM_ENABLE_PULSE_DETECTION = CM_ENABLE_PULSE_DETECTION,
|
||||
K_CM_DISABLE_PULSE_DETECTION = CM_DISABLE_PULSE_DETECTION,
|
||||
K_CM_ENABLE_ECHO_CANCELLER = CM_ENABLE_ECHO_CANCELLER,
|
||||
K_CM_DISABLE_ECHO_CANCELLER = CM_DISABLE_ECHO_CANCELLER,
|
||||
K_CM_ENABLE_AGC = CM_ENABLE_AGC,
|
||||
K_CM_DISABLE_AGC = CM_DISABLE_AGC,
|
||||
K_CM_ENABLE_HIGH_IMP_EVENTS = CM_ENABLE_HIGH_IMP_EVENTS,
|
||||
K_CM_DISABLE_HIGH_IMP_EVENTS = CM_DISABLE_HIGH_IMP_EVENTS,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_CM_ENABLE_CALL_ANSWER_INFO = CM_ENABLE_CALL_ANSWER_INFO,
|
||||
K_CM_DISABLE_CALL_ANSWER_INFO = CM_DISABLE_CALL_ANSWER_INFO,
|
||||
#endif
|
||||
K_CM_RESET_LINK = CM_RESET_LINK,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_CM_CLEAR_LINK_ERROR_COUNTER = CM_CLEAR_LINK_ERROR_COUNTER,
|
||||
#endif
|
||||
K_CM_SEND_DTMF = CM_SEND_DTMF,
|
||||
K_CM_STOP_AUDIO = CM_STOP_AUDIO,
|
||||
K_CM_HARD_RESET = CM_HARD_RESET,
|
||||
K_CM_SEND_TO_CTBUS = CM_SEND_TO_CTBUS,
|
||||
K_CM_RECV_FROM_CTBUS = CM_RECV_FROM_CTBUS,
|
||||
K_CM_SETUP_H100 = CM_SETUP_H100,
|
||||
K_CM_MIXER = CM_MIXER,
|
||||
K_CM_CLEAR_MIXER = CM_CLEAR_MIXER,
|
||||
K_CM_PLAY_FROM_FILE = CM_PLAY_FROM_FILE,
|
||||
K_CM_RECORD_TO_FILE = CM_RECORD_TO_FILE,
|
||||
K_CM_PLAY_FROM_STREAM = CM_PLAY_FROM_STREAM,
|
||||
K_CM_INTERNAL_PLAY = CM_INTERNAL_PLAY,
|
||||
K_CM_STOP_PLAY = CM_STOP_PLAY,
|
||||
K_CM_STOP_RECORD = CM_STOP_RECORD,
|
||||
K_CM_PAUSE_PLAY = CM_PAUSE_PLAY,
|
||||
K_CM_PAUSE_RECORD = CM_PAUSE_RECORD,
|
||||
K_CM_RESUME_PLAY = CM_RESUME_PLAY,
|
||||
K_CM_RESUME_RECORD = CM_RESUME_RECORD,
|
||||
K_CM_INCREASE_VOLUME = CM_INCREASE_VOLUME,
|
||||
K_CM_DECREASE_VOLUME = CM_DECREASE_VOLUME,
|
||||
K_CM_LISTEN = CM_LISTEN,
|
||||
K_CM_STOP_LISTEN = CM_STOP_LISTEN,
|
||||
K_CM_PREPARE_FOR_LISTEN = CM_PREPARE_FOR_LISTEN,
|
||||
K_CM_PLAY_SOUND_CARD = CM_PLAY_SOUND_CARD,
|
||||
K_CM_STOP_SOUND_CARD = CM_STOP_SOUND_CARD,
|
||||
K_CM_MIXER_CTBUS = CM_MIXER_CTBUS,
|
||||
K_CM_PLAY_FROM_STREAM_EX = CM_PLAY_FROM_STREAM_EX,
|
||||
K_CM_INTERNAL_PLAY_EX = CM_INTERNAL_PLAY_EX,
|
||||
K_CM_ENABLE_PLAYER_AGC = CM_ENABLE_PLAYER_AGC,
|
||||
K_CM_DISABLE_PLAYER_AGC = CM_DISABLE_PLAYER_AGC,
|
||||
K_CM_START_STREAM_BUFFER = CM_START_STREAM_BUFFER,
|
||||
K_CM_ADD_STREAM_BUFFER = CM_ADD_STREAM_BUFFER,
|
||||
K_CM_STOP_STREAM_BUFFER = CM_STOP_STREAM_BUFFER,
|
||||
K_CM_SEND_BEEP = CM_SEND_BEEP,
|
||||
K_CM_SEND_BEEP_CONF = CM_SEND_BEEP_CONF,
|
||||
K_CM_ADD_TO_CONF = CM_ADD_TO_CONF,
|
||||
K_CM_REMOVE_FROM_CONF = CM_REMOVE_FROM_CONF,
|
||||
K_CM_RECORD_TO_FILE_EX = CM_RECORD_TO_FILE_EX,
|
||||
K_CM_SET_VOLUME = CM_SET_VOLUME,
|
||||
K_CM_SET_LINE_CONDITION = CM_SET_LINE_CONDITION,
|
||||
K_CM_SEND_LINE_CONDITION = CM_SEND_LINE_CONDITION,
|
||||
K_CM_SET_CALLER_CATEGORY = CM_SET_CALLER_CATEGORY,
|
||||
K_CM_DIAL_MFC = CM_DIAL_MFC,
|
||||
#if !K3L_AT_LEAST(2,0,0)
|
||||
K_CM_VOIP_START_DEBUG = CM_VOIP_START_DEBUG,
|
||||
K_CM_VOIP_STOP_DEBUG = CM_VOIP_STOP_DEBUG,
|
||||
K_CM_VOIP_DUMP_STAT = CM_VOIP_DUMP_STAT,
|
||||
K_CM_ISDN_DEBUG = CM_ISDN_DEBUG,
|
||||
K_CM_PING = CM_PING,
|
||||
K_CM_LOG_REQUEST = CM_LOG_REQUEST,
|
||||
K_CM_LOG_CREATE_DISPATCHER = CM_LOG_CREATE_DISPATCHER,
|
||||
K_CM_LOG_DESTROY_DISPATCHER = CM_LOG_DESTROY_DISPATCHER,
|
||||
#endif
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_CM_START_CADENCE = CM_START_CADENCE,
|
||||
K_CM_STOP_CADENCE = CM_STOP_CADENCE,
|
||||
K_CM_CHECK_NEW_SMS = CM_CHECK_NEW_SMS,
|
||||
K_CM_SEND_TO_MODEM = CM_SEND_TO_MODEM,
|
||||
#endif
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
K_CM_START_FAX_TX = CM_START_FAX_TX,
|
||||
K_CM_STOP_FAX_TX = CM_STOP_FAX_TX,
|
||||
K_CM_ADD_FAX_FILE = CM_ADD_FAX_FILE,
|
||||
K_CM_ADD_FAX_PAGE_BREAK = CM_ADD_FAX_PAGE_BREAK,
|
||||
K_CM_START_FAX_RX = CM_START_FAX_RX,
|
||||
K_CM_STOP_FAX_RX = CM_STOP_FAX_RX,
|
||||
K_CM_SIM_CARD_SELECT = CM_SIM_CARD_SELECT,
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
K_CM_NOTIFY_WATCHDOG = CM_NOTIFY_WATCHDOG,
|
||||
K_CM_STOP_WATCHDOG = CM_STOP_WATCHDOG,
|
||||
K_CM_WATCHDOG_COUNT = CM_WATCHDOG_COUNT,
|
||||
K_CM_START_WATCHDOG = CM_START_WATCHDOG,
|
||||
#endif
|
||||
|
||||
}
|
||||
kcommand;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
K_EV_CHANNEL_FREE = EV_CHANNEL_FREE,
|
||||
K_EV_CONNECT = EV_CONNECT,
|
||||
K_EV_DISCONNECT = EV_DISCONNECT,
|
||||
K_EV_CALL_SUCCESS = EV_CALL_SUCCESS,
|
||||
K_EV_CALL_FAIL = EV_CALL_FAIL,
|
||||
K_EV_NO_ANSWER = EV_NO_ANSWER,
|
||||
K_EV_BILLING_PULSE = EV_BILLING_PULSE,
|
||||
K_EV_SEIZE_SUCCESS = EV_SEIZE_SUCCESS,
|
||||
K_EV_SEIZE_FAIL = EV_SEIZE_FAIL,
|
||||
K_EV_SEIZURE_START = EV_SEIZURE_START,
|
||||
K_EV_CAS_LINE_STT_CHANGED = EV_CAS_LINE_STT_CHANGED,
|
||||
K_EV_CAS_MFC_RECV = EV_CAS_MFC_RECV,
|
||||
K_EV_NEW_CALL = EV_NEW_CALL,
|
||||
K_EV_USER_INFORMATION = EV_USER_INFORMATION,
|
||||
K_EV_DIALED_DIGIT = EV_DIALED_DIGIT,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_EV_SIP_REGISTER_INFO = EV_SIP_REGISTER_INFO,
|
||||
K_EV_RING_DETECTED = EV_RING_DETECTED,
|
||||
#endif
|
||||
K_EV_CALL_HOLD_START = EV_CALL_HOLD_START,
|
||||
K_EV_CALL_HOLD_STOP = EV_CALL_HOLD_STOP,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_EV_SS_TRANSFER_FAIL = EV_SS_TRANSFER_FAIL,
|
||||
K_EV_FLASH = EV_FLASH,
|
||||
#endif
|
||||
K_EV_DTMF_DETECTED = EV_DTMF_DETECTED,
|
||||
K_EV_DTMF_SEND_FINISH = EV_DTMF_SEND_FINISH,
|
||||
K_EV_AUDIO_STATUS = EV_AUDIO_STATUS,
|
||||
K_EV_CADENCE_RECOGNIZED = EV_CADENCE_RECOGNIZED,
|
||||
K_EV_END_OF_STREAM = EV_END_OF_STREAM,
|
||||
K_EV_PULSE_DETECTED = EV_PULSE_DETECTED,
|
||||
K_EV_POLARITY_REVERSAL = EV_POLARITY_REVERSAL,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_EV_ISDN_PROGRESS_INDICATOR = EV_ISDN_PROGRESS_INDICATOR,
|
||||
K_EV_CALL_ANSWER_INFO = EV_CALL_ANSWER_INFO,
|
||||
K_EV_COLLECT_CALL = EV_COLLECT_CALL,
|
||||
K_EV_SIP_DTMF_DETECTED = EV_SIP_DTMF_DETECTED,
|
||||
K_EV_RECV_FROM_MODEM = EV_RECV_FROM_MODEM,
|
||||
K_EV_NEW_SMS = EV_NEW_SMS,
|
||||
K_EV_SMS_INFO = EV_SMS_INFO,
|
||||
K_EV_SMS_DATA = EV_SMS_DATA,
|
||||
K_EV_SMS_SEND_RESULT = EV_SMS_SEND_RESULT,
|
||||
#endif
|
||||
#if K3L_HAS_MPTY_SUPPORT
|
||||
K_EV_CALL_MPTY_START = EV_CALL_MPTY_START,
|
||||
K_EV_CALL_MPTY_STOP = EV_CALL_MPTY_STOP,
|
||||
K_EV_GSM_COMMAND_STATUS = EV_GSM_COMMAND_STATUS,
|
||||
#endif
|
||||
K_EV_CHANNEL_FAIL = EV_CHANNEL_FAIL,
|
||||
K_EV_REFERENCE_FAIL = EV_REFERENCE_FAIL,
|
||||
K_EV_INTERNAL_FAIL = EV_INTERNAL_FAIL,
|
||||
K_EV_HARDWARE_FAIL = EV_HARDWARE_FAIL,
|
||||
K_EV_LINK_STATUS = EV_LINK_STATUS,
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
K_EV_PHYSICAL_LINK_UP = EV_PHYSICAL_LINK_UP,
|
||||
K_EV_PHYSICAL_LINK_DOWN = EV_PHYSICAL_LINK_DOWN,
|
||||
#endif
|
||||
K_EV_CLIENT_RECONNECT = EV_CLIENT_RECONNECT,
|
||||
K_EV_VOIP_SEIZURE = EV_VOIP_SEIZURE,
|
||||
K_EV_SEIZURE = EV_SEIZURE,
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
K_EV_FAX_CHANNEL_FREE = EV_FAX_CHANNEL_FREE,
|
||||
K_EV_FAX_FILE_SENT = EV_FAX_FILE_SENT,
|
||||
K_EV_FAX_FILE_FAIL = EV_FAX_FILE_FAIL,
|
||||
K_EV_FAX_MESSAGE_CONFIRMATION = EV_FAX_MESSAGE_CONFIRMATION,
|
||||
K_EV_FAX_TX_TIMEOUT = EV_FAX_TX_TIMEOUT,
|
||||
K_EV_FAX_PAGE_CONFIRMATION = EV_FAX_PAGE_CONFIRMATION,
|
||||
K_EV_FAX_REMOTE_INFO = EV_FAX_REMOTE_INFO,
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
K_EV_WATCHDOG_COUNT = EV_WATCHDOG_COUNT,
|
||||
#endif
|
||||
|
||||
#if !K3L_AT_LEAST(2,0,0)
|
||||
K_EV_PONG = EV_PONG,
|
||||
#endif
|
||||
}
|
||||
kevent;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
R2_COUNTRY_ARG = 1,
|
||||
R2_COUNTRY_BRA = 2,
|
||||
R2_COUNTRY_CHI = 3,
|
||||
R2_COUNTRY_MEX = 4,
|
||||
R2_COUNTRY_URY = 5,
|
||||
R2_COUNTRY_VEN = 6
|
||||
}
|
||||
R2CountryType;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
HUMAN,
|
||||
EXACT
|
||||
}
|
||||
Presentation;
|
||||
|
||||
/* dynamic (object) stuff */
|
||||
|
||||
Verbose(K3LAPI & api): _api(api) {};
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
std::string event(int32, K3L_EVENT*, R2CountryType r2_country = R2_COUNTRY_BRA,
|
||||
Presentation fmt = HUMAN);
|
||||
#else
|
||||
std::string event(int32, K3L_EVENT*, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
std::string channelStatus(int32, int32, int32, Presentation fmt = HUMAN);
|
||||
|
||||
/* end of dynamic (object) stuff */
|
||||
|
||||
protected:
|
||||
K3LAPI & _api;
|
||||
|
||||
/* used internally */
|
||||
struct internal_not_found {};
|
||||
|
||||
public:
|
||||
|
||||
/* static (class) stuff */
|
||||
|
||||
static std::string echoLocation(KEchoLocation, Presentation fmt = HUMAN);
|
||||
static std::string echoCancellerConfig(KEchoCancellerConfig, Presentation fmt = HUMAN);
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
static std::string event(KSignaling, int32, K3L_EVENT*, R2CountryType = R2_COUNTRY_BRA,
|
||||
Presentation fmt = HUMAN);
|
||||
#else
|
||||
static std::string event(KSignaling, int32, K3L_EVENT*, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
static std::string command(int32, K3L_COMMAND*, R2CountryType = R2_COUNTRY_BRA,
|
||||
Presentation fmt = HUMAN);
|
||||
static std::string command(int32, int32, int32, const char *, R2CountryType = R2_COUNTRY_BRA,
|
||||
Presentation fmt = HUMAN);
|
||||
#else
|
||||
static std::string command(int32, K3L_COMMAND*, Presentation fmt = HUMAN);
|
||||
static std::string command(int32, int32, int32, const char *, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
|
||||
static std::string deviceName(KDeviceType, int32, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string deviceType(KDeviceType, Presentation fmt = HUMAN);
|
||||
static std::string deviceModel(KDeviceType, int32, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string channelFeatures(int32, Presentation fmt = HUMAN);
|
||||
static std::string signaling(KSignaling, Presentation fmt = HUMAN);
|
||||
static std::string systemObject(KSystemObject, Presentation fmt = HUMAN);
|
||||
static std::string mixerTone(KMixerTone, Presentation fmt = HUMAN);
|
||||
static std::string mixerSource(KMixerSource, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string seizeFail(KSeizeFail, Presentation fmt = HUMAN);
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
static std::string callFail(KSignaling, R2CountryType, int32, Presentation fmt = HUMAN);
|
||||
#else
|
||||
static std::string callFail(KSignaling, int32, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
static std::string channelFail(KSignaling, int32, Presentation fmt = HUMAN);
|
||||
static std::string internalFail(KInternalFail, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string linkErrorCounter(KLinkErrorCounter, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string linkStatus(KSignaling, int32, Presentation fmt = HUMAN);
|
||||
static std::string channelStatus(KSignaling, int32, Presentation fmt = HUMAN);
|
||||
static std::string callStatus(KCallStatus, Presentation fmt = HUMAN);
|
||||
static std::string status(KLibraryStatus, Presentation fmt = HUMAN);
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
static std::string signGroupB(KSignGroupB, R2CountryType contry = R2_COUNTRY_BRA,
|
||||
Presentation fmt = HUMAN);
|
||||
#else
|
||||
static std::string signGroupB(KSignGroupB, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
static std::string signGroupII(KSignGroupII, R2CountryType contry = R2_COUNTRY_BRA,
|
||||
Presentation fmt = HUMAN);
|
||||
#else
|
||||
static std::string signGroupII(KSignGroupII, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
static std::string h100configIndex(KH100ConfigIndex, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string eventName(int32);
|
||||
static std::string commandName(int32);
|
||||
|
||||
#if K3L_AT_LEAST(1,5,0)
|
||||
static std::string sipFailures(KSIP_Failures, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(1,5,1)
|
||||
static std::string isdnCause(KQ931Cause, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(1,5,2)
|
||||
static std::string isdnDebug(int32, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
static std::string callStartInfo(KCallStartInfo, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string gsmCallCause(KGsmCallCause, Presentation fmt = HUMAN);
|
||||
static std::string gsmMobileCause(KGsmMobileCause, Presentation fmt = HUMAN);
|
||||
static std::string gsmSmsCause(KGsmSmsCause, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string q931ProgressIndication(KQ931ProgressIndication,
|
||||
Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
static std::string faxResult(KFaxResult code, Presentation fmt = HUMAN);
|
||||
static std::string faxFileErrorCause(KFaxFileErrorCause code, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
/* end of static (class) stuff */
|
||||
|
||||
private:
|
||||
static std::string internal_deviceType(KDeviceType);
|
||||
static std::string internal_deviceModel(KDeviceType, int32);
|
||||
|
||||
#if K3L_AT_LEAST(1,5,0)
|
||||
static std::string internal_sipFailures(KSIP_Failures, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
#if K3L_AT_LEAST(1,5,1)
|
||||
static std::string internal_isdnCause(KQ931Cause, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(2,0,0)
|
||||
static std::string internal_signGroupB(KSignGroupB, R2CountryType contry, Presentation fmt = HUMAN);
|
||||
static std::string internal_signGroupII(KSignGroupII, R2CountryType contry, Presentation fmt = HUMAN);
|
||||
#else
|
||||
static std::string internal_signGroupB(KSignGroupB, Presentation fmt = HUMAN);
|
||||
static std::string internal_signGroupII(KSignGroupII, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
#if K3L_AT_LEAST(1,6,0)
|
||||
static std::string internal_gsmCallCause(KGsmCallCause, Presentation fmt = HUMAN);
|
||||
static std::string internal_gsmMobileCause(KGsmMobileCause, Presentation fmt = HUMAN);
|
||||
static std::string internal_gsmSmsCause(KGsmSmsCause, Presentation fmt = HUMAN);
|
||||
|
||||
static std::string internal_q931ProgressIndication(KQ931ProgressIndication, Presentation fmt = HUMAN);
|
||||
#endif
|
||||
|
||||
private:
|
||||
enum Type
|
||||
{
|
||||
DEVICE,
|
||||
CHANNEL,
|
||||
PLAYER,
|
||||
MIXER,
|
||||
LINK,
|
||||
NONE
|
||||
};
|
||||
|
||||
struct Target
|
||||
{
|
||||
Target(Type _type)
|
||||
: type(_type), device(-1), object(-1)
|
||||
{};
|
||||
|
||||
Target(Type _type, int32 _device)
|
||||
: type(_type), device(_device), object(-1)
|
||||
{};
|
||||
|
||||
Target(Type _type, int32 _device, int32 _object)
|
||||
: type(_type), device(_device), object(_object)
|
||||
{};
|
||||
|
||||
Type type;
|
||||
int32 device;
|
||||
int32 object;
|
||||
};
|
||||
|
||||
static void generate(std::string &, std::string &, Target, std::string &);
|
||||
|
||||
static std::string show(std::string &, std::string, Target, std::string &);
|
||||
static std::string show(std::string &, std::string, Target);
|
||||
|
||||
template < typename ReturnType >
|
||||
static ReturnType presentation(Presentation fmt, ReturnType str_exact, ReturnType str_human)
|
||||
{
|
||||
switch (fmt)
|
||||
{
|
||||
case HUMAN: return str_human;
|
||||
case EXACT: return str_exact;
|
||||
};
|
||||
|
||||
return str_exact;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* _VERBOSE_HPP_ */
|
||||
1093
src/mod/endpoints/mod_khomp/docs/Manual.html
Normal file
1093
src/mod/endpoints/mod_khomp/docs/Manual.html
Normal file
File diff suppressed because it is too large
Load Diff
BIN
src/mod/endpoints/mod_khomp/docs/Manual.pdf
Normal file
BIN
src/mod/endpoints/mod_khomp/docs/Manual.pdf
Normal file
Binary file not shown.
39
src/mod/endpoints/mod_khomp/docs/README.html
Normal file
39
src/mod/endpoints/mod_khomp/docs/README.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Mod Khomp: README</title></head><body><a name="Guia_R.C3.A1pido" id="Guia_R.C3.A1pido"></a><h1> <span class="mw-headline"> Guia R<>pido </span></h1>
|
||||
<p>Depois de instalar a placa no sistema, e executar o programa de instala<6C><61>o pela primeira vez, os <i>drivers</i> e servi<76>os da Khomp ser<65>o adicionados na inicializa<7A><61>o do sistema, e automaticamente carregados.
|
||||
</p><p>Se o sistema possuir pelo menos uma placa de tronco E1 e/ou uma placa FXO, uma tela de configura<72><61>o b<>sica dever<65> ser apresentada ao final do processo, questionando par<61>metros de sinaliza<7A><61>o - no caso de existirem placas E1 - e/ou realizando o ajuste de cad<61>ncias - no caso de existirem placas FXO.
|
||||
</p><p>Se todas estas etapas foram executadas com sucesso, prossiga para o item <b>Utilizando o <i>Endpoint</i> da Khomp</b>. Caso algum problema ocorra, consulte o <a href=".htmlMod_Khomp/Manual" title="Mod Khomp/Manual">Manual do Endpoint</a>, seguindo para a se<73><65>o de <a href=".htmlMod_Khomp/Manual#Solu.C3.A7.C3.A3o_de_problemas" title="Mod Khomp/Manual">Solu<EFBFBD><EFBFBD>o de problemas</a>.
|
||||
</p>
|
||||
<a name="Utilizando_o_Endpoint_da_Khomp" id="Utilizando_o_Endpoint_da_Khomp"></a><h1> <span class="mw-headline"> Utilizando o <i>Endpoint</i> da Khomp </span></h1>
|
||||
<p>Ap<EFBFBD>s a instala<6C><61>o e inicializa<7A><61>o dos servi<76>os necess<73>rios pela Khomp, o FreeSWITCH j<> pode ser carregado ou inicializado.
|
||||
</p>
|
||||
<ul><li> <b>AVISO</b>: <20> imprescind<6E>vel que o <b>FreeSWITCH</b> n<>o seja executado utilizando a op<6F><70>o que desabilita o escalonamento em tempo real (-nort), <b>especialmente</b> se este estiver sendo executado lado-a-lado com servidores <i>web</i> ou servidores de banco de dados. N<>o realizar este procedimento pode resultar em perda de qualidade do <20>udio, gerando um s<>rio comprometimento no andamento das liga<67><61>es do sistema.
|
||||
</li></ul>
|
||||
<p>Ap<EFBFBD>s a inicializa<7A><61>o do FreeSWITCH, pode-se verificar se o m<>dulo da Khomp foi carregado atrav<61>s do seguinte comando:<br />
|
||||
</p>
|
||||
<pre> freeswitch@hostname> module_exists mod_khomp
|
||||
</pre>
|
||||
<p>A pr<70>xima linha dever<65> responder algo tipo:
|
||||
</p>
|
||||
<pre> true
|
||||
</pre>
|
||||
<p>Para verificar se o FreeSWITCH reconheceu todas as placas, digite:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> khomp summary
|
||||
</pre>
|
||||
<p>A sa<73>da desse comando mostrar<61> detalhes de todas placas reconhecidas.
|
||||
</p><p>No caso de uma placa de tronco E1, pode-se verificar o estado dos links com o seguinte comando:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> khomp show links
|
||||
</pre>
|
||||
<p>O estado dos canais individuais, por sua vez, pode ser aferido com o comando:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> khomp show channels
|
||||
</pre>
|
||||
<p>Para mais detalhes sobre os comandos do Endpoint da khomp digite no console do FreeSWITCH:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> help khomp
|
||||
</pre>
|
||||
<ul><li> <b>IMPORTANTE</b>: Para fazer completo uso da sua placa Khomp, ser<65> preciso configurar suas regras de discagem, seja atrav<61>s do arquivo <i>default.xml</i> ou de um aplicativo externo, definindo regras espec<65>ficas para realizar ao receber liga<67><61>es.<br /><br />No <a href=".htmlMod_Khomp/Manual" title="Mod Khomp/Manual">Manual do Endpoint</a>, podem ser encontradas informa<6D><61>es sobre o formato dos contextos de entrada (respons<6E>veis por receber as liga<67><61>es) e sobre as op<6F><70>es dispon<6F>veis no application Bridge (respons<6E>vel por realizar liga<67><61>es) que podem ser utilizadas com o Endpoint da Khomp, al<61>m de outras funcionalidades especiais providas pelo mesmo.
|
||||
</li></ul>
|
||||
<p><br />
|
||||
</p></body></html>
|
||||
BIN
src/mod/endpoints/mod_khomp/docs/README.pdf
Normal file
BIN
src/mod/endpoints/mod_khomp/docs/README.pdf
Normal file
Binary file not shown.
40
src/mod/endpoints/mod_khomp/docs/README_en.html
Normal file
40
src/mod/endpoints/mod_khomp/docs/README_en.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Mod Khomp: README: en</title></head><body><a name="Quick_guide" id="Quick_guide"></a><h1> <span class="mw-headline"> Quick guide </span></h1>
|
||||
<p>After installing the card in the system and run the installation program for the first time, the <i>drivers</i> and Khomp services will be added at system startup, and automatically loaded.
|
||||
</p><p>If your system has at least one E1 trunk card and / or an FXO card, a basic setup screen will be presented at the end of the process, questioning signaling parameters - if there are signs E1 - and/or performing the adjustment cadences - where that FXO cards.
|
||||
</p><p>If all these steps were performed successfully, proceed to the item <b>Using Khomp Endpoint</b>. If a problem occurs, see the <a href=".htmlMod_Khomp/User_Guide" title="Mod Khomp/User Guide">User Guide</a>, in the section <a href=".htmlMod_Khomp/User_Guide#Troubleshooting" title="Mod Khomp/User Guide">Troubleshooting</a>.
|
||||
</p>
|
||||
<a name="Using_the_Khomp_Endpoint" id="Using_the_Khomp_Endpoint"></a><h1> <span class="mw-headline"> Using the Khomp Endpoint </span></h1>
|
||||
<p>After installation and startup services required by Khomp, FreeSWITCH can now be loaded or initialized.
|
||||
</p>
|
||||
<ul><li><b>WARNING</b>: It is imperative that the<b>FreeSWITCH</b> is not executed using the option that disables the escalation in real time (-nort), <b>especially</b> if this is running side-by server-side with web servers or database. Failure to do so may result in loss of audio quality, causing a serious commitment on the progress of system connections.
|
||||
</li></ul>
|
||||
<p>After initialization of FreeSWITCH, you can verify that the module was loaded Khomp through the following command: <br />
|
||||
</p><p><br />
|
||||
</p>
|
||||
<pre> freeswitch@hostname> module_exists mod_khomp
|
||||
</pre>
|
||||
<p>The next line should answer something like:
|
||||
</p>
|
||||
<pre> true
|
||||
</pre>
|
||||
<p>To verify that the FreeSwitch recognize all the cards, type:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> khomp summary
|
||||
</pre>
|
||||
<p>The output of this command will show details of all cards accepted.
|
||||
</p><p>In the case of an E1 card, you can check the status of links with the following command:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> khomp show links
|
||||
</pre>
|
||||
<p>The state of individual channels, in turn, can be measured with the command:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> khomp show channels
|
||||
</pre>
|
||||
<p>For more details on the commands of the Endpoint khomp type in the console of FreeSWITCH:
|
||||
</p>
|
||||
<pre> freeswitch@hostname> help khomp
|
||||
</pre>
|
||||
<ul><li><b>IMPORTANT</b>: To make full use of your card Khomp, you must configure your dialing rules, either through the extension.conf or from an external application by setting specific rules for conduct by receiving connections. <br /> In the <a href=".htmlMod_Khomp/User_Guide" title="Mod Khomp/User Guide">User Guide</a>, information can be found on the format of input contexts (responsible for receiving calls) and on the options available on application Dial (responsible for make calls) that can be used with the Endpoint Khomp, and other special features provided by it.
|
||||
</li></ul>
|
||||
<p><br />
|
||||
</p></body></html>
|
||||
BIN
src/mod/endpoints/mod_khomp/docs/README_en.pdf
Normal file
BIN
src/mod/endpoints/mod_khomp/docs/README_en.pdf
Normal file
Binary file not shown.
1099
src/mod/endpoints/mod_khomp/docs/User_Guide.html
Normal file
1099
src/mod/endpoints/mod_khomp/docs/User_Guide.html
Normal file
File diff suppressed because it is too large
Load Diff
BIN
src/mod/endpoints/mod_khomp/docs/User_Guide.pdf
Normal file
BIN
src/mod/endpoints/mod_khomp/docs/User_Guide.pdf
Normal file
Binary file not shown.
962
src/mod/endpoints/mod_khomp/include/applications.h
Normal file
962
src/mod/endpoints/mod_khomp/include/applications.h
Normal file
@@ -0,0 +1,962 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _APPLICATIONS_H_
|
||||
#define _APPLICATIONS_H_
|
||||
|
||||
#include "lock.h"
|
||||
#include "khomp_pvt.h"
|
||||
|
||||
struct Application
|
||||
{
|
||||
Application(Board::KhompPvt * pvt) : _pvt(pvt) {}
|
||||
~Application() {}
|
||||
|
||||
Statistics * statistics() { return _app_statistics; }
|
||||
|
||||
template <typename T>
|
||||
T* statistics() { return static_cast<T*>(_app_statistics); }
|
||||
|
||||
Board::KhompPvt *_pvt;
|
||||
Statistics *_app_statistics;
|
||||
};
|
||||
|
||||
/*************************** FAX **********************************************/
|
||||
struct Fax
|
||||
{
|
||||
Fax(Board::KhompPvt * pvt) : _pvt(pvt) {}
|
||||
|
||||
/*
|
||||
bool clear(Board::KhompPvt * pvt)
|
||||
{
|
||||
_pvt = pvt;
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
bool adjustForFax();
|
||||
|
||||
bool sendFax(switch_core_session_t * session, const char *data);
|
||||
bool receiveFax(switch_core_session_t * session, const char *data);
|
||||
|
||||
bool onFaxChannelRelease(K3L_EVENT *e);
|
||||
|
||||
bool startFaxTX(const char * orig_addr = NULL);
|
||||
bool stopFaxTX();
|
||||
bool startFaxRX(const char * filename, const char * orig_addr = NULL);
|
||||
bool stopFaxRX();
|
||||
bool addFaxFile(const char * filename, bool last = true);
|
||||
|
||||
|
||||
Board::KhompPvt *_pvt;
|
||||
|
||||
/* used by app FAX */
|
||||
SavedCondition _fax_cond;
|
||||
KFaxResult _fax_result;
|
||||
|
||||
};
|
||||
|
||||
/*************************** TRANSFER *****************************************/
|
||||
template <typename T, bool flash = true>
|
||||
struct Transfer
|
||||
{
|
||||
Transfer(Board::KhompPvt * pvt) : _pvt(pvt), _is_ok(false) {}
|
||||
|
||||
bool clear()
|
||||
{
|
||||
if(!_is_ok)
|
||||
{
|
||||
_call = dynamic_cast<T *>(_pvt->call());
|
||||
|
||||
_is_ok = true;
|
||||
|
||||
if(!_call)
|
||||
{
|
||||
DBG(FUNC, D("Error in cast"));
|
||||
_is_ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
_call->_flags.clear(Kflags::XFER_DIALING);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool userTransfer(switch_core_session_t * session, const char *data)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c"));
|
||||
|
||||
std::string dest("");
|
||||
std::string opts("");
|
||||
|
||||
try
|
||||
{
|
||||
Strings::vector_type params;
|
||||
|
||||
Strings::tokenize((const char *)data, params, "|,", 2);
|
||||
|
||||
dest = params[0];
|
||||
|
||||
if (params.size() > 1)
|
||||
{
|
||||
// other options go here...
|
||||
}
|
||||
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
int timeout = 5;
|
||||
|
||||
if(!_pvt->call()->_flags.check(Kflags::REALLY_CONNECTED) && !_pvt->loopWhileFlagTimed(Kflags::REALLY_CONNECTED, timeout, false))
|
||||
return false;
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "flashing channel!"));
|
||||
|
||||
_pvt->command(KHOMP_LOG, CM_FLASH);
|
||||
|
||||
lock.unlock();
|
||||
|
||||
timeout = 15; // 15 * 200000 = 3s
|
||||
|
||||
do
|
||||
{
|
||||
usleep(200000);
|
||||
timeout--;
|
||||
|
||||
ScopedPvtLock lock2(_pvt);
|
||||
|
||||
if(!_pvt->call()->_flags.check(Kflags::IS_INCOMING) && !_pvt->call()->_flags.check(Kflags::IS_OUTGOING))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "unable to do a user transfer, channel disconnected"));
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
while(timeout);
|
||||
|
||||
ScopedPvtLock lock3(_pvt);
|
||||
|
||||
_pvt->command(KHOMP_LOG, CM_DIAL_DTMF, dest.c_str());
|
||||
|
||||
_pvt->call()->_flags.set(Kflags::WAIT_SEND_DTMF);
|
||||
|
||||
lock3.unlock();
|
||||
|
||||
timeout = 300; // 300 * 200000 = 60s
|
||||
|
||||
do
|
||||
{
|
||||
usleep(200000);
|
||||
timeout--;
|
||||
|
||||
ScopedPvtLock lock4(_pvt);
|
||||
|
||||
if(!_pvt->call()->_flags.check(Kflags::WAIT_SEND_DTMF))
|
||||
break;
|
||||
}
|
||||
while(timeout);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(),"r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
/* User transfer functions */
|
||||
bool doUserXferUnlocked()
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c (flashing channel!)"));
|
||||
|
||||
bool ret = false;
|
||||
|
||||
ret = _pvt->command(KHOMP_LOG, CM_FLASH);
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (%s)") % (ret ? "true" : "false"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool checkUserXferUnlocked(std::string digit)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c (CM_FLASH)"));
|
||||
|
||||
|
||||
if (_call->_user_xfer_digits.empty())
|
||||
{
|
||||
_call->_digits_buffer += digit;
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (disabled)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
_call->_user_xfer_buffer += digit;
|
||||
|
||||
/* temporary buffer */
|
||||
std::string tmp = _call->_user_xfer_buffer;
|
||||
|
||||
unsigned int amount = tmp.size();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if (amount == _call->_user_xfer_digits.size())
|
||||
{
|
||||
if (tmp == _call->_user_xfer_digits)
|
||||
{
|
||||
bool ret = doUserXferUnlocked();
|
||||
|
||||
_call->_user_xfer_buffer.clear();
|
||||
_call->_digits_buffer.clear();
|
||||
|
||||
Board::board(_pvt->target().device)->_timers.del(_idx_xfer_dial);
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (ret=%s, done xfer)") % (ret ? "true" : "false"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
_call->_digits_buffer += tmp[0];
|
||||
_call->_user_xfer_buffer.erase(0, 1);
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (false, no xfer)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tmp == _call->_user_xfer_digits.substr(0,amount))
|
||||
{
|
||||
if (!(_call->_flags.check(Kflags::XFER_DIALING)))
|
||||
{
|
||||
_call->_flags.set(Kflags::XFER_DIALING);
|
||||
_idx_xfer_dial = Board::board(_pvt->target().device)->_timers.add(Opt::_transferdigittimeout, &userXferTimer, _pvt, TM_VAL_CALL);
|
||||
}
|
||||
else
|
||||
{
|
||||
Board::board(_pvt->target().device)->_timers.restart(_idx_xfer_dial);
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (true, buffering)"));
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
catch (K3LAPI::invalid_device & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "Unable to get device: %d!") % err.device);
|
||||
}
|
||||
|
||||
_call->_digits_buffer += tmp[0];
|
||||
_call->_user_xfer_buffer.erase(0, 1);
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (false, buffering)"));
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
static void userXferTimer(Board::KhompPvt * pvt)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "c"));
|
||||
|
||||
T * call = static_cast<T *>(pvt->call());
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(pvt);
|
||||
|
||||
if (!call->_user_xfer_buffer.empty())
|
||||
{
|
||||
pvt->command(KHOMP_LOG, CM_DIAL_DTMF, call->_user_xfer_buffer.c_str());
|
||||
|
||||
/* clear the buffer that has been send */
|
||||
call->_user_xfer_buffer.clear();
|
||||
}
|
||||
|
||||
call->_flags.clear(Kflags::XFER_DIALING);
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(pvt->target(),"r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "r"));
|
||||
}
|
||||
|
||||
bool _is_ok;
|
||||
T * _call;
|
||||
Board::KhompPvt * _pvt;
|
||||
Board::ChanTimer::Index _idx_xfer_dial;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Transfer<T, false>
|
||||
{
|
||||
Transfer(Board::KhompPvt * pvt) : _pvt(pvt), _is_ok(false) {}
|
||||
|
||||
bool clear()
|
||||
{
|
||||
if(!_is_ok)
|
||||
{
|
||||
_call = dynamic_cast<T *>(_pvt->call());
|
||||
|
||||
_is_ok = true;
|
||||
|
||||
if(!_call)
|
||||
{
|
||||
DBG(FUNC, D("Error in cast"));
|
||||
_is_ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
_call->_flags.clear(Kflags::XFER_DIALING);
|
||||
_call->_flags.clear(Kflags::XFER_QSIG_DIALING);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool userTransfer(switch_core_session_t * session, const char *data)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c"));
|
||||
|
||||
std::string dest("");
|
||||
|
||||
bool opt_nowait = false;
|
||||
|
||||
try
|
||||
{
|
||||
Strings::vector_type params;
|
||||
|
||||
Strings::tokenize((const char *)data, params, "|,", 2);
|
||||
|
||||
dest = params[0];
|
||||
|
||||
if (params.size() > 1)
|
||||
{
|
||||
opt_nowait = (params[1].find('n') != std::string::npos);
|
||||
|
||||
// other options go here...
|
||||
}
|
||||
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
int timeout = 5;
|
||||
|
||||
if(!_pvt->call()->_flags.check(Kflags::REALLY_CONNECTED) && !_pvt->loopWhileFlagTimed(Kflags::REALLY_CONNECTED, timeout, false))
|
||||
return false;
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "ss_transfer on channel!"));
|
||||
|
||||
_pvt->command(KHOMP_LOG, CM_SS_TRANSFER,
|
||||
STG(FMT("transferred_to=\"%s\" await_connect=\"%d\"")
|
||||
% dest % (opt_nowait ? 0 : 1)).c_str());
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(),"r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
/* User transfer functions */
|
||||
bool doUserXferUnlocked(void)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c"));
|
||||
|
||||
bool ret = false;
|
||||
|
||||
if (_call->_flags.check(Kflags::XFER_QSIG_DIALING))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "ss_transfer on channel!"));
|
||||
|
||||
_call->_flags.clear(Kflags::XFER_DIALING);
|
||||
_call->_flags.clear(Kflags::XFER_QSIG_DIALING);
|
||||
|
||||
ret = _pvt->command(KHOMP_LOG, CM_SS_TRANSFER,
|
||||
STG(FMT("transferred_to=\"%s\" await_connect=\"1\"") % _call->_qsig_number).c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "starting to store digits for ss_transfer..."));
|
||||
_call->_flags.set(Kflags::XFER_QSIG_DIALING);
|
||||
|
||||
_xfer_thread = threadCreate(Transfer<T, false>::userXferPlayback,(void*) _pvt);
|
||||
_xfer_thread->start();
|
||||
|
||||
ret = true;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (%s)") % (ret ? "true" : "false"));
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
bool checkUserXferUnlocked(std::string digit)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c (CM_SS_TRANSFER)"));
|
||||
|
||||
|
||||
if (_call->_user_xfer_digits.empty())
|
||||
{
|
||||
_call->_digits_buffer += digit;
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (disabled)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
_call->_user_xfer_buffer += digit;
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c digits=[%s] buffer=[%s]") % _call->_user_xfer_digits % _call->_user_xfer_buffer );
|
||||
|
||||
/* temporary buffer */
|
||||
std::string tmp = _call->_user_xfer_buffer;
|
||||
|
||||
unsigned int amount = tmp.size();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if (amount == _call->_user_xfer_digits.size())
|
||||
{
|
||||
if (tmp == _call->_user_xfer_digits)
|
||||
{
|
||||
bool ret = doUserXferUnlocked();
|
||||
|
||||
_call->_user_xfer_buffer.clear();
|
||||
_call->_qsig_number.clear();
|
||||
_call->_digits_buffer.clear();
|
||||
|
||||
if(!_call->_flags.check(Kflags::XFER_QSIG_DIALING))
|
||||
{
|
||||
Board::board(_pvt->target().device)->_timers.del(_idx_xfer_dial);
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (ret=%s, done xfer)") % (ret ? "true" : "false"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Board::board(_pvt->target().device)->_timers.restart(_idx_xfer_dial);
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (waiting digits for transfer)"));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (_call->_flags.check(Kflags::XFER_QSIG_DIALING))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "putting digits ('%s') on transfer-to number!") % tmp);
|
||||
|
||||
_call->_qsig_number += tmp[0];
|
||||
_call->_user_xfer_buffer.erase(0, 1);
|
||||
Board::board(_pvt->target().device)->_timers.restart(_idx_xfer_dial);
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (true, qsig transfer)"));
|
||||
return true;
|
||||
}
|
||||
|
||||
_call->_digits_buffer += tmp[0];
|
||||
_call->_user_xfer_buffer.erase(0, 1);
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (false, no qsig)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tmp == _call->_user_xfer_digits.substr(0,amount))
|
||||
{
|
||||
if (!(_call->_flags.check(Kflags::XFER_DIALING) || _call->_flags.check(Kflags::XFER_QSIG_DIALING)))
|
||||
{
|
||||
_call->_flags.set(Kflags::XFER_DIALING);
|
||||
_idx_xfer_dial = Board::board(_pvt->target().device)->_timers.add(Opt::_transferdigittimeout, &userXferTimer, _pvt, TM_VAL_CALL);
|
||||
}
|
||||
else
|
||||
{
|
||||
Board::board(_pvt->target().device)->_timers.restart(_idx_xfer_dial);
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (true, buffering)"));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_call->_flags.check(Kflags::XFER_QSIG_DIALING))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "putting digits ('%s') on transfer-to number!") % tmp);
|
||||
|
||||
_call->_qsig_number += tmp[0];
|
||||
_call->_user_xfer_buffer.erase(0, 1);
|
||||
|
||||
Board::board(_pvt->target().device)->_timers.restart(_idx_xfer_dial);
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (true, qsig buffering)"));
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
catch (K3LAPI::invalid_device & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "Unable to get device: %d!") % err.device);
|
||||
}
|
||||
|
||||
_call->_digits_buffer += tmp[0];
|
||||
_call->_user_xfer_buffer.erase(0, 1);
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r (false, buffering)"));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void userXferTimer(Board::KhompPvt * pvt)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "c"));
|
||||
|
||||
T * call = static_cast<T *>(pvt->call());
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(pvt);
|
||||
|
||||
if (!call->_user_xfer_buffer.empty())
|
||||
{
|
||||
pvt->command(KHOMP_LOG, CM_DIAL_DTMF, call->_user_xfer_buffer.c_str());
|
||||
|
||||
/* clear the buffer that has been send */
|
||||
call->_user_xfer_buffer.clear();
|
||||
}
|
||||
|
||||
if (!call->_qsig_number.empty())
|
||||
{
|
||||
pvt->command(KHOMP_LOG, CM_SS_TRANSFER,
|
||||
STG(FMT("transferred_to=\"%s\" await_connect=\"1\"") % call->_qsig_number).c_str());
|
||||
|
||||
/* clear the buffer that has been send */
|
||||
call->_qsig_number.clear();
|
||||
}
|
||||
|
||||
call->_flags.clear(Kflags::XFER_DIALING);
|
||||
call->_flags.clear(Kflags::XFER_QSIG_DIALING);
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(pvt->target(),"r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "r"));
|
||||
}
|
||||
|
||||
static switch_status_t dtmfCallback(switch_core_session_t *session, void *input, switch_input_type_t itype, void *buf, unsigned int buflen)
|
||||
{
|
||||
char sbuf[3];
|
||||
|
||||
if(!session)
|
||||
{
|
||||
DBG(FUNC,D("session is NULL"))
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
switch_channel_t * chan = switch_core_session_get_channel(session);
|
||||
|
||||
if(!chan)
|
||||
{
|
||||
DBG(FUNC,D("channel is NULL"))
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
switch_core_session_t *peer_session = switch_core_session_locate(switch_channel_get_variable(chan,SWITCH_SIGNAL_BOND_VARIABLE));
|
||||
|
||||
if(!peer_session)
|
||||
{
|
||||
DBG(FUNC,D("session is NULL"))
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
switch (itype)
|
||||
{
|
||||
case SWITCH_INPUT_TYPE_DTMF:
|
||||
{
|
||||
switch_dtmf_t *dtmf = (switch_dtmf_t *) input;
|
||||
|
||||
Board::KhompPvt * tech_pvt = static_cast< Board::KhompPvt* >(switch_core_session_get_private(peer_session));
|
||||
if(!tech_pvt)
|
||||
{
|
||||
DBG(FUNC,D("Init: pvt is NULL"))
|
||||
switch_core_session_rwunlock(peer_session);
|
||||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
char s[] = { dtmf->digit, '\0' };
|
||||
tech_pvt->sendDtmf(s);
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
switch_core_session_rwunlock(peer_session);
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static int userXferPlayback(void * pvt_ptr)
|
||||
{
|
||||
/* get pointer... */
|
||||
Board::KhompPvt * pvt = static_cast < Board::KhompPvt * > (pvt_ptr);
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(pvt);
|
||||
|
||||
/* get the owner */
|
||||
switch_channel_t * chan = pvt->getFSChannel();
|
||||
|
||||
/* get other side of the bridge */
|
||||
switch_core_session_t * peer_session = NULL;
|
||||
switch_core_session_get_partner(pvt->session(),&peer_session);
|
||||
|
||||
if(!peer_session)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "r (session is null)"));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch_channel_t * peer = switch_core_session_get_channel(peer_session);
|
||||
|
||||
/* put the channel in hold */
|
||||
//switch_core_session_t *session = switch_core_session_locate(switch_channel_get_variable(chan,SWITCH_SIGNAL_BOND_VARIABLE));
|
||||
//switch_channel_t *chan_core = switch_core_session_get_channel(session);
|
||||
|
||||
const char *stream;
|
||||
|
||||
if (!(stream = switch_channel_get_variable(chan, SWITCH_HOLD_MUSIC_VARIABLE)))
|
||||
{
|
||||
stream = "silence";
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "stream=%s") % stream);
|
||||
|
||||
if (stream && strcasecmp(stream, "silence"))
|
||||
{
|
||||
/* Freeswitch not get/put frames */
|
||||
//switch_channel_set_flag(channel, CF_HOLD);
|
||||
switch_ivr_broadcast(switch_core_session_get_uuid(pvt->session()),stream, SMF_ECHO_ALEG | SMF_LOOP | SMF_PRIORITY);
|
||||
}
|
||||
|
||||
switch_core_session_rwunlock(peer_session);
|
||||
lock.unlock();
|
||||
|
||||
/* kickstart my heart */
|
||||
switch_input_args_t args = {0};
|
||||
args.input_callback = dtmfCallback;
|
||||
|
||||
/* wait while xfering... */
|
||||
while (true)
|
||||
{
|
||||
switch_ivr_collect_digits_callback(peer_session,&args,1000,0);
|
||||
ScopedPvtLock lock2(pvt);
|
||||
|
||||
if (!pvt->call()->_flags.check(Kflags::XFER_QSIG_DIALING))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
lock2.unlock();
|
||||
}
|
||||
|
||||
//switch_channel_clear_flag(channel, CF_HOLD);
|
||||
|
||||
switch_channel_stop_broadcast(chan);
|
||||
switch_channel_wait_for_flag(chan, CF_BROADCAST, SWITCH_FALSE, 5000, NULL);
|
||||
switch_core_session_rwunlock(pvt->session());
|
||||
switch_core_session_rwunlock(peer_session);
|
||||
|
||||
//switch_ivr_unhold_uuid(switch_channel_get_variable(chan,SWITCH_SIGNAL_BOND_VARIABLE));
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(pvt->target(),"r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return NULL;
|
||||
}
|
||||
catch (Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(pvt->target(), "r (%s)") % err._msg.c_str() );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "r"));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool _is_ok;
|
||||
T * _call;
|
||||
Board::KhompPvt * _pvt;
|
||||
Thread * _xfer_thread;
|
||||
Board::ChanTimer::Index _idx_xfer_dial;
|
||||
};
|
||||
|
||||
/*************************** SMS **********************************************/
|
||||
struct SMS : public Application
|
||||
{
|
||||
typedef std::list< switch_core_session_t *> OwnersList;
|
||||
|
||||
struct SMSStatistics : public Statistics
|
||||
{
|
||||
SMSStatistics():
|
||||
_sms_number_incoming(0),
|
||||
_sms_number_outgoing(0),
|
||||
_sms_number_confirm(0),
|
||||
_sms_number_broadcast(0) {};
|
||||
|
||||
void incrementIncoming()
|
||||
{
|
||||
_sms_number_incoming++;
|
||||
}
|
||||
|
||||
void incrementOutgoing()
|
||||
{
|
||||
_sms_number_outgoing++;
|
||||
}
|
||||
|
||||
void incrementConfirm()
|
||||
{
|
||||
_sms_number_confirm++;
|
||||
}
|
||||
|
||||
void incrementBroadcast()
|
||||
{
|
||||
_sms_number_broadcast++;
|
||||
}
|
||||
|
||||
std::string getDetailed()
|
||||
{
|
||||
std::string tmpBuffer;
|
||||
|
||||
tmpBuffer.append(STG(FMT("Number of incoming SMS: \t%d\n") % _sms_number_incoming));
|
||||
tmpBuffer.append(STG(FMT("Number of outgoing SMS: \t%d\n") % _sms_number_outgoing));
|
||||
tmpBuffer.append(STG(FMT("Number of broadcast SMS: \t%d\n") % _sms_number_broadcast));
|
||||
tmpBuffer.append(STG(FMT("Number of confirm SMS: \t%d\n") % _sms_number_confirm));
|
||||
|
||||
return tmpBuffer;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_sms_number_incoming = 0;
|
||||
_sms_number_outgoing = 0;
|
||||
_sms_number_confirm = 0;
|
||||
_sms_number_broadcast = 0;
|
||||
}
|
||||
|
||||
unsigned int _sms_number_incoming;
|
||||
unsigned int _sms_number_outgoing;
|
||||
unsigned int _sms_number_confirm;
|
||||
unsigned int _sms_number_broadcast;
|
||||
};
|
||||
|
||||
SMS(Board::KhompPvt * pvt) :
|
||||
Application(pvt),
|
||||
_thread(NULL),
|
||||
_shutdown(false),
|
||||
_can_receive(false),
|
||||
_can_send(false),
|
||||
_result(0),
|
||||
_mutex(Globals::module_pool),
|
||||
_cond(Globals::module_pool),
|
||||
_buffer(8)
|
||||
{
|
||||
_cond.reset();
|
||||
_app_statistics = new SMSStatistics();
|
||||
}
|
||||
|
||||
~SMS()
|
||||
{
|
||||
stop();
|
||||
delete _app_statistics;
|
||||
}
|
||||
|
||||
struct ReceiveData
|
||||
{
|
||||
ReceiveData() {};
|
||||
|
||||
ReceiveData(const ReceiveData & o)
|
||||
{
|
||||
_type = o._type;
|
||||
_from = o._from;
|
||||
_date = o._date;
|
||||
_size = o._size;
|
||||
_coding = o._coding;
|
||||
_serial = o._serial;
|
||||
_id = o._id;
|
||||
_page = o._page;
|
||||
_pages = o._pages;
|
||||
_sc_date = o._sc_date;
|
||||
_status = o._status;
|
||||
_body = o._body;
|
||||
};
|
||||
|
||||
void clear(void)
|
||||
{
|
||||
/* reset data stuff */
|
||||
_type.clear();
|
||||
_from.clear();
|
||||
_date.clear();
|
||||
_size.clear();
|
||||
_coding.clear();
|
||||
_serial.clear();
|
||||
_id.clear();
|
||||
_page.clear();
|
||||
_pages.clear();
|
||||
_sc_date.clear();
|
||||
_status.clear();
|
||||
_body.clear();
|
||||
};
|
||||
|
||||
std::string _type;
|
||||
std::string _from;
|
||||
std::string _date;
|
||||
std::string _size;
|
||||
std::string _coding;
|
||||
std::string _serial;
|
||||
std::string _id;
|
||||
std::string _page;
|
||||
std::string _pages;
|
||||
std::string _sc_date;
|
||||
std::string _status;
|
||||
std::string _body;
|
||||
};
|
||||
|
||||
struct SendData
|
||||
{
|
||||
SendData(): _conf(false) {};
|
||||
|
||||
SendData(const SendData & o)
|
||||
{
|
||||
_dest = o._dest;
|
||||
_body = o._body;
|
||||
_conf = o._conf;
|
||||
};
|
||||
|
||||
void clear(void)
|
||||
{
|
||||
/* reset data stuff */
|
||||
_dest.clear();
|
||||
_body.clear();
|
||||
_conf = false;
|
||||
};
|
||||
|
||||
std::string _dest;
|
||||
std::string _body;
|
||||
bool _conf;
|
||||
};
|
||||
|
||||
struct Request
|
||||
{
|
||||
/* "empty" constructor */
|
||||
Request(): _finished(NULL), _cause(NULL) {};
|
||||
|
||||
/* "real" constructor */
|
||||
Request(SendData & send_sms, volatile bool * finished, volatile KGsmCallCause * cause)
|
||||
: _send_sms(send_sms), _finished(finished), _cause(cause)
|
||||
{};
|
||||
|
||||
SendData _send_sms;
|
||||
|
||||
volatile bool * _finished;
|
||||
volatile KGsmCallCause * _cause;
|
||||
};
|
||||
|
||||
bool start()
|
||||
{
|
||||
_pvt->call()->_flags.clear(Kflags::SMS_DOING_UPLOAD);
|
||||
|
||||
_thread = threadCreate(&smsThread, (void*) this);
|
||||
_thread->start();
|
||||
}
|
||||
|
||||
bool stop()
|
||||
{
|
||||
if(!_thread)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_shutdown = true;
|
||||
_cond.signal();
|
||||
_thread->join();
|
||||
delete _thread;
|
||||
_thread = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool justAlloc(unsigned int count = 0);
|
||||
bool justStart();
|
||||
|
||||
bool sendSMS(switch_core_session_t * session, const char *data);
|
||||
|
||||
|
||||
bool onNewSMS(K3L_EVENT *e);
|
||||
bool onSMSInfo(K3L_EVENT *e);
|
||||
bool onSMSData(K3L_EVENT *e);
|
||||
bool onSMSSendResult(K3L_EVENT *e);
|
||||
|
||||
Thread *_thread;
|
||||
bool _shutdown;
|
||||
bool _can_receive;
|
||||
bool _can_send;
|
||||
ReceiveData _got_sms;
|
||||
SendData _send_sms;
|
||||
int _result;
|
||||
SavedCondition _cond;
|
||||
Globals::Mutex _mutex;
|
||||
Ringbuffer < SMS::Request > _buffer;
|
||||
OwnersList _owners;
|
||||
|
||||
static int smsThread(void * sms_ptr);
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
#endif /* _APPLICATIONS_H_ */
|
||||
|
||||
1143
src/mod/endpoints/mod_khomp/include/cli.h
Normal file
1143
src/mod/endpoints/mod_khomp/include/cli.h
Normal file
File diff suppressed because it is too large
Load Diff
211
src/mod/endpoints/mod_khomp/include/defs.h
Normal file
211
src/mod/endpoints/mod_khomp/include/defs.h
Normal file
@@ -0,0 +1,211 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _DEFS_H_
|
||||
#define _DEFS_H_
|
||||
|
||||
/* formatation macros */
|
||||
#include "switch_version.h"
|
||||
#include "revision.h"
|
||||
|
||||
#define KHOMP_LOG __FILE__, __SWITCH_FUNC__, __LINE__
|
||||
#define KHOMP_EVENT_MAINT "khomp::maintenance"
|
||||
|
||||
#define OBJ_FMT(dev,obj,msg) \
|
||||
FMT("%s (d=%02d,c=%03d) " msg) % __SWITCH_FUNC__ % dev % obj
|
||||
|
||||
#define OBJ_MSG(dev,obj,msg) \
|
||||
"%s (d=%02d,c=%03d) " msg, __SWITCH_FUNC__, dev, obj
|
||||
|
||||
#define PVT_FMT(tgt,msg) \
|
||||
FMT("%s (d=%02hu,c=%03hu) " msg) % __SWITCH_FUNC__ % tgt.device % tgt.object
|
||||
|
||||
#define PVT_MSG(tgt,msg) \
|
||||
"%s (d=%02hu,c=%03hu) " msg, __SWITCH_FUNC__, tgt.device, tgt.object
|
||||
|
||||
#define D(x) ( FMT( "%s: " x ) % __func__ )
|
||||
|
||||
#define STR(fmt) \
|
||||
STG(fmt).c_str()
|
||||
|
||||
/* version controller macro */
|
||||
#define SWITCH_LESS_THAN(x,y,z) \
|
||||
(FS_VERSION_MICRO != -1) && \
|
||||
(((FS_VERSION_MAJOR == x) && (FS_VERSION_MINOR == y) && (FS_VERSION_MICRO <= z)) || \
|
||||
((FS_VERSION_MAJOR == x) && (FS_VERSION_MINOR < y)) || (FS_VERSION_MAJOR < x))
|
||||
|
||||
/* signalling groups macro */
|
||||
#define CASE_R2_SIG \
|
||||
case ksigR2Digital: \
|
||||
case ksigContinuousEM: \
|
||||
case ksigPulsedEM: \
|
||||
case ksigUserR2Digital: \
|
||||
case ksigOpenCAS: \
|
||||
case ksigOpenR2
|
||||
/*
|
||||
case ksigLineSide: \
|
||||
case ksigCAS_EL7: \
|
||||
case ksigE1LC
|
||||
*/
|
||||
|
||||
#define CASE_RDSI_SIG \
|
||||
case ksigPRI_EndPoint: \
|
||||
case ksigPRI_Network: \
|
||||
case ksigPRI_Passive: \
|
||||
case ksigOpenCCS
|
||||
|
||||
#define CASE_FLASH_GRP \
|
||||
case ksigLineSide: \
|
||||
case ksigCAS_EL7: \
|
||||
case ksigE1LC
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* Buffering size constants */
|
||||
|
||||
#define SILENCE_PACKS 2
|
||||
|
||||
#define KHOMP_READ_PACKET_TIME 16 // board sample (ms)
|
||||
|
||||
#define KHOMP_READ_PACKET_SIZE (KHOMP_READ_PACKET_TIME * 8) // asterisk sample size (bytes)
|
||||
|
||||
#define KHOMP_MIN_READ_PACKET_SIZE (10 * 8) // min size to return on khomp_read
|
||||
#define KHOMP_MAX_READ_PACKET_SIZE (30 * 8) // max size to return on khomp_read
|
||||
|
||||
#define KHOMP_AUDIO_BUFFER_SIZE (KHOMP_READ_PACKET_SIZE * 8) // buffer size (bytes)
|
||||
|
||||
/* debug and log macros */
|
||||
#define DBG(x,y) \
|
||||
{ \
|
||||
if (K::Logger::Logg.classe( C_DBG_##x ).enabled()) \
|
||||
K::Logger::Logg( C_DBG_##x , y ); \
|
||||
}
|
||||
|
||||
#define LOG(x,y) \
|
||||
{ \
|
||||
K::Logger::Logg( C_##x , y ); \
|
||||
}
|
||||
|
||||
#define LOGC(x,y) \
|
||||
{ \
|
||||
if (K::Logger::Logg.classe( C_##x ).enabled()) \
|
||||
LOG( x , y ); \
|
||||
}
|
||||
|
||||
/* useful to debug arguments */
|
||||
#define DEBUG_CLI_CMD() \
|
||||
{ \
|
||||
K::Logger::Logg2(C_CLI,stream,FMT("argc: %d ") % argc); \
|
||||
for(int i = 0;i< argc;i++) \
|
||||
K::Logger::Logg2(C_CLI,stream,FMT("argv[%d]:%s ") % i % argv[i]); \
|
||||
}
|
||||
|
||||
/* macros to cli commands */
|
||||
#define ARG_CMP(a,b) (argv[a] && !strncasecmp(argv[a],b,sizeof(b)))
|
||||
#define EXEC_CLI_CMD(command) Cli::command.execute(argc,argv)
|
||||
|
||||
/* macro to string treats */
|
||||
#define SAFE_sprintf(a, ...) snprintf(a,sizeof(a), __VA_ARGS__)
|
||||
#define SAFE_strcasecmp(a,b) strncasecmp(a, b, sizeof(b))
|
||||
|
||||
/* tags for timers */
|
||||
#define TM_VAL_CALL (unsigned int)0x01
|
||||
#define TM_VAL_CHANNEL (unsigned int)0x02
|
||||
|
||||
/* macro to creating contexts */
|
||||
#define BEGIN_CONTEXT do
|
||||
#define END_CONTEXT while(false);
|
||||
|
||||
/* Define log type */
|
||||
typedef enum
|
||||
{
|
||||
C_CLI, /* cli msgs */
|
||||
|
||||
C_ERROR, /* errors */
|
||||
C_WARNING, /* warnings */
|
||||
C_MESSAGE, /* normal msgs */
|
||||
|
||||
C_EVENT, /* k3l events */
|
||||
C_COMMAND, /* k3l commands */
|
||||
|
||||
C_AUDIO_EV, /* k3l audio events */
|
||||
C_MODEM_EV, /* gsm modem events */
|
||||
C_LINK_STT, /* link status msgs */
|
||||
C_CAS_MSGS, /* cas events msgs */
|
||||
|
||||
C_DBG_FUNC,
|
||||
C_DBG_LOCK,
|
||||
C_DBG_THRD,
|
||||
C_DBG_STRM,
|
||||
|
||||
C_DBG_CONF,
|
||||
}
|
||||
class_type;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
O_CONSOLE,
|
||||
O_GENERIC,
|
||||
O_R2TRACE,
|
||||
}
|
||||
output_type;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SCE_SHOW_WARNING,
|
||||
SCE_SHOW_DEBUG,
|
||||
SCE_SHOW_SAME,
|
||||
SCE_HIDE
|
||||
}
|
||||
send_cmd_error_type;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
T_UNKNOWN = 2,
|
||||
T_TRUE = 1,
|
||||
T_FALSE = 0,
|
||||
}
|
||||
TriState;
|
||||
|
||||
|
||||
#endif
|
||||
160
src/mod/endpoints/mod_khomp/include/frame.h
Normal file
160
src/mod/endpoints/mod_khomp/include/frame.h
Normal file
@@ -0,0 +1,160 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _FRAME_HPP_
|
||||
#define _FRAME_HPP_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <ringbuffer.hpp>
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
struct FrameStorage
|
||||
{
|
||||
static const unsigned int frame_count = 24;//6;
|
||||
static const unsigned int audio_count = 24;//4;
|
||||
|
||||
FrameStorage(switch_codec_t * codec, int packet_size);
|
||||
virtual ~FrameStorage();
|
||||
|
||||
inline switch_frame_t * next_frame(void)
|
||||
{
|
||||
return &(_frames[next_index()]);
|
||||
}
|
||||
|
||||
inline unsigned int next_index()
|
||||
{
|
||||
unsigned int tmp = _index;
|
||||
|
||||
if (++_index >= frame_count)
|
||||
_index = 0;
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
inline switch_frame_t * cng_frame(void)
|
||||
{
|
||||
return &_cng_frame;
|
||||
}
|
||||
|
||||
char * audio_buffer()
|
||||
{
|
||||
return _buffer;
|
||||
};
|
||||
|
||||
private:
|
||||
switch_frame_t _cng_frame;
|
||||
|
||||
switch_frame_t * _frames;
|
||||
char * _buffer;
|
||||
|
||||
unsigned int _index;
|
||||
};
|
||||
|
||||
/* Internal frame array structure. */
|
||||
template < int S >
|
||||
struct FrameManager: protected FrameStorage
|
||||
{
|
||||
typedef char Packet[ S ];
|
||||
|
||||
typedef Ringbuffer < Packet > AudioBuffer;
|
||||
|
||||
FrameManager(switch_codec_t * codec)
|
||||
: FrameStorage(codec, S),
|
||||
_audio(audio_count, (Packet*)audio_buffer())
|
||||
{};
|
||||
|
||||
// ~FrameManager();
|
||||
|
||||
// may throw Ringbuffer::BufferEmpty
|
||||
switch_frame_t * pick(void)
|
||||
{
|
||||
try
|
||||
{
|
||||
/* try to consume from buffer.. */
|
||||
Packet & a = _audio.consumer_start();
|
||||
|
||||
switch_frame * f = next_frame();
|
||||
|
||||
/* adjust pointer */
|
||||
f->data = (char *)(&a);
|
||||
|
||||
/* advance now */
|
||||
_audio.consumer_commit();
|
||||
|
||||
return f;
|
||||
}
|
||||
catch (...) // AudioBuffer::BufferEmpty & e)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// may throw Ringbuffer::BufferFull
|
||||
bool give(const char * buf, unsigned int size)
|
||||
{
|
||||
return _audio.provider_partial(buf, size);
|
||||
}
|
||||
|
||||
switch_frame_t * cng(void)
|
||||
{
|
||||
return cng_frame();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_audio.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
AudioBuffer _audio;
|
||||
|
||||
unsigned int _index;
|
||||
};
|
||||
|
||||
typedef FrameManager < Globals::switch_packet_size > FrameSwitchManager;
|
||||
typedef FrameManager < Globals::boards_packet_size > FrameBoardsManager;
|
||||
|
||||
#endif /* _FRAME_HPP_ */
|
||||
119
src/mod/endpoints/mod_khomp/include/globals.h
Normal file
119
src/mod/endpoints/mod_khomp/include/globals.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _GLOBALS_H_
|
||||
#define _GLOBALS_H_
|
||||
#include "k3lapi.hpp"
|
||||
|
||||
#include <config_options.hpp>
|
||||
#include <k3lutil.hpp>
|
||||
#include <verbose.hpp>
|
||||
#include <regex.hpp>
|
||||
#include <timer.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
#include <simple_lock.hpp>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <switch.h>
|
||||
}
|
||||
|
||||
/* As this is a static-variable-only struct, member variable *
|
||||
* names need not to get "_" in front of the name */
|
||||
|
||||
struct Globals
|
||||
{
|
||||
typedef void * global_timer_data;
|
||||
typedef void (global_timer_func)(global_timer_data);
|
||||
typedef TimerTemplate < global_timer_func, global_timer_data > GlobalTimer;
|
||||
|
||||
typedef SimpleNonBlockLock<25,100> Mutex;
|
||||
|
||||
/* FreeSWITCH variables for packets and buffer */
|
||||
// in ms
|
||||
static const unsigned int switch_packet_duration = 30;
|
||||
static const unsigned int boards_packet_duration = 24;
|
||||
// in bytes
|
||||
static const unsigned int switch_packet_size = switch_packet_duration * 8;
|
||||
static const unsigned int boards_packet_size = boards_packet_duration * 8;
|
||||
static const unsigned int cng_buffer_size = (switch_packet_size > boards_packet_size ? switch_packet_size : boards_packet_size);
|
||||
|
||||
/* Father */
|
||||
static K3LAPI k3lapi;
|
||||
|
||||
/* Son */
|
||||
static K3LUtil k3lutil;
|
||||
|
||||
/* Holy Spirit */
|
||||
static Verbose verbose;
|
||||
|
||||
/* Global timer */
|
||||
static GlobalTimer * global_timer;
|
||||
|
||||
/* Mutex to KhompPvt allocation */
|
||||
static Mutex khomp_alloc_mutex;
|
||||
|
||||
/* Config options class */
|
||||
static ConfigOptions options;
|
||||
|
||||
/* FreeSWITCH Articular Ligaments */
|
||||
static switch_endpoint_interface_t * khomp_endpoint_interface;
|
||||
static switch_endpoint_interface_t * khomp_sms_endpoint_interface;
|
||||
static switch_endpoint_interface_t * khomp_pr_endpoint_interface;
|
||||
static switch_application_interface_t * khomp_app_inteface;
|
||||
static switch_api_interface_t * api_interface;
|
||||
static switch_memory_pool_t * module_pool;
|
||||
|
||||
/* Still a nice workaround */
|
||||
volatile static bool logs_being_rotated;
|
||||
|
||||
/* Trace flags */
|
||||
static bool flag_trace_rdsi;
|
||||
};
|
||||
|
||||
#endif /* _GLOBALS_H_ */
|
||||
|
||||
1996
src/mod/endpoints/mod_khomp/include/k3l.h
Normal file
1996
src/mod/endpoints/mod_khomp/include/k3l.h
Normal file
File diff suppressed because it is too large
Load Diff
1289
src/mod/endpoints/mod_khomp/include/khomp_pvt.h
Normal file
1289
src/mod/endpoints/mod_khomp/include/khomp_pvt.h
Normal file
File diff suppressed because it is too large
Load Diff
376
src/mod/endpoints/mod_khomp/include/khomp_pvt_fxo.h
Normal file
376
src/mod/endpoints/mod_khomp/include/khomp_pvt_fxo.h
Normal file
@@ -0,0 +1,376 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _KHOMP_PVT_FXO_H_
|
||||
#define _KHOMP_PVT_FXO_H_
|
||||
|
||||
#include "khomp_pvt.h"
|
||||
#include "applications.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/********************************* FXO Board **********************************/
|
||||
/******************************************************************************/
|
||||
struct BoardFXO: public Board
|
||||
{
|
||||
/******************************************************************************/
|
||||
/******************************** FXO Channel *********************************/
|
||||
struct KhompPvtFXO: public KhompPvt
|
||||
{
|
||||
/********************************** FXO Call **********************************/
|
||||
struct CallFXO : public Call
|
||||
{
|
||||
CallFXO() {}
|
||||
|
||||
bool process(std::string name, std::string value = "")
|
||||
{
|
||||
if (name == "pre")
|
||||
{
|
||||
DBG(FUNC, D("pre digits adjusted (%s).") % value);
|
||||
_pre_digits = value;
|
||||
}
|
||||
else if (name == "answer_info")
|
||||
{
|
||||
_call_info_report = true;
|
||||
}
|
||||
else if (name == "drop_on")
|
||||
{
|
||||
_call_info_report = true;
|
||||
|
||||
Strings::vector_type drop_item;
|
||||
Strings::tokenize (value, drop_item, ".+");
|
||||
|
||||
for (Strings::vector_type::iterator i = drop_item.begin(); i != drop_item.end(); i++)
|
||||
{
|
||||
|
||||
if ((*i) == "message_box") _call_info_drop |= CI_MESSAGE_BOX;
|
||||
else if ((*i) == "human_answer") _call_info_drop |= CI_HUMAN_ANSWER;
|
||||
else if ((*i) == "answering_machine") _call_info_drop |= CI_ANSWERING_MACHINE;
|
||||
else if ((*i) == "carrier_message") _call_info_drop |= CI_CARRIER_MESSAGE;
|
||||
else if ((*i) == "unknown") _call_info_drop |= CI_UNKNOWN;
|
||||
else
|
||||
{
|
||||
LOG(ERROR, FMT("unknown paramenter to 'calldrop' Dial option: '%s'.") % (*i));
|
||||
continue;
|
||||
}
|
||||
|
||||
DBG(FUNC, FMT("droping call on '%s'.") % (*i));
|
||||
}
|
||||
}
|
||||
else if (name == "usr_xfer")
|
||||
{
|
||||
_user_xfer_digits = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Call::process(name, value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool clear()
|
||||
{
|
||||
_pre_digits.clear();
|
||||
_call_info_report = false;
|
||||
_call_info_drop = 0;
|
||||
|
||||
_user_xfer_digits = Opt::_user_xfer;
|
||||
_user_xfer_buffer.clear();
|
||||
_digits_buffer.clear();
|
||||
|
||||
_var_fax_adjust = T_UNKNOWN;
|
||||
|
||||
return Call::clear();
|
||||
}
|
||||
|
||||
std::string _pre_digits;
|
||||
|
||||
/* report what we got? */
|
||||
bool _call_info_report;
|
||||
|
||||
/* what call info flags should make us drop the call? */
|
||||
long int _call_info_drop;
|
||||
|
||||
/* used for xfer on user signaling */
|
||||
std::string _user_xfer_digits;
|
||||
std::string _user_xfer_buffer;
|
||||
std::string _digits_buffer;
|
||||
|
||||
TriState _var_fax_adjust;
|
||||
|
||||
//ChanTimer::Index _idx_disconnect;
|
||||
};
|
||||
/******************************************************************************/
|
||||
KhompPvtFXO(K3LAPI::target & target) : KhompPvt(target)
|
||||
{
|
||||
_fax = new Fax(this);
|
||||
_transfer = new Transfer<CallFXO>(this);
|
||||
command(KHOMP_LOG, CM_ENABLE_CALL_ANSWER_INFO);
|
||||
}
|
||||
|
||||
~KhompPvtFXO()
|
||||
{
|
||||
delete _fax;
|
||||
delete _transfer;
|
||||
}
|
||||
|
||||
CallFXO * callFXO()
|
||||
{
|
||||
return (CallFXO *)call();
|
||||
}
|
||||
|
||||
int makeCall(std::string params = "");
|
||||
bool doChannelAnswer(CommandRequest &);
|
||||
|
||||
bool onNewCall(K3L_EVENT *e);
|
||||
bool onDisconnect(K3L_EVENT *e);
|
||||
bool onChannelRelease(K3L_EVENT *e);
|
||||
bool onCallSuccess(K3L_EVENT *e);
|
||||
bool onCallFail(K3L_EVENT *e);
|
||||
bool onAudioStatus(K3L_EVENT *e);
|
||||
bool onSeizeSuccess(K3L_EVENT *e);
|
||||
bool onDtmfDetected(K3L_EVENT *e);
|
||||
bool onDtmfSendFinish(K3L_EVENT *);
|
||||
bool onCallAnswerInfo(K3L_EVENT *e);
|
||||
|
||||
virtual bool eventHandler(K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case EV_NEW_CALL:
|
||||
ret = onNewCall(e);
|
||||
break;
|
||||
case EV_DISCONNECT:
|
||||
ret = onDisconnect(e);
|
||||
break;
|
||||
case EV_CHANNEL_FREE:
|
||||
case EV_CHANNEL_FAIL:
|
||||
ret = onChannelRelease(e);
|
||||
break;
|
||||
case EV_CALL_SUCCESS:
|
||||
ret = onCallSuccess(e);
|
||||
break;
|
||||
case EV_CALL_FAIL:
|
||||
ret = onCallFail(e);
|
||||
break;
|
||||
case EV_AUDIO_STATUS:
|
||||
ret = onAudioStatus(e);
|
||||
break;
|
||||
case EV_SEIZE_SUCCESS:
|
||||
ret = onSeizeSuccess(e);
|
||||
break;
|
||||
case EV_DTMF_DETECTED:
|
||||
case EV_PULSE_DETECTED:
|
||||
ret = onDtmfDetected(e);
|
||||
break;
|
||||
case EV_DTMF_SEND_FINISH:
|
||||
ret = onDtmfSendFinish(e);
|
||||
break;
|
||||
case EV_CALL_ANSWER_INFO:
|
||||
ret = onCallAnswerInfo(e);
|
||||
break;
|
||||
case EV_FAX_CHANNEL_FREE:
|
||||
ret = _fax->onFaxChannelRelease(e);
|
||||
break;
|
||||
case EV_FAX_FILE_SENT:
|
||||
case EV_FAX_FILE_FAIL:
|
||||
case EV_FAX_TX_TIMEOUT:
|
||||
case EV_FAX_PAGE_CONFIRMATION:
|
||||
case EV_FAX_REMOTE_INFO:
|
||||
break;
|
||||
case EV_POLARITY_REVERSAL:
|
||||
break;
|
||||
default:
|
||||
ret = KhompPvt::eventHandler(e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool application(ApplicationType type, switch_core_session_t * session, const char *data);
|
||||
|
||||
bool setupConnection();
|
||||
bool autoGainControl(bool enable);
|
||||
void setAnswerInfo(int answer_info);
|
||||
bool indicateBusyUnlocked(int cause, bool sent_signaling = false);
|
||||
void reportFailToReceive(int fail_code);
|
||||
bool validContexts(Board::KhompPvt::ContextListType & contexts, std::string extra_context = "");
|
||||
|
||||
bool isOK(void);
|
||||
|
||||
bool isPhysicalFree()
|
||||
{
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
if (k3lGetDeviceStatus (_target.device, _target.object + ksoChannel, &status, sizeof (status)) != ksSuccess)
|
||||
return false;
|
||||
|
||||
bool physically_free = (status.AddInfo == kfcsEnabled);
|
||||
|
||||
if(status.CallStatus != kcsFree || !physically_free)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "call status not free, or not physically free!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool cleanup(CleanupType type = CLN_HARD)
|
||||
{
|
||||
//Board::board(_target.device)->_timers.del(callFXO()->_idx_disconnect);
|
||||
//callFXO()->_idx_disconnect.reset();
|
||||
|
||||
call()->_flags.clear(Kflags::CALL_WAIT_SEIZE);
|
||||
call()->_flags.clear(Kflags::EARLY_RINGBACK);
|
||||
|
||||
_transfer->clear();
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case CLN_HARD:
|
||||
case CLN_FAIL:
|
||||
call()->_flags.clear(Kflags::FAX_DETECTED);
|
||||
break;
|
||||
case CLN_SOFT:
|
||||
break;
|
||||
}
|
||||
|
||||
return KhompPvt::cleanup(type);
|
||||
}
|
||||
|
||||
virtual void getSpecialVariables()
|
||||
{
|
||||
try
|
||||
{
|
||||
const char * str_fax = getFSChannelVar("KAdjustForFax");
|
||||
|
||||
callFXO()->_var_fax_adjust = (str_fax ? (!SAFE_strcasecmp(str_fax, "true") ? T_TRUE : T_FALSE) : T_UNKNOWN);
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) %s") % err._msg.c_str());
|
||||
}
|
||||
|
||||
KhompPvt::getSpecialVariables();
|
||||
}
|
||||
|
||||
bool sendDtmf(std::string digit);
|
||||
|
||||
/* used by app FAX */
|
||||
Fax * _fax;
|
||||
|
||||
Transfer<CallFXO> * _transfer;
|
||||
|
||||
// static void delayedDisconnect(Board::KhompPvt * pvt);
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
BoardFXO(int id) : Board(id) {}
|
||||
|
||||
void initializeChannels(void)
|
||||
{
|
||||
LOG(MESSAGE, "(FXO) loading channels ...");
|
||||
|
||||
for (unsigned obj = 0; obj < Globals::k3lapi.channel_count(_device_id); obj++)
|
||||
{
|
||||
K3LAPI::target tgt(Globals::k3lapi, K3LAPI::target::CHANNEL, _device_id, obj);
|
||||
KhompPvt * pvt;
|
||||
|
||||
switch(Globals::k3lapi.channel_config(_device_id, obj).Signaling)
|
||||
{
|
||||
case ksigAnalog:
|
||||
pvt = new BoardFXO::KhompPvtFXO(tgt);
|
||||
pvt->_call = new BoardFXO::KhompPvtFXO::CallFXO();
|
||||
DBG(FUNC, "(FXO) FXO channel");
|
||||
break;
|
||||
default:
|
||||
pvt = new Board::KhompPvt(tgt);
|
||||
pvt->_call = new Board::KhompPvt::Call();
|
||||
DBG(FUNC, FMT("(FXO) signaling %d unknown") % Globals::k3lapi.channel_config(_device_id, obj).Signaling);
|
||||
break;
|
||||
}
|
||||
|
||||
_channels.push_back(pvt);
|
||||
|
||||
pvt->cleanup();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
virtual bool eventHandler(const int obj, K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(FXO Board) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case :
|
||||
break;
|
||||
default:
|
||||
ret = Board::eventHandler(obj, e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(FXO Board) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
#endif /* _KHOMP_PVT_FXO_H_*/
|
||||
|
||||
332
src/mod/endpoints/mod_khomp/include/khomp_pvt_gsm.h
Normal file
332
src/mod/endpoints/mod_khomp/include/khomp_pvt_gsm.h
Normal file
@@ -0,0 +1,332 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _KHOMP_PVT_GSM_H_
|
||||
#define _KHOMP_PVT_GSM_H_
|
||||
|
||||
#include "khomp_pvt.h"
|
||||
#include "applications.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/********************************* GSM Board **********************************/
|
||||
/******************************************************************************/
|
||||
struct BoardGSM: public Board
|
||||
{
|
||||
/******************************************************************************/
|
||||
/******************************** GSM Channel *********************************/
|
||||
struct KhompPvtGSM: public KhompPvt
|
||||
{
|
||||
/********************************** GSM Call **********************************/
|
||||
struct CallGSM : public Call
|
||||
{
|
||||
CallGSM() {}
|
||||
|
||||
bool process(std::string name, std::string value = "")
|
||||
{
|
||||
if (name == "answer_info")
|
||||
{
|
||||
_call_info_report = true;
|
||||
}
|
||||
else if (name == "drop_on")
|
||||
{
|
||||
_call_info_report = true;
|
||||
|
||||
Strings::vector_type drop_item;
|
||||
Strings::tokenize (value, drop_item, ".+");
|
||||
|
||||
for (Strings::vector_type::iterator i = drop_item.begin(); i != drop_item.end(); i++)
|
||||
{
|
||||
|
||||
if ((*i) == "message_box") _call_info_drop |= CI_MESSAGE_BOX;
|
||||
else if ((*i) == "human_answer") _call_info_drop |= CI_HUMAN_ANSWER;
|
||||
else if ((*i) == "answering_machine") _call_info_drop |= CI_ANSWERING_MACHINE;
|
||||
else if ((*i) == "carrier_message") _call_info_drop |= CI_CARRIER_MESSAGE;
|
||||
else if ((*i) == "unknown") _call_info_drop |= CI_UNKNOWN;
|
||||
else
|
||||
{
|
||||
LOG(ERROR, FMT("unknown paramenter to 'calldrop' Dial option: '%s'.") % (*i));
|
||||
continue;
|
||||
}
|
||||
|
||||
DBG(FUNC, FMT("droping call on '%s'.") % (*i));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Call::process(name, value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool clear()
|
||||
{
|
||||
_call_info_report = false;
|
||||
_call_info_drop = 0;
|
||||
|
||||
return Call::clear();
|
||||
}
|
||||
|
||||
/* report what we got? */
|
||||
bool _call_info_report;
|
||||
|
||||
/* what call info flags should make us drop the call? */
|
||||
long int _call_info_drop;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
KhompPvtGSM(K3LAPI::target & target) : KhompPvt(target)
|
||||
{
|
||||
_sms = new SMS(this);
|
||||
command(KHOMP_LOG, CM_ENABLE_CALL_ANSWER_INFO);
|
||||
}
|
||||
|
||||
~KhompPvtGSM()
|
||||
{
|
||||
delete _sms;
|
||||
}
|
||||
|
||||
CallGSM * callGSM()
|
||||
{
|
||||
return (CallGSM *)call();
|
||||
}
|
||||
|
||||
int makeCall(std::string params = "");
|
||||
bool doChannelAnswer(CommandRequest &);
|
||||
bool doChannelHangup(CommandRequest &);
|
||||
|
||||
//bool onNewCall(K3L_EVENT *e);
|
||||
bool onChannelRelease(K3L_EVENT *e);
|
||||
bool onCallFail(K3L_EVENT *e);
|
||||
bool onCallSuccess(K3L_EVENT *e);
|
||||
bool onCallAnswerInfo(K3L_EVENT *e);
|
||||
bool onDisconnect(K3L_EVENT *e);
|
||||
//bool onDtmfDetected(K3L_EVENT *e);
|
||||
|
||||
virtual bool eventHandler(K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(GSM) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
/*
|
||||
case EV_NEW_CALL:
|
||||
onNewCall(e);
|
||||
break;
|
||||
*/
|
||||
case EV_CHANNEL_FREE:
|
||||
case EV_CHANNEL_FAIL:
|
||||
ret = onChannelRelease(e);
|
||||
break;
|
||||
case EV_CALL_SUCCESS:
|
||||
ret = onCallSuccess(e);
|
||||
break;
|
||||
case EV_DISCONNECT:
|
||||
ret = onDisconnect(e);
|
||||
break;
|
||||
case EV_CALL_FAIL:
|
||||
ret = onCallFail(e);
|
||||
break;
|
||||
case EV_CALL_ANSWER_INFO:
|
||||
ret = onCallAnswerInfo(e);
|
||||
break;
|
||||
case EV_RECV_FROM_MODEM:
|
||||
break;
|
||||
case EV_NEW_SMS:
|
||||
ret = _sms->onNewSMS(e);
|
||||
break;
|
||||
case EV_SMS_INFO:
|
||||
ret = _sms->onSMSInfo(e);
|
||||
break;
|
||||
case EV_SMS_DATA:
|
||||
ret = _sms->onSMSData(e);
|
||||
break;
|
||||
case EV_SMS_SEND_RESULT:
|
||||
ret = _sms->onSMSSendResult(e);
|
||||
break;
|
||||
default:
|
||||
ret = KhompPvt::eventHandler(e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(GSM) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool application(ApplicationType type, switch_core_session_t * session, const char *data);
|
||||
|
||||
bool setupConnection();
|
||||
void setAnswerInfo(int answer_info);
|
||||
bool indicateBusyUnlocked(int cause, bool sent_signaling = false);
|
||||
void reportFailToReceive(int fail_code);
|
||||
int causeFromCallFail(int fail);
|
||||
int callFailFromCause(int cause);
|
||||
bool isOK(void);
|
||||
|
||||
bool isPhysicalFree()
|
||||
{
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
if (k3lGetDeviceStatus (_target.device, _target.object + ksoChannel, &status, sizeof (status)) != ksSuccess)
|
||||
return false;
|
||||
|
||||
bool physically_free = (status.AddInfo == kgsmIdle);
|
||||
|
||||
if(status.CallStatus != kcsFree || !physically_free)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "call status not free, or not physically free!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sendPreAudio(int rb_value = RingbackDefs::RB_SEND_NOTHING)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool selectSimCard(const char * sim_card)
|
||||
{
|
||||
return command(KHOMP_LOG, CM_SIM_CARD_SELECT, sim_card);
|
||||
}
|
||||
|
||||
std::string getStatistics(Statistics::Type type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case Statistics::DETAILED:
|
||||
{
|
||||
/* buffer our data to return at the end */
|
||||
std::string strBuffer;
|
||||
|
||||
strBuffer.append(_pvt_statistics->getDetailed());
|
||||
strBuffer.append(_sms->statistics()->getDetailed());
|
||||
return strBuffer;
|
||||
}
|
||||
case Statistics::ROW:
|
||||
{
|
||||
return _pvt_statistics->getRow();
|
||||
}
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void clearStatistics()
|
||||
{
|
||||
KhompPvt::clearStatistics();
|
||||
_sms->statistics()->clear();
|
||||
}
|
||||
|
||||
bool validContexts(Board::KhompPvt::ContextListType & contexts, std::string extra_context = "");
|
||||
|
||||
SMS * _sms;
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
BoardGSM(int id) : Board(id) {}
|
||||
|
||||
void initializeChannels(void)
|
||||
{
|
||||
LOG(MESSAGE, "(GSM) loading channels ...");
|
||||
|
||||
for (unsigned obj = 0; obj < Globals::k3lapi.channel_count(_device_id); obj++)
|
||||
{
|
||||
K3LAPI::target tgt(Globals::k3lapi, K3LAPI::target::CHANNEL, _device_id, obj);
|
||||
KhompPvt * pvt;
|
||||
|
||||
switch(Globals::k3lapi.channel_config(_device_id, obj).Signaling)
|
||||
{
|
||||
case ksigGSM:
|
||||
pvt = new BoardGSM::KhompPvtGSM(tgt);
|
||||
pvt->_call = new BoardGSM::KhompPvtGSM::CallGSM();
|
||||
((BoardGSM::KhompPvtGSM *)pvt)->_sms->start();
|
||||
DBG(FUNC, "(GSM) GSM channel");
|
||||
break;
|
||||
default:
|
||||
pvt = new Board::KhompPvt(tgt);
|
||||
pvt->_call = new Board::KhompPvt::Call();
|
||||
DBG(FUNC, FMT("(GSM) signaling %d unknown") % Globals::k3lapi.channel_config(_device_id, obj).Signaling);
|
||||
break;
|
||||
}
|
||||
|
||||
_channels.push_back(pvt);
|
||||
|
||||
pvt->cleanup();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
virtual bool eventHandler(const int obj, K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(GSM Board) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case :
|
||||
break;
|
||||
default:
|
||||
ret = Board::eventHandler(obj, e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(GSM Board) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
#endif /* _KHOMP_PVT_GSM_H_*/
|
||||
|
||||
1122
src/mod/endpoints/mod_khomp/include/khomp_pvt_kxe1.h
Normal file
1122
src/mod/endpoints/mod_khomp/include/khomp_pvt_kxe1.h
Normal file
File diff suppressed because it is too large
Load Diff
304
src/mod/endpoints/mod_khomp/include/khomp_pvt_passive.h
Normal file
304
src/mod/endpoints/mod_khomp/include/khomp_pvt_passive.h
Normal file
@@ -0,0 +1,304 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _KHOMP_PVT_PASSIVE_H_
|
||||
#define _KHOMP_PVT_PASSIVE_H_
|
||||
|
||||
#include "khomp_pvt.h"
|
||||
#include "applications.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Passive Board *******************************/
|
||||
/******************************************************************************/
|
||||
struct BoardPassive: public Board
|
||||
{
|
||||
/******************************************************************************/
|
||||
/******************************** Passive Channel *****************************/
|
||||
struct KhompPvtPassive: public KhompPvt
|
||||
{
|
||||
/******************************** Passive Call ********************************/
|
||||
/*
|
||||
struct CallPassive : public Call
|
||||
{
|
||||
CallPassive() {}
|
||||
|
||||
bool process(std::string name, std::string value = "")
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
return Call::process(name, value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool clear()
|
||||
{
|
||||
return Call::clear();
|
||||
}
|
||||
|
||||
};
|
||||
*/
|
||||
/******************************************************************************/
|
||||
KhompPvtPassive(K3LAPI::target & target) : KhompPvt(target)
|
||||
{
|
||||
}
|
||||
|
||||
~KhompPvtPassive()
|
||||
{
|
||||
}
|
||||
/*
|
||||
CallPassive * callPassive()
|
||||
{
|
||||
return (CallPassive *)call();
|
||||
}
|
||||
*/
|
||||
|
||||
virtual bool eventHandler(K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(Passive) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case EV_CHANNEL_FREE:
|
||||
case EV_CHANNEL_FAIL:
|
||||
ret = onChannelRelease(e);
|
||||
break;
|
||||
/*
|
||||
case EV_CALL_SUCCESS:
|
||||
case EV_CALL_FAIL:
|
||||
case EV_PULSE_DETECTED:
|
||||
*/
|
||||
case EV_DISCONNECT:
|
||||
case EV_DTMF_DETECTED:
|
||||
case EV_AUDIO_STATUS:
|
||||
break;
|
||||
default:
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(Passive) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool validContexts(Board::KhompPvt::ContextListType & contexts, std::string extra_context = "");
|
||||
|
||||
/*
|
||||
virtual bool cleanup(CleanupType type = CLN_HARD)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case CLN_HARD:
|
||||
case CLN_FAIL:
|
||||
break;
|
||||
case CLN_SOFT:
|
||||
break;
|
||||
}
|
||||
|
||||
return KhompPvt::cleanup(type);
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/********************************** HI Channel ********************************/
|
||||
struct KhompPvtHI: public KhompPvtPassive
|
||||
{
|
||||
KhompPvtHI(K3LAPI::target & target) : KhompPvtPassive(target)
|
||||
{
|
||||
}
|
||||
|
||||
~KhompPvtHI()
|
||||
{
|
||||
}
|
||||
|
||||
bool onSeizureStart(K3L_EVENT *e);
|
||||
|
||||
virtual bool eventHandler(K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(HI) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case EV_SEIZURE_START:
|
||||
ret = onSeizureStart(e);
|
||||
break;
|
||||
case EV_RING_DETECTED:
|
||||
case EV_POLARITY_REVERSAL:
|
||||
break;
|
||||
default:
|
||||
ret = KhompPvtPassive::eventHandler(e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(HI) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/********************************* KPR Channel ********************************/
|
||||
struct KhompPvtKPR: public KhompPvtPassive
|
||||
{
|
||||
KhompPvtKPR(K3LAPI::target & target) : KhompPvtPassive(target)
|
||||
{
|
||||
}
|
||||
|
||||
~KhompPvtKPR()
|
||||
{
|
||||
}
|
||||
|
||||
bool onNewCall(K3L_EVENT *e);
|
||||
/*
|
||||
bool onConnect(K3L_EVENT *e);
|
||||
*/
|
||||
|
||||
virtual bool eventHandler(K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(KPR) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case EV_NEW_CALL:
|
||||
ret = onNewCall(e);
|
||||
break;
|
||||
case EV_CONNECT:
|
||||
break;
|
||||
case EV_SEIZURE_START:
|
||||
case EV_CAS_MFC_RECV:
|
||||
case EV_CAS_LINE_STT_CHANGED:
|
||||
case EV_LINK_STATUS:
|
||||
break;
|
||||
default:
|
||||
ret = KhompPvtPassive::eventHandler(e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(KPR) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool obtainBoth();
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
BoardPassive(int id) : Board(id) {}
|
||||
|
||||
void initializeChannels(void)
|
||||
{
|
||||
LOG(MESSAGE, "(Passive) loading channels ...");
|
||||
|
||||
for (unsigned obj = 0; obj < Globals::k3lapi.channel_count(_device_id); obj++)
|
||||
{
|
||||
K3LAPI::target tgt(Globals::k3lapi, K3LAPI::target::CHANNEL, _device_id, obj);
|
||||
KhompPvt * pvt;
|
||||
|
||||
switch(Globals::k3lapi.channel_config(_device_id, obj).Signaling)
|
||||
{
|
||||
case ksigAnalog:
|
||||
pvt = new BoardPassive::KhompPvtHI(tgt);
|
||||
pvt->_call = new BoardPassive::KhompPvtHI::Call();
|
||||
DBG(FUNC, "(Passive) HI channel");
|
||||
break;
|
||||
CASE_RDSI_SIG:
|
||||
CASE_R2_SIG:
|
||||
CASE_FLASH_GRP:
|
||||
case ksigAnalogTerminal:
|
||||
pvt = new BoardPassive::KhompPvtKPR(tgt);
|
||||
pvt->_call = new BoardPassive::KhompPvtKPR::Call();
|
||||
DBG(FUNC, "(Passive) KPR channel");
|
||||
break;
|
||||
default:
|
||||
pvt = new Board::KhompPvt(tgt);
|
||||
pvt->_call = new Board::KhompPvt::Call();
|
||||
DBG(FUNC, FMT("(Passive) signaling %d unknown") % Globals::k3lapi.channel_config(_device_id, obj).Signaling);
|
||||
break;
|
||||
}
|
||||
|
||||
_channels.push_back(pvt);
|
||||
|
||||
pvt->cleanup();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
virtual bool eventHandler(const int obj, K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, D("(Passive Board) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch(e->Code)
|
||||
{
|
||||
case :
|
||||
break;
|
||||
default:
|
||||
ret = Board::eventHandler(obj, e);
|
||||
break;
|
||||
}
|
||||
|
||||
DBG(STRM, D("(Passive Board) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
#endif /* _KHOMP_PVT_PASSIVE_H_*/
|
||||
|
||||
86
src/mod/endpoints/mod_khomp/include/lock.h
Normal file
86
src/mod/endpoints/mod_khomp/include/lock.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef CHAN_LOCK_H
|
||||
#define CHAN_LOCK_H
|
||||
|
||||
#include <scoped_lock.hpp>
|
||||
#include "khomp_pvt.h"
|
||||
|
||||
|
||||
struct ScopedLockFailed
|
||||
{
|
||||
typedef enum { NULL_PVT, NULL_SOFT_CHAN, FAILED, ALLOC_FAILED } FailType;
|
||||
|
||||
ScopedLockFailed(FailType fail, std::string msg)
|
||||
: _fail(fail), _msg(msg) {};
|
||||
|
||||
FailType _fail;
|
||||
std::string _msg;
|
||||
};
|
||||
|
||||
|
||||
struct ScopedPvtLock: public ScopedLockBasic
|
||||
{
|
||||
typedef Board::KhompPvt KhompPvt;
|
||||
|
||||
ScopedPvtLock(KhompPvt * pvt);
|
||||
~ScopedPvtLock();
|
||||
|
||||
void unlock();
|
||||
|
||||
protected:
|
||||
KhompPvt * _pvt;
|
||||
};
|
||||
|
||||
|
||||
struct ScopedAllocLock : public ScopedLockBasic
|
||||
{
|
||||
ScopedAllocLock();
|
||||
~ScopedAllocLock();
|
||||
|
||||
void unlock();
|
||||
|
||||
};
|
||||
|
||||
#endif /* CHAN_LOCK_H */
|
||||
141
src/mod/endpoints/mod_khomp/include/logger.h
Normal file
141
src/mod/endpoints/mod_khomp/include/logger.h
Normal file
@@ -0,0 +1,141 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _Logger_H_
|
||||
#define _Logger_H_
|
||||
|
||||
#include <switch.h>
|
||||
#include <string>
|
||||
#include <simple_lock.hpp>
|
||||
#include <logger.hpp>
|
||||
#include <configurator/configfile.hpp>
|
||||
#include <klog-config.hpp>
|
||||
#include "defs.h"
|
||||
#include "format.hpp"
|
||||
|
||||
namespace K
|
||||
{
|
||||
struct LogConfig
|
||||
{
|
||||
static bool set(Logfile &, const char *, const char *, bool);
|
||||
static bool commit(Logfile &);
|
||||
};
|
||||
|
||||
struct SwitchConsoleLog {};
|
||||
|
||||
struct SwitchPrinter: public Logger::DefaultPrinter
|
||||
{
|
||||
typedef Logger::DefaultPrinter Super;
|
||||
typedef Tagged::Union < std::ostream *, Tagged::Union < int, Tagged::Union < SwitchConsoleLog > > > BaseType;
|
||||
|
||||
SwitchPrinter(std::string & msg): Super(msg) {};
|
||||
|
||||
using Super::operator();
|
||||
|
||||
bool operator()(const SwitchConsoleLog & ignored)
|
||||
{
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN,SWITCH_LOG_CONSOLE,"%s",_msg.c_str());
|
||||
return true;
|
||||
};
|
||||
};
|
||||
|
||||
/* Log manager declaration. */
|
||||
typedef Logger::Manager <class_type, output_type, SwitchPrinter, SimpleLock> LogManager;
|
||||
|
||||
/* Forward declaration */
|
||||
struct LogInternalManager;
|
||||
|
||||
struct Logger
|
||||
{
|
||||
/* Logger instance. */
|
||||
static LogManager Logg;
|
||||
|
||||
/* Util Logger instance. */
|
||||
static LogInternalManager Logg2;
|
||||
|
||||
static bool start();
|
||||
static void stop();
|
||||
static bool rotate();
|
||||
static void processLogConsole(switch_stream_handle_t *s, const std::string options, bool invert, bool unique);
|
||||
static void processLogDisk(switch_stream_handle_t *s, const std::string options, bool invert, bool unique);
|
||||
|
||||
private:
|
||||
static std::string base_path;
|
||||
static std::ofstream generic_file;
|
||||
};
|
||||
|
||||
/* Internal logging facility declaration */
|
||||
struct LogInternalManager
|
||||
{
|
||||
bool operator()(class_type classe, switch_stream_handle_t *stream, const char *args)
|
||||
{
|
||||
switch (classe)
|
||||
{
|
||||
case C_CLI:
|
||||
stream->write_function(stream,"%s\n",args);
|
||||
return true;
|
||||
default:
|
||||
return K::Logger::Logg(classe, args);
|
||||
}
|
||||
}
|
||||
|
||||
bool operator()(class_type classe, switch_stream_handle_t *stream, Format &fmt)
|
||||
{
|
||||
return K::Logger::Logg2(classe,stream,(char*) STR(fmt));
|
||||
}
|
||||
|
||||
bool operator()(class_type classe, switch_stream_handle_t *stream, const char *fmt, const char *args)
|
||||
{
|
||||
switch (classe)
|
||||
{
|
||||
case C_CLI:
|
||||
stream->write_function(stream,fmt,args);
|
||||
return true;
|
||||
default:
|
||||
return K::Logger::Logg(classe, FMT(fmt) % args);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* _Logger_H_ */
|
||||
250
src/mod/endpoints/mod_khomp/include/opt.h
Normal file
250
src/mod/endpoints/mod_khomp/include/opt.h
Normal file
@@ -0,0 +1,250 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _OPT_H_
|
||||
#define _OPT_H_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "switch.h"
|
||||
#include "utils.h"
|
||||
|
||||
struct CadenceType
|
||||
{
|
||||
CadenceType(void)
|
||||
: ring(0), ring_s(0), ring_ext(0), ring_ext_s(0) {};
|
||||
|
||||
CadenceType(unsigned int _ring, unsigned int _ring_s)
|
||||
: ring(_ring), ring_s(_ring_s), ring_ext(0), ring_ext_s(0) {};
|
||||
|
||||
CadenceType(unsigned int _ring, unsigned int _ring_s, unsigned int _ring_ext, unsigned int _ring_ext_s)
|
||||
: ring(_ring), ring_s(_ring_s), ring_ext(_ring_ext), ring_ext_s(_ring_ext_s) {};
|
||||
|
||||
unsigned int ring;
|
||||
unsigned int ring_s;
|
||||
unsigned int ring_ext;
|
||||
unsigned int ring_ext_s;
|
||||
};
|
||||
|
||||
struct CSpan {
|
||||
std::string _dialplan;
|
||||
std::string _context;
|
||||
std::string _dialstring;
|
||||
};
|
||||
|
||||
struct Opt
|
||||
{
|
||||
typedef std::pair < std::string, CadenceType > CadencesPairType;
|
||||
typedef std::map < std::string, CadenceType > CadencesMapType;
|
||||
|
||||
typedef std::map < std::string, std::string > OrigToDestMapType;
|
||||
typedef std::pair < std::string, std::string > OrigToDestPairType;
|
||||
typedef std::vector < std::string > DestVectorType;
|
||||
|
||||
typedef std::map < unsigned int, std::string > BoardToOrigMapType;
|
||||
typedef std::pair < unsigned int, std::string > BoardToOrigPairType;
|
||||
|
||||
typedef std::map < std::string, unsigned int > OrigToNseqMapType;
|
||||
typedef std::pair < std::string, unsigned int > OrigToNseqPairType;
|
||||
|
||||
typedef std::pair < unsigned int,unsigned int > ObjectIdType;
|
||||
typedef std::pair < std::string, ObjectIdType > BranchToObjectPairType;
|
||||
typedef std::map < std::string, ObjectIdType > BranchToObjectMapType;
|
||||
|
||||
typedef std::map < std::string, std::string > BranchToOptMapType;
|
||||
typedef std::pair < std::string, std::string > BranchToOptPairType;
|
||||
|
||||
typedef std::map < std::string, std::string > GroupToDestMapType;
|
||||
typedef std::pair < std::string, std::string > GroupToDestPairType;
|
||||
|
||||
typedef std::pair < std::string, CSpan > SpanPairType;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GFLAG_MY_CODEC_PREFS = (1 << 0)
|
||||
}
|
||||
GFLAGS;
|
||||
|
||||
static void initialize(void);
|
||||
static void obtain(void);
|
||||
static void commit(void);
|
||||
static void printConfiguration(switch_stream_handle_t*);
|
||||
|
||||
protected:
|
||||
|
||||
static void loadConfiguration(const char *, const char **, bool show_errors = true);
|
||||
static void cleanConfiguration(void);
|
||||
|
||||
static switch_xml_t processSimpleXML(switch_xml_t &xml, const std::string& child_name);
|
||||
static void processGroupXML(switch_xml_t &xml);
|
||||
static void processCadenceXML(switch_xml_t &xml);
|
||||
static void processFXSBranchesXML(switch_xml_t &xml);
|
||||
static void processFXSHotlines(switch_xml_t &xml);
|
||||
static void processFXSOptions(switch_xml_t &xml);
|
||||
|
||||
public:
|
||||
static bool _debug;
|
||||
static std::string _dialplan;
|
||||
static std::string _context;
|
||||
static std::map < std::string, CSpan > _spans;
|
||||
static GroupToDestMapType _groups;
|
||||
static CadencesMapType _cadences;
|
||||
|
||||
static bool _echo_canceller;
|
||||
static bool _auto_gain_control;
|
||||
static bool _out_of_band_dtmfs;
|
||||
static bool _suppression_delay;
|
||||
static bool _pulse_forwarding;
|
||||
static bool _native_bridge;
|
||||
static bool _recording;
|
||||
static bool _has_ctbus;
|
||||
static bool _fxs_bina;
|
||||
static bool _fxo_send_pre_audio;
|
||||
static bool _drop_collect_call;
|
||||
static bool _ignore_letter_dtmfs;
|
||||
static bool _optimize_audio_path;
|
||||
|
||||
static bool _auto_fax_adjustment;
|
||||
static unsigned int _fax_adjustment_timeout;
|
||||
|
||||
static bool _r2_strict_behaviour;
|
||||
static unsigned int _r2_preconnect_wait;
|
||||
|
||||
static unsigned int _fxs_digit_timeout;
|
||||
|
||||
static unsigned int _transferdigittimeout;
|
||||
|
||||
static std::string _blindxfer;
|
||||
static std::string _atxfer;
|
||||
|
||||
static unsigned int _ringback_co_delay;
|
||||
static unsigned int _ringback_pbx_delay;
|
||||
|
||||
static unsigned int _disconnect_delay;
|
||||
|
||||
static int _input_volume;
|
||||
static int _output_volume;
|
||||
|
||||
static DestVectorType _fxs_co_dialtone;
|
||||
static OrigToDestMapType _fxs_hotline;
|
||||
static std::string _fxs_global_orig_base;
|
||||
|
||||
static BoardToOrigMapType _fxs_orig_base;
|
||||
static BranchToObjectMapType _fxs_branch_map;
|
||||
static BranchToOptMapType _branch_options;
|
||||
|
||||
static std::string _global_mohclass;
|
||||
static std::string _global_language;
|
||||
|
||||
static std::string _record_prefix;
|
||||
|
||||
static std::string _context_gsm_call;
|
||||
static std::string _context2_gsm_call;
|
||||
static std::string _context_gsm_sms;
|
||||
static std::string _context_fxo;
|
||||
static std::string _context2_fxo;
|
||||
static std::string _context_fxs;
|
||||
static std::string _context2_fxs;
|
||||
static std::string _context_digital;
|
||||
static std::string _context_pr;
|
||||
static std::string _user_xfer;
|
||||
|
||||
static int _amaflags;
|
||||
static std::string _callgroup;
|
||||
static std::string _pickupgroup; /* or intercept */
|
||||
|
||||
static std::string _accountcode;
|
||||
|
||||
static unsigned int _kommuter_timeout;
|
||||
static std::string _kommuter_activation;
|
||||
|
||||
static unsigned int _audio_packet_size;
|
||||
|
||||
protected:
|
||||
|
||||
struct ProcessFXSCODialtone
|
||||
{
|
||||
void operator()(std::string options);
|
||||
};
|
||||
|
||||
struct ProcessRecordPrefix
|
||||
{
|
||||
void operator()(std::string path);
|
||||
};
|
||||
|
||||
struct ProcessAMAFlags
|
||||
{
|
||||
void operator()(std::string options);
|
||||
};
|
||||
|
||||
struct ProcessCallGroup
|
||||
{
|
||||
void operator()(std::string options);
|
||||
};
|
||||
|
||||
struct ProcessPickupGroup
|
||||
{
|
||||
void operator()(std::string options);
|
||||
};
|
||||
|
||||
struct ProcessLogOptions
|
||||
{
|
||||
ProcessLogOptions(output_type output): _output(output) {};
|
||||
|
||||
void operator()(std::string options);
|
||||
|
||||
protected:
|
||||
output_type _output;
|
||||
};
|
||||
|
||||
struct ProcessTraceOptions
|
||||
{
|
||||
void operator()(std::string options);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* _OPT_H_ */
|
||||
|
||||
1
src/mod/endpoints/mod_khomp/include/revision.h
Normal file
1
src/mod/endpoints/mod_khomp/include/revision.h
Normal file
@@ -0,0 +1 @@
|
||||
#define MOD_KHOMP_VERSION "1.0 - (rev: 5796)"
|
||||
98
src/mod/endpoints/mod_khomp/include/spec.h
Normal file
98
src/mod/endpoints/mod_khomp/include/spec.h
Normal file
@@ -0,0 +1,98 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _SPEC_HPP_
|
||||
#define _SPEC_HPP_
|
||||
|
||||
#include <function.hpp>
|
||||
#include "khomp_pvt.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SPR_CONTINUE,
|
||||
SPR_SUCCESS,
|
||||
SPR_FAIL,
|
||||
}
|
||||
SpecRetType;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SPF_FIRST = 0x01,
|
||||
SPF_CYCLIC = 0x02,
|
||||
}
|
||||
SpecFlagType;
|
||||
|
||||
typedef unsigned int SpecFlagsType;
|
||||
|
||||
|
||||
typedef Function::Function3 < bool, unsigned int, unsigned int, SpecFlagsType & > SpecFunType;
|
||||
|
||||
|
||||
//SpecRetType processSpecAtom(std::string &, SpecFlagsType &, SpecFunType &);
|
||||
//SpecRetType processSpecAtoms(std::string &, SpecFlagsType &, SpecFunType &);
|
||||
//bool processCallChannelString(std::string &, Board::KhompPvt *&, int *, bool need_free = true);
|
||||
|
||||
/* Request a Board::KhompPvt based on dialplan string.
|
||||
|
||||
-- Format of dialplan string. --
|
||||
|
||||
Dial(Khomp/B2C58/4832625644)
|
||||
| | | |
|
||||
| | | |
|
||||
| | | |
|
||||
| | | +- Destination number.
|
||||
| | |
|
||||
| | +- Identifier for board 2, channel 58.
|
||||
| |
|
||||
| +- Khomp channel identifier.
|
||||
|
|
||||
+- Dial Application.
|
||||
*/
|
||||
|
||||
Board::KhompPvt * processDialString(const char *, int *);
|
||||
void processGroupString();
|
||||
|
||||
Board::KhompPvt * processSMSString(const char *, int *);
|
||||
|
||||
#endif /* _SPEC_HPP_ */
|
||||
|
||||
772
src/mod/endpoints/mod_khomp/include/utils.h
Normal file
772
src/mod/endpoints/mod_khomp/include/utils.h
Normal file
@@ -0,0 +1,772 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef _UTILS_H_
|
||||
#define _UTILS_H_
|
||||
|
||||
#include <bitset>
|
||||
#include <refcounter.hpp>
|
||||
#include <ringbuffer.hpp>
|
||||
#include <simple_lock.hpp>
|
||||
#include <saved_condition.hpp>
|
||||
#include <thread.hpp>
|
||||
#include "globals.h"
|
||||
#include "logger.h"
|
||||
#include "defs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/************************** Defining applications *****************************/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
FAX_ADJUST,
|
||||
FAX_SEND,
|
||||
FAX_RECEIVE,
|
||||
USER_TRANSFER,
|
||||
SMS_CHECK,
|
||||
SMS_SEND,
|
||||
SELECT_SIM_CARD,
|
||||
}
|
||||
ApplicationType;
|
||||
|
||||
/******************************************************************************/
|
||||
/***************** Abstraction for defining channel flags *********************/
|
||||
|
||||
struct Kflags
|
||||
{
|
||||
typedef enum
|
||||
{
|
||||
CONNECTED = 0,
|
||||
REALLY_CONNECTED,
|
||||
|
||||
IS_OUTGOING,
|
||||
IS_INCOMING,
|
||||
|
||||
STREAM_UP,
|
||||
LISTEN_UP,
|
||||
|
||||
GEN_CO_RING,
|
||||
GEN_PBX_RING,
|
||||
|
||||
HAS_PRE_AUDIO,
|
||||
HAS_CALL_FAIL,
|
||||
|
||||
DROP_COLLECT,
|
||||
|
||||
NEEDS_RINGBACK_CMD, //R2
|
||||
EARLY_RINGBACK, //FXO
|
||||
|
||||
FAX_DETECTED, //Digital, FXO
|
||||
FAX_SENDING, //Digital, FXO
|
||||
FAX_RECEIVING, //Digital, FXO
|
||||
|
||||
OUT_OF_BAND_DTMFS,
|
||||
|
||||
KEEP_DTMF_SUPPRESSION,
|
||||
KEEP_ECHO_CANCELLATION,
|
||||
KEEP_AUTO_GAIN_CONTROL,
|
||||
|
||||
WAIT_SEND_DTMF,
|
||||
|
||||
CALL_WAIT_SEIZE, //FXO
|
||||
|
||||
NUMBER_DIAL_FINISHD, //R2
|
||||
NUMBER_DIAL_ONGOING, //R2
|
||||
|
||||
FXS_OFFHOOK, //FXS
|
||||
FXS_DIAL_FINISHD, //FXS
|
||||
FXS_DIAL_ONGOING, //FXS
|
||||
FXS_FLASH_TRANSFER, //FXS
|
||||
|
||||
XFER_QSIG_DIALING, //ISDN
|
||||
XFER_DIALING, //ISDN, FXO
|
||||
|
||||
SMS_DOING_UPLOAD, //GSM
|
||||
|
||||
/*
|
||||
NOW LOADING ...
|
||||
|
||||
BRIDGED,
|
||||
*/
|
||||
|
||||
/* Do not remove this last FLAG */
|
||||
INVALID_FLAG
|
||||
}
|
||||
FlagType;
|
||||
|
||||
struct Flag
|
||||
{
|
||||
const char *_name;
|
||||
bool _value;
|
||||
};
|
||||
|
||||
Kflags() { init(); };
|
||||
|
||||
void init();
|
||||
|
||||
inline const char * name(FlagType bit) { return _flags[bit]._name; }
|
||||
|
||||
inline bool check(FlagType bit)
|
||||
{
|
||||
#ifdef DEBUG_FLAGS
|
||||
DBG(FUNC, D("Flag %s=%s") % name(bit) %
|
||||
(_flags[bit]._value ? "TRUE" : "FALSE"));
|
||||
#endif
|
||||
return _flags[bit]._value;
|
||||
}
|
||||
|
||||
inline void set(FlagType bit)
|
||||
{
|
||||
#ifdef DEBUG_FLAGS
|
||||
DBG(FUNC, D("Flag %s") % name(bit));
|
||||
#endif
|
||||
_flags[bit]._value = true;
|
||||
}
|
||||
|
||||
inline void clear(FlagType bit)
|
||||
{
|
||||
#ifdef DEBUG_FLAGS
|
||||
DBG(FUNC, D("Flag %s") % name(bit));
|
||||
#endif
|
||||
_flags[bit]._value = false;
|
||||
}
|
||||
|
||||
inline void clearAll()
|
||||
{
|
||||
unsigned int index = 0;
|
||||
for(; (FlagType)index < INVALID_FLAG; index++)
|
||||
{
|
||||
if(_flags[index]._value)
|
||||
{
|
||||
DBG(FUNC, D("Flag %s was not clean!") % name((FlagType)index));
|
||||
clear((FlagType)index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
Flag _flags[INVALID_FLAG+1];
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
/************************* Commands and Events Handler ************************/
|
||||
struct CommandRequest
|
||||
{
|
||||
typedef enum
|
||||
{
|
||||
NONE = 0,
|
||||
COMMAND,
|
||||
ACTION
|
||||
}
|
||||
ReqType;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
CNONE = 0,
|
||||
|
||||
/* Commands */
|
||||
CMD_CALL,
|
||||
CMD_ANSWER,
|
||||
CMD_HANGUP,
|
||||
|
||||
/* Actions */
|
||||
FLUSH_REC_STREAM,
|
||||
FLUSH_REC_BRIDGE,
|
||||
START_RECORD,
|
||||
STOP_RECORD
|
||||
|
||||
}
|
||||
CodeType;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RFA_CLOSE,
|
||||
RFA_KEEP_OPEN,
|
||||
RFA_REMOVE
|
||||
}
|
||||
RecFlagType;
|
||||
|
||||
/* "empty" constructor */
|
||||
CommandRequest() :
|
||||
_type(NONE),
|
||||
_code(CNONE),
|
||||
_obj(-1)
|
||||
{}
|
||||
|
||||
CommandRequest(ReqType type, CodeType code, int obj) :
|
||||
_type(type),
|
||||
_code(code),
|
||||
_obj(obj)
|
||||
{}
|
||||
|
||||
CommandRequest(const CommandRequest & cmd) :
|
||||
_type(cmd._type),
|
||||
_code(cmd._code),
|
||||
_obj(cmd._obj)
|
||||
{}
|
||||
|
||||
~CommandRequest() {}
|
||||
|
||||
void operator=(const CommandRequest & cmd)
|
||||
{
|
||||
_type = cmd._type;
|
||||
_code = cmd._code;
|
||||
_obj = cmd._obj;
|
||||
}
|
||||
|
||||
void mirror(const CommandRequest & cmd_request)
|
||||
{
|
||||
_type = cmd_request._type;
|
||||
_code = cmd_request._code;
|
||||
_obj = cmd_request._obj;
|
||||
}
|
||||
|
||||
short type() { return _type; }
|
||||
|
||||
short code() { return _code; }
|
||||
|
||||
int obj() { return _obj; }
|
||||
|
||||
private:
|
||||
short _type;
|
||||
short _code;
|
||||
int _obj;
|
||||
};
|
||||
|
||||
struct EventRequest
|
||||
{
|
||||
/* "empty" constructor */
|
||||
EventRequest(bool can_delete = true) :
|
||||
_delete(can_delete),
|
||||
_obj(-1)
|
||||
{
|
||||
if(can_delete)
|
||||
_event = new K3L_EVENT();
|
||||
}
|
||||
|
||||
/* Temporary constructor */
|
||||
EventRequest(int obj, K3L_EVENT * ev) :
|
||||
_delete(false),
|
||||
_obj(obj),
|
||||
_event(ev)
|
||||
{}
|
||||
|
||||
//EventRequest(const EventRequest & ev) : _obj(ev._obj) {}
|
||||
|
||||
~EventRequest()
|
||||
{
|
||||
|
||||
if(!_delete || !_event)
|
||||
return;
|
||||
|
||||
if(_event->ParamSize)
|
||||
free(_event->Params);
|
||||
|
||||
delete _event;
|
||||
}
|
||||
|
||||
void operator=(const EventRequest & ev)
|
||||
{
|
||||
_delete = false;
|
||||
_obj = ev._obj;
|
||||
_event = ev._event;
|
||||
|
||||
}
|
||||
|
||||
void mirror(const EventRequest & ev_request)
|
||||
{
|
||||
//Checar o _event
|
||||
|
||||
if(_event->ParamSize)
|
||||
{
|
||||
free(_event->Params);
|
||||
}
|
||||
|
||||
_event->Params = NULL;
|
||||
|
||||
_obj = ev_request._obj;
|
||||
|
||||
K3L_EVENT * ev = ev_request._event;
|
||||
|
||||
if(!ev)
|
||||
{
|
||||
clearEvent();
|
||||
return;
|
||||
}
|
||||
|
||||
_event->Code = ev->Code; // API code
|
||||
_event->AddInfo = ev->AddInfo; // Parameter 1
|
||||
_event->DeviceId = ev->DeviceId; // Hardware information
|
||||
_event->ObjectInfo = ev->ObjectInfo; // Additional object information
|
||||
_event->ParamSize = ev->ParamSize; // Size of parameter buffer
|
||||
_event->ObjectId = ev->ObjectId; // KEventObjectId: Event thrower object id
|
||||
|
||||
if(ev->ParamSize)
|
||||
{
|
||||
// Pointer to the parameter buffer
|
||||
_event->Params = malloc(ev->ParamSize+1);
|
||||
memcpy(_event->Params, ev->Params, ev->ParamSize);
|
||||
((char *)(_event->Params))[ev->ParamSize] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool clearEvent()
|
||||
{
|
||||
_event->Code = -1;
|
||||
_event->AddInfo = -1;
|
||||
_event->DeviceId = -1;
|
||||
_event->ObjectInfo = -1;
|
||||
_event->ParamSize = 0;
|
||||
_event->ObjectId = -1;
|
||||
}
|
||||
|
||||
int obj() { return _obj; }
|
||||
|
||||
K3L_EVENT * event() { return _event; }
|
||||
|
||||
|
||||
private:
|
||||
bool _delete;
|
||||
int _obj;
|
||||
K3L_EVENT * _event;
|
||||
};
|
||||
|
||||
template < typename R, int S >
|
||||
struct GenericFifo
|
||||
{
|
||||
typedef R RequestType;
|
||||
typedef SimpleNonBlockLock<25,100> LockType;
|
||||
|
||||
GenericFifo(int device) :
|
||||
_device(device),
|
||||
_shutdown(false),
|
||||
_buffer(S),
|
||||
_mutex(Globals::module_pool),
|
||||
_cond(Globals::module_pool)
|
||||
{};
|
||||
|
||||
int _device;
|
||||
bool _shutdown;
|
||||
Ringbuffer < RequestType > _buffer;
|
||||
LockType _mutex; /* to sync write acess to event list */
|
||||
SavedCondition _cond;
|
||||
Thread *_thread;
|
||||
|
||||
};
|
||||
|
||||
typedef GenericFifo < CommandRequest, 250 > CommandFifo;
|
||||
typedef GenericFifo < EventRequest, 500 > EventFifo;
|
||||
|
||||
/* Used inside KhompPvt to represent an command handler */
|
||||
struct ChanCommandHandler: NEW_REFCOUNTER(ChanCommandHandler)
|
||||
{
|
||||
typedef int (HandlerType)(void *);
|
||||
|
||||
ChanCommandHandler(int device, HandlerType * handler)
|
||||
{
|
||||
_fifo = new CommandFifo(device);
|
||||
/* device event handler */
|
||||
_fifo->_thread = new Thread(handler, (void *)this, Globals::module_pool);
|
||||
if(_fifo->_thread->start())
|
||||
{
|
||||
DBG(FUNC,"Device command handler started");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR, "Device command handler error");
|
||||
}
|
||||
}
|
||||
|
||||
ChanCommandHandler(const ChanCommandHandler & cmd)
|
||||
: INC_REFCOUNTER(cmd, ChanCommandHandler),
|
||||
_fifo(cmd._fifo)
|
||||
{};
|
||||
|
||||
void unreference();
|
||||
|
||||
CommandFifo * fifo()
|
||||
{
|
||||
return _fifo;
|
||||
}
|
||||
|
||||
void signal()
|
||||
{
|
||||
_fifo->_cond.signal();
|
||||
};
|
||||
|
||||
bool writeNoSignal(const CommandRequest &);
|
||||
bool write(const CommandRequest &);
|
||||
|
||||
protected:
|
||||
CommandFifo * _fifo;
|
||||
};
|
||||
|
||||
/* Used inside KhompPvt to represent an event handler */
|
||||
struct ChanEventHandler: NEW_REFCOUNTER(ChanEventHandler)
|
||||
{
|
||||
typedef int (HandlerType)(void *);
|
||||
|
||||
ChanEventHandler(int device, HandlerType * handler)
|
||||
{
|
||||
_fifo = new EventFifo(device);
|
||||
/* device event handler */
|
||||
_fifo->_thread = new Thread(handler, (void *)this, Globals::module_pool);
|
||||
if(_fifo->_thread->start())
|
||||
{
|
||||
DBG(FUNC,"Device event handler started");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR, "Device event handler error");
|
||||
}
|
||||
}
|
||||
|
||||
ChanEventHandler(const ChanEventHandler & evt)
|
||||
: INC_REFCOUNTER(evt, ChanEventHandler),
|
||||
_fifo(evt._fifo)
|
||||
{};
|
||||
|
||||
void unreference();
|
||||
|
||||
EventFifo * fifo()
|
||||
{
|
||||
return _fifo;
|
||||
}
|
||||
|
||||
void signal()
|
||||
{
|
||||
_fifo->_cond.signal();
|
||||
};
|
||||
|
||||
bool provide(const EventRequest &);
|
||||
bool writeNoSignal(const EventRequest &);
|
||||
bool write(const EventRequest &);
|
||||
|
||||
protected:
|
||||
EventFifo * _fifo;
|
||||
};
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
/****************************** Internal **************************************/
|
||||
struct RingbackDefs
|
||||
{
|
||||
enum
|
||||
{
|
||||
RB_SEND_DEFAULT = -1,
|
||||
RB_SEND_NOTHING = -2,
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RBST_SUCCESS,
|
||||
RBST_UNSUPPORTED,
|
||||
RBST_FAILURE,
|
||||
}
|
||||
RingbackStType;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* Others ***************************************/
|
||||
static bool checkTrueString(const char * str)
|
||||
{
|
||||
if (str && *str)
|
||||
{
|
||||
if (!SAFE_strcasecmp(str, "yes") || !SAFE_strcasecmp(str, "true") ||
|
||||
!SAFE_strcasecmp(str, "enabled") || !SAFE_strcasecmp(str, "sim"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool checkFalseString(const char * str)
|
||||
{
|
||||
if (str && *str)
|
||||
{
|
||||
if (!SAFE_strcasecmp(str, "no") || !SAFE_strcasecmp(str, "false") ||
|
||||
!SAFE_strcasecmp(str, "disabled") || !SAFE_strcasecmp(str, "não"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static TriState getTriStateValue(const char * str)
|
||||
{
|
||||
if (str)
|
||||
{
|
||||
/**/ if (checkTrueString(str)) return T_TRUE;
|
||||
else if (checkFalseString(str)) return T_FALSE;
|
||||
else /***********************************/ return T_UNKNOWN;
|
||||
}
|
||||
|
||||
return T_UNKNOWN;
|
||||
}
|
||||
|
||||
const char * answerInfoToString(int answer_info);
|
||||
|
||||
static bool replaceTemplate(std::string & haystack, const std::string & needed, int value)
|
||||
{
|
||||
Regex::Expression e(std::string(needed).c_str());
|
||||
Regex::Match r(haystack,e);
|
||||
|
||||
if(!r.matched()) return false;
|
||||
|
||||
int len;
|
||||
std::string fmt;
|
||||
std::string tmp;
|
||||
|
||||
// if needed isn't "SSSS" the length of needed will give string format
|
||||
// if needed is "SSSS" the length of value will give string format
|
||||
std::string ssss("SSSS");
|
||||
|
||||
if (ssss != needed)
|
||||
{
|
||||
len = needed.size();
|
||||
fmt = STG(FMT("%%0%dd") % len);
|
||||
tmp = STG(FMT(fmt) % value);
|
||||
}
|
||||
else
|
||||
{
|
||||
len = (STG(FMT("%d") % value)).size();
|
||||
fmt = STG(FMT("%%%dd") % len);
|
||||
tmp = STG(FMT(fmt) % value);
|
||||
}
|
||||
|
||||
haystack = r.replace(STG(FMT(fmt) % value));
|
||||
|
||||
DBG(FUNC,FMT("haystack: %s") % haystack);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static std::string timeToString (time_t time_value)
|
||||
{
|
||||
int horas = (int)time_value / 3600;
|
||||
|
||||
if (horas > 0)
|
||||
time_value -= horas * 3600;
|
||||
|
||||
int minutos = (int)time_value / 60;
|
||||
|
||||
if (minutos > 0)
|
||||
time_value -= minutos * 60;
|
||||
|
||||
return STG(FMT("%02d:%02d:%02d") % horas % minutos % (int)time_value);
|
||||
}
|
||||
/******************************************************************************/
|
||||
/******************************* Match functions ******************************/
|
||||
typedef enum
|
||||
{
|
||||
MATCH_NONE,
|
||||
MATCH_EXACT,
|
||||
MATCH_MORE
|
||||
}
|
||||
MatchType;
|
||||
|
||||
static bool canMatch(std::string & context, std::string & exten, std::string & caller_id, bool match_more = false)
|
||||
{
|
||||
switch_xml_t xml = NULL;
|
||||
switch_xml_t xcontext = NULL;
|
||||
switch_regex_t *re;
|
||||
int ovector[30];
|
||||
|
||||
if (switch_xml_locate("dialplan","context","name",context.c_str(),&xml,&xcontext, NULL,SWITCH_FALSE) == SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
switch_xml_t xexten = NULL;
|
||||
|
||||
if(!(xexten = switch_xml_child(xcontext,"extension")))
|
||||
{
|
||||
DBG(FUNC,"extension cannot match, returning");
|
||||
|
||||
if(xml)
|
||||
switch_xml_free(xml);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
while(xexten)
|
||||
{
|
||||
switch_xml_t xcond = NULL;
|
||||
|
||||
for (xcond = switch_xml_child(xexten, "condition"); xcond; xcond = xcond->next)
|
||||
{
|
||||
std::string expression;
|
||||
|
||||
if (switch_xml_child(xcond, "condition"))
|
||||
{
|
||||
LOG(ERROR,"Nested conditions are not allowed");
|
||||
}
|
||||
|
||||
switch_xml_t xexpression = switch_xml_child(xcond, "expression");
|
||||
|
||||
if ((xexpression = switch_xml_child(xcond, "expression")))
|
||||
{
|
||||
expression = switch_str_nil(xexpression->txt);
|
||||
}
|
||||
else
|
||||
{
|
||||
expression = switch_xml_attr_soft(xcond, "expression");
|
||||
}
|
||||
|
||||
if(expression.empty() || expression == "^(.*)$")
|
||||
{
|
||||
/**
|
||||
* We're not gonna take it
|
||||
* No, we ain't gonna take it
|
||||
* We're not gonna take it anymore
|
||||
**/
|
||||
continue;
|
||||
}
|
||||
|
||||
int pm = -1;
|
||||
switch_status_t is_match = SWITCH_STATUS_FALSE;
|
||||
is_match = switch_regex_match_partial(exten.c_str(),expression.c_str(),&pm);
|
||||
|
||||
if(is_match == SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
if(match_more)
|
||||
{
|
||||
if(pm == 1)
|
||||
{
|
||||
switch_xml_free(xml);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch_xml_free(xml);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// not match
|
||||
}
|
||||
}
|
||||
|
||||
xexten = xexten->next;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(FUNC,"context cannot match, returning");
|
||||
}
|
||||
|
||||
if(xml)
|
||||
switch_xml_free(xml);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/************************** Thread helper functions ***************************/
|
||||
typedef int (HandlerType)(void *);
|
||||
static Thread * threadCreate(HandlerType * handler,void * arg)
|
||||
{
|
||||
Thread *t = new Thread(handler, (void *) arg, Globals::module_pool);
|
||||
return t;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Kommuter ************************************/
|
||||
struct Kommuter
|
||||
{
|
||||
Kommuter() :
|
||||
_kommuter_count(-1),
|
||||
_kwtd_timer_on(false)
|
||||
{}
|
||||
|
||||
bool start()
|
||||
{
|
||||
/* Checks for kommuter */
|
||||
try
|
||||
{
|
||||
Globals::k3lapi.command(-1,-1,CM_WATCHDOG_COUNT);
|
||||
}
|
||||
catch(K3LAPI::failed_command & e)
|
||||
{
|
||||
LOG(WARNING , "libkwd.so used by Kommuter devices is not available.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool stop();
|
||||
|
||||
bool initialize(K3L_EVENT *e);
|
||||
|
||||
/* Index of the WatchDog timer */
|
||||
TimerTraits::Index _kwtd_timer_index;
|
||||
int _kommuter_count;
|
||||
bool _kwtd_timer_on;
|
||||
|
||||
/* Timer to control our dog (totó) */
|
||||
static void wtdKickTimer(void *);
|
||||
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* Statistics ***********************************/
|
||||
struct Statistics
|
||||
{
|
||||
typedef enum
|
||||
{
|
||||
DETAILED = 0,
|
||||
ROW
|
||||
}
|
||||
Type;
|
||||
|
||||
virtual std::string getDetailed() { return ""; }
|
||||
virtual void clear() {}
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
#endif /* _UTILS_H_ */
|
||||
93
src/mod/endpoints/mod_khomp/install.sh
Executable file
93
src/mod/endpoints/mod_khomp/install.sh
Executable file
@@ -0,0 +1,93 @@
|
||||
#!/bin/bash
|
||||
|
||||
###############################################################################
|
||||
# KHOMP generic endpoint/channel library.
|
||||
# Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
# The contents of this file are subject to the Mozilla Public License
|
||||
# Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing rights and limitations under the License.
|
||||
|
||||
# Alternatively, the contents of this file may be used under the terms of the
|
||||
# "GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
# case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
# If you wish to allow use of your version of this file only under the terms of
|
||||
# the LGPL License and not to allow others to use your version of this file
|
||||
# under the MPL, indicate your decision by deleting the provisions above and
|
||||
# replace them with the notice and other provisions required by the LGPL
|
||||
# License. If you do not delete the provisions above, a recipient may use your
|
||||
# version of this file under either the MPL or the LGPL License.
|
||||
|
||||
# The LGPL header follows below:
|
||||
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this library; if not, write to the Free Software Foundation,
|
||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
###############################################################################
|
||||
|
||||
|
||||
clean()
|
||||
{
|
||||
printf "$1"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [ `whoami` != 'root' ]
|
||||
then
|
||||
clean "Need to be root\n"
|
||||
fi
|
||||
|
||||
if [ "w$FREESWITCH_PATH" == "w" ]
|
||||
then
|
||||
clean "FreeSWITCH source PATH is needed: \n\tSet \"FREESWITCH_PATH=\"\n"
|
||||
fi
|
||||
|
||||
if ! which 'kserver' &> /dev/null
|
||||
then
|
||||
echo "k3l will be installed"
|
||||
K3L_FILE="k3l_2.1_client.sh"
|
||||
|
||||
if [ "w`uname -m | grep x86_64`" == "w" ]
|
||||
then
|
||||
echo "Downloading i686 package"
|
||||
wget -t15 -c --progress=bar:force -O $K3L_FILE.gz http://www.khomp.com.br/binaries/softpbx/freeswitch/k3l_2.1_client_i686.sh.gz
|
||||
else
|
||||
echo "Downloading x86_64 package"
|
||||
wget -t15 -c --progress=bar:force -O $K3L_FILE.gz http://www.khomp.com.br/binaries/softpbx/freeswitch/k3l_2.1_client_x86-64.sh.gz
|
||||
fi
|
||||
|
||||
gunzip $K3L_FILE.gz
|
||||
chmod 0755 $K3L_FILE
|
||||
|
||||
(./$K3L_FILE) || clean "Error on k3l install\n"
|
||||
|
||||
rm $K3L_FILE
|
||||
fi
|
||||
|
||||
if [ "w`kserver --version | grep 2.1`" == "w" ]
|
||||
then
|
||||
clean "k3l version 2.1 must be installed: \n\tUninstall the old version of k3l and try again\n"
|
||||
fi
|
||||
|
||||
make || clean "Error on compilation\n"
|
||||
|
||||
make install || clean "Error on installation\n"
|
||||
|
||||
echo "Successfully installed!"
|
||||
|
||||
1509
src/mod/endpoints/mod_khomp/mod_khomp.cpp
Normal file
1509
src/mod/endpoints/mod_khomp/mod_khomp.cpp
Normal file
File diff suppressed because it is too large
Load Diff
968
src/mod/endpoints/mod_khomp/src/applications.cpp
Normal file
968
src/mod/endpoints/mod_khomp/src/applications.cpp
Normal file
@@ -0,0 +1,968 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "applications.h"
|
||||
#include "lock.h"
|
||||
|
||||
bool Fax::adjustForFax()
|
||||
{
|
||||
/* Don't worry, let the man work */
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(),"Channel is now being adjusted for fax!"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
_pvt->echoCancellation(false);
|
||||
_pvt->dtmfSuppression(false);
|
||||
_pvt->autoGainControl(false);
|
||||
|
||||
_pvt->call()->_input_volume = 0;
|
||||
_pvt->setVolume("input" , 0);
|
||||
_pvt->call()->_output_volume = 0;
|
||||
_pvt->setVolume("output", 0);
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (unable to lock: %s!)") % err._msg.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fax::sendFax(switch_core_session_t * session, const char *data)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c (%s)") % data);
|
||||
|
||||
try
|
||||
{
|
||||
switch_channel_t *channel = _pvt->getFSChannel(session);
|
||||
|
||||
switch_channel_set_variable(channel, "KFaxSent", "no");
|
||||
switch_channel_set_variable(channel, "KFaxResult", "none");
|
||||
|
||||
std::string fax_string((const char *)data);
|
||||
Strings::vector_type fax_args;
|
||||
Strings::vector_type fax_files;
|
||||
|
||||
Strings::tokenize(fax_string, fax_args, "|,");
|
||||
|
||||
if(fax_args.size() != 1 && fax_args.size() != 2)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (invalid string '%s': wrong number of separators.)") % fax_string);
|
||||
return false;
|
||||
}
|
||||
|
||||
Strings::tokenize(fax_args[0], fax_files, ":");
|
||||
|
||||
if(fax_files.size() <= 0)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (invalid string '%s': wrong number of separators.)") % fax_string);
|
||||
return false;
|
||||
}
|
||||
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
std::string id;
|
||||
|
||||
if(fax_args.size() == 2)
|
||||
{
|
||||
id = fax_args[1];
|
||||
}
|
||||
else if(!_pvt->call()->_orig_addr.empty())
|
||||
{
|
||||
id = _pvt->call()->_orig_addr;
|
||||
}
|
||||
|
||||
const char * orig_addr = id.empty() ? NULL : id.c_str();
|
||||
|
||||
int timeout = 5;
|
||||
|
||||
if(!_pvt->call()->_flags.check(Kflags::REALLY_CONNECTED) && !_pvt->loopWhileFlagTimed(Kflags::REALLY_CONNECTED, timeout, false))
|
||||
return false;
|
||||
|
||||
if(!startFaxTX(orig_addr))
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (unable to start send fax)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < fax_files.size() ; ++i)
|
||||
{
|
||||
if (!addFaxFile(fax_files[i].c_str(), (i == fax_files.size()-1)? true : false))
|
||||
{
|
||||
DBG(FUNC, D("unable to add fax file='%s'")
|
||||
% fax_files[i].c_str());
|
||||
}
|
||||
}
|
||||
|
||||
_fax_cond.reset();
|
||||
|
||||
lock.unlock();
|
||||
|
||||
_fax_cond.wait();
|
||||
|
||||
switch_channel_set_variable(channel, "KFaxSent", ((_fax_result == kfaxrEndOfTransmission) ? "yes" : "no"));
|
||||
switch_channel_set_variable(channel, "KFaxResult", (Verbose::faxResult((KFaxResult)_fax_result).c_str()));
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (unable to lock: %s!)") % err._msg.c_str());
|
||||
return false;
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (%s)") % err._msg.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Fax::receiveFax(switch_core_session_t * session, const char *data)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c (%s)") % data);
|
||||
|
||||
try
|
||||
{
|
||||
switch_channel_t *channel = _pvt->getFSChannel(session);
|
||||
|
||||
switch_channel_set_variable(channel, "KFaxReceived", "no");
|
||||
switch_channel_set_variable(channel, "KFaxResult", "none");
|
||||
|
||||
std::string fax_string((const char *)data);
|
||||
Strings::vector_type fax_args;
|
||||
|
||||
Strings::tokenize(fax_string, fax_args, "|,");
|
||||
|
||||
if(fax_args.size() != 1 && fax_args.size() != 2)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (invalid string '%s': wrong number of separators.)") % fax_string);
|
||||
return false;
|
||||
}
|
||||
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
std::string id;
|
||||
|
||||
if(fax_args.size() == 2)
|
||||
{
|
||||
id = fax_args[1];
|
||||
}
|
||||
else if(!_pvt->call()->_dest_addr.empty())
|
||||
{
|
||||
id = _pvt->call()->_dest_addr;
|
||||
}
|
||||
|
||||
const char * orig_addr = id.empty() ? NULL : id.c_str();
|
||||
|
||||
int timeout = 5;
|
||||
|
||||
if(!_pvt->call()->_flags.check(Kflags::REALLY_CONNECTED) && !_pvt->loopWhileFlagTimed(Kflags::REALLY_CONNECTED, timeout, false))
|
||||
return false;
|
||||
|
||||
if(!startFaxRX(fax_args[0].c_str(), orig_addr))
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (unable to start receive fax)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
_fax_cond.reset();
|
||||
|
||||
lock.unlock();
|
||||
|
||||
_fax_cond.wait();
|
||||
|
||||
switch_channel_set_variable(channel, "KFaxReceived", ((_fax_result == kfaxrEndOfReception) ? "yes" : "no"));
|
||||
switch_channel_set_variable(channel, "KFaxResult", (Verbose::faxResult((KFaxResult)_fax_result).c_str()));
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (unable to lock: %s!)") % err._msg.c_str());
|
||||
return false;
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (%s)") % err._msg.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fax::onFaxChannelRelease(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
if (_pvt->call()->_flags.check(Kflags::FAX_SENDING))
|
||||
{
|
||||
_pvt->call()->_flags.clear(Kflags::FAX_SENDING);
|
||||
|
||||
/* make audio come back.. */
|
||||
_pvt->startStream(false);
|
||||
_pvt->startListen();
|
||||
}
|
||||
else if (_pvt->call()->_flags.check(Kflags::FAX_RECEIVING))
|
||||
{
|
||||
_pvt->call()->_flags.clear(Kflags::FAX_RECEIVING);
|
||||
|
||||
/* make audio come back.. */
|
||||
_pvt->startStream(false);
|
||||
_pvt->startListen();
|
||||
}
|
||||
|
||||
_fax_result = (KFaxResult)e->AddInfo;
|
||||
|
||||
_fax_cond.signal();
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (unable to lock %s!)") % err._msg.c_str());
|
||||
return false;
|
||||
};
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fax::startFaxTX(const char * orig_addr)
|
||||
{
|
||||
/* no audio after this point, so stop streams! */
|
||||
_pvt->stopStream(false);
|
||||
_pvt->stopListen();
|
||||
|
||||
std::string id;
|
||||
|
||||
if(orig_addr)
|
||||
id = STG(FMT("orig_addr=\"%s\"") % orig_addr);
|
||||
|
||||
const char * param = id.empty() ? NULL : id.c_str();
|
||||
|
||||
|
||||
if (!_pvt->command(KHOMP_LOG, CM_START_FAX_TX, param))
|
||||
{
|
||||
_pvt->startStream(false);
|
||||
_pvt->startListen();
|
||||
return false;
|
||||
}
|
||||
|
||||
_pvt->call()->_flags.set(Kflags::FAX_SENDING);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool Fax::stopFaxTX(void)
|
||||
{
|
||||
/* EV_FAX_CHANNEL_FREE , EV_FAX_FILE_FAIL */
|
||||
if (!_pvt->command(KHOMP_LOG, CM_STOP_FAX_TX))
|
||||
return false;
|
||||
|
||||
_pvt->call()->_flags.clear(Kflags::FAX_SENDING);
|
||||
|
||||
/* make audio come back.. */
|
||||
_pvt->startStream(false);
|
||||
_pvt->startListen();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Fax::startFaxRX(const char * filename, const char * orig_addr)
|
||||
{
|
||||
if(!filename)
|
||||
return false;
|
||||
|
||||
std::string param = STG(FMT("filename=\"%s\"") % filename);
|
||||
|
||||
if(orig_addr)
|
||||
param += STG(FMT(" orig_addr=\"%s\"") % orig_addr);
|
||||
|
||||
/* no audio after this point, so stop streams! */
|
||||
_pvt->stopStream(false);
|
||||
_pvt->stopListen();
|
||||
|
||||
if (!_pvt->command(KHOMP_LOG, CM_START_FAX_RX, param.c_str()))
|
||||
{
|
||||
_pvt->startStream(false);
|
||||
_pvt->startListen();
|
||||
return false;
|
||||
}
|
||||
|
||||
_pvt->call()->_flags.set(Kflags::FAX_RECEIVING);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fax::stopFaxRX()
|
||||
{
|
||||
/* Events: EV_FAX_CHANNEL_FREE */
|
||||
if (!_pvt->command(KHOMP_LOG, CM_STOP_FAX_RX))
|
||||
return false;
|
||||
|
||||
_pvt->call()->_flags.clear(Kflags::FAX_RECEIVING);
|
||||
|
||||
/* make audio come back.. */
|
||||
_pvt->startStream(false);
|
||||
_pvt->startListen();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Fax::addFaxFile(const char * filename, bool last)
|
||||
{
|
||||
/* Events: EV_FAX_FILE_SENT , EV_FAX_FILE_FAIL , EV_FAX_TX_TIMEOUT */
|
||||
if(!filename)
|
||||
return false;
|
||||
|
||||
std::string params = STG(FMT("filename=\"%s\" last=\"%s\"") % filename
|
||||
% (last ? "true": "false"));
|
||||
|
||||
if (!_pvt->command(KHOMP_LOG, CM_ADD_FAX_FILE, params.c_str()))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
bool SMS::justAlloc(unsigned int count)
|
||||
{
|
||||
/* incoming contexts */
|
||||
Board::KhompPvt::ContextListType contexts;
|
||||
|
||||
contexts.push_back(Opt::_context_gsm_sms);
|
||||
|
||||
/* temporary variables */
|
||||
std::string context;
|
||||
std::string exten;
|
||||
|
||||
K3L_DEVICE_CONFIG & dev_cfg = Globals::k3lapi.device_config(_pvt->_target.device);
|
||||
|
||||
for (Board::KhompPvt::ContextListType::iterator i = contexts.begin(); i != contexts.end(); i++)
|
||||
{
|
||||
replaceTemplate((*i), "DD", _pvt->_target.device);
|
||||
replaceTemplate((*i), "CC", _pvt->_target.object);
|
||||
replaceTemplate((*i), "SSSS", atoi(dev_cfg.SerialNumber));
|
||||
}
|
||||
|
||||
switch(_pvt->findExtension(exten, context, contexts, _got_sms._type, _got_sms._from, false, true))
|
||||
{
|
||||
case MATCH_NONE:
|
||||
if( _got_sms._type != "broadcast")
|
||||
{
|
||||
LOG(WARNING, PVT_FMT(_pvt->target(), "unable to find context/exten for incoming SMS (s/%s), processing disabled for this channel.")
|
||||
% (contexts.size() >= 1 ? contexts[0] : "default"));
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
default:
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "our: context '%s', exten '%s'") % context % exten);
|
||||
break;
|
||||
}
|
||||
|
||||
for(unsigned int c = 0; c < count; c++)
|
||||
{
|
||||
|
||||
#if SWITCH_LESS_THAN(1,0,6)
|
||||
switch_core_session_t *session = switch_core_session_request(Globals::khomp_sms_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, NULL);
|
||||
#else
|
||||
switch_core_session_t *session = switch_core_session_request(Globals::khomp_sms_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL);
|
||||
#endif
|
||||
|
||||
if(!session)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "Initilization Error, session not created!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
switch_core_session_add_stream(session, NULL);
|
||||
|
||||
switch_core_session_set_private(session, _pvt);
|
||||
|
||||
switch_caller_profile_t *caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session),
|
||||
"Khomp_SMS", //username
|
||||
"XML", //dialplan
|
||||
NULL, //caller_id_name
|
||||
_got_sms._from.c_str(), //caller_id_number
|
||||
NULL, //network_addr
|
||||
_got_sms._from.c_str(), //ani
|
||||
NULL, //aniii
|
||||
NULL, //rdnis
|
||||
(char *) "mod_khomp", //source
|
||||
context.c_str(), //context
|
||||
exten.c_str()); //destination_number
|
||||
|
||||
if(!caller_profile)
|
||||
{
|
||||
_pvt->destroy(session);
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (Cannot create caller profile)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
|
||||
if(!channel)
|
||||
{
|
||||
_pvt->destroy(session);
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (Cannot get channel)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string name = STG(FMT("Khomp_SMS/%hu/%hu")
|
||||
% _pvt->target().device
|
||||
% _pvt->target().object);
|
||||
|
||||
switch_channel_set_name(channel, name.c_str());
|
||||
switch_channel_set_caller_profile(channel, caller_profile);
|
||||
|
||||
//DBG(FUNC, PVT_FMT(_pvt->target(), "Connect inbound SMS channel %s") % name.c_str());
|
||||
|
||||
switch_channel_set_state(channel, CS_INIT);
|
||||
|
||||
|
||||
_owners.push_front(session);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SMS::justStart()
|
||||
{
|
||||
OwnersList::iterator i = _owners.begin();
|
||||
|
||||
if(i == _owners.end())
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "Cannot get session"));
|
||||
return false;
|
||||
}
|
||||
|
||||
switch_core_session_t *session = (*i);
|
||||
|
||||
_owners.pop_front();
|
||||
|
||||
if(!session)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "Cannot get session"));
|
||||
return false;
|
||||
}
|
||||
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
|
||||
if(!channel)
|
||||
{
|
||||
_pvt->destroy(session);
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "r (Cannot get channel)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
switch_channel_set_variable(channel, "KSmsType", _got_sms._type.c_str());
|
||||
|
||||
if (_got_sms._type == "message" || _got_sms._type == "confirm")
|
||||
{
|
||||
switch_channel_set_variable(channel, "KSmsFrom", _got_sms._from.c_str());
|
||||
switch_channel_set_variable(channel, "KSmsDate", _got_sms._date.c_str());
|
||||
}
|
||||
|
||||
if(_got_sms._type == "confirm")
|
||||
{
|
||||
switch_channel_set_variable(channel, "KSmsDelivery", _got_sms._sc_date.c_str());
|
||||
switch_channel_set_variable(channel, "KSmsStatus", _got_sms._status.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_got_sms._type == "broadcast")
|
||||
{
|
||||
switch_channel_set_variable(channel, "KSmsSerial", _got_sms._serial.c_str());
|
||||
switch_channel_set_variable(channel, "KSmsPage", _got_sms._page.c_str());
|
||||
switch_channel_set_variable(channel, "KSmsPages", _got_sms._pages.c_str());
|
||||
}
|
||||
switch_channel_set_variable(channel, "KSmsSize", _got_sms._size.c_str());
|
||||
switch_channel_set_variable(channel, "KSmsMode", _got_sms._coding.c_str());
|
||||
switch_channel_set_variable(channel, "KSmsBody", _got_sms._body.c_str());
|
||||
}
|
||||
|
||||
|
||||
if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
_pvt->destroy(session);
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "Error spawning thread"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SMS::sendSMS(switch_core_session_t * session, const char *data)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) c"));
|
||||
|
||||
volatile bool finished = false;
|
||||
volatile KGsmCallCause result = kgccNone;
|
||||
|
||||
try
|
||||
{
|
||||
//ScopedAllocLock alloc_lock;
|
||||
|
||||
if(!_can_send)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (cannot send SMS messages, modem NOT initialized!)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string sms_string((const char *)data);
|
||||
Strings::vector_type sms_args;
|
||||
|
||||
Strings::tokenize(sms_string, sms_args, "/|,", 3);
|
||||
|
||||
if (sms_args.size() != 3)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (invalid dial string '%s': wrong number of separators.)") % sms_string);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string dest(sms_args[1]);
|
||||
|
||||
bool conf = false;
|
||||
|
||||
if (dest[0] == '!')
|
||||
{
|
||||
dest.erase(0,1);
|
||||
conf = true;
|
||||
}
|
||||
|
||||
if (dest[dest.size()-1] == '!')
|
||||
{
|
||||
dest.erase(dest.size()-1,1);
|
||||
conf = true;
|
||||
}
|
||||
|
||||
// get options/values
|
||||
|
||||
_send_sms._dest = dest;
|
||||
_send_sms._conf = conf;
|
||||
_send_sms._body = sms_args[2];
|
||||
|
||||
Request request_sms(_send_sms, &finished, &result);
|
||||
|
||||
_mutex.lock();
|
||||
bool status = _buffer.provide(request_sms);
|
||||
_mutex.unlock();
|
||||
|
||||
_pvt->_mutex.unlock();
|
||||
|
||||
if(status)
|
||||
_cond.signal();
|
||||
|
||||
while (!finished)
|
||||
{
|
||||
usleep(200000);
|
||||
}
|
||||
|
||||
_pvt->_mutex.lock();
|
||||
|
||||
switch_channel_t *channel = _pvt->getFSChannel(session);
|
||||
|
||||
switch_channel_set_variable(channel, "KSmsDelivered", ((KGsmSmsCause)result == ((KGsmSmsCause)0) ? "yes" : "no"));
|
||||
switch_channel_set_variable(channel, "KSmsErrorCode", STG(FMT("%d") % result).c_str());
|
||||
switch_channel_set_variable(channel, "KSmsErrorName", ((KGsmSmsCause)result == ((KGsmSmsCause)0) ? "None" : Verbose::gsmSmsCause((KGsmSmsCause)result).c_str()));
|
||||
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (unable to global alloc lock)"));
|
||||
return false;
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) %s") % err._msg.c_str());
|
||||
//return false;
|
||||
}
|
||||
|
||||
bool ret = ((KGsmSmsCause)result == ((KGsmSmsCause)0));
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) r (%s)") % (ret ? "true" : "false"));
|
||||
|
||||
//return (KGsmSmsCause)result;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool SMS::onNewSMS(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
if(!_can_receive)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) received new SMS message(s), but receiving is disabled. keeping the message(s) at the SIM card."));
|
||||
ret = false;
|
||||
}
|
||||
else if(!justAlloc(e->AddInfo))
|
||||
{
|
||||
LOG(WARNING, PVT_FMT(_pvt->target(), "(SMS) unable to allocate channel for new SMS message(s). disabling processing to prevent messages from being lost."));
|
||||
_can_receive = false;
|
||||
ret = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "downloading %d SMS message(s) on the SIM card.") % e->AddInfo);
|
||||
|
||||
_pvt->command(KHOMP_LOG, CM_GET_SMS);
|
||||
}
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool SMS::onSMSInfo(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
Globals::k3lapi.get_param(e, "sms_type", _got_sms._type);
|
||||
Globals::k3lapi.get_param(e, "sms_from", _got_sms._from);
|
||||
Globals::k3lapi.get_param(e, "sms_date", _got_sms._date);
|
||||
Globals::k3lapi.get_param(e, "sms_size", _got_sms._size);
|
||||
Globals::k3lapi.get_param(e, "sms_coding", _got_sms._coding);
|
||||
Globals::k3lapi.get_param(e, "sms_serial", _got_sms._serial);
|
||||
Globals::k3lapi.get_param(e, "sms_id", _got_sms._id);
|
||||
Globals::k3lapi.get_param(e, "sms_page", _got_sms._page);
|
||||
Globals::k3lapi.get_param(e, "sms_pages", _got_sms._pages);
|
||||
Globals::k3lapi.get_param(e, "sms_sc_date", _got_sms._sc_date);
|
||||
Globals::k3lapi.get_param(e, "sms_status", _got_sms._status);
|
||||
|
||||
//DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) type=%s from=%s date=%s size=%s coding=%s serial=%s id=%s page=%s pages=%s sc_date=%s status=%s") % _got_sms._type % _got_sms._from % _got_sms._date % _got_sms._size % _got_sms._coding % _got_sms._serial % _got_sms._id % _got_sms._page % _got_sms._pages % _got_sms._sc_date % _got_sms._status);
|
||||
|
||||
if (_owners.empty() && !justAlloc(1) && (_got_sms._type.compare("broadcast") != 0))
|
||||
{
|
||||
// this error is fatal
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "unable to allocate channel, new SMS message from %s will not be sent to dialplan!") % _got_sms._from);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SMS::onSMSData(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
if (_owners.empty())
|
||||
{
|
||||
if(_got_sms._type != "broadcast")
|
||||
{
|
||||
LOG(WARNING, PVT_FMT(_pvt->target(), "unable to allocate channel for new SMS message(s). disabling processing to prevent messages from being lost."));
|
||||
_can_receive = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_got_sms._body = (const char *)(e->Params ? e->Params : "");
|
||||
|
||||
if(!justStart())
|
||||
{
|
||||
if(_got_sms._type != "broadcast")
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "unable to receive SMS from '%s', something wrong!") % _got_sms._from);
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "disabling SMS processing to prevent messages from being lost."));
|
||||
|
||||
_can_receive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(_can_receive)
|
||||
{
|
||||
/* stats update! */
|
||||
if(_got_sms._type == "message")
|
||||
{
|
||||
statistics<SMSStatistics>()->incrementIncoming();
|
||||
}
|
||||
else if(_got_sms._type == "confirm")
|
||||
{
|
||||
statistics<SMSStatistics>()->incrementConfirm();
|
||||
}
|
||||
else
|
||||
{
|
||||
statistics<SMSStatistics>()->incrementBroadcast();
|
||||
}
|
||||
}
|
||||
|
||||
/* reset data stuff */
|
||||
_got_sms.clear();
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SMS::onSMSSendResult(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(_pvt);
|
||||
|
||||
_result = (KGsmCallCause)e->AddInfo;
|
||||
_pvt->call()->_flags.clear(Kflags::SMS_DOING_UPLOAD);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_pvt->target(), "(SMS) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_pvt->target(), "(SMS) r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int SMS::smsThread(void * sms_ptr)
|
||||
{
|
||||
SMS * sms = static_cast < SMS * > (sms_ptr);
|
||||
|
||||
Board::KhompPvt * pvt = static_cast < Board::KhompPvt * > (sms->_pvt);
|
||||
|
||||
DBG(THRD, PVT_FMT(pvt->target(), "c"));
|
||||
|
||||
bool loop = true;
|
||||
|
||||
while(loop)
|
||||
{
|
||||
if(sms->_cond.wait(1000))
|
||||
{
|
||||
if(sms->_shutdown)
|
||||
return 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(pvt);
|
||||
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
KLibraryStatus ret = (KLibraryStatus) k3lGetDeviceStatus(
|
||||
pvt->_target.device,
|
||||
pvt->_target.object + ksoChannel,
|
||||
&status,
|
||||
sizeof(status));
|
||||
|
||||
if (ret == ksSuccess)
|
||||
{
|
||||
switch (status.AddInfo)
|
||||
{
|
||||
case kgsmModemError:
|
||||
case kgsmSIMCardError:
|
||||
return 0;
|
||||
|
||||
case kgsmIdle:
|
||||
{
|
||||
/* pede registro do canal.. */
|
||||
pvt->command(KHOMP_LOG, CM_SEND_TO_MODEM, "AT+COPS?");
|
||||
|
||||
/* pede estado da antena.. */
|
||||
pvt->command(KHOMP_LOG, CM_SEND_TO_MODEM, "AT+CSQ?");
|
||||
|
||||
/* forca tipo padrao a ser message... */
|
||||
sms->_got_sms._type = "message";
|
||||
|
||||
/* pre-aloca canal para receber mensagens SMS. */
|
||||
if (sms->justAlloc())
|
||||
{
|
||||
/* habilita processamento de SMSs entrantes */
|
||||
sms->_can_receive = true;
|
||||
|
||||
/* envia comando para "limpar" SMSs do SIM card */
|
||||
pvt->command(KHOMP_LOG, CM_CHECK_NEW_SMS);
|
||||
}
|
||||
|
||||
sms->_got_sms.clear();
|
||||
|
||||
/* sai fora do loop! */
|
||||
loop = false;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(pvt->_target, "unable to obtain lock: %s") % err._msg.c_str());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sms->_can_send = true;
|
||||
|
||||
while(true)
|
||||
{
|
||||
Request request_sms;
|
||||
|
||||
DBG(THRD, PVT_FMT(pvt->_target, "begin"));
|
||||
|
||||
while(!sms->_buffer.consume(request_sms))
|
||||
{
|
||||
DBG(THRD, PVT_FMT(pvt->_target, "buffer empty"));
|
||||
|
||||
sms->_cond.wait();
|
||||
|
||||
DBG(THRD, PVT_FMT(pvt->_target, "waked up!"));
|
||||
|
||||
if(sms->_shutdown)
|
||||
return 0;
|
||||
}
|
||||
|
||||
DBG(THRD, PVT_FMT(pvt->_target, "processing buffer..."));
|
||||
|
||||
try
|
||||
{
|
||||
int timeout = 30;
|
||||
|
||||
ScopedPvtLock lock(pvt);
|
||||
|
||||
pvt->call()->_flags.set(Kflags::SMS_DOING_UPLOAD);
|
||||
|
||||
if (pvt->command(KHOMP_LOG, CM_PREPARE_SMS, request_sms._send_sms._body.c_str()))
|
||||
{
|
||||
std::string extra = (request_sms._send_sms._conf ? " sms_confirm=\"TRUE\"" : "");
|
||||
|
||||
if (pvt->command(KHOMP_LOG, CM_SEND_SMS,
|
||||
STG(FMT("sms_to=\"%s\"%s") % request_sms._send_sms._dest % extra).c_str()))
|
||||
{
|
||||
if (!pvt->loopWhileFlagTimed(Kflags::SMS_DOING_UPLOAD, timeout))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
sms->_result = kgccResourceUnavailable;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sms->_result = kgccInvalidMessage;
|
||||
}
|
||||
|
||||
pvt->call()->_flags.clear(Kflags::SMS_DOING_UPLOAD);
|
||||
|
||||
if (request_sms._cause)
|
||||
*(request_sms._cause) = (KGsmCallCause)sms->_result;
|
||||
|
||||
if (request_sms._finished)
|
||||
*(request_sms._finished) = true;
|
||||
|
||||
if (sms->_result == kgccNone)
|
||||
{
|
||||
/* stats update if sent! */
|
||||
sms->statistics<SMSStatistics>()->incrementOutgoing();
|
||||
}
|
||||
|
||||
sms->_send_sms.clear();
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(pvt->_target, "unable to obtain lock: %s") % err._msg.c_str());
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "ok, going to loop..."));
|
||||
|
||||
}
|
||||
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "r"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
2408
src/mod/endpoints/mod_khomp/src/cli.cpp
Normal file
2408
src/mod/endpoints/mod_khomp/src/cli.cpp
Normal file
File diff suppressed because it is too large
Load Diff
124
src/mod/endpoints/mod_khomp/src/frame.cpp
Normal file
124
src/mod/endpoints/mod_khomp/src/frame.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "frame.h"
|
||||
|
||||
#define ALLOC(T,s) ((T*)calloc(1,s))
|
||||
|
||||
/* Internal frame manager structure. */
|
||||
FrameStorage::FrameStorage(switch_codec_t * codec, int packet_size)
|
||||
: _frames(ALLOC(switch_frame_t, frame_count * sizeof(switch_frame_t))),
|
||||
_buffer(ALLOC( char, audio_count * packet_size)),
|
||||
_index(0)
|
||||
{
|
||||
for (unsigned int i = 0; i < frame_count; i++)
|
||||
{
|
||||
_frames[i].codec = codec;
|
||||
_frames[i].source = "mod_khomp";
|
||||
|
||||
_frames[i].packet = 0;
|
||||
_frames[i].packetlen = 0;
|
||||
_frames[i].extra_data = 0;
|
||||
|
||||
_frames[i].data = (char *)0;
|
||||
_frames[i].datalen = packet_size;
|
||||
_frames[i].buflen = packet_size;
|
||||
|
||||
_frames[i].samples = packet_size; // packet_duration * 8
|
||||
_frames[i].rate = 8000;
|
||||
_frames[i].payload = 0;
|
||||
|
||||
_frames[i].timestamp = 0u;
|
||||
|
||||
_frames[i].seq = 0u;
|
||||
_frames[i].ssrc = 0u;
|
||||
_frames[i].m = SWITCH_FALSE;
|
||||
_frames[i].flags = SFF_NONE;
|
||||
}
|
||||
|
||||
_cng_frame.codec = codec;
|
||||
_cng_frame.source = "mod_khomp";
|
||||
_cng_frame.packet = 0;
|
||||
_cng_frame.packetlen = 0;
|
||||
_cng_frame.extra_data = 0;
|
||||
|
||||
_cng_frame.data = (void*)"A";
|
||||
_cng_frame.datalen = 2;
|
||||
_cng_frame.buflen = 2;
|
||||
|
||||
_cng_frame.samples = packet_size;
|
||||
_cng_frame.rate = 8000;
|
||||
_cng_frame.payload = 0;
|
||||
|
||||
_cng_frame.timestamp = 0u;
|
||||
|
||||
_cng_frame.seq = 0u;
|
||||
_cng_frame.ssrc = 0u;
|
||||
_cng_frame.m = SWITCH_FALSE;
|
||||
_cng_frame.flags = SFF_CNG;
|
||||
|
||||
// if (mlock(&_frames, frames_size) < 0)
|
||||
// {
|
||||
// DBG(CONF, F("Unable to lock ast_frame buffer memory in RAM: %s") % strerror(errno));
|
||||
// std::cerr << "chan_khomp: Unable to lock ast_frame buffer memory in RAM: "
|
||||
// << strerror(errno) << ". "
|
||||
// << "This is not a catastrophic failure, but may cause unpredictable "
|
||||
// << "audio delay under extreme load conditions." << std::endl;
|
||||
// }
|
||||
//
|
||||
// if (mlock(&_buffer, buffer_size) < 0)
|
||||
// {
|
||||
// DBG(CONF, F("Unable to lock audio buffer memory in RAM: %s") % strerror(errno));
|
||||
// std::cerr << "chan_khomp: Unable to lock temporary audio buffer memory in RAM: "
|
||||
// << strerror(errno) << ". "
|
||||
// << "This is not a catastrophic failure, but may cause unpredictable "
|
||||
// << "audio delay under extreme load conditions." << std::endl;
|
||||
// }
|
||||
};
|
||||
|
||||
FrameStorage::~FrameStorage()
|
||||
{
|
||||
free(_frames);
|
||||
free(_buffer);
|
||||
}
|
||||
|
||||
66
src/mod/endpoints/mod_khomp/src/globals.cpp
Normal file
66
src/mod/endpoints/mod_khomp/src/globals.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
K3LAPI Globals::k3lapi;
|
||||
K3LUtil Globals::k3lutil(Globals::k3lapi);
|
||||
Verbose Globals::verbose(Globals::k3lapi);
|
||||
|
||||
/* Global Timer */
|
||||
Globals::GlobalTimer * Globals::global_timer = NULL;
|
||||
|
||||
Globals::Mutex Globals::khomp_alloc_mutex;
|
||||
|
||||
ConfigOptions Globals::options;
|
||||
|
||||
switch_endpoint_interface_t * Globals::khomp_endpoint_interface = NULL;
|
||||
switch_endpoint_interface_t * Globals::khomp_sms_endpoint_interface = NULL;
|
||||
switch_endpoint_interface_t * Globals::khomp_pr_endpoint_interface = NULL;
|
||||
switch_application_interface_t * Globals::khomp_app_inteface = NULL;
|
||||
switch_api_interface_t * Globals::api_interface = NULL;
|
||||
switch_memory_pool_t * Globals::module_pool = NULL;
|
||||
|
||||
volatile bool Globals::logs_being_rotated = false;
|
||||
|
||||
bool Globals::flag_trace_rdsi = false;
|
||||
2516
src/mod/endpoints/mod_khomp/src/khomp_pvt.cpp
Normal file
2516
src/mod/endpoints/mod_khomp/src/khomp_pvt.cpp
Normal file
File diff suppressed because it is too large
Load Diff
791
src/mod/endpoints/mod_khomp/src/khomp_pvt_fxo.cpp
Normal file
791
src/mod/endpoints/mod_khomp/src/khomp_pvt_fxo.cpp
Normal file
@@ -0,0 +1,791 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "khomp_pvt_fxo.h"
|
||||
#include "lock.h"
|
||||
#include "logger.h"
|
||||
|
||||
int BoardFXO::KhompPvtFXO::makeCall(std::string params)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
int ret = ksSuccess;
|
||||
|
||||
/* we always have audio */
|
||||
call()->_flags.set(Kflags::HAS_PRE_AUDIO);
|
||||
|
||||
if(callFXO()->_call_info_drop == 0 && !callFXO()->_call_info_report)
|
||||
{
|
||||
command(KHOMP_LOG, CM_DISABLE_CALL_ANSWER_INFO);
|
||||
}
|
||||
|
||||
if (!callFXO()->_pre_digits.empty())
|
||||
{
|
||||
/* Seize the line at local PABX. */
|
||||
|
||||
callFXO()->_flags.set(Kflags::CALL_WAIT_SEIZE);
|
||||
|
||||
if (!command(KHOMP_LOG, CM_SEIZE, call()->_orig_addr.c_str()))
|
||||
return ksFail;
|
||||
|
||||
int timeout = 150;
|
||||
|
||||
if(!loopWhileFlagTimed(Kflags::CALL_WAIT_SEIZE, timeout))
|
||||
return ksFail;
|
||||
|
||||
if (callFXO()->_flags.check(Kflags::CALL_WAIT_SEIZE) || (timeout <= 0))
|
||||
return ksFail;
|
||||
|
||||
/* Grab line from local PABX. */
|
||||
|
||||
callFXO()->_flags.set(Kflags::WAIT_SEND_DTMF);
|
||||
|
||||
if (!command(KHOMP_LOG, CM_DIAL_DTMF, callFXO()->_pre_digits.c_str()))
|
||||
return ksFail;
|
||||
|
||||
if(!loopWhileFlagTimed(Kflags::WAIT_SEND_DTMF, timeout))
|
||||
return ksFail;
|
||||
|
||||
if (callFXO()->_flags.check(Kflags::WAIT_SEND_DTMF) || (timeout <= 0))
|
||||
return ksFail;
|
||||
|
||||
/* Seize line from public central (works because the *
|
||||
* continuous cadence is always detected by the k3l api. */
|
||||
|
||||
callFXO()->_flags.set(Kflags::CALL_WAIT_SEIZE);
|
||||
|
||||
if (!command(KHOMP_LOG, CM_SEIZE, call()->_orig_addr.c_str()))
|
||||
return ksFail;
|
||||
|
||||
if(!loopWhileFlagTimed(Kflags::CALL_WAIT_SEIZE, timeout))
|
||||
return ksFail;
|
||||
|
||||
if (callFXO()->_flags.check(Kflags::CALL_WAIT_SEIZE) || (timeout <= 0))
|
||||
return ksFail;
|
||||
|
||||
/* we want the audio as soon as dialing ends */
|
||||
callFXO()->_flags.set(Kflags::EARLY_RINGBACK);
|
||||
|
||||
if (!command(KHOMP_LOG, CM_DIAL_DTMF, call()->_dest_addr.c_str()))
|
||||
return ksFail;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* we want the audio as soon as dialing ends */
|
||||
callFXO()->_flags.set(Kflags::EARLY_RINGBACK);
|
||||
|
||||
|
||||
if(!call()->_orig_addr.empty())
|
||||
params += STG(FMT(" orig_addr=\"%s\"") % _call->_orig_addr);
|
||||
|
||||
|
||||
ret = KhompPvt::makeCall(params);
|
||||
|
||||
if(ret != ksSuccess)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "Fail on make call"));
|
||||
}
|
||||
|
||||
call()->_cleanup_upon_hangup = (ret == ksInvalidParams);
|
||||
}
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target, "(FXO) r"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::doChannelAnswer(CommandRequest &cmd)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
// is this a collect call?
|
||||
bool has_recv_collect_call = _call->_collect_call;
|
||||
|
||||
if(has_recv_collect_call)
|
||||
DBG(FUNC, PVT_FMT(target(), "receive a collect call"));
|
||||
|
||||
if(call()->_flags.check(Kflags::DROP_COLLECT))
|
||||
DBG(FUNC, PVT_FMT(target(), "flag DROP_COLLECT == true"));
|
||||
|
||||
// do we have to drop collect calls?
|
||||
bool has_drop_collect_call = call()->_flags.check(Kflags::DROP_COLLECT);
|
||||
|
||||
// do we have to drop THIS call?
|
||||
bool do_drop_call = has_drop_collect_call && has_recv_collect_call;
|
||||
|
||||
if(!do_drop_call)
|
||||
{
|
||||
command(KHOMP_LOG, CM_CONNECT);
|
||||
}
|
||||
|
||||
if(has_drop_collect_call)
|
||||
{
|
||||
if(has_recv_collect_call)
|
||||
{
|
||||
usleep(75000);
|
||||
|
||||
DBG(FUNC, PVT_FMT(target(), "disconnecting collect call doChannelAnswer FXO"));
|
||||
command(KHOMP_LOG,CM_DISCONNECT);
|
||||
|
||||
// thou shalt not talk anymore!
|
||||
stopListen();
|
||||
stopStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(target(), "dropping collect call at doChannelAnswer FXO"));
|
||||
command(KHOMP_LOG, CM_DROP_COLLECT_CALL);
|
||||
}
|
||||
}
|
||||
|
||||
ret = KhompPvt::doChannelAnswer(cmd);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target,"(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onNewCall(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target,"(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
/* we always have audio */
|
||||
call()->_flags.set(Kflags::HAS_PRE_AUDIO);
|
||||
|
||||
ret = KhompPvt::onNewCall(e);
|
||||
|
||||
if(!ret)
|
||||
return false;
|
||||
|
||||
startListen();
|
||||
startStream();
|
||||
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onDisconnect(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
command(KHOMP_LOG, CM_DISCONNECT);
|
||||
|
||||
ret = KhompPvt::onDisconnect(e);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onChannelRelease(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
if (call()->_flags.check(Kflags::FAX_SENDING))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "stopping fax tx"));
|
||||
_fax->stopFaxTX();
|
||||
}
|
||||
else if (call()->_flags.check(Kflags::FAX_RECEIVING))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "stopping fax rx"));
|
||||
_fax->stopFaxRX();
|
||||
}
|
||||
|
||||
command(KHOMP_LOG, CM_ENABLE_CALL_ANSWER_INFO);
|
||||
|
||||
ret = KhompPvt::onChannelRelease(e);
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onCallSuccess(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
ret = KhompPvt::onCallSuccess(e);
|
||||
|
||||
if (call()->_pre_answer)
|
||||
{
|
||||
dtmfSuppression(Opt::_out_of_band_dtmfs && !call()->_flags.check(Kflags::FAX_DETECTED));
|
||||
|
||||
startListen();
|
||||
startStream();
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onCallFail(K3L_EVENT *e)
|
||||
{
|
||||
bool ret = true;
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
command(KHOMP_LOG, CM_DISCONNECT);
|
||||
|
||||
setHangupCause(causeFromCallFail(e->AddInfo), true);
|
||||
|
||||
ret = KhompPvt::onCallFail(e);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "unable to lock %s!") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onAudioStatus(K3L_EVENT *e)
|
||||
{
|
||||
DBG(STRM, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
try
|
||||
{
|
||||
//ScopedPvtLock lock(this);
|
||||
|
||||
if(e->AddInfo == kmtFax)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "Fax detected"));
|
||||
|
||||
bool already_detected = call()->_flags.check(Kflags::FAX_DETECTED);
|
||||
BEGIN_CONTEXT
|
||||
{
|
||||
if(already_detected)
|
||||
break;
|
||||
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
if (callFXO()->_call_info_drop != 0 || callFXO()->_call_info_report)
|
||||
{
|
||||
/* we did not detected fax yet, send answer info! */
|
||||
setAnswerInfo(Board::KhompPvt::CI_FAX);
|
||||
|
||||
if (callFXO()->_call_info_drop & Board::KhompPvt::CI_FAX)
|
||||
{
|
||||
/* fastest way to force a disconnection */
|
||||
command(KHOMP_LOG,CM_DISCONNECT);//,SCE_HIDE);
|
||||
}
|
||||
}
|
||||
|
||||
if (Opt::_auto_fax_adjustment)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "communication will be adjusted for fax!"));
|
||||
_fax->adjustForFax();
|
||||
}
|
||||
}
|
||||
END_CONTEXT
|
||||
|
||||
if (!already_detected)
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
/* adjust the flag */
|
||||
call()->_flags.set(Kflags::FAX_DETECTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = KhompPvt::onAudioStatus(e);
|
||||
|
||||
DBG(STRM, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onSeizeSuccess(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
callFXO()->_flags.clear(Kflags::CALL_WAIT_SEIZE);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onDtmfDetected(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
if (callFXO()->_flags.check(Kflags::WAIT_SEND_DTMF) ||
|
||||
callFXO()->_flags.check(Kflags::CALL_WAIT_SEIZE))
|
||||
{
|
||||
/* waiting digit or seize means DEADLOCK if we try to
|
||||
* queue something below. */
|
||||
DBG(FUNC, PVT_FMT(_target, "not queueing dtmf, waiting stuff!"));
|
||||
return true;
|
||||
}
|
||||
|
||||
ret = KhompPvt::onDtmfDetected(e);
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onDtmfSendFinish(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
ret = KhompPvt::onDtmfSendFinish(e);
|
||||
|
||||
if (callFXO()->_flags.check(Kflags::EARLY_RINGBACK))
|
||||
{
|
||||
callFXO()->_flags.clear(Kflags::EARLY_RINGBACK);
|
||||
|
||||
/* start grabbing */
|
||||
startListen();
|
||||
|
||||
/* activate resources early... */
|
||||
bool fax_detected = callFXO()->_flags.check(Kflags::FAX_DETECTED);
|
||||
|
||||
bool res_out_of_band_dtmf = Opt::_suppression_delay && Opt::_out_of_band_dtmfs && !fax_detected;
|
||||
bool res_echo_cancellator = Opt::_echo_canceller && !fax_detected;
|
||||
bool res_auto_gain_cntrol = Opt::_auto_gain_control && !fax_detected;
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_DTMF_SUPPRESSION))
|
||||
dtmfSuppression(res_out_of_band_dtmf);
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_ECHO_CANCELLATION))
|
||||
echoCancellation(res_echo_cancellator);
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_AUTO_GAIN_CONTROL))
|
||||
autoGainControl(res_auto_gain_cntrol);
|
||||
|
||||
/* start sending audio if wanted so */
|
||||
if (Opt::_fxo_send_pre_audio)
|
||||
startStream();
|
||||
|
||||
//TODO: Verificar isso aqui
|
||||
if (call()->_pre_answer)
|
||||
{
|
||||
/* tell the user we are answered! */
|
||||
switch_channel_mark_answered(getFSChannel());
|
||||
//pvt->signal_state(AST_CONTROL_ANSWER);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* are we ringing, now? lets try this way! */
|
||||
switch_channel_mark_ring_ready(getFSChannel());
|
||||
//pvt->signal_state(AST_CONTROL_RINGING);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(FXO) r (no valid channel: %s)") % err._msg.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::onCallAnswerInfo(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
int info_code = -1;
|
||||
|
||||
switch (e->AddInfo)
|
||||
{
|
||||
case kcsiCellPhoneMessageBox:
|
||||
info_code = CI_MESSAGE_BOX;
|
||||
break;
|
||||
case kcsiHumanAnswer:
|
||||
info_code = CI_HUMAN_ANSWER;
|
||||
break;
|
||||
case kcsiAnsweringMachine:
|
||||
info_code = CI_ANSWERING_MACHINE;
|
||||
break;
|
||||
case kcsiCarrierMessage:
|
||||
info_code = CI_CARRIER_MESSAGE;
|
||||
break;
|
||||
case kcsiUnknown:
|
||||
info_code = CI_UNKNOWN;
|
||||
break;
|
||||
default:
|
||||
DBG(FUNC, PVT_FMT(_target, "got an unknown call answer info '%d', ignoring...") % e->AddInfo);
|
||||
break;
|
||||
}
|
||||
|
||||
if (info_code != -1)
|
||||
{
|
||||
if (callFXO()->_call_info_report)
|
||||
{
|
||||
//TODO: HOW WE TREAT THAT
|
||||
// make the channel export this
|
||||
setAnswerInfo(info_code);
|
||||
}
|
||||
|
||||
if (callFXO()->_call_info_drop & info_code)
|
||||
{
|
||||
command(KHOMP_LOG,CM_DISCONNECT);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target,"(FXO) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::application(ApplicationType type, switch_core_session_t * session, const char *data)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case FAX_ADJUST:
|
||||
return _fax->adjustForFax();
|
||||
case FAX_SEND:
|
||||
return _fax->sendFax(session, data);
|
||||
case FAX_RECEIVE:
|
||||
return _fax->receiveFax(session, data);
|
||||
case USER_TRANSFER:
|
||||
return _transfer->userTransfer(session, data);
|
||||
default:
|
||||
return KhompPvt::application(type, session, data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::setupConnection()
|
||||
{
|
||||
if(!call()->_flags.check(Kflags::IS_INCOMING) && !call()->_flags.check(Kflags::IS_OUTGOING))
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target, "Channel already disconnected"));
|
||||
return false;
|
||||
}
|
||||
|
||||
callFXO()->_flags.clear(Kflags::CALL_WAIT_SEIZE);
|
||||
callFXO()->_flags.clear(Kflags::WAIT_SEND_DTMF);
|
||||
|
||||
/* if received some disconnect from 'drop collect call'
|
||||
feature of some pbx, then leave the call rock and rolling */
|
||||
//Board::board(_target.device)->_timers.del(callFXO()->_idx_disconnect);
|
||||
|
||||
bool fax_detected = callFXO()->_flags.check(Kflags::FAX_DETECTED) || (callFXO()->_var_fax_adjust == T_TRUE);
|
||||
|
||||
bool res_out_of_band_dtmf = (call()->_var_dtmf_state == T_UNKNOWN || fax_detected ?
|
||||
Opt::_suppression_delay && Opt::_out_of_band_dtmfs && !fax_detected : (call()->_var_dtmf_state == T_TRUE));
|
||||
|
||||
bool res_echo_cancellator = (call()->_var_echo_state == T_UNKNOWN || fax_detected ?
|
||||
Opt::_echo_canceller && !fax_detected : (call()->_var_echo_state == T_TRUE));
|
||||
|
||||
bool res_auto_gain_cntrol = (call()->_var_gain_state == T_UNKNOWN || fax_detected ?
|
||||
Opt::_auto_gain_control && !fax_detected : (call()->_var_gain_state == T_TRUE));
|
||||
|
||||
if (!call()->_flags.check(Kflags::REALLY_CONNECTED))
|
||||
{
|
||||
obtainRX(res_out_of_band_dtmf);
|
||||
|
||||
/* esvazia buffers de leitura/escrita */
|
||||
cleanupBuffers();
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_DTMF_SUPPRESSION))
|
||||
dtmfSuppression(res_out_of_band_dtmf);
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_ECHO_CANCELLATION))
|
||||
echoCancellation(res_echo_cancellator);
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_AUTO_GAIN_CONTROL))
|
||||
autoGainControl(res_auto_gain_cntrol);
|
||||
|
||||
startListen(false);
|
||||
|
||||
startStream();
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) Audio callbacks initialized successfully"));
|
||||
}
|
||||
|
||||
return KhompPvt::setupConnection();
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::autoGainControl(bool enable)
|
||||
{
|
||||
bool ret = KhompPvt::autoGainControl(enable);
|
||||
|
||||
/* enable this AGC also, can be very useful */
|
||||
ret &= command(KHOMP_LOG, (enable ? CM_ENABLE_PLAYER_AGC : CM_DISABLE_PLAYER_AGC));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void BoardFXO::KhompPvtFXO::setAnswerInfo(int answer_info)
|
||||
{
|
||||
const char * value = answerInfoToString(answer_info);
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target,"signaled unknown call answer info '%d', using 'Unknown'...") % answer_info);
|
||||
value = "Unknown";
|
||||
}
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target,"KCallAnswerInfo: %s") % value);
|
||||
|
||||
try
|
||||
{
|
||||
setFSChannelVar("KCallAnswerInfo", value);
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR,PVT_FMT(_target,"Cannot obtain the channel variable: %s") % err._msg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::indicateBusyUnlocked(int cause, bool sent_signaling)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) c"));
|
||||
|
||||
if(!KhompPvt::indicateBusyUnlocked(cause, sent_signaling))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(FXO) r (false)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(call()->_flags.check(Kflags::IS_INCOMING))
|
||||
{
|
||||
if(!call()->_flags.check(Kflags::CONNECTED) && !sent_signaling)
|
||||
{
|
||||
//we are talking about branches, not trunks
|
||||
command(KHOMP_LOG, CM_CONNECT);
|
||||
command(KHOMP_LOG, CM_DISCONNECT);
|
||||
}
|
||||
else
|
||||
{
|
||||
//already connected or sent signaling...
|
||||
mixer(KHOMP_LOG, 1, kmsGenerator, kmtBusy);
|
||||
}
|
||||
}
|
||||
else if(call()->_flags.check(Kflags::IS_OUTGOING))
|
||||
{
|
||||
//already connected or sent signaling...
|
||||
mixer(KHOMP_LOG, 1, kmsGenerator, kmtBusy);
|
||||
}
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target, "(FXO) r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BoardFXO::KhompPvtFXO::reportFailToReceive(int fail_code)
|
||||
{
|
||||
KhompPvt::reportFailToReceive(fail_code);
|
||||
|
||||
command(KHOMP_LOG, CM_CONNECT);
|
||||
|
||||
command(KHOMP_LOG, CM_DISCONNECT);
|
||||
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::validContexts(Board::KhompPvt::ContextListType & contexts, std::string extra_context)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target,"(FXO) c"));
|
||||
|
||||
contexts.push_back(Opt::_context_fxo);
|
||||
contexts.push_back(Opt::_context2_fxo);
|
||||
|
||||
for (Board::KhompPvt::ContextListType::iterator i = contexts.begin(); i != contexts.end(); i++)
|
||||
{
|
||||
replaceTemplate((*i), "CC", _target.object);
|
||||
}
|
||||
|
||||
bool ret = Board::KhompPvt::validContexts(contexts,extra_context);
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target,"(FXO) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::isOK()
|
||||
{
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
if (k3lGetDeviceStatus (_target.device, _target.object + ksoChannel, &status, sizeof (status)) != ksSuccess)
|
||||
return false;
|
||||
|
||||
return (status.AddInfo == kfcsEnabled);
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "unable to lock %s!") % err._msg.c_str() );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BoardFXO::KhompPvtFXO::sendDtmf(std::string digit)
|
||||
{
|
||||
if(_transfer->checkUserXferUnlocked(digit))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(target(), "started (or waiting for) an user xfer"));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ret = KhompPvt::sendDtmf(callFXO()->_digits_buffer);
|
||||
|
||||
callFXO()->_digits_buffer.clear();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
565
src/mod/endpoints/mod_khomp/src/khomp_pvt_gsm.cpp
Normal file
565
src/mod/endpoints/mod_khomp/src/khomp_pvt_gsm.cpp
Normal file
@@ -0,0 +1,565 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "khomp_pvt_gsm.h"
|
||||
#include "lock.h"
|
||||
#include "logger.h"
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::isOK()
|
||||
{
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
K3L_CHANNEL_STATUS status;
|
||||
|
||||
if (k3lGetDeviceStatus (_target.device, _target.object + ksoChannel, &status, sizeof (status)) != ksSuccess)
|
||||
return false;
|
||||
|
||||
switch (status.AddInfo)
|
||||
{
|
||||
case kgsmModemError:
|
||||
case kgsmSIMCardError:
|
||||
case kgsmNetworkError:
|
||||
case kgsmNotReady:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "unable to lock %s!") % err._msg.c_str() );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::onChannelRelease(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
call()->_flags.clear(Kflags::HAS_PRE_AUDIO);
|
||||
command(KHOMP_LOG, CM_ENABLE_CALL_ANSWER_INFO);
|
||||
|
||||
ret = KhompPvt::onChannelRelease(e);
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::onCallSuccess(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
ret = KhompPvt::onCallSuccess(e);
|
||||
|
||||
if (call()->_pre_answer)
|
||||
{
|
||||
dtmfSuppression(Opt::_out_of_band_dtmfs);
|
||||
|
||||
startListen();
|
||||
startStream();
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void BoardGSM::KhompPvtGSM::setAnswerInfo(int answer_info)
|
||||
{
|
||||
const char * value = answerInfoToString(answer_info);
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target,"signaled unknown call answer info '%d', using 'Unknown'...") % answer_info);
|
||||
value = "Unknown";
|
||||
}
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target,"KCallAnswerInfo: %s") % value);
|
||||
|
||||
try
|
||||
{
|
||||
setFSChannelVar("KCallAnswerInfo",value);
|
||||
}
|
||||
catch(Board::KhompPvt::InvalidSwitchChannel & err)
|
||||
{
|
||||
LOG(ERROR,PVT_FMT(_target,"Cannot obtain the channel variable: %s") % err._msg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::onCallAnswerInfo(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) c"));
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
int info_code = -1;
|
||||
|
||||
switch (e->AddInfo)
|
||||
{
|
||||
case kcsiCellPhoneMessageBox:
|
||||
info_code = CI_MESSAGE_BOX;
|
||||
break;
|
||||
case kcsiHumanAnswer:
|
||||
info_code = CI_HUMAN_ANSWER;
|
||||
break;
|
||||
case kcsiAnsweringMachine:
|
||||
info_code = CI_ANSWERING_MACHINE;
|
||||
break;
|
||||
case kcsiCarrierMessage:
|
||||
info_code = CI_CARRIER_MESSAGE;
|
||||
break;
|
||||
case kcsiUnknown:
|
||||
info_code = CI_UNKNOWN;
|
||||
break;
|
||||
default:
|
||||
DBG(FUNC, PVT_FMT(_target, "got an unknown call answer info '%d', ignoring...") % e->AddInfo);
|
||||
break;
|
||||
}
|
||||
|
||||
if (info_code != -1)
|
||||
{
|
||||
if (callGSM()->_call_info_report)
|
||||
{
|
||||
//TODO: HOW WE TREAT THAT
|
||||
// make the channel export this
|
||||
setAnswerInfo(info_code);
|
||||
}
|
||||
|
||||
if (callGSM()->_call_info_drop & info_code)
|
||||
{
|
||||
command(KHOMP_LOG, CM_SEND_TO_MODEM, "ATH");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target,"(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
bool BoardGSM::KhompPvtGSM::onDtmfDetected(K3L_EVENT *e)
|
||||
{
|
||||
bool ret = KhompPvt::onDtmfDetected(e);
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target, "(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::onNewCall(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target,"(GSM) c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
bool ret = KhompPvtGSM::onNewCall(e);
|
||||
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::onCallFail(K3L_EVENT *e)
|
||||
{
|
||||
bool ret = true;
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
//K::internal::gsm_cleanup_and_restart(pvt, owner_nr, true);
|
||||
setHangupCause(causeFromCallFail(e->AddInfo), true);
|
||||
|
||||
ret = KhompPvt::onCallFail(e);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "unable to lock %s!") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::onDisconnect(K3L_EVENT *e)
|
||||
{
|
||||
bool ret = true;
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
//gsm_cleanup_and_restart(pvt, (int)evt.gsm_call_ref);
|
||||
|
||||
ret = KhompPvt::onDisconnect(e);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(GSM) unable to lock %s!") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int BoardGSM::KhompPvtGSM::makeCall(std::string params)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target, "(GSM) c"));
|
||||
|
||||
if(callGSM()->_call_info_drop == 0 && !callGSM()->_call_info_report)
|
||||
{
|
||||
command(KHOMP_LOG, CM_DISABLE_CALL_ANSWER_INFO);
|
||||
}
|
||||
|
||||
int ret = KhompPvt::makeCall(params);
|
||||
|
||||
if(ret != ksSuccess)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "Fail on make call"));
|
||||
}
|
||||
|
||||
call()->_cleanup_upon_hangup = (ret == ksInvalidParams || ret == ksInvalidState);
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target, "(GSM) r"));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::doChannelAnswer(CommandRequest &cmd)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
command(KHOMP_LOG, CM_CONNECT);
|
||||
|
||||
ret = KhompPvt::doChannelAnswer(cmd);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target,"(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::doChannelHangup(CommandRequest &cmd)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) c"));
|
||||
|
||||
bool ret = true;
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
int owner_nr = 0;
|
||||
|
||||
command(KHOMP_LOG, CM_DISCONNECT,
|
||||
STG(FMT("gsm_call_ref=%d") % (int)owner_nr).c_str());
|
||||
|
||||
|
||||
//ret = KhompPvt::doChannelHangup(cmd);
|
||||
|
||||
}
|
||||
catch (ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(_target,"(GSM) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::application(ApplicationType type, switch_core_session_t * session, const char *data)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case SMS_CHECK:
|
||||
return true;
|
||||
case SMS_SEND:
|
||||
return _sms->sendSMS(session, data);
|
||||
case SELECT_SIM_CARD:
|
||||
return selectSimCard(data);
|
||||
default:
|
||||
return KhompPvt::application(type, session, data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::setupConnection()
|
||||
{
|
||||
if(!call()->_flags.check(Kflags::IS_INCOMING) && !call()->_flags.check(Kflags::IS_OUTGOING))
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target, "Channel already disconnected"));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool res_out_of_band_dtmf = (call()->_var_dtmf_state == T_UNKNOWN ?
|
||||
Opt::_suppression_delay && Opt::_out_of_band_dtmfs : (call()->_var_dtmf_state == T_TRUE));
|
||||
|
||||
bool res_echo_cancellator = (call()->_var_echo_state == T_UNKNOWN ?
|
||||
Opt::_echo_canceller : (call()->_var_echo_state == T_TRUE));
|
||||
|
||||
|
||||
bool res_auto_gain_cntrol = (call()->_var_gain_state == T_UNKNOWN ?
|
||||
Opt::_auto_gain_control : (call()->_var_gain_state == T_TRUE));
|
||||
|
||||
|
||||
if (!call()->_flags.check(Kflags::REALLY_CONNECTED))
|
||||
{
|
||||
obtainRX(res_out_of_band_dtmf);
|
||||
|
||||
/* esvazia buffers de leitura/escrita */
|
||||
cleanupBuffers();
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_DTMF_SUPPRESSION))
|
||||
dtmfSuppression(res_out_of_band_dtmf);
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_ECHO_CANCELLATION))
|
||||
echoCancellation(res_echo_cancellator);
|
||||
|
||||
if (!call()->_flags.check(Kflags::KEEP_AUTO_GAIN_CONTROL))
|
||||
autoGainControl(res_auto_gain_cntrol);
|
||||
|
||||
startListen(false);
|
||||
|
||||
startStream();
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) Audio callbacks initialized successfully"));
|
||||
}
|
||||
|
||||
return Board::KhompPvt::setupConnection();
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::indicateBusyUnlocked(int cause, bool sent_signaling)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) c"));
|
||||
|
||||
if(!KhompPvt::indicateBusyUnlocked(cause, sent_signaling))
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(GSM) r (false)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if(call()->_flags.check(Kflags::IS_INCOMING))
|
||||
{
|
||||
if(!call()->_flags.check(Kflags::CONNECTED) && !sent_signaling)
|
||||
{
|
||||
/* we are talking about branches, not trunks */
|
||||
command(KHOMP_LOG, CM_DISCONNECT);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* already connected or sent signaling... */
|
||||
mixer(KHOMP_LOG, 1, kmsGenerator, kmtBusy);
|
||||
}
|
||||
}
|
||||
else if(call()->_flags.check(Kflags::IS_OUTGOING))
|
||||
{
|
||||
/* already connected or sent signaling... */
|
||||
mixer(KHOMP_LOG, 1, kmsGenerator, kmtBusy);
|
||||
}
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target, "(GSM) r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BoardGSM::KhompPvtGSM::reportFailToReceive(int fail_code)
|
||||
{
|
||||
KhompPvt::reportFailToReceive(fail_code);
|
||||
|
||||
command(KHOMP_LOG, CM_CONNECT);
|
||||
|
||||
/* K3L may fail depending on its configuration (GsmModem) */
|
||||
if (!command(KHOMP_LOG, CM_DISCONNECT))
|
||||
{
|
||||
int owner_nr = 0;
|
||||
command(KHOMP_LOG, CM_DISCONNECT,
|
||||
STG(FMT("gsm_call_ref=\"%d\"") % owner_nr).c_str());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int BoardGSM::KhompPvtGSM::causeFromCallFail(int fail)
|
||||
{
|
||||
int switch_cause = SWITCH_CAUSE_USER_BUSY;
|
||||
|
||||
if (fail <= 127)
|
||||
switch_cause = fail;
|
||||
else
|
||||
switch_cause = SWITCH_CAUSE_INTERWORKING;
|
||||
|
||||
return switch_cause;
|
||||
}
|
||||
|
||||
int BoardGSM::KhompPvtGSM::callFailFromCause(int cause)
|
||||
{
|
||||
int k3l_fail = -1; // default
|
||||
|
||||
if (cause <= 127)
|
||||
k3l_fail = cause;
|
||||
else
|
||||
k3l_fail = kgccInterworking;
|
||||
|
||||
return k3l_fail;
|
||||
}
|
||||
|
||||
bool BoardGSM::KhompPvtGSM::validContexts(Board::KhompPvt::ContextListType & contexts, std::string extra_context)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target,"(GSM) c"));
|
||||
|
||||
if (!extra_context.empty())
|
||||
{
|
||||
if (!_group_context.empty())
|
||||
{
|
||||
std::string pvt_context(_group_context);
|
||||
pvt_context += "-";
|
||||
pvt_context += extra_context;
|
||||
contexts.push_back(pvt_context);
|
||||
}
|
||||
|
||||
if (!Opt::_context_gsm_call.empty())
|
||||
{
|
||||
std::string context(Opt::_context_gsm_call);
|
||||
context += "-";
|
||||
context += extra_context;
|
||||
contexts.push_back(_group_context);
|
||||
}
|
||||
|
||||
if (!Opt::_context2_gsm_call.empty())
|
||||
{
|
||||
std::string context(Opt::_context2_gsm_call);
|
||||
context += "-";
|
||||
context += extra_context;
|
||||
contexts.push_back(_group_context);
|
||||
}
|
||||
|
||||
if (!Opt::_context2_gsm_call.empty())
|
||||
{
|
||||
std::string context(Opt::_context2_gsm_call);
|
||||
context += "-";
|
||||
context += extra_context;
|
||||
contexts.push_back(_group_context);
|
||||
}
|
||||
}
|
||||
|
||||
contexts.push_back(Opt::_context_gsm_call);
|
||||
contexts.push_back(Opt::_context2_gsm_call);
|
||||
|
||||
for (Board::KhompPvt::ContextListType::iterator i = contexts.begin(); i != contexts.end(); i++)
|
||||
replaceTemplate((*i), "CC", _target.object);
|
||||
|
||||
bool ret = Board::KhompPvt::validContexts(contexts,extra_context);
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target,"(GSM) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
3425
src/mod/endpoints/mod_khomp/src/khomp_pvt_kxe1.cpp
Normal file
3425
src/mod/endpoints/mod_khomp/src/khomp_pvt_kxe1.cpp
Normal file
File diff suppressed because it is too large
Load Diff
288
src/mod/endpoints/mod_khomp/src/khomp_pvt_passive.cpp
Normal file
288
src/mod/endpoints/mod_khomp/src/khomp_pvt_passive.cpp
Normal file
@@ -0,0 +1,288 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "khomp_pvt_passive.h"
|
||||
#include "lock.h"
|
||||
#include "logger.h"
|
||||
|
||||
bool BoardPassive::KhompPvtPassive::validContexts(Board::KhompPvt::ContextListType & contexts, std::string extra_context)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target,"(Passive) c"));
|
||||
|
||||
contexts.push_back(Opt::_context_pr);
|
||||
|
||||
for (Board::KhompPvt::ContextListType::iterator i = contexts.begin(); i != contexts.end(); i++)
|
||||
{
|
||||
replaceTemplate((*i), "CC", _target.object);
|
||||
}
|
||||
|
||||
bool ret = Board::KhompPvt::validContexts(contexts,extra_context);
|
||||
|
||||
DBG(FUNC,PVT_FMT(_target,"(Passive) r"));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool BoardPassive::KhompPvtHI::onSeizureStart(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC,PVT_FMT(_target,"(HI) c"));
|
||||
|
||||
try
|
||||
{
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
_call->_orig_addr.clear();
|
||||
_call->_dest_addr = "s";
|
||||
|
||||
#if SWITCH_LESS_THAN(1,0,6)
|
||||
session(switch_core_session_request(Globals::khomp_pr_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, NULL));
|
||||
#else
|
||||
session(switch_core_session_request(Globals::khomp_pr_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL));
|
||||
#endif
|
||||
|
||||
if (justAlloc(true) != SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
setHangupCause(SWITCH_CAUSE_UNALLOCATED_NUMBER);
|
||||
cleanup(CLN_FAIL);
|
||||
LOG(ERROR, PVT_FMT(target(), "(HI) r (Initilization Error on alloc!)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* begin context adjusting + processing */
|
||||
ContextListType contexts;
|
||||
|
||||
validContexts(contexts);
|
||||
|
||||
std::string tmp_exten;
|
||||
std::string tmp_context;
|
||||
|
||||
switch (findExtension(tmp_exten, tmp_context, contexts, call()->_dest_addr, call()->_orig_addr, false))
|
||||
{
|
||||
case MATCH_NONE:
|
||||
destroy();
|
||||
owner(NULL);
|
||||
LOG(WARNING, PVT_FMT(_target, "(HI) r (unable to find exten/context on incoming call %s/%s)")
|
||||
% _call->_dest_addr % (contexts.size() >= 1 ? contexts[0] : "default"));
|
||||
return false;
|
||||
default:
|
||||
DBG(FUNC, PVT_FMT(_target, "(HI) our: context '%s', exten '%s'") % tmp_context % tmp_exten);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
call()->_incoming_context = tmp_context;
|
||||
|
||||
startListen();
|
||||
|
||||
if (justStart() != SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
setHangupCause(SWITCH_CAUSE_REQUESTED_CHAN_UNAVAIL);
|
||||
cleanup(CLN_FAIL);
|
||||
LOG(ERROR, PVT_FMT(target(), "(HI) r (Initilization Error on start!)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
call()->_flags.set(Kflags::REALLY_CONNECTED);
|
||||
call()->statistics()->incrementNewCall();
|
||||
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(HI) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(HI) r"));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BoardPassive::KhompPvtKPR::onNewCall(K3L_EVENT *e)
|
||||
{
|
||||
DBG(FUNC, PVT_FMT(_target, "(KPR) c"));
|
||||
|
||||
try
|
||||
{
|
||||
std::string orig_addr, dest_addr;
|
||||
|
||||
Globals::k3lapi.get_param(e, "orig_addr", orig_addr);
|
||||
Globals::k3lapi.get_param(e, "dest_addr", dest_addr);
|
||||
|
||||
if(dest_addr.empty())
|
||||
{
|
||||
dest_addr="s";
|
||||
}
|
||||
|
||||
bool isdn_reverse_charge = false;
|
||||
std::string isdn_reverse_charge_str;
|
||||
int isdn_status = Globals::k3lapi.get_param(e, "isdn_reverse_charge", isdn_reverse_charge_str);
|
||||
|
||||
if (isdn_status == ksSuccess && !isdn_reverse_charge_str.empty())
|
||||
{
|
||||
try
|
||||
{
|
||||
isdn_reverse_charge = Strings::toboolean(isdn_reverse_charge_str);
|
||||
}
|
||||
catch (Strings::invalid_value & err)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
long int r2_category = -1;
|
||||
std::string r2_categ_a;
|
||||
int r2_status = Globals::k3lapi.get_param(e, "r2_categ_a", r2_categ_a);
|
||||
|
||||
if (r2_status == ksSuccess && !r2_categ_a.empty())
|
||||
{
|
||||
try
|
||||
{
|
||||
r2_category = Strings::toulong(r2_categ_a);
|
||||
}
|
||||
catch (Strings::invalid_value e)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ScopedPvtLock lock(this);
|
||||
|
||||
_call->_orig_addr = orig_addr;
|
||||
_call->_dest_addr = dest_addr;
|
||||
|
||||
if(isdn_reverse_charge || r2_category == kg2CollectCall)
|
||||
{
|
||||
call()->_collect_call = true;
|
||||
}
|
||||
|
||||
#if SWITCH_LESS_THAN(1,0,6)
|
||||
session(switch_core_session_request(Globals::khomp_pr_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, NULL));
|
||||
#else
|
||||
session(switch_core_session_request(Globals::khomp_pr_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, SOF_NONE, NULL));
|
||||
#endif
|
||||
|
||||
if (justAlloc(true) != SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
setHangupCause(SWITCH_CAUSE_UNALLOCATED_NUMBER);
|
||||
cleanup(CLN_FAIL);
|
||||
LOG(ERROR, PVT_FMT(target(), "(KPR) r (Initilization Error on alloc!)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* begin context adjusting + processing */
|
||||
ContextListType contexts;
|
||||
|
||||
validContexts(contexts);
|
||||
|
||||
std::string tmp_exten;
|
||||
std::string tmp_context;
|
||||
|
||||
switch (findExtension(tmp_exten, tmp_context, contexts, call()->_dest_addr, call()->_orig_addr, false))
|
||||
{
|
||||
case MATCH_NONE:
|
||||
destroy();
|
||||
owner(NULL);
|
||||
LOG(WARNING, PVT_FMT(_target, "(KPR) r (unable to find exten/context on incoming call %s/%s)")
|
||||
% _call->_dest_addr % (contexts.size() >= 1 ? contexts[0] : "default"));
|
||||
return false;
|
||||
default:
|
||||
DBG(FUNC, PVT_FMT(_target, "(KPR) our: context '%s', exten '%s'") % tmp_context % tmp_exten);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
call()->_incoming_context = tmp_context;
|
||||
_call->_dest_addr = tmp_exten;
|
||||
|
||||
obtainBoth();
|
||||
|
||||
startListen(false);
|
||||
|
||||
if (justStart() != SWITCH_STATUS_SUCCESS)
|
||||
{
|
||||
setHangupCause(SWITCH_CAUSE_REQUESTED_CHAN_UNAVAIL);
|
||||
cleanup(CLN_FAIL);
|
||||
LOG(ERROR, PVT_FMT(target(), "(KPR) r (Initilization Error on start!)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
call()->_flags.set(Kflags::REALLY_CONNECTED);
|
||||
call()->statistics()->incrementNewCall();
|
||||
|
||||
}
|
||||
catch(ScopedLockFailed & err)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "(KPR) r (unable to lock %s!)") % err._msg.c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(_target, "(KPR) r"));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BoardPassive::KhompPvtKPR::obtainBoth()
|
||||
{
|
||||
/* estes buffers *NAO PODEM SER ESTATICOS*! */
|
||||
char cmd1[] = { 0x3f, 0x03, 0xff, 0x00, 0x05, 0xff };
|
||||
char cmd2[] = { 0x3f, 0x03, 0xff, 0x01, 0x05, 0xff };
|
||||
|
||||
cmd1[2] = cmd1[5] = cmd2[2] = _target.object;
|
||||
cmd2[5] = _target.object + 30;
|
||||
|
||||
try
|
||||
{
|
||||
int dsp = Globals::k3lapi.get_dsp(_target.device, K3LAPI::DSP_AUDIO);
|
||||
|
||||
Globals::k3lapi.raw_command(_target.device, dsp, cmd1, sizeof(cmd1));
|
||||
|
||||
Globals::k3lapi.raw_command(_target.device, dsp, cmd2, sizeof(cmd2));
|
||||
|
||||
}
|
||||
catch(K3LAPI::failed_raw_command & e)
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(target(), "ERROR sending mixer command!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
138
src/mod/endpoints/mod_khomp/src/lock.cpp
Normal file
138
src/mod/endpoints/mod_khomp/src/lock.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "lock.h"
|
||||
|
||||
/* ScopedPvtLock */
|
||||
|
||||
ScopedPvtLock::ScopedPvtLock(KhompPvt * pvt)
|
||||
: ScopedLockBasic(false), _pvt(pvt)
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "c"));
|
||||
|
||||
if (! _pvt)
|
||||
throw ScopedLockFailed(ScopedLockFailed::NULL_PVT, "null KhompPvt");
|
||||
|
||||
switch(_pvt->_mutex.lock())
|
||||
{
|
||||
case SimpleLock::FAILURE:
|
||||
throw ScopedLockFailed(ScopedLockFailed::FAILED, "Failure");
|
||||
break;
|
||||
case SimpleLock::ISINUSE:
|
||||
throw ScopedLockFailed(ScopedLockFailed::FAILED, "In use");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
_locked = true;
|
||||
|
||||
//DBG(LOCK, DP(_pvt, "r"));
|
||||
}
|
||||
|
||||
ScopedPvtLock::~ScopedPvtLock()
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "c"));
|
||||
unlock();
|
||||
//DBG(LOCK, DP(_pvt, "r"));
|
||||
}
|
||||
|
||||
void ScopedPvtLock::unlock()
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "c"));
|
||||
|
||||
if (_locked)
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "unlocking!"));
|
||||
|
||||
_locked = false;
|
||||
_pvt->_mutex.unlock();
|
||||
}
|
||||
|
||||
//DBG(LOCK, DP(_pvt, "r"));
|
||||
}
|
||||
|
||||
ScopedAllocLock::ScopedAllocLock()
|
||||
: ScopedLockBasic(false)
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "c"));
|
||||
|
||||
switch(Globals::khomp_alloc_mutex.lock())
|
||||
{
|
||||
case SimpleLock::FAILURE:
|
||||
throw ScopedLockFailed(ScopedLockFailed::ALLOC_FAILED, "Failure");
|
||||
break;
|
||||
case SimpleLock::ISINUSE:
|
||||
throw ScopedLockFailed(ScopedLockFailed::ALLOC_FAILED, "In use");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
_locked = true;
|
||||
|
||||
//DBG(LOCK, DP(_pvt, "r"));
|
||||
}
|
||||
|
||||
ScopedAllocLock::~ScopedAllocLock()
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "c"));
|
||||
unlock();
|
||||
//DBG(LOCK, DP(_pvt, "r"));
|
||||
}
|
||||
|
||||
void ScopedAllocLock::unlock()
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "c"));
|
||||
|
||||
if (_locked)
|
||||
{
|
||||
//DBG(LOCK, DP(_pvt, "unlocking!"));
|
||||
|
||||
_locked = false;
|
||||
Globals::khomp_alloc_mutex.unlock();
|
||||
}
|
||||
|
||||
//DBG(LOCK, DP(_pvt, "r"));
|
||||
}
|
||||
|
||||
437
src/mod/endpoints/mod_khomp/src/logger.cpp
Normal file
437
src/mod/endpoints/mod_khomp/src/logger.cpp
Normal file
@@ -0,0 +1,437 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "logger.h"
|
||||
#include "globals.h"
|
||||
|
||||
namespace K
|
||||
{
|
||||
LogManager Logger::Logg;
|
||||
LogInternalManager Logger::Logg2;
|
||||
|
||||
std::string Logger::base_path;
|
||||
std::ofstream Logger::generic_file;
|
||||
|
||||
bool LogConfig::set(Logfile & file, const char * section, const char * option, bool value)
|
||||
{
|
||||
Section * sec = file.root().section_find(section);
|
||||
|
||||
if (!sec)
|
||||
return false;
|
||||
|
||||
Option * opt = sec->option_find(option);
|
||||
|
||||
if (!opt)
|
||||
return false;
|
||||
|
||||
opt->set( value ? "Ativado" : "Desativado" );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LogConfig::commit(Logfile & file)
|
||||
{
|
||||
return file.provide();
|
||||
}
|
||||
|
||||
bool Logger::start()
|
||||
{
|
||||
/* we love shortcuts! */
|
||||
typedef LogManager::Option LogOpt;
|
||||
|
||||
typedef LogOpt::Flags Flags;
|
||||
typedef LogOpt::InitFlags FL;
|
||||
|
||||
/* configures default log levels */
|
||||
Logger::Logg.classe(C_ERROR)
|
||||
& LogOpt(O_CONSOLE, "ERROR: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::ENABLED)))
|
||||
& LogOpt(O_GENERIC, "E: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_WARNING)
|
||||
& LogOpt(O_CONSOLE, "WARNING: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::ENABLED)))
|
||||
& LogOpt(O_GENERIC, "W: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_MESSAGE)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::ENABLED)))
|
||||
& LogOpt(O_GENERIC, "M: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
/* k3l messages */
|
||||
Logger::Logg.classe(C_COMMAND)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::DATETIME)))
|
||||
& LogOpt(O_GENERIC, "c: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID)));
|
||||
|
||||
Logger::Logg.classe(C_EVENT)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::DATETIME)))
|
||||
& LogOpt(O_GENERIC, "e: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID)));
|
||||
|
||||
Logger::Logg.classe(C_AUDIO_EV)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::DATETIME)))
|
||||
& LogOpt(O_GENERIC, "a: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID)));
|
||||
|
||||
Logger::Logg.classe(C_MODEM_EV)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::DATETIME)))
|
||||
& LogOpt(O_GENERIC, "m: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_LINK_STT)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::DATETIME) & FL(LogOpt::ENABLED)))
|
||||
& LogOpt(O_GENERIC, "s: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_CAS_MSGS)
|
||||
& LogOpt(O_CONSOLE, Flags(FL(LogOpt::DATETIME) & FL(LogOpt::ENABLED)))
|
||||
& LogOpt(O_GENERIC, "p: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
/* channel debug */
|
||||
Logger::Logg.classe(C_DBG_FUNC)
|
||||
& LogOpt(O_GENERIC, "F: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_DBG_LOCK)
|
||||
& LogOpt(O_GENERIC, "L: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_DBG_THRD)
|
||||
& LogOpt(O_GENERIC, "T: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_DBG_STRM)
|
||||
& LogOpt(O_GENERIC, "S: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_DBG_CONF)
|
||||
& LogOpt(O_GENERIC, "C: ", Flags(FL(LogOpt::DATETIME) & FL(LogOpt::THREADID) & FL(LogOpt::ENABLED)));
|
||||
|
||||
Logger::Logg.classe(C_DBG_FUNC).enabled(false);
|
||||
Logger::Logg.classe(C_DBG_LOCK).enabled(false);
|
||||
Logger::Logg.classe(C_DBG_THRD).enabled(false);
|
||||
Logger::Logg.classe(C_DBG_STRM).enabled(false);
|
||||
|
||||
/* we debug config file loading, by default */
|
||||
Logger::Logg.classe(C_DBG_CONF).enabled(true);
|
||||
|
||||
/* adds a prefix to the cli messages */
|
||||
Logger::Logg.classe(C_CLI).prefix("<K> ");
|
||||
|
||||
/* inserts default console log before opening files */
|
||||
Logger::Logg.add(O_CONSOLE,SwitchConsoleLog(), "mod_khomp: ");
|
||||
|
||||
time_t tv;
|
||||
struct tm lt;
|
||||
|
||||
/* get local time! */
|
||||
time (&tv);
|
||||
localtime_r (&tv, <);
|
||||
|
||||
base_path = STG(FMT("/var/log/khomp%d.%d/mod_khomp-%04d%02d%02d_%02d%02d%02d/")
|
||||
% k3lApiMajorVersion % k3lApiMinorVersion % (lt.tm_year + 1900) % (lt.tm_mon + 1)
|
||||
% lt.tm_mday % lt.tm_hour % lt.tm_min % lt.tm_sec );
|
||||
|
||||
// NOTE: ALWAYS unlink, as we may have a dangling symlink lying around...
|
||||
std::string link_path = STG(FMT("/var/log/khomp%d.%d/current") % k3lApiMajorVersion % k3lApiMinorVersion );
|
||||
unlink(link_path.c_str());
|
||||
|
||||
if (mkdir(base_path.c_str(), 493 /*755*/) < 0 && errno != EEXIST)
|
||||
{
|
||||
Logger::Logg(C_ERROR, D("unable to create log directory '%s': %s!") % base_path % strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (symlink(base_path.c_str(), link_path.c_str()))
|
||||
{
|
||||
Logger::Logg(C_ERROR, D("unable to create symlink to latest log directory '%s': %s!") % base_path % strerror(errno));
|
||||
}
|
||||
|
||||
std::string gen_tmp = base_path + std::string("generic.log");
|
||||
generic_file.open(gen_tmp.c_str());
|
||||
|
||||
if (!generic_file.good())
|
||||
{
|
||||
Logger::Logg(C_ERROR, D("could not open file '%s': %s") % gen_tmp % strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
/* inserts other file descriptors (TODO: delete this when stopping logs) */
|
||||
Logger::Logg.add(O_GENERIC, &generic_file);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Logger::stop()
|
||||
{
|
||||
if(generic_file.is_open())
|
||||
{
|
||||
generic_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
bool Logger::rotate()
|
||||
{
|
||||
std::string new_gen;
|
||||
|
||||
for (unsigned int i = 0;; i++)
|
||||
{
|
||||
std::string tmp = base_path + STG(FMT("generic.%d.log") % i);
|
||||
|
||||
if (access(tmp.c_str(), R_OK|W_OK) != 0 && errno == ENOENT)
|
||||
{
|
||||
new_gen = tmp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string old_gen = base_path + "generic.log";
|
||||
|
||||
if (rename(old_gen.c_str(), new_gen.c_str()) != 0)
|
||||
{
|
||||
Logger::Logg(C_ERROR, FMT("unable to move generic log file: %s.") % strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
Globals::logs_being_rotated = true;
|
||||
|
||||
generic_file.close();
|
||||
generic_file.open(old_gen.c_str());
|
||||
|
||||
Globals::logs_being_rotated = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Logger::processLogConsole(switch_stream_handle_t *s, const std::string options, bool invert, bool unique)
|
||||
{
|
||||
class_type classe = ( !s ? C_MESSAGE : C_CLI );
|
||||
|
||||
Strings::vector_type tokens;
|
||||
Strings::tokenize(options, tokens, ",");
|
||||
|
||||
bool flag_errors = false;
|
||||
bool flag_warnings = false;
|
||||
bool flag_messages = false;
|
||||
bool flag_events = false;
|
||||
bool flag_commands = false;
|
||||
bool flag_audio = false;
|
||||
bool flag_modem = false;
|
||||
bool flag_link = false;
|
||||
bool flag_cas = false;
|
||||
|
||||
Strings::Merger strs;
|
||||
|
||||
for (Strings::vector_type::iterator i = tokens.begin(); i != tokens.end(); i++)
|
||||
{
|
||||
std::string tok = Strings::trim(*i);
|
||||
|
||||
/**/ if ((tok) == "errors") flag_errors = true;
|
||||
else if ((tok) == "warnings") flag_warnings = true;
|
||||
else if ((tok) == "messages") flag_messages = true;
|
||||
else if ((tok) == "events") flag_events = true;
|
||||
else if ((tok) == "commands") flag_commands = true;
|
||||
else if ((tok) == "audio") flag_audio = true;
|
||||
else if ((tok) == "modem") flag_modem = true;
|
||||
else if ((tok) == "link") flag_link = true;
|
||||
else if ((tok) == "cas") flag_cas = true;
|
||||
else if ((tok) == "standard")
|
||||
{
|
||||
flag_errors = true;
|
||||
flag_warnings = true;
|
||||
flag_messages = true;
|
||||
flag_link = true;
|
||||
}
|
||||
else if ((tok) == "all")
|
||||
{
|
||||
flag_errors = true;
|
||||
flag_warnings = true;
|
||||
flag_messages = true;
|
||||
flag_events = true;
|
||||
flag_commands = true;
|
||||
flag_audio = true;
|
||||
flag_modem = true;
|
||||
flag_link = true;
|
||||
flag_cas = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
K::Logger::Logg2(classe, s, FMT("WARNING: The following console message option is not valid and will be ignored: %s.") % (tok) );
|
||||
continue;
|
||||
}
|
||||
|
||||
strs.add(tok);
|
||||
}
|
||||
|
||||
if ((unique && !flag_errors) || flag_errors) K::Logger::Logg.classe(C_ERROR) .set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_errors));
|
||||
if ((unique && !flag_warnings) || flag_warnings) K::Logger::Logg.classe(C_WARNING) .set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_warnings));
|
||||
if ((unique && !flag_messages) || flag_messages) K::Logger::Logg.classe(C_MESSAGE) .set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_messages));
|
||||
if ((unique && !flag_events) || flag_events) K::Logger::Logg.classe(C_EVENT) .set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_events));
|
||||
if ((unique && !flag_commands) || flag_commands) K::Logger::Logg.classe(C_COMMAND) .set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_commands));
|
||||
if ((unique && !flag_audio) || flag_audio) K::Logger::Logg.classe(C_AUDIO_EV).set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_audio));
|
||||
if ((unique && !flag_modem) || flag_modem) K::Logger::Logg.classe(C_MODEM_EV).set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_modem));
|
||||
if ((unique && !flag_link) || flag_link) K::Logger::Logg.classe(C_LINK_STT).set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_link));
|
||||
if ((unique && !flag_cas) || flag_cas) K::Logger::Logg.classe(C_CAS_MSGS).set(O_CONSOLE, LogManager::Option::ENABLED, (!invert && flag_cas));
|
||||
|
||||
if (!strs.empty())
|
||||
{
|
||||
K::Logger::Logg2(classe, s, FMT("NOTICE: %s %sthe following console messages: %s.")
|
||||
% (invert ? "Disabling" : "Enabling")
|
||||
% (unique ? "just " : "") % strs.merge(", "));
|
||||
}
|
||||
else
|
||||
{
|
||||
K::Logger::Logg2(classe, s, "WARNING: No valid console messages have been specified, doing nothing.");
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::processLogDisk(switch_stream_handle_t *s, const std::string options, bool invert, bool unique)
|
||||
{
|
||||
class_type classe = ( !s ? C_MESSAGE : C_CLI );
|
||||
|
||||
Strings::vector_type tokens;
|
||||
Strings::tokenize(options, tokens, ",");
|
||||
|
||||
bool flag_errors = false;
|
||||
bool flag_warnings = false;
|
||||
bool flag_messages = false;
|
||||
bool flag_events = false;
|
||||
bool flag_commands = false;
|
||||
bool flag_audio = false;
|
||||
bool flag_modem = false;
|
||||
bool flag_link = false;
|
||||
bool flag_cas = false;
|
||||
bool flag_functions = false;
|
||||
bool flag_threads = false;
|
||||
bool flag_locks = false;
|
||||
bool flag_streams = false;
|
||||
|
||||
Strings::Merger strs;
|
||||
|
||||
for (Strings::vector_type::iterator i = tokens.begin(); i != tokens.end(); i++)
|
||||
{
|
||||
std::string tok = Strings::trim(*i);
|
||||
|
||||
/**/ if ((tok) == "errors") flag_errors = true;
|
||||
else if ((tok) == "warnings") flag_warnings = true;
|
||||
else if ((tok) == "messages") flag_messages = true;
|
||||
else if ((tok) == "events") flag_events = true;
|
||||
else if ((tok) == "commands") flag_commands = true;
|
||||
else if ((tok) == "audio") flag_audio = true;
|
||||
else if ((tok) == "modem") flag_modem = true;
|
||||
else if ((tok) == "link") flag_link = true;
|
||||
else if ((tok) == "cas") flag_cas = true;
|
||||
else if ((tok) == "functions") flag_functions = true;
|
||||
else if ((tok) == "threads") flag_threads = true;
|
||||
else if ((tok) == "locks") flag_locks = true;
|
||||
else if ((tok) == "streams") flag_streams = true;
|
||||
else if ((tok) == "standard")
|
||||
{
|
||||
flag_errors = true;
|
||||
flag_warnings = true;
|
||||
flag_messages = true;
|
||||
flag_link = true;
|
||||
}
|
||||
else if ((tok) == "debugging")
|
||||
{
|
||||
flag_errors = true;
|
||||
flag_warnings = true;
|
||||
flag_messages = true;
|
||||
flag_commands = true;
|
||||
flag_events = true;
|
||||
flag_audio = true;
|
||||
flag_modem = true;
|
||||
flag_link = true;
|
||||
flag_cas = true;
|
||||
flag_functions = true;
|
||||
}
|
||||
else if ((tok) == "all")
|
||||
{
|
||||
flag_errors = true;
|
||||
flag_warnings = true;
|
||||
flag_messages = true;
|
||||
flag_events = true;
|
||||
flag_commands = true;
|
||||
flag_audio = true;
|
||||
flag_modem = true;
|
||||
flag_link = true;
|
||||
flag_cas = true;
|
||||
flag_functions = true;
|
||||
flag_threads = true;
|
||||
flag_locks = true;
|
||||
flag_streams = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* do not show! */
|
||||
continue;
|
||||
}
|
||||
|
||||
strs.add(tok);
|
||||
}
|
||||
|
||||
if ((unique && !flag_errors) || flag_errors) K::Logger::Logg.classe(C_ERROR) .set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_errors));
|
||||
if ((unique && !flag_warnings) || flag_warnings) K::Logger::Logg.classe(C_WARNING) .set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_warnings));
|
||||
if ((unique && !flag_messages) || flag_messages) K::Logger::Logg.classe(C_MESSAGE) .set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_messages));
|
||||
if ((unique && !flag_events) || flag_events) K::Logger::Logg.classe(C_EVENT) .set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_events));
|
||||
if ((unique && !flag_commands) || flag_commands) K::Logger::Logg.classe(C_COMMAND) .set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_commands));
|
||||
if ((unique && !flag_audio) || flag_audio) K::Logger::Logg.classe(C_AUDIO_EV).set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_audio));
|
||||
if ((unique && !flag_modem) || flag_modem) K::Logger::Logg.classe(C_MODEM_EV).set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_modem));
|
||||
if ((unique && !flag_link) || flag_link) K::Logger::Logg.classe(C_LINK_STT).set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_link));
|
||||
if ((unique && !flag_cas) || flag_cas) K::Logger::Logg.classe(C_CAS_MSGS).set(O_GENERIC, LogManager::Option::ENABLED, (!invert && flag_cas));
|
||||
if ((unique && !flag_functions) || flag_functions) K::Logger::Logg.classe(C_DBG_FUNC).enabled(!invert && flag_functions);
|
||||
if ((unique && !flag_threads) || flag_threads) K::Logger::Logg.classe(C_DBG_THRD).enabled(!invert && flag_threads);
|
||||
if ((unique && !flag_locks) || flag_locks) K::Logger::Logg.classe(C_DBG_LOCK).enabled(!invert && flag_locks);
|
||||
if ((unique && !flag_streams) || flag_streams) K::Logger::Logg.classe(C_DBG_STRM).enabled(!invert && flag_streams);
|
||||
|
||||
if (!strs.empty())
|
||||
{
|
||||
K::Logger::Logg2(classe, s, FMT("NOTICE: %s %sthe logging of the following messages: %s.")
|
||||
% (invert ? "Disabling" : "Enabling")
|
||||
% (unique ? "just " : "") % strs.merge(", "));
|
||||
|
||||
if ((flag_streams || flag_locks) && !invert)
|
||||
{
|
||||
K::Logger::Logg2(classe, s, "WARNING: You have enabled *INTENSIVE* debug messages for the Khomp channel!");
|
||||
K::Logger::Logg2(classe, s, "WARNING: Don't *EVER* use these options on production systems, unless you *REALLY* know what you are doing!");
|
||||
}
|
||||
else if ((flag_functions || flag_threads) && !invert)
|
||||
{
|
||||
K::Logger::Logg2(classe, s, "WARNING: You have enabled some debug messages for the Khomp channel.");
|
||||
K::Logger::Logg2(classe, s, "WARNING: Do not use these options on production systems unless you really know what you are doing!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
K::Logger::Logg2(classe, s, "WARNING: No valid log messages have been specified, doing nothing.");
|
||||
}
|
||||
}
|
||||
}
|
||||
772
src/mod/endpoints/mod_khomp/src/opt.cpp
Normal file
772
src/mod/endpoints/mod_khomp/src/opt.cpp
Normal file
@@ -0,0 +1,772 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include "opt.h"
|
||||
#include "globals.h"
|
||||
#include "defs.h"
|
||||
#include "logger.h"
|
||||
#include "spec.h"
|
||||
|
||||
bool Opt::_debug;
|
||||
std::string Opt::_dialplan;
|
||||
std::string Opt::_context;
|
||||
std::string Opt::_user_xfer;
|
||||
std::map < std::string, CSpan > Opt::_spans;
|
||||
Opt::GroupToDestMapType Opt::_groups;
|
||||
Opt::CadencesMapType Opt::_cadences;
|
||||
|
||||
bool Opt::_echo_canceller;
|
||||
bool Opt::_auto_gain_control;
|
||||
bool Opt::_out_of_band_dtmfs;
|
||||
bool Opt::_suppression_delay;
|
||||
bool Opt::_pulse_forwarding;
|
||||
bool Opt::_native_bridge;
|
||||
bool Opt::_recording;
|
||||
bool Opt::_has_ctbus;
|
||||
bool Opt::_fxs_bina;
|
||||
bool Opt::_fxo_send_pre_audio;
|
||||
bool Opt::_drop_collect_call;
|
||||
bool Opt::_ignore_letter_dtmfs;
|
||||
bool Opt::_optimize_audio_path;
|
||||
|
||||
bool Opt::_auto_fax_adjustment;
|
||||
unsigned int Opt::_fax_adjustment_timeout;
|
||||
|
||||
bool Opt::_r2_strict_behaviour;
|
||||
unsigned int Opt::_r2_preconnect_wait;
|
||||
|
||||
unsigned int Opt::_fxs_digit_timeout;
|
||||
|
||||
unsigned int Opt::_transferdigittimeout;
|
||||
|
||||
std::string Opt::_atxfer;
|
||||
std::string Opt::_blindxfer;
|
||||
|
||||
unsigned int Opt::_ringback_co_delay;
|
||||
unsigned int Opt::_ringback_pbx_delay;
|
||||
|
||||
unsigned int Opt::_disconnect_delay;
|
||||
|
||||
int Opt::_input_volume;
|
||||
int Opt::_output_volume;
|
||||
|
||||
Opt::DestVectorType Opt::_fxs_co_dialtone;
|
||||
Opt::OrigToDestMapType Opt::_fxs_hotline;
|
||||
std::string Opt::_fxs_global_orig_base;
|
||||
|
||||
Opt::BoardToOrigMapType Opt::_fxs_orig_base;
|
||||
Opt::BranchToObjectMapType Opt::_fxs_branch_map;
|
||||
Opt::BranchToOptMapType Opt::_branch_options;
|
||||
|
||||
std::string Opt::_global_mohclass;
|
||||
std::string Opt::_global_language;
|
||||
|
||||
std::string Opt::_record_prefix;
|
||||
|
||||
std::string Opt::_context_gsm_call;
|
||||
std::string Opt::_context2_gsm_call;
|
||||
std::string Opt::_context_gsm_sms;
|
||||
std::string Opt::_context_fxo;
|
||||
std::string Opt::_context2_fxo;
|
||||
std::string Opt::_context_fxs;
|
||||
std::string Opt::_context2_fxs;
|
||||
std::string Opt::_context_digital;
|
||||
std::string Opt::_context_pr;
|
||||
|
||||
int Opt::_amaflags;
|
||||
std::string Opt::_callgroup;
|
||||
std::string Opt::_pickupgroup;
|
||||
|
||||
std::string Opt::_accountcode;
|
||||
|
||||
unsigned int Opt::_kommuter_timeout;
|
||||
std::string Opt::_kommuter_activation;
|
||||
|
||||
unsigned int Opt::_audio_packet_size;
|
||||
|
||||
void Opt::initialize(void)
|
||||
{
|
||||
Globals::options.add(ConfigOption("debug", _debug, false));
|
||||
Globals::options.add(ConfigOption("dialplan", _dialplan, "XML"));
|
||||
Globals::options.add(ConfigOption("context", _context, "default"));
|
||||
|
||||
Globals::options.add(ConfigOption("echo-canceller", _echo_canceller, true));
|
||||
Globals::options.add(ConfigOption("auto-gain-control", _auto_gain_control, true));
|
||||
Globals::options.add(ConfigOption("out-of-band-dtmfs", _out_of_band_dtmfs, true));
|
||||
Globals::options.add(ConfigOption("suppression-delay", _suppression_delay, true));
|
||||
Globals::options.add(ConfigOption("pulse-forwarding", _pulse_forwarding, false));
|
||||
Globals::options.add(ConfigOption("native-bridge", _native_bridge, true));
|
||||
Globals::options.add(ConfigOption("recording", _recording, true));
|
||||
Globals::options.add(ConfigOption("has-ctbus", _has_ctbus, false));
|
||||
Globals::options.add(ConfigOption("fxs-bina", _fxs_bina, true));
|
||||
Globals::options.add(ConfigOption("fxo-send-pre-audio", _fxo_send_pre_audio, true));
|
||||
Globals::options.add(ConfigOption("drop-collect-call", _drop_collect_call, false));
|
||||
Globals::options.add(ConfigOption("ignore-letter-dtmfs", _ignore_letter_dtmfs, false));
|
||||
Globals::options.add(ConfigOption("optimize-audio-path", _optimize_audio_path, false));
|
||||
|
||||
Globals::options.add(ConfigOption("auto-fax-adjustment", _auto_fax_adjustment, true));
|
||||
Globals::options.add(ConfigOption("fax-adjustment-timeout", _fax_adjustment_timeout, 30u, 3u, 9999u));
|
||||
|
||||
Globals::options.add(ConfigOption("r2-strict-behaviour", _r2_strict_behaviour, false));
|
||||
Globals::options.add(ConfigOption("r2-preconnect-wait", _r2_preconnect_wait, 250u, 25u, 500u));
|
||||
|
||||
Globals::options.add(ConfigOption("fxs-digit-timeout", _fxs_digit_timeout, 7u, 1u, 30u));
|
||||
|
||||
Globals::options.add(ConfigOption("transferdigittimeout", _transferdigittimeout, 3000u, 0u, 90000u));
|
||||
|
||||
Globals::options.add(ConfigOption("atxfer", _atxfer, ""));
|
||||
Globals::options.add(ConfigOption("blindxfer", _blindxfer, ""));
|
||||
|
||||
Globals::options.add(ConfigOption("delay-ringback-co", _ringback_co_delay, 1500u, 0u, 999000u));
|
||||
Globals::options.add(ConfigOption("delay-ringback-pbx", _ringback_pbx_delay, 2500u, 0u, 999000u));
|
||||
|
||||
Globals::options.add(ConfigOption("disconnect-delay", _disconnect_delay, 0u, 0u, 100000u));
|
||||
|
||||
Globals::options.add(ConfigOption("input-volume", _input_volume, 0, -10, 10));
|
||||
Globals::options.add(ConfigOption("output-volume", _output_volume, 0, -10, 10));
|
||||
|
||||
|
||||
Globals::options.add(ConfigOption("fxs-co-dialtone", ProcessFXSCODialtone(), ""));
|
||||
Globals::options.add(ConfigOption("fxs-global-orig", _fxs_global_orig_base, "0"));
|
||||
|
||||
Globals::options.add(ConfigOption("language", _global_language, ""));
|
||||
Globals::options.add(ConfigOption("mohclass", _global_mohclass, ""));
|
||||
|
||||
Globals::options.add(ConfigOption("record-prefix", ProcessRecordPrefix(), "/var/spool/freeswitch/monitor/"));
|
||||
|
||||
Globals::options.add(ConfigOption("context-fxo", _context_fxo, "khomp-DD-CC"));
|
||||
Globals::options.add(ConfigOption("context-fxo-alt", _context2_fxo, "khomp-DD"));
|
||||
Globals::options.add(ConfigOption("context-fxs", _context_fxs, "khomp-DD-CC"));
|
||||
Globals::options.add(ConfigOption("context-fxs-alt", _context2_fxs, "khomp-DD"));
|
||||
Globals::options.add(ConfigOption("context-gsm-call", _context_gsm_call, "khomp-DD-CC"));
|
||||
Globals::options.add(ConfigOption("context-gsm-call-alt", _context2_gsm_call, "khomp-DD"));
|
||||
Globals::options.add(ConfigOption("context-gsm-sms", _context_gsm_sms, "khomp-sms-DD-CC"));
|
||||
Globals::options.add(ConfigOption("context-digital", _context_digital, "khomp-DD-LL"));
|
||||
Globals::options.add(ConfigOption("context-pr", _context_pr, "khomp-DD-CC"));
|
||||
|
||||
Globals::options.add(ConfigOption("amaflags", ProcessAMAFlags(), "default"));
|
||||
Globals::options.add(ConfigOption("callgroup", _callgroup, "0"));
|
||||
Globals::options.add(ConfigOption("pickupgroup", _pickupgroup, "0"));
|
||||
//Globals::options.add(ConfigOption("callgroup", ProcessCallGroup(), "0"));
|
||||
//Globals::options.add(ConfigOption("pickupgroup", ProcessPickupGroup(), "0"));
|
||||
|
||||
Globals::options.add(ConfigOption("accountcode", _accountcode, ""));
|
||||
|
||||
ConfigOption::string_allowed_type kommuter_allowed;
|
||||
kommuter_allowed.insert("auto");
|
||||
kommuter_allowed.insert("manual");
|
||||
|
||||
Globals::options.add(ConfigOption("kommuter-activation", _kommuter_activation , "auto", kommuter_allowed));
|
||||
Globals::options.add(ConfigOption("kommuter-timeout", _kommuter_timeout ,(unsigned int) 10 , (unsigned int) 0 , (unsigned int) 255));
|
||||
|
||||
Globals::options.add(ConfigOption("audio-packet-length", _audio_packet_size,
|
||||
(unsigned int)KHOMP_READ_PACKET_SIZE, (unsigned int)KHOMP_MIN_READ_PACKET_SIZE, (unsigned int)KHOMP_MAX_READ_PACKET_SIZE, 8u));
|
||||
|
||||
Globals::options.add(ConfigOption("log-to-disk", ProcessLogOptions(O_GENERIC), "standard", false));
|
||||
Globals::options.add(ConfigOption("log-to-console", ProcessLogOptions(O_CONSOLE), "standard", false));
|
||||
|
||||
Globals::options.add(ConfigOption("trace", ProcessTraceOptions(), "", false));
|
||||
Globals::options.add(ConfigOption("user-transfer-digits", _user_xfer, ""));
|
||||
|
||||
/* aliases */
|
||||
Globals::options.synonym("context-gsm", "context-gsm-call");
|
||||
Globals::options.synonym("context-gsm-alt", "context-gsm-call-alt");
|
||||
Globals::options.synonym("echocanceller", "echo-canceller");
|
||||
Globals::options.synonym("dtmfsuppression", "out-of-band-dtmfs");
|
||||
Globals::options.synonym("dtmfsupression", "out-of-band-dtmfs");
|
||||
Globals::options.synonym("pulsedetection", "pulse-forwarding");
|
||||
Globals::options.synonym("r2preconnectwait", "r2-preconnect-wait");
|
||||
Globals::options.synonym("bridge", "native-bridge");
|
||||
Globals::options.synonym("suppressiondelay", "suppression-delay");
|
||||
Globals::options.synonym("log", "log-to-disk");
|
||||
Globals::options.synonym("volume", "output-volume");
|
||||
Globals::options.synonym("disconnectdelay", "disconnect-delay");
|
||||
|
||||
}
|
||||
|
||||
void Opt::obtain(void)
|
||||
{
|
||||
/* everything should start clean! */
|
||||
cleanConfiguration();
|
||||
|
||||
/* reset loaded options */
|
||||
Globals::options.reset();
|
||||
|
||||
loadConfiguration("khomp.conf", NULL);
|
||||
|
||||
/* commit, loading defaults where needed */
|
||||
ConfigOptions::messages_type msgs = Globals::options.commit();
|
||||
|
||||
/* config already full loaded at this point, so we can use our own log system... */
|
||||
for (ConfigOptions::messages_type::iterator i = msgs.begin(); i != msgs.end(); i++)
|
||||
{
|
||||
DBG(FUNC,FMT("%s") % (*i).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::commit(void)
|
||||
{
|
||||
processGroupString();
|
||||
|
||||
/* Check FXS hotlines correcteness */
|
||||
OrigToDestMapType::const_iterator endOfHotlines = _fxs_hotline.end();
|
||||
|
||||
for (OrigToDestMapType::const_iterator i = _fxs_hotline.begin(); i != endOfHotlines; i++)
|
||||
{
|
||||
BranchToObjectMapType::const_iterator j = _fxs_branch_map.find(i->first);
|
||||
|
||||
if (j == _fxs_branch_map.end())
|
||||
{
|
||||
LOG(ERROR, FMT("unable to find branch '%s': hotline '%s' to '%s' is invalid!")
|
||||
% i->first % i->first % i->second);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check FXS options correcteness */
|
||||
BranchToOptMapType::const_iterator endOfOptions = _branch_options.end();
|
||||
|
||||
for (BranchToOptMapType::const_iterator i = _branch_options.begin(); i != endOfOptions; i++)
|
||||
{
|
||||
BranchToObjectMapType::const_iterator j = _fxs_branch_map.find(i->first);
|
||||
if (j == _fxs_branch_map.end())
|
||||
{
|
||||
LOG(ERROR, FMT("unable to find branch '%s' for options '%s'")
|
||||
% i->first % i->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::loadConfiguration(const char *file_name, const char **section, bool show_errors)
|
||||
{
|
||||
switch_xml_t cfg, xml, settings;
|
||||
|
||||
if (!(xml = switch_xml_open_cfg(file_name, &cfg, NULL))) {
|
||||
LOG(ERROR,FMT("Open of %s failed") % file_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Load all the global settings pertinent to all boards */
|
||||
settings = processSimpleXML(cfg,"settings");
|
||||
|
||||
/* Process channel settings */
|
||||
processSimpleXML(settings,"channels");
|
||||
|
||||
/* Process groups settings */
|
||||
processGroupXML(settings);
|
||||
|
||||
/* Process cadence settings */
|
||||
processCadenceXML(settings);
|
||||
|
||||
/* Process fxs branches settings */
|
||||
processFXSBranchesXML(settings);
|
||||
|
||||
/* Process hotlines settings */
|
||||
processFXSHotlines(settings);
|
||||
|
||||
/* Process fxs options settings */
|
||||
processFXSOptions(settings);
|
||||
|
||||
switch_xml_free(xml);
|
||||
}
|
||||
|
||||
void Opt::printConfiguration(switch_stream_handle_t* stream)
|
||||
{
|
||||
for( std::map<std::string, CSpan>::iterator ii=_spans.begin(); ii!=_spans.end(); ++ii )
|
||||
{
|
||||
stream->write_function(stream,
|
||||
"Span: %s.\nDialplan: %s.\nContext: %s.\nDialstring: %s.\n\n",
|
||||
(*ii).first.c_str(),
|
||||
(*ii).second._dialplan.c_str(),
|
||||
(*ii).second._context.c_str(),
|
||||
(*ii).second._dialstring.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::cleanConfiguration(void)
|
||||
{
|
||||
_fxs_orig_base.clear();
|
||||
_fxs_hotline.clear();
|
||||
_fxs_co_dialtone.clear();
|
||||
_fxs_branch_map.clear();
|
||||
_branch_options.clear();
|
||||
|
||||
_groups.clear();
|
||||
_cadences.clear();
|
||||
|
||||
_cadences.insert(CadencesPairType("fast-busy", CadenceType(100,100)));
|
||||
_cadences.insert(CadencesPairType("ringback", CadenceType(1000,4000)));
|
||||
_cadences.insert(CadencesPairType("pbx-dialtone", CadenceType(1000,100)));
|
||||
_cadences.insert(CadencesPairType("co-dialtone", CadenceType(0,0)));
|
||||
_cadences.insert(CadencesPairType("vm-dialtone", CadenceType(1000,100,100,100)));
|
||||
}
|
||||
|
||||
void Opt::ProcessFXSCODialtone::operator()(std::string options) // const
|
||||
{
|
||||
Strings::vector_type tokens;
|
||||
Strings::tokenize(options, tokens, ",");
|
||||
|
||||
for (Strings::vector_type::iterator i = tokens.begin(); i != tokens.end(); i++)
|
||||
_fxs_co_dialtone.push_back(*i);
|
||||
}
|
||||
|
||||
void Opt::ProcessRecordPrefix::operator()(std::string path) // const
|
||||
{
|
||||
if (mkdir(path.c_str(), 493 /* 0755 */) < 0 && errno != EEXIST)
|
||||
{
|
||||
throw ConfigProcessFailure("the default recording directory could not be created.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_record_prefix = path;
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::ProcessAMAFlags::operator()(std::string options)
|
||||
{
|
||||
/*
|
||||
//TODO: Do we need this ?
|
||||
amaflags = ast_cdr_amaflags2int(options.c_str());
|
||||
|
||||
if (amaflags < 0)
|
||||
throw ConfigProcessFailure(STG(FMT("invalid AMA flags: %s") % options));
|
||||
*/
|
||||
}
|
||||
|
||||
//TODO: Check this
|
||||
void Opt::ProcessCallGroup::operator()(std::string options)
|
||||
{
|
||||
// _callgroup = options.c_str();
|
||||
}
|
||||
|
||||
//TODO: Check this
|
||||
void Opt::ProcessPickupGroup::operator()(std::string options)
|
||||
{
|
||||
// _pickupgroup = options.c_str();
|
||||
}
|
||||
|
||||
void Opt::ProcessLogOptions::operator()(std::string options)
|
||||
{
|
||||
switch (_output)
|
||||
{
|
||||
case O_GENERIC:
|
||||
K::Logger::processLogDisk(NULL, options, false, true);
|
||||
break;
|
||||
|
||||
case O_CONSOLE:
|
||||
K::Logger::processLogConsole(NULL, options, false, true);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw ConfigProcessFailure("attempt to process unknown log file configuration");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::ProcessTraceOptions::operator()(std::string options)
|
||||
{
|
||||
Strings::vector_type tokens;
|
||||
Strings::tokenize(options, tokens, ",");
|
||||
|
||||
bool enable_k3l_tracing = false;
|
||||
bool enable_r2_tracing = false;
|
||||
bool enable_rdsi_tracing = false;
|
||||
|
||||
for (Strings::vector_type::iterator i = tokens.begin(); i != tokens.end(); i++)
|
||||
{
|
||||
std::string tok = Strings::trim(*i);
|
||||
|
||||
if (tok == "k3l")
|
||||
{
|
||||
enable_k3l_tracing = true;
|
||||
}
|
||||
else if (tok == "r2")
|
||||
{
|
||||
enable_r2_tracing = true;
|
||||
}
|
||||
else if (tok == "rdsi")
|
||||
{
|
||||
enable_rdsi_tracing = true;
|
||||
}
|
||||
}
|
||||
|
||||
Logfile logfile;
|
||||
|
||||
/* k3l tracing */
|
||||
if (enable_k3l_tracing)
|
||||
{
|
||||
K::LogConfig::set(logfile, "K3L", "Value", enable_k3l_tracing);
|
||||
K::LogConfig::set(logfile, "K3L", "CallProgress", enable_k3l_tracing);
|
||||
K::LogConfig::set(logfile, "K3L", "CallAnalyzer", enable_k3l_tracing);
|
||||
K::LogConfig::set(logfile, "K3L", "CadenceRecog", enable_k3l_tracing);
|
||||
K::LogConfig::set(logfile, "K3L", "CallControl", enable_k3l_tracing);
|
||||
K::LogConfig::set(logfile, "K3L", "Fax", enable_k3l_tracing);
|
||||
}
|
||||
|
||||
/* r2 tracing */
|
||||
K::LogConfig::set(logfile, "R2", "Value", enable_r2_tracing);
|
||||
K::LogConfig::set(logfile, "R2", "Signaling", enable_r2_tracing);
|
||||
K::LogConfig::set(logfile, "R2", "States", enable_r2_tracing);
|
||||
|
||||
/* ISDN tracing */
|
||||
if (enable_rdsi_tracing ^ Globals::flag_trace_rdsi)
|
||||
{
|
||||
K::LogConfig::set(logfile, "ISDN", "Value", enable_rdsi_tracing);
|
||||
K::LogConfig::set(logfile, "ISDN", "Lapd", enable_rdsi_tracing);
|
||||
K::LogConfig::set(logfile, "ISDN", "Q931", enable_rdsi_tracing);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Globals::k3lapi.command(-1, -1, CM_LOG_UPDATE);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
LOG(ERROR,"Error while send command CM_LOG_UPDATE");
|
||||
}
|
||||
}
|
||||
|
||||
switch_xml_t Opt::processSimpleXML(switch_xml_t &xml, const std::string& child_name)
|
||||
{
|
||||
switch_xml_t param, child;
|
||||
|
||||
if ((child = switch_xml_child(xml, child_name.c_str())))
|
||||
{
|
||||
for (param = switch_xml_child(child, "param"); param; param = param->next)
|
||||
{
|
||||
char *var = (char *) switch_xml_attr_soft(param, "name");
|
||||
char *val = (char *) switch_xml_attr_soft(param, "value");
|
||||
|
||||
try
|
||||
{
|
||||
DBG(FUNC,FMT("loading '%s' options: '%s'...")
|
||||
% var
|
||||
% val);
|
||||
|
||||
Globals::options.process(var, val);
|
||||
}
|
||||
catch (ConfigProcessFailure e)
|
||||
{
|
||||
LOG(ERROR,FMT("config processing error: %s. [%s=%s]")
|
||||
% e.msg.c_str()
|
||||
% var
|
||||
% val);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
void Opt::processGroupXML(switch_xml_t &xml)
|
||||
{
|
||||
switch_xml_t param, child;
|
||||
|
||||
if ((child = switch_xml_child(xml,"groups")))
|
||||
{
|
||||
for (param = switch_xml_child(child, "param"); param; param = param->next)
|
||||
{
|
||||
char *var = (char *) switch_xml_attr_soft(param, "name");
|
||||
char *val = (char *) switch_xml_attr_soft(param, "value");
|
||||
|
||||
try
|
||||
{
|
||||
DBG(FUNC,FMT("loading group '%s' options: '%s'...")
|
||||
% var
|
||||
% val);
|
||||
|
||||
_groups.insert(GroupToDestPairType(var,val));
|
||||
|
||||
}
|
||||
catch (ConfigProcessFailure e)
|
||||
{
|
||||
LOG(ERROR,FMT("config processing error: %s. [%s=%s]")
|
||||
% e.msg.c_str()
|
||||
% var
|
||||
% val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::processCadenceXML(switch_xml_t &xml)
|
||||
{
|
||||
switch_xml_t param, child;
|
||||
|
||||
if ((child = switch_xml_child(xml,"cadences")))
|
||||
{
|
||||
for (param = switch_xml_child(child, "param"); param; param = param->next)
|
||||
{
|
||||
char *var = (char *) switch_xml_attr_soft(param, "name");
|
||||
char *val = (char *) switch_xml_attr_soft(param, "value");
|
||||
|
||||
try
|
||||
{
|
||||
DBG(FUNC,FMT("loading cadences '%s' options: '%s'...")
|
||||
% var
|
||||
% val);
|
||||
|
||||
Strings::vector_type values;
|
||||
Strings::tokenize(val, values, " :,.");
|
||||
|
||||
if (values.size() != 2 && values.size() != 4)
|
||||
{
|
||||
LOG(WARNING, FMT("file '%s': wrong number of arguments at cadence '%s'!")
|
||||
% "khomp.conf.xml"
|
||||
% var);
|
||||
}
|
||||
else
|
||||
{
|
||||
CadenceType cadence;
|
||||
|
||||
cadence.ring = Strings::toulong(Strings::trim(values[0]));
|
||||
cadence.ring_s = Strings::toulong(Strings::trim(values[1]));
|
||||
|
||||
if (values.size() == 4)
|
||||
{
|
||||
cadence.ring_ext = Strings::toulong(Strings::trim(values[2]));
|
||||
cadence.ring_ext_s = Strings::toulong(Strings::trim(values[3]));
|
||||
}
|
||||
|
||||
_cadences.erase(var); /* erases previous (possibly predefined) cadence */
|
||||
_cadences.insert(CadencesPairType(var, cadence));
|
||||
}
|
||||
}
|
||||
catch (ConfigProcessFailure e)
|
||||
{
|
||||
LOG(ERROR,FMT("config processing error: %s. [%s=%s]")
|
||||
% e.msg.c_str()
|
||||
% var
|
||||
% val);
|
||||
|
||||
}
|
||||
catch (Strings::invalid_value e)
|
||||
{
|
||||
LOG(ERROR,FMT("file '%s': number expected at cadence '%s', got '%s'.")
|
||||
% "khomp.conf.xml"
|
||||
% var
|
||||
% e.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::processFXSBranchesXML(switch_xml_t &xml)
|
||||
{
|
||||
switch_xml_t param, child;
|
||||
|
||||
if ((child = switch_xml_child(xml,"fxs-branches")))
|
||||
{
|
||||
for (param = switch_xml_child(child, "param"); param; param = param->next)
|
||||
{
|
||||
char *var = (char *) switch_xml_attr_soft(param, "name");
|
||||
char *val = (char *) switch_xml_attr_soft(param, "value");
|
||||
|
||||
|
||||
DBG(FUNC,FMT("loading fxs-branches '%s' options: '%s'...")
|
||||
% var
|
||||
% val);
|
||||
|
||||
try
|
||||
{
|
||||
unsigned long orig_number = Strings::toulong(var);
|
||||
|
||||
/* gcc! stop complaining! */
|
||||
(void)orig_number;
|
||||
|
||||
Strings::vector_type boards;
|
||||
Strings::tokenize(val, boards, " :,");
|
||||
|
||||
if (boards.size() < 1)
|
||||
{
|
||||
DBG(FUNC,FMT("file '%s': orig number '%s' without any board!")
|
||||
% "khomp.conf.xml"
|
||||
% var);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Strings::vector_type::iterator iter = boards.begin(); iter != boards.end(); iter++)
|
||||
{
|
||||
/* quebrar em strings por vírgula/espaço */
|
||||
unsigned long serial_number = Strings::toulong(Strings::trim(*iter));
|
||||
|
||||
bool found = false;
|
||||
|
||||
for (unsigned int device = 0; device < Globals::k3lapi.device_count(); device++)
|
||||
{
|
||||
K3L_DEVICE_CONFIG & conf = Globals::k3lapi.device_config(device);
|
||||
|
||||
std::string str_serial(conf.SerialNumber);
|
||||
|
||||
if (Strings::toulong(str_serial) == serial_number)
|
||||
{
|
||||
found = true;
|
||||
|
||||
_fxs_orig_base.insert(BoardToOrigPairType(device, var));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
LOG(WARNING, FMT("file 'khomp.conf.xml': board with serial number '%u' not found!")
|
||||
% serial_number);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ConfigProcessFailure e)
|
||||
{
|
||||
LOG(ERROR,FMT("config processing error: %s. [%s=%s]")
|
||||
% e.msg.c_str()
|
||||
% var
|
||||
% val);
|
||||
}
|
||||
catch (Strings::invalid_value & e)
|
||||
{
|
||||
LOG(ERROR, D("invalid numeric value: %s") % e.value());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
LOG(ERROR, D("config processing error..."));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::processFXSHotlines(switch_xml_t &xml)
|
||||
{
|
||||
switch_xml_t param, child;
|
||||
|
||||
if ((child = switch_xml_child(xml,"fxs-hotlines")))
|
||||
{
|
||||
for (param = switch_xml_child(child, "param"); param; param = param->next)
|
||||
{
|
||||
char *var = (char *) switch_xml_attr_soft(param, "name");
|
||||
char *val = (char *) switch_xml_attr_soft(param, "value");
|
||||
|
||||
try
|
||||
{
|
||||
DBG(FUNC,FMT("loading fxs-hotlines '%s' options: '%s'...")
|
||||
% var
|
||||
% val);
|
||||
|
||||
unsigned long orig_number = Strings::toulong(var);
|
||||
|
||||
(void)orig_number; /* stop complaining! */
|
||||
|
||||
_fxs_hotline.insert(OrigToDestPairType(var, val));
|
||||
|
||||
}
|
||||
catch (ConfigProcessFailure e)
|
||||
{
|
||||
LOG(ERROR,FMT("config processing error: %s. [%s=%s]")
|
||||
% e.msg.c_str()
|
||||
% var
|
||||
% val);
|
||||
}
|
||||
catch (Strings::invalid_value e)
|
||||
{
|
||||
LOG(WARNING, FMT("file '%s': number expected, got '%s'!")
|
||||
% "khomp.conf.xml" % e.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Opt::processFXSOptions(switch_xml_t &xml)
|
||||
{
|
||||
switch_xml_t param, child;
|
||||
|
||||
if ((child = switch_xml_child(xml,"fxs-options")))
|
||||
{
|
||||
for (param = switch_xml_child(child, "param"); param; param = param->next)
|
||||
{
|
||||
char *var = (char *) switch_xml_attr_soft(param, "name");
|
||||
char *val = (char *) switch_xml_attr_soft(param, "value");
|
||||
|
||||
try
|
||||
{
|
||||
DBG(FUNC,FMT("loading fxs-options '%s' options: '%s'...")
|
||||
% var
|
||||
% val);
|
||||
|
||||
Strings::vector_type branches;
|
||||
Strings::tokenize(var, branches, " ,");
|
||||
|
||||
if (branches.size() < 1)
|
||||
{
|
||||
//TODO: Get linenumber
|
||||
LOG(WARNING, FMT("file '%s': no branches specified in line %d!")
|
||||
% "khomp.conf.xml" % 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Strings::vector_type::iterator iter = branches.begin();
|
||||
iter != branches.end(); iter++)
|
||||
{
|
||||
std::string tmp_branch = Strings::trim(*iter);
|
||||
|
||||
unsigned long branch_number = Strings::toulong(tmp_branch);
|
||||
(void) branch_number;
|
||||
|
||||
_branch_options.insert(BranchToOptPairType(tmp_branch, val));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ConfigProcessFailure e)
|
||||
{
|
||||
LOG(ERROR,FMT("config processing error: %s. [%s=%s]")
|
||||
% e.msg.c_str()
|
||||
% var
|
||||
% val);
|
||||
}
|
||||
catch (Strings::invalid_value e)
|
||||
{
|
||||
LOG(WARNING, FMT("file '%s': number expected, got '%s'!")
|
||||
% "khomp.conf.xml" % e.value().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
885
src/mod/endpoints/mod_khomp/src/spec.cpp
Normal file
885
src/mod/endpoints/mod_khomp/src/spec.cpp
Normal file
@@ -0,0 +1,885 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <regex.hpp>
|
||||
#include "spec.h"
|
||||
#include "logger.h"
|
||||
#include "khomp_pvt.h"
|
||||
#include "khomp_pvt_kxe1.h"
|
||||
|
||||
/************************** Forward declaration *******************************/
|
||||
static SpecRetType processSpecAtom(std::string &, SpecFlagsType &, SpecFunType &);
|
||||
static SpecRetType processSpecAtoms(std::string &, SpecFlagsType &, SpecFunType &);
|
||||
|
||||
static bool processCallChannelString(std::string &, Board::KhompPvt *&, int *, bool need_free = true);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static SpecRetType processSpecAtom(std::string & atom, SpecFlagsType & flags, SpecFunType & fun)
|
||||
{
|
||||
std::string allocstr = Strings::trim(atom);
|
||||
|
||||
DBG(FUNC, D("allocation string 'atom': %s") % allocstr.c_str());
|
||||
|
||||
/* check if is a group */
|
||||
if (allocstr.size() >= 1 && (allocstr[0] == 'g' || allocstr[0] == 'G'))
|
||||
{
|
||||
std::string group_name = allocstr.substr(1);
|
||||
|
||||
Opt::GroupToDestMapType::iterator it = Opt::_groups.find(group_name);
|
||||
|
||||
if (it == Opt::_groups.end())
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': no valid group found!") % allocstr.c_str());
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
allocstr = it->second;
|
||||
return processSpecAtoms(allocstr, flags, fun);
|
||||
}
|
||||
|
||||
Regex::Expression e("(((([bB])[ ]*([0-9]+))|(([sS])[ ]*([0-9]+)))[ ]*(([cClL])[ ]*([0-9]+)[ ]*([-][ ]*([0-9]+))?)?)|(([rR])[ ]*([0-9]+)[ ]*([-][ ]*([0-9]+))?)", Regex::E_EXTENDED);
|
||||
|
||||
Regex::Match what(allocstr, e);
|
||||
|
||||
if (!what.matched())
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': this is not a valid expression.") % allocstr.c_str());
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
bool reverse = true;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
unsigned long int board_id = UINT_MAX;
|
||||
|
||||
if (what.matched(3)) //matched [bB]
|
||||
{
|
||||
board_id = Strings::toulong(what.submatch(5));
|
||||
|
||||
DBG(FUNC, D("board matched: %d") % board_id);
|
||||
|
||||
|
||||
if (board_id >= Globals::k3lapi.device_count())
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': no such board '%d'.") % allocstr.c_str() % board_id);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
switch ((what.submatch(4))[0])
|
||||
{
|
||||
case 'b': reverse = false; break;
|
||||
case 'B': reverse = true; break;
|
||||
}
|
||||
}
|
||||
|
||||
else if (what.matched(6)) //matched [sS]
|
||||
{
|
||||
unsigned int serial_id = Strings::toulong(what.submatch(8));
|
||||
|
||||
DBG(FUNC, D("serial matched: %d") % serial_id);
|
||||
|
||||
for (unsigned int i = 0; i < Globals::k3lapi.device_count(); i++)
|
||||
{
|
||||
|
||||
K3L_DEVICE_CONFIG conf = Globals::k3lapi.device_config(i);
|
||||
|
||||
unsigned int tmp = Strings::toulong((const char *)conf.SerialNumber);
|
||||
|
||||
if (tmp == serial_id)
|
||||
{
|
||||
board_id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (board_id == UINT_MAX)
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': there is no board with serial '%04d'.") % allocstr.c_str() % serial_id);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
switch ((what.submatch(7))[0])
|
||||
{
|
||||
case 's': reverse = false; break;
|
||||
case 'S': reverse = true; break;
|
||||
}
|
||||
}
|
||||
else if (what.matched(14)) //matched [rR]
|
||||
{
|
||||
std::string base_addr = what.submatch(16);
|
||||
|
||||
unsigned int branch_id = Strings::toulong(base_addr);
|
||||
|
||||
if (what.matched(17))
|
||||
{
|
||||
unsigned int branch2_id = Strings::toulong(what.submatch(18));
|
||||
|
||||
DBG(FUNC, D("branch range matched (%d to %d)") % branch_id % branch2_id);
|
||||
|
||||
switch ((what.submatch(15))[0])
|
||||
{
|
||||
case 'r': reverse = false; break;
|
||||
case 'R': reverse = true; break;
|
||||
}
|
||||
|
||||
if (!reverse)
|
||||
{
|
||||
for (unsigned int i = branch_id; i <= branch2_id; i++)
|
||||
{
|
||||
std::string call_addr = BoardE1::KhompPvtFXS::padOrig(base_addr, i - branch_id);
|
||||
Opt::BranchToObjectMapType::iterator i = Opt::_fxs_branch_map.find(call_addr);
|
||||
|
||||
if (i == Opt::_fxs_branch_map.end())
|
||||
{
|
||||
LOG(WARNING, FMT("invalid value '%s': there is no such branch number.") % call_addr);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
if (!fun(i->second.first, i->second.second, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned int i = branch2_id; i >= branch_id; i--)
|
||||
{
|
||||
std::string call_addr = BoardE1::KhompPvtFXS::padOrig(base_addr, i - branch_id);
|
||||
|
||||
Opt::BranchToObjectMapType::iterator i = Opt::_fxs_branch_map.find(call_addr);
|
||||
|
||||
if (i == Opt::_fxs_branch_map.end())
|
||||
{
|
||||
LOG(WARNING, FMT("invalid value '%s': there is no such branch number.") % call_addr);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
if (!fun(i->second.first, i->second.second, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(FUNC, D("branch matched: %s") % base_addr);
|
||||
|
||||
Opt::BranchToObjectMapType::iterator i = Opt::_fxs_branch_map.find(base_addr);
|
||||
|
||||
if (i == Opt::_fxs_branch_map.end())
|
||||
{
|
||||
LOG(WARNING, FMT("invalid value '%s': there is no such branch number.") % base_addr);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
if (!fun(i->second.first, i->second.second, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': unknown allocation method.") % allocstr.c_str());
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
if (what.matched(9)) // matched something about links/channels [cClL]n|[cC]n-m
|
||||
{
|
||||
DBG(FUNC, D("channel/link matched"));
|
||||
unsigned long int object_id = Strings::toulong(what.submatch(11));
|
||||
|
||||
if (what.matched(12))
|
||||
{
|
||||
|
||||
DBG(FUNC, D("channel range matched")); // matched [cC]n-m
|
||||
|
||||
if ((what.submatch(10))[0] != 'c' && (what.submatch(10))[0] != 'C')
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': range just allowed for channels.") % allocstr.c_str());
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
unsigned long int object2_id = Strings::toulong(what.submatch(13));
|
||||
|
||||
DBG(FUNC, D("(d=%d,lo=%d,up=%d,r=%s) c") % board_id % object_id % object2_id % (reverse ? "true" : "false"));
|
||||
|
||||
if (reverse)
|
||||
{
|
||||
for (unsigned int obj = std::min<unsigned int>(object2_id + 1, Globals::k3lapi.channel_count(board_id)); obj > 0 && obj > object_id; obj--)
|
||||
{
|
||||
if (!fun(board_id, obj-1, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned int obj = object_id; obj < std::min<unsigned int>(Globals::k3lapi.channel_count(board_id), object2_id + 1); obj++)
|
||||
{
|
||||
if (!fun(board_id, obj, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else // matched [cClL]n
|
||||
{
|
||||
DBG(FUNC, D("individual channel/link matched"));
|
||||
|
||||
switch ((what.submatch(10))[0])
|
||||
{
|
||||
case 'C':
|
||||
case 'c':
|
||||
DBG(FUNC, D("individual channel matched"));
|
||||
|
||||
if (!fun(board_id, object_id, flags))
|
||||
return SPR_SUCCESS;
|
||||
|
||||
return SPR_CONTINUE;
|
||||
|
||||
case 'l':
|
||||
case 'L':
|
||||
DBG(FUNC, D("individual link matched"));
|
||||
|
||||
switch (Globals::k3lapi.device_type(board_id))
|
||||
{
|
||||
case kdtE1:
|
||||
case kdtPR:
|
||||
case kdtE1GW:
|
||||
case kdtE1IP:
|
||||
case kdtE1Spx:
|
||||
#if K3L_AT_LEAST(2,1,0)
|
||||
case kdtE1FXSSpx:
|
||||
#endif
|
||||
{
|
||||
unsigned int link_first = object_id * 30;
|
||||
unsigned int link_final = ((object_id + 1) * 30);
|
||||
|
||||
if (reverse)
|
||||
{
|
||||
for (unsigned int obj = std::min(link_final, Globals::k3lapi.channel_count(board_id)); obj > 0 && obj > link_first; obj--)
|
||||
{
|
||||
if (!fun(board_id, obj-1, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned int obj = link_first; obj < std::min(Globals::k3lapi.channel_count(board_id), link_final); obj++)
|
||||
{
|
||||
if (!fun(board_id, obj, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
return SPR_CONTINUE;
|
||||
|
||||
}
|
||||
|
||||
default:
|
||||
LOG(ERROR, FMT("invalid dial string '%s': board '%d' does not have links.")
|
||||
% allocstr.c_str() % board_id);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
default:
|
||||
LOG(ERROR, FMT("invalid dial string '%s': invalid object specification.") % allocstr.c_str());
|
||||
return SPR_FAIL;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (what.matched(3) || what.matched(6)) // matched something about boards [bBsS]
|
||||
{
|
||||
if (reverse)
|
||||
{
|
||||
for (unsigned int obj = Globals::k3lapi.channel_count(board_id); obj > 0; obj--)
|
||||
{
|
||||
if (!fun(board_id, obj-1, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned int obj = 0; obj < Globals::k3lapi.channel_count(board_id); obj++)
|
||||
{
|
||||
if (!fun(board_id, obj, flags))
|
||||
return SPR_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Strings::invalid_value e)
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': invalid numeric value specified.") % allocstr.c_str());
|
||||
return SPR_FAIL;
|
||||
}
|
||||
catch (Function::EmptyFunction & e)
|
||||
{
|
||||
LOG(ERROR, FMT("invalid function."));
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
return SPR_CONTINUE;
|
||||
}
|
||||
|
||||
static SpecRetType processSpecAtoms(std::string & gotatoms, SpecFlagsType & flags, SpecFunType & fun)
|
||||
{
|
||||
std::string atoms(gotatoms);
|
||||
|
||||
DBG(FUNC, D("allocation string 'atoms': %s") % atoms);
|
||||
|
||||
|
||||
if (!atoms.empty() && (atoms.at(0) == '*')) //so it is a "cyclical" allocation
|
||||
{
|
||||
atoms.erase(0, 1);
|
||||
|
||||
if (flags & SPF_FIRST)
|
||||
{
|
||||
if (!(flags & SPF_CYCLIC))
|
||||
{
|
||||
DBG(FUNC, D("got a cyclic/fair allocation (%s), priorizing less used channels...") % atoms);
|
||||
flags |= SPF_CYCLIC;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(FUNC, D("cyclic/fair allocation NOT at first string, ignoring..."));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Strings::vector_type boundaries;
|
||||
Strings::tokenize(atoms, boundaries, "+");
|
||||
|
||||
if (boundaries.size() < 1)
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': no allocation string found!") % atoms);
|
||||
return SPR_FAIL;
|
||||
}
|
||||
|
||||
for (Strings::vector_type::iterator iter = boundaries.begin(); iter != boundaries.end(); iter++)
|
||||
{
|
||||
switch (processSpecAtom(*iter, flags, fun))
|
||||
{
|
||||
// if had some error processing dialstring, bail out..
|
||||
case SPR_FAIL:
|
||||
return SPR_FAIL;
|
||||
|
||||
// found someone? return ASAP!
|
||||
case SPR_SUCCESS:
|
||||
return SPR_SUCCESS;
|
||||
|
||||
// else, keep going..
|
||||
case SPR_CONTINUE:
|
||||
break;
|
||||
}
|
||||
|
||||
flags &= ~SPF_FIRST;
|
||||
}
|
||||
|
||||
/* found nothing, but this is NOT an error */
|
||||
return SPR_CONTINUE;
|
||||
}
|
||||
|
||||
struct funProcessCallChannelString
|
||||
{
|
||||
funProcessCallChannelString(int *cause, bool need_free)
|
||||
: _cause(cause), _need_free(need_free),
|
||||
_all_fail(true), //_fxs_only(true),
|
||||
_pvt(NULL)
|
||||
{};
|
||||
|
||||
bool operator()(unsigned int dev, unsigned int obj, SpecFlagsType & flags)
|
||||
{
|
||||
try
|
||||
{
|
||||
Board::KhompPvt *tmp = Board::get(dev, obj);
|
||||
|
||||
// used for cause definition
|
||||
if (_all_fail)
|
||||
_all_fail = (tmp ? !tmp->isOK() : true);
|
||||
}
|
||||
catch (K3LAPI::invalid_channel & err)
|
||||
{
|
||||
_all_fail = true;
|
||||
}
|
||||
|
||||
//used for precise cause definition
|
||||
//if (_fxs_only)
|
||||
// _fxs_only = (tmp ? tmp->is_fxs() : false);
|
||||
|
||||
if (flags & SPF_CYCLIC)
|
||||
{
|
||||
Board::queueAddChannel(_channels, dev, obj);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_pvt = Board::findFree(dev, obj, _need_free);
|
||||
return (_pvt == NULL);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Board::KhompPvt * pvt(SpecFlagsType & flags)
|
||||
{
|
||||
|
||||
if ((flags & SPF_CYCLIC) && !_pvt)
|
||||
{
|
||||
//we have no pvt 'till now, lets find a suitable one..
|
||||
_pvt = Board::queueFindFree(_channels);
|
||||
}
|
||||
|
||||
if (!_pvt && _cause && !(*_cause))
|
||||
{
|
||||
|
||||
if (_all_fail)
|
||||
{
|
||||
// all channels are in fail
|
||||
*_cause = SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
|
||||
}
|
||||
else
|
||||
{
|
||||
//if (_fxs_only)
|
||||
// *_cause = SWITCH_CAUSE_USER_BUSY;
|
||||
//else
|
||||
*_cause = SWITCH_CAUSE_SWITCH_CONGESTION;
|
||||
}
|
||||
}
|
||||
|
||||
return _pvt;
|
||||
}
|
||||
|
||||
int * _cause;
|
||||
|
||||
bool _need_free;
|
||||
|
||||
bool _all_fail;
|
||||
//bool _fxs_only; //TODO: futuro implementar a parte de FXS
|
||||
|
||||
Board::KhompPvt * _pvt;
|
||||
Board::PriorityCallQueue _channels;
|
||||
};
|
||||
|
||||
struct funProcessSMSChannelString
|
||||
{
|
||||
|
||||
funProcessSMSChannelString(int *cause)
|
||||
: _cause(cause), _all_fail(true), _pvt(NULL)
|
||||
{};
|
||||
|
||||
bool operator()(unsigned int dev, unsigned int obj, SpecFlagsType & flags)
|
||||
{
|
||||
Board::KhompPvt *pvt = Board::findFree(dev, obj);
|
||||
|
||||
if (pvt)
|
||||
{
|
||||
// found something? check if its GSM
|
||||
if (pvt->application(SMS_CHECK, NULL, NULL))
|
||||
{
|
||||
// used for cause definition
|
||||
if (_all_fail)
|
||||
_all_fail = (pvt ? !pvt->isOK() : true);
|
||||
|
||||
if (flags & SPF_CYCLIC)
|
||||
{
|
||||
Board::queueAddChannel(_channels, dev, obj);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_pvt = pvt;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// not gsm, return ASAP and stop search
|
||||
LOG(ERROR, PVT_FMT(pvt->target(), "channel is NOT a GSM channel! unable to send message!"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// keep searching
|
||||
return true;
|
||||
}
|
||||
|
||||
Board::KhompPvt * pvt(SpecFlagsType & flags)
|
||||
{
|
||||
if ((flags & SPF_CYCLIC) && !_pvt)
|
||||
{
|
||||
// we have no pvt 'till now, lets find a suitable one..
|
||||
_pvt = Board::queueFindFree(_channels);
|
||||
}
|
||||
|
||||
if (!_pvt && _cause && !(*_cause))
|
||||
{
|
||||
if (_all_fail)
|
||||
{
|
||||
// all channels are in fail
|
||||
*_cause = SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, congestion..
|
||||
*_cause = SWITCH_CAUSE_SWITCH_CONGESTION;
|
||||
}
|
||||
}
|
||||
|
||||
return _pvt;
|
||||
}
|
||||
|
||||
int * _cause;
|
||||
|
||||
bool _need_free;
|
||||
|
||||
bool _all_fail;
|
||||
|
||||
Board::KhompPvt * _pvt;
|
||||
Board::PriorityCallQueue _channels;
|
||||
};
|
||||
|
||||
struct FunProcessGroupString
|
||||
{
|
||||
/* used for group processing */
|
||||
FunProcessGroupString(std::string ctx)
|
||||
: _ctx(ctx) {};
|
||||
|
||||
FunProcessGroupString(const FunProcessGroupString &o)
|
||||
: _ctx(o._ctx) {};
|
||||
|
||||
bool operator()(unsigned int dev, unsigned int obj, SpecFlagsType & flags) const
|
||||
{
|
||||
try
|
||||
{
|
||||
Board::KhompPvt * pvt = Board::get(dev,obj);
|
||||
|
||||
DBG(CONF, FMT("loading context %s for channel %d,%d") % _ctx % dev % obj);
|
||||
|
||||
if (pvt) pvt->_group_context = _ctx;
|
||||
}
|
||||
catch (K3LAPI::invalid_channel & err)
|
||||
{
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
std::string _ctx;
|
||||
};
|
||||
|
||||
static bool processCallChannelString(std::string & str, Board::KhompPvt *& pvt, int * cause, bool need_free)
|
||||
{
|
||||
funProcessCallChannelString proc(cause, need_free);
|
||||
|
||||
SpecFlagsType flags = SPF_FIRST;
|
||||
SpecFunType fun(proc, false); // = ReferenceWrapper < SpecFunType > (proc);
|
||||
|
||||
bool ret = true;
|
||||
|
||||
switch (processSpecAtoms(str, flags, fun))
|
||||
{
|
||||
case SPR_FAIL:
|
||||
DBG(FUNC, D("SPR_FAIL: %p") % cause);
|
||||
|
||||
if (cause)
|
||||
*cause = SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
|
||||
|
||||
ret = false;
|
||||
break;
|
||||
case SPR_SUCCESS:
|
||||
case SPR_CONTINUE:
|
||||
pvt = proc.pvt(flags);
|
||||
DBG(FUNC, D("pvt = %p") % pvt);
|
||||
|
||||
if (!pvt && cause && !(*cause))
|
||||
*cause = SWITCH_CAUSE_INTERWORKING;
|
||||
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool processSMSChannelString(std::string & str, Board::KhompPvt *& pvt, int *cause)
|
||||
{
|
||||
funProcessSMSChannelString proc(cause);
|
||||
|
||||
SpecFlagsType flags = SPF_FIRST;
|
||||
SpecFunType fun(proc, false);
|
||||
|
||||
switch (processSpecAtoms(str, flags, fun))
|
||||
{
|
||||
case SPR_FAIL:
|
||||
DBG(FUNC, FMT("SPR_FAIL: %p") % cause);
|
||||
if (cause)
|
||||
*cause = SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
|
||||
return false;
|
||||
|
||||
case SPR_SUCCESS:
|
||||
case SPR_CONTINUE:
|
||||
pvt = proc.pvt(flags);
|
||||
DBG(FUNC, FMT("pvt = %p") % pvt);
|
||||
|
||||
if (!pvt && cause && !(*cause))
|
||||
*cause = SWITCH_CAUSE_INTERWORKING;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Board::KhompPvt * processDialString (const char *dial_charv, int *cause)
|
||||
{
|
||||
DBG(FUNC, D("c (%p, %p)") % dial_charv % cause);
|
||||
std::string dial_string(dial_charv);
|
||||
Strings::vector_type dial_args;
|
||||
|
||||
Strings::tokenize(dial_string, dial_args, "/");
|
||||
|
||||
Board::KhompPvt *pvt = NULL;
|
||||
|
||||
DBG(FUNC, FMT("processing dial string [%d] : '%s'") % dial_args.size() % dial_string);
|
||||
|
||||
if ((dial_args.size() < 1 && dial_args.size() > 3))
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': wrong number of separators ('/').") % dial_string);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool dial_string_ok = processCallChannelString(dial_args[0], pvt, cause, true);
|
||||
|
||||
if (pvt == NULL)
|
||||
{
|
||||
if (dial_string_ok)
|
||||
LOG(WARNING, "unable to allocate channel -- no free channel found!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DBG(FUNC, PVT_FMT(pvt->target(), "pvt %p") % pvt);
|
||||
|
||||
unsigned int opt_size = (pvt->hasNumberDial() ? 3 : 2);
|
||||
unsigned int opt_arg = opt_size - 1;
|
||||
|
||||
if (dial_args.size() == opt_size)
|
||||
{
|
||||
Strings::vector_type options_args;
|
||||
Strings::tokenize (dial_args[opt_arg], options_args, ":");
|
||||
|
||||
for (Strings::vector_type::iterator opt_arg = options_args.begin();
|
||||
opt_arg != options_args.end(); opt_arg++)
|
||||
{
|
||||
std::string str = (*opt_arg);
|
||||
|
||||
Strings::vector_type option_item;
|
||||
Strings::tokenize (str, option_item, "=");
|
||||
|
||||
switch (option_item.size())
|
||||
{
|
||||
case 2:
|
||||
{
|
||||
std::string index (option_item[0]);
|
||||
std::string value (option_item[1]);
|
||||
|
||||
if(pvt->call()->process(index, value))
|
||||
continue;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 1:
|
||||
{
|
||||
std::string index (option_item[0]);
|
||||
|
||||
if(pvt->call()->process(index))
|
||||
continue;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
LOG(ERROR, FMT("invalid option specification '%s'.") % str);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
LOG(ERROR, FMT("unknown option name '%s', ignoring...") % str);
|
||||
}
|
||||
}
|
||||
|
||||
if(pvt->hasNumberDial())
|
||||
{
|
||||
if (dial_args.size() <= 1)
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': missing destination number!") % dial_string);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string name ("dest");
|
||||
std::string value (dial_args[1]);
|
||||
|
||||
pvt->call()->process(name, value);
|
||||
|
||||
//pvt->call()->dest_addr = dial_args[1];
|
||||
}
|
||||
|
||||
return pvt;
|
||||
};
|
||||
|
||||
Board::KhompPvt * processSMSString (const char *sms_charv, int *cause)
|
||||
{
|
||||
std::string sms_string(sms_charv);
|
||||
Strings::vector_type sms_args;
|
||||
|
||||
Strings::tokenize(sms_string, sms_args, "/|,", 3); // '/' is a backward compatibility feature!
|
||||
|
||||
DBG(FUNC, FMT("processing SMS string [%d] : '%s'") % sms_args.size() % sms_string);
|
||||
|
||||
if (sms_args.size () != 3)
|
||||
{
|
||||
LOG(ERROR, FMT("invalid dial string '%s': wrong number of separators.") % sms_string);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Board::KhompPvt *pvt = NULL;
|
||||
|
||||
bool dial_string_ok = processSMSChannelString(sms_args[0], pvt, cause);
|
||||
|
||||
if (pvt == NULL)
|
||||
{
|
||||
if (dial_string_ok)
|
||||
{
|
||||
LOG(WARNING, "unable to allocate channel -- no free channel found!");
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!pvt->application(SMS_CHECK, NULL, NULL))
|
||||
{
|
||||
LOG(ERROR, PVT_FMT(pvt->target(), "allocated channel is NOT a GSM channel! unable to send message!"));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
std::string dest(sms_args[1]);
|
||||
|
||||
bool conf = false;
|
||||
|
||||
if (dest[0] == '!')
|
||||
{
|
||||
dest.erase(0,1);
|
||||
conf = true;
|
||||
}
|
||||
|
||||
if (dest[dest.size()-1] == '!')
|
||||
{
|
||||
dest.erase(dest.size()-1,1);
|
||||
conf = true;
|
||||
}
|
||||
|
||||
// get options/values
|
||||
pvt->send_sms.sms_dest = dest;
|
||||
pvt->send_sms.sms_conf = conf;
|
||||
pvt->send_sms.sms_body = sms_args[2];
|
||||
*/
|
||||
|
||||
return pvt;
|
||||
};
|
||||
|
||||
void processGroupString()
|
||||
{
|
||||
for (Opt::GroupToDestMapType::iterator i = Opt::_groups.begin(); i != Opt::_groups.end(); i++)
|
||||
{
|
||||
const std::string & name = (*i).first;
|
||||
std::string & opts = (*i).second;
|
||||
|
||||
Strings::vector_type tokens;
|
||||
Strings::tokenize(opts, tokens, ",:", 2);
|
||||
|
||||
if (tokens.size() != 2 && tokens.size() != 1)
|
||||
{
|
||||
LOG(WARNING, FMT("wrong number of arguments at group '%s', ignoring group!\n") % name.c_str());
|
||||
opts.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tokens.size() < 2)
|
||||
continue;
|
||||
FunProcessGroupString proc(tokens[1]);
|
||||
|
||||
SpecFlagsType flags = SPF_FIRST;
|
||||
SpecFunType fun(proc, false);
|
||||
|
||||
switch (processSpecAtoms(tokens[0], flags, fun))
|
||||
{
|
||||
case SPR_CONTINUE:
|
||||
// remove context from spec
|
||||
opts = tokens[0];
|
||||
|
||||
// log this!
|
||||
DBG(CONF, FMT("group '%s' is now '%s', with context '%s'...")
|
||||
% name % tokens[0] % tokens[1]);
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG(WARNING, FMT("skipping group '%s', bad configuration!\n") % name.c_str());
|
||||
|
||||
// "zero" group
|
||||
opts.clear();
|
||||
|
||||
// log this!
|
||||
DBG(CONF, FMT("group '%s' have misconfigured options, ignoring...") % name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
393
src/mod/endpoints/mod_khomp/src/utils.cpp
Normal file
393
src/mod/endpoints/mod_khomp/src/utils.cpp
Normal file
@@ -0,0 +1,393 @@
|
||||
/*******************************************************************************
|
||||
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License
|
||||
Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file
|
||||
under the MPL, indicate your decision by deleting the provisions above and
|
||||
replace them with the notice and other provisions required by the LGPL
|
||||
License. If you do not delete the provisions above, a recipient may use your
|
||||
version of this file under either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <utils.h>
|
||||
#include "khomp_pvt.h"
|
||||
|
||||
|
||||
void Kflags::init()
|
||||
{
|
||||
_flags[CONNECTED]._name = "CONNECTED";
|
||||
_flags[CONNECTED]._value = false;
|
||||
_flags[REALLY_CONNECTED]._name = "REALLY_CONNECTED";
|
||||
_flags[REALLY_CONNECTED]._value = false;
|
||||
_flags[IS_OUTGOING]._name = "IS_OUTGOING";
|
||||
_flags[IS_OUTGOING]._value = false;
|
||||
_flags[IS_INCOMING]._name = "IS_INCOMING";
|
||||
_flags[IS_INCOMING]._value = false;
|
||||
_flags[STREAM_UP]._name = "STREAM_UP";
|
||||
_flags[STREAM_UP]._value = false;
|
||||
_flags[LISTEN_UP]._name = "LISTEN_UP";
|
||||
_flags[LISTEN_UP]._value = false;
|
||||
_flags[GEN_CO_RING]._name = "GEN_CO_RING";
|
||||
_flags[GEN_CO_RING]._value = false;
|
||||
_flags[GEN_PBX_RING]._name = "GEN_PBX_RING";
|
||||
_flags[GEN_PBX_RING]._value = false;
|
||||
_flags[HAS_PRE_AUDIO]._name = "HAS_PRE_AUDIO";
|
||||
_flags[HAS_PRE_AUDIO]._value = false;
|
||||
_flags[HAS_CALL_FAIL]._name = "HAS_CALL_FAIL";
|
||||
_flags[HAS_CALL_FAIL]._value = false;
|
||||
_flags[DROP_COLLECT]._name = "DROP_COLLECT";
|
||||
_flags[DROP_COLLECT]._value = false;
|
||||
_flags[NEEDS_RINGBACK_CMD]._name = "NEEDS_RINGBACK_CMD";
|
||||
_flags[NEEDS_RINGBACK_CMD]._value = false;
|
||||
_flags[EARLY_RINGBACK]._name = "EARLY_RINGBACK";
|
||||
_flags[EARLY_RINGBACK]._value = false;
|
||||
_flags[FAX_DETECTED]._name = "FAX_DETECTED";
|
||||
_flags[FAX_DETECTED]._value = false;
|
||||
_flags[FAX_SENDING]._name = "FAX_SENDING";
|
||||
_flags[FAX_SENDING]._value = false;
|
||||
_flags[FAX_RECEIVING]._name = "FAX_RECEIVING";
|
||||
_flags[FAX_RECEIVING]._value = false;
|
||||
_flags[OUT_OF_BAND_DTMFS]._name = "OUT_OF_BAND_DTMFS";
|
||||
_flags[OUT_OF_BAND_DTMFS]._value = false;
|
||||
_flags[KEEP_DTMF_SUPPRESSION]._name = "KEEP_DTMF_SUPPRESSION";
|
||||
_flags[KEEP_DTMF_SUPPRESSION]._value = false;
|
||||
_flags[KEEP_ECHO_CANCELLATION]._name = "KEEP_ECHO_CANCELLATION";
|
||||
_flags[KEEP_ECHO_CANCELLATION]._value = false;
|
||||
_flags[KEEP_AUTO_GAIN_CONTROL]._name = "KEEP_AUTO_GAIN_CONTROL";
|
||||
_flags[KEEP_AUTO_GAIN_CONTROL]._value = false;
|
||||
_flags[WAIT_SEND_DTMF]._name = "WAIT_SEND_DTMF";
|
||||
_flags[WAIT_SEND_DTMF]._value = false;
|
||||
_flags[CALL_WAIT_SEIZE]._name = "CALL_WAIT_SEIZE";
|
||||
_flags[CALL_WAIT_SEIZE]._value = false;
|
||||
_flags[NUMBER_DIAL_FINISHD]._name = "NUMBER_DIAL_FINISHD";
|
||||
_flags[NUMBER_DIAL_FINISHD]._value = false;
|
||||
_flags[NUMBER_DIAL_ONGOING]._name = "NUMBER_DIAL_ONGOING";
|
||||
_flags[NUMBER_DIAL_ONGOING]._value = false;
|
||||
|
||||
_flags[FXS_OFFHOOK]._name = "FXS_OFFHOOK";
|
||||
_flags[FXS_OFFHOOK]._value = false;
|
||||
_flags[FXS_DIAL_FINISHD]._name = "FXS_DIAL_FINISHD";
|
||||
_flags[FXS_DIAL_FINISHD]._value = false;
|
||||
_flags[FXS_DIAL_ONGOING]._name = "FXS_DIAL_ONGOING";
|
||||
_flags[FXS_DIAL_ONGOING]._value = false;
|
||||
_flags[FXS_FLASH_TRANSFER]._name = "FXS_FLASH_TRANSFER";
|
||||
_flags[FXS_FLASH_TRANSFER]._value = false;
|
||||
_flags[XFER_QSIG_DIALING]._name = "XFER_QSIG_DIALING";
|
||||
_flags[XFER_QSIG_DIALING]._value = false;
|
||||
_flags[XFER_DIALING]._name = "XFER_DIALING";
|
||||
_flags[XFER_DIALING]._value = false;
|
||||
|
||||
_flags[SMS_DOING_UPLOAD]._name = "SMS_DOING_UPLOAD";
|
||||
_flags[SMS_DOING_UPLOAD]._value = false;
|
||||
|
||||
/*
|
||||
NOW LOADING ...
|
||||
|
||||
_flags[BRIDGED]._name = "BRIDGED";
|
||||
_flags[BRIDGED]._value = false;
|
||||
*/
|
||||
|
||||
_flags[INVALID_FLAG]._name = "INVALID_FLAG";
|
||||
_flags[INVALID_FLAG]._value = false;
|
||||
}
|
||||
|
||||
|
||||
/* Command */
|
||||
|
||||
bool ChanCommandHandler::writeNoSignal(const CommandRequest & cmd)
|
||||
{
|
||||
_fifo->_mutex.lock();
|
||||
bool status = _fifo->_buffer.provide(cmd);
|
||||
_fifo->_mutex.unlock();
|
||||
return status;
|
||||
};
|
||||
|
||||
bool ChanCommandHandler::write(const CommandRequest & cmd)
|
||||
{
|
||||
bool status = writeNoSignal(cmd);
|
||||
|
||||
if (status) signal();
|
||||
|
||||
return status;
|
||||
};
|
||||
|
||||
void ChanCommandHandler::unreference()
|
||||
{
|
||||
|
||||
if (!_fifo)
|
||||
return;
|
||||
|
||||
if(_fifo->_thread)
|
||||
{
|
||||
_fifo->_thread->join();
|
||||
delete _fifo->_thread;
|
||||
_fifo->_thread = NULL;
|
||||
}
|
||||
|
||||
delete _fifo;
|
||||
_fifo = NULL;
|
||||
};
|
||||
|
||||
|
||||
/* Event */
|
||||
|
||||
bool ChanEventHandler::provide(const EventRequest & evt)
|
||||
{
|
||||
_fifo->_mutex.lock();
|
||||
bool status = _fifo->_buffer.provide(evt);
|
||||
_fifo->_mutex.unlock();
|
||||
return status;
|
||||
};
|
||||
|
||||
bool ChanEventHandler::writeNoSignal(const EventRequest & evt)
|
||||
{
|
||||
bool status = true;
|
||||
_fifo->_mutex.lock();
|
||||
|
||||
try
|
||||
{
|
||||
_fifo->_buffer.provider_start().mirror(evt);
|
||||
_fifo->_buffer.provider_commit();
|
||||
}
|
||||
catch(...) //BufferFull & e
|
||||
{
|
||||
status = false;
|
||||
}
|
||||
|
||||
_fifo->_mutex.unlock();
|
||||
return status;
|
||||
};
|
||||
|
||||
bool ChanEventHandler::write(const EventRequest & evt)
|
||||
{
|
||||
bool status = writeNoSignal(evt);
|
||||
|
||||
if (status) signal();
|
||||
|
||||
return status;
|
||||
};
|
||||
|
||||
void ChanEventHandler::unreference()
|
||||
{
|
||||
|
||||
if (!_fifo)
|
||||
return;
|
||||
|
||||
if(_fifo->_thread)
|
||||
{
|
||||
_fifo->_thread->join();
|
||||
delete _fifo->_thread;
|
||||
_fifo->_thread = NULL;
|
||||
}
|
||||
|
||||
delete _fifo;
|
||||
_fifo = NULL;
|
||||
};
|
||||
|
||||
const char * answerInfoToString(int answer_info)
|
||||
{
|
||||
switch (answer_info)
|
||||
{
|
||||
case Board::KhompPvt::CI_MESSAGE_BOX:
|
||||
return "MessageBox";
|
||||
case Board::KhompPvt::CI_HUMAN_ANSWER:
|
||||
return "HumanAnswer";
|
||||
case Board::KhompPvt::CI_ANSWERING_MACHINE:
|
||||
return "AnsweringMachine";
|
||||
case Board::KhompPvt::CI_CARRIER_MESSAGE:
|
||||
return "CarrierMessage";
|
||||
case Board::KhompPvt::CI_UNKNOWN:
|
||||
return "Unknown";
|
||||
case Board::KhompPvt::CI_FAX:
|
||||
return "Fax";
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/******************************** Kommuter ************************************/
|
||||
bool Kommuter::stop()
|
||||
{
|
||||
if(_kwtd_timer_on)
|
||||
{
|
||||
Globals::global_timer->del(_kwtd_timer_index);
|
||||
_kwtd_timer_on = false;
|
||||
}
|
||||
|
||||
/* stop all watches */
|
||||
if (Opt::_kommuter_activation == "auto")
|
||||
{
|
||||
for (int kommuter = 0 ; kommuter < _kommuter_count ; kommuter++)
|
||||
{
|
||||
try
|
||||
{
|
||||
Globals::k3lapi.command(-1, kommuter, CM_STOP_WATCHDOG);
|
||||
LOG(MESSAGE, FMT("kommuter device (%d) was stoped at finalize_module().") % kommuter);
|
||||
}
|
||||
catch(K3LAPI::failed_command & e)
|
||||
{
|
||||
LOG(ERROR, FMT("could not stop the Kommuter device (%d) at finalize_module().") % kommuter);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Kommuter::initialize(K3L_EVENT *e)
|
||||
{
|
||||
/* get total of kommuter devices */
|
||||
_kommuter_count = e->AddInfo;
|
||||
|
||||
if(Opt::_kommuter_activation == "manual")
|
||||
{
|
||||
if (_kommuter_count > 0)
|
||||
{
|
||||
LOG(WARNING, "Kommuter devices were found on your system, but activation is set to manual. To activate this devices use the command 'khomp kommuter on'.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_kommuter_count > 0)
|
||||
{
|
||||
bool start_timer = false;
|
||||
int timeout = Opt::_kommuter_timeout;
|
||||
|
||||
std::string param = STG(FMT("timeout=%d") % timeout);
|
||||
|
||||
for (int kommuter = 0; kommuter < _kommuter_count; kommuter++)
|
||||
{
|
||||
try
|
||||
{
|
||||
Globals::k3lapi.command(-1, kommuter, CM_START_WATCHDOG, param.c_str());
|
||||
start_timer = true;
|
||||
}
|
||||
catch(K3LAPI::failed_command & e)
|
||||
{
|
||||
switch(e.rc)
|
||||
{
|
||||
case ksInvalidParams:
|
||||
LOG(ERROR, FMT("invalid timeout '%d' for Kommuter device '%d' timeout. Mininum is 0, maximum is 255.")
|
||||
% timeout % kommuter);
|
||||
break;
|
||||
default:
|
||||
LOG(ERROR, FMT("could not start the kommuter device number '%d'.") % kommuter);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
LOG(ERROR, FMT("could not start the kommuter device number '%d'.") % kommuter);
|
||||
}
|
||||
}
|
||||
|
||||
if(timeout == 0)
|
||||
{
|
||||
DBG(FUNC, D("kommuter watchdog timer not created because timeout is 0."));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (start_timer)
|
||||
{
|
||||
if (!Globals::global_timer)
|
||||
{
|
||||
LOG(ERROR, D("error creating the timer for kommuter."));
|
||||
}
|
||||
else
|
||||
{
|
||||
_kwtd_timer_index = Globals::global_timer->add((timeout < 5 ? (timeout*500) : 2000), &wtdKickTimer);
|
||||
_kwtd_timer_on = true;
|
||||
|
||||
DBG(FUNC, D("kommuter watchdog timer created and started."));
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DBG(FUNC, D("no kommuter devices were found on system."));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Kommuter::wtdKickTimer(void *)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool restart_timer = false;
|
||||
|
||||
for (int kommuter = 0; kommuter < Board::kommuter._kommuter_count; kommuter++)
|
||||
{
|
||||
try
|
||||
{
|
||||
Globals::k3lapi.command(-1, kommuter, CM_NOTIFY_WATCHDOG);
|
||||
restart_timer = true;
|
||||
DBG(FUNC, D("Kommuter device (%d) notified.") % kommuter);
|
||||
}
|
||||
catch(K3LAPI::failed_command & e)
|
||||
{
|
||||
switch(e.rc)
|
||||
{
|
||||
case ksInvalidState:
|
||||
LOG(ERROR, FMT("Kommuter device '%d' was not initialized.") % kommuter);
|
||||
break;
|
||||
case ksNotAvailable:
|
||||
LOG(ERROR, FMT("Kommuter device '%d' not found.") % kommuter);
|
||||
break;
|
||||
case ksFail:
|
||||
LOG(ERROR, FMT("Kommuter notify command has failed for device '%d'.") % kommuter);
|
||||
break;
|
||||
default:
|
||||
LOG(ERROR, FMT("Kommuter device '%d' could not be notified for some unknow reason.") % kommuter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(restart_timer)
|
||||
{
|
||||
Globals::global_timer->restart( Board::kommuter._kwtd_timer_index, true );
|
||||
DBG(FUNC, D("Kommuter timer restarted."));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
50
src/mod/endpoints/mod_khomp/support/config_defaults.cpp
Normal file
50
src/mod/endpoints/mod_khomp/support/config_defaults.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <config_defaults.hpp>
|
||||
|
||||
/* boolean value */
|
||||
Restriction::Pair booleanValue[3] =
|
||||
{
|
||||
{ "Ativado", "true" },
|
||||
{ "Desativado", "false" },
|
||||
{ NULL , NULL },
|
||||
};
|
||||
75
src/mod/endpoints/mod_khomp/support/config_defaults.hpp
Normal file
75
src/mod/endpoints/mod_khomp/support/config_defaults.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <configurator/restriction.hpp>
|
||||
|
||||
#ifndef _CONFIG_DEFAULTS_HPP_
|
||||
#define _CONFIG_DEFAULTS_HPP_
|
||||
|
||||
/* default khomp config directory */
|
||||
#define KHOMP_CONFIG_DIR "/etc/khomp/config"
|
||||
|
||||
/* define util macro for text conversion */
|
||||
#define CONV(s) \
|
||||
encoder::recode_env(encoder::C_ISO88591, s)
|
||||
|
||||
/* define another util macro for text conversion */
|
||||
#define CONVU(s) \
|
||||
encoder::recode_env(encoder::C_UTF8, s)
|
||||
|
||||
/* shortcut */
|
||||
#define K(x) Restriction::K_##x
|
||||
#define N(x) Restriction::N_##x
|
||||
#define B(x) Restriction::B_##x
|
||||
|
||||
/* boolean value */
|
||||
extern Restriction::Pair booleanValue[3];
|
||||
|
||||
/* nasty shortcuts */
|
||||
#define BOOLEAN_VALUE booleanValue[0], booleanValue[1], booleanValue[2]
|
||||
|
||||
#define BOOLEAN_ALWAYS_TRUE booleanValue[0], booleanValue[2]
|
||||
#define BOOLEAN_ALWAYS_FALSE booleanValue[1], booleanValue[2]
|
||||
|
||||
#define BOOLEAN_TRUE booleanValue[0].pretty
|
||||
#define BOOLEAN_FALSE booleanValue[1].pretty
|
||||
|
||||
#endif /* _CONFIG_DEFAULTS_HPP_ */
|
||||
122
src/mod/endpoints/mod_khomp/support/klog-config.cpp
Normal file
122
src/mod/endpoints/mod_khomp/support/klog-config.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <config_defaults.hpp>
|
||||
|
||||
#include <support/klog-config.hpp>
|
||||
|
||||
Logfile::Logfile()
|
||||
: Configfile("Options", "Log Options")
|
||||
{
|
||||
_filename = STG(FMT("%s/klog.cfg") % KHOMP_CONFIG_DIR);
|
||||
|
||||
// necessario?
|
||||
add(&_logoptions);
|
||||
|
||||
/* Obtain configuration, if file exists. */
|
||||
_good = obtain();
|
||||
}
|
||||
|
||||
Logfile::~Logfile()
|
||||
{
|
||||
/* nothing for now */
|
||||
};
|
||||
|
||||
bool Logfile::select(Section ** ptr, std::string str)
|
||||
{
|
||||
// default section, needed for API compliance.
|
||||
if (str == "")
|
||||
*ptr = &_logoptions;
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
*ptr = _logoptions.section_find(str);
|
||||
}
|
||||
catch (Section::not_found e)
|
||||
{
|
||||
_errors.push_back(STG(FMT("unsupported section '%s', ignoring!") % str));
|
||||
}
|
||||
}
|
||||
|
||||
return (*ptr != 0);
|
||||
};
|
||||
|
||||
bool Logfile::serialize(std::ofstream &fd)
|
||||
{
|
||||
typedef std::vector < std::string > StrList;
|
||||
|
||||
StrList vec;
|
||||
|
||||
vec.push_back("KLogger");
|
||||
vec.push_back("K3L");
|
||||
vec.push_back("IntfK3L");
|
||||
vec.push_back("IntfK3L_C");
|
||||
vec.push_back("ISDN");
|
||||
vec.push_back("R2");
|
||||
vec.push_back("Firmware");
|
||||
vec.push_back("Audio");
|
||||
vec.push_back("SS7");
|
||||
vec.push_back("SIP");
|
||||
vec.push_back("GSM");
|
||||
vec.push_back("Timer");
|
||||
|
||||
for (StrList::iterator i = vec.begin(); i != vec.end(); i++)
|
||||
{
|
||||
fd << "[" << (*i) << "]" << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
Section * sec = _logoptions.section_find(*i);
|
||||
recurse(fd, sec);
|
||||
}
|
||||
catch (Section::not_found e)
|
||||
{
|
||||
_errors.push_back(STG(FMT("unable to find section '%s' in memory, ignoring!") % (*i)));
|
||||
}
|
||||
|
||||
fd << std::endl;
|
||||
}
|
||||
|
||||
fd << "# precisa ter um caracter sobrando no final!" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
69
src/mod/endpoints/mod_khomp/support/klog-config.hpp
Normal file
69
src/mod/endpoints/mod_khomp/support/klog-config.hpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#pragma GCC visibility push(default)
|
||||
#include <stdexcept>
|
||||
#pragma GCC visibility pop
|
||||
|
||||
#include <format.hpp>
|
||||
|
||||
#include <configurator/configfile.hpp>
|
||||
|
||||
#include <support/klog-options.hpp>
|
||||
|
||||
#ifndef _KLOG_CONFIG_HPP_
|
||||
#define _KLOG_CONFIG_HPP_
|
||||
|
||||
struct Logfile: public Configfile
|
||||
{
|
||||
Logfile();
|
||||
~Logfile();
|
||||
|
||||
bool select(Section **, std::string str = "");
|
||||
bool serialize(std::ofstream &);
|
||||
|
||||
Section & root() { return _logoptions; };
|
||||
|
||||
protected:
|
||||
LogOptions _logoptions;
|
||||
};
|
||||
|
||||
#endif /* _KLOG_CONFIG_HPP_ */
|
||||
158
src/mod/endpoints/mod_khomp/support/klog-options.cpp
Normal file
158
src/mod/endpoints/mod_khomp/support/klog-options.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <config_defaults.hpp>
|
||||
|
||||
#include <support/klog-options.hpp>
|
||||
|
||||
LogOptions::KLogger::KLogger()
|
||||
: Section("KLogger", "KLogger", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("FullLog", "Log everything", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::K3L::K3L()
|
||||
: Section("K3L", "K3L", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("CallProgress", "Call Progress events", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("CallAnalyzer", "Call Analyzer events", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("CadenceRecog", "Cadences recognized", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("CallControl", "Call control", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("Fax", "Fax stack", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::IntfK3L::IntfK3L()
|
||||
: Section("IntfK3L", "IntfK3L", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("AudioEvent", "Audio events", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::IntfK3L_C::IntfK3L_C()
|
||||
: Section("IntfK3L_C", "IntfK3L_C", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("AudioEvent", "Audio events", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::ISDN::ISDN()
|
||||
: Section("ISDN", "ISDN", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("Lapd", "LAPD layer log", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("Q931", "Q.931 layer log", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::R2::R2()
|
||||
: Section("R2", "R2", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("Signaling", "R2 Signaling log", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("States", "R2 States Log", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::Firmware::Firmware()
|
||||
: Section("Firmware", "Firmware", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("FwHdlcMsg", "HDLC messages", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("FwLinkErrors", "Link errors", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("FwModemChar", "MODEM char TX/RX", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::Audio::Audio()
|
||||
: Section("Audio", "Audio", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("DSP", "DSP audio messages", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("K3L", "K3L audio messages", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::SS7::SS7()
|
||||
: Section("SS7", "SS7", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("MTP2States", "MTP2 States", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("MTP2Debug", "MTP2 Debug", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("MTP3Management", "MTP3 Management", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("MTP3Test", "MTP3 Test", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("MTP3Debug", "MTP3 Debug", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("ISUPStates", "ISUP States", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("ISUPDebug", "ISUP Debug", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
add(Option("ISUPMessages", "ISUP Messages", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::SIP::SIP()
|
||||
: Section("SIP", "SIP", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::GSM::GSM()
|
||||
: Section("GSM", "GSM", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::Timer::Timer()
|
||||
: Section("Timer", "Timer", false)
|
||||
{
|
||||
add(Option("Value", "Enable log class?", BOOLEAN_FALSE, Restriction(K(STRING), N(UNIQUE), BOOLEAN_VALUE)));
|
||||
};
|
||||
|
||||
LogOptions::LogOptions()
|
||||
: Section("Options", "KLog Options", false)
|
||||
{
|
||||
add(&_klogger);
|
||||
add(&_k3l);
|
||||
add(&_intfk3l);
|
||||
add(&_intfk3lc);
|
||||
add(&_isdn);
|
||||
add(&_r2);
|
||||
add(&_firmware);
|
||||
add(&_audio);
|
||||
add(&_ss7);
|
||||
add(&_sip);
|
||||
add(&_gsm);
|
||||
add(&_timer);
|
||||
};
|
||||
81
src/mod/endpoints/mod_khomp/support/klog-options.hpp
Normal file
81
src/mod/endpoints/mod_khomp/support/klog-options.hpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
KHOMP generic endpoint/channel library.
|
||||
Copyright (C) 2007-2009 Khomp Ind. & Com.
|
||||
|
||||
The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
(the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
||||
|
||||
Software distributed under the License is distributed on an "AS IS" basis,
|
||||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
the specific language governing rights and limitations under the License.
|
||||
|
||||
Alternatively, the contents of this file may be used under the terms of the
|
||||
"GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
If you wish to allow use of your version of this file only under the terms of
|
||||
the LGPL License and not to allow others to use your version of this file under
|
||||
the MPL, indicate your decision by deleting the provisions above and replace them
|
||||
with the notice and other provisions required by the LGPL License. If you do not
|
||||
delete the provisions above, a recipient may use your version of this file under
|
||||
either the MPL or the LGPL License.
|
||||
|
||||
The LGPL header follows below:
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <types.hpp>
|
||||
|
||||
#include <configurator/option.hpp>
|
||||
#include <configurator/section.hpp>
|
||||
|
||||
#ifndef _KLOG_OPTIONS_HPP_
|
||||
#define _KLOG_OPTIONS_HPP_
|
||||
|
||||
struct LogOptions: public Section
|
||||
{
|
||||
struct KLogger: public Section { KLogger(); };
|
||||
struct K3L: public Section { K3L(); };
|
||||
struct IntfK3L: public Section { IntfK3L(); };
|
||||
struct IntfK3L_C: public Section { IntfK3L_C(); };
|
||||
struct ISDN: public Section { ISDN(); };
|
||||
struct R2: public Section { R2(); };
|
||||
struct Firmware: public Section { Firmware(); };
|
||||
struct Audio: public Section { Audio(); };
|
||||
struct SS7: public Section { SS7(); };
|
||||
struct SIP: public Section { SIP(); };
|
||||
struct GSM: public Section { GSM(); };
|
||||
struct Timer: public Section { Timer(); };
|
||||
|
||||
LogOptions();
|
||||
|
||||
KLogger _klogger;
|
||||
K3L _k3l;
|
||||
IntfK3L _intfk3l;
|
||||
IntfK3L_C _intfk3lc;
|
||||
ISDN _isdn;
|
||||
R2 _r2;
|
||||
Firmware _firmware;
|
||||
Audio _audio;
|
||||
SS7 _ss7;
|
||||
SIP _sip;
|
||||
GSM _gsm;
|
||||
Timer _timer;
|
||||
};
|
||||
|
||||
#endif /* _KLOG_OPTIONS_HPP_ */
|
||||
51
src/mod/endpoints/mod_khomp/tools/getversion.sh
Executable file
51
src/mod/endpoints/mod_khomp/tools/getversion.sh
Executable file
@@ -0,0 +1,51 @@
|
||||
#/bin/bash
|
||||
|
||||
###############################################################################
|
||||
# KHOMP generic endpoint/channel library.
|
||||
# Copyright (C) 2007-2010 Khomp Ind. & Com.
|
||||
|
||||
# The contents of this file are subject to the Mozilla Public License
|
||||
# Version 1.1 (the "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
# http://www.mozilla.org/MPL/
|
||||
|
||||
# Software distributed under the License is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
||||
# the specific language governing rights and limitations under the License.
|
||||
|
||||
# Alternatively, the contents of this file may be used under the terms of the
|
||||
# "GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
|
||||
# case the provisions of "LGPL License" are applicable instead of those above.
|
||||
|
||||
# If you wish to allow use of your version of this file only under the terms of
|
||||
# the LGPL License and not to allow others to use your version of this file
|
||||
# under the MPL, indicate your decision by deleting the provisions above and
|
||||
# replace them with the notice and other provisions required by the LGPL
|
||||
# License. If you do not delete the provisions above, a recipient may use your
|
||||
# version of this file under either the MPL or the LGPL License.
|
||||
|
||||
# The LGPL header follows below:
|
||||
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this library; if not, write to the Free Software Foundation,
|
||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
###############################################################################
|
||||
|
||||
version=`grep "#define $1" $2/src/include/switch_version.h | sed -r "s/#define\s+$1.*\"(.*)\"/\1/g"`
|
||||
|
||||
if [ "$version" == "head" ]
|
||||
then
|
||||
echo -1
|
||||
else
|
||||
echo $version
|
||||
fi
|
||||
Reference in New Issue
Block a user