xref: /aosp_15_r20/external/pigweed/pw_bluetooth_profiles/device_info_service.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_bluetooth_profiles/device_info_service.h"
16 
17 #include "pw_assert/check.h"
18 
19 namespace pw::bluetooth_profiles {
20 
PublishService(bluetooth::gatt::Server * gatt_server,Callback<void (bluetooth::Result<bluetooth::gatt::Server::PublishServiceError>)> result_callback)21 void DeviceInfoServiceImpl::PublishService(
22     bluetooth::gatt::Server* gatt_server,
23     Callback<
24         void(bluetooth::Result<bluetooth::gatt::Server::PublishServiceError>)>
25         result_callback) {
26   PW_CHECK(publish_service_callback_ == nullptr);
27   publish_service_callback_ = std::move(result_callback);
28   this->delegate_.SetServicePtr(nullptr);
29   return gatt_server->PublishService(
30       service_info_,
31       &delegate_,
32       [this](bluetooth::gatt::Server::PublishServiceResult result) {
33         if (result.ok()) {
34           this->publish_service_callback_({});
35           this->delegate_.SetServicePtr(std::move(result.value()));
36         } else {
37           this->publish_service_callback_(result.error());
38         }
39       });
40 }
41 
OnError(bluetooth::gatt::Error)42 void DeviceInfoServiceImpl::Delegate::OnError(
43     bluetooth::gatt::Error /* error */) {
44   local_service_.reset();
45 }
46 
ReadValue(bluetooth::PeerId,bluetooth::gatt::Handle handle,uint32_t offset,Function<void (bluetooth::Result<bluetooth::gatt::Error,span<const std::byte>>)> && result_callback)47 void DeviceInfoServiceImpl::Delegate::ReadValue(
48     bluetooth::PeerId /* peer_id */,
49     bluetooth::gatt::Handle handle,
50     uint32_t offset,
51     Function<void(
52         bluetooth::Result<bluetooth::gatt::Error, span<const std::byte>>)>&&
53         result_callback) {
54   const uint32_t value_index = static_cast<uint32_t>(handle);
55   if (value_index >= values_.size()) {
56     result_callback(bluetooth::gatt::Error::kInvalidHandle);
57     return;
58   }
59   span<const std::byte> value = values_[value_index];
60   if (offset > value.size()) {
61     result_callback(bluetooth::gatt::Error::kInvalidOffset);
62     return;
63   }
64   result_callback({std::in_place, value.subspan(offset)});
65 }
66 
67 }  // namespace pw::bluetooth_profiles
68