Remote Server Get CDR from GSM Gateway by AMI
Introduction:
OpenVox GSM Gateway can support AMI(Asterisk Manager Interface,if you want to get the CDR from remote server,you can use the AMI to do it!
Step 1:
Set the AMI in OpenVox GSM Gateway,follows:
Step 2:Write a Program to get the CDR like follows:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
//Login
void login_fun(int sock_fd)
{
char username[20];
char secret[20];
int login_len=0;
int secret_len=0;
int res = 0;
char receive_buf[4096];
char login_buf[40];
memset(username,’\0′,20);
memset(secret,’\0′,20);
printf(“please input your username\n”);
scanf(“%s”,username);
fflush(stdin);
printf(“please input your secret\n”);
scanf(“%s”,secret);
fflush(stdin);
memset(login_buf,’\0′,40);
sprintf(login_buf,”action:Login\r\nusername:%s\r\nsecret:%s\r\n\r\n”,username,secret);
login_len = strlen(login_buf);
if(res = write(sock_fd,login_buf,login_len) == login_len)
{
sleep(1);
memset(receive_buf,’\0′,4096);
if(res = read(sock_fd,receive_buf,sizeof(receive_buf))<0)
{
perror(“login failed\n”);
return;
}
printf(“%s\n”,receive_buf);
if(NULL != strstr(receive_buf,”Authentication accepted”))
{
printf(“login success\n”);
}
}
}
void read_cdr(int sock_fd)
{
char receive_buf[4096];
int res=0;
char *receive_Data;
char temp_buf[1024];
char *temp = NULL;
struct CDR
{
char CallerID[512];
char CalleeID[512];
char From[512];
char To[512];
char StartTime[512];
char Duration[512];
char Result[512];
};
struct CDR cdr_buff;
int i =0;
int j =0;
while(1)
{
memset(receive_buf,’\0′,4096);
memset(cdr_buff.CallerID,’\0′,512);
memset(cdr_buff.CalleeID,’\0′,512);
memset(cdr_buff.From,’\0′,512);
memset(cdr_buff.To,’\0′,512);
memset(cdr_buff.StartTime,’\0′,512);
memset(cdr_buff.Duration,’\0′,512);
memset(cdr_buff.Result,’\0′,512);
if(res = read(sock_fd,receive_buf,sizeof(receive_buf))<0)
{
perror(“read failed\n”);
}
else
{
//memset(receive_buf,’\0′,4096);
if((receive_Data = strstr(receive_buf,”Application: WriteCDR”))!=NULL)
{
//printf(“111111111111111111111111111111111111111111111%s\n”,receive_Data);
i=0;
j=0;
if((temp=strstr(receive_Data,”AppData:”))!=NULL)
{
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’:’)
{
j++;
}
else
{
break;
}
}
temp=temp+j+2;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’,’)
{
cdr_buff.CallerID[i]=temp[i];
}
else
{
break;
}
}
temp=temp+i+1;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’,’)
{
cdr_buff.CalleeID[i]=temp[i];
}
else
{
break;
}
}
temp=temp+i+1;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’,’)
{
cdr_buff.From[i]=temp[i];
}
else
{
break;
}
}
temp=temp+i+1;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’,’)
{
cdr_buff.To[i]=temp[i];
}
else
{
break;
}
}
temp=temp+i+1;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’,’)
{
cdr_buff.StartTime[i]=temp[i];
}
else
{
break;
}
}
temp=temp+i+1;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’,’)
{
cdr_buff.Duration[i]=temp[i];
}
else
{
break;
}
}
temp=temp+i+1;
for(i=0;i<strlen(temp);i++)
{
if(temp[i]!=’\n’)
{
cdr_buff.Result[i]=temp[i];
}
else
{
break;
}
}
printf(“Callerid==============%s\n”,cdr_buff.CallerID);
printf(“Calleeid==============%s\n”,cdr_buff.CalleeID);
printf(“From==============%s\n”,cdr_buff.From);
printf(“To==============%s\n”,cdr_buff.To);
printf(“StartTime==============%s\n”,cdr_buff.StartTime);
printf(“Duration==============%s\n”,cdr_buff.Duration);
printf(“Result==============%s\n”,cdr_buff.Result);
// break;
}
}
}
}
}
int main(void)
{
int client_socket;
struct sockaddr_in client_addr;
char ServerIp[20];
memset(ServerIp,’\0′,20);
printf(“Please input your server ip address\n”);
scanf(“%s”,ServerIp);
fflush(stdin);
client_socket = socket(AF_INET,SOCK_STREAM,0);
if(client_socket < 0)
{
perror(“create socket error\n”);
return -1;
}
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(5038);
client_addr.sin_addr.s_addr = inet_addr(ServerIp);
//connect
if(connect(client_socket,(struct sockaddr *)&client_addr,sizeof(client_addr))<0)
{
perror(“connect error\n”);
return -1;
}
else
{
printf(“connect to %s success\n”,ServerIp);
}
login_fun(client_socket);
read_cdr(client_socket);
return 0;
}
Step 3:compile and run the app
Notice: This just a test program,just use for refer it!
Thanks!