31 #include <sys/types.h>
38 #define CONTENT_TYPE_JSON "application/json"
39 #define DEVICE_PROPS_FILE "/etc/device.properties"
40 #define CLIENT_PORT_NUM 6667
42 #define LOG_FILE "./tr69hostIflog.txt"
43 #define WEBPA_CONFIG_FILE "/etc/webpa_cfg.json"
44 #define PARODUS_URL "tcp://127.0.0.1:6666"
45 #define PAROUDUS_CLIENT_URL "tcp://127.0.0.1:6667"
52 #include <libparodus.h>
61 static long timeValDiff(
struct timespec *starttime,
struct timespec *finishtime);
66 libpd_instance_t libparodus_instance = NULL;
67 char parodus_url[URL_SIZE] = {
'\0'};
68 char client_url[URL_SIZE] = {
'\0'};
69 bool exit_parodus_recv =
false;
70 pthread_cond_t parodus_cond = PTHREAD_COND_INITIALIZER;
71 pthread_mutex_t parodus_lock = PTHREAD_MUTEX_INITIALIZER;
87 exit_parodus_recv =
true;
88 pthread_cond_signal(&parodus_cond);
98 const char *fl =
"/tmp/webpa";
99 int err = mkdir(fl, S_IRWXU | S_IRWXG);
100 if((err != 0) && (EEXIST == errno))
102 RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,
"Failed to create \"%s\" folder with error no(%d). \n", fl, errno);
105 status = checkDataModelStatus();
108 RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,
"Error in Database loading,Webpa Cannot be initialized..!! \n");
136 wrp_msg_t *res_wrp_msg ;
138 struct timespec start,end,*startPtr,*endPtr,currTime;
142 char *contentType = NULL;
144 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"Entering parodus_receive_wait.. \n");
146 while (!exit_parodus_recv)
148 rtn = libparodus_receive (libparodus_instance, &wrp_msg, 2000);
155 clock_gettime(CLOCK_MONOTONIC, &currTime);
156 currTime.tv_sec += 5;
157 pthread_mutex_lock(&parodus_lock);
158 if(0 != pthread_cond_timedwait(&parodus_cond, &parodus_lock,&currTime ))
160 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"parodus_receive_wait()::%s:%d wait for key acquisition timed out");
162 pthread_mutex_unlock(&parodus_lock);
166 if (wrp_msg && wrp_msg->msg_type == WRP_MSG_TYPE__REQ)
168 res_wrp_msg = (wrp_msg_t *)malloc(
sizeof(wrp_msg_t));
169 memset(res_wrp_msg, 0,
sizeof(wrp_msg_t));
172 processRequest((
char*)wrp_msg->u.req.payload, wrp_msg->u.req.transaction_uuid, ((
char **)(&(res_wrp_msg->u.req.payload))));
174 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"Response payload is %s\n",(
char *)(res_wrp_msg->u.req.payload));
175 if(res_wrp_msg->u.req.payload !=NULL)
177 res_wrp_msg->u.req.payload_size = strlen((
const char *)res_wrp_msg->u.req.payload);
179 res_wrp_msg->msg_type = wrp_msg->msg_type;
180 if(wrp_msg->u.req.dest != NULL)
181 res_wrp_msg->u.req.source = strdup(wrp_msg->u.req.dest);
182 if(wrp_msg->u.req.source != NULL)
183 res_wrp_msg->u.req.dest = strdup(wrp_msg->u.req.source);
184 if(wrp_msg->u.req.transaction_uuid != NULL)
185 res_wrp_msg->u.req.transaction_uuid = strdup(wrp_msg->u.req.transaction_uuid);
187 contentType = (
char *)malloc(
sizeof(
char)*(strlen(CONTENT_TYPE_JSON)+1));
188 strncpy(contentType,CONTENT_TYPE_JSON,strlen(CONTENT_TYPE_JSON)+1);
189 res_wrp_msg->u.req.content_type = contentType;
190 int sendStatus = libparodus_send(libparodus_instance, res_wrp_msg);
194 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"Sent message successfully to parodus\n");
198 RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,
"Failed to send message: '%s'\n",libparodus_strerror((libpd_error_t )sendStatus));
201 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"Elapsed time : %ld ms\n", timeValDiff(startPtr, endPtr));
202 wrp_free_struct (res_wrp_msg);
203 wrp_free_struct(wrp_msg);
206 rtn = libparodus_close_receiver (libparodus_instance);
209 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"Successfully closed libparodus receiver. \n");
213 RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,
"Failure in closing libparodus receiver, Ret = %d\n",rtn);
215 libparodus_shutdown(&libparodus_instance);
216 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"End of parodus_upstream\n");
227 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"************Inside sendNotification ************\n");
228 wrp_msg_t *notif_wrp_msg = NULL;
231 int backoffRetryTime = 0;
233 char *contentType = NULL;
235 notif_wrp_msg = (wrp_msg_t *)malloc(
sizeof(wrp_msg_t));
236 memset(notif_wrp_msg, 0,
sizeof(wrp_msg_t));
238 notif_wrp_msg ->msg_type = WRP_MSG_TYPE__EVENT;
239 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"source: %s\n",source);
240 notif_wrp_msg ->u.event.source = strdup(source);
241 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"destination: %s\n", destination);
242 notif_wrp_msg ->u.event.dest = strdup(destination);
243 contentType = (
char *)malloc(
sizeof(
char)*(strlen(CONTENT_TYPE_JSON)+1));
244 strncpy(contentType,CONTENT_TYPE_JSON,strlen(CONTENT_TYPE_JSON)+1);
245 notif_wrp_msg->u.event.content_type = contentType;
246 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"content_type is %s\n",notif_wrp_msg->u.event.content_type);
248 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"Notification payload: %s\n",payload);
249 notif_wrp_msg ->u.event.payload = (
void *)payload;
250 notif_wrp_msg ->u.event.payload_size = strlen((
const char*)notif_wrp_msg ->u.event.payload);
252 while(retry_count<=3)
254 backoffRetryTime = (int) pow(2, c) -1;
256 sendStatus = libparodus_send(libparodus_instance, notif_wrp_msg );
260 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"Notification successfully sent to parodus\n");
265 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"sendNotification backoffRetryTime %d seconds\n", backoffRetryTime);
266 sleep(backoffRetryTime);
272 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"sendStatus is %d\n",sendStatus);
273 wrp_free_struct (notif_wrp_msg );
274 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"Freed notif_wrp_msg struct.\n");
289 return "LOG.RDK.PARADUSIF";
301 char *webpaCfgFile = NULL;
304 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"Reading Parodus URL...\n");
305 fp = fopen(WEBPA_CONFIG_FILE,
"r");
308 fseek(fp, 0, SEEK_END);
309 ch_count = ftell(fp);
312 RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,
"WebPA config file is Empty %s\n", WEBPA_CONFIG_FILE);
317 fseek(fp, 0, SEEK_SET);
318 webpaCfgFile = (
char *) malloc(
sizeof(
char) * (ch_count + 1));
321 fread(webpaCfgFile, 1, ch_count,fp);
322 webpaCfgFile[ch_count] =
'\0';
325 cJSON *webpa_cfg = cJSON_Parse(webpaCfgFile);
331 pUrl = cJSON_GetObjectItem(webpa_cfg,
"ParodusURL");
332 cUrl = cJSON_GetObjectItem(webpa_cfg,
"ParodusClientURL");
333 if((NULL != pUrl && NULL != cUrl) && (NULL != pUrl->valuestring && NULL != pUrl->valuestring))
335 strncpy(parodus_url,pUrl->valuestring,strlen(pUrl->valuestring));
336 strncpy(client_url,cUrl->valuestring,strlen(pUrl->valuestring));
346 RDK_LOG(RDK_LOG_ERROR,LOG_PARODUS_IF,
"Failed to open Webpa cfg file %s\n", WEBPA_CONFIG_FILE);
351 strncpy(parodus_url,PARODUS_URL,strlen(PARODUS_URL));
352 strncpy(client_url,PAROUDUS_CLIENT_URL,strlen(PAROUDUS_CLIENT_URL));
354 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"Parodus URL formed = %s \n",parodus_url);
355 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"Client URL formed = %s \n",client_url);
363 int backoffRetryTime = 0;
364 int backoff_max_time = 5;
370 pthread_detach(pthread_self());
372 max_retry_sleep = (int) pow(2, backoff_max_time) -1;
373 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"max_retry_sleep is %d\n", max_retry_sleep );
377 libpd_cfg_t cfg1 = {.service_name =
"config",
378 .receive =
true, .keepalive_timeout_secs = 64,
379 .parodus_url = parodus_url,
380 .client_url = client_url
383 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"libparodus_init with parodus url %s and client url %s\n",cfg1.parodus_url,cfg1.client_url);
387 if(backoffRetryTime < max_retry_sleep)
389 backoffRetryTime = (int) pow(2, c) -1;
392 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"New backoffRetryTime value calculated as %d seconds\n", backoffRetryTime);
393 int ret =libparodus_init (&libparodus_instance, &cfg1);
396 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"Init for parodus Success..!!\n");
397 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"WebPA is now ready to process requests\n");
399 const char* webpa_start_tm_file =
"/tmp/webpa/start_time";
401 if(access(webpa_start_tm_file, F_OK) !=0)
403 FILE *fp = fopen(webpa_start_tm_file,
"w");
405 fprintf(fp,
"%ld", time(NULL));
413 sleep(backoffRetryTime);
416 if(backoffRetryTime == max_retry_sleep)
419 backoffRetryTime = 0;
420 RDK_LOG(RDK_LOG_INFO,LOG_PARODUS_IF,
"backoffRetryTime reached max value, reseting to initial value\n");
423 if(libparodus_instance)
425 retval = libparodus_shutdown(&libparodus_instance);
426 RDK_LOG(RDK_LOG_DEBUG,LOG_PARODUS_IF,
"libparodus_shutdown retval %d\n", retval);
432 static long timeValDiff(
struct timespec *starttime,
struct timespec *finishtime)
435 msec=(finishtime->tv_sec-starttime->tv_sec)*1000;
436 msec+=(finishtime->tv_nsec-starttime->tv_nsec)/1000000;