TinyB  0.5.1
TinyB - The Tiny Bluetooth LE library
BluetoothEvent.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 #include <string>
26 #include <condition_variable>
27 #include <atomic>
28 #include <functional>
29 #include "BluetoothObject.hpp"
30 #pragma once
31 
32 using namespace tinyb;
33 
34 typedef std::function<void (BluetoothObject &, void *)> BluetoothCallback;
35 
37 private:
38  std::string *name;
39  std::string *identifier;
40  BluetoothObject *parent;
41  BluetoothType type;
42  bool execute_once;
43  BluetoothCallback cb;
44  void *data;
45  bool canceled;
46 
47 class BluetoothConditionVariable {
48 
49  friend class BluetoothEvent;
50 
51  std::condition_variable cv;
52  std::mutex lock;
53  BluetoothObject *result;
54  std::atomic_bool triggered;
55  std::atomic_uint waiting;
56 
57  BluetoothConditionVariable() : cv(), lock() {
58  result = nullptr;
59  waiting = 0;
60  triggered = false;
61  }
62 
63  BluetoothObject *wait() {
64  if (result != nullptr)
65  return result;
66 
67  if (!triggered) {
68  std::unique_lock<std::mutex> lk(lock);
69  waiting++;
70  cv.wait(lk);
71  waiting--;
72  }
73 
74  return result;
75  }
76 
77  BluetoothObject *wait_for(std::chrono::milliseconds timeout) {
78  if (result != nullptr)
79  return result;
80 
81  if (!triggered) {
82  waiting++;
83  std::unique_lock<std::mutex> lk(lock);
84  cv.wait_for(lk, timeout);
85  waiting--;
86  }
87 
88  return result;
89  }
90 
91  void notify() {
92  triggered = true;
93  while (waiting != 0)
94  cv.notify_all();
95  }
96 
97  ~BluetoothConditionVariable() {
98  notify();
99  }
100 };
101 
102 
103 BluetoothConditionVariable cv;
104 
105 static void generic_callback(BluetoothObject &object, void *data);
106 public:
107 
108  BluetoothEvent(BluetoothType type, std::string *name, std::string *identifier,
109  BluetoothObject *parent, bool execute_once = true,
110  BluetoothCallback cb = nullptr, void *data = NULL);
111  ~BluetoothEvent();
112 
113  BluetoothType get_type() const {
114  return type;
115  }
116 
117  std::string *get_name() const {
118  return name;
119  }
120 
121  std::string *get_identifier() const {
122  return identifier;
123  }
124 
125  BluetoothObject *get_parent() const {
126  return parent;
127  }
128 
129  bool execute_callback(BluetoothObject &object);
130  bool has_callback() {
131  return (cb != NULL);
132  }
133 
134  BluetoothObject *get_result() {
135  return cv.result;
136  }
137 
138  void cancel();
139 
140  void wait(std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
141 
142  bool operator==(BluetoothEvent const &other);
143 };
Definition: BluetoothObject.hpp:63
Definition: BluetoothObject.hpp:32
Definition: BluetoothEvent.hpp:36