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 17 package com.android.internal.telephony; 18 19 import android.util.Log; 20 21 import java.io.IOException; 22 import java.net.InetSocketAddress; 23 import java.nio.channels.SocketChannel; 24 import java.nio.ByteBuffer; 25 26 public class RilChannel { 27 private static final boolean DBG = false; 28 29 private static final String MOCKRIL_ADDR_STR = "127.0.0.1"; 30 private static final int MOCKRIL_PORT = 54312; 31 private SocketChannel mChannel = null; 32 33 /** 34 * Constructor 35 */ 36 private RilChannel() { 37 if (DBG) log("ctor EX"); 38 } 39 40 /** 41 * Open the channel 42 * 43 * @throws IOException 44 */ 45 private void open() throws IOException { 46 InetSocketAddress mockRilAddr = new InetSocketAddress(MOCKRIL_ADDR_STR, MOCKRIL_PORT); 47 mChannel= SocketChannel.open(mockRilAddr); 48 } 49 50 /** 51 * Make the channel 52 * 53 * @return the channel 54 * @throws IOException 55 */ 56 public static RilChannel makeRilChannel() throws IOException { 57 if (DBG) log("makeMockRilChannel E"); 58 RilChannel sm = new RilChannel(); 59 sm.open(); 60 if (DBG) log("makeMockRilChannel X"); 61 return sm; 62 } 63 64 /** 65 * Close an open channel 66 */ 67 public void close() { 68 try { 69 if (mChannel != null) { 70 mChannel.close(); 71 if (DBG) log("DefaultState.enter closed socket"); 72 } 73 } catch (IOException e) { 74 log("Could not close conection to mock-ril"); 75 e.printStackTrace(); 76 } 77 } 78 79 /** 80 * @return the channel 81 */ 82 public SocketChannel getChannel() { 83 return mChannel; 84 } 85 86 /** 87 * write the bb contents to sc 88 * 89 * @param bb is the ByteBuffer to write 90 * @return number of bytes written 91 * @throws IOException 92 */ 93 public final int sendAll(ByteBuffer bb) throws IOException { 94 int count = 0; 95 while (bb.remaining() != 0) { 96 count += mChannel.write(bb); 97 } 98 return count; 99 } 100 101 /** 102 * read from sc until bb is filled then rewind bb 103 * 104 * @param bb is the ByteBuffer to fill 105 * @return number of bytes read 106 * @throws IOException 107 */ 108 public final int recvAll(ByteBuffer bb) throws IOException { 109 int count = 0; 110 while (bb.remaining() != 0) { 111 count += mChannel.read(bb); 112 } 113 return count; 114 } 115 116 /** 117 * Rewind bb then write the contents to sc 118 * 119 * @param bb is the ByteBuffer to write 120 * @return number of bytes written 121 * @throws IOException 122 */ 123 public final int rewindSendAll(ByteBuffer bb) throws IOException { 124 bb.rewind(); 125 return sendAll(bb); 126 } 127 128 /** 129 * read from sc until bb is filled then rewind bb 130 * 131 * @param bb is the ByteBuffer to fill 132 * @return number of bytes read 133 * @throws IOException 134 */ 135 public final int recvAllRewind(ByteBuffer bb) throws IOException { 136 int count = recvAll(bb); 137 bb.rewind(); 138 return count; 139 } 140 141 /** 142 * Write to log. 143 * 144 * @param s 145 */ 146 static void log(String s) { 147 Log.v("MockRilChannel", s); 148 } 149 } 150