RDK Documentation (Open Sourced RDK Components)
webpa_notification.cpp
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 2017 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 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include "webpa_notification.h"
23 #include "webpa_adapter.h"
24 #include "webpa_parameter.h"
25 #include <stddef.h>
26 #include "cJSON.h"
27 #include "rdk_debug.h"
28 #include "safec_lib.h"
29 
30 #define DEVICE_ESTB_MAC_PARAM "Device.DeviceInfo.X_COMCAST-COM_STB_MAC"
31 #define WEBPA_UNKNOWN_PARAM_VALUE "Unknown"
32 #define WEBPA_NOTIFY_SRC_LEN 32
33 
34 
35 char **g_notifyParamList = NULL;
36 unsigned int g_notifyListSize = 0;
37 const char* webpaNotifyConfigFile = NULL;
38 char* notificationSource = NULL;
39 
40 static void macToLower(char macValue[],char macConverted[]);
41 
42 
43 /**
44  * @brief Initializes WebPA configuration file
45  *
46  * @return void.
47  */
48 void setNotifyConfigurationFile(const char* nofityConfigFile)
49 {
50  if(NULL != nofityConfigFile)
51  {
52  webpaNotifyConfigFile = nofityConfigFile;
53  RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,"Notify Configuration file set %s \n",webpaNotifyConfigFile);
54  }
55  else
56  {
57  RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,"Unable to set Notify Configuration file\n");
58  }
59 }
60 
61 /**
62  * @brief Get the Notification source
63  *
64  * @return void.
65  */
67 {
68  WDMP_STATUS* ret = 0;
69  size_t *retCount = 0;
70  char convertedMac[32]= {'\0'};
71  char deviceMac[32] = {'\0'};
72 
73  RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,"Entering .... %s \n", __FUNCTION__);
74 
75  if(NULL == notificationSource)
76  {
77  char *getParamList[1];
78  getParamList[0]= (char*) calloc(1,MAX_PARAMETER_LENGTH);
79  strncpy(getParamList[0],DEVICE_ESTB_MAC_PARAM,MAX_PARAMETER_LENGTH);
80  param_t **parametervalArr = (param_t **) malloc(sizeof(param_t **));
81  if(parametervalArr)
82  {
83  *parametervalArr = NULL; //CID:44089 - Reverse_inull
84  }
85  ret = (WDMP_STATUS *) malloc(sizeof(WDMP_STATUS)); //CID:18425 - sizeofmismatch
86  retCount = (size_t *) malloc(sizeof(size_t));
87  getValues(const_cast<const char**>(getParamList), 1, &parametervalArr, &retCount, &ret);
88  notificationSource = (char*) malloc(WEBPA_NOTIFY_SRC_LEN);
89  if((NULL != parametervalArr) && (NULL != *parametervalArr) && (NULL != (*parametervalArr)[0].value))
90  {
91  strncpy(deviceMac,const_cast<const char*>((*parametervalArr)[0].value),WEBPA_NOTIFY_SRC_LEN);
92  RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,"[%s] Calling MacToLower for MAC: %s ", __FUNCTION__,deviceMac);
93  macToLower(deviceMac,convertedMac);
94  RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,"[%s] Converted MAC: %s ", __FUNCTION__,convertedMac);
95  snprintf(notificationSource, WEBPA_NOTIFY_SRC_LEN, "mac:%s", convertedMac);
96  RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,"[%s] Notify Source :- %s ", __FUNCTION__,notificationSource);
97  }
98  else
99  {
100  RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,"[%s] Unable to get Notification Source .!! \n", __FUNCTION__);
101  strncpy(notificationSource, WEBPA_UNKNOWN_PARAM_VALUE,sizeof(WEBPA_UNKNOWN_PARAM_VALUE));
102  }
103  // Lets free all allocated values
104  WAL_FREE(getParamList[0]);
105  WAL_FREE(ret);
106  WAL_FREE(retCount);
107 
108  if(NULL != parametervalArr)
109  {
110  if(NULL != *parametervalArr) {
111  WAL_FREE((*parametervalArr)[0].value);
112  WAL_FREE((*parametervalArr)[0].name);
113  }
114  WAL_FREE(parametervalArr);
115  }
116  }
117  return notificationSource;
118 }
119 
120 
121 /*
122  * @brief To convert MAC to lower case without colon
123  * assuming max MAC size as 32
124  */
125 static void macToLower(char macValue[],char macConverted[])
126 {
127  int i = 0;
128  int j;
129  char *token[32];
130  char tmp[32];
131  strncpy(tmp, macValue,sizeof(tmp));
132  token[i] = strtok(tmp, ":");
133  if(token[i]!=NULL)
134  {
135  strncat(macConverted, token[i],31);
136  macConverted[31]='\0';
137  i++;
138  }
139  while ((token[i] = strtok(NULL, ":")) != NULL)
140  {
141  strncat(macConverted, token[i],31);
142  macConverted[31]='\0';
143  i++;
144  }
145  macConverted[31]='\0';
146  for(j = 0; macConverted[j]; j++)
147  {
148  macConverted[j] = tolower(macConverted[j]);
149  }
150 }
151 
152 /**
153  * @brief Read Notify configuration file from config file
154  *
155  * @return void.
156  */
157 int getnotifyparamList(char ***notifyParamList,int *ptrnotifyListSize)
158 {
159  char *temp_ptr = NULL;
160  char *notifycfg_file_content = NULL;
161  int i = 0;
162  int ch_count = 0;
163  FILE *fp = NULL;
164  errno_t rc = -1;
165 
166  // Read file notification Configuration file
167  if(NULL == webpaNotifyConfigFile)
168  {
169  RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,"WebPA notification file path not set");
170  return -1;
171  }
172  RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,"Inside getnotifyparamList trying to open %s\n", webpaNotifyConfigFile);
173  fp = fopen(webpaNotifyConfigFile, "r");
174  if (fp == NULL)
175  {
176  RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,"Failed to open cfg file %s\n", webpaNotifyConfigFile);
177  return -1;
178  }
179  fseek(fp, 0, SEEK_END);
180  ch_count = ftell(fp);
181  if(ch_count < 1)
182  {
183  RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,"WebPA notification file is Empty %s\n", webpaNotifyConfigFile);
184  fclose(fp);
185  return -1;
186  }
187  fseek(fp, 0, SEEK_SET);
188  notifycfg_file_content = (char *) malloc(sizeof(char) * (ch_count + 1));
189  fread(notifycfg_file_content, 1, ch_count,fp);
190  notifycfg_file_content[ch_count] ='\0';
191  fclose(fp); //CID:18630 - NEGATIVE RETURNS
192 
193  cJSON *notify_cfg = cJSON_Parse(notifycfg_file_content);
194  cJSON *notifyArray = cJSON_GetObjectItem(notify_cfg,"Notify");
195  if(NULL != notifyArray)
196  {
197  *ptrnotifyListSize =(int)cJSON_GetArraySize(notifyArray);
198  *notifyParamList = (char **)malloc(sizeof(char *) **ptrnotifyListSize);
199  for (i = 0 ; i < cJSON_GetArraySize(notifyArray) ; i++)
200  {
201  temp_ptr = cJSON_GetArrayItem(notifyArray, i)->valuestring;
202  if(temp_ptr)
203  {
204  (*notifyParamList)[i] = (char *)malloc(sizeof(char ) * (strlen(temp_ptr)+1));
205  rc=strcpy_s((*notifyParamList)[i],(strlen(temp_ptr)+1),temp_ptr);
206  if(rc!=EOK)
207  {
208  ERR_CHK(rc);
209  }
210  RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,"Notify Param = %s\n", temp_ptr);
211  }
212  }
213  // Update local Parameter list from generic layer
214  if(NULL != notifyParamList && NULL != ptrnotifyListSize)
215  {
216  g_notifyParamList = *notifyParamList;
217  g_notifyListSize = *ptrnotifyListSize;
218  }
219  }
220  else
221  {
222  RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,"Unable to parse Configuration file");
223  }
224  if(notifycfg_file_content)
225  {
226  free(notifycfg_file_content);
227  }
228  return 0;
229 }
setNotifyConfigurationFile
void setNotifyConfigurationFile(const char *nofityConfigFile)
Initializes WebPA configuration file.
Definition: webpa_notification.cpp:48
webpa_adapter.h
rdk_debug.h
getnotifyparamList
int getnotifyparamList(char ***notifyParamList, int *ptrnotifyListSize)
Read Notify configuration file from config file.
Definition: webpa_notification.cpp:157
getNotifySource
char * getNotifySource()
Get the Notification source.
Definition: webpa_notification.cpp:66
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
webpa_parameter.h
getValues
void getValues(const char *paramName[], const unsigned int paramCount, param_t ***paramValArr, size_t **retValCount, WDMP_STATUS **retStatus)
getValues interface returns the parameter values.
Definition: webpa_parameter.cpp:68