Home | History | Annotate | Download | only in gatt
      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  */
     16 package com.android.bluetooth.gatt;
     17 
     18 import android.util.Log;
     19 import java.util.ArrayList;
     20 import java.util.Iterator;
     21 import java.util.List;
     22 import java.util.UUID;
     23 
     24 class 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         boolean advertisePreferred = false;
     43 
     44         Entry(UUID uuid, int serviceType, int instance) {
     45             this.type = TYPE_SERVICE;
     46             this.uuid = uuid;
     47             this.instance = instance;
     48             this.serviceType = serviceType;
     49         }
     50 
     51         Entry(UUID uuid, int serviceType, int instance, boolean advertisePreferred) {
     52           this.type = TYPE_SERVICE;
     53           this.uuid = uuid;
     54           this.instance = instance;
     55           this.serviceType = serviceType;
     56           this.advertisePreferred = advertisePreferred;
     57         }
     58 
     59         Entry(UUID uuid, int properties, int permissions, int instance) {
     60             this.type = TYPE_CHARACTERISTIC;
     61             this.uuid = uuid;
     62             this.instance = instance;
     63             this.permissions = permissions;
     64             this.properties = properties;
     65         }
     66 
     67         Entry(UUID uuid, int permissions) {
     68             this.type = TYPE_DESCRIPTOR;
     69             this.uuid = uuid;
     70             this.permissions = permissions;
     71         }
     72     }
     73 
     74     List<Entry> mEntries = null;
     75     int mNumHandles = 0;
     76 
     77     ServiceDeclaration() {
     78         mEntries = new ArrayList<Entry>();
     79     }
     80 
     81     void addService(UUID uuid, int serviceType, int instance, int minHandles,
     82         boolean advertisePreferred) {
     83         mEntries.add(new Entry(uuid, serviceType, instance, advertisePreferred));
     84         if (minHandles == 0) {
     85             ++mNumHandles;
     86         } else {
     87             mNumHandles = minHandles;
     88         }
     89     }
     90 
     91     void addIncludedService(UUID uuid, int serviceType, int instance) {
     92         Entry entry = new Entry(uuid, serviceType, instance);
     93         entry.type = TYPE_INCLUDED_SERVICE;
     94         mEntries.add(entry);
     95         ++mNumHandles;
     96     }
     97 
     98     void addCharacteristic(UUID uuid, int properties, int permissions) {
     99         mEntries.add(new Entry(uuid, properties, permissions, 0 /*instance*/));
    100         mNumHandles += 2;
    101     }
    102 
    103     void addDescriptor(UUID uuid, int permissions) {
    104         mEntries.add(new Entry(uuid, permissions));
    105         ++mNumHandles;
    106     }
    107 
    108     Entry getNext() {
    109         if (mEntries.isEmpty()) return null;
    110         Entry entry = mEntries.get(0);
    111         mEntries.remove(0);
    112         return entry;
    113     }
    114 
    115     boolean isServiceAdvertisePreferred(UUID uuid) {
    116       for (Entry entry : mEntries) {
    117           if (entry.uuid.equals(uuid)) {
    118               return entry.advertisePreferred;
    119           }
    120       }
    121       return false;
    122     }
    123 
    124     int getNumHandles() {
    125         return mNumHandles;
    126     }
    127 }
    128