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 * @file data_format_json.c
10 * @brief The data_format_json.c file implements JSON data format interfaces and services.
11 */
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string.h>
15 #include <stdbool.h>
16 #include "data_format_json.h"
17 /*******************************************************************************
18 * Variables
19 ******************************************************************************/
20 const char *gpJsonHeader = {"{\"d\":{"};
21 const char *gpJsonFooter = "}}";
22 /*******************************************************************************
23 * Prototypes
24 ******************************************************************************/
25
26 /* @brief This defines the json packet state: Note: - Just dummy state:@todo for the actual implementation
27 */
28 typedef enum _json_packet_state_
29 {
30 STATE_NONE,
31 STATE_START_PACKET,
32 STATE_PAYLOAD,
33 STATE_END_PACKET,
34 } json_packet_state_t;
35 /*******************************************************************************
36 * Code
37 *****************************************************************************/
JSON_Serialize(char * pStr,char * pDataTagStr,char * pDataValue,json_format_t type,bool end)38 int32_t JSON_Serialize(char *pStr, char *pDataTagStr, char *pDataValue, json_format_t type, bool end)
39 {
40 //@todo handle the validation error. may be need to re-do for the better implementation
41 if (JSON_TYPE_OBJECT == type)
42 {
43 if (NULL != pStr)
44 {
45 if (0 == strlen(pStr))
46 {
47 // Add Header
48 strcat(pStr, gpJsonHeader);
49 }
50 else
51 {
52 strcat(pStr, ",");
53 }
54
55 strcat(pStr, "\"");
56 strcat(pStr, pDataTagStr);
57 strcat(pStr, "\":");
58 strcat(pStr, pDataValue);
59 }
60 // Add the footer
61 if (end)
62 {
63 strcat(pStr, gpJsonFooter);
64 }
65 }
66 return DATA_FORMAT_JSON_OK;
67 }
68 /*Note: Signature of this function may change based on the actual implemenation*/
JSON_Deserialize(void * pInData,void * pDataTag,char * pDataValue,json_format_t type)69 int32_t JSON_Deserialize(void *pInData, void *pDataTag, char *pDataValue, json_format_t type)
70 {
71 //@todo handle the validation error(array bounce. etc..) may need to redo for the better implementation
72 return DATA_FORMAT_JSON_OK;
73 }
74 #if HOST_INTERFACE
JSON_BlockDataRead_BlockingCall(host_interface_handle_t * pHandle,void * pRecvData)75 void JSON_BlockDataRead_BlockingCall(host_interface_handle_t *pHandle, void *pRecvData)
76 {
77 // @todo- This function does not implement exact json format.its an dummy implementation,
78 // Need to implement proper handling for actual example applicaton
79 comm_interface_t *pCommInt = pHandle->pCommInterface;
80 uint8_t *pData = pRecvData;
81 uint8_t data;
82 bool isReceivedStream = false;
83 uint8_t state = STATE_NONE;
84 while (!isReceivedStream)
85 {
86 pCommInt->Receive(&pHandle->commHandle, (uint8_t *)&data, 1);
87 switch (state)
88 {
89 case STATE_NONE:
90 {
91 if (data == '{')
92 state = STATE_START_PACKET;
93 else
94 continue;
95 }
96 break;
97 case STATE_START_PACKET:
98 case STATE_PAYLOAD:
99 if (data == '}')
100 {
101 state = STATE_NONE;
102 isReceivedStream = true;
103 }
104 else
105 {
106 *pData++ = data;
107 }
108 break;
109 default:
110 state = STATE_NONE;
111 isReceivedStream = true;
112 break;
113 }
114 }
115 }
116 //@todo- this is a dummy implemenation for a quick hack
JSON_Get_Stream_NonBlockingCall(void * pRecvData,uint8_t data,uint8_t * state,uint8_t * buffIndex)117 int32_t JSON_Get_Stream_NonBlockingCall(void *pRecvData, uint8_t data, uint8_t *state, uint8_t *buffIndex)
118 {
119 uint8_t *pData = pRecvData;
120 bool isReceivedStream = false;
121
122 switch (*state)
123 {
124 case STATE_NONE:
125 if (data == '{')
126 *state = STATE_START_PACKET;
127 break;
128 case STATE_START_PACKET:
129 case STATE_PAYLOAD:
130 if (data == '}')
131 {
132 *state = STATE_NONE;
133 isReceivedStream = true;
134 *buffIndex = 0;
135 }
136 else
137 {
138 *(pData + *buffIndex) = data;
139 (*buffIndex)++;
140 }
141 break;
142 default:
143 *state = STATE_NONE;
144 isReceivedStream = true;
145 break;
146 }
147 if (isReceivedStream == true)
148 {
149 return DATA_FORMAT_JSON_OK;
150 }
151 else
152 {
153 return -1;
154 }
155 }
156
157 /* TODO: Function to handle incomming JSON encoded bytes form the Host over UART. */
JSON_Process_Rx_Byte(uint8_t c,host_rx_packet_t * pHostRxPkt)158 bool JSON_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
159 {
160 return false;
161 }
162
163 /* TODO: Function to format data for JSON and send bytes to Host over UART. */
JSON_Process_Tx_Msg(const uint8_t * pBuffer,uint8_t * pMsg,size_t size)164 size_t JSON_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
165 {
166 memcpy(pMsg, pBuffer, size);
167 return size;
168 }
169
170 #endif
171