forked from ANRGUSC/iris-mbed-os-3pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_pkt.cpp
138 lines (127 loc) · 4.5 KB
/
uart_pkt.cpp
1
2
3
4
5
6
7
8
9
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
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
63
64
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright (c) 2017, Autonomous Networks Research Group. All rights reserved.
* Developed by:
* Autonomous Networks Research Group (ANRG)
* University of Southern California
* http://anrg.usc.edu/
*
* Contributors:
* Jason A. Tran
* Pradipta Ghosh
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* - Neither the names of Autonomous Networks Research Group, nor University of
* Southern California, nor the names of its contributors may be used to
* endorse or promote products derived from this Software without specific
* prior written permission.
* - A citation to the Autonomous Networks Research Group must be included in
* any publications benefiting from the use of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
* THE SOFTWARE.
*/
/**
* @file uart_pkt.c
* @brief Helper library for creating packets to be over UART via hdlc.
*
* @author Jason A. Tran <[email protected]>
* @author Pradipta Ghosh <[email protected]>
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "uart_pkt.h"
#define DEBUG 0
#if (DEBUG)
#define DEBUG(...) pc.printf(__VA_ARGS__)
extern Serial pc;
#else
#define DEBUG(...)
#endif /* (DEBUG) */
/**
* @brief Inserts the oacket header in the packetbuffer
*
* @param buf The packet buffer
* @param[in] buf_len The available buffer length
* @param[in] hdr The packet header
*
* @return pointer to the data section of the packet
*/
void *uart_pkt_insert_hdr(void *buf, size_t buf_len, const uart_pkt_hdr_t *hdr)
{
if (buf_len < UART_PKT_HDR_LEN) {
DEBUG("Buffer size too small\n");
return NULL;
}
memcpy(buf, hdr, sizeof(uart_pkt_hdr_t));
return (buf + UART_PKT_DATA_FIELD);
}
/**
* @brief Copy data from an array into a uart packet buffer.
*
* @param buf destination buffer
* @param buf_len destination buffer size
* @param data buffer containing data
* @param data_len size of buffer containing data
*
* @return total size of packet on success or 0 on failure.
*/
size_t uart_pkt_cpy_data(void *buf, size_t buf_len, const void *data,
size_t data_len)
{
if (data_len + UART_PKT_HDR_LEN > buf_len) {
DEBUG("Not enough space in destination buffer\n");
return 0;
}
memcpy(buf + UART_PKT_HDR_LEN, data, data_len);
return (UART_PKT_HDR_LEN + data_len);
}
/**
* @brief parse the received packet for validity and returns the header
*
* @param dst_hdr The destination header
* @param[in] src The received packet
* @param[in] src_len The received packet length
*
* @return Status
*/
int uart_pkt_parse_hdr(uart_pkt_hdr_t *dst_hdr, const void *src, size_t src_len)
{
if (src_len < UART_PKT_HDR_LEN) {
DEBUG("Invalid source buffer size.\n");
return -1;
}
memcpy(dst_hdr, src, UART_PKT_HDR_LEN);
return 0;
}
/**
* @brief returns pointer to the data section of the received packet
*
* @param src received packet
* @param[in] src_len received packet length/size
*
* @return description_of_the_return_value
*/
void *uart_pkt_get_data(void *src, size_t src_len)
{
if (src_len < UART_PKT_HDR_LEN) {
DEBUG("Invalid source buffer size.\n");
return NULL;
}
return (src + UART_PKT_HDR_LEN);
}