app_usbd_string_desc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. static uint8_t const m_string_translation[APP_USBD_STRING_ID_CNT] =
  95. {
  96. [APP_USBD_STRING_ID_LANGIDS] = APP_USBD_STRING_ID_LANGIDS_ARRAY_POS,
  97. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  98. [APP_USBD_STRING_ID_MANUFACTURER] = APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS,
  99. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  100. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  101. [APP_USBD_STRING_ID_PRODUCT] = APP_USBD_STRING_ID_PRODUCT_ARRAY_POS,
  102. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  103. #if (APP_USBD_STRING_ID_SERIAL != 0)
  104. [APP_USBD_STRING_ID_SERIAL] = APP_USBD_STRING_ID_SERIAL_ARRAY_POS,
  105. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  106. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  107. [APP_USBD_STRING_ID_CONFIGURATION] = APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS,
  108. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  109. #define X(mnemonic, str_idx, ...) [mnemonic] = CONCAT_2(mnemonic, _ARRAY_POS),
  110. APP_USBD_STRINGS_USER
  111. #undef X
  112. };
  113. #ifndef APP_USBD_STRINGS_MANUFACTURER_EXTERN
  114. #define APP_USBD_STRINGS_MANUFACTURER_EXTERN 0
  115. #endif
  116. #if APP_USBD_STRINGS_MANUFACTURER_EXTERN
  117. extern uint8_t APP_USBD_STRINGS_MANUFACTURER[];
  118. #endif
  119. #ifndef APP_USBD_STRINGS_PRODUCT_EXTERN
  120. #define APP_USBD_STRINGS_PRODUCT_EXTERN 0
  121. #endif
  122. #if APP_USBD_STRINGS_PRODUCT_EXTERN
  123. extern uint8_t APP_USBD_STRINGS_PRODUCT[];
  124. #endif
  125. #ifndef APP_USBD_STRING_SERIAL_EXTERN
  126. #define APP_USBD_STRING_SERIAL_EXTERN 0
  127. #endif
  128. #if APP_USBD_STRING_SERIAL_EXTERN
  129. extern uint8_t APP_USBD_STRING_SERIAL[];
  130. #endif
  131. #ifndef APP_USBD_STRING_CONFIGURATION_EXTERN
  132. #define APP_USBD_STRING_CONFIGURATION_EXTERN 0
  133. #endif
  134. #if APP_USBD_STRING_CONFIGURATION_EXTERN
  135. extern uint8_t APP_USBD_STRING_CONFIGURATION[];
  136. #endif
  137. /**
  138. * @brief String descriptor table.
  139. * */
  140. static uint8_t const * m_string_dsc[APP_USBD_STRING_ID_CNT][ARRAY_SIZE(m_langids)] =
  141. {
  142. [APP_USBD_STRING_ID_LANGIDS_ARRAY_POS] = {APP_USBD_STRING_RAW16_DESC(APP_USBD_STRINGS_LANGIDS)},
  143. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  144. [APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS] = { APP_USBD_STRINGS_MANUFACTURER },
  145. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  146. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  147. [APP_USBD_STRING_ID_PRODUCT_ARRAY_POS] = { APP_USBD_STRINGS_PRODUCT },
  148. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  149. #if (APP_USBD_STRING_ID_SERIAL != 0)
  150. [APP_USBD_STRING_ID_SERIAL_ARRAY_POS] = { APP_USBD_STRING_SERIAL },
  151. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  152. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  153. [APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS] = { APP_USBD_STRINGS_CONFIGURATION },
  154. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  155. #define X(mnemonic, str_idx, ...) [CONCAT_2(mnemonic, _ARRAY_POS)] = {__VA_ARGS__},
  156. APP_USBD_STRINGS_USER
  157. #undef X
  158. };
  159. /**
  160. * @brief Function for preparing UTF16 string descriptor.
  161. *
  162. * @param idx String descriptor ID.
  163. * @param langid Language ID.
  164. *
  165. * @return Pointer to the string descriptor.
  166. */
  167. static uint16_t * app_usbd_prepare_string(uint8_t idx, uint16_t langid)
  168. {
  169. if (m_string_dsc[idx][langid][0] == 0x00)
  170. {
  171. return (uint16_t *) &(m_string_dsc[idx][langid][2]);
  172. }
  173. #if ((APP_USBD_CONFIG_DESC_STRING_SIZE * 2) + 2) <= NRF_DRV_USBD_EPSIZE
  174. uint16_t * string_buffer = app_usbd_core_setup_transfer_buff_get(NULL);
  175. #else
  176. static uint16_t string_buffer[APP_USBD_CONFIG_DESC_STRING_SIZE + 1];
  177. // + 1 element for string descriptor type and size
  178. #endif
  179. uint8_t size = 0;
  180. const uint8_t * p_pos = m_string_dsc[idx][langid];
  181. #if APP_USBD_CONFIG_DESC_STRING_UTF_ENABLED
  182. size = utf8UTF16Count((char *) p_pos, 0);
  183. ASSERT(size <= APP_USBD_CONFIG_DESC_STRING_SIZE);
  184. uint16_t * p_out = &(string_buffer[1]);
  185. uint32_t rune;
  186. while (*p_pos != 0)
  187. {
  188. p_pos = (uint8_t *) utf8DecodeRune((char *) p_pos, 0, &rune);
  189. p_out += utf16EncodeRune(rune, p_out);
  190. }
  191. #else
  192. while(*p_pos != 0)
  193. {
  194. ASSERT(size < APP_USBD_CONFIG_DESC_STRING_SIZE);
  195. ++size;
  196. string_buffer[size] = *p_pos;
  197. ++p_pos;
  198. }
  199. #endif
  200. // Descriptor size is length of the string times 2 bytes per character + 2 bytes for
  201. // descriptor type and size.
  202. string_buffer[0] = (0xff & (size * 2 + 2)) | ((uint16_t)APP_USBD_DESCRIPTOR_STRING) << 8;
  203. return string_buffer;
  204. }
  205. uint16_t const * app_usbd_string_desc_get(uint8_t idx, uint16_t langid)
  206. {
  207. /* LANGID string. */
  208. if (APP_USBD_STRING_ID_LANGIDS == idx)
  209. {
  210. return app_usbd_prepare_string(APP_USBD_STRING_ID_LANGIDS_ARRAY_POS, 0);
  211. }
  212. /* Searching for the language. */
  213. uint8_t lang_idx = 0;
  214. if (ARRAY_SIZE(m_langids) > 1)
  215. {
  216. while (m_langids[lang_idx] != langid)
  217. {
  218. ++lang_idx;
  219. if (lang_idx >= ARRAY_SIZE(m_langids))
  220. {
  221. return NULL;
  222. }
  223. }
  224. }
  225. /* Get the string index in array. */
  226. if (idx >= ARRAY_SIZE(m_string_translation))
  227. {
  228. return NULL;
  229. }
  230. uint8_t str_pos = m_string_translation[idx];
  231. if (str_pos == 0)
  232. {
  233. return NULL;
  234. }
  235. if ((ARRAY_SIZE(m_langids) > 1) && (lang_idx != 0))
  236. {
  237. if (m_string_dsc[str_pos][lang_idx] == NULL)
  238. {
  239. lang_idx = 0;
  240. }
  241. }
  242. if (m_string_dsc[str_pos][lang_idx] == NULL)
  243. {
  244. return NULL;
  245. }
  246. return app_usbd_prepare_string(str_pos, lang_idx);
  247. }
  248. /** @} */
  249. #endif // APP_USBD_ENABLED