Debug automatic dialing for 3G in android

Source: Internet
Author: User

Debug automatic dialing for 3G in android

This chapter briefly describes how android implements the automatic dialing function. This function is implemented using the rild service started by the system. Because the rild service cannot be killed, it can enable automatic redial after a dial failure or network disconnection to enhance the reliability of Internet access. Here, only the dialing function is implemented, and some functions implemented by the ril library are removed.

1. Modify the rild program source code

Delete all the files in.../hardware/ril, create the rild folder, and put the following code under the new folder.

1. rild. c

# Define LOG_TAG "RILD" # include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include "ril_fun.h" int main (int argc, char * argv []) {int opt; int err; pthread_t tid; char buf [PROPERTY_VALUE_MAX]; static int device_fd; static char * device_path = NULL; umask (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); while (-1! = (Opt = getopt (argc, argv, "d:") {// used to analyze the command line parameter switch (opt) {case 'D' passed by the main function ': device_path = optarg; ALOGD ("Opening tty device % s \ n", device_path); break; default: ALOGD ("Not tty device"); goto done ;}} device_fd = device_open (device_path); // enable device_init (device_fd) on the device node; // initialize the device node property_set ("ctl. stop ", PPPD_SERVICE_NAME); // set the pppd_gprs attribute to stop request_setup_datacall (device_fd); // continue executing the dialing pthread_create (& tid, NULL, ip_detection, (void *) device_fd ); // create an IP address query thread, disconnect the network, and then dial done again: while (1) {sleep (0x00ffffff);} return 0 ;}
     
    
   
  
 

2. ril_fun.c

# Define LOG_TAG "RIL" # include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include "ril_fun.h" # define PPPD_EXIT_CODE "net. gprs. ppp-exit "# define PPP_NET_LOCAL_IP" net. pp1_local-ip "# define PPP_SYSFS_RETRY 5 # define PPP_OPERSTATE_PATH"/sys/class/net/ppp0/operstate "static int pppd_started = 0; int device_open (char * device_path) {int device_fd =-1; while (device_fd <0) {if (device_path! = NULL) {device_fd = open (device_path, O_RDWR); // enable the device of the serial port AT module} if (device_fd <0) {ALOGD ("opening AT interface. retrying... "); sleep (3) ;}} return device_fd;} void device_init (int device_fd) {struct termios options; tcgetattr (device_fd, & options ); // obtain the serial port attribute cfsetispeed (& options, B115200); // set the receiving baud rate cfsetospeed (& options, B115200); // set the sending baud rate options. c_iflag & = ~ (IGNBRK | BRKINT | PARMRK | ISTRIP | IGNCR | ICRNL | IXON); options. c_cflag & = ~ PARENB; // No parity bit options. c_cflag & = ~ CSTOPB; // The stop bit is 1-bit options. c_cflag & = ~ CSIZE; options. c_cflag | = CS8; // The data bit is 8-bit options. c_lflag & = ~ (ICANON | ECHO | ECHOE | ISIG); tcsetattr (device_fd, TCSANOW, & options);} int at_send_command (int device_fd, char * cmd) {int ret; ret = write (device_fd, cmd, strlen (cmd); return ret;} void request_setup_datacall (int device_fd) {const char * apn = "cmnet"; char * cmd = NULL; int err; ALOGD ("requesting data connection to APN '% S'", apn); if (pppd_started) {ALOGD ("Stop existing PPPd before activating PDP"); pro Perty_set ("ctl. stop ", PPPD_SERVICE_NAME); // set the pppd_gprs attribute to stop pppd_started = 0;} err = at_send_command (device_fd," at $ qcpdplt = 0 "); asprintf (& cmd, "AT + CGDCONT = 1, \" IP \ ", \" % s \ ",", apn); // set the PDP context err = at_send_command (device_fd, cmd); free (cmd); if (err <0) {ALOGD ("AT Send Command error! ");} Err = property_set (PPPD_EXIT_CODE," "); // Set net. gprs. ppp-exit to null if (err <0) {ALOGW (" Set PPPD_EXIT_CODE failed! "); Goto ppp_error;} err = property_set (PPP_NET_LOCAL_IP," "); // set net. pp1_local-ip is empty if (err <0) {ALOGW ("Set PPPD_NET_LOCAL_IP failed! "); Goto ppp_error;} err = property_set (" ctl. start ", PPPD_SERVICE_NAME); // set the pppd_gprs attribute to start pppd_started = 1; if (err <0) {ALOGW (" Can not start PPPd "); goto ppp_error ;} ALOGD ("PPPd started"); return; ppp_error: at_send_command (device_fd, "AT + CGACT ="); // the PDP context to activate if (pppd_started) {property_set ("ctl. stop ", PPPD_SERVICE_NAME); pppd_started = 0 ;}} void * ip_detection (void * arg) {int fd; int err; int devic E_fd = (int) arg; char buffer [20]; char exit_code [PROPERTY_VALUE_MAX]; static char local_ip [PROPERTY_VALUE_MAX]; static int pppd_started = 0; sleep (2 ); while (1) {property_get (PPPD_EXIT_CODE, exit_code, ""); // obtain the exit status of pppd if (strcmp (exit_code ,"")! = 0) {// check if pppd exits ALOGW abnormally ("PPPd exit with code % s", exit_code); request_setup_datacall (device_fd); // continue executing the dialing goto done ;} fd = open (PPP_OPERSTATE_PATH, O_RDONLY); // read/sys/class/net/ppp0/operstate to monitor the status of data network data if (fd> = 0) {buffer [0] = 0; read (fd, buffer, sizeof (buffer); close (fd); ALOGW ("buffer = % s", buffer); if (! Strncmp (buffer, "up", strlen ("up") |! Strncmp (buffer, "unknown", strlen ("unknown") {memset (local_ip, '\ 0', sizeof (local_ip); err = property_get (PPP_NET_LOCAL_IP, local_ip, ""); // Get IPif (err <0) {ALOGW ("Get PPPD_NET_LOCAL_IP failed! ");} If (! Strcmp (local_ip, "") {ALOGW ("PPP link is up but no local IP is assigned. will retry times after % d seconds ", PPP_SYSFS_RETRY); property_set (" ctl. stop ", PPPD_SERVICE_NAME); // if no IP address is available, stop pppd} else {ALOGD (" PPP link is up with local IP address % s ", local_ip );}} else {ALOGW ("PPP link status in % s is % s. will retry times after % d seconds ", \ PPP_OPERSTATE_PATH, buffer, PPP_SYSFS_RETRY); property_set (" ctl. stop ", PPPD_SERVICE_NAME); // if the data network status is incorrect, stop pppd} else {ALOGW (" Can not detect PPP state in % s. will retry times after % d seconds ", \ PPP_OPERSTATE_PATH, PPP_SYSFS_RETRY); request_setup_datacall (device_fd); // continue dialing} done: sleep (PPP_SYSFS_RETRY);} return NULL ;}
        
       
      
     
    
   
  
 

