Entering the PIN into the VRA automatically

Translation of the original in Catalan. Read it in: CatalàCastellano

This is one of those posts that could have 40 different titles. In fact, I just want to pass on to you a couple of pieces of code, one in C and the other in perl, which do nothing more than open the virtual serial port that the Vodafone UMTS/GPRS PCMCIA modem creates and then send it the corresponding AT command to enter the PIN of the SIM that the card carries and that's it.

Before getting to the code topics a small summary of useful AT commands for PIN matters:

AT+CPIN=1234 -> Introduim el PIN a la SIM del mòdem.
AT+CPIN? -> preguntem al mòdem si ja hem introduit la SIM.
AT+CPWD="SC",old_pin,new_pin -> canviem el PIN de la SIM.

In fact, if you don't want to write code as nice as the one I use, you can also do something as simple as this:

# echo AT+CPIN=1234 > /dev/tts/USB0

Being 1234 the PIN code and /dev/tts/USB0 the serial device that the vodafone PCMCIA modem creates.

Going to slightly more interesting code here you have a piece of C code to do the same as we did in the previous command:

#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 */
main() {
	int fd;
	fd = open_port();
	init_modem(fd);
	close(fd);
}
int open_port(void) {
	int fd; /* File descriptor for the port */
	struct termios options;
	fd = open("/dev/tts/USB0", O_RDWR | O_NOCTTY | O_NDELAY);
	if (fd == -1) {
		/*
		* Could not open the port.
		*/
		perror("open_port: Unable to open /dev/tts/USB0 - ");
	}
	else fcntl(fd, F_SETFL, 0);
	/* get the current options */
	tcgetattr(fd, &options);
	cfsetispeed(&options, B19200);
	cfsetospeed(&options, B19200);
	/* set raw input, 1 second timeout */
	options.c_cflag &= ~PARENB;
	options.c_cflag &= ~CSTOPB;
	options.c_cflag &= ~CSIZE;
	options.c_cflag |= CS8;
	options.c_cflag     |= (CLOCAL | CREAD);
	options.c_cflag	    &= ~CNEW_RTSCTS;
	/* set the options */
	tcsetattr(fd, TCSANOW, &options);
	return (fd);
}
int                  /* O - 0 = MODEM ok, -1 = MODEM bad */
init_modem(int fd)   /* I - Serial port file */
{
	char buffer[255];  /* Input buffer */
	char *bufptr;      /* Current char in buffer */
	int  nbytes;       /* Number of bytes read */
	int  tries;        /* Number of tries so far */
	for (tries = 0; tries < 3; tries ++) {
		/* send an AT command followed by a CR */
		if (write(fd, "AT+CPIN=1234\r", 13) < 13) continue;
			/* read characters into our string buffer until we get a CR or NL */
			bufptr = buffer;
			while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0) {
				bufptr += nbytes;
				if (bufptr[-1] == '\n' || bufptr[-1] == '\r') break;
			}
		/* nul terminate the string and see if we got an OK response */
		*bufptr = '\0';
		if (strncmp(buffer, "OK", 2) == 0) return (0);
	}
	return (-1);
}

As in the previous example the serial device is /dev/tts/USB0 and the PIN code 1234. This code is a copy/paste of part of a serial howto that I no longer even remember where I got it from. I only kept the code I needed and I added some otpions that were in the howto but not in the code examples.

Next I post an equivalent code done in perl. As you can see this perl code has a dependency on the CPAN library: Device::Gsm. With this library it is also very simple to send SMS and to find out some information about the phone, such as the manufacturer or the imei.

use Device::Gsm;
my $gsm = new Device::Gsm( port => '/dev/tts/USB0', pin => '4466' );
if( $gsm->connect() ) {
  print "connected!\n";
} else {
  print "sorry, no connection with gsm phone on serial port!\n";
}
# Register to GSM network (you must supply PIN number in above new() call)
print $gsm->register();