1 package com.android.server.wifi.anqp; 2 3 import java.net.ProtocolException; 4 import java.nio.ByteBuffer; 5 import java.util.ArrayList; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import static com.android.server.wifi.anqp.Constants.BYTE_MASK; 10 11 public class CellularNetwork implements Iterable<String> { 12 private static final int PLMNListType = 0; 13 14 private final List<String> mMccMnc; 15 16 private CellularNetwork(int plmnCount, ByteBuffer payload) throws ProtocolException { 17 mMccMnc = new ArrayList<>(plmnCount); 18 19 while (plmnCount > 0) { 20 if (payload.remaining() < 3) { 21 throw new ProtocolException("Truncated PLMN info"); 22 } 23 byte[] plmn = new byte[3]; 24 payload.get(plmn); 25 26 int mcc = ((plmn[0] << 8) & 0xf00) | 27 (plmn[0] & 0x0f0) | 28 (plmn[1] & 0x00f); 29 30 int mnc = ((plmn[2] << 4) & 0xf0) | 31 ((plmn[2] >> 4) & 0x0f); 32 33 int n2 = (plmn[1] >> 4) & 0x0f; 34 String mccMnc = n2 != 0xf ? 35 String.format("%03x%03x", mcc, (mnc << 4) | n2) : 36 String.format("%03x%02x", mcc, mnc); 37 38 mMccMnc.add(mccMnc); 39 plmnCount--; 40 } 41 } 42 43 public static CellularNetwork buildCellularNetwork(ByteBuffer payload) 44 throws ProtocolException { 45 int iei = payload.get() & BYTE_MASK; 46 int plmnLen = payload.get() & 0x7f; 47 48 if (iei != PLMNListType) { 49 payload.position(payload.position() + plmnLen); 50 return null; 51 } 52 53 int plmnCount = payload.get() & BYTE_MASK; 54 return new CellularNetwork(plmnCount, payload); 55 } 56 57 @Override 58 public Iterator<String> iterator() { 59 return mMccMnc.iterator(); 60 } 61 62 @Override 63 public String toString() { 64 StringBuilder sb = new StringBuilder("PLMN:"); 65 for (String mccMnc : mMccMnc) { 66 sb.append(' ').append(mccMnc); 67 } 68 return sb.toString(); 69 } 70 } 71