Sponsor: VoiceMeUp - Corporate & Wholesale VoIP Services

VoIP Mailing List Archives
Mailing list archives for the VoIP community
 SearchSearch 

[asterisk-users] [AGI 1.4] C sample?


 
Post new topic   Reply to topic    VoIP Mailing List Archives Forum Index -> Asterisk Users
View previous topic :: View next topic  
Author Message
vincent.delporte at bi...
Guest





PostPosted: Sun Jan 27, 2008 1:57 am    Post subject: [asterisk-users] [AGI 1.4] C sample? Reply with quote

Hello

I'm pretty much a newbie when it comes to C, but I have to use
this language to write a couple of AGI proggies because I need them to
be statically compiled.

Strangely enough, Google didn't return much when looking for the
"Hello, world!" of AGI in C.

The following doesn't work: The file never gets written:
===========
//check_cid.c
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>

int main(int argc, char *argv[])
{
char line[80];
int i;

setlinebuf(stdout);
setlinebuf(stderr);

FILE *file;
file = fopen("file.txt","a+");
while (1) {
fgets(line,80,stdin);
fprintf(file,"%s",line);
if (strlen(line) <= 1) break;
}
fclose(file);

return(EXIT_SUCCESS);
}
===========

This is how it's called in extensions.conf:
===========
[inside]
exten => 9999,1,Verbose(Yes!)
exten => 9999,n,AGI(check_cid.exe|123)
===========

And this is the output of "agi debug" in CLI:
===========
*CLI> -- Executing [9999 at inside:1] Verbose("SIP/2000-0904bee0",
"Yes!") in new stack
Yes!
-- Executing [9999 at inside:2] AGI("SIP/2000-0904bee0",
"check_cid.exe|123") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/check_cid.exe
AGI Tx >> agi_request: check_cid.exe
AGI Tx >> agi_channel: SIP/2000-0904bee0
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1201412176.3
AGI Tx >> agi_callerid: 2000
AGI Tx >> agi_calleridname: Fred
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 9999
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: inside
AGI Tx >> agi_extension: 9999
AGI Tx >> agi_priority: 2
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
-- AGI Script check_cid.exe completed, returning 0
== Auto fallthrough, channel 'SIP/2000-0904bee0' status is 'UNKNOWN'
===========

If someone has a very basic example in C that shows how to read the
CID #, and rewrite the CID name, I'm interested.

Thank you.
Back to top
lee at datatrakpos.com
Guest





PostPosted: Sun Jan 27, 2008 9:09 am    Post subject: [asterisk-users] [AGI 1.4] C sample? Reply with quote

Vincent wrote:
Quote:
Hello

I'm pretty much a newbie when it comes to C, but I have to use
this language to write a couple of AGI proggies because I need them to
be statically compiled.

Strangely enough, Google didn't return much when looking for the
"Hello, world!" of AGI in C.

The following doesn't work: The file never gets written:
===========
//check_cid.c
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>

int main(int argc, char *argv[])
{
char line[80];
int i;

setlinebuf(stdout);
setlinebuf(stderr);

FILE *file;
file = fopen("file.txt","a+");
while (1) {
fgets(line,80,stdin);
fprintf(file,"%s",line);
if (strlen(line) <= 1) break;
}
fclose(file);

return(EXIT_SUCCESS);
}
===========

This is how it's called in extensions.conf:
===========
[inside]
exten => 9999,1,Verbose(Yes!)
exten => 9999,n,AGI(check_cid.exe|123)
===========

And this is the output of "agi debug" in CLI:
===========
*CLI> -- Executing [9999 at inside:1] Verbose("SIP/2000-0904bee0",
"Yes!") in new stack
Yes!
-- Executing [9999 at inside:2] AGI("SIP/2000-0904bee0",
"check_cid.exe|123") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/check_cid.exe
AGI Tx >> agi_request: check_cid.exe
AGI Tx >> agi_channel: SIP/2000-0904bee0
AGI Tx >> agi_language: en
AGI Tx >> agi_type: SIP
AGI Tx >> agi_uniqueid: 1201412176.3
AGI Tx >> agi_callerid: 2000
AGI Tx >> agi_calleridname: Fred
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: 9999
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: inside
AGI Tx >> agi_extension: 9999
AGI Tx >> agi_priority: 2
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
-- AGI Script check_cid.exe completed, returning 0
== Auto fallthrough, channel 'SIP/2000-0904bee0' status is 'UNKNOWN'
===========

If someone has a very basic example in C that shows how to read the
CID #, and rewrite the CID name, I'm interested.

Thank you.



Sorry, I don't have a sample for you as I write mostly in Freepascal/Lazarus
these days and use my own library for AGI/FastAGI. That said, did you try
saving the file to a fully qualified path?

I say that because in pascal, usually you can do this on Windows:

var
sFile: string
AStringList: TStringList;
begin
sFile := ExtractFilePath(ParamStr(0)) + 'myfile.txt';

AStringList := TStringList.create;
try
AStringList.LoadFromFile(sFile);
Write(AStringList.Text);
finally;
AStringList.free;
end;
end;

Normally ExtractFilePath would return the directory path that the executable is
locate in. My understanding is that that is not necessarily the case on linux
which takes into account the directory from which the call to the executable is
being made, which might not be the same directory that the executable is located on.

