GCC Code Coverage Report


Directory: src/gate/
File: src/gate/system/cxx_devices.cpp
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 0 81 0.0%
Functions: 0 15 0.0%
Branches: 0 82 0.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/system/devices.hpp"
30 #include "gate/exceptions.hpp"
31
32 namespace gate
33 {
34 namespace sys
35 {
36
37
38 BluetoothLE::BluetoothLE()
39 {
40 }
41
42 BluetoothLE::~BluetoothLE()
43 {
44 }
45
46 static gate_bool_t bthle_enum_cb(gate_device_bthle_t const* device, void* param)
47 {
48 BluetoothLE::DeviceEnumCallback const* cb = static_cast<BluetoothLE::DeviceEnumCallback const*>(param);
49 if (cb)
50 {
51 try
52 {
53 cb->invoke(device);
54 }
55 catch (...)
56 {
57 return false;
58 }
59 }
60 return true;
61 }
62
63 static gate_bool_t bthle_enum_devices_cb(gate_device_bthle_t const* device, void* param)
64 {
65 ArrayList<BluetoothLE::Device>* devices = static_cast<ArrayList<BluetoothLE::Device>*>(param);
66 if (devices && device)
67 {
68 try
69 {
70 devices->add(BluetoothLE::Device(*device));
71 }
72 catch (...)
73 {
74 }
75 }
76 return true;
77 }
78
79
80 void BluetoothLE::enumDevices(DeviceEnumCallback const& cb)
81 {
82 result_t result = gate_device_bthle_enum(&bthle_enum_cb, (void*)&cb);
83 GATEXX_CHECK_EXCEPTION(result);
84 }
85
86 Array<BluetoothLE::Device> BluetoothLE::enumDevices()
87 {
88 ArrayList<Device> devices;
89 result_t result = gate_device_bthle_enum(&bthle_enum_devices_cb, static_cast<void*>(&devices));
90 GATEXX_CHECK_EXCEPTION(result);
91 return devices.toArray();
92 }
93
94 BluetoothLE::Device BluetoothLE::getDeviceByUid(String const& uid)
95 {
96 Device ret;
97 result_t result = gate_device_bthle_get(uid.c_impl(), &ret);
98 GATEXX_CHECK_EXCEPTION(result);
99 return ret;
100 }
101
102 BluetoothLE::Device BluetoothLE::getDeviceByAddress(String const& address)
103 {
104 Device ret;
105 result_t result = gate_device_bthle_get_by_address(address.c_impl(), &ret);
106 GATEXX_CHECK_EXCEPTION(result);
107 return ret;
108 }
109
110
111 struct GATE_API_LOCAL bthle_chara_cb_param
112 {
113 BluetoothLE::Device const* device;
114 BluetoothLE::Service const* service;
115 BluetoothLE::CharacteristicsEnumCallback const* cb;
116 };
117
118 static gate_bool_t bthle_chara_cb(gate_device_bthle_characteristic_t const* chara, void* user_param)
119 {
120 bthle_chara_cb_param* param = static_cast<bthle_chara_cb_param*>(user_param);
121 if (param)
122 {
123 try
124 {
125 param->cb->invoke(param->device, param->service, chara);
126 }
127 catch (...)
128 {
129 }
130 }
131 return true;
132 }
133
134 static BluetoothLE::Service const* resolveBthLEService(BluetoothLE::Device const& device, Guid const& serviceGuid)
135 {
136 for (size_t ndx = 0; ndx != device.service_count; ++ndx)
137 {
138 if (serviceGuid == device.services[ndx].service_guid)
139 {
140 return &device.services[ndx];
141 }
142 }
143 return NULL;
144 }
145
146 void BluetoothLE::enumCharacteristics(Device const& device, Guid const& serviceGuid, CharacteristicsEnumCallback const& cb)
147 {
148 Service const* const ptrSvc = resolveBthLEService(device, serviceGuid);
149 if (ptrSvc == NULL)
150 {
151 GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Invalid service GUID", 0);
152 }
153 bthle_chara_cb_param param;
154 param.device = &device;
155 param.service = ptrSvc;
156 param.cb = &cb;
157 result_t result = gate_device_bthle_service_enum(&device, ptrSvc, &bthle_chara_cb, &param);
158 GATEXX_CHECK_EXCEPTION(result);
159 }
160
161 static gate_bool_t bthle_chara_guid_cb(gate_device_bthle_characteristic_t const* chara, void* user_param)
162 {
163 ArrayList<Guid>* guids = static_cast<ArrayList<Guid>*>(user_param);
164 if (guids)
165 {
166 try
167 {
168 guids->add(Guid(chara->characteristic_guid));
169 }
170 catch (...)
171 {
172 }
173 }
174 return true;
175 }
176
177 Array<Guid> BluetoothLE::enumCharacteristicsGuids(Device const& device, Guid const& serviceGuid)
178 {
179 ArrayList<Guid> guids;
180 Service const* const ptrSvc = resolveBthLEService(device, serviceGuid);
181 if (ptrSvc == NULL)
182 {
183 GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Invalid service GUID", 0);
184 }
185 result_t result = gate_device_bthle_service_enum(&device, ptrSvc, &bthle_chara_guid_cb, &guids);
186 GATEXX_CHECK_EXCEPTION(result);
187 return guids.toArray();
188 }
189
190
191 Blob BluetoothLE::getCharacteristicValue(Device const& device, Guid const& serviceGuid, Guid const& charaGuid)
192 {
193 char buffer[4096];
194 gate_size_t bufferLen = sizeof(buffer);
195 result_t result = gate_device_bthle_characteristic_get(&device, &serviceGuid, &charaGuid, buffer, &bufferLen);
196 GATEXX_CHECK_EXCEPTION(result);
197
198 if (bufferLen > sizeof(buffer))
199 {
200 GATEXX_RAISE_ERROR(results::OutOfBounds);
201 }
202 return Blob(&buffer[0], bufferLen);
203 }
204
205 void BluetoothLE::setCharacteristicValue(Device const& device, Guid const& serviceGuid, Guid const& charaGuid, Blob const& value)
206 {
207 result_t result = gate_device_bthle_characteristic_set(&device, &serviceGuid, &charaGuid,
208 static_cast<char const*>(value.data()), value.length());
209 GATEXX_CHECK_EXCEPTION(result);
210 }
211
212
213 } // end of namespace sys
214 } // end of namespace gate
215