1/* 2 * Copyright (C) 2010, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.telephony.mockril; 18 19import android.os.Bundle; 20import android.util.Log; 21 22import com.android.internal.communication.MsgHeader; 23import com.android.internal.communication.Msg; 24import com.android.internal.telephony.RilChannel; 25import com.android.internal.telephony.ril_proto.RilCtrlCmds; 26import com.android.internal.telephony.ril_proto.RilCmds; 27import com.google.protobuf.micro.MessageMicro; 28 29import java.io.IOException; 30 31/** 32 * Contain a list of commands to control Mock RIL. Before using these commands the devices 33 * needs to be set with Mock RIL. Refer to hardware/ril/mockril/README.txt for details. 34 * 35 */ 36public class MockRilController { 37 private static final String TAG = "MockRILController"; 38 private RilChannel mRilChannel = null; 39 private Msg mMessage = null; 40 41 public MockRilController() throws IOException { 42 mRilChannel = RilChannel.makeRilChannel(); 43 } 44 45 /** 46 * Close the channel after the communication is done. 47 * This method has to be called after the test is finished. 48 */ 49 public void closeChannel() { 50 mRilChannel.close(); 51 } 52 53 /** 54 * Send commands and return true on success 55 * @param cmd for MsgHeader 56 * @param token for MsgHeader 57 * @param status for MsgHeader 58 * @param pbData for Msg data 59 * @return true if command is sent successfully, false if it fails 60 */ 61 private boolean sendCtrlCommand(int cmd, long token, int status, MessageMicro pbData) { 62 try { 63 Msg.send(mRilChannel, cmd, token, status, pbData); 64 } catch (IOException e) { 65 Log.v(TAG, "send command : %d failed: " + e.getStackTrace()); 66 return false; 67 } 68 return true; 69 } 70 71 /** 72 * Get control response 73 * @return Msg if response is received, else return null. 74 */ 75 private Msg getCtrlResponse() { 76 Msg response = null; 77 try { 78 response = Msg.recv(mRilChannel); 79 } catch (IOException e) { 80 Log.v(TAG, "receive response for getRadioState() error: " + e.getStackTrace()); 81 return null; 82 } 83 return response; 84 } 85 86 /** 87 * @return the radio state if it is valid, otherwise return -1 88 */ 89 public int getRadioState() { 90 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_GET_RADIO_STATE, 0, 0, null)) { 91 return -1; 92 } 93 Msg response = getCtrlResponse(); 94 if (response == null) { 95 Log.v(TAG, "failed to get response"); 96 return -1; 97 } 98 response.printHeader(TAG); 99 RilCtrlCmds.CtrlRspRadioState resp = 100 response.getDataAs(RilCtrlCmds.CtrlRspRadioState.class); 101 int state = resp.getState(); 102 if ((state >= RilCmds.RADIOSTATE_OFF) && (state <= RilCmds.RADIOSTATE_NV_READY)) 103 return state; 104 else 105 return -1; 106 } 107 108 /** 109 * Set the radio state of mock ril to the given state 110 * @param state for given radio state 111 * @return true if the state is set successful, false if it fails 112 */ 113 public boolean setRadioState(int state) { 114 RilCtrlCmds.CtrlReqRadioState req = new RilCtrlCmds.CtrlReqRadioState(); 115 if (state < 0 || state > RilCmds.RADIOSTATE_NV_READY) { 116 Log.v(TAG, "the give radio state is not valid."); 117 return false; 118 } 119 req.setState(state); 120 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_SET_RADIO_STATE, 0, 0, req)) { 121 Log.v(TAG, "send set radio state request failed."); 122 return false; 123 } 124 Msg response = getCtrlResponse(); 125 if (response == null) { 126 Log.v(TAG, "failed to get response for setRadioState"); 127 return false; 128 } 129 response.printHeader(TAG); 130 RilCtrlCmds.CtrlRspRadioState resp = 131 response.getDataAs(RilCtrlCmds.CtrlRspRadioState.class); 132 int curstate = resp.getState(); 133 return curstate == state; 134 } 135 136 /** 137 * Start an incoming call for the given phone number 138 * 139 * @param phoneNumber is the number to show as incoming call 140 * @return true if the incoming call is started successfully, false if it fails. 141 */ 142 public boolean startIncomingCall(String phoneNumber) { 143 RilCtrlCmds.CtrlReqSetMTCall req = new RilCtrlCmds.CtrlReqSetMTCall(); 144 145 req.setPhoneNumber(phoneNumber); 146 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_SET_MT_CALL, 0, 0, req)) { 147 Log.v(TAG, "send CMD_SET_MT_CALL request failed"); 148 return false; 149 } 150 return true; 151 } 152 153 /** 154 * Hang up a connection remotelly for the given call fail cause 155 * 156 * @param connectionID is the connection to be hung up 157 * @param failCause is the call fail cause defined in ril.h 158 * @return true if the hangup is successful, false if it fails 159 */ 160 public boolean hangupRemote(int connectionId, int failCause) { 161 RilCtrlCmds.CtrlHangupConnRemote req = new RilCtrlCmds.CtrlHangupConnRemote(); 162 req.setConnectionId(connectionId); 163 req.setCallFailCause(failCause); 164 165 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_HANGUP_CONN_REMOTE, 0, 0, req)) { 166 Log.v(TAG, "send CTRL_CMD_HANGUP_CONN_REMOTE request failed"); 167 return false; 168 } 169 return true; 170 } 171 172 /** 173 * Set call transition flag to the Mock Ril 174 * 175 * @param flag is a boolean value for the call transiton flag 176 * true: call transition: dialing->alert, alert->active is controlled 177 * false: call transition is automatically handled by Mock Ril 178 * @return true if the request is successful, false if it failed to set the flag 179 */ 180 public boolean setCallTransitionFlag(boolean flag) { 181 RilCtrlCmds.CtrlSetCallTransitionFlag req = new RilCtrlCmds.CtrlSetCallTransitionFlag(); 182 183 req.setFlag(flag); 184 185 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_SET_CALL_TRANSITION_FLAG, 0, 0, req)) { 186 Log.v(TAG, "send CTRL_CMD_SET_CALL_TRANSITION_FLAG request failed"); 187 return false; 188 } 189 return true; 190 } 191 192 /** 193 * Set the dialing call to alert if the call transition flag is true 194 * 195 * @return true if the call transition is successful, false if it fails 196 */ 197 public boolean setDialCallToAlert() { 198 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_SET_CALL_ALERT, 0, 0, null)) { 199 Log.v(TAG, "send CTRL_CMD_SET_CALL_ALERT request failed"); 200 return false; 201 } 202 return true; 203 } 204 205 /** 206 * Set the alert call to active if the call transition flag is true 207 * 208 * @return true if the call transition is successful, false if it fails 209 */ 210 public boolean setAlertCallToActive() { 211 if (!sendCtrlCommand(RilCtrlCmds.CTRL_CMD_SET_CALL_ACTIVE, 0, 0, null)) { 212 Log.v(TAG, "send CTRL_CMD_SET_CALL_ACTIVE request failed"); 213 return false; 214 } 215 return true; 216 } 217} 218