RDK Documentation (Open Sourced RDK Components)
dcmjsonparser.c
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21 *@file dcmjsonparser.c
22 *
23 *@brief Types and APIs exposed by dcm.
24 */
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <cjson/cJSON.h>
30 #include <errno.h>
31 #include <telemetry_busmessage_sender.h>
32 #include "safec_lib.h"
33 
34 
35 /**
36  * @defgroup DCM DCM (Device Configuration Management)
37  *
38  * - This component parses Device Configuration Management JSON responses.
39  *
40  * @image html DCM.png
41  * @defgroup DCM_API DCM Public APIs
42  * @ingroup DCM
43  *
44  * @defgroup DCM_TYPES DCM Data Types
45  * Data types defined for RFC(Remote Feature Control).
46  * @ingroup DCM
47  */
48 
49 /**
50  * @addtogroup DCM_TYPES
51  * @{
52  */
53 
54 #define RFC_CONFIG_DATA "/tmp/rfc_configdata.txt" //!< Contains Configured data’s that are parsed to the client.
55 #define RFC_SSH_CONFIG_DATA "/tmp/RFC/.RFC_SSHWhiteList.list" //!< Contains the list of IPs obtained from RFC feature control at startup.
56 /**
57  * @}
58  */
59 
60 
61 /**
62  * @addtogroup DCM_API
63  * @{
64  */
65 
66 /**
67  * @brief This function takes a cJSON node as argument and verifies whether the supplied argument is an node array.
68  *
69  * @param[in] item item to know its type.
70  *
71  * @retval Returns 1 if the supplied item is an array kind otherwise returns 0.
72  */
73 static int isNodeArrayType(cJSON * const item)
74 {
75  if (item == NULL)
76  {
77  return 0;
78  }
79 
80  return (item->type & 0xFF) == cJSON_Array;
81 }
82 
83 /**
84  * @brief This function writes the parsed JSON information to a file.
85  *
86  * @param[in] arrayNode JSON array.
87  * @param[in] filename Name of the file where parsed data will be stored.
88  *
89  * @ret returns status(0/1).
90  */
91 static int saveToFile(cJSON * arrayNode, const char* filename)
92 {
93  printf("dcmjsonparser: Entering %s\n", __FUNCTION__);
94  int status = 0;
95  FILE * fp;
96  int i = 0;
97  fp = fopen(filename,"w+");
98  if(NULL != fp)
99  {
100  int arraySize = cJSON_GetArraySize(arrayNode);
101  for(i =0;i<arraySize;i++)
102  {
103  cJSON * aItem = cJSON_GetArrayItem(arrayNode,i);
104  status = fprintf(fp,"%s\n",aItem->valuestring);
105  if(status <= 0)
106  printf("dcmjsonparser: %s:%d Warning failed to write to file %s \n",__FUNCTION__, __LINE__,strerror(errno));
107  }
108  fclose(fp);
109  status = 1;
110  }
111  else
112  {
113  printf("dcmjsonparser: Failed to open %s : %s\n",filename,strerror(errno));
114  }
115  return status;
116 }
117 
118 /**
119  * @brief This function traverses through the cJSON node list and returns the next node that is an Array.
120  *
121  * @param[in] node JSON response from SSH whitelist.
122  *
123  * @ret returns JSON array.
124  */
125 static cJSON* getArrayNode(cJSON *node)
126 {
127  cJSON * arrayNode = NULL;
128  while(NULL != node)
129  {
130 
131  if(isNodeArrayType(node))
132  {
133  arrayNode = node;
134  break;
135  }
136  else if (NULL != node->child)
137  {
138  cJSON * childNode = getArrayNode(node->child);
139  if( NULL != childNode)
140  {
141  arrayNode = childNode;
142  break;
143  }
144  }
145  node = node->next;
146  }
147  return arrayNode;
148 }
149 
150 /**
151  * @brief This function processes IP and MAC lists configured in the feature list.
152  *
153  * -Example:
154  * {
155  * "name":"SNMPWhitelist",
156  * "effectiveImmediate":false,
157  * "enable":true,
158  * "configData":{},
159  * "listType":"IPv4",
160  * "listSize":5,
161  * "SNMP IP4 WL":["178.62.43.255","128.82.34.17","192.168.1.80","192.168.1.1/24","10.0.0.32/6"]
162  * },
163  *
164  * @param[in] sshFeature Contains JSON values like List type and list size
165  *
166  */
167 static void processSSHWhiteList(cJSON *sshFeature)
168 {
169  printf("dcmjsonparser: Entering %s\n", __FUNCTION__);
170  cJSON* childNode = getArrayNode(sshFeature);
171  if(NULL != childNode)
172  {
173  if (1 == saveToFile(childNode, RFC_SSH_CONFIG_DATA))
174  printf("dcmjsonparser: SSHWhiteList processed successfully\n");
175  }
176 }
177 
178 /**
179  * @brief This function parses dcm JSON information and save it to a file.
180  *
181  * JSON will contain these information Feature control information like effectiveImmediate,configData.
182  * - Example:In this main function "featureControl" JSON is created with features in KeyValue format.
183  *
184  * FeatureControl
185  * {
186  * "features": [
187  * {
188  * "name": "<feature name>",
189  * "effectiveImmediate": true,
190  * "enable": true,
191  * "configData": {
192  <Parameter to configure>
193  * }
194  * "listType": "B8:27:EB:50:C1:CF",
195  * "listSize": 1,
196  * "RDK_RPI": [
197  * "B8:27:EB:50:C1:FC"
198  * ]
199  * }
200  * ]
201  * }
202  *
203  */
204 void main(int argc, char **argv)
205 {
206  char *data = NULL,*dcmResponse = NULL;
207  cJSON *paramObj = NULL, *childObj = NULL,*json=NULL,*configData=NULL,*effectiveImmediate=NULL;
208  FILE *fileRead = NULL,*fileWrite =NULL;
209 
210  long len;
211  int i;
212  errno_t rc = -1;
213  int ind = -1;
214 
215  if(argc != 2)
216  {
217  printf("dcmjsonparser: Pass valid arguments \n");
218  exit(0);
219  }
220  else
221  {
222  dcmResponse = argv[1];
223  }
224 
225  t2_init("dcm-parser");
226 
227  printf("dcmjsonparser: dcm response file name %s\n",dcmResponse);
228 
229  fileRead = fopen( dcmResponse, "r+" );
230  if( fileRead == NULL )
231  {
232  printf( "dcmjsonparser: Error opening file in read mode\n" );
233  exit(0);
234  }
235 
236  fseek( fileRead, 0, SEEK_END );
237  len = ftell( fileRead );
238  fseek( fileRead, 0, SEEK_SET );
239  data = ( char* )malloc( len + 1 );
240  fread( data, 1, len, fileRead );
241  fclose( fileRead );
242 
243  if( data != NULL )
244  {
245  json = cJSON_Parse( data );
246 
247  if( !json )
248  {
249  printf( "dcmjsonparser: json parse error: [%s]\n", cJSON_GetErrorPtr() );
250  t2_event_d("SYS_INFO_WEBPA_Config_Corruption", 1);
251  }
252  else
253  {
254  printf( "dcmjsonparser: cjson parse success\n" );
255 
256  fileWrite=fopen(RFC_CONFIG_DATA,"w+");
257  if(fileWrite ==NULL)
258  {
259  printf( "dcmjsonparser: Error opening file in write mode\n" );
260  free(data);
261  exit(0);
262  }
263 
264  paramObj = cJSON_GetObjectItem( json, "featureControl" );
265  if( paramObj != NULL )
266  {
267  cJSON * configset_id = cJSON_GetObjectItem( paramObj, "configset-id" );
268  if(configset_id !=NULL)
269  {
270  char *configset = cJSON_GetObjectItem( paramObj, "configset-id" )->valuestring;
271  if(configset !=NULL)
272  {
273  printf( "dcmjsonparser: configset-id is %s\n", configset );
274  }
275  else
276  {
277  printf( "dcmjsonparser: configset-id value is NULL\n");
278  }
279  }
280  else
281  {
282  printf( "dcmjsonparser: configset-id not recieved in response\n");
283  }
284  cJSON * configset_label = cJSON_GetObjectItem( paramObj, "configset-label" );
285  if(configset_label !=NULL)
286  {
287  char *configset = cJSON_GetObjectItem( paramObj, "configset-label" )->valuestring;
288  if(configset !=NULL)
289  {
290  printf( "dcmjsonparser: configset-label is %s\n", configset );
291  }
292  else
293  {
294  printf( "dcmjsonparser: configset-label value is NULL\n");
295  }
296  }
297  else
298  {
299  printf( "dcmjsonparser: configset-label not recieved in response\n");
300  }
301 
302  childObj = cJSON_GetObjectItem( paramObj, "features" );
303  if(childObj !=NULL)
304  {
305  int subitemSize = cJSON_GetArraySize( childObj );
306  printf("dcmjsonparser: features array size is %d\n",subitemSize);
307  int length = strlen("sshwhitelist");
308  for( i = 0; i < subitemSize; i++ )
309  {
310  cJSON* subitem = cJSON_GetArrayItem( childObj, i );
311  cJSON* featureName = cJSON_GetObjectItem(subitem, "name" );
312  cJSON* listType = cJSON_GetObjectItem(subitem, "listType");
313  if (featureName != NULL && listType != NULL)
314  {
315  rc = strcasecmp_s("sshwhitelist",length, featureName->valuestring, &ind);
316  ERR_CHK(rc);
317  if ((!ind) && (rc == EOK))
318  {
319  printf("dcmjsonparser: SSHWhiteList feature found!!\n");
320  processSSHWhiteList(subitem);
321  }
322  }
323 
324  effectiveImmediate = cJSON_GetObjectItem( subitem, "effectiveImmediate" );
325  int effectiveImmediatevalue;
326  if(effectiveImmediate !=NULL)
327  {
328  effectiveImmediatevalue=effectiveImmediate->valueint;
329  printf("dcmjsonparser: effectiveImmediate is %d\n",effectiveImmediatevalue);
330  }
331  else
332  {
333  printf("dcmjsonparser: featureControl.features.effectiveImmediate object is not present\n");
334  }
335 
336  configData = cJSON_GetObjectItem( subitem, "configData" );
337  if(configData !=NULL)
338  {
339  cJSON *configObject = configData->child;
340  while( configObject )
341  {
342  char *configKey = configObject->string;
343  char *configValue = configObject->valuestring;
344  printf("dcmjsonparser: key is %s\n",configKey);
345  printf("dcmjsonparser: value is %s\n",configValue);
346  if(strncmp(configKey,"tr181.",6)==0)
347  {
348  /* #~ is used to seperate configKey,configValue,effectiveImmediatevalue which can used as delimiter to cut these values respectively.*/
349  fprintf(fileWrite, "%s#~%s#~%d\n", configKey,configValue,effectiveImmediatevalue );
350  }
351  configObject = configObject->next;
352  }
353  }
354  }
355  }
356  else
357  {
358  printf("dcmjsonparser: featureControl.features object is not present\n");
359  }
360  }
361  else
362  {
363  printf("dcmjsonparser: featureControl object is not present\n");
364  }
365  fclose(fileWrite);
366  }
367  free(data);
368  }
369 }
370 
371 /**
372  * @} //End of Doxygen
373  */
374 
isNodeArrayType
static int isNodeArrayType(cJSON *const item)
This function takes a cJSON node as argument and verifies whether the supplied argument is an node ar...
Definition: dcmjsonparser.c:73
saveToFile
static int saveToFile(cJSON *arrayNode, const char *filename)
This function writes the parsed JSON information to a file.
Definition: dcmjsonparser.c:91
processSSHWhiteList
static void processSSHWhiteList(cJSON *sshFeature)
This function processes IP and MAC lists configured in the feature list.
Definition: dcmjsonparser.c:167
RFC_SSH_CONFIG_DATA
#define RFC_SSH_CONFIG_DATA
Contains the list of IPs obtained from RFC feature control at startup.
Definition: dcmjsonparser.c:55
getArrayNode
static cJSON * getArrayNode(cJSON *node)
This function traverses through the cJSON node list and returns the next node that is an Array.
Definition: dcmjsonparser.c:125
RFC_CONFIG_DATA
#define RFC_CONFIG_DATA
Contains Configured data’s that are parsed to the client.
Definition: dcmjsonparser.c:54
main
void main(int argc, char **argv)
This function parses dcm JSON information and save it to a file.
Definition: dcmjsonparser.c:204