Collect GPRS wireless Internet access notes in Linux

Source: Internet
Author: User

This article uses the GPRS module in Linux to implement wireless Internet access. The code and method are difficult to understand, so you must read the code patiently. I believe you can learn a lot.

 

Dial-up Internet access through GPRS module in Linux

First from the/usr/share/doc/ppp-2.4.4/scripts cp ppp-on, PPP-off, PPP-on-dialer three scripts to/home/A/PPP. Then make the following changes:

In PPP-on

1. Change the phone number to * 99 *** 1 #

2. Clear the account and password

3. Change the dialer_script path to/home/A/PPP-on-dialer.

4. Change the device to/dev/ttys0 and the speed to 115200.

5. Remove the crtscts parameter,

Remove the account and password in PPP-on-dialer.

The following describes the script. Of course, they are all searched online. :-)

/Home/A/PPP-on

#! /Bin/sh

#

# Script to initiate a PPP connection. This is the first part of

# Pair of scripts. This is not a secure pair of scripts as the Codes

# Are visible with the 'ps' command. However, it is simple.

#

# These are the parameters. Change as needed.

Telephone = * 99 *** 1 # the telephone number for the connection

Account = # the account name for Logon (as in 'George Burns ')

Password = # The password for this account (and 'gracie Allen ')

Local_ip = 0.0.0.0 # local IP address if known. Dynamic = 0.0.0.0

Remote_ip = 0.0.0.0 # remote IP address if desired. Normally 0.0.0.0

Netmask = 255.255.255.0 # The proper netmask if needed

#

# Export them so that they will be available at 'ppp-on-dialer' time.

Export telephone account password

#

# This is the location of the script which dials the phone and logs

# In. Please use the absolute file name as the $ PATH variable is not

# Used on the connect option. (to do so on a 'root' account wocould be

# A security hole so don't ask .)

#

Dialer_script =/home/A/PPP-on-dialer

#

# Initiate the connection

#

# I put most of the common options on this command. Please, don't

# Forget the 'lock' option or some programs such as mgetty will not

# Work. The asyncmap and escape will permit the PPP link to work

# A telnet or rlogin connection. You are welcome to make any changes

# As desired. Don't use the 'defaultroute 'option if you currently

# Have a default route to an Ethernet gateway.

#

Exec/usr/sbin/pppd debug lock modem/dev/ttys0 115200 \

Asyncmap 20a0000 escape FF kdebug 0 $ local_ip: $ remote_ip \

Noipdefault netmask $ netmask defaultroute connect $ dialer_script

Exec/usr/sbin/pppd debug lock modem/dev/ttys0 115200 \

Asyncmap 20a0000 escape FF kdebug 0 $ local_ip: $ remote_ip \

Noipdefault netmask $ netmask defaultroute connect $ dialer_script: (you can use man pppd to view help)

Run the/usr/sbin/pppd program and pass the command line parameters to it. The significance is:

/Dev/ttys0: Serial Port connected by the modem used for dialing (ttys0 is Serial Port 1, ttys1 is Serial Port 2 ...)

115200: MODEM communication rate (57600 bps)

Modem: use modem for dialing.

Nocrtscts: no traffic control

Lock: Lock this device

Debug: Output debugging information

Defaultroute: this dial-up connection serves as the default route

Connect/home/A/PPP-on-dialer: Use/home/A/PPP-on-dialer for dialing

/Home/A/PPP-on-dialer

#! /Bin/sh

#

# This is part 2 of the PPP-on script. It will perform the connection

# Protocol for the desired connection.

#

Exec chat-V \

Timeout 3 \

Abort '\ nbusy \ R '\

Abort '\ Nno answer \ R '\

Abort '\ nringing \ r \ n \ r \ nringing \ R '\

''\ Rat \

'OK-++ \ c-OK' ATH0 \

Timeout 30 \

OK at + IPR = 115200 \

OK at + cgdcont = 1, "ip", "cmnet "\

OK at + cgact = 1, 1 \

OK atdt * 99 *** 1 #\

Connect ''\

# Ogin: -- ogin: $ account \

# Assword: $ Password

For more information about chat commands, see man-a chat.

Abort busy

Abort Error

Abort no carrier

Abort no dialtone

Abort Ring

OK at + IPR = 115200

OK at + cgact = 1, 1

/Home/A/PPP-off

#! /Bin/sh

######################################## ######

# Determine the device to be terminated.

#

If ["$1" = ""]; then

Device = ppp0

Else

Device = $1

Fi

######################################## #######

# If the ppp0 PID file is present then the program is # running. Stop it.

If [-r/var/run/$ device. pid]; then

Kill-int 'cat/var/run/$ device. Pi'

#

# If the kill did not work then there is no process running for this

# PID. It may also mean that the lock file will be left. You may wish

