1/* 2 * Copyright (C) 2013 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 */ 16package com.android.bluetooth.gatt; 17 18import android.util.Log; 19import java.util.ArrayList; 20import java.util.Iterator; 21import java.util.List; 22import java.util.UUID; 23 24class ServiceDeclaration { 25 private static final boolean DBG = GattServiceConfig.DBG; 26 private static final String TAG = GattServiceConfig.TAG_PREFIX + "ServiceDeclaration"; 27 28 public static final byte TYPE_UNDEFINED = 0; 29 public static final byte TYPE_SERVICE = 1; 30 public static final byte TYPE_CHARACTERISTIC = 2; 31 public static final byte TYPE_DESCRIPTOR = 3; 32 public static final byte TYPE_INCLUDED_SERVICE = 4; 33 34 class Entry { 35 byte type = TYPE_UNDEFINED; 36 UUID uuid = null; 37 int instance = 0; 38 int permissions = 0; 39 int properties = 0; 40 int serviceType = 0; 41 int serviceHandle = 0; 42 43 Entry(UUID uuid, int serviceType, int instance) { 44 this.type = TYPE_SERVICE; 45 this.uuid = uuid; 46 this.instance = instance; 47 this.serviceType = serviceType; 48 } 49 50 Entry(UUID uuid, int properties, int permissions, int instance) { 51 this.type = TYPE_CHARACTERISTIC; 52 this.uuid = uuid; 53 this.instance = instance; 54 this.permissions = permissions; 55 this.properties = properties; 56 } 57 58 Entry(UUID uuid, int permissions) { 59 this.type = TYPE_DESCRIPTOR; 60 this.uuid = uuid; 61 this.permissions = permissions; 62 } 63 } 64 65 List<Entry> mEntries = null; 66 int mNumHandles = 0; 67 68 ServiceDeclaration() { 69 mEntries = new ArrayList<Entry>(); 70 } 71 72 void addService(UUID uuid, int serviceType, int instance, int minHandles) { 73 mEntries.add(new Entry(uuid, serviceType, instance)); 74 if (minHandles == 0) { 75 ++mNumHandles; 76 } else { 77 mNumHandles = minHandles; 78 } 79 } 80 81 void addIncludedService(UUID uuid, int serviceType, int instance) { 82 Entry entry = new Entry(uuid, serviceType, instance); 83 entry.type = TYPE_INCLUDED_SERVICE; 84 mEntries.add(entry); 85 ++mNumHandles; 86 } 87 88 void addCharacteristic(UUID uuid, int properties, int permissions) { 89 mEntries.add(new Entry(uuid, properties, permissions, 0 /*instance*/)); 90 mNumHandles += 2; 91 } 92 93 void addDescriptor(UUID uuid, int permissions) { 94 mEntries.add(new Entry(uuid, permissions)); 95 ++mNumHandles; 96 } 97 98 Entry getNext() { 99 if (mEntries.isEmpty()) return null; 100 Entry entry = mEntries.get(0); 101 mEntries.remove(0); 102 return entry; 103 } 104 105 int getNumHandles() { 106 return mNumHandles; 107 } 108} 109