2020-01-31 01:32:18 +00:00
|
|
|
// Copyright 2020 The Pigweed Authors
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// https://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.
|
2020-02-04 15:36:45 +00:00
|
|
|
|
|
|
|
// This file defines classes for managing the in-flash format for KVS entires.
|
2020-01-31 01:32:18 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2020-02-04 15:36:45 +00:00
|
|
|
#include <string_view>
|
2020-01-31 01:32:18 +00:00
|
|
|
|
2020-02-04 15:36:45 +00:00
|
|
|
#include "pw_kvs/checksum.h"
|
|
|
|
#include "pw_kvs/flash_memory.h"
|
2020-01-31 01:32:18 +00:00
|
|
|
#include "pw_span/span.h"
|
|
|
|
|
|
|
|
namespace pw::kvs {
|
|
|
|
|
2020-02-04 15:36:45 +00:00
|
|
|
// EntryHeader represents a key-value entry as stored in flash.
|
|
|
|
class EntryHeader {
|
|
|
|
public:
|
|
|
|
EntryHeader() = default;
|
2020-01-31 01:32:18 +00:00
|
|
|
|
|
|
|
EntryHeader(uint32_t magic,
|
2020-02-04 15:36:45 +00:00
|
|
|
ChecksumAlgorithm* algorithm,
|
|
|
|
std::string_view key,
|
|
|
|
span<const std::byte> value,
|
|
|
|
uint32_t key_version);
|
2020-01-31 01:32:18 +00:00
|
|
|
|
2020-02-04 15:36:45 +00:00
|
|
|
Status VerifyChecksum(ChecksumAlgorithm* algorithm,
|
|
|
|
std::string_view key,
|
|
|
|
span<const std::byte> value) const;
|
|
|
|
|
|
|
|
Status VerifyChecksumInFlash(FlashPartition* partition,
|
|
|
|
FlashPartition::Address header_address,
|
2020-02-05 01:47:40 +00:00
|
|
|
ChecksumAlgorithm* algorithm) const;
|
2020-01-31 01:32:18 +00:00
|
|
|
|
2020-02-02 21:58:19 +00:00
|
|
|
size_t entry_size() const {
|
|
|
|
return sizeof(*this) + key_length() + value_length();
|
|
|
|
}
|
|
|
|
|
2020-01-31 01:32:18 +00:00
|
|
|
uint32_t magic() const { return magic_; }
|
2020-02-02 21:58:19 +00:00
|
|
|
|
2020-02-04 15:36:45 +00:00
|
|
|
uint32_t checksum() const { return checksum_; }
|
2020-02-02 21:58:19 +00:00
|
|
|
|
2020-01-31 01:32:18 +00:00
|
|
|
size_t key_length() const { return key_value_length_ & kKeyLengthMask; }
|
2020-02-02 21:58:19 +00:00
|
|
|
void set_key_length(uint32_t key_length) {
|
|
|
|
key_value_length_ = key_length | (~kKeyLengthMask & key_value_length_);
|
|
|
|
}
|
|
|
|
|
2020-01-31 01:32:18 +00:00
|
|
|
size_t value_length() const { return key_value_length_ >> kValueLengthShift; }
|
2020-02-02 21:58:19 +00:00
|
|
|
void set_value_length(uint32_t value_length) {
|
|
|
|
key_value_length_ = (value_length << kValueLengthShift) |
|
|
|
|
(kKeyLengthMask & key_value_length_);
|
|
|
|
}
|
|
|
|
|
2020-01-31 01:32:18 +00:00
|
|
|
uint32_t key_version() const { return key_version_; }
|
|
|
|
|
|
|
|
private:
|
2020-02-04 15:36:45 +00:00
|
|
|
static constexpr uint32_t kNoChecksum = 0;
|
2020-01-31 01:32:18 +00:00
|
|
|
static constexpr uint32_t kKeyLengthMask = 0b111111;
|
|
|
|
static constexpr uint32_t kValueLengthShift = 8;
|
|
|
|
|
2020-02-05 01:47:40 +00:00
|
|
|
static constexpr size_t checked_data_offset() {
|
|
|
|
return offsetof(EntryHeader, key_value_length_);
|
|
|
|
}
|
|
|
|
|
2020-02-04 15:36:45 +00:00
|
|
|
span<const std::byte> checksum_bytes() const {
|
|
|
|
return as_bytes(span(&checksum_, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalculateChecksum(ChecksumAlgorithm* algorithm,
|
|
|
|
std::string_view key,
|
2020-02-05 01:47:40 +00:00
|
|
|
span<const std::byte> value) const;
|
2020-02-04 15:36:45 +00:00
|
|
|
|
2020-01-31 01:32:18 +00:00
|
|
|
uint32_t magic_;
|
|
|
|
uint32_t checksum_;
|
2020-02-02 21:58:19 +00:00
|
|
|
// 6 bits, 0: 5 - key - maximum 64 characters
|
|
|
|
// 2 bits, 6: 7 - reserved
|
|
|
|
// 24 bits, 8:31 - value - maximum 16MB
|
2020-01-31 01:32:18 +00:00
|
|
|
uint32_t key_value_length_;
|
|
|
|
uint32_t key_version_;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(EntryHeader) == 16, "EntryHeader should have no padding");
|
|
|
|
|
|
|
|
} // namespace pw::kvs
|