123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #include "sdk_config.h"
- #if APP_USBD_ENABLED
- #include "app_usbd_string_desc.h"
- #include "app_usbd_langid.h"
- #include "app_usbd_core.h"
- #include "nordic_common.h"
- #include "utf.h"
- static uint16_t const m_langids[] = { APP_USBD_STRINGS_LANGIDS };
- enum {
- APP_USBD_STRING_ID_LANGIDS_ARRAY_POS = 0,
- #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
- APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS,
- #endif
- #if (APP_USBD_STRING_ID_PRODUCT != 0)
- APP_USBD_STRING_ID_PRODUCT_ARRAY_POS,
- #endif
- #if (APP_USBD_STRING_ID_SERIAL != 0)
- APP_USBD_STRING_ID_SERIAL_ARRAY_POS,
- #endif
- #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
- APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS,
- #endif
- #define X(mnemonic, str_idx, ...) CONCAT_2(mnemonic, _ARRAY_POS),
- APP_USBD_STRINGS_USER
- #undef X
- };
- #define X(mnemonic, str_idx, ...) 1 +
- static app_usbd_strings_convert_t const m_string_translation[APP_USBD_STRINGS_NUM] =
- #undef X
- {
- {
- .identifier = APP_USBD_STRING_ID_LANGIDS,
- .array_pos = APP_USBD_STRING_ID_LANGIDS_ARRAY_POS,
- },
- #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
- {
- .identifier = APP_USBD_STRING_ID_MANUFACTURER,
- .array_pos = APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS,
- },
- #endif
- #if (APP_USBD_STRING_ID_PRODUCT != 0)
- {
- .identifier = APP_USBD_STRING_ID_PRODUCT,
- .array_pos = APP_USBD_STRING_ID_PRODUCT_ARRAY_POS,
- },
- #endif
- #if (APP_USBD_STRING_ID_SERIAL != 0)
- {
- .identifier = APP_USBD_STRING_ID_SERIAL,
- .array_pos = APP_USBD_STRING_ID_SERIAL_ARRAY_POS,
- },
- #endif
- #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
- {
- .identifier = APP_USBD_STRING_ID_CONFIGURATION,
- .array_pos = APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS,
- },
- #endif
- #define X(mnemonic, ...) \
- { \
- .identifier = mnemonic, \
- .array_pos = CONCAT_2(mnemonic, _ARRAY_POS), \
- },
- APP_USBD_STRINGS_USER
- #undef X
- };
- #ifndef APP_USBD_STRINGS_MANUFACTURER_EXTERN
- #define APP_USBD_STRINGS_MANUFACTURER_EXTERN 0
- #endif
- #if APP_USBD_STRINGS_MANUFACTURER_EXTERN
- extern uint8_t APP_USBD_STRINGS_MANUFACTURER[];
- #endif
- #ifndef APP_USBD_STRINGS_PRODUCT_EXTERN
- #define APP_USBD_STRINGS_PRODUCT_EXTERN 0
- #endif
- #if APP_USBD_STRINGS_PRODUCT_EXTERN
- extern uint8_t APP_USBD_STRINGS_PRODUCT[];
- #endif
- #ifndef APP_USBD_STRING_SERIAL_EXTERN
- #define APP_USBD_STRING_SERIAL_EXTERN 0
- #endif
- #if APP_USBD_STRING_SERIAL_EXTERN
- extern uint8_t APP_USBD_STRING_SERIAL[];
- #endif
- #ifndef APP_USBD_STRING_CONFIGURATION_EXTERN
- #define APP_USBD_STRING_CONFIGURATION_EXTERN 0
- #endif
- #if APP_USBD_STRING_CONFIGURATION_EXTERN
- extern uint8_t APP_USBD_STRING_CONFIGURATION[];
- #endif
- #define X(mnemonic, str_idx, ...) 1 +
- static uint8_t const * m_string_dsc[APP_USBD_STRINGS_NUM][ARRAY_SIZE(m_langids)] =
- #undef X
- {
- [APP_USBD_STRING_ID_LANGIDS_ARRAY_POS] = {APP_USBD_STRING_RAW16_DESC(APP_USBD_STRINGS_LANGIDS)},
- #if (APP_USBD_STRING_ID_MANUFACTURER != 0)
- [APP_USBD_STRING_ID_MANUFACTURER_ARRAY_POS] = { APP_USBD_STRINGS_MANUFACTURER },
- #endif
- #if (APP_USBD_STRING_ID_PRODUCT != 0)
- [APP_USBD_STRING_ID_PRODUCT_ARRAY_POS] = { APP_USBD_STRINGS_PRODUCT },
- #endif
- #if (APP_USBD_STRING_ID_SERIAL != 0)
- [APP_USBD_STRING_ID_SERIAL_ARRAY_POS] = { APP_USBD_STRING_SERIAL },
- #endif
- #if (APP_USBD_STRING_ID_CONFIGURATION != 0)
- [APP_USBD_STRING_ID_CONFIGURATION_ARRAY_POS] = { APP_USBD_STRINGS_CONFIGURATION },
- #endif
- #define X(mnemonic, str_idx, ...) [CONCAT_2(mnemonic, _ARRAY_POS)] = {__VA_ARGS__},
- APP_USBD_STRINGS_USER
- #undef X
- };
- static uint16_t * app_usbd_prepare_string(uint8_t idx, uint16_t langid)
- {
- if (m_string_dsc[idx][langid][0] == 0x00)
- {
- return (uint16_t *) &(m_string_dsc[idx][langid][2]);
- }
- #if ((APP_USBD_CONFIG_DESC_STRING_SIZE * 2) + 2) <= NRF_DRV_USBD_EPSIZE
- uint16_t * string_buffer = app_usbd_core_setup_transfer_buff_get(NULL);
- #else
- static uint16_t string_buffer[APP_USBD_CONFIG_DESC_STRING_SIZE + 1];
-
- #endif
- uint8_t size = 0;
- const uint8_t * p_pos = m_string_dsc[idx][langid];
- #if APP_USBD_CONFIG_DESC_STRING_UTF_ENABLED
- size = utf8UTF16Count((char *) p_pos, 0);
- ASSERT(size <= APP_USBD_CONFIG_DESC_STRING_SIZE);
- uint16_t * p_out = &(string_buffer[1]);
- uint32_t rune;
- while (*p_pos != 0)
- {
- p_pos = (uint8_t *) utf8DecodeRune((char *) p_pos, 0, &rune);
- p_out += utf16EncodeRune(rune, p_out);
- }
- #else
- while(*p_pos != 0)
- {
- ASSERT(size < APP_USBD_CONFIG_DESC_STRING_SIZE);
- ++size;
- string_buffer[size] = *p_pos;
- ++p_pos;
- }
- #endif
-
-
- string_buffer[0] = (0xff & (size * 2 + 2)) | ((uint16_t)APP_USBD_DESCRIPTOR_STRING) << 8;
- return string_buffer;
- }
- uint16_t const * app_usbd_string_desc_get(uint8_t idx, uint16_t langid)
- {
-
- if (APP_USBD_STRING_ID_LANGIDS == idx)
- {
- return app_usbd_prepare_string(APP_USBD_STRING_ID_LANGIDS_ARRAY_POS, 0);
- }
-
- uint8_t lang_idx = 0;
- if (ARRAY_SIZE(m_langids) > 1)
- {
- while (m_langids[lang_idx] != langid)
- {
- ++lang_idx;
- if (lang_idx >= ARRAY_SIZE(m_langids))
- {
- return NULL;
- }
- }
- }
- uint8_t str_pos = 0;
- for (uint8_t i = 0; i < ARRAY_SIZE(m_string_translation); i++)
- {
- if (m_string_translation[i].identifier == idx)
- {
- str_pos = m_string_translation[i].array_pos;
- break;
- }
- }
- if (str_pos == 0)
- {
- return NULL;
- }
- if ((ARRAY_SIZE(m_langids) > 1) && (lang_idx != 0))
- {
- if (m_string_dsc[str_pos][lang_idx] == NULL)
- {
- lang_idx = 0;
- }
- }
- if (m_string_dsc[str_pos][lang_idx] == NULL)
- {
- return NULL;
- }
- return app_usbd_prepare_string(str_pos, lang_idx);
- }
- #endif
|