herald  2.0.0
zephyr_context.h
1 // Copyright 2020-2021 Herald Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #ifndef HERALD_ZEPHYR_CONTEXT_H
6 #define HERALD_ZEPHYR_CONTEXT_H
7 
8 #include "context.h"
9 #include "ble/bluetooth_state_manager.h"
10 
11 // Herald logging to zephyr - see zephyr_context.cpp for details
12 #ifndef CONFIG_HERALD_LOG_LEVEL
13  #define CONFIG_HERALD_LOG_LEVEL 0
14 #endif
15 
16 #include "data/zephyr/zephyr_logging_sink.h"
17 #include "data/sensor_logger.h"
18 
19 #include <memory>
20 #include <iosfwd>
21 #include <string>
22 #include <functional>
23 #include <optional>
24 
25 #include <bluetooth/bluetooth.h>
26 #include <bluetooth/gatt.h>
27 #include <bluetooth/gatt_dm.h>
28 
29 namespace herald {
30 
31 using namespace herald::ble;
32 using namespace herald::data;
33 
35 namespace zephyrinternal {
36  class Advertiser {
37  public:
38  Advertiser();
39  ~Advertiser();
40  void stopAdvertising() noexcept;
41  void startAdvertising() noexcept;
42  void registerStopCallback(std::function<void()> cb);
43  void registerStartCallback(std::function<void()> cb);
44  private:
45  std::optional<std::function<void()>> startCallback;
46  std::optional<std::function<void()>> stopCallback;
47  };
48 }
49 
58 public:
61 
62  ZephyrLoggingSink& getLoggingSink();
63  BluetoothStateManager& getBluetoothStateManager();
64 
65  // Bluetooth State Manager override methods
66  void add(BluetoothStateManagerDelegate& delegate) override;
67  BluetoothState state() override;
68 
69  // Zephyr OS specific methods
70  int enableBluetooth() noexcept;
71 
72  herald::zephyrinternal::Advertiser& getAdvertiser() noexcept;
73 
74  int startBluetooth() noexcept;
75  int stopBluetooth() noexcept;
76 
77  void periodicActions() noexcept;
78 
79  Date getNow() noexcept;
80 
81 private:
82  ZephyrLoggingSink sink;
83 
85 
86  std::vector<std::reference_wrapper<BluetoothStateManagerDelegate>> stateDelegates;
87 
88  bool bluetoothEnabled;
89 };
90 
91 
92 // Other zephyr internal-but-public API base classes
93 namespace zephyrinternal {
94 
96 class Callbacks {
97 public:
98  Callbacks() = default;
99  virtual ~Callbacks() = default;
100 
101  // Scanning
102  virtual void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
103  struct net_buf_simple *buf) = 0;
104 
105  // GATT Discovery
106  virtual void discovery_completed_cb(struct bt_gatt_dm *dm, void *context) = 0;
107  virtual void discovery_service_not_found_cb(struct bt_conn *conn, void *context) = 0;
108  virtual void discovery_error_found_cb(struct bt_conn *conn, int err, void *context) = 0;
109  virtual uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err,
110  struct bt_gatt_read_params *params,
111  const void *data, uint16_t length) = 0;
112 
113  // Connection management
114  virtual void le_param_updated(struct bt_conn *conn, uint16_t interval,
115  uint16_t latency, uint16_t timeout) = 0;
116  virtual void connected(struct bt_conn *conn, uint8_t err) = 0;
117  virtual void disconnected(struct bt_conn *conn, uint8_t reason) = 0;
118 };
119 
120 }
121 
122 } // end namespace
123 
124 #endif
Holds generic state across our application for any Zephyr RTOS device.
Definition: zephyr_context.h:57
Definition: bluetooth_state_manager_delegate.h:16
Definition: bluetooth_state_manager.h:19
Definition: zephyr_logging_sink.h:19
Definition: date.h:18
Definition: zephyr_context.h:36
INTERNAL utility class to allow Zephyr C API to call callbacks in the Zephyr internal Context Impl cl...
Definition: zephyr_context.h:96
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15