ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
service.hpp
Go to the documentation of this file.
1#ifndef ELEC_C7222_BLE_GATT_SERVICE_HPP_
2#define ELEC_C7222_BLE_GATT_SERVICE_HPP_
3
4#include "characteristic.hpp"
5#include "uuid.hpp"
6
7#include <iosfwd>
8#include <list>
9#include <vector>
10
11namespace c7222 {
12
119class Service : public MovableOnly {
120 public:
123
129 enum class ServiceType : uint8_t {
131 kPrimary = 0,
133 kSecondary = 1
134 };
136
139
147 explicit Service(const Uuid& uuid, ServiceType service_type = ServiceType::kPrimary,
148 uint16_t declaration_handle = 0);
149
162 explicit Service(Attribute&& declaration_attr,
163 std::vector<Attribute>&& included_service_declarations,
164 std::list<Characteristic>&& characteristics);
165
170 Service(Service&& other) noexcept = default;
171
176 Service& operator=(Service&& other) noexcept = default;
177
182 Service() = delete;
187 ~Service() override = default;
188
202 static std::list<Service> ParseFromAttributes(std::list<Attribute>& attributes);
204
207
212 [[nodiscard]] const Uuid& GetUuid() const { return uuid_; }
213
218 [[nodiscard]] ServiceType GetServiceType() const { return service_type_; }
219
224 [[nodiscard]] uint16_t GetDeclarationHandle() const { return declaration_attr_.GetHandle(); }
225
230 [[nodiscard]] size_t GetCharacteristicCount() const { return characteristics_.size(); }
231
236 [[nodiscard]] const std::list<Characteristic>& GetCharacteristics() const {
237 return characteristics_;
238 }
244 std::list<Characteristic>& GetCharacteristics() {
245 return characteristics_;
246 }
247
254
260 [[nodiscard]] const Characteristic& GetCharacteristic(size_t index) const;
261
268
275 [[nodiscard]] std::list<Characteristic*> FindCharacteristicsByProperties(Characteristic::Properties properties) const;
276
283 [[nodiscard]] std::list<Characteristic*> FindCharacteristicsDynamic() const;
284
286
289
296
300 [[nodiscard]] bool HasCharacteristicsRequiringEncryption() const;
301
310
313
319 [[nodiscard]] std::list<Characteristic*> FindCharacteristicsWritable() const {
321 }
322
329 [[nodiscard]] std::list<Characteristic*> FindCharacteristicsReadable() const {
331 }
332
342
348 [[nodiscard]] const Characteristic* FindCharacteristicByUuid(const Uuid& uuid) const;
349
356
362 [[nodiscard]] const Characteristic* FindCharacteristicByHandle(uint16_t handle) const;
363
370
376 [[nodiscard]] const Attribute* FindServiceAttributeByHandle(uint16_t handle) const;
378
381
385 [[nodiscard]] size_t GetIncludedServiceCount() const { return included_services_.size(); }
386
393
399 [[nodiscard]] const Service& GetIncludedService(size_t index) const;
400
405 [[nodiscard]] size_t GetIncludedServiceDeclarationCount() const { return included_service_declarations_.size(); }
406
411 [[nodiscard]] bool IsValid() const;
412
417 [[nodiscard]] bool Uses128BitUuid() const { return uuid_.Is128Bit(); }
418
424 void SetConnectionHandle(uint16_t connection_handle);
425
430 [[nodiscard]] uint16_t GetConnectionHandle() const { return connection_handle_; }
432
435
446
461 uint8_t properties,
462 uint16_t declaration_handle,
463 uint16_t value_handle);
464
471 bool RemoveCharacteristic(size_t index);
472
478
481
494 void AddIncludedService(const Service& included_service, uint16_t declaration_handle = 0);
495
502 bool RemoveIncludedService(size_t index);
503
509
512
517 auto begin() { return characteristics_.begin(); }
518
523 auto end() { return characteristics_.end(); }
524
529 [[nodiscard]] auto begin() const { return characteristics_.begin(); }
530
535 [[nodiscard]] auto end() const { return characteristics_.end(); }
536
541 [[nodiscard]] auto cbegin() const { return characteristics_.cbegin(); }
542
547 [[nodiscard]] auto cend() const { return characteristics_.cend(); }
549
552
566 friend std::ostream& operator<<(std::ostream& os, const Service& service);
568
569private:
576 Uuid uuid_;
577
584 ServiceType service_type_;
585
592 Attribute declaration_attr_;
593
600 uint16_t connection_handle_ = 0;
601
608 std::list<Characteristic> characteristics_;
609
616 std::list<Service> included_services_;
617
626 std::vector<Attribute> included_service_declarations_;
627
628};
629
630} // namespace c7222
631
632#endif // ELEC_C7222_BLE_GATT_SERVICE_HPP_
ATT attribute wrapper with BTstack-compatible fields.
Definition attribute.hpp:119
uint16_t GetHandle() const
Get the ATT handle.
Definition attribute.hpp:403
Represents a GATT Characteristic with its declaration, value, and descriptors.
Definition characteristic.hpp:229
Properties
GATT Characteristic Properties as defined in Bluetooth Core Spec.
Definition characteristic.hpp:242
@ kNotify
Characteristic supports notifications.
@ kRead
Characteristic value can be read.
@ kWrite
Characteristic value can be written with response.
@ kIndicate
Characteristic supports indications.
Convenience base that allows move but forbids copy.
Definition non_copyable.hpp:91
Represents a GATT Service with its characteristics and included services.
Definition service.hpp:119
const Characteristic * FindCharacteristicByUuid(const Uuid &uuid) const
Get a characteristic by UUID (const version).
std::list< Characteristic * > FindCharacteristicsWritable() const
Find characteristics that are writable.
Definition service.hpp:319
void ClearIncludedServices()
Remove all included services.
Characteristic * FindCharacteristicByHandle(uint16_t handle)
Get a characteristic that containes the specfied attribute handle.
void SetConnectionHandle(uint16_t connection_handle)
Set the connection handle for all characteristics in this service.
bool IsValid() const
Check if this service is valid.
const Uuid & GetUuid() const
Get the UUID of this service.
Definition service.hpp:212
void ClearCharacteristics()
Remove all characteristics from this service.
const Attribute * FindServiceAttributeByHandle(uint16_t handle) const
Find a service declaration or included service declaration by handle (const version).
auto end() const
Get const iterator to end of characteristics.
Definition service.hpp:535
~Service() override=default
Destructor. Cleans up all managed characteristics and included services.
Service()=delete
Deleted default constructor. A service must have a UUID and type specified.
Service & operator=(Service &&other) noexcept=default
Move assignment operator. Transfers ownership of all internal characteristics and attributes.
bool RemoveCharacteristic(size_t index)
Remove a characteristic by index.
size_t GetCharacteristicCount() const
Get the number of characteristics in this service.
Definition service.hpp:230
uint16_t GetConnectionHandle() const
Get the current connection handle for this service.
Definition service.hpp:430
bool HasCharacteristicsRequiringAuthentication() const
Check whether any characteristic requires authentication.
ServiceType GetServiceType() const
Get the service type (Primary or Secondary).
Definition service.hpp:218
std::list< Characteristic > & GetCharacteristics()
Get the Characteristics object.
Definition service.hpp:244
Characteristic & GetCharacteristic(size_t index)
Get a characteristic by index.
Characteristic & CreateCharacteristic(const Uuid &uuid, uint8_t properties, uint16_t declaration_handle, uint16_t value_handle)
Create and add a new characteristic to this service.
ServiceType
Service type indicators.
Definition service.hpp:129
@ kSecondary
Secondary Service - included in other services, not directly discoverable.
@ kPrimary
Primary Service - top-level service discoverable by clients.
const Characteristic & GetCharacteristic(size_t index) const
Get a characteristic by index (const version).
size_t GetIncludedServiceCount() const
Get the number of included services.
Definition service.hpp:385
auto begin() const
Get const iterator to first characteristic.
Definition service.hpp:529
std::list< Characteristic * > FindCharacteristicsReadable() const
Find characteristics that are readable.
Definition service.hpp:329
Service(Attribute &&declaration_attr, std::vector< Attribute > &&included_service_declarations, std::list< Characteristic > &&characteristics)
Construct a Service by moving parsed attributes.
const Characteristic * FindCharacteristicByHandle(uint16_t handle) const
Get a characteristic that containes the specfied attribute handle (const version).
std::list< Characteristic * > FindCharacteristicsByProperties(Characteristic::Properties properties) const
Find characteristics that include all specified properties.
friend std::ostream & operator<<(std::ostream &os, const Service &service)
Stream insertion operator for service information.
const std::list< Characteristic > & GetCharacteristics() const
Get the list of characteristics in this service.
Definition service.hpp:236
bool HasCharacteristicsRequiringAuthorization() const
Check whether any characteristic requires authorization.
uint16_t GetDeclarationHandle() const
Get the handle of the Declaration attribute.
Definition service.hpp:224
bool Uses128BitUuid() const
Check if this service uses 128-bit UUID.
Definition service.hpp:417
auto cend() const
Get const iterator to end of characteristics.
Definition service.hpp:547
bool RemoveIncludedService(size_t index)
Remove an included service by index.
Attribute * FindServiceAttributeByHandle(uint16_t handle)
Find a service declaration or included service declaration by handle.
auto begin()
Get iterator to first characteristic.
Definition service.hpp:517
Service & GetIncludedService(size_t index)
Get an included service by index.
bool HasCharacteristicsRequiringEncryption() const
Check whether any characteristic requires encrypted link security.
auto end()
Get iterator to end of characteristics.
Definition service.hpp:523
size_t GetIncludedServiceDeclarationCount() const
Get the number of included service declaration attributes.
Definition service.hpp:405
std::list< Characteristic * > FindCharacteristicsNotifiableOrIndicatable() const
Find characteristics that can notify or indicate.
Definition service.hpp:339
Characteristic & AddCharacteristic(Characteristic &&characteristic)
Add a characteristic to this service.
Characteristic * FindCharacteristicByUuid(const Uuid &uuid)
Get a characteristic by UUID.
const Service & GetIncludedService(size_t index) const
Get an included service by index (const version).
void AddIncludedService(const Service &included_service, uint16_t declaration_handle=0)
Add an included service reference.
Service(const Uuid &uuid, ServiceType service_type=ServiceType::kPrimary, uint16_t declaration_handle=0)
Construct a new Service. Creates the service with the given UUID and type.
std::list< Characteristic * > FindCharacteristicsDynamic() const
Find characteristics that use dynamically assigned value handles.
Service(Service &&other) noexcept=default
Move constructor. Transfers ownership of all internal characteristics and attributes.
static std::list< Service > ParseFromAttributes(std::list< Attribute > &attributes)
Parse Services from an ordered attribute list.
auto cbegin() const
Get const iterator to first characteristic.
Definition service.hpp:541
UUID storage for 16-bit and 128-bit UUIDs.
Definition uuid.hpp:53
bool Is128Bit() const
Returns true if this UUID is 128-bit.
C7222 course abstractions namespace.
Definition ble.hpp:20
GATT UUID wrapper.