usbcfg_common.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * This file is free software: you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This file is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. * See the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. * Modified for use in AP_HAL by Andrew Tridgell and Siddharth Bharat Purohit
  28. */
  29. #include "hal.h"
  30. #include "hwdef.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "usbcfg.h"
  34. #if defined(HAL_USB_PRODUCT_ID)
  35. /*
  36. check if one string contains another
  37. */
  38. bool string_contains(const char *haystack, const char *needle)
  39. {
  40. uint8_t needle_len = strlen(needle);
  41. while (*haystack) {
  42. if (strncmp(haystack, needle, needle_len) == 0) {
  43. return true;
  44. }
  45. haystack++;
  46. }
  47. return false;
  48. }
  49. /*
  50. handle substitution of variables in strings for USB descriptors
  51. */
  52. void string_substitute(const char *str, char *str2)
  53. {
  54. const char *board = "%BOARD%";
  55. const char *serial = "%SERIAL%";
  56. uint8_t new_len = strlen(str);
  57. if (string_contains(str, board)) {
  58. new_len += strlen(HAL_BOARD_NAME) - strlen(board);
  59. }
  60. if (string_contains(str, serial)) {
  61. new_len += 24 - strlen(serial);
  62. }
  63. if (new_len+1 > USB_DESC_MAX_STRLEN) {
  64. strcpy(str2, str);
  65. return;
  66. }
  67. char *p = str2;
  68. while (*str) {
  69. char c = *str;
  70. if (c == '%') {
  71. if (strncmp(str, board, strlen(board)) == 0) {
  72. memcpy(p, HAL_BOARD_NAME, strlen(HAL_BOARD_NAME));
  73. str += 7;
  74. p += strlen(HAL_BOARD_NAME);
  75. continue;
  76. }
  77. if (strncmp(str, serial, strlen(serial)) == 0) {
  78. const char *hex = "0123456789ABCDEF";
  79. const uint8_t *cpu_id = (const uint8_t *)UDID_START;
  80. uint8_t i;
  81. for (i=0; i<12; i++) {
  82. *p++ = hex[(cpu_id[i]>>4)&0xF];
  83. *p++ = hex[cpu_id[i]&0xF];
  84. }
  85. str += 8;
  86. continue;
  87. }
  88. }
  89. *p++ = *str++;
  90. }
  91. *p = 0;
  92. }
  93. #endif