app_usbd_string_desc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_config.h"
  41. #if APP_USBD_ENABLED
  42. #include "app_usbd_string_desc.h"
  43. #include "app_usbd_langid.h"
  44. #include "app_usbd_core.h"
  45. #include "nordic_common.h"
  46. #include "utf.h"
  47. /**
  48. * @defgroup app_usbd_string_desc
  49. * @ingroup app_usbd
  50. *
  51. * USBD string descriptor management
  52. * @{
  53. */
  54. /**
  55. * @brief Array with language identifiers.
  56. *
  57. * This array is used to search the proper string for the selected language.
  58. */
  59. static uint16_t const m_langids[] = { APP_USBD_STRINGS_LANGIDS };
  60. /**
  61. * @brief Language ID descriptor.
  62. *
  63. * Language.
  64. */
  65. /**
  66. * @brief Mnemonics for the string positions in the array.
  67. *
  68. * The mnemonics for the indexes of the strings inside the string array.
  69. */
  70. enum {
  71. APP_USBD_STRING_ID_LANGIDS_ARRAY_POS = 0, /**< Supported language identifiers. */
  72. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  73. APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS, /**< Manufacturer name. */
  74. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  75. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  76. APP_USBD_STRING_ID_PRODUCT_ARRAY_POS, /**< Product name. */
  77. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  78. #if (APP_USBD_STRING_ID_SERIAL != 0)
  79. APP_USBD_STRING_ID_SERIAL_ARRAY_POS, /**< Serial number. */
  80. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  81. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  82. APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS, /**< Configuration string. */
  83. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  84. #define X(mnemonic, str_idx, ...) CONCAT_2(mnemonic, _ARRAY_POS),
  85. APP_USBD_STRINGS_USER
  86. #undef X
  87. };
  88. /**
  89. * @brief String index into internal array index conversion table.
  90. *
  91. * The array that transforms the USB string indexes into internal array position.
  92. * @note Value 0 is used to mark non-existing string.
  93. */
  94. #define X(mnemonic, str_idx, ...) 1 +
  95. static app_usbd_strings_convert_t const m_string_translation[APP_USBD_STRINGS_NUM] =
  96. #undef X
  97. {
  98. {
  99. .identifier = APP_USBD_STRING_ID_LANGIDS,
  100. .array_pos = APP_USBD_STRING_ID_LANGIDS_ARRAY_POS,
  101. },
  102. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  103. {
  104. .identifier = APP_USBD_STRING_ID_MANUFACTURER,
  105. .array_pos = APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS,
  106. },
  107. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  108. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  109. {
  110. .identifier = APP_USBD_STRING_ID_PRODUCT,
  111. .array_pos = APP_USBD_STRING_ID_PRODUCT_ARRAY_POS,
  112. },
  113. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  114. #if (APP_USBD_STRING_ID_SERIAL != 0)
  115. {
  116. .identifier = APP_USBD_STRING_ID_SERIAL,
  117. .array_pos = APP_USBD_STRING_ID_SERIAL_ARRAY_POS,
  118. },
  119. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  120. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  121. {
  122. .identifier = APP_USBD_STRING_ID_CONFIGURATION,
  123. .array_pos = APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS,
  124. },
  125. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  126. #define X(mnemonic, ...) \
  127. { \
  128. .identifier = mnemonic, \
  129. .array_pos = CONCAT_2(mnemonic, _ARRAY_POS), \
  130. },
  131. APP_USBD_STRINGS_USER
  132. #undef X
  133. };
  134. #ifndef APP_USBD_STRINGS_MANUFACTURER_EXTERN
  135. #define APP_USBD_STRINGS_MANUFACTURER_EXTERN 0
  136. #endif
  137. #if APP_USBD_STRINGS_MANUFACTURER_EXTERN
  138. extern uint8_t APP_USBD_STRINGS_MANUFACTURER[];
  139. #endif
  140. #ifndef APP_USBD_STRINGS_PRODUCT_EXTERN
  141. #define APP_USBD_STRINGS_PRODUCT_EXTERN 0
  142. #endif
  143. #if APP_USBD_STRINGS_PRODUCT_EXTERN
  144. extern uint8_t APP_USBD_STRINGS_PRODUCT[];
  145. #endif
  146. #ifndef APP_USBD_STRING_SERIAL_EXTERN
  147. #define APP_USBD_STRING_SERIAL_EXTERN 0
  148. #endif
  149. #if APP_USBD_STRING_SERIAL_EXTERN
  150. extern uint8_t APP_USBD_STRING_SERIAL[];
  151. #endif
  152. #ifndef APP_USBD_STRING_CONFIGURATION_EXTERN
  153. #define APP_USBD_STRING_CONFIGURATION_EXTERN 0
  154. #endif
  155. #if APP_USBD_STRING_CONFIGURATION_EXTERN
  156. extern uint8_t APP_USBD_STRING_CONFIGURATION[];
  157. #endif
  158. /**
  159. * @brief String descriptor table.
  160. * */
  161. #define X(mnemonic, str_idx, ...) 1 +
  162. static uint8_t const * m_string_dsc[APP_USBD_STRINGS_NUM][ARRAY_SIZE(m_langids)] =
  163. #undef X
  164. {
  165. [APP_USBD_STRING_ID_LANGIDS_ARRAY_POS] = {APP_USBD_STRING_RAW16_DESC(APP_USBD_STRINGS_LANGIDS)},
  166. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  167. [APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS] = { APP_USBD_STRINGS_MANUFACTURER },
  168. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  169. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  170. [APP_USBD_STRING_ID_PRODUCT_ARRAY_POS] = { APP_USBD_STRINGS_PRODUCT },
  171. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  172. #if (APP_USBD_STRING_ID_SERIAL != 0)
  173. [APP_USBD_STRING_ID_SERIAL_ARRAY_POS] = { APP_USBD_STRING_SERIAL },
  174. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  175. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  176. [APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS] = { APP_USBD_STRINGS_CONFIGURATION },
  177. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  178. #define X(mnemonic, str_idx, ...) [CONCAT_2(mnemonic, _ARRAY_POS)] = {__VA_ARGS__},
  179. APP_USBD_STRINGS_USER
  180. #undef X
  181. };
  182. /**
  183. * @brief Function for preparing UTF16 string descriptor.
  184. *
  185. * @param idx String descriptor ID.
  186. * @param langid Language ID.
  187. *
  188. * @return Pointer to the string descriptor.
  189. */
  190. static uint16_t * app_usbd_prepare_string(uint8_t idx, uint16_t langid)
  191. {
  192. if (m_string_dsc[idx][langid][0] == 0x00)
  193. {
  194. return (uint16_t *) &(m_string_dsc[idx][langid][2]);
  195. }
  196. #if ((APP_USBD_CONFIG_DESC_STRING_SIZE * 2) + 2) <= NRF_DRV_USBD_EPSIZE
  197. uint16_t * string_buffer = app_usbd_core_setup_transfer_buff_get(NULL);
  198. #else
  199. static uint16_t string_buffer[APP_USBD_CONFIG_DESC_STRING_SIZE + 1];
  200. // + 1 element for string descriptor type and size
  201. #endif
  202. uint8_t size = 0;
  203. const uint8_t * p_pos = m_string_dsc[idx][langid];
  204. #if APP_USBD_CONFIG_DESC_STRING_UTF_ENABLED
  205. size = utf8UTF16Count((char *) p_pos, 0);
  206. ASSERT(size <= APP_USBD_CONFIG_DESC_STRING_SIZE);
  207. uint16_t * p_out = &(string_buffer[1]);
  208. uint32_t rune;
  209. while (*p_pos != 0)
  210. {
  211. p_pos = (uint8_t *) utf8DecodeRune((char *) p_pos, 0, &rune);
  212. p_out += utf16EncodeRune(rune, p_out);
  213. }
  214. #else
  215. while(*p_pos != 0)
  216. {
  217. ASSERT(size < APP_USBD_CONFIG_DESC_STRING_SIZE);
  218. ++size;
  219. string_buffer[size] = *p_pos;
  220. ++p_pos;
  221. }
  222. #endif
  223. // Descriptor size is length of the string times 2 bytes per character + 2 bytes for
  224. // descriptor type and size.
  225. string_buffer[0] = (0xff & (size * 2 + 2)) | ((uint16_t)APP_USBD_DESCRIPTOR_STRING) << 8;
  226. return string_buffer;
  227. }
  228. uint16_t const * app_usbd_string_desc_get(uint8_t idx, uint16_t langid)
  229. {
  230. /* LANGID string. */
  231. if (APP_USBD_STRING_ID_LANGIDS == idx)
  232. {
  233. return app_usbd_prepare_string(APP_USBD_STRING_ID_LANGIDS_ARRAY_POS, 0);
  234. }
  235. /* Searching for the language. */
  236. uint8_t lang_idx = 0;
  237. if (ARRAY_SIZE(m_langids) > 1)
  238. {
  239. while (m_langids[lang_idx] != langid)
  240. {
  241. ++lang_idx;
  242. if (lang_idx >= ARRAY_SIZE(m_langids))
  243. {
  244. return NULL;
  245. }
  246. }
  247. }
  248. uint8_t str_pos = 0;
  249. for (uint8_t i = 0; i < ARRAY_SIZE(m_string_translation); i++)
  250. {
  251. if (m_string_translation[i].identifier == idx)
  252. {
  253. str_pos = m_string_translation[i].array_pos;
  254. break;
  255. }
  256. }
  257. if (str_pos == 0)
  258. {
  259. return NULL;
  260. }
  261. if ((ARRAY_SIZE(m_langids) > 1) && (lang_idx != 0))
  262. {
  263. if (m_string_dsc[str_pos][lang_idx] == NULL)
  264. {
  265. lang_idx = 0;
  266. }
  267. }
  268. if (m_string_dsc[str_pos][lang_idx] == NULL)
  269. {
  270. return NULL;
  271. }
  272. return app_usbd_prepare_string(str_pos, lang_idx);
  273. }
  274. /** @} */
  275. #endif // APP_USBD_ENABLED