GCC Code Coverage Report


Directory: src/gate/
File: src/gate/system/cxx_devices.cpp
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 0 78 0.0%
Functions: 0 15 0.0%
Branches: 0 84 0.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2026, 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 GATE_CALL 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 ExceptionInfo xcptStatus;
52 GATEXX_TRY_CATCHINFO(xcptStatus, {
53 cb->invoke(device);
54 });
55 return xcptStatus.succeeded();
56 }
57 else
58 {
59 return true;
60 }
61 }
62
63 static gate_bool_t GATE_CALL 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 devices->tryAdd(BluetoothLE::Device(*device));
69 }
70 return true;
71 }
72
73 void BluetoothLE::enumDevices(DeviceEnumCallback const& cb)
74 {
75 result_t result = gate_device_bthle_enum(&bthle_enum_cb, (void*)&cb);
76 GATEXX_CHECK_EXCEPTION(result);
77 }
78
79 Array<BluetoothLE::Device> BluetoothLE::enumDevices()
80 {
81 ArrayList<Device> devices;
82 result_t result = gate_device_bthle_enum(&bthle_enum_devices_cb, static_cast<void*>(&devices));
83 GATEXX_CHECK_EXCEPTION(result);
84 return devices.toArray();
85 }
86
87 BluetoothLE::Device BluetoothLE::getDeviceByUid(String const& uid)
88 {
89 Device ret;
90 result_t result = gate_device_bthle_get(uid.c_impl(), &ret);
91 GATEXX_CHECK_EXCEPTION(result);
92 return ret;
93 }
94
95 BluetoothLE::Device BluetoothLE::getDeviceByAddress(String const& address)
96 {
97 Device ret;
98 result_t result = gate_device_bthle_get_by_address(address.c_impl(), &ret);
99 GATEXX_CHECK_EXCEPTION(result);
100 return ret;
101 }
102
103
104 struct GATE_API_LOCAL bthle_chara_cb_param
105 {
106 BluetoothLE::Device const* device;
107 BluetoothLE::Service const* service;
108 BluetoothLE::CharacteristicsEnumCallback const* cb;
109 };
110
111 static gate_bool_t GATE_CALL bthle_chara_cb(gate_device_bthle_characteristic_t const* chara, void* user_param)
112 {
113 bthle_chara_cb_param* param = static_cast<bthle_chara_cb_param*>(user_param);
114 if (param)
115 {
116 GATEXX_TRY_IGNORE({
117 param->cb->invoke(param->device, param->service, chara);
118 });
119 }
120 return true;
121 }
122
123 static BluetoothLE::Service const* resolveBthLEService(BluetoothLE::Device const& device, Guid const& serviceGuid)
124 {
125 for (size_t ndx = 0; ndx != device.service_count; ++ndx)
126 {
127 if (serviceGuid == device.services[ndx].service_guid)
128 {
129 return &device.services[ndx];
130 }
131 }
132 return NULL;
133 }
134
135 void BluetoothLE::enumCharacteristics(Device const& device, Guid const& serviceGuid, CharacteristicsEnumCallback const& cb)
136 {
137 Service const* const ptrSvc = resolveBthLEService(device, serviceGuid);
138 if (ptrSvc == NULL)
139 {
140 GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Invalid service GUID", 0);
141 }
142 bthle_chara_cb_param param;
143 param.device = &device;
144 param.service = ptrSvc;
145 param.cb = &cb;
146 result_t result = gate_device_bthle_service_enum(&device, ptrSvc, &bthle_chara_cb, &param);
147 GATEXX_CHECK_EXCEPTION(result);
148 }
149
150 static gate_bool_t GATE_CALL bthle_chara_guid_cb(gate_device_bthle_characteristic_t const* chara, void* user_param)
151 {
152 ArrayList<Guid>* guids = static_cast<ArrayList<Guid>*>(user_param);
153 if (guids)
154 {
155 guids->tryAdd(Guid(chara->characteristic_guid));
156 }
157 return true;
158 }
159
160 Array<Guid> BluetoothLE::enumCharacteristicsGuids(Device const& device, Guid const& serviceGuid)
161 {
162 ArrayList<Guid> guids;
163 Service const* const ptrSvc = resolveBthLEService(device, serviceGuid);
164 if (ptrSvc == NULL)
165 {
166 GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Invalid service GUID", 0);
167 }
168 result_t result = gate_device_bthle_service_enum(&device, ptrSvc, &bthle_chara_guid_cb, &guids);
169 GATEXX_CHECK_EXCEPTION(result);
170 return guids.toArray();
171 }
172
173
174 Blob BluetoothLE::getCharacteristicValue(Device const& device, Guid const& serviceGuid, Guid const& charaGuid)
175 {
176 char buffer[4096];
177 gate_size_t bufferLen = sizeof(buffer);
178 result_t result = gate_device_bthle_characteristic_get(&device, &serviceGuid, &charaGuid, buffer, &bufferLen);
179 GATEXX_CHECK_EXCEPTION(result);
180
181 if (bufferLen > sizeof(buffer))
182 {
183 GATEXX_RAISE_ERROR(results::OutOfBounds);
184 }
185 return Blob(&buffer[0], bufferLen);
186 }
187
188 void BluetoothLE::setCharacteristicValue(Device const& device, Guid const& serviceGuid, Guid const& charaGuid, Blob const& value)
189 {
190 result_t result = gate_device_bthle_characteristic_set(&device, &serviceGuid, &charaGuid,
191 static_cast<char const*>(value.data()), value.length());
192 GATEXX_CHECK_EXCEPTION(result);
193 }
194
195
196 } // end of namespace sys
197 } // end of namespace gate
198