Ril.java分析

Framework层Ril控制流程分析

RIL Native层分析:http://www.cnblogs.com/bastard/archive/2012/11/05/2754891.html

一 RIL整体框架

看一下整个RIL部分框架图:

    

  实际上Framework部分比较复杂的,包含了很多类;但其核心的两个类是GSMPhone/,RIL.Java.

还包括围绕这两个类构成的状态管理,命令交互的类。

二 PhoneApp 启动过程

在AndroidManifest.xml文件中:

  <application android:name="PhoneApp"

     android:persistent="true"

     …… 

         在开机启动时到ActivityManagerService的systemReady时,会将带有这个属性的App启动。

调用应用的Application——PhoneApp:

复制代码
public class PhoneApp extends Application
{
    @Override
    public void onCreate() {
         //初始化framework层telephony相关
         PhoneFactory.makeDefaultPhones(this);
     Phone phone = PhoneFactory.getDefaultPhone();
         
         CallManager mCM = CallManager.getInstance();
         mCM.registerPhone(phone);
    }
}
复制代码

PhoneFactory初始化Framework层相关对象:  

复制代码
public static void makeDefaultPhone(Context context) 
{
    sPhoneNotifier = new DefaultPhoneNotifier();
    
    //创建CommandsInterface实例RIL
    sCommandsInterface = new RIL(context, networkMode, cdmaSubscription);
    
    // Instantiate UiccController so that all other classes can just call getInstance()
    UiccController.make(context, sCommandsInterface);
    
    //根据类型创建Phone实例
    int phoneType = getPhoneType(networkMode);
    //创建Phone实例GSMPhone 以及代理对象ProxyPhone
    sProxyPhone = new PhoneProxy(new GSMPhone(context,
                        sCommandsInterface, sPhoneNotifier));
}
复制代码

  这个sCommandsInterface很关键 是连接GSMPhone与RIL。

  整个Framewrok Telephony中很关键的就是Phone和CommandInterface两个接口:

核心就是围绕这两个接口派生的类;还有各种对Phone进行了包装的类。

  Phone:Internal interface used to control the phone;

  CommandInterface:提供跟native层Ril交互的接口发送命令和接受命令,注册状态传递回调接口,向上层传递状态变化。

下面是这几个核心类的结构图:

    

三 命令控制流程

         命令从Phone开始,在传递到RIL,在通过Socket传递给Rild。下面看一下framework中RIL类工作流程。

与Native端交互过程:

    

  RIL提供了call和connection过程命令接口,状态变化回调,unsolicited事件通知。

类结构图:

    

 

  其中关键的两个线程:sender和receiver ,运行单独的线程中处理向下和向上的请求。

RIL构造函数:

复制代码
public RIL(Context context, ……) {    
    //创建sender运行线程 
    mSenderThread = new HandlerThread("RILSender");
    mSenderThread.start();
    //send消息处理循环
    Looper looper = mSenderThread.getLooper();
    mSender = new RILSender(looper);
    
    //建立receiver运行的线程
    mReceiver = new RILReceiver();
    mReceiverThread = new Thread(mReceiver, "RILReceiver");
    mReceiverThread.start();
}

1 send事件流程

dial拨号流程:

 

public void dial(String address, int clirMode, 
                UUSInfo uusInfo, Message result) {
                                    
    //获取一个RILRequest对象 请求对象类型
    RILRequest rr = RILRequest.obtain(RIL_REQUEST_DIAL, result);
    //将数据打包成parcel
    rr.mp.writeString(address);
    rr.mp.writeInt(clirMode);
    rr.mp.writeInt(0); // UUS information is absent
    ……
    //将数据发送到send线程中处理 内部公共接口send
    send(rr);
}
    
private void send(RILRequest rr) {
    //Handler异步处理消息
    msg = mSender.obtainMessage(EVENT_SEND, rr);
    msg.sendToTarget();
} 

 

Send消息处理过程:

class RILSender extends Handler implements Runnable {
  public RILSender(Looper looper) {
      super(looper);
 }

  // Only allocated once
  byte[] dataLength = new byte[4];

  public void run() {
      //setup if needed
  }

  handleMessage(Message msg) {
      RILRequest rr = (RILRequest)(msg.obj);
      RILRequest req = null;

      switch (msg.what) {
        case EVENT_SEND:
          //与rild通信socket
          LocalSocket s;
          s = mSocket;    
      //加入请求队列
          synchronized (mRequestsList) {
              mRequestsList.add(rr);
              mRequestMessagesWaiting++;
          }
          byte[] data;
          data = rr.mp.marshall();
          //将将数据通过socket传入到rild进程中
          s.getOutputStream().write(dataLength);
          s.getOutputStream().write(data);
            break;
    }
  }
}

         在send线程中将数据通过socket传递给rild进程处理。

2 receive事件流程

Receiver接收rild发送来的事件以及send事件的response事件:

 

