#include #include #include #include #include #include #include #include #include #include #include #define BAUDRATE B19200 int main(int *argc, char **argv){ printf("Sending sms...\n"); printf("%d\n",argc); if (argc <5 ) { printf("Usage %s port_speed device_name phone_num message\n",argv[0]); return -1; } int fd; char buf[256] , res[256]; size_t nbytes; struct termios oldtio,newtio; printf("port speed -> %s, device -> %s, phone_num -> %s, message -> %s\n",argv[1],argv[2],argv[3],argv[4]); //fd = open(argv[2], O_RDWR | O_NOCTTY | O_NONBLOCK); fd = open(argv[2], O_RDWR | O_NOCTTY); if (fd == -1) { perror("Error opening port"); } tcgetattr(fd,&oldtio); /* save current serial port settings */ bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */ /* BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. CRTSCTS : output hardware flow control (only used if the cable has all necessary lines. See sect. 7 of Serial-HOWTO) CS8 : 8n1 (8bit,no parity,1 stopbit) CLOCAL : local connection, no modem contol CREAD : enable receiving characters */ newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; //newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; /* IGNPAR : ignore bytes with parity errors ICRNL : map CR to NL (otherwise a CR input on the other computer will not terminate input) otherwise make device raw (no other input processing) */ newtio.c_iflag = IGNPAR | ICRNL; /* Raw output. */ newtio.c_oflag = 0; /* ICANON : enable canonical input disable all echo functionality, and don't send signals to calling program */ newtio.c_lflag = ICANON; /* now clean the modem line and activate the settings for the port */ int fdFlags; fcntl(fd, F_SETFL, fdFlags); fdFlags &= ~O_NONBLOCK; fcntl(fd, F_SETFL, fdFlags); tcflush(fd, TCOFLUSH); // toggle DTR to reset modem int mctl = TIOCM_DTR; ioctl(fd, TIOCMBIC, &mctl); tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); /* logics here */ //printf("%d\n",sizeof(buf)); //memset(buf, 0, sizeof(buf)); //strcpy(buf,"ATZ"); //write(fd,buf,strlen(buf)); //sleep(13); fd_set fds; struct timeval timeout; FD_ZERO(&fds); FD_SET(fd, &fds); timeout.tv_sec = 50; timeout.tv_usec = 0; //select(FD_SETSIZE, &fds, NULL, NULL, &timeout); select(FD_SETSIZE, NULL, &fds, NULL, &timeout); //strcpy(buf,"AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0"); strcpy(buf,"ATZ\r"); printf("say -> %s\n",buf); write(fd,buf,strlen(buf)); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, &fds, NULL, NULL, &timeout); read(fd,res,255); //read "OK" printf("got response <- %s\n",res); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, NULL, &fds, NULL, &timeout); strcpy(buf,"AT+CMGF=1\r"); printf("say -> %s\n",buf); write(fd,buf,strlen(buf)); memset(res, 0, sizeof(res)); //sleep(3); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, &fds, NULL, NULL, &timeout); read(fd,res,255); //read "OK" printf("got response <- %s\n",res); memset(buf, 0, sizeof(buf)); strcpy(buf,"AT+CMGW=\""); strcpy(buf+strlen(buf),argv[3]); strcpy(buf+strlen(buf),"\"\r"); printf("say -> %s\n",buf); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, NULL, &fds, NULL, &timeout); write(fd,buf,strlen(buf)); memset(res, 0, sizeof(res)); //FD_ZERO(&fds); //FD_SET(fd, &fds); //select(FD_SETSIZE, &fds, NULL, NULL, &timeout); //read(fd,res,255); //read ">" //printf("got response <- %s\n",res); memset(buf, 0, sizeof(buf)); strcpy(buf,argv[4]); strcpy(buf+strlen(buf),"\032"); printf("sending payload %s\n",buf); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, NULL, &fds, NULL, &timeout); write(fd,buf,strlen(buf)); //write message to //sleep(5); //sleep after send memset(buf, 0, sizeof(buf)); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, &fds, NULL, NULL, &timeout); read(fd,buf,255); //read response "+CMGW: ID" printf("got response <- %s\n",buf); memset(buf, 0, sizeof(buf)); while (strstr(buf,"CMGW:")==NULL){ memset(buf, 0, sizeof(buf)); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, &fds, NULL, NULL, &timeout); read(fd,buf,255); //read response "+CMGW: ID" printf("got response <- %s\n",buf); } memset(res, 0, sizeof(res)); strncpy(res,strchr(buf,' ')+1,(strlen(strchr(buf,' ')+1))); //searching for sms ID printf("got sms ID number -- %s\n",res); memset(buf, 0, sizeof(buf)); strcpy(buf,"AT+CMSS="); strncpy(buf+strlen(buf),res,strlen(res)); strcpy(buf+strlen(buf),"\r"); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, NULL, &fds, NULL, &timeout); write(fd,buf,strlen(buf)); memset(buf, 0, sizeof(buf)); FD_ZERO(&fds); FD_SET(fd, &fds); select(FD_SETSIZE, &fds, NULL, NULL, &timeout); read(fd,buf,255); //read printf("got response <- %s\n",buf); memset(res, 0, sizeof(res)); strncpy(res,strchr(buf,' ')+1,(strlen(strchr(buf,' ')+1))); //searching for _sent_ sms ID printf("got _sent_ sms ID %s\n",res); //read +CMSS: 218 //deleteting stored sms (but how?) /* restore the old port settings */ tcsetattr(fd,TCSANOW,&oldtio); return 0; }