ble_sn2model.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "ble_sn2model.h"
  2. #include "ble.h"
  3. #include <ctype.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. DEV_MODEL model_tbl[] = SN2MODEL_MAP_TBL;
  8. void hex_to_str(const uint8_t *src, uint8_t *dest, int32_t src_len)
  9. {
  10. int32_t i;
  11. uint8_t tmp[3];
  12. for (i = 0; i < src_len; i++)
  13. {
  14. sprintf(tmp, "%02X", src[i]);
  15. memcpy(&dest[i * 2], tmp, 2);
  16. }
  17. return;
  18. }
  19. void str_to_hex(const uint8_t *src, uint8_t *dest, int32_t src_len)
  20. {
  21. int32_t i;
  22. uint8_t high_byte, low_byte;
  23. for (i = 0; i < src_len; i += 2)
  24. {
  25. high_byte = toupper(src[i]);
  26. low_byte = tolower(src[i + 1]);
  27. if (high_byte > 0x39)
  28. {
  29. high_byte -= 0x37;
  30. }
  31. else
  32. {
  33. high_byte -= 0x30;
  34. }
  35. if (low_byte > 0x39)
  36. {
  37. low_byte -= 0x37;
  38. }
  39. else
  40. {
  41. low_byte -= 0x30;
  42. }
  43. dest[i / 2] = (high_byte << 4) | low_byte;
  44. }
  45. return;
  46. }
  47. int32_t str_sn_2_model(uint8_t *str_sn, uint8_t *model)
  48. {
  49. for (int32_t i = 0; i < sizeof(model_tbl) / sizeof(DEV_MODEL); i++)
  50. {
  51. if (strncmp(model_tbl[i].prefix, &str_sn[4], MODEL_LEN) == 0)
  52. {
  53. strcpy(model, model_tbl[i].model);
  54. return 0;
  55. }
  56. }
  57. return 1;
  58. }
  59. int32_t hex_sn_2_model(uint8_t *hex_sn, uint8_t *model)
  60. {
  61. uint8_t str_sn[SN_LEN * 2 + 1] = {0};
  62. hex_to_str(hex_sn, str_sn, SN_LEN);
  63. return str_sn_2_model(str_sn, model);
  64. }
  65. int32_t sn_2_model(uint8_t *sn, int32_t sn_type, uint8_t *model)
  66. {
  67. if (sn_type == 0)
  68. {
  69. return str_sn_2_model(sn, model);
  70. }
  71. else
  72. {
  73. return hex_sn_2_model(sn, model);
  74. }
  75. }