Could be the same thing you are experiencing. Try using a fully qualified path
/usr/mydirect/myfile.txt
--
Warm Regards,

Lee

"Everything I needed to learn in life, I learned selling encyclopedias door to
door."
Back to top
vincent.delporte at bi...
Guest





PostPosted: Sun Jan 27, 2008 1:36 pm    Post subject: [asterisk-users] [AGI 1.4] C sample? Reply with quote

On Sun, 27 Jan 2008 09:09:59 -0500, Lee Jenkins <lee at datatrakpos.com>
wrote:
Quote:
Sorry, I don't have a sample for you as I write mostly in Freepascal/Lazarus
these days and use my own library for AGI/FastAGI. That said, did you try
saving the file to a fully qualified path?

My hero! Smile That did it.

Both work:

file = fopen("/tmp/file.txt","w");
file = fopen("./file.txt","w");

Also, and contrary to what I've read in some online pages, we don't
have to put AGI scripts in /var/lib/asterisk/agi-bin:

exten => 9999,n,AGI(/tmp/check_cid.exe|123)

BTW, as I'm used to Delphi, I'm also interested in checking out FP. Is
it easy to install on eg. CentOS? Can I come up with a totally
self-dependent EXE that I can just drop into a limited host running
AstLinux?

Quote:
Normally ExtractFilePath would return the directory path that the executable is
locate in. My understanding is that that is not necessarily the case on linux
which takes into account the directory from which the call to the executable is
being made, which might not be the same directory that the executable is located on.

If someone has an idea why it's required to use a full path for the
file to be written, I'm interested.

Thanks again.
Back to top
asterisk.org at sedwar...
Guest





PostPosted: Sun Jan 27, 2008 2:37 pm    Post subject: [asterisk-users] [AGI 1.4] C sample? Reply with quote

On Sun, 27 Jan 2008, Vincent wrote:

Quote:
If someone has an idea why it's required to use a full path for the
file to be written, I'm interested.

If you don't specify a path, the current working directory of the
executing process is used. So, the question is, what is the CWD of your
AGI process? Unless you change it in your AGI, it will be the CWD of the
parent, Asterisk.

To see what the CWD is, try something like this, either from your dialplan
or your AGI.

system(set >/tmp/my-environment)
system("set >/tmp/my-environment");

Here's a snippet from my /etc/init.d/asterisk file:

env --ignore-environment\
DATABASE-DATABASE=$DATABASE_DATABASE\
DATABASE-PASSWORD=$DATABASE_PASSWORD\
DATABASE-SERVER=$DATABASE_SERVER\
DATABASE-USER=$DATABASE_USER\
HOST=$HOST\
PATH=/usr/local/bin/:/bin/\
$ASTERISK $START_OPTIONS

Thus, nothing that I don't know about is in Asterisk's environment. And
no, I'm not a control freak. Honestly. No, I mean it.

Thanks in advance,
------------------------------------------------------------------------
Steve Edwards sedwards at sedwards.com Voice: +1-760-468-3867 PST
Newline Fax: +1-760-731-3000
Back to top
lee at datatrakpos.com
Guest





PostPosted: Sun Jan 27, 2008 3:05 pm    Post subject: [asterisk-users] [AGI 1.4] C sample? Reply with quote

Vincent wrote:
Quote:
On Sun, 27 Jan 2008 09:09:59 -0500, Lee Jenkins <lee at datatrakpos.com>
wrote:
Quote:
Sorry, I don't have a sample for you as I write mostly in Freepascal/Lazarus
these days and use my own library for AGI/FastAGI. That said, did you try
saving the file to a fully qualified path?

My hero! Smile That did it.

Both work:

file = fopen("/tmp/file.txt","w");
file = fopen("./file.txt","w");

Also, and contrary to what I've read in some online pages, we don't
have to put AGI scripts in /var/lib/asterisk/agi-bin:

exten => 9999,n,AGI(/tmp/check_cid.exe|123)

BTW, as I'm used to Delphi, I'm also interested in checking out FP. Is
it easy to install on eg. CentOS? Can I come up with a totally
self-dependent EXE that I can just drop into a limited host running
AstLinux?


I've used the Windows version for most development, but have lazarus running
nicely on a CentOS4 VM. Installing from the setup program on windows and from
rpm on linux was easy.

Just as with delphi, freepascal creates native stand alone executables as well
as libraries (.dll or .so) and with speed on par (but not as fast) as C .
Lazarus is the IDE that I use to work with Freepascal. Lazarus is now about on
par with Delphi 6, IMO and one of the best IDE's available for Linux. Not the
prettiest, but one of the most useful Wink

I also wrote an objectpascal based AGI/FastAGI wrapper a while back if you're
interested:

http://www.leebo.dreamhosters.com/asterisk/pasagi.pas

--
Warm Regards,

Lee

"Everything I needed to learn in life, I learned selling encyclopedias door to
door."
Back to top
Display posts from previous:   
Post new topic   Reply to topic    VoIP Mailing List Archives Forum Index -> Asterisk Users All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

VoiceMeUp - Corporate & Wholesale VoIP Services