Home | History | Annotate | Download | only in service
      1 //
      2 //  Copyright 2015 Google, Inc.
      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 #pragma once
     17 
     18 #include <bluetooth/uuid.h>
     19 #include <array>
     20 #include <memory>
     21 #include <unordered_map>
     22 #include <vector>
     23 
     24 #include "hardware/bluetooth.h"
     25 #include "hardware/bt_gatt.h"
     26 
     27 namespace bluetooth {
     28 namespace gatt {
     29 
     30 // Attribute permission values
     31 const int kPermissionRead = 0x1;
     32 const int kPermissionReadEncrypted = 0x2;
     33 const int kPermissionReadEncryptedMitm = 0x4;
     34 const int kPermissionWrite = 0x10;
     35 const int kPermissionWriteEnecrypted = 0x20;
     36 const int KPermissionWriteEncryptedMitm = 0x40;
     37 const int kPermissionWriteSigned = 0x80;
     38 const int kPermissionWriteSignedMitm = 0x100;
     39 
     40 // GATT characteristic properties bit-field values
     41 const int kPropertyBroadcast = 0x1;
     42 const int kPropertyRead = 0x2;
     43 const int kPropertyWriteNoResponse = 0x4;
     44 const int kPropertyWrite = 0x8;
     45 const int kPropertyNotify = 0x10;
     46 const int kPropertyIndicate = 0x20;
     47 const int kPropertySignedWrite = 0x40;
     48 const int kPropertyExtendedProps = 0x80;
     49 
     50 // A mapping from string bluetooth addresses to RSSI measurements.
     51 typedef std::unordered_map<std::string, int> ScanResults;
     52 
     53 // TODO(armansito): This should be a private internal class though I don't see
     54 // why we even need this class. Instead it should probably be merged into
     55 // Server.
     56 struct ServerInternals;
     57 
     58 // Server is threadsafe and internally locked.
     59 // Asynchronous IO is identified via a gatt_pipe FD,
     60 // and synchronously read with 'GetCharacteristicValue'
     61 //
     62 // ****DEPRECATED****
     63 //
     64 // TODO(armansito): This class has been deprecated and is being replaced by
     65 // bluetooth::GattServer. We will remove this entirely once the new code is
     66 // ready.
     67 class Server {
     68  public:
     69   Server();
     70   ~Server();
     71 
     72   // Register GATT interface, initialize internal state,
     73   // and open a pipe for characteristic write notification.
     74   bool Initialize(const Uuid& service_id, int* gatt_pipe);
     75 
     76   // Control the content of service advertisement.
     77   bool SetAdvertisement(const std::vector<Uuid>& ids,
     78                         const std::vector<uint8_t>& service_data,
     79                         const std::vector<uint8_t>& manufacturer_data,
     80                         bool transmit_name);
     81 
     82   // Control the content of service scan response.
     83   bool SetScanResponse(const std::vector<Uuid>& ids,
     84                        const std::vector<uint8_t>& service_data,
     85                        const std::vector<uint8_t>& manufacturer_data,
     86                        bool transmit_name);
     87 
     88   // Add an ordinary characteristic for reading and/or writing.
     89   bool AddCharacteristic(const Uuid& id, int properties, int permissions);
     90 
     91   // Add a special 'blob' characteristic with a corresponding control
     92   // attribute to manipulate which part of the blob the attribute represents.
     93   bool AddBlob(const Uuid& id, const Uuid& control_id, int properties,
     94                int permissions);
     95 
     96   // Put a new value into a characeteristic.
     97   // It will be read from a client starting at the next 0-offset read.
     98   bool SetCharacteristicValue(const Uuid& id,
     99                               const std::vector<uint8_t>& value);
    100 
    101   // Get the current value of a characteristic.
    102   bool GetCharacteristicValue(const Uuid& id, std::vector<uint8_t>* value);
    103 
    104   // Start this service. Activate advertisements, allow connections.
    105   // Characteristics should all be created before this.
    106   bool Start();
    107 
    108   // Cease advertisements and disallow connections.
    109   bool Stop();
    110 
    111   // Enable LE scan. Scan results will be cached internally.
    112   bool ScanEnable();
    113 
    114   // Disable LE scan.
    115   bool ScanDisable();
    116 
    117   // Copy out the cached scan results.
    118   bool GetScanResults(ScanResults* results);
    119 
    120  private:
    121   // Internal data.
    122   std::unique_ptr<ServerInternals> internal_;
    123 };
    124 
    125 }  // namespace gatt
    126 }  // namespace bluetooth
    127