# To delete the lock file at the same time.

If [! "$? "=" 0 "]; then

Rm-F/var/run/$ device. PID

Echo "error: removed stale PID file"

Exit 1

Fi

#

# Success. Let pppd clean up its own junk.

Echo "PPP link to $ device terminated ."

Exit 0

Fi

#

# The PPP process is not running for ppp0

Echo "error: PPP link is not active on $ device"

Exit 1

 

 

 

Int main (void)
{

U8_t Buf [max_read_len + 1];
Int Len, PD;
Sys_err err;

Int * FD = NULL;
Int * linkstatecx = NULL;

/*
* Init basic parametes of ourselves
*/
Gpioinit (); // gpio port Initialization
Sys_timer_init (); // system clock Initialization
Local_sys_init (); // There are serial port initialization and timer initialization.
Lwip_init (); // LWIP protocol initialization

Debug_event ("Main () \ r \ n"); // debug_event is a function that outputs prompt information to the serial port.

/*
* Start config GPRS Modem, such as Huawei gtm900, mc35i, etc.
*/
Err = active_modem (); // activates the GPRS module.

If (err neq sys_err_ OK)
{
Debug_event ("modem open failed! We must shut down system! \ R \ n ");
Shut_system ();
}
Debug_event ("modem open successful! \ R \ n ");
Debug_event ("system block 30 seconds! \ R \ n "); // wait 30 seconds until the SIM card finds the network

/*
* Wait for GPRS Modem serching network,
* Initinalize: such as SMS, telephone book etc.
*/
Second_sleep (30 );

Err = init_modem (); // write some basic AT commands
If (err neq sys_err_ OK)
{
Debug_event ("modem basic AT commands initialize failed! We must Shut Down System \ r \ n ");
Shut_system ();
}
Debug_event ("modem basic AT commands initialize successful! \ R \ n ");

/*
* Start dial GPRS number, such as: * 99 *** 1 #
*/
Start_tcpip: // start_tcpip is a label
Err = dial_gprs (); // dial ATD * 99 *** 1 #

If (err neq sys_err_ OK)
{
Debug_event ("dial GPRS failed, we must shut down system! \ R \ n ");
Shut_system ();
}
Debug_event ("dial GPRS successful, Now we start PPP config! \ R \ n ");

/*
* Start config PPP, and send LCP packet to GPRS Modem
*/
Pd = pppopen (void *) FD, & pppstatecallback, (void *) linkstatecx);/* Send LCP req to peer */

While (1)
{
/*
* Check if one timer is overflow, if so, run it
*/
Check_sys_timer ();

/*
* Check the Receive Buffer of modem
*/
If (get_modem_datalen ()> 0) // If the modem receives the message
{
Debug_event ("receive some data from modem: \ r \ n ");
Len = get_modem_data (BUF); // receives
Pppinproc (PD, Buf, Len); // Process
}

/*
* Check the Receive Buffer of user equipment
*/
If (get_user_datalen ()> 0) // if the user sends a message to the modem
{
Debug_event ("receive some data from user Eqipment! \ R \ n ");

Len = get_user_data (BUF); // receives

If (get_mytcpip_state () eq udp_established) // you can determine which connection exists.
{
If (udp_write (BUF, Len) NEQ err_ OK)
{
Debug_err ("send some data to UDP peer, but failed! \ R \ n ");
// Add you code here.
// Store the data
}
}
Else if (get_mytcpip_state () eq tcp_established) // you can determine which connection exists.

{
If (tcp_write (tcp_active_pcb, (const void *) BUF, Len, 1) NEQ err_ OK)
{
Debug_err ("send some data to TCP peer, but failed! \ R \ n ");
// Add you code here.
// Store the data
}
}
Else
{
Debug_event ("no TCP or UDP connect exist! Drop the data \ r \ n ");
}
}

/*
* Check the state of PPP
* If the link is dead, I deal it very simple,
* Do not release the source of PPP/TCP/IP/udp etc.
* Just call the function: lwip_init (). But it work.
* Then goto the head of main func.
* The State of DCD shoshould be checked here, but I will ignore it.
*/
If (ppp_dead (PD) eq true) // if the link is dead
{
Lwip_init ();
Set_mytcpip_state (ppp_dead );
Goto start_tcpip; // go back and dial again
}

/*
* Check the state of my TCPIP
*/
Switch (get_mytcpip_state () // check the current status
{
Case ppp_established: // The status of the grip.
If (get_sys_err () <max_sys_err_allow)
{
Tcpip_open (); // enable tcp_ip after the handshake is completed (in this function, you can choose whether to use UDP or TCP, The udp I selected)
Break;
}
Else
Set_mytcpip_state (tcpip_state_unknown );

Case tcpip_state_unknown:
Shut_system ();
Break;

Default:
Break;
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.