TinyB  0.5.1
TinyB - The Tiny Bluetooth LE library
BluetoothManager.hpp
1 /*
2  * Author: Petre Eftime <petre.p.eftime@intel.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 #include "BluetoothObject.hpp"
27 #include "BluetoothEvent.hpp"
28 #include <vector>
29 #include <list>
30 
32 {
33 friend class BluetoothAdapter;
34 friend class BluetoothDevice;
35 friend class BluetoothGattService;
36 friend class BluetoothGattCharacteristic;
37 friend class BluetoothGattDescriptor;
38 friend class BluetoothEventManager;
39 
40 private:
41  std::unique_ptr<BluetoothAdapter> default_adapter;
42  static BluetoothManager *bluetooth_manager;
43  std::list<std::shared_ptr<BluetoothEvent>> event_list;
44 
46  BluetoothManager(const BluetoothManager &object);
47 
48 protected:
49 
50  void handle_event(BluetoothType type, std::string *name,
51  std::string *identifier, BluetoothObject *parent, BluetoothObject &object);
52 
53 public:
54 
55  static std::string java_class() {
56  return std::string(JAVA_PACKAGE "/BluetoothManager");
57  }
58 
59  static std::string get_api_version();
60  static std::string get_library_version();
61 
62  virtual std::string get_java_class() const;
63  virtual std::string get_class_name() const;
64  virtual std::string get_object_path() const;
65  virtual BluetoothType get_bluetooth_type() const;
66 
72 
76  void add_event(std::shared_ptr<BluetoothEvent> &event) {
77  event_list.push_back(event);
78  }
79 
82  void remove_event(std::shared_ptr<BluetoothEvent> &event) {
83  event_list.remove(event);
84  }
85 
86  void remove_event(BluetoothEvent &event) {
87  for(auto it = event_list.begin(); it != event_list.end(); ++it) {
88  if ((*it).get() == &event) {
89  event_list.remove(*it);
90  break;
91  }
92  }
93  }
94 
95 
112  template<class T>
113  std::unique_ptr<T> find(std::string *name,
114  std::string* identifier, BluetoothObject *parent,
115  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero())
116  {
117  std::unique_ptr<BluetoothObject> obj = find(T::class_type(), name, identifier, parent, timeout);
118  T *t = dynamic_cast<T *>(obj.release());
119  return std::unique_ptr<T>(t);
120  }
121 
141  std::unique_ptr<BluetoothObject> find(BluetoothType type, std::string *name,
142  std::string* identifier, BluetoothObject *parent,
143  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
144 
162  std::weak_ptr<BluetoothEvent> find(BluetoothType type, std::string *name,
163  std::string* identifier, BluetoothObject *parent, BluetoothCallback cb,
164  bool execute_once = true,
165  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
166 
181  std::unique_ptr<BluetoothObject> get_object(BluetoothType type,
182  std::string *name, std::string *identifier, BluetoothObject *parent);
183 
198  std::vector<std::unique_ptr<BluetoothObject>> get_objects(
199  BluetoothType type = BluetoothType::NONE,
200  std::string *name = nullptr, std::string *identifier = nullptr,
201  BluetoothObject *parent = nullptr);
202 
206  std::vector<std::unique_ptr<BluetoothAdapter>> get_adapters(
207  );
208 
212  std::vector<std::unique_ptr<BluetoothDevice>> get_devices(
213  );
214 
218  std::vector<std::unique_ptr<BluetoothGattService>> get_services(
219  );
220 
224  bool set_default_adapter(
225  BluetoothAdapter &adapter
226  );
227 
228  std::unique_ptr<BluetoothAdapter> get_default_adapter();
229 
233  bool start_discovery(
234  );
235 
239  bool stop_discovery(
240  );
241 
245  bool get_discovering(
246  );
247 };
Definition: BluetoothObject.hpp:63
std::unique_ptr< BluetoothObject > get_object(BluetoothType type, std::string *name, std::string *identifier, BluetoothObject *parent)
std::vector< std::unique_ptr< BluetoothDevice > > get_devices()
virtual BluetoothType get_bluetooth_type() const
virtual std::string get_class_name() const
Definition: BluetoothAdapter.hpp:41
static BluetoothManager * get_bluetooth_manager()
std::unique_ptr< T > find(std::string *name, std::string *identifier, BluetoothObject *parent, std::chrono::milliseconds timeout=std::chrono::milliseconds::zero())
Definition: BluetoothManager.hpp:113
void add_event(std::shared_ptr< BluetoothEvent > &event)
Definition: BluetoothManager.hpp:76
Definition: BluetoothGattService.hpp:41
virtual std::string get_java_class() const
virtual std::string get_object_path() const
Definition: BluetoothManager.hpp:31
std::vector< std::unique_ptr< BluetoothGattService > > get_services()
void remove_event(std::shared_ptr< BluetoothEvent > &event)
Definition: BluetoothManager.hpp:82
bool set_default_adapter(BluetoothAdapter &adapter)
std::vector< std::unique_ptr< BluetoothObject > > get_objects(BluetoothType type=BluetoothType::NONE, std::string *name=nullptr, std::string *identifier=nullptr, BluetoothObject *parent=nullptr)
Definition: BluetoothGattCharacteristic.hpp:44
Definition: BluetoothDevice.hpp:45
Definition: BluetoothEvent.hpp:36
Definition: BluetoothGattDescriptor.hpp:40
std::vector< std::unique_ptr< BluetoothAdapter > > get_adapters()