advertisement.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/12/31 Bernard Add license info
  9. */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. static uint8_t ad_data[] = { 0x01, 0x08, 0x20, 0x20, 0x14, 0x02, 0x01, 0x06, 0x0d,
  16. 0x09, 0x62, 0x6c, 0x65, 0x68, 0x72, 0x5f, 0x73, 0x65, 0x6e,
  17. 0x73, 0x6f, 0x72, 0x02, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  19. static uint8_t ad_params[] = { 0x01, 0x06, 0x20, 0x0f, 0x30, 0x00, 0x60, 0x00, 0x00,
  20. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00 };
  21. static uint8_t ad_enable[] = { 0x01, 0x0a, 0x20, 0x01, 0x01 };
  22. static void print_command(uint8_t *buf, uint16_t len)
  23. {
  24. rt_kprintf("CMD => ");
  25. for (uint16_t i = 0; i < len; i++) {
  26. rt_kprintf("%02X ", buf[i]);
  27. }
  28. rt_kprintf("\n");
  29. }
  30. static void print_event(uint8_t *buf, uint16_t len)
  31. {
  32. rt_kprintf("EVT <= ");
  33. for (uint16_t i = 0; i < len; i++) {
  34. rt_kprintf("%02X ", buf[i]);
  35. }
  36. rt_kprintf("\n");
  37. }
  38. static int start_advertisement(int argc, char *argv[])
  39. {
  40. uint8_t recv[50];
  41. uint16_t recv_len;
  42. rt_device_t ble = rt_device_find("uart1");
  43. rt_device_open(ble, RT_DEVICE_FLAG_INT_RX);;
  44. // Set advertisement data
  45. print_command(ad_data, sizeof(ad_data));
  46. rt_device_write(ble, 0, ad_data, sizeof(ad_data));
  47. rt_thread_mdelay(10);
  48. recv_len = rt_device_read(ble, 0, recv, sizeof(recv));
  49. print_event(recv, recv_len);
  50. // Set advertisement parameters
  51. print_command(ad_params, sizeof(ad_params));
  52. rt_device_write(ble, 0, ad_params, sizeof(ad_params));
  53. rt_thread_mdelay(10);
  54. recv_len = rt_device_read(ble, 0, recv, sizeof(recv));
  55. print_event(recv, recv_len);
  56. // Enable advertisement
  57. print_command(ad_enable, sizeof(ad_enable));
  58. rt_device_write(ble, 0, ad_enable, sizeof(ad_enable));
  59. rt_thread_mdelay(10);
  60. recv_len = rt_device_read(ble, 0, recv, sizeof(recv));
  61. print_event(recv, recv_len);
  62. return 0;
  63. }
  64. MSH_CMD_EXPORT(start_advertisement, "HM sample");