Setting Up an SMS Gateway with Ubuntu 8.04, Kannel and Huawei E220 GSM Modem
http://kerneltrap.org/node/17017
Setting Up an SMS Gateway with Ubuntu 8.04, Kannel and Huawei E220 GSM Modem
Submitted by Eus
on March 18, 2009 - 2:07am
* Life
Setting up Kannel 1.4.1 in Ubuntu 8.04 Desktop was easy.
The challenging part was finding the right GSM modem or cellphone because some hardware vendors still refuse to cooperate with us in the Free Software and Open Source Software communities. Woe to those vendors!
Googling a bit I found out that Huawei E220 GSM modem works well with a GNU/Linux system.
Fortunately, the device was available in my area. So, I ordered one.
I took the Kannel SMS gateway setting from someone named Lasantha who posted it on http://www.kannel.org/pipermail/users/2008-August/005342.html.
After putting user `kannel' under group `dialout' to get read-write access to /dev/ttyUSB*, the configuration worked like a charm.
But, when I sent a single SMS to the modem, the following conversation ensued:
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CPMS?^M
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CPMS?
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- +CPMS: "SM",1,40,"SM",1,40,"SM",1,40
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: 1 messages waiting in memory
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=1^M
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=1
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: not deleted.
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=2^M
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=2
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 13:58:36 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: not deleted.
.
.
.
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=39^M
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=39
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: not deleted.
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=40^M
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=40
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- +CMS ERROR: 321
2009-03-16 13:58:40 [31308] [6] ERROR: AT2[/dev/ttyUSB0]: CMS ERROR: +CMS ERROR: 321
2009-03-16 13:58:40 [31308] [6] ERROR: AT2[/dev/ttyUSB0]: CMS ERROR: Invalid memory index -> don't worry, just memory fragmentation. (321)
2009-03-16 13:58:40 [31308] [6] DEBUG: AT2[/dev/ttyUSB0]: failed to get message 40.
and the same conversation kept repeating in an endless loop filling up my harddisk space.
Naturally I googled for the case first.
The first hit for `kannel huawei e220 failed to get message' was: http://www.nabble.com/Kannel-Not-RECEIVING-SMS-from-USB-MODEM-(HUAWEI-(E220)-HSDPA-)-td22262301.html
Although the first hit didn't have the answer, it told me that I was not alone. So, I sent a private message to the OP.
While waiting for the answer, I tried to google with `kannel huawei cms error 321' that returned http://www.developershome.com/sms/resultCodes2.asp.
It said that 321 means: `The memory/message storage index assigned to the AT command is invalid.'
Seeing the word index made me conclude that perhaps while Kannel addressed the SMS slots from 1 to 40 inclusive, the modem addressed the SMS slots from 0 to 39 inclusive just like when addressing an array.
I confirmed that this was true by sending another SMS that Kannel happily fetched and replied successfully although it still could not fetch the old message as follows:
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CPMS?^M
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CPMS?
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- +CPMS: "SM",2,40,"SM",2,40,"SM",2,40
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: 2 messages waiting in memory
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=1^M
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=1
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- +CMGR: 0,,41
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- [EUS: Sorry, the message is private]
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: received message from SMSC: [EUS: Sorry, the number is private]
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: Numeric sender (international) <[EUS: Sorry, the number is private]>
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: User data length read as (23)
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: Udh decoding done len=23 udhi=0 udhlen=0 udh=''
2009-03-16 15:55:36 [5241] [18] DEBUG: send_msg: sending msg to box: <127.0.0.1>
2009-03-16 15:55:36 [5241] [18] DEBUG: boxc_sender: sent message to <127.0.0.1>
2009-03-16 15:55:36 [5241] [17] DEBUG: boxc_receiver: got ack
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGD=1^M
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGD=1
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=2^M
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=2
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- OK
2009-03-16 15:55:36 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: not deleted.
.
.
.
2009-03-16 15:55:40 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: --> AT+CMGR=40^M
2009-03-16 15:55:40 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- AT+CMGR=40
2009-03-16 15:55:40 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: <-- +CMS ERROR: 321
2009-03-16 15:55:40 [5241] [6] ERROR: AT2[/dev/ttyUSB0]: CMS ERROR: +CMS ERROR: 321
2009-03-16 15:55:40 [5241] [6] ERROR: AT2[/dev/ttyUSB0]: CMS ERROR: Invalid memory index -> don't worry, just memory fragmentation. (321)
2009-03-16 15:55:40 [5241] [6] DEBUG: AT2[/dev/ttyUSB0]: failed to get message 40.
Now this is the benefit of buying a free software (are you confused with the word `buying'? See http://gnu.org for the answer), you have the advantage to be able to fix this kind of corner cases quickly.
So, I went on by downloading the source package as described in http://www.cyberciti.biz/faq/rebuilding-ubuntu-debian-linux-binary-package/.
Adding
--message_number;
at line 778 of kannel-1.4.1/gw/smsc/smsc_at.c fixed the problem.
While trying to make a better patch to be posted to kannel-devel mailing list, I noticed an interesting line in kannel-1.4.1/gw/smsc/smsc_at.c as follows:
if (!at2_read_delete_message(privdata, location)) {
error(1, "AT2[%s]: CMTI notification received, but no message found in memory!",
octstr_get_cstr(privdata->name));
}
The interesting part was `CMTI notification received'.
It gave me an inspiration that instead of having Kannel probed the GSM modem every five seconds asking whether or not there is a new message, the GSM modem can notify Kannel whenever an SMS is received, and so it will reduce the server's load.
Moreover, I hypotesized that since the notification would also supply the location to fetch the SMS, this method would not encounter the previous addressing problem.
So, the next step was to google for `Huawei CMTI' that returned http://forum.huawei.com/jive4/thread.jspa?threadID=325681&tstart=0&order....
There was no answer on the page, but I found an interesting fact: ttyUSB1 should be used instead of ttyUSB0.
Besides that, it also mentioned that CNMI is the AT command to set the modem to issue CMTI.
So, after googling for a specification that describes CNMI and fiddling with the modem using minicom, I found out that it was true that ttyUSB1 should be used instead of ttyUSB0 since the modem does not send the notification through ttyUSB0.
This article found via Google the other day also confirmed this: http://oozie.fm.interia.pl/pro/huawei-e220/
After a while, I came out with the following final configuration that makes the modem notifies Kannel whenever an SMS is received:
group = smsc
smsc = at
port = 10000
modemtype = huawei_e220_00
device = /dev/ttyUSB1
sms-center = [EUS: Sorry, the number is private]
my-number = [EUS: Sorry, the number is private]
connect-allow-ip = "127.0.0.1"
group = modems
id = huawei_e220_00
name = "Huawei E220"
detect-string = "huawei"
init-string = "AT+CNMI=2,1,2,2,0"
message-storage = sm
speed = 460800
Besides that, my hypothesis is proven to be correct since Kannel is able to fetch an SMS from location/index zero as supplied in the CMTI notification message. Therefore, there is no need to work on my patch any further.
You may find that /dev/ttyUSB1 is not the right one for you. For example, in another server I use /dev/ttyUSB3.
In that server, Kannel can successfully open /dev/ttyUSB0, but it does not receive the notification.
Using /dev/ttyUSB1, /var/log/kannel/bearerbox.log got the following error message:
2009-03-18 11:07:13 [5147] [6] INFO: AT2[/dev/ttyUSB1]: opening device
2009-03-18 11:07:13 [5147] [6] ERROR: AT2[/dev/ttyUSB1]: open failed! ERRNO=8
2009-03-18 11:07:13 [5147] [6] ERROR: System error 8: Exec format error
To conclude, setting up an SMS gateway with GNU/Linux Ubuntu 8.04, Kannel 1.4.1, and Huawei E220 GSM modem has been proven to work successfully by using the right SMSC configuration and USB-serial device node (e.g., /dev/ttyUSB1).
Keywords: Kannel SMS Huawei E220 Ubuntu 8.04
- jlquesada's blog
- Login to post comments