BNO08x Driver
API reference
Loading...
Searching...
No Matches
MemoryFirmware.hpp
Go to the documentation of this file.
1#pragma once
2#include "HcBin.h"
3#include <cstdint>
4#include <cstring>
5
17public:
18 MemoryFirmware(const uint8_t* data, uint32_t len, const char* format = "BNO_V1",
19 const char* part = "unknown", uint32_t packet_len = 0)
20 : data_(data), len_(len), format_(format), part_(part), packet_len_(packet_len) {}
21
23 const HcBin_t& hcbin() const {
24 pending() = this;
25 return impl_;
26 }
27
28private:
29 static int open();
30 static int close();
31 static const char* getMeta(const char* key);
32 static uint32_t getAppLen();
33 static uint32_t getPacketLen();
34 static int getAppData(uint8_t* packet, uint32_t offset, uint32_t len);
35
36 static const HcBin_t impl_;
37
38 // Per-thread adapter state, kept in function-local statics: Apple clang emits a
39 // non-weak thread-local wrapper for an inline thread_local static data member, so
40 // two translation units using MemoryFirmware failed to link with duplicate symbols.
41 static const MemoryFirmware*& pending() noexcept {
42 static thread_local const MemoryFirmware* p = nullptr;
43 return p;
44 }
45 static const MemoryFirmware*& active() noexcept {
46 static thread_local const MemoryFirmware* a = nullptr;
47 return a;
48 }
49
50 const uint8_t* data_;
51 uint32_t len_;
52 const char* format_;
53 const char* part_;
54 uint32_t packet_len_;
55};
56
57// --- Implementation ---------------------------------------------------------
58
59inline int MemoryFirmware::open() {
60 if (active() != nullptr) {
61 return -1;
62 }
63 if (pending() == nullptr) {
64 return -1;
65 }
66 active() = pending();
67 pending() = nullptr;
68 return 0;
69}
70inline int MemoryFirmware::close() {
71 active() = nullptr;
72 pending() = nullptr;
73 return 0;
74}
75inline const char* MemoryFirmware::getMeta(const char* key) {
76 if (!active())
77 return nullptr;
78 if (std::strcmp(key, "FW-Format") == 0)
79 return active()->format_;
80 if (std::strcmp(key, "SW-Part-Number") == 0)
81 return active()->part_;
82 return nullptr;
83}
84inline uint32_t MemoryFirmware::getAppLen() {
85 return active() ? active()->len_ : 0;
86}
87inline uint32_t MemoryFirmware::getPacketLen() {
88 return active() ? active()->packet_len_ : 0;
89}
90inline int MemoryFirmware::getAppData(uint8_t* packet, uint32_t offset, uint32_t len) {
91 if (!active() || offset > active()->len_ || len > (active()->len_ - offset))
92 return -1;
93 std::memcpy(packet, active()->data_ + offset, len);
94 return 0;
95}
96
97inline const HcBin_t MemoryFirmware::impl_ = {
98 MemoryFirmware::open, MemoryFirmware::close, MemoryFirmware::getMeta,
99 MemoryFirmware::getAppLen, MemoryFirmware::getPacketLen, MemoryFirmware::getAppData};
API Definition for HcBin objects (Hillcrest Binary Files).
HcBin_t implementation for firmware stored in memory.
Definition MemoryFirmware.hpp:16
const HcBin_t & hcbin() const
Definition MemoryFirmware.hpp:23
MemoryFirmware(const uint8_t *data, uint32_t len, const char *format="BNO_V1", const char *part="unknown", uint32_t packet_len=0)
Definition MemoryFirmware.hpp:18
Definition HcBin.h:47