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#include <functional>
6
7#include "core.hpp"
8#include "ep_pool.hpp"
9#include "lockfree_list.hpp"
10#include "lockfree_pool.hpp"
11
12namespace LibXR::USB
13{
19{
20 public:
24 enum class Index : uint8_t
25 {
26 LANGUAGE_ID = 0x00,
27 MANUFACTURER_STRING = 0x01,
28 PRODUCT_STRING = 0x02,
30 };
31
35 enum class Language : uint16_t
36 {
37 EN_US = 0x0409,
38 ZH_CN = 0x0804
39 };
40
41 typedef const char* StringData;
42 static constexpr size_t STRING_LIST_SIZE = 3;
43
49 {
51 StringData strings[STRING_LIST_SIZE];
53 size_t string_lens[STRING_LIST_SIZE];
56 };
57
69 template <size_t N1, size_t N2, size_t N3>
70 static const constexpr LanguagePack MakeLanguagePack(Language lang,
71 const char (&manu)[N1],
72 const char (&prod)[N2],
73 const char (&serial)[N3])
74 {
75 static_assert(N1 < 128 && N2 < 128 && N3 < 128,
76 "String length must be less than 128.");
77
78 auto len_manu = CalcUTF16LELen(manu);
79 auto len_prod = CalcUTF16LELen(prod);
80 auto len_serial = CalcUTF16LELen(serial);
81
82 size_t maxlen = len_manu;
83 if (len_prod > maxlen)
84 {
85 maxlen = len_prod;
86 }
87 if (len_serial > maxlen)
88 {
89 maxlen = len_serial;
90 }
91
92 return LanguagePack{
93 lang, {manu, prod, serial}, {len_manu, len_prod, len_serial}, maxlen};
94 }
95
102 DescriptorStrings(const std::initializer_list<const LanguagePack*>& lang_list);
103
112 ErrorCode GenerateString(Index index, uint16_t lang);
119
126
127 private:
128 template <size_t N>
129 static constexpr size_t CalcUTF16LELen(const char (&input)[N])
130 {
131 size_t len = 0;
132 for (size_t i = 0; i < N && input[i];)
133 {
134 unsigned char c = static_cast<unsigned char>(input[i]);
135 if (c < 0x80)
136 {
137 len += 2;
138 i += 1;
139 }
140 else if ((c & 0xE0) == 0xC0)
141 {
142 len += 2;
143 i += 2;
144 }
145 else if ((c & 0xF0) == 0xE0)
146 {
147 len += 2;
148 i += 3;
149 }
150 else
151 {
152 i += 4;
153 }
154 }
155 return len;
156 }
157
158 static void ToUTF16LE(const char* str, uint8_t* buffer);
159
160 const size_t LANG_NUM;
161 uint16_t* header_;
162 uint16_t* land_id_;
165};
166} // namespace LibXR::USB
原始数据封装类。 A class for encapsulating raw data.
字符串描述符管理器 / USB string descriptor manager
Definition desc_str.hpp:19
Language
语言 / Supported language
Definition desc_str.hpp:36
@ ZH_CN
简体中文 / Simplified Chinese
ErrorCode GenerateString(Index index, uint16_t lang)
生成指定语言和索引的字符串描述符 Generate USB string descriptor for given language and string index
Definition desc_str.cpp:30
const size_t LANG_NUM
已注册语言数量 / Registered language count
Definition desc_str.hpp:160
RawData GetData()
获取当前构建好的字符串描述符数据 Get the descriptor buffer
Definition desc_str.cpp:63
DescriptorStrings(const std::initializer_list< const LanguagePack * > &lang_list)
USB 描述符字符串管理器构造函数 USB descriptor string manager constructor.
Definition desc_str.cpp:5
RawData buffer_
临时描述符缓冲区 / Temp descriptor buffer
Definition desc_str.hpp:164
RawData GetLangIDData()
获取语言ID描述符内容 Get LangID descriptor data
Definition desc_str.cpp:70
Index
描述符字符串索引 / USB descriptor string index
Definition desc_str.hpp:25
@ 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:163
uint16_t * land_id_
语言ID数组 / LangID array
Definition desc_str.hpp:162
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:70
uint16_t * header_
语言ID描述符头部 / LangID descriptor header
Definition desc_str.hpp:161
单语言描述符包(用于注册静态多语言字符串) Single language USB string descriptor pack for registration
Definition desc_str.hpp:49
StringData strings[STRING_LIST_SIZE]
Definition desc_str.hpp:51
size_t max_string_length
最大字符串长度 / Maximum string length
Definition desc_str.hpp:55