ble_fota_service.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "include.h"
  2. #include "fota_proc.h"
  3. #if (AB_FOT_CHANNEL & AB_FOT_CHANNEL_BLE)
  4. #define TRACE_EN 1
  5. #if TRACE_EN
  6. #define TRACE(...) my_printf("[BLE] ");\
  7. my_printf(__VA_ARGS__)
  8. #define TRACE_R(...) my_print_r(__VA_ARGS__);
  9. #else
  10. #define TRACE(...)
  11. #define TRACE_R(...)
  12. #endif // TRACE_EN
  13. static att_service_handler_t fota_service;
  14. static uint16_t fota_client_config;
  15. static void fota_event_packet_handle(uint8_t event_type, uint8_t *param, uint16_t size)
  16. {
  17. switch(event_type){
  18. case BLE_EVT_CONNECT:{
  19. memcpy(&ble_cb.con_hanle, &param[7], 2);
  20. fota_connected_handle(FOTA_CHL_BLE);
  21. } break;
  22. case BLE_EVT_DISCONNECT:{
  23. ble_cb.con_hanle = 0;
  24. fota_client_config = CCCD_DFT;
  25. fota_disconnected_handle(FOTA_CHL_BLE);
  26. } break;
  27. default:
  28. break;
  29. }
  30. }
  31. static uint16_t fota_read_callback(uint16_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size)
  32. {
  33. if(attribute_handle == ATT_CHARACTERISTIC_FF15_01_VALUE_HANDLE){
  34. u8 read_data[] = "FOTA";
  35. if(buffer){
  36. memcpy(buffer, read_data, sizeof(read_data));
  37. }
  38. return sizeof(read_data);
  39. }
  40. return 0;
  41. }
  42. static int fota_write_callback(uint16_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size)
  43. {
  44. if(attribute_handle == ATT_CHARACTERISTIC_FF14_01_CLIENT_CONFIGURATION_HANDLE){
  45. fota_client_config = GET_LE16(&buffer[0]);
  46. }else if(attribute_handle == ATT_CHARACTERISTIC_FF15_01_VALUE_HANDLE){
  47. fota_recv_packet_handle(buffer, buffer_size, FOTA_CHL_BLE);
  48. }
  49. return ATT_ERR_NO_ERR;
  50. }
  51. AT(.text.fot.cache)
  52. static int fota_service_notify(uint8_t *buf, uint16_t size)
  53. {
  54. int res = 0;
  55. if (fota_client_config) {
  56. TRACE("fot tx:");
  57. TRACE_R(buf, size);
  58. res = ble_notify_for_handle(ble_cb.con_hanle, ATT_CHARACTERISTIC_FF14_01_VALUE_HANDLE, buf, size);
  59. }
  60. return res;
  61. }
  62. static int fota_service_mtu_get(uint32_t *mtu)
  63. {
  64. int res;
  65. res = att_server_get_mtu(ble_cb.con_hanle, (u16 *)mtu);
  66. *mtu -= 3;
  67. return res;
  68. }
  69. void ble_fota_service_init(void)
  70. {
  71. printf("ble_fota_service_init\n");
  72. // get service handle range
  73. uint16_t start_handle = ATT_SERVICE_FF12_START_HANDLE;
  74. uint16_t end_handle = ATT_SERVICE_FF12_END_HANDLE;
  75. ab_fota_cb_t param;
  76. fota_client_config = CCCD_DFT;
  77. // register service with ATT Server
  78. fota_service.start_handle = start_handle;
  79. fota_service.end_handle = end_handle;
  80. fota_service.read_callback = &fota_read_callback;
  81. fota_service.write_callback = &fota_write_callback;
  82. fota_service.event_handler = &fota_event_packet_handle;
  83. att_server_register_service_handler(&fota_service);
  84. param.fota_send_packet_handle = fota_service_notify;
  85. param.fota_mtu_check_handle = fota_service_mtu_get;
  86. fota_init(&param, FOTA_CHL_BLE);
  87. }
  88. #endif