class RILReceiver implements Runnable {
  byte[] buffer;
  RILReceiver() {
      buffer = new byte[RIL_MAX_COMMAND_BYTES];
  }
    public void run() {
        String rilSocket = "rild";
        //循环处理rild传递来的事件
        for (;;) {
            //创建于rild通信的socket 建立连接
            s = new LocalSocket();
            l = new LocalSocketAddress(rilSocket,
                            LocalSocketAddress.Namespace.RESERVED);
            s.connect(l);
              mSocket = s;
              //读取socket数据
            InputStream is = mSocket.getInputStream();
        
          for (;;) {
              //解析数据
              Parcel p;
              length = readRilMessage(is, buffer);
              p = Parcel.obtain();
              p.unmarshall(buffer, 0, length);
              p.setDataPosition(0);
              
              //处理rild传递来的消息
              processResponse(p);
          }
        
          setRadioState (RadioState.RADIO_UNAVAILABLE);
          mSocket.close();
          // Clear request list on close
          clearRequestsList(RADIO_NOT_AVAILABLE, false);
      }
    }
}
    private void processResponse (Parcel p) {
  type = p.readInt();
    //新事件Or send事件response
  if (type == RESPONSE_UNSOLICITED) {
      processUnsolicited (p);
  } else if (type == RESPONSE_SOLICITED) {
      processSolicited (p);
  }
}

网络端事件处理流程:

 

private void processUnsolicited (Parcel p) {
    Object ret;
    int response = p.readInt();
    switch(response) {
        //来电
        case RIL_UNSOL_CALL_RING: 
            ret =  responseCallRing(p); break;
        ……
    }
    
    switch(response) {
        case RIL_UNSOL_CALL_RING:
            //通知注册来电事件的register
            if (mRingRegistrant != null) {
                mRingRegistrant.notifyRegistrant(
                    new AsyncResult (null, ret, null));
            }
            break;
        ……
    }
} 

分析:主动上报处理,判断来电后responseCallRing(p) 处理,结果返回给ret;接着将ret通知相关注册事件了。

 

3 receiver事件通知register机制

对事件状态的注册是在RIL的父类BaseCommands完成的。

BaseCommands类注册事件的实现:

 

public abstract class BaseCommands implements CommandsInterface {
    //注册到列表中
    protected RegistrantList mRadioStateChangedRegistrants = new RegistrantList();
    protected RegistrantList mCallStateRegistrants = new RegistrantList();
    protected RegistrantList mDataNetworkStateRegistrants = new RegistrantList();
    ……
    
    //注册某一事件发生
    protected Registrant mRingRegistrant;
    protected Registrant mSmsStatusRegistrant;
    protected Registrant mRestrictedStateRegistrant;
    ……
    
    //注册接口
    public void registerForRadioStateChanged(Handler h, int what, Object obj) {
        Registrant r = new Registrant (h, what, obj);
        
        synchronized (mStateMonitor) {
            mRadioStateChangedRegistrants.add(r);
            r.notifyRegistrant();
        }
    }

    //注册回调
    public void setOnCallRing(Handler h, int what, Object obj) {
        mRingRegistrant = new Registrant (h, what, obj);
    }
} 

这里有RegistrantList与Registrant类,注册事件以Registrant对象存储;

下面是Registrant类得主要成员:

 

public class Registrant{
    WeakReference   refH;
    int             what;
    Object          userObj;
    public Registrant(Handler h, int what, Object obj){
        refH = new WeakReference(h);
        this.what = what;
        userObj = obj;
    }
    
    public void notifyRegistrant(){
        internalNotifyRegistrant (null, null);
    }
    
    void internalNotifyRegistrant (Object result, Throwable exception){
      //通过Handler传递消息
    Handler h = getHandler();
    Message msg = Message.obtain();
    msg.what = what;
    msg.obj = new AsyncResult(userObj, result, exception);
    h.sendMessage(msg);
    }
} 

 

4.processSolicited函数是对命令请求---接受rild响应的处理:

      4.1  receiver不断从socket中读取数据,对于读取的数据,分两种类型处理,主动上报的和非主动上报的response,处理主动上报的函数是processUnsolicited,处理非主动上报的函数是processSolicited 。

      4.2  processSolicited 读取RIL请求序列号后,根据不同的Request号,用switch-case调用不同的responseXXX函数族获取AT执行结果(处理后放入ret中),然后将包含了结果数据的msg送给调用者 ,即放置到消息队列上。

        AsyncResult.forMessage(rr.mResult, ret, null);
        rr.mResult.sendToTarget

       4.3 对于processSolicited和processUnsolicited 而言,处理函数很多相似,procesSolicited函数对结果ret放到消息队列,发送给调用者;processUnsolicited函数对结果ret通过通知的方式广播出去。

      4.4 ril.java而言,一个RILReceiver线程;一个RILSender线程;RILRequest类。

            ------------- 对于和上层函数的接口dial函数-----调用RILRequest类,然后通过RILSender发送出去。

==============================================================================================================================

    

 

 

 

 

 这里说直接使用Handler,来实现了一个Observer模式,但是Handler是处理线程的框架类,实现异步的处理消息,具有更强的功能。

所以看到Telephony中很多类都是从Handler继承下来的,就是为了监听事件发生,状态变化等消息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值