app_usbd_string_desc.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef APP_USBD_STRING_DESC_H__
  41. #define APP_USBD_STRING_DESC_H__
  42. #include <stdint.h>
  43. #include "sdk_common.h"
  44. #include "app_usbd.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup app_usbd_string_desc USBD string descriptors
  50. * @ingroup app_usbd
  51. *
  52. * @brief @tagAPI52840 USBD string descriptor management.
  53. * @{
  54. */
  55. /**
  56. * @brief USB Language identifier initialization.
  57. *
  58. * @param[in] lang Language identifier.
  59. */
  60. #define APP_USBD_LANG(lang) \
  61. ((app_usbd_langid_t) lang)
  62. /**
  63. * @brief USB Language identifier with sublanguage initialization.
  64. *
  65. * @param[in] lang Language identifier.
  66. * @param[in] sublang Sublanguage identifier.
  67. */
  68. #define APP_USBD_LANG_AND_SUBLANG(lang, sublang) \
  69. ((app_usbd_langid_t) lang | (app_usbd_langid_t) sublang)
  70. /**
  71. * @brief USB string initialization.
  72. *
  73. * Macro that creates initialization values for the USB string.
  74. * The string must be declared as a NULL-terminated string.
  75. *
  76. * @param[in] str NULL-terminated string.
  77. *
  78. * @return String descriptor initialization data.
  79. */
  80. #define APP_USBD_STRING_DESC(str) (const uint8_t *)(const char[]){str}
  81. /**
  82. * @brief USB raw 8-bit string initialization.
  83. *
  84. * Macro that creates header for raw values passed into descriptor.
  85. * Values must be of the uint8_t type and separated by commas.
  86. *
  87. * @param[in] ... comma-separated values.
  88. *
  89. * @return String descriptor initialization data.
  90. */
  91. #define APP_USBD_STRING_RAW8_DESC(...) (const uint8_t[]){ \
  92. 0x00, 0x00, /* NULL character at start to differentiate from normal string */ \
  93. (0xff & (sizeof((uint8_t[]){__VA_ARGS__}) + 2)), \
  94. (APP_USBD_DESCRIPTOR_STRING), \
  95. __VA_ARGS__ }
  96. /**
  97. * @brief USB raw 16-bit string initialization.
  98. *
  99. * Macro that creates header for raw values passed into descriptor.
  100. * Values must be of the uint16_t type and separated by commas.
  101. *
  102. * @param[in] ... comma-separated values.
  103. *
  104. * @return String descriptor initialization data.
  105. */
  106. #define APP_USBD_STRING_RAW16_DESC(...) (const uint8_t *) ((const uint16_t[]){ \
  107. 0x00, /* NULL character at start to differentiate from normal string */ \
  108. (0xff & (sizeof((uint16_t[]){__VA_ARGS__}) + 2)) | \
  109. ((uint16_t)APP_USBD_DESCRIPTOR_STRING) << 8, \
  110. __VA_ARGS__ })
  111. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  112. #define APP_USBD_STRING_ID_MANUFACTURER_LEN 1
  113. #else
  114. #define APP_USBD_STRING_ID_MANUFACTURER_LEN 0
  115. #endif
  116. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  117. #define APP_USBD_STRING_ID_PRODUCT_LEN 1
  118. #else
  119. #define APP_USBD_STRING_ID_PRODUCT_LEN 0
  120. #endif
  121. #if (APP_USBD_STRING_ID_SERIAL != 0)
  122. #define APP_USBD_STRING_ID_SERIAL_LEN 1
  123. #else
  124. #define APP_USBD_STRING_ID_SERIAL_LEN 0
  125. #endif
  126. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  127. #define APP_USBD_STRING_ID_CONFIGURATION_LEN 1
  128. #else
  129. #define APP_USBD_STRING_ID_CONFIGURATION_LEN 0
  130. #endif
  131. /** @brief Total number of USB strings */
  132. #define APP_USBD_STRINGS_NUM \
  133. ((APP_USBD_STRINGS_USER 0) + 1 + APP_USBD_STRING_ID_MANUFACTURER_LEN + APP_USBD_STRING_ID_PRODUCT_LEN + APP_USBD_STRING_ID_SERIAL_LEN + APP_USBD_STRING_ID_CONFIGURATION_LEN)
  134. /**
  135. * @brief USB string descriptors IDs
  136. */
  137. typedef enum {
  138. APP_USBD_STRING_ID_LANGIDS = 0, /**< Supported language identifiers */
  139. /// Placeholders used only for alignement of user strings. Do not use or modify them.
  140. #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
  141. APP_USBD_STRING_ID_MANUFACTURER_PLACEHOLDER = APP_USBD_STRING_ID_MANUFACTURER,
  142. #endif // (APP_USBD_STRING_ID_MANUFACTURER != 0)
  143. #if (APP_USBD_STRING_ID_PRODUCT != 0)
  144. APP_USBD_STRING_ID_PRODUCT_PLACEHOLDER = APP_USBD_STRING_ID_PRODUCT,
  145. #endif // (APP_USBD_STRING_ID_PRODUCT != 0)
  146. #if (APP_USBD_STRING_ID_SERIAL != 0)
  147. APP_USBD_STRING_ID_SERIAL_PLACEHOLDER = APP_USBD_STRING_ID_SERIAL,
  148. #endif // (APP_USBD_STRING_ID_SERIAL != 0)
  149. #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
  150. APP_USBD_STRING_ID_CONFIGURATION_PLACEHOLDER = APP_USBD_STRING_ID_CONFIGURATION,
  151. #endif // (APP_USBD_STRING_ID_CONFIGURATION != 0)
  152. #define X(mnemonic, str_idx, ...) mnemonic str_idx,
  153. APP_USBD_STRINGS_USER
  154. #undef X
  155. } app_usbd_string_desc_idx_t;
  156. /** @brief String ID conversion struct */
  157. typedef struct {
  158. uint8_t const identifier;
  159. uint8_t const array_pos;
  160. } app_usbd_strings_convert_t;
  161. /**
  162. * @brief Get string descriptor.
  163. *
  164. * @param[in] idx String descriptor index.
  165. * @param[in] langid Selected language for the string.
  166. * @return String descriptor, or NULL if it does not exist.
  167. * */
  168. uint16_t const * app_usbd_string_desc_get(uint8_t idx, uint16_t langid);
  169. /**
  170. * @brief Get string length.
  171. *
  172. * Function for getting string length from descriptor (descriptor returned by @ref app_usbd_string_desc_get).
  173. *
  174. * @param[in] p_str String descriptor pointer.
  175. * @return Total descriptor length in bytes.
  176. */
  177. static inline size_t app_usbd_string_desc_length(uint16_t const * p_str)
  178. {
  179. return ((const app_usbd_descriptor_string_t *)p_str)->bLength;
  180. }
  181. /** @} */
  182. #ifdef __cplusplus
  183. }
  184. #endif
  185. #endif /* APP_USBD_STRING_DESC_H__ */