Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2010 Telefonica, I+D. Printed in Spain (Europe). All Rights
- * Reserved.
- * The copyright to the software program(s) is property of Telefonica I+D.
- * The program(s) may be used and or copied only with the express written
- * consent of Telefonica I+D or in acordance with the terms and conditions
- * stipulated in the agreement/contract under which the program(s) have
- * been supplied.
- */
- package com.tid.fs.nfc.sl;
- import net.sf.microlog.core.Logger;
- import net.sf.microlog.core.LoggerFactory;
- import com.tid.fs.nfc.FsException;
- import com.tid.fs.nfc.hal.nfc.NfcService;
- import com.tid.fs.nfc.hal.store.ConfigData;
- import com.tid.fs.nfc.hal.store.StoreService;
- import com.tid.fs.nfc.util.WUtil;
- // #ifdef RIM
- import net.rim.device.api.system.SIMCardInfo;
- import net.rim.device.api.system.SIMCardException;
- import net.rim.device.api.system.capability.DeviceCapability;
- // #endif
- public class ConfigManager {
- private final static Logger LOG = LoggerFactory.getLogger(ConfigManager.class);
- private byte SIM_mode; //0x00 2G 0x01 3G
- private ConfigAPDU cdata;
- private String midletName = null;
- private String iccid = null;
- private String imsi = null;
- private boolean NormallyTerminated;
- private boolean isNFCActivated;
- private boolean isNFCAvailable;
- private boolean isInitiated;
- private boolean isSIMchanged;
- private static ConfigManager sInstance = null;
- private ConfigManager(){
- cdata = new ConfigAPDU();
- isInitiated = false;
- NormallyTerminated = false;
- }
- public static synchronized ConfigManager getInstance() {
- if (sInstance == null) {
- sInstance = new ConfigManager();
- }
- return sInstance;
- }
- public void Init() throws FsException{
- if(!isInitiated){
- try {
- ConfigData cd = (ConfigData) StoreService.getInstance().getConfigStore().load("0");
- RestoreData(cd);
- setAbnormallyTerminated();
- } catch (FsException e) {
- if (e.getCode() == FsException.CARD_NOT_PAIRED) {
- UpdateDataStored();
- isInitiated=true;
- }
- else
- throw e;
- }
- }
- }
- public boolean isInitiated(){
- return isInitiated;
- }
- public void UpdateDataStored() throws FsException{
- ConfigData cd = new ConfigData(this.getIMSI(), this.getICCID(), this.midletName,
- this.NormallyTerminated, this.isNFCAvailable());
- StoreService.getInstance().getConfigStore().save(cd);
- }
- private void RestoreData(ConfigData cd) throws FsException {
- if(! this.NormallyTerminated ){
- this.NormallyTerminated = cd.getIsNormallyTerminated();
- cd.setIsNormallyTerminated(false);//Pongo a false para prevenir cierres
- }
- this.isNFCAvailable = cd.getIsNFCavailable();
- if( this.midletName != null ) //Lo recuperamos del RMS ˙nicamente si es nulo, quiere decir que no lo sabemos
- this.midletName = cd.getMidletName();
- String IMSIrms = cd.getLastImsi();
- String ICCIDrms = cd.getLastIccid();
- String ICCIDsim = this.getICCID();
- this.isSIMchanged = (ICCIDsim.compareTo(ICCIDrms) != 0);
- if(this.isSIMchanged){
- this.iccid = ICCIDsim;
- this.imsi = this.getIMSI();
- }else{
- this.iccid = ICCIDrms;
- this.imsi = IMSIrms;
- }
- LOG.debug ("ICCID RMS: " + iccid +"IMSI RMS: " + imsi +"AB_TER RMS: " +
- new Boolean(NormallyTerminated).toString() + "isNFAV RMS: " +
- new Boolean(isNFCAvailable).toString() + "Midlet Name RMS: " + midletName);
- StoreService.getInstance().getConfigStore().save(cd); //Se actualiza el Normally Terminated
- }
- private void setAbnormallyTerminated() throws FsException {
- ConfigData cd = new ConfigData(this.getIMSI(), this.getICCID(), this.midletName,
- false, this.isNFCAvailable());
- StoreService.getInstance().getConfigStore().save(cd);
- }
- /**
- * M»todo para acceder a la SIM y leer el ICCID
- * @return String ICCID
- * @throws FsException Imposible connect to SE
- */
- public String getICCID () throws FsException{
- if (iccid == null) {
- byte[] response = readICCID();
- if(response != null){
- byte[] ICCIDb = this.convertLSB_ICCID(response);
- String tmp = WUtil.getHexString(ICCIDb, 0, ICCIDb.length,0);
- iccid = tmp.substring(1);
- }
- }
- return iccid;
- }
- /**
- * M»todo para acceder a la SIM y leer el IMSI
- * @return String IMSI
- * @throws FsException Imposible connect to SE
- */
- public String getIMSI () throws FsException{
- if (imsi == null) {
- byte[] response = readIMSI();
- if(response != null){
- byte[] IMSIb = this.convertLSB_IMSI(response);
- imsi = WUtil.getHexString(IMSIb,0,IMSIb.length,0);
- }
- }
- return imsi;
- }
- /**
- * M»todo para saber si el dispositivo tiene NFC
- * @return
- */
- public boolean isNFCAvailable (){
- // #ifdef RIM
- isNFCAvailable = DeviceCapability.isNFCSupported();
- // #else
- String isNFC = System.getProperty("microedition.contactless.version");
- if(isNFC != null)
- isNFCAvailable = true;
- else
- isNFCAvailable = false;
- // #endif
- return isNFCAvailable;
- }
- public boolean isNFCActivate(){
- if(!isNFCAvailable())
- isNFCActivated = false;
- else
- isNFCActivated = NfcService.getInstance().getNfcAdapter().isNFCActivate();
- return isNFCActivated;
- }
- public String getMidletName () {
- return midletName;
- }
- public void setMidletname (String midletName) throws FsException {
- if (midletName == null)
- throw new FsException("Midlet name cannot be null",FsException.ILLEGAL_ARGUMENT);
- if(!isInitiated)
- Init();
- this.midletName = midletName;
- UpdateDataStored();
- }
- public boolean getNormallyTerminated() {
- return NormallyTerminated;
- }
- public void setNormallyTerminated(){
- if(!isInitiated){
- try {
- this.NormallyTerminated = true;
- UpdateDataStored();
- } catch (FsException e) {
- LOG.error("Failure when setting normally terminated: " + e );
- }
- }else{
- LOG.error("This method cannot be called before initializing the Config Manager");
- }
- }
- /**
- * M»todo para saber si se ha cambiado de SIM comparando los ICCID's
- * @return
- * @throws FsException Imposible connect to SE
- */
- public boolean isSIMchanged() throws FsException{
- String ICCIDrms = null;
- ConfigData configData = (ConfigData) StoreService.getInstance().getConfigStore().load("0");
- if (configData != null) {
- ICCIDrms = configData.getLastIccid();
- LOG.debug ("ICCID RMS: " + ICCIDrms);
- }
- String ICCIDsim = this.getICCID();
- return ((ICCIDrms != null) && (ICCIDrms.compareTo(ICCIDsim)==0));
- }
- /**
- * @return byte[] con ICCID formato leÃdo de SIM
- * @throws FsException Imposible connect to SE
- */
- private byte[] readICCID () throws FsException{
- byte[] response = null;
- /* // #ifdef RIM
- try {
- response = SIMCardInfo.getICCID();
- } catch(SIMCardException e) {
- throw new FsException("An error occurs when reading ICCID: " + e.toString(), e, FsException.UNEXPECTED_ERROR);
- }
- // #else
- if(cdata.connect()){
- try{
- cdata.selectDF(ConfigAPDU.EF_MF);
- cdata.selectDF(ConfigAPDU.EF_ICCID);
- response = cdata.readEF();// ConfigAPDU.ICCID_LENGTH);
- }finally{
- cdata.disconnect();
- }
- }
- // #endif
- */
- return response;
- }
- /**
- *
- * @return byte[] con IMSI formato leÃdo de SIM
- * @throws FsException Imposible connect to SE
- */
- private byte[] readIMSI () throws FsException{
- byte[] response = null;
- /* // #ifdef RIM
- try {
- response = SIMCardInfo.getIMSI();
- } catch(SIMCardException e) {
- throw new FsException("An error occurs when reading IMSI: " + e.toString(), e, FsException.UNEXPECTED_ERROR);
- }
- // #else
- if(cdata.connect()){
- try{
- cdata.selectDF( ConfigAPDU.EF_MF);
- cdata.selectDF( ConfigAPDU.EF_GSM);
- cdata.selectDF( ConfigAPDU.EF_IMSI);
- response = cdata.readEF();// ConfigAPDU.IMSI_LENGTH);
- }finally{
- cdata.disconnect();
- }
- }
- // #endif
- */
- return response;
- }
- private byte[] convertLSB_ICCID(byte[] input){
- byte[] output = new byte[input.length];
- for(int i = 0;i<input.length;i++){
- output[i] = (byte)(((byte)output[i])|(byte) (((byte)input[i])&((byte)0x0F)));
- if((i+1)!=input.length)
- output[i+1]= (byte)(((byte)input[i])&((byte)0xF0));
- }
- return output;
- }
- private byte[] convertLSB_IMSI(byte[] input){
- byte[] output = new byte[input.length];
- for(int i = 0;i<input.length;i++){
- byte LSB = (byte) (((byte)input[i])&((byte)0x0F));
- byte MSB = (byte) (((byte)input[i])&((byte)0xF0));
- byte LSBM = (byte) (((byte) (LSB << 4))&((byte)(0xF0)));
- byte MSBL = (byte) (((byte) (MSB >>> 4))&((byte)(0x0F)));
- output[i] = (byte) (MSBL | LSBM);
- }
- return output;
- }
- }
Add Comment
Please, Sign In to add comment