File: otp.h

package info (click to toggle)
picotool 2.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,448 kB
  • sloc: cpp: 59,935; ansic: 2,513; python: 97; sh: 48; makefile: 27; xml: 18
file content (97 lines) | stat: -rw-r--r-- 4,108 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef _OTP_H
#define _OTP_H

#include <string>
#include <cassert>
#include <vector>
#include <cstdint>

#include "nlohmann/json.hpp"

using json = nlohmann::json;

template <typename T> std::basic_string<T> lowercase(const std::basic_string<T>& s);
template <typename T> std::basic_string<T> uppercase(const std::basic_string<T>& s);

struct otp_field {
    otp_field() = default;
    otp_field(std::string name, uint32_t mask) : name(std::move(name)), mask(mask) {
        upper_name = uppercase(this->name);
    }
    otp_field(std::string name, uint32_t mask, std::string description) : otp_field(std::move(name), mask) {
        this->description = std::move(description);
    }
    std::string name;
    std::string upper_name;
    uint32_t mask;
    std::string description;

    friend void to_json(json& nlohmann_json_j, const otp_field& nlohmann_json_t) {
        NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, name, mask, description))
    }

    friend void from_json(const json& nlohmann_json_j, otp_field& nlohmann_json_t) {
        NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, name, mask, description))
        nlohmann_json_t.upper_name = uppercase(nlohmann_json_t.name);
    }
};

struct otp_reg {
    otp_reg() = default;
    explicit otp_reg(std::string name, uint32_t row, uint32_t mask) : name(std::move(name)), row(row), mask(mask) {
        upper_name = uppercase(this->name);
    }
    otp_reg& with_ecc() { assert(!redundancy); ecc = true; return *this; }
    otp_reg& with_crit() { assert(!ecc); crit = true; return *this; }
    otp_reg& with_redundancy(int r) { assert(!ecc); redundancy = r; return *this; }
    otp_reg& with_description(std::string d) { description = std::move(d); return *this; }
    otp_reg& with_field(const otp_field& field) { fields.push_back(field); return *this; }
    otp_reg& with_sequence(std::string prefix, int index, int length) { seq_prefix = std::move(prefix); seq_index = index; seq_length = length; return *this; }
    std::string name;
    std::string upper_name;
    std::string description;
    uint32_t row = 0xffffffff;
    uint32_t mask = 0;
    bool ecc = false;
    bool crit = false;
    unsigned int redundancy = 0;
    unsigned int seq_length = 0;
    unsigned int seq_index = 0;
    std::string seq_prefix;
    std::vector<otp_field> fields;

    friend void to_json(json& nlohmann_json_j, const otp_reg& nlohmann_json_t) {
        NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, name, description, ecc, crit, fields, mask, redundancy, row, seq_index, seq_length, seq_prefix))
    }

    friend void from_json(const json& nlohmann_json_j, otp_reg& nlohmann_json_t) {
        NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, name, description, row))
        if (nlohmann_json_j.contains("ecc")) nlohmann_json_j.at("ecc").get_to(nlohmann_json_t.ecc);
        if (nlohmann_json_j.contains("crit")) nlohmann_json_j.at("crit").get_to(nlohmann_json_t.crit);
        if (nlohmann_json_j.contains("redundancy")) nlohmann_json_j.at("redundancy").get_to(nlohmann_json_t.redundancy);

        if (nlohmann_json_j.contains("fields")) nlohmann_json_j.at("fields").get_to(nlohmann_json_t.fields);
        if (nlohmann_json_j.contains("mask")){
            nlohmann_json_j.at("mask").get_to(nlohmann_json_t.mask);
        } else {
            nlohmann_json_t.mask = nlohmann_json_t.ecc ? 0xffff : 0xffffff;
        }

        if (nlohmann_json_j.contains("seq_length") || nlohmann_json_j.contains("seq_index") || nlohmann_json_j.contains("seq_prefix")) {
            nlohmann_json_j.at("seq_length").get_to(nlohmann_json_t.seq_length);
            nlohmann_json_j.at("seq_index").get_to(nlohmann_json_t.seq_index);
            nlohmann_json_j.at("seq_prefix").get_to(nlohmann_json_t.seq_prefix);
        }
        nlohmann_json_t.upper_name = uppercase(nlohmann_json_t.name);
    }
};

void init_otp(std::map<uint32_t, otp_reg> &otp_regs, std::vector<std::string> extra_otp_files = {});

#endif