Function Deserialize

Synopsis

#include <bta/gatt/database.h>

static Database Deserialize(const std::vector< gatt::StoredAttribute > &nv_attr, bool *success)

Description

No description yet.

Source

Lines 120-183 in bta/gatt/database.cc. Line 113 in bta/gatt/database.h.

Database Database::Deserialize(const std::vector<StoredAttribute>& nv_attr,
                               bool* success) {
  // clear reallocating
  Database result;
  auto it = nv_attr.cbegin();

  for (; it != nv_attr.cend(); ++it) {
    const auto& attr = *it;
    if (attr.type != PRIMARY_SERVICE && attr.type != SECONDARY_SERVICE) break;
    result.services.emplace_back(
        Service{.handle = attr.handle,
                .end_handle = attr.value.service.end_handle,
                .is_primary = (attr.type == PRIMARY_SERVICE),
                .uuid = attr.value.service.uuid});
  }

  auto current_service_it = result.services.begin();
  for (; it != nv_attr.cend(); it++) {
    const auto& attr = *it;

    // go to the service this attribute belongs to; attributes are stored in
    // order, so iterating just forward is enough
    while (current_service_it != result.services.end() &&
           current_service_it->end_handle < attr.handle) {
      current_service_it++;
    }

    if (current_service_it == result.services.end() ||
        !HandleInRange(*current_service_it, attr.handle)) {
      LOG(ERROR) << "Can't find service for attribute with handle: "
                 << loghex(attr.handle);
      *success = false;
      return result;
    }

    if (attr.type == INCLUDE) {
      Service* included_service =
          FindService(result.services, attr.value.included_service.handle);
      if (!included_service) {
        LOG(ERROR) << __func__ << ": Non-existing included service!";
        *success = false;
        return result;
      }
      current_service_it->included_services.push_back(IncludedService{
          .handle = attr.handle,
          .uuid = attr.value.included_service.uuid,
          .start_handle = attr.value.included_service.handle,
          .end_handle = attr.value.included_service.end_handle,
      });
    } else if (attr.type == CHARACTERISTIC) {
      current_service_it->characteristics.emplace_back(
          Characteristic{.declaration_handle = attr.handle,
                         .value_handle = attr.value.characteristic.value_handle,
                         .properties = attr.value.characteristic.properties,
                         .uuid = attr.value.characteristic.uuid});

    } else {
      current_service_it->characteristics.back().descriptors.emplace_back(
          Descriptor{.handle = attr.handle, .uuid = attr.type});
    }
  }
  *success = true;
  return result;
}





Add Discussion as Guest

Log in to DocsForge