ISSDK  1.8
IoT Sensing Software Development Kit
data_format_hdlc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10 * @file data_format_hdlc.c
11 * @brief The data_format_hdlc.c file contains definitions for
12  encoding and decoding HDLC messages to and from Host.
13 */
14 
15 /*******************************************************************************
16  * ISSDK Includes
17  ******************************************************************************/
18 #include "data_format_hdlc.h"
19 
20 /*******************************************************************************
21  * Functions
22  ******************************************************************************/
23 /* Function to handle incomming HDLC encoded bytes form the Host over UART. */
24 bool HDLC_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
25 {
26  static uint8_t c_prev = 0;
27  bool bRxPacketerror = false, bPacketReceived = false;
28 
29  switch (pHostRxPkt->rxState) /* Process the character. */
30  {
32  if (c == 0x7E)
33  { /* Wait for the remaining packet. */
34  pHostRxPkt->mIndex = 0;
36  }
37  break;
39  if (c != 0x7E)
40  { /* Got some real data (non-marker char).
41  * Do escape decode if needed. */
42  if (c == 0x7D)
43  {
44  c_prev = c;
45  break;
46  }
47  else if (c_prev == 0x7D)
48  {
49  if (c == 0x5D)
50  {
51  c = 0x7D;
52  }
53  else if (c == 0x5E)
54  {
55  c = 0x7E;
56  }
57  else
58  { /* Illegal escape sequence. Reset and wait for next packet. */
59  bRxPacketerror = true;
60  }
61  c_prev = 0;
62  }
63  /* Escape decode done. Just got data byte, save it. */
64  pHostRxPkt->pRxbuf[pHostRxPkt->mIndex] = c;
65  ++(pHostRxPkt->mIndex);
66  }
67  else
68  {
69  /* Got packet marker while waiting for data. */
70  if (pHostRxPkt->mIndex > 0)
71  { /* Got a complete packet.
72  * Set state wait for next packet. */
74  /* Set flag to process packet just received. */
75  bPacketReceived = true;
76  }
77  else
78  {
79  /* We got back to back packet marker. This 2nd packet marker will
80  * then be treated as a start marker. Go back to wait for data. */
82  pHostRxPkt->mIndex = 0;
83  }
84  }
85  break;
86  default:
87  /* Unknown state. */
88  bRxPacketerror = true;
89  break;
90  }
91 
92  /* Handle packet error. */
93  if (bRxPacketerror == true)
94  {
95  pHostRxPkt->mIndex = 0;
97  }
98 
99  return bPacketReceived;
100 }
101 
102 /* Function to format data for HDLC and send bytes to Host over UART. */
103 size_t HDLC_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
104 {
105  size_t index = 0;
106 
107  if (pBuffer == NULL || pMsg == NULL || size == 0)
108  {
109  return 0;
110  }
111 
112  /* Construct the Host Message. */
113  pMsg[index++] = 0x7E; /* Add the Start marker. */
114  for (size_t offset = 0; offset < size; offset++)
115  {
116  switch (pBuffer[offset])
117  {
118  case 0x7D: /* Add escape sequence for escape character. */
119  pMsg[index++] = 0x7D;
120  pMsg[index++] = 0x5D;
121  break;
122  case 0x7E: /* Add escape sequence for start/stop character. */
123  pMsg[index++] = 0x7D;
124  pMsg[index++] = 0x5E;
125  break;
126  default: /* Add the actual character. */
127  pMsg[index++] = pBuffer[offset];
128  }
129  }
130  pMsg[index++] = 0x7E; /* Add the Stop marker. */
131 
132  return index;
133 }
uint32_t size
This structure holds information to receive a packet of data to the host.
Definition: host_io_uart.h:49
uint8_t * pRxbuf
Definition: host_io_uart.h:53
bool HDLC_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
size_t HDLC_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
The data_format_hdlc.h file contains the Host interface definitions and configuration.