libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
desc_str.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "core.hpp"
7#include "libxr_type.hpp"
8
9namespace LibXR::USB
10{
15{
16 public:
20 enum class Index : uint8_t
21 {
22 LANGUAGE_ID = 0x00,
23 MANUFACTURER_STRING = 0x01,
24 PRODUCT_STRING = 0x02,
26 };
27
31 enum class Language : uint16_t
32 {
33 EN_US = 0x0409,
34 ZH_CN = 0x0804
35 };
36
37 typedef const char* StringData;
38 static constexpr size_t STRING_LIST_SIZE = 3;
39
45 {
47 StringData strings[STRING_LIST_SIZE];
48 size_t string_lens[STRING_LIST_SIZE];
50 };
51
56 template <size_t N1, size_t N2, size_t N3>
57 static const constexpr LanguagePack MakeLanguagePack(Language lang,
58 const char (&manu)[N1],
59 const char (&prod)[N2],
60 const char (&serial)[N3])
61 {
62 static_assert(N1 < 128 && N2 < 128 && N3 < 128,
63 "String length must be less than 128.");
64
65 auto len_manu = CalcUTF16LELen(manu);
66 auto len_prod = CalcUTF16LELen(prod);
67 auto len_serial = CalcUTF16LELen(serial);
68
69 size_t maxlen = len_manu;
70 if (len_prod > maxlen)
71 {
72 maxlen = len_prod;
73 }
74 if (len_serial > maxlen)
75 {
76 maxlen = len_serial;
77 }
78
79 return LanguagePack{
80 lang, {manu, prod, serial}, {len_manu, len_prod, len_serial}, maxlen};
81 }
82
93 DescriptorStrings(const std::initializer_list<const LanguagePack*>& lang_list,
94 const uint8_t* uid = nullptr, size_t uid_len = 0);
95
96 DescriptorStrings(const DescriptorStrings&) = delete;
97 DescriptorStrings& operator=(const DescriptorStrings&) = delete;
98
107 ErrorCode GenerateString(Index index, uint16_t lang);
108
115
122
128 [[nodiscard]] bool HasLanguage(uint16_t lang) const;
129
130 private:
131 template <size_t N>
132 static constexpr size_t CalcUTF16LELen(const char (&input)[N])
133 {
134 size_t len = 0;
135 for (size_t i = 0; i < N && input[i];)
136 {
137 unsigned char c = static_cast<unsigned char>(input[i]);
138 if (c < 0x80)
139 {
140 len += 2;
141 i += 1;
142 }
143 else if ((c & 0xE0) == 0xC0)
144 {
145 len += 2;
146 i += 2;
147 }
148 else if ((c & 0xF0) == 0xE0)
149 {
150 len += 2;
151 i += 3;
152 }
153 else
154 {
155 i += 4;
156 }
157 }
158 return len;
159 }
160
161 static void ToUTF16LE(const char* str, uint8_t* buffer);
162
163 const size_t LANG_NUM;
164 uint16_t* header_;
165 uint16_t* land_id_;
168
169 const uint8_t* serial_uid_;
172};
173
174} // namespace LibXR::USB
原始数据封装类。 A class for encapsulating raw data.
字符串描述符管理器 / USB string descriptor manager
Definition desc_str.hpp:15
Language
语言 / Supported language
Definition desc_str.hpp:32
@ ZH_CN
简体中文 / Simplified Chinese
const size_t LANG_NUM
已注册语言数量 / Registered language count
Definition desc_str.hpp:163
bool HasLanguage(uint16_t lang) const
检查是否注册了指定语言 / Check whether the given language is registered
Definition desc_str.cpp:151
RawData GetData()
获取当前构建好的字符串描述符数据 Get the descriptor buffer
Definition desc_str.cpp:139
RawData buffer_
临时描述符缓冲区 / Temp descriptor buffer
Definition desc_str.hpp:167
size_t serial_uid_len_
UID 字节数 / UID byte count.
Definition desc_str.hpp:171
RawData GetLangIDData()
获取语言ID描述符内容 Get LangID descriptor data
Definition desc_str.cpp:146
DescriptorStrings(const std::initializer_list< const LanguagePack * > &lang_list, const uint8_t *uid=nullptr, size_t uid_len=0)
USB 描述符字符串管理器构造函数 USB descriptor string manager constructor.
Definition desc_str.cpp:5
Index
描述符字符串索引 / USB descriptor string index
Definition desc_str.hpp:21
@ MANUFACTURER_STRING
厂商字符串索引 / Manufacturer string
@ PRODUCT_STRING
产品字符串索引 / Product string
@ LANGUAGE_ID
语言ID描述符 / LangID descriptor
@ SERIAL_NUMBER_STRING
序列号字符串索引 / Serial number string
const LanguagePack ** string_list_
多语言包指针表 / LanguagePack pointer table
Definition desc_str.hpp:166
ErrorCode GenerateString(Index index, uint16_t lang)
生成指定语言和索引的字符串描述符 Generate USB string descriptor for given language and string index
Definition desc_str.cpp:58
uint16_t * land_id_
语言ID数组 / LangID array
Definition desc_str.hpp:165
static const constexpr LanguagePack MakeLanguagePack(Language lang, const char(&manu)[N1], const char(&prod)[N2], const char(&serial)[N3])
编译期构造 LanguagePack Compile-time LanguagePack constructor
Definition desc_str.hpp:57
uint16_t * header_
语言ID描述符头部 / LangID descriptor header
Definition desc_str.hpp:164
ErrorCode
定义错误码枚举
单语言描述符包(用于注册静态多语言字符串) Single language USB string descriptor pack for registration
Definition desc_str.hpp:45
size_t string_lens[STRING_LIST_SIZE]
每个字符串的字节数 / String byte lengths
Definition desc_str.hpp:48
StringData strings[STRING_LIST_SIZE]
指向 UTF-16LE 静态数组 / UTF-16LE strings
Definition desc_str.hpp:47
size_t max_string_length
最大字符串长度 / Maximum string length
Definition desc_str.hpp:49