ISSDK  1.8
IoT Sensing Software Development Kit
data_format_json.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 * @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 {
35 /*******************************************************************************
36  * Code
37  *****************************************************************************/
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*/
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
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
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. */
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. */
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
uint32_t size
enum _json_format_ json_format_t
This structure holds information to receive a packet of data to the host.
Definition: host_io_uart.h:49
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
The format_json.h file describes the structures and definitions for the data-format standard JSON...
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.
#define DATA_FORMAT_JSON_OK
COMM_Receive_t * Receive
uint8_t data[FXLS8962_DATA_SIZE]
int32_t JSON_Serialize(char *pStr, char *pDataTagStr, char *pDataValue, json_format_t type, bool end)
The function to serialize the data,.
size_t JSON_Process_Tx_Msg(const uint8_t *pBuffer, uint8_t *pMsg, size_t size)
const char * gpJsonFooter
comm_interface_t * pCommInterface
enum _json_packet_state_ json_packet_state_t
int32_t JSON_Deserialize(void *pInData, void *pDataTag, char *pDataValue, json_format_t type)
The function to deserialize the data,.
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.
_json_packet_state_
const char * gpJsonHeader
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...