ser_conn_event_encoder.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include "app_error.h"
  15. #include "app_scheduler.h"
  16. #include "ble_conn.h"
  17. #include "ble_serialization.h"
  18. #include "ser_config.h"
  19. #include "ser_hal_transport.h"
  20. #include "ser_conn_event_encoder.h"
  21. void ser_conn_ble_event_encoder(void * p_event_data, uint16_t event_size)
  22. {
  23. if (NULL == p_event_data)
  24. {
  25. APP_ERROR_CHECK(NRF_ERROR_NULL);
  26. }
  27. UNUSED_PARAMETER(event_size);
  28. uint32_t err_code = NRF_SUCCESS;
  29. uint8_t * p_tx_buf = NULL;
  30. uint32_t tx_buf_len = 0;
  31. ble_evt_t * p_ble_evt = (ble_evt_t *)p_event_data;
  32. /* Allocate a memory buffer from HAL Transport layer for transmitting an event.
  33. * Loop until a buffer is available. */
  34. do
  35. {
  36. err_code = ser_hal_transport_tx_pkt_alloc(&p_tx_buf, (uint16_t *)&tx_buf_len);
  37. }
  38. while (err_code == NRF_ERROR_NO_MEM);
  39. APP_ERROR_CHECK(err_code);
  40. /* Create a new packet. */
  41. p_tx_buf[SER_PKT_TYPE_POS] = SER_PKT_TYPE_EVT;
  42. tx_buf_len -= SER_PKT_TYPE_SIZE;
  43. /* Pass a memory for an event (opcode + data) and encode it. */
  44. err_code = ble_event_enc(p_ble_evt, 0, &p_tx_buf[SER_PKT_OP_CODE_POS], &tx_buf_len);
  45. if (NRF_ERROR_NOT_SUPPORTED != err_code)
  46. {
  47. APP_ERROR_CHECK(err_code);
  48. tx_buf_len += SER_PKT_TYPE_SIZE;
  49. err_code = ser_hal_transport_tx_pkt_send(p_tx_buf, (uint16_t)tx_buf_len);
  50. APP_ERROR_CHECK(err_code);
  51. /* TX buffer is going to be freed automatically in the HAL Transport layer.
  52. * Scheduler must be paused because this function returns before a packet is physically sent
  53. * by transport layer. This can cause start processing of a next event from the application
  54. * scheduler queue. In result the next event reserves the TX buffer before the current
  55. * packet is sent. If in meantime a command arrives a command response cannot be sent in
  56. * result. Pausing the scheduler temporary prevents processing a next event. */
  57. app_sched_pause();
  58. }
  59. else
  60. {
  61. /* Event was NOT encoded, therefore the buffer is freed immediately. */
  62. err_code = ser_hal_transport_tx_pkt_free(p_tx_buf);
  63. APP_ERROR_CHECK(err_code);
  64. APP_ERROR_CHECK(SER_WARNING_CODE);
  65. }
  66. }