ble_common.cpp 4.03 KB
Newer Older
1
2
3
/*
 * Copyright (c) 2016 Nordic Semiconductor ASA
 * All rights reserved.
4
 *
5
6
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
7
 *
8
9
 *   1. Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
10
 *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 *   2. Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
 *   contributors to this software may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 *   4. This software must only be used in or with a processor manufactured by Nordic
 *   Semiconductor ASA, or in or with a processor manufactured by a third party that
 *   is used in combination with a processor manufactured by Nordic Semiconductor.
 *
 *   5. Any software provided in binary or object form under this license must not be
 *   reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 */

 #include "ble_common.h"

#include <memory>
#include <sstream>

#include "adapter_internal.h"
#include "nrf_error.h"
#include "ser_config.h"

uint32_t encode_decode(adapter_t *adapter, encode_function_t encode_function, decode_function_t decode_function)
{
    uint32_t tx_buffer_length = SER_HAL_TRANSPORT_MAX_PKT_SIZE;
    uint32_t rx_buffer_length = 0;

    std::unique_ptr<uint8_t> tx_buffer(static_cast<uint8_t*>(std::malloc(SER_HAL_TRANSPORT_MAX_PKT_SIZE)));
    std::unique_ptr<uint8_t> rx_buffer(static_cast<uint8_t*>(std::malloc(SER_HAL_TRANSPORT_MAX_PKT_SIZE)));

    std::stringstream error_message;

    auto _adapter = static_cast<AdapterInternal*>(adapter->internal);

    uint32_t err_code = encode_function(tx_buffer.get(), &tx_buffer_length);

    if (_adapter->isInternalError(err_code))
    {
63
64
        error_message << "Not able to encode packet. Code #" << err_code;
        _adapter->statusHandler(PKT_ENCODE_ERROR, error_message.str().c_str());
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        return NRF_ERROR_INTERNAL;
    }

    if (decode_function != nullptr)
    {
        err_code = _adapter->transport->send(
            tx_buffer.get(),
            tx_buffer_length,
            rx_buffer.get(),
            &rx_buffer_length);
    }
    else
    {
        err_code = _adapter->transport->send(
            tx_buffer.get(),
            tx_buffer_length,
            nullptr,
            &rx_buffer_length);
    }

    if (_adapter->isInternalError(err_code))
    {
        error_message << "Error sending packet to target. Code #" << err_code;
        _adapter->statusHandler(PKT_SEND_ERROR, error_message.str().c_str());
        return NRF_ERROR_INTERNAL;
    }

    uint32_t result_code = NRF_SUCCESS;

    if (decode_function != nullptr)
    {
        err_code = decode_function(rx_buffer.get(), rx_buffer_length, &result_code);
    }

    if (_adapter->isInternalError(err_code))
    {
        error_message << "Not able to decode packet. Code #" << err_code;
        _adapter->statusHandler(PKT_DECODE_ERROR, error_message.str().c_str());
        return NRF_ERROR_INTERNAL;
    }

    return result_code;
}