1 /*
2  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 /**
9 * @file data_format_json.h
10 * @brief The format_json.h file describes the structures and definitions for the data-format standard JSON.
11 */
12 
13 #ifndef DATA_FORMAT_JSON_H_
14 #define DATA_FORMAT_JSON_H_
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <stdbool.h>
18 #define HOST_INTERFACE 1
19 #if HOST_INTERFACE
20 #include "host_interface_service.h"
21 #include "host_io_uart.h"
22 #endif
23 #define DATA_FORMAT_JSON_OK 0
24 /*******************************************************************************
25  * Definitions
26  ******************************************************************************/
27 
28 typedef enum _json_format_
29 {
30     JSON_TYPE_OBJECT, /* String value pair, Note: Value will be primitive data type*/
31     JSON_TYPE_ARRAY   /* Array of Values*/
32 } json_format_t;
33 /******************************************************************************/
34 /*! @brief       The function to serialize the data,
35  *  @details     it applys the serialization in single data element,
36                  Once user finishes the data stream with different data tag, set end flag to TRUE.
37 
38  *  @param[in]   pStr - pointer to the data stream - formated data put into this buffer
39  *  @param[in]   pDataTagStr - Data tag name for a data
40  *  @param[in]   pDataValue - Data value
41  *  @param[in]   type  - Json data type
42  *  @param[in]   end - flag determine the end of data stream serialization.
43  *  @return      ::JSON_Serialize() returns the status .
44  *
45  *  @Constraints None
46  *
47  *  @Reentrant   Yes
48  */
49 int32_t JSON_Serialize(char *pStr, char *pDataTagStr, char *pDataValue, json_format_t type, bool end);
50 
51 /*! @brief       The function to deserialize the data,
52  *  @details     it applys the deserialization in a single data element,
53 
54  *  @param[in]   pStr - pointer to a formated data stream
55  *  @param[out]  pDataTagStr - Data tag name for a data
56  *  @param[out]  pDataValue - Data value
57  *  @param[in]   type  - Json data type
58  *  @return      ::JSON_Deserialize() returns the status .
59  *
60  *  @Constraints None
61  *
62  *  @Reentrant   Yes
63  */
64 // Note: Signature of this function may change based on the implementation and usability.
65 int32_t JSON_Deserialize(void *pInData, void *pDataTag, char *pDataValue, json_format_t type);
66 
67 #if HOST_INTERFACE
68 
69 /*! @brief       The function provides block data read for the JSON stream, This is for Blocking receive call
70  *  @details     This can be the argument to host interface
71 
72  *  @param[in]   pHandle - pointer to a formated data stream
73  *  @param[out]  pRecvData -  Pointer to RecvBuffer;
74  *  @return      None.
75  *
76  *  @Constraints None
77  *
78  *  @Reentrant   Yes
79  */
80 // Note: Signature of this function may change based on the implementation and usability.
81 void JSON_BlockDataRead_BlockingCall(host_interface_handle_t *pHandle, void *pRecvData);
82 
83 /*! @brief       This function is a helper function to get json stream where the data length is unknown
84  *  @details     Basically look for start and end packets and form a packet
85 
86  *  @param[out]  pRecvData -  Pointer to RecvBuffer;
87  *  @param[out]  data -  one byte of data received.
88  *  @parma[in]   event     - status event for send complete and receive complete
89  *  @return      DATA_FORMAT_JSON_OK, if data read is completed. else still get more data.
90  *
91  *  @Constraints None
92  *
93  *  @Reentrant   Yes
94  */
95 // Note: Signature of this function may change based on the implementation and usability.
96 
97 int32_t JSON_Get_Stream_NonBlockingCall(void *pRecvData, uint8_t data, uint8_t *state, uint8_t *buffIndex);
98 
99 /*! @brief       Function to handle incomming JSON encoded bytes form the Host over UART.
100  *  @details     This function will be called on receipt of every UART Byte
101  *               and will do the JSON combination to create a Host Message.
102  *  @param[in]   uint8_t c The character in the UART payload.
103  *  @param[in]   host_rx_packet_t *pHostRxPkt The Host Packet context structure.
104  *  @return      bool Success/Failure.
105  *  @constraints This should be the called only after DEBUG/UART has been initialized.
106  *  @reeentrant  No
107  */
108 bool JSON_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt);
109 
110 /*  @brief       Function to format bytes for JSON to be sent to Host over UART.
111  *  @details     This function will encode a Host Protocol formatted Message to be sent to Host over UART.
112  *  @param[in]   uint8_t *pbuffer The handle to the input buffer containing the Host message.
113  *  @param[in]   uint8_t *pMsg    The handle to the output buffer containing the formatted Host message.
114  *  @param[in]   size_t size     The number of bytes to be sent starting form the buffer.
115  *  @return      size_t          Length of the encoded message.
116  *  @constraints This should be the called only after DEBUG/UART has been initialized.
117  *  @reeentrant  No
118  */
119 size_t JSON_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size);
120 
121 #endif
122 
123 #endif // DATA_FORMAT_JSON_H_
124