Guest User

Untitled

a guest
Feb 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.58 KB | None | 0 0
  1. /*
  2. * Copyright 2010 Telefonica, I+D. Printed in Spain (Europe). All Rights
  3. * Reserved.
  4. * The copyright to the software program(s) is property of Telefonica I+D.
  5. * The program(s) may be used and or copied only with the express written
  6. * consent of Telefonica I+D or in acordance with the terms and conditions
  7. * stipulated in the agreement/contract under which the program(s) have
  8. * been supplied.
  9. */
  10.  
  11.  
  12. package com.tid.fs.nfc.sl;
  13.  
  14.  
  15. import net.sf.microlog.core.Logger;
  16. import net.sf.microlog.core.LoggerFactory;
  17.  
  18. import com.tid.fs.nfc.FsException;
  19. import com.tid.fs.nfc.hal.nfc.NfcService;
  20. import com.tid.fs.nfc.hal.store.ConfigData;
  21. import com.tid.fs.nfc.hal.store.StoreService;
  22. import com.tid.fs.nfc.util.WUtil;
  23.  
  24. // #ifdef RIM
  25. import net.rim.device.api.system.SIMCardInfo;
  26. import net.rim.device.api.system.SIMCardException;
  27. import net.rim.device.api.system.capability.DeviceCapability;
  28. // #endif
  29.  
  30. public class ConfigManager {
  31.  
  32. private final static Logger LOG = LoggerFactory.getLogger(ConfigManager.class);
  33.  
  34. private byte SIM_mode; //0x00 2G 0x01 3G
  35. private ConfigAPDU cdata;
  36.  
  37. private String midletName = null;
  38. private String iccid = null;
  39. private String imsi = null;
  40. private boolean NormallyTerminated;
  41. private boolean isNFCActivated;
  42. private boolean isNFCAvailable;
  43.  
  44.  
  45. private boolean isInitiated;
  46. private boolean isSIMchanged;
  47.  
  48. private static ConfigManager sInstance = null;
  49.  
  50. private ConfigManager(){
  51. cdata = new ConfigAPDU();
  52. isInitiated = false;
  53. NormallyTerminated = false;
  54. }
  55.  
  56. public static synchronized ConfigManager getInstance() {
  57. if (sInstance == null) {
  58. sInstance = new ConfigManager();
  59. }
  60.  
  61. return sInstance;
  62. }
  63.  
  64. public void Init() throws FsException{
  65. if(!isInitiated){
  66. try {
  67. ConfigData cd = (ConfigData) StoreService.getInstance().getConfigStore().load("0");
  68. RestoreData(cd);
  69. setAbnormallyTerminated();
  70. } catch (FsException e) {
  71. if (e.getCode() == FsException.CARD_NOT_PAIRED) {
  72. UpdateDataStored();
  73. isInitiated=true;
  74. }
  75. else
  76. throw e;
  77. }
  78.  
  79. }
  80. }
  81.  
  82.  
  83. public boolean isInitiated(){
  84. return isInitiated;
  85. }
  86.  
  87. public void UpdateDataStored() throws FsException{
  88. ConfigData cd = new ConfigData(this.getIMSI(), this.getICCID(), this.midletName,
  89. this.NormallyTerminated, this.isNFCAvailable());
  90. StoreService.getInstance().getConfigStore().save(cd);
  91. }
  92.  
  93.  
  94. private void RestoreData(ConfigData cd) throws FsException {
  95.  
  96. if(! this.NormallyTerminated ){
  97. this.NormallyTerminated = cd.getIsNormallyTerminated();
  98. cd.setIsNormallyTerminated(false);//Pongo a false para prevenir cierres
  99. }
  100. this.isNFCAvailable = cd.getIsNFCavailable();
  101. if( this.midletName != null ) //Lo recuperamos del RMS ˙nicamente si es nulo, quiere decir que no lo sabemos
  102. this.midletName = cd.getMidletName();
  103. String IMSIrms = cd.getLastImsi();
  104. String ICCIDrms = cd.getLastIccid();
  105. String ICCIDsim = this.getICCID();
  106. this.isSIMchanged = (ICCIDsim.compareTo(ICCIDrms) != 0);
  107. if(this.isSIMchanged){
  108. this.iccid = ICCIDsim;
  109. this.imsi = this.getIMSI();
  110. }else{
  111. this.iccid = ICCIDrms;
  112. this.imsi = IMSIrms;
  113. }
  114.  
  115. LOG.debug ("ICCID RMS: " + iccid +"IMSI RMS: " + imsi +"AB_TER RMS: " +
  116. new Boolean(NormallyTerminated).toString() + "isNFAV RMS: " +
  117. new Boolean(isNFCAvailable).toString() + "Midlet Name RMS: " + midletName);
  118.  
  119. StoreService.getInstance().getConfigStore().save(cd); //Se actualiza el Normally Terminated
  120. }
  121.  
  122. private void setAbnormallyTerminated() throws FsException {
  123. ConfigData cd = new ConfigData(this.getIMSI(), this.getICCID(), this.midletName,
  124. false, this.isNFCAvailable());
  125. StoreService.getInstance().getConfigStore().save(cd);
  126. }
  127.  
  128. /**
  129. * M»todo para acceder a la SIM y leer el ICCID
  130. * @return String ICCID
  131. * @throws FsException Imposible connect to SE
  132. */
  133. public String getICCID () throws FsException{
  134. if (iccid == null) {
  135. byte[] response = readICCID();
  136. if(response != null){
  137. byte[] ICCIDb = this.convertLSB_ICCID(response);
  138. String tmp = WUtil.getHexString(ICCIDb, 0, ICCIDb.length,0);
  139. iccid = tmp.substring(1);
  140. }
  141. }
  142. return iccid;
  143. }
  144.  
  145. /**
  146. * M»todo para acceder a la SIM y leer el IMSI
  147. * @return String IMSI
  148. * @throws FsException Imposible connect to SE
  149. */
  150. public String getIMSI () throws FsException{
  151. if (imsi == null) {
  152. byte[] response = readIMSI();
  153. if(response != null){
  154. byte[] IMSIb = this.convertLSB_IMSI(response);
  155. imsi = WUtil.getHexString(IMSIb,0,IMSIb.length,0);
  156. }
  157. }
  158. return imsi;
  159. }
  160. /**
  161. * M»todo para saber si el dispositivo tiene NFC
  162. * @return
  163. */
  164.  
  165. public boolean isNFCAvailable (){
  166.  
  167. // #ifdef RIM
  168. isNFCAvailable = DeviceCapability.isNFCSupported();
  169. // #else
  170. String isNFC = System.getProperty("microedition.contactless.version");
  171. if(isNFC != null)
  172. isNFCAvailable = true;
  173. else
  174. isNFCAvailable = false;
  175. // #endif
  176.  
  177. return isNFCAvailable;
  178.  
  179. }
  180.  
  181. public boolean isNFCActivate(){
  182. if(!isNFCAvailable())
  183. isNFCActivated = false;
  184. else
  185. isNFCActivated = NfcService.getInstance().getNfcAdapter().isNFCActivate();
  186. return isNFCActivated;
  187. }
  188.  
  189. public String getMidletName () {
  190. return midletName;
  191. }
  192.  
  193. public void setMidletname (String midletName) throws FsException {
  194.  
  195. if (midletName == null)
  196. throw new FsException("Midlet name cannot be null",FsException.ILLEGAL_ARGUMENT);
  197.  
  198. if(!isInitiated)
  199. Init();
  200.  
  201. this.midletName = midletName;
  202. UpdateDataStored();
  203. }
  204.  
  205. public boolean getNormallyTerminated() {
  206. return NormallyTerminated;
  207. }
  208.  
  209. public void setNormallyTerminated(){
  210. if(!isInitiated){
  211. try {
  212. this.NormallyTerminated = true;
  213. UpdateDataStored();
  214. } catch (FsException e) {
  215. LOG.error("Failure when setting normally terminated: " + e );
  216. }
  217. }else{
  218. LOG.error("This method cannot be called before initializing the Config Manager");
  219. }
  220. }
  221.  
  222. /**
  223. * M»todo para saber si se ha cambiado de SIM comparando los ICCID's
  224. * @return
  225. * @throws FsException Imposible connect to SE
  226. */
  227.  
  228. public boolean isSIMchanged() throws FsException{
  229. String ICCIDrms = null;
  230. ConfigData configData = (ConfigData) StoreService.getInstance().getConfigStore().load("0");
  231. if (configData != null) {
  232. ICCIDrms = configData.getLastIccid();
  233. LOG.debug ("ICCID RMS: " + ICCIDrms);
  234. }
  235. String ICCIDsim = this.getICCID();
  236.  
  237. return ((ICCIDrms != null) && (ICCIDrms.compareTo(ICCIDsim)==0));
  238. }
  239. /**
  240. * @return byte[] con ICCID formato leÃdo de SIM
  241. * @throws FsException Imposible connect to SE
  242. */
  243. private byte[] readICCID () throws FsException{
  244. byte[] response = null;
  245.  
  246. /* // #ifdef RIM
  247. try {
  248. response = SIMCardInfo.getICCID();
  249. } catch(SIMCardException e) {
  250. throw new FsException("An error occurs when reading ICCID: " + e.toString(), e, FsException.UNEXPECTED_ERROR);
  251. }
  252.  
  253. // #else
  254. if(cdata.connect()){
  255. try{
  256. cdata.selectDF(ConfigAPDU.EF_MF);
  257. cdata.selectDF(ConfigAPDU.EF_ICCID);
  258. response = cdata.readEF();// ConfigAPDU.ICCID_LENGTH);
  259. }finally{
  260. cdata.disconnect();
  261. }
  262. }
  263. // #endif
  264.  
  265. */
  266. return response;
  267. }
  268.  
  269.  
  270. /**
  271. *
  272. * @return byte[] con IMSI formato leÃdo de SIM
  273. * @throws FsException Imposible connect to SE
  274. */
  275. private byte[] readIMSI () throws FsException{
  276. byte[] response = null;
  277.  
  278. /* // #ifdef RIM
  279. try {
  280. response = SIMCardInfo.getIMSI();
  281. } catch(SIMCardException e) {
  282. throw new FsException("An error occurs when reading IMSI: " + e.toString(), e, FsException.UNEXPECTED_ERROR);
  283. }
  284.  
  285. // #else
  286. if(cdata.connect()){
  287. try{
  288. cdata.selectDF( ConfigAPDU.EF_MF);
  289. cdata.selectDF( ConfigAPDU.EF_GSM);
  290. cdata.selectDF( ConfigAPDU.EF_IMSI);
  291. response = cdata.readEF();// ConfigAPDU.IMSI_LENGTH);
  292. }finally{
  293. cdata.disconnect();
  294. }
  295. }
  296. // #endif
  297. */
  298.  
  299. return response;
  300. }
  301.  
  302.  
  303. private byte[] convertLSB_ICCID(byte[] input){
  304. byte[] output = new byte[input.length];
  305. for(int i = 0;i<input.length;i++){
  306. output[i] = (byte)(((byte)output[i])|(byte) (((byte)input[i])&((byte)0x0F)));
  307. if((i+1)!=input.length)
  308. output[i+1]= (byte)(((byte)input[i])&((byte)0xF0));
  309. }
  310.  
  311. return output;
  312. }
  313.  
  314. private byte[] convertLSB_IMSI(byte[] input){
  315. byte[] output = new byte[input.length];
  316. for(int i = 0;i<input.length;i++){
  317. byte LSB = (byte) (((byte)input[i])&((byte)0x0F));
  318. byte MSB = (byte) (((byte)input[i])&((byte)0xF0));
  319. byte LSBM = (byte) (((byte) (LSB << 4))&((byte)(0xF0)));
  320. byte MSBL = (byte) (((byte) (MSB >>> 4))&((byte)(0x0F)));
  321. output[i] = (byte) (MSBL | LSBM);
  322. }
  323.  
  324. return output;
  325. }
  326.  
  327. }
Add Comment
Please, Sign In to add comment