Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 930076b4 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Optimize UUID parsing in GattService"

parents 9652d7f5 337ec54a
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -89,6 +89,8 @@ public class GattService extends ProfileService {
    private static final boolean DBG = GattServiceConfig.DBG;
    private static final boolean VDBG = GattServiceConfig.VDBG;
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "GattService";
    private static final String UUID_SUFFIX = "-0000-1000-8000-00805f9b34fb";
    private static final String UUID_ZERO_PAD = "00000000";

    static final int SCAN_FILTER_ENABLED = 1;
    static final int SCAN_FILTER_MODIFIED = 2;
@@ -3276,7 +3278,8 @@ public class GattService extends ProfileService {
        }
    }

    private List<UUID> parseUuids(byte[] advData) {
    @VisibleForTesting
    List<UUID> parseUuids(byte[] advData) {
        List<UUID> uuids = new ArrayList<UUID>();

        int offset = 0;
@@ -3294,8 +3297,10 @@ public class GattService extends ProfileService {
                        int uuid16 = advData[offset++];
                        uuid16 += (advData[offset++] << 8);
                        len -= 2;
                        uuids.add(UUID.fromString(
                                String.format("%08x-0000-1000-8000-00805f9b34fb", uuid16)));
                        String uuid_prefix = Integer.toHexString(uuid16);
                        // Pad zeroes to make uuid_prefix length exactly 8.
                        uuids.add(UUID.fromString(UUID_ZERO_PAD.substring(uuid_prefix.length())
                                                  + uuid_prefix + UUID_SUFFIX));
                    }
                    break;

+22 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;
import java.util.UUID;

/**
 * Test cases for {@link GattService}.
 */
@@ -90,4 +93,23 @@ public class GattServiceTest {
        Assert.assertEquals(99700000000L, timestampNanos);
    }

    @Test
    public void testParseUuids() {
        // Experimentally observed raw advertiser data.
        // It contains a partial list of service class UUIDs.
        String rawHexAdvData = "0201061BFF570102FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF03E060719405"
                          + "D310094D6920536D6172742042616E6420350302E0FE";
        String expected_uuid_str = "fffffde0-0000-1000-8000-00805f9b34fb";
        // Convert to byte array and test.
        int len = rawHexAdvData.length();
        byte[] advData = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            advData[i / 2] = (byte) ((Character.digit(rawHexAdvData.charAt(i), 16) << 4)
                                + Character.digit(rawHexAdvData.charAt(i + 1), 16));
        }
        List<UUID> uuids = mService.parseUuids(advData);
        Assert.assertEquals(1, uuids.size());
        Assert.assertEquals(expected_uuid_str, uuids.get(0).toString());
    }

}