3. ril_fun.h

# Ifndef _ RIL_FUN_H _ # define _ RIL_FUN_H _ # include
 
  
# Include
  
   
// ALOG * () # include
   
    
// Property_set () # definePPPD_SERVICE_NAME "pppd_gprs "/********************************** * *************************** function: open the serial device file * parameter: device_path: serial device file name * returned value: ************************************ * ************************/int device_open (char * device_path ); /*************************************** * *********************** function: initialize the device node * parameter: device_fd: serial device file descriptor * return value: no *************************************** * *********************/void device_init (int device_fd ); /*************************************** * *********************** function: send AT command function * parameter: device_fd: serial device file descriptor * cmd: AT command * return value: ret: status *************************************** * *********************/int at_send_command (int device_fd, char * cmd ); /*************************************** * *********************** function: set the dialing function * parameter: device_fd: serial device file descriptor * returned value: no *************************************** * *********************/void request_setup_datacall (int device_fd ); /*************************************** * *********************** function: create an IP address query thread * parameter: arg: void type pointer * return value: NULL *************************************** * *********************/void * ip_detection (void * arg ); # endif
   
  
 

4. Android. mk

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES:= \rild.c \ril_fun.cLOCAL_SHARED_LIBRARIES := \libcutils \libdlLOCAL_LDLIBS += -lpthreadLOCAL_MODULE:= rildLOCAL_MODULE_TAGS := optionalinclude $(BUILD_EXECUTABLE)

After the preceding files are added, compile the files. If the files are compiled before the compilation, run make clean.

2. Place the following script under/etc/ppp on the board.

1. init. gprs-pppd

#! /System/bin/sh # An unforunate wrapper script # so that the exit code of pppd may be retrievedPPPD_PID =/system/bin/setprop "net. gprs. ppp-exit "" # Set net. gprs. ppp-exit is empty/system/bin/log-t pppd "Starting pppd"/system/bin/pppd connect 'chat-v-s-r "/var/log/chat. log "-f"/etc/ppp/3gdata_call.conf "'disconnect \ 'chat-r"/var/log/chat. log "-t 30-e-v" "++ ATH" no carrier "'/dev/ttyusb3115200 mru 12 80 mtu 1280 \ nodetach debug defaultroute usepeerdns crtscts user card password card noipdefault ipcp-accept-local \ ipcp-accept-remotePPPD_EXIT = $? # Obtain the returned value PPPD_PID after executing the command =$! # PID/system/bin/log-t pppd "pppd exited with $ PPPD_EXIT"/system/bin/setprop "net. gprs. ppp-exit "" $ PPPD_EXIT "# Set the return value to net. gprs. ppp-exit

Note:/dev/ttyUSB3 in the script is the Modem port of the module USB serial port.

2. Define data_call.conf

ABORT "NO CARRIER"ABORT "NO DIALTONE"ABORT "ERROR"ABORT "NO ANSWER"ABORT "BUSY"TIMEOUT 120"" atOK atd*99***1#CONNECT

 

3. Modify the ini. rc Script

Before modifying init. rc, you must compile the source code first, because the modified file is the file generated after compilation, because it is a little troublesome to change the source file.

Open.../out/target/product/sabresd_6dq/root/init. rc and modify it in the red box below.

Add a statement to modify the permissions of a new object.

Note:/dev/ttyUSB2 is the AT port of the module USB serial port.

At this point, you can pack make snod and burn the image for testing.

Run logcat-B radio to view the output information of rild for debugging. If everything is normal, the IP address will be printed (or you can run the command necfg to check the IP address). ping 202.108.22.5 (Baidu IP) to check whether the network can be pinged. If the ping command is successful, the automatic dialing is successful. Run the command ps to view the rild and pppd process numbers, run the command kill-9 <process number> to kill any process, and check if the process is running again after 5s.

 

 

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.