ISSDK  1.7
IoT Sensing Software Development Kit
data_format_json.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 * @file data_format_json.c
36 * @brief The data_format_json.c file implements JSON data format interfaces and services.
37 */
38 #include <stddef.h>
39 #include <stdint.h>
40 #include <string.h>
41 #include <stdbool.h>
42 #include "data_format_json.h"
43 /*******************************************************************************
44  * Variables
45  ******************************************************************************/
46 const char *gpJsonHeader = {"{\"d\":{"};
47 const char *gpJsonFooter = "}}";
48 /*******************************************************************************
49 * Prototypes
50 ******************************************************************************/
51 
52 /* @brief This defines the json packet state: Note: - Just dummy state:@todo for the actual implementation
53  */
54 typedef enum _json_packet_state_
55 {
61 /*******************************************************************************
62  * Code
63  *****************************************************************************/
64 int32_t JSON_Serialize(char *pStr, char *pDataTagStr, char *pDataValue, json_format_t type, bool end)
65 {
66  //@todo handle the validation error. may be need to re-do for the better implementation
67  if (JSON_TYPE_OBJECT == type)
68  {
69  if (NULL != pStr)
70  {
71  if (0 == strlen(pStr))
72  {
73  // Add Header
74  strcat(pStr, gpJsonHeader);
75  }
76  else
77  {
78  strcat(pStr, ",");
79  }
80 
81  strcat(pStr, "\"");
82  strcat(pStr, pDataTagStr);
83  strcat(pStr, "\":");
84  strcat(pStr, pDataValue);
85  }
86  // Add the footer
87  if (end)
88  {
89  strcat(pStr, gpJsonFooter);
90  }
91  }
92  return DATA_FORMAT_JSON_OK;
93 }
94 /*Note: Signature of this function may change based on the actual implemenation*/
95 int32_t JSON_Deserialize(void *pInData, void *pDataTag, char *pDataValue, json_format_t type)
96 {
97  //@todo handle the validation error(array bounce. etc..) may need to redo for the better implementation
98  return DATA_FORMAT_JSON_OK;
99 }
100 #if HOST_INTERFACE
102 {
103  // @todo- This function does not implement exact json format.its an dummy implementation,
104  // Need to implement proper handling for actual example applicaton
105  comm_interface_t *pCommInt = pHandle->pCommInterface;
106  uint8_t *pData = pRecvData;
107  uint8_t data;
108  bool isReceivedStream = false;
109  uint8_t state = STATE_NONE;
110  while (!isReceivedStream)
111  {
112  pCommInt->Receive(&pHandle->commHandle, (uint8_t *)&data, 1);
113  switch (state)
114  {
115  case STATE_NONE:
116  {
117  if (data == '{')
118  state = STATE_START_PACKET;
119  else
120  continue;
121  }
122  break;
123  case STATE_START_PACKET:
124  case STATE_PAYLOAD:
125  if (data == '}')
126  {
127  state = STATE_NONE;
128  isReceivedStream = true;
129  }
130  else
131  {
132  *pData++ = data;
133  }
134  break;
135  default:
136  state = STATE_NONE;
137  isReceivedStream = true;
138  break;
139  }
140  }
141 }
142 //@todo- this is a dummy implemenation for a quick hack
143 int32_t JSON_Get_Stream_NonBlockingCall(void *pRecvData, uint8_t data, uint8_t *state, uint8_t *buffIndex)
144 {
145  uint8_t *pData = pRecvData;
146  bool isReceivedStream = false;
147 
148  switch (*state)
149  {
150  case STATE_NONE:
151  if (data == '{')
152  *state = STATE_START_PACKET;
153  break;
154  case STATE_START_PACKET:
155  case STATE_PAYLOAD:
156  if (data == '}')
157  {
158  *state = STATE_NONE;
159  isReceivedStream = true;
160  *buffIndex = 0;
161  }
162  else
163  {
164  *(pData + *buffIndex) = data;
165  (*buffIndex)++;
166  }
167  break;
168  default:
169  *state = STATE_NONE;
170  isReceivedStream = true;
171  break;
172  }
173  if (isReceivedStream == true)
174  {
175  return DATA_FORMAT_JSON_OK;
176  }
177  else
178  {
179  return -1;
180  }
181 }
182 
183 /* TODO: Function to handle incomming JSON encoded bytes form the Host over UART. */
184 bool JSON_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
185 {
186  return false;
187 }
188 
189 /* TODO: Function to format data for JSON and send bytes to Host over UART. */
190 size_t JSON_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
191 {
192  memcpy(pMsg, pBuffer, size);
193  return size;
194 }
195 
196 #endif
const char * gpJsonHeader
bool JSON_Process_Rx_Byte(uint8_t c, host_rx_packet_t *pHostRxPkt)
Function to handle incomming JSON encoded bytes form the Host over UART.
#define DATA_FORMAT_JSON_OK
uint8_t data[FXLS8962_DATA_SIZE]
The format_json.h file describes the structures and definitions for the data-format standard JSON...
int32_t JSON_Serialize(char *pStr, char *pDataTagStr, char *pDataValue, json_format_t type, bool end)
The function to serialize the data,.
void JSON_BlockDataRead_BlockingCall(host_interface_handle_t *pHandle, void *pRecvData)
The function provides block data read for the JSON stream, This is for Blocking receive call...
This structure holds information to receive a packet of data to the host.
Definition: host_io_uart.h:75
comm_interface_t * pCommInterface
_json_packet_state_
enum _json_packet_state_ json_packet_state_t
const char * gpJsonFooter
COMM_Receive_t * Receive
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
uint32_t size
enum _json_format_ json_format_t
size_t JSON_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
int32_t JSON_Get_Stream_NonBlockingCall(void *pRecvData, uint8_t data, uint8_t *state, uint8_t *buffIndex)
This function is a helper function to get json stream where the data length is unknown.
int32_t JSON_Deserialize(void *pInData, void *pDataTag, char *pDataValue, json_format_t type)
The function to deserialize the data,.