ISSDK  1.7
IoT Sensing Software Development Kit
data_format_hdlc.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36 * @file data_format_hdlc.c
37 * @brief The data_format_hdlc.c file contains definitions for
38  encoding and decoding HDLC messages to and from Host.
39 */
40 
41 /*******************************************************************************
42  * ISSDK Includes
43  ******************************************************************************/
44 #include "data_format_hdlc.h"
45 
46 /*******************************************************************************
47  * Functions
48  ******************************************************************************/
49 /* Function to handle incomming HDLC encoded bytes form the Host over UART. */
50 bool HDLC_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
51 {
52  static uint8_t c_prev = 0;
53  bool bRxPacketerror = false, bPacketReceived = false;
54 
55  switch (pHostRxPkt->rxState) /* Process the character. */
56  {
58  if (c == 0x7E)
59  { /* Wait for the remaining packet. */
60  pHostRxPkt->mIndex = 0;
62  }
63  break;
65  if (c != 0x7E)
66  { /* Got some real data (non-marker char).
67  * Do escape decode if needed. */
68  if (c == 0x7D)
69  {
70  c_prev = c;
71  break;
72  }
73  else if (c_prev == 0x7D)
74  {
75  if (c == 0x5D)
76  {
77  c = 0x7D;
78  }
79  else if (c == 0x5E)
80  {
81  c = 0x7E;
82  }
83  else
84  { /* Illegal escape sequence. Reset and wait for next packet. */
85  bRxPacketerror = true;
86  }
87  c_prev = 0;
88  }
89  /* Escape decode done. Just got data byte, save it. */
90  pHostRxPkt->pRxbuf[pHostRxPkt->mIndex] = c;
91  ++(pHostRxPkt->mIndex);
92  }
93  else
94  {
95  /* Got packet marker while waiting for data. */
96  if (pHostRxPkt->mIndex > 0)
97  { /* Got a complete packet.
98  * Set state wait for next packet. */
100  /* Set flag to process packet just received. */
101  bPacketReceived = true;
102  }
103  else
104  {
105  /* We got back to back packet marker. This 2nd packet marker will
106  * then be treated as a start marker. Go back to wait for data. */
108  pHostRxPkt->mIndex = 0;
109  }
110  }
111  break;
112  default:
113  /* Unknown state. */
114  bRxPacketerror = true;
115  break;
116  }
117 
118  /* Handle packet error. */
119  if (bRxPacketerror == true)
120  {
121  pHostRxPkt->mIndex = 0;
123  }
124 
125  return bPacketReceived;
126 }
127 
128 /* Function to format data for HDLC and send bytes to Host over UART. */
129 size_t HDLC_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
130 {
131  size_t index = 0;
132 
133  if (pBuffer == NULL || pMsg == NULL || size == 0)
134  {
135  return 0;
136  }
137 
138  /* Construct the Host Message. */
139  pMsg[index++] = 0x7E; /* Add the Start marker. */
140  for (size_t offset = 0; offset < size; offset++)
141  {
142  switch (pBuffer[offset])
143  {
144  case 0x7D: /* Add escape sequence for escape character. */
145  pMsg[index++] = 0x7D;
146  pMsg[index++] = 0x5D;
147  break;
148  case 0x7E: /* Add escape sequence for start/stop character. */
149  pMsg[index++] = 0x7D;
150  pMsg[index++] = 0x5E;
151  break;
152  default: /* Add the actual character. */
153  pMsg[index++] = pBuffer[offset];
154  }
155  }
156  pMsg[index++] = 0x7E; /* Add the Stop marker. */
157 
158  return index;
159 }
The data_format_hdlc.h file contains the Host interface definitions and configuration.
This structure holds information to receive a packet of data to the host.
Definition: host_io_uart.h:75
size_t HDLC_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
bool HDLC_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
uint32_t size
uint8_t * pRxbuf
Definition: host_io_uart.h:79