ser_conn_cmd_decoder.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <stdint.h>
  13. #include <string.h>
  14. #include "nordic_common.h"
  15. #include "app_error.h"
  16. #include "ble_serialization.h"
  17. #include "ser_config.h"
  18. #include "conn_mw.h"
  19. #include "ser_hal_transport.h"
  20. #include "ser_conn_cmd_decoder.h"
  21. uint32_t ser_conn_command_process(uint8_t * p_command, uint16_t command_len)
  22. {
  23. SER_ASSERT_NOT_NULL(p_command);
  24. SER_ASSERT_LENGTH_LEQ(SER_OP_CODE_SIZE, command_len);
  25. uint32_t err_code = NRF_SUCCESS;
  26. uint8_t * p_tx_buf = NULL;
  27. uint32_t tx_buf_len = 0;
  28. uint8_t opcode = p_command[SER_CMD_OP_CODE_POS];
  29. uint32_t index = 0;
  30. /* Allocate a memory buffer from HAL Transport layer for transmitting the Command Response.
  31. * Loop until a buffer is available. */
  32. do
  33. {
  34. err_code = ser_hal_transport_tx_pkt_alloc(&p_tx_buf, (uint16_t *)&tx_buf_len);
  35. }
  36. while (NRF_ERROR_NO_MEM == err_code);
  37. if (NRF_SUCCESS == err_code)
  38. {
  39. /* Create a new response packet. */
  40. p_tx_buf[SER_PKT_TYPE_POS] = SER_PKT_TYPE_RESP;
  41. tx_buf_len -= SER_PKT_TYPE_SIZE;
  42. /* Decode a request, pass a memory for a response command (opcode + data) and encode it. */
  43. err_code = conn_mw_handler
  44. (p_command, command_len, &p_tx_buf[SER_PKT_OP_CODE_POS], &tx_buf_len);
  45. /* Command decoder not found. */
  46. if (NRF_ERROR_NOT_SUPPORTED == err_code)
  47. {
  48. APP_ERROR_CHECK(SER_WARNING_CODE);
  49. err_code = op_status_enc
  50. (opcode, NRF_ERROR_NOT_SUPPORTED,
  51. &p_tx_buf[SER_PKT_OP_CODE_POS], &tx_buf_len, &index);
  52. if (NRF_SUCCESS == err_code)
  53. {
  54. tx_buf_len += SER_PKT_TYPE_SIZE;
  55. err_code = ser_hal_transport_tx_pkt_send(p_tx_buf, (uint16_t)tx_buf_len);
  56. /* TX buffer is going to be freed automatically in the HAL Transport layer. */
  57. if (NRF_SUCCESS != err_code)
  58. {
  59. err_code = NRF_ERROR_INTERNAL;
  60. }
  61. }
  62. else
  63. {
  64. err_code = NRF_ERROR_INTERNAL;
  65. }
  66. }
  67. else if (NRF_SUCCESS == err_code) /* Send a response. */
  68. {
  69. tx_buf_len += SER_PKT_TYPE_SIZE;
  70. err_code = ser_hal_transport_tx_pkt_send(p_tx_buf, (uint16_t)tx_buf_len);
  71. /* TX buffer is going to be freed automatically in the HAL Transport layer. */
  72. if (NRF_SUCCESS != err_code)
  73. {
  74. err_code = NRF_ERROR_INTERNAL;
  75. }
  76. }
  77. else
  78. {
  79. err_code = NRF_ERROR_INTERNAL;
  80. }
  81. }
  82. else
  83. {
  84. err_code = NRF_ERROR_INTERNAL;
  85. }
  86. return err_code;
  87. }