有懂Perl的高手嗎???請幫幫忙,感謝

我們生活周遭存在太多太多的 embedded system ,過去 embedded system 有專屬的 OS,現在 linux 也可以應用在嵌入式環境,不過本版將廣泛討論所有嵌入式系統不只 linux 喔,歡迎有這方面經驗或有興趣的朋友一同進來討論。

版主: chester

有懂Perl的高手嗎???請幫幫忙,感謝

文章xxppxx » 週一 1月 29, 2007 3:10 pm

我有一段asterisk使用的agi程式是用perl寫的....
但因為板子上沒有perl可以用
所以想把它改寫成C的方式...



#!/usr/bin/perl -w
#

use Asterisk::AGI;

my $debug = 1;

my %ext; # Hash that will contain our list of extensions to call
my $cidnum; # Caller ID Number for this call
my $cidname; # Caller ID Name for this call
my $timer; # Call timer for Dial command
my $dialopts;# options for dialing
my $rc; # Catch return code
my $priority;# Next priority

my $AGI = new Asterisk::AGI;
my %input = $AGI->ReadParse();
$AGI->setcallback(\&mycallback);

if ($debug >= 2) {
foreach $key (keys %input) {
debug("$key = " . $input{$key},3);
}
}
$priority = $input{'priority'} + 1;

if ($input{'callerid'} =~ /^\"(.*)\"\s+\<(\d+)\>\s*$/) {
$cidname = $1;
$cidnum = $2;
debug("Caller ID name is '$cidname' number is '$cidnum'", 1);
} else {
$cidname = undef;
$cidnum = undef;
debug("Caller ID is not set", 1);
}

$timer = $AGI->get_variable('ARG1') || 0;
$dialopts = $AGI->get_variable('ARG2') || '';

# Start with AC set to 3 as two args are used
my $arg_cnt = 3;
while(my $arg = $AGI->get_variable('ARG' . $arg_cnt)) {
$ext{$arg} = $arg;
debug("Added extension $arg to extension map", 3);
$arg_cnt++;
}

# Check for call forwarding first
# This doesn't currently check for multilevel forwards or loops
foreach my $k (keys %ext) {
my $cf = $AGI->database_get('CF',$k);
if ($cf) {
$ext{$k} = $cf;
debug("Extension $k has call forward set to $cf", 1);
} else {
debug("Extension $k cf is disabled", 3);
}
}

# Now check for DND
foreach my $k (keys %ext) {
my $dnd = $AGI->database_get('DND',$ext{$k});
if ($dnd) {
debug("Extension $ext{$k} has do not disturb enabled", 1);
delete $ext{$k};
} else {
debug("Extension $ext{$k} do not disturb is disabled", 3);
}
}

# Update Caller ID for calltrace application
foreach my $k (keys %ext) {
if ($cidnum) {
$rc = $AGI->database_put('CALLTRACE', $ext{$k}, $cidnum);
if ($rc == 1) {
debug("DbSet CALLTRACE/$ext{$k} to $cidnum", 3);
} else {
debug("Failed to DbSet CALLTRACE/$ext{$k} to $cidnum ($rc)", 1);
}
} else {
# We don't care about retval, this key may not exist
$AGI->database_del('CALLTRACE', $ext{$k});
debug("DbDel CALLTRACE/$ext{$k} - Caller ID is not defined", 3);
}
}

my $ds = '';
foreach my $k (keys %ext) {
$ds .= 'SIP/' . $ext{$k} . '&';
}
chop $ds if length($ds);

if (!length($ds)) {
$AGI->exec('NoOp');
} else {
$ds .= '|';
$ds .= $timer if ($timer);
$ds .= '|' . $dialopts; # pound to transfer, provide ringing

debug("About to execute Dial($ds)", 1);
$rc = $AGI->exec('Dial ' . $ds);
# returns 0 on no answer -1 on hangup
# i still need to check what it returns on busy
debug("Dial return value was $rc", 1);
if ($rc == -1) {
debug("Setting Priority to " . ($priority + 20) . ' from '
. $priority, 1);
$AGI->set_priority($priority + 20);
}
}

exit 0;

sub debug
{
my $string = shift;
my $level = shift || 3;

if ($debug) {
$AGI->verbose($string, $level);
}
return(0);
}

sub mycallback
{
my $rc = shift;
debug("User hung up. (rc=" . $rc . ")", 1);
exit ($rc)
}
xxppxx
可愛的小學生
可愛的小學生
 
文章: 12
註冊時間: 週二 5月 23, 2006 11:23 am

文章企鵝狂 » 週一 1月 29, 2007 3:38 pm

http://www.voip-info.org/wiki-Asterisk+AGI

這裡有C的API...
這也不是單純會Perl就能寫的...
因為他用到了AGI這個不知道甚麼東西
http://news.bbc.co.uk/1/hi/world/asia-pacific/country_profiles/1285915.stm


Country profile: Taiwan
Map of Taiwan
Taiwan is the island which has for all practical purposes been independent for half a century, but which China regards as a rebel region that must be reunited with the mainland - by force if necessary.
企鵝狂
活潑的高中生
活潑的高中生
 
文章: 477
註冊時間: 週三 3月 31, 2004 11:13 am
來自: 台灣共和國


回到 embedded system

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客

cron