ich habe schon ein bisschen was fertig, nichts tolles aber vor allem, habe ich einmal das Programm im Ansatz versucht "Out of the BOX" auf einen Atmega 8 um zu stellen, knapp 2 Kbyte werden aktuell "erst", obwohl hier viel Redundant ist und noch nicht wirklich gut Programmiertechnisch optimiert, verbraucht. Klar der Speicherfressendeteil der Umsetzung von PDU nach ASCII kommt erst, aber immerhin.
Aktuell stellt der Code die Kommunikation mit dem (Handy-) Modem her, stellt die wichtigsten Dinge ein und wartet nach Einstellungen auf Reaktionen auf der Schnittstelle.
Wenn nun eine SMS eintrift, wird darauf reagiert und der komplette PDU String in seine Einzelteile zerlegt und ausgegeben:
Nachdem nun die SMS ausgegeben wurde, wird diese auch schon direkt wieder aus dem SMS Speicher gelöscht.
Jetzt gilt es die Umsetzung zu erledigen, PDU 2 ASCII, mal sehen wie weit ich komme.
BEvor sich Jemand beschwert und sich darüber auslässt wie schlecht der Code ist, es sei nochmals erwähnt, ja der Code hat viel Redundanz und bedarf einer zwingenden Optimierung, ich bin dran, aller Anfang ist schwer. Kommentarzeilen fehlen zudem auch noch.
Code: Alles auswählen
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#define BAUDRATE 19200
#define MODEMDEVICE "/dev/ttyUSB0"
#define FALSE 0
#define TRUE 1
int init_port(void);
char *ende_string(char *string, char *string_ret);
char *lese_befehl ( int fd, char *string, char *string_ret);
char *do_sms_string (int fd, char *string, char *string_ret);
char *func_read_sms( char *string, char *return_sms_date);
char *rev_string(char *string, char *new_string);
char *func_read_sms_date( char *string, char *new_string);
char *func_read_sms_center( char *string, char *new_string);
char *func_read_sms_number( char *string, char *new_string);
char *func_read_sms_text( char *string, char *new_string);
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int init_port(void)
{
int fd; /* File descriptor for the port */
int i=0, bytes = 0, n = 0;
struct termios options;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
fcntl(fd, F_SETFL, 0);
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 19200...
*/
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// options.c_lflag |= (ICANON );
options.c_cflag &= ~CRTSCTS;
/* set the options */
tcsetattr(fd, TCSANOW, &options);
//tcsetattr(fd, TCSAFLUSH, &options);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyUSB0 - ");
close(fd);
return (-1);
}
else
{
fcntl(fd, F_SETFL, 0);
}
/* FIXME
* Check up connections!!
* and start handy after three times of
* check.
* init modem squence for
* testing the connections
* after these tests we can wait for input
* from modem handy
*/
for (i=0;i<=3;i++)
{
n = write(fd, "at\r\n", bytes);
if (n < 0)
{
//printf("write(%s) of %i bytes failed!\n",string, str_bytes);
return 1;
}
}
//puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
//printf (" Daten 1: %s \n", puffer_ptr );
return (fd);
}
char *func_read_sms_center(char *string, char *new_string)
{
int i, a = 0;
for (i=0; string[i] !='\0'; i++)
{
// SMS Center Number
if (i >= 4 && i <= 15)
{
new_string[a] = string[i];
a++;
}
}
new_string = rev_string( new_string, new_string);
return new_string;
}
char *func_read_sms_number( char *string, char *new_string)
{
int i, b = 0;
for( i = 0; string[i] != '\0'; i++)
{
// SMS Receive or Sender Number
if (i >= 22 && i <=33)
{
new_string[b] = string[i];
b++;
}
}
new_string = rev_string( new_string, new_string);
return new_string;
}
char *func_read_sms_text( char *string, char *new_string)
{
int i, d = 0;
for ( i = 0 ; string[i] != '\0'; i++)
{
// SMS Message
if (i > 54 )
{
//return_char[d] = string[i];
new_string[d] = string[i];
d++;
}
}
return new_string;
}
char *func_read_sms_date( char *string, char *new_string)
{
int c=0, i;
for ( i=0; string[i] != '\0'; i++)
{
// SMS Date
if (i >= 38 && i <= 52 )
{
//pdu_sms_date[c] = string[i];
new_string[c] = string[i];
c++;
}
}
new_string = rev_string( new_string, new_string);
return new_string;
}
char *rev_string(char *string, char *new_string)
{
int c = 0,s=0;
char a, b;
while (string[s] !='\0')
{
a = string[s];
s++;
b = string[s];
s++;
new_string[c] = b;
c++;
new_string[c] = a;
c++;
}
return new_string;
}
char *lese_befehl ( int fd, char *string, char *string_ret )
{
int bytes = 0, return_check = 0, res = 0;
//while ( (ioctl(fd, FIONREAD, &bytes)) != 0 ) {} ;
sleep(1);
return_check = ioctl(fd, FIONREAD, &bytes);
if ( return_check != -1 && bytes > 0 )
{
res=read(fd, string, bytes);
string[res]=0;
string_ret = ende_string(string, string_ret);
}
return string_ret;
}
int sende_befehl ( int fd, char *string )
{
int n = 0, str_bytes = 0;
str_bytes = (sizeof(string)+2);
printf("sende_befehl %s \n",string);
n = write(fd, string, str_bytes);
if (n < 0)
{
printf("write(%s) of %i bytes failed!\n",string, str_bytes);
return 1;
}
else
{
return 0;
}
}
char *ende_string(char *string, char *string_ret)
{
int e=0, i=0;
for(;;)
{
if(string[i] != '\n' )
{
string_ret[e] = string[i];
e++;
}
if(string[i] == '\0')
{
string_ret[e] = '\0';
break;
}
i++;
}
return string_ret;
}
char *read_pdu_str(int pos, int n, char *string, char *string_ret)
{
int e=0, i=0;
for(;;)
{
if(string[i] != 'O' && string[i] != ',' && string[i] != 'K' && string[i] != ' ' && string[i] != '+' && string[i] != ':' )
// if( string[i] != 'O' && string[i] != 'K' && )
{
string_ret[e] = string[i];
e++;
}
if(string[i] == '\0')
{
string_ret[e] = '\0';
break;
}
i++;
}
for (i = pos+n;string_ret[i] != '\0'; i++)
{
string_ret[i-n] = string_ret[i];
}
return string_ret;
}
int main (int argc, char **argv)
{
char check_handy_con[] = "at\r\n"; // ok
char start_modem[] = "at???"; // we need the step
char reset_modem[] = "atz0\r\n"; // OK -> after this do initialisize
char enable_clip[] = "at+clip=1\r\n"; // ok
char disable_local_echo[] = "ate0\r\n"; // ok
char disable_vibration[] = "at+cvib=0\r\n"; // ok
char battery_status[] = "at+cbc\r\n"; // +CBC: 0,100
char read_manufacter[] = "at+cgmi\r\n"; // SIEMENS
char read_sms_new[] = "at+cmgl=0\r\n";// new message or ok
char read_funk_quality[] = "at+csq\r\n"; // +CSQ: 19,99
char info_sms_inc[] = "at+cnmi=1,1\r\n"; // ok # +CMTI: "SM",x
char delete_sms_ind[] = "at+cmgd=1\r\n"; // ok
char disable_ringtone[] ="at^SRTC=0\r\n"; // ok
char read_scid[] = "at^scid\r\n"; //
char handy_off[] ="AT^SMSO\r\n"; // ^SMSO: MS OFF
char pin_input[] = "at+CPIN=1234\r\n"; //
char manu[] = "SIEMENS";
int fd;
// initialisize the ttyusb port
// in this case the port will be initialisize
// and will check the connection
// to the handy modell.
fd=init_port();
char sms_date[20] = "";
char sms_text[255] = "";
char sms_number[20] = "";
char sms_center[20] = "";
char puffer[160] ="";
char puffer_alt[160] ="";
char puffer_neu[160] ="";
char *puffer_ptr;
char *puffer_neu_ptr;
char *puffer_alt_ptr;
char *sms_date_ptr;
char *sms_text_ptr;
char *sms_number_ptr;
char *sms_center_ptr;
sms_date_ptr = sms_date;
sms_text_ptr = sms_text;
sms_number_ptr = sms_number;
sms_center_ptr = sms_center;
puffer_ptr = puffer;
puffer_neu_ptr = puffer_neu;
puffer_alt_ptr = puffer_alt;
int i = 1;
while (1)
{
/*
for(i=0;i<step;i++)
{
}
*/
if (i==1)
{
sende_befehl (fd, check_handy_con);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "OK") != 0)
{
i--;
}
else
{
printf("check modem connection: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==2)
{
sende_befehl (fd, disable_local_echo);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("disable local echo: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==3)
{
sende_befehl (fd, disable_vibration);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("disable vibration: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==4)
{
sende_befehl (fd, info_sms_inc);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("Set sms info on Modem device: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==5)
{
sende_befehl (fd, read_scid);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("SCID Status read: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==6)
{
sende_befehl (fd, battery_status);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("Battery Status read: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==7)
{
sende_befehl (fd, enable_clip);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("enable clip read: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==8)
{
sende_befehl (fd, disable_vibration);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("disable vibration status read: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
if (i==9)
{
sende_befehl (fd, disable_ringtone);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
i--;
}
else
{
printf("disable ringtone status read: %s\n", puffer_ptr);
}
puffer_ptr[0] = '\0';
}
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (puffer_ptr[0] != '\0')
{
/* !!!! FIXME !!!!
* If we have a call we want to answer on that
* if we have a sms we need to read this sms.
*
*/
printf ("puffer_ptr: %s \n", puffer_ptr );
if (strncmp(puffer_ptr, "+CMTI", 5) == 0)
{
printf("SMS incoming %s \n", puffer_ptr);
sende_befehl(fd, read_sms_new);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_ptr);
puffer_ptr = read_pdu_str(0,8,puffer_ptr, puffer_ptr);
printf("SMS PDU_TEXT %s \n", puffer_ptr);
sms_date_ptr = func_read_sms_date(puffer_ptr, sms_date_ptr);
printf("SMS_Date %s \n", sms_date_ptr);
sms_text_ptr = func_read_sms_text(puffer_ptr, sms_text_ptr);
printf("SMS_Text %s \n", sms_text_ptr);
sms_number_ptr = func_read_sms_number(puffer_ptr, sms_number_ptr);
printf("SMS_Date %s \n", sms_number_ptr);
sende_befehl (fd, delete_sms_ind);
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
if (strcmp (puffer_ptr, "ERROR") == 0)
{
puffer_ptr = lese_befehl(fd, puffer_alt_ptr, puffer_neu_ptr);
}
else
{
printf("delete sms status: %s\n", puffer_ptr);
// save into MYSQL DB
}
}
if ( strncmp(puffer_ptr, "RING+CLIP:", 10) == 0)
{
printf("call incoming %s \n", puffer_ptr);
// make a hangup if the number is not correct?
// lets ring 3 times if number is correct and than hang up?
}
puffer_ptr[0] = '\0';
}
i++;
}
close(fd);
return 0;
}