HF-BNO08x  0.1.0-dev
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 static thread_local const MemoryFirmware* pending_;
38 static thread_local const MemoryFirmware* active_;
39
40 const uint8_t* data_;
41 uint32_t len_;
42 const char* format_;
43 const char* part_;
44 uint32_t packet_len_;
45};
46
47// --- Implementation ---------------------------------------------------------
48
49inline int MemoryFirmware::open() {
50 if (active_ != nullptr) {
51 return -1;
52 }
53 if (pending_ == nullptr) {
54 return -1;
55 }
56 active_ = pending_;
57 pending_ = nullptr;
58 return 0;
59}
60inline int MemoryFirmware::close() {
61 active_ = nullptr;
62 pending_ = nullptr;
63 return 0;
64}
65inline const char* MemoryFirmware::getMeta(const char* key) {
66 if (!active_)
67 return nullptr;
68 if (std::strcmp(key, "FW-Format") == 0)
69 return active_->format_;
70 if (std::strcmp(key, "SW-Part-Number") == 0)
71 return active_->part_;
72 return nullptr;
73}
74inline uint32_t MemoryFirmware::getAppLen() {
75 return active_ ? active_->len_ : 0;
76}
77inline uint32_t MemoryFirmware::getPacketLen() {
78 return active_ ? active_->packet_len_ : 0;
79}
80inline int MemoryFirmware::getAppData(uint8_t* packet, uint32_t offset, uint32_t len) {
81 if (!active_ || offset > active_->len_ || len > (active_->len_ - offset))
82 return -1;
83 std::memcpy(packet, active_->data_ + offset, len);
84 return 0;
85}
86
87inline const HcBin_t MemoryFirmware::impl_ = {
88 MemoryFirmware::open, MemoryFirmware::close, MemoryFirmware::getMeta,
89 MemoryFirmware::getAppLen, MemoryFirmware::getPacketLen, MemoryFirmware::getAppData};
90inline thread_local const MemoryFirmware* MemoryFirmware::pending_ = nullptr;
91inline thread_local const MemoryFirmware* MemoryFirmware::active_ = nullptr;
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