Tuesday, September 4, 2012

How to connect a cellular modem on your Android and BeagleBoard on boot up

Last week, I wrote a tutorial about how to get a 3G/4G cellular modem working on my Android/BeagleBoard setup. The next question (after everything works) is, how can I get this running automatically every time I boot up my Android device?

image
Boot scripts are nothing too fancy, and anyone with some basic Linux or Android experience can navigate this process pretty easily. But I always find it helpful to have the actual scripts written out. As a beginner, it’s a great way to figure out what the commands actually mean, and as an advanced developer, it’s a huge timesaver to copy and paste what’s already been done.

As a quick recap, I'm running all of this off my BeagleBoard xM, BeagleTouch, and Android Ambrosia SD card/image. I decided to use a T-Mobile 4G Rocket 3.0 USB cellular modem, but any USB cellular modem should work, with some tweaks to the vendor/product ID as well as the chat script (more on that in the previous post).

Outline the process

Looking through the original 3G/4G cellular modem integration process, I needed to do 4 things:

1) Get usb_modeswitch and usbserial.ko onto your Android setup

2) Execute usb_modeswitch using the correct usb_modeswitch.conf config file

3) Once modeswitch was successful, install usbserial.ko module to the correct vendor and product ID for the device

4) Execute chat script using pppd command

Wrap up relevant components into a script

I created a file called init_cellular_pppd.sh, which contains the relevant content and commands to create the usb_modeswitch.conf file as well as the dialtest.chat script.

#!/system/bin/sh
#This is a wrapper script to enable cellular wireless
TAG="Cellular"

#
# echo usb modeswitch config
#
echo_modeswitch_conf_zte() {
echo "$TAG building modeswitch config"
cat > /system/etc/usb_modeswitch.conf << EOF

########################################################
# ZTE devices

DefaultVendor=  0x19d2
DefaultProduct= 0x2000
TargetVendor=   0x19d2
TargetProductList="0001,0002,0015,0016,0017,0031,0037,0052,0055,0063,0064,0066,0091,0108,0117,0128,0157,2002,2003"
MessageContent="5553424312345678000000000000061e000000000000000000000000000000"
MessageContent2="5553424312345679000000000000061b000000020000000000000000000000"
MessageContent3="55534243123456702000000080000c85010101180101010101000000000000"

NeedResponse=1
CheckSuccess=20
EOF
}

#
# echo chatscript
#
echo_chatscript_tmobile() {
echo "$TAG building chatscript"
cat > /system/etc/ppp/dialtest.chat << EOF
TIMEOUT 10
ABORT 'BUSY'
ABORT 'NO ANSWER'
ABORT 'ERROR'
SAY 'Starting GPRS connect script\n'

""'ATZ'
SAY 'Setting APN\n'
OK 'AT+CGDCONT=1,"IP","wap.voicestream.com"'

ABORT 'NO CARRIER'
SAY 'Dialing...\n'
OK 'ATD*99***1#'
CONNECT
EOF
}

#
# Switch USB modes
#
switch_usb() {
    echo "$TAG switching USB mode" && \
    usb_modeswitch -I -W -c /system/etc/usb_modeswitch.conf && \
    sleep 5 && \
    echo "$TAG mode switch success" && \
    return 0
}

#
# Start T Mobile firmware
#
start_tmobile() {
    rmmod usbserial
    echo "$TAG tmobile firmware provider"

    echo_modeswitch_conf_zte
    echo_chatscript_tmobile

    switch_usb
    echo "$TAG installing firmware"
    busybox insmod /system/etc/firmware/usbserial.ko vendor=0x19d2 product=0x0157
   
    echo "$TAG starting pppd"
    /system/bin/pppd /dev/ttyUSB1 persist defaultroute usepeerdns -detach crtscts noauth debug connect "/system/xbin/chat -v -s -f /system/etc/ppp/dialtest.chat" &
}


Insert command to run script into Android’s init.rc bootup commands

You’ll find init.rc in the root folder on your Android device.

I placed the init_cellular_pppd.sh script in /system/etc/ppp, though it could technically go anywhere – as long as it’s being called in the right location, and the files it generates are going to the right places as well.

Then I just added these two lines to Android’s init.rc bootup script:

service cellular_up /system/bin/logwrapper /system/xbin/busybox sh -c /system/etc/ppp/init_cellular_pppd.sh
    oneshot


This ensures that I execute init_cellular_pppd.sh every time I boot, which in turn generates the files I need and executes the commands required to have a fully functional 3G/4G cellular connection every time I boot up my Android device, provided I have the modem plugged in.

In my last blog post, I cover which parameters you’ll want to tweak if you’re using a different modem or cellular provider, but if you have any questions or suggestions, I’d love to hear it.

Email me at justin@liquidware.com or follow me on twitter @liquidware

2 comments:

EMW SEO said...

Thanks for sharing a wonderful information about android.

Android Programming

Android Developers

Jackie Champion said...

Hi there! I'll be looking forward for your other posts. Keep it up! You have such a wonderful post! I can really relate to what you have written here. Thank you for sharing this very interesting and informative post. Whether we are sampling in-store or out in the community, C.A. Courtesy reaches your target at the point of impact. We focus on brand trial and brand awareness with the ultimate goal of increasing sales and profitability. Product Demo Washington DC