Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 026a813b authored by Myles Watson's avatar Myles Watson
Browse files

packet/parser: Add Checksum support

Make TypeDef represent Enum, Custom, and Checksum types.
Add SimpleSum as a test checksum.

Test: bluetooth_test_gd --gtest_filter=*SimpleSum*
Change-Id: Ie145d1dd17a710b50e98de8714102945b3c65c99
parent 0c675904
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ cc_binary_host {
  name: "bluetooth_packetgen",
  srcs: [
    "fields/body_field.cc",
    "fields/checksum_field.cc",
    "fields/checksum_start_field.cc",
    "fields/custom_field.cc",
    "fields/enum_field.cc",
    "fields/fixed_field.cc",
@@ -11,6 +13,7 @@ cc_binary_host {
    "fields/reserved_field.cc",
    "fields/scalar_field.cc",
    "fields/size_field.cc",
    "checksum_def.cc",
    "custom_field_def.cc",
    "enum_def.cc",
    "enum_gen.cc",
+8 −0
Original line number Diff line number Diff line
@@ -12,6 +12,13 @@ Packet Views and Builders
  extract the fields that are defined in the pdl file.  Builders check the input
  arguments and can be serialized.

Checksum types
  checksum MyChecksumClass : 16 "path/to/the/class/"
  Checksum fields need to implement the following three static methods:
    static void Initialize(MyChecksumClass&);
    static void AddByte(MyChecksumClass&, uint8_t);
    // Assuming a 16-bit (uint16_t) checksum:
    static uint16_t GetChecksum(MyChecksumClass&);
-------------
 LIMITATIONS
-------------
@@ -27,6 +34,7 @@ Packet Views and Builders

  - Can't handle size for Body type fields yet since they might not be byte aligned.


-------
 NOTES
-------
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "checksum_def.h"

#include "util.h"

ChecksumDef::ChecksumDef(std::string name, std::string include, int size) : CustomFieldDef(name, include, size) {}

PacketField* ChecksumDef::GetNewField(const std::string& name, ParseLocation loc) const {
  return new ChecksumField(name, name_, size_, loc);
}

TypeDef::Type ChecksumDef::GetDefinitionType() const {
  return TypeDef::Type::CHECKSUM;
}

void ChecksumDef::GenInclude(std::ostream& s) const {
  CustomFieldDef::GenInclude(s);
}

void ChecksumDef::GenUsing(std::ostream& s) const {
  CustomFieldDef::GenUsing(s);
}

void ChecksumDef::GenChecksumCheck(std::ostream& s) const {
  s << "static_assert(ChecksumTypeChecker<" << name_ << "," << util::GetTypeForSize(size_) << ">::value, \"";
  s << name_ << " is not a valid checksum type. Please see README for more details.\");";
}
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <iostream>

#include "checksum_type_checker.h"
#include "custom_field_def.h"
#include "fields/checksum_field.h"
#include "parse_location.h"
#include "type_def.h"

class ChecksumDef : public CustomFieldDef {
 public:
  ChecksumDef(std::string name, std::string include, int size);

  virtual PacketField* GetNewField(const std::string& name, ParseLocation loc) const override;

  virtual TypeDef::Type GetDefinitionType() const override;

  virtual void GenInclude(std::ostream& s) const override;

  virtual void GenUsing(std::ostream& s) const override;

  void GenChecksumCheck(std::ostream& s) const;

  const std::string include_;
};
+36 −0
Original line number Diff line number Diff line
#include <optional>

namespace bluetooth {
namespace packet {
namespace parser {

// Checks for Initialize(), AddByte(), and GetChecksum().
// T and TRET are the checksum class Type and the checksum return type
// C and CRET are the substituted types for T and TRET
template <typename T, typename TRET>
class ChecksumTypeChecker {
 public:
  template <class C, void (*)(C&)>
  struct InitializeChecker {};

  template <class C, void (*)(C&, uint8_t byte)>
  struct AddByteChecker {};

  template <class C, typename CRET, CRET (*)(const C&)>
  struct GetChecksumChecker {};

  // If all the methods are defined, this one matches
  template <class C, typename CRET>
  static int Test(InitializeChecker<C, &C::Initialize>*, AddByteChecker<C, &C::AddByte>*,
                  GetChecksumChecker<C, CRET, &C::GetChecksum>*);

  // This one matches everything else
  template <class C, typename CRET>
  static char Test(...);

  // This checks which template was matched
  static constexpr bool value = (sizeof(Test<T, TRET>(0, 0, 0)) == sizeof(int));
};
}  // namespace parser
}  // namespace packet
}  // namespace bluetooth
Loading