Home | History | Annotate | Download | only in tech
      1 /*
      2  * Copyright (C) 2011 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.cts.verifier.nfc.tech;
     18 
     19 import android.nfc.Tag;
     20 import android.nfc.tech.MifareUltralight;
     21 
     22 import java.io.IOException;
     23 import java.util.Arrays;
     24 import java.util.Random;
     25 
     26 /**
     27  * {@link TagTester} for MIFARE Ultralight tags. It writes random bytes to the
     28  * tag's first user page and verifies that it matches when scanned later.
     29  */
     30 public class MifareUltralightTagTester implements TagTester {
     31 
     32     private static final int USER_PAGE_OFFSET = 5;
     33 
     34     private static final int NUM_PAGES = 4;
     35 
     36     @Override
     37     public boolean isTestableTag(Tag tag) {
     38         if (tag != null) {
     39             for (String tech : tag.getTechList()) {
     40                 if (tech.equals(MifareUltralight.class.getName())) {
     41                     return true;
     42                 }
     43             }
     44         }
     45         return false;
     46     }
     47 
     48     @Override
     49     public TagVerifier writeTag(Tag tag) throws IOException {
     50         Random random = new Random();
     51         MifareUltralight ultralight = MifareUltralight.get(tag);
     52         ultralight.connect();
     53 
     54         final byte[] fourPages = new byte[NUM_PAGES * MifareUltralight.PAGE_SIZE];
     55         byte[] onePage = new byte[MifareUltralight.PAGE_SIZE];
     56         for (int i = 0; i < NUM_PAGES; i++) {
     57             random.nextBytes(onePage);
     58             System.arraycopy(onePage, 0, fourPages, i * onePage.length, onePage.length);
     59             ultralight.writePage(USER_PAGE_OFFSET + i, onePage);
     60         }
     61 
     62         final CharSequence expectedContent = NfcUtils.displayByteArray(fourPages);
     63 
     64         return new TagVerifier() {
     65             @Override
     66             public Result verifyTag(Tag tag) throws IOException {
     67                 MifareUltralight ultralight = MifareUltralight.get(tag);
     68                 if (ultralight != null) {
     69                     ultralight.connect();
     70                     byte[] actualFourPages = ultralight.readPages(USER_PAGE_OFFSET);
     71                     CharSequence actualContent = NfcUtils.displayByteArray(actualFourPages);
     72                     return new Result(expectedContent, actualContent,
     73                             Arrays.equals(fourPages, actualFourPages));
     74                 } else {
     75                     return new Result(expectedContent, null, false);
     76                 }
     77             }
     78         };
     79     }
     80 }
     81