Skip to content
Snippets Groups Projects
utils.c 16.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    /*
    *	This program is free software: you can redistribute it and/or modify
    *	it under the terms of the GNU General Public License as published by
    *	the Free Software Foundation, either version 2 of the License, or
    *	(at your option) any later version.
    *
    *	Copyright (C) 2022 iopsys Software Solutions AB
    *		Author: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
    *
    */
    
    #include <curl/curl.h>
    #include <libubus.h>
    #include <libubox/blob.h>
    #include <libubox/blobmsg.h>
    #include <libubox/blobmsg_json.h>
    #include "utils.h"
    
    #define USP_OBJECT_NAME "usp.raw"
    
    static unsigned char LogLevel = DEFAULT_LOG_LEVEL;
    static bool g_usp_object_available = false;
    
    void set_log_level(unsigned char level)
    {
    	LogLevel = level;
    }
    
    void print_error(const char *format, ...)
    {
    	va_list arglist;
    
    	if (LogLevel < 1)
    		return;
    
    	va_start(arglist, format);
    	vsyslog(LOG_ERR, format, arglist);
    	va_end(arglist);
    }
    
    void print_warning(const char *format, ...)
    {
    	va_list arglist;
    
    	if (LogLevel < 2)
    		return;
    
    	va_start(arglist, format);
    	vsyslog(LOG_WARNING, format, arglist);
    	va_end(arglist);
    }
    
    void print_info(const char *format, ...)
    {
    	va_list arglist;
    
    	if (LogLevel < 3)
    		return;
    
    	va_start(arglist, format);
    	vsyslog(LOG_INFO, format, arglist);
    	va_end(arglist);
    }
    
    void print_debug(const char *format, ...)
    {
    	va_list arglist;
    
    	if (LogLevel < 4)
    		return;
    
    	va_start(arglist, format);
    	vsyslog(LOG_DEBUG, format, arglist);
    	va_end(arglist);
    }
    
    const char *bulkdata_get_time(void)
    {
    	static char local_time[64];
    
    	time_t t_time = time(NULL);
    	struct tm *t_tm = localtime(&t_time);
    	if (t_tm == NULL)
    		return NULL;
    
    	if (strftime(local_time, sizeof(local_time),"Date: %a, %d %b %Y %X%z GMT", t_tm) == 0)
    		return NULL;
    
    	return local_time;
    }
    
    void get_time_stamp(const char *format, char **timestamp)
    {
    	time_t now = time(NULL);
    
    	if (strcmp(format, "unix") == 0) {
    		asprintf(timestamp, "%lld", (long long int)now);
    	} else if (strcmp(format, "iso8601") == 0) {
    		char buf[32] = {0};
    		struct tm *ts = localtime(&now);
    		strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%Z", ts);
    		asprintf(timestamp, "%s", buf);
    	} else
    		*timestamp = NULL;
    }
    
    unsigned int get_next_period(time_t time_reference, int reporting_interval)
    {
    	unsigned int next_period;
    	time_t now = time(NULL);
    
    	if (now > time_reference)
    		next_period = reporting_interval - ((now - time_reference) % reporting_interval);
    	else
    		next_period = (time_reference - now) % reporting_interval;
    
    	if (next_period == 0)
    		next_period = reporting_interval;
    
    	return next_period;
    }
    
    static void bulkdata_add_data_to_list(struct list_head *dup_list, char *name, char *value, char *type)
    {
    	param_data_t *link = calloc(1, sizeof(param_data_t));
    
    	list_add_tail(&link->list, dup_list);
    	link->name = strdup(name);
    	link->data = strdup(value);
    	link->type = strdup(type);
    }
    
    void bulkdata_free_data_from_list(struct list_head *dup_list)
    {
    	param_data_t *link = NULL;
    
    	while (dup_list->next != dup_list) {
    		link = list_entry(dup_list->next, param_data_t, list);
    		list_del(&link->list);
    		FREE(link->name);
    		FREE(link->data);
    		FREE(link->type);
    		FREE(link);
    	}
    }
    
    static int detect_fault(struct blob_attr *msg)
    {
    	int fault = 0;
    	enum {
    		FAULT_ID,
    		__FAULT_MAX
    	};
    	struct blob_attr *f_attr[__FAULT_MAX] = {NULL};
    	const struct blobmsg_policy fp[__FAULT_MAX] = {
    		{ "fault", BLOBMSG_TYPE_INT32 }
    	};
    
    	blobmsg_parse(fp, __FAULT_MAX, f_attr, blobmsg_data(msg), blobmsg_len(msg));
    
    	if (f_attr[FAULT_ID])
    		fault = blobmsg_get_u32(f_attr[FAULT_ID]);
    
    	return fault;
    }
    
    static struct blob_attr *get_parameters(struct blob_attr *msg)
    {
    	struct blob_attr *params = NULL;
    	struct blob_attr *cur;
    	int rem;
    
    	blobmsg_for_each_attr(cur, msg, rem) {
    		if (blobmsg_type(cur) == BLOBMSG_TYPE_ARRAY) {
    			params = cur;
    			break;
    		}
    	}
    
    	return params;
    }
    
    static void get_value_single_cb(struct ubus_request *req, int type __attribute__((unused)), struct blob_attr *msg)
    {
    	 vendor_data_t *arg = req->priv;
    	struct blob_attr *params;
    	struct blob_attr *cur;
    	size_t rem;
    	enum {
    		P_PARAM,
    		P_VALUE,
    		__P_MAX
    	};
    	const struct blobmsg_policy p[__P_MAX] = {
    		{ "parameter", BLOBMSG_TYPE_STRING },
    		{ "value", BLOBMSG_TYPE_STRING }
    	};
    
    	if (!msg)
    		return;
    
    	if (detect_fault(msg))
    		return;
    
    	params = get_parameters(msg);
    	if (params == NULL)
    		return;
    
    	blobmsg_for_each_attr(cur, params, rem) {
    		struct blob_attr *tb[__P_MAX] = {NULL, NULL};
    		char *path, *val;
    
    		blobmsg_parse(p, __P_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
    
    		if (!tb[P_PARAM] || !tb[P_VALUE])
    			continue;
    
    		path = blobmsg_get_string(tb[P_PARAM]);
    		val = blobmsg_get_string(tb[P_VALUE]);
    
    		if (strcmp(path, arg->path) == 0) {
    			arg->p_value = strdup(val);
    			break;
    		}
    	}
    }
    
    static void get_value_group_cb(struct ubus_request *req, int type __attribute__((unused)), struct blob_attr *msg)
    {
    	vendor_data_t *arg = req->priv;
    	struct blob_attr *params;
    	struct blob_attr *cur;
    	size_t rem;
    	enum {
    		P_PARAM,
    		P_VALUE,
    		P_TYPE,
    		__P_MAX
    	};
    	const struct blobmsg_policy p[__P_MAX] = {
    		{ "parameter", BLOBMSG_TYPE_STRING },
    		{ "value", BLOBMSG_TYPE_STRING },
    		{ "type", BLOBMSG_TYPE_STRING }
    	};
    
    
    	if (!msg)
    		return;
    
    	if (detect_fault(msg))
    		return;
    
    	params = get_parameters(msg);
    	if (params == NULL)
    		return;
    
    	blobmsg_for_each_attr(cur, params, rem) {
    		struct blob_attr *tb[__P_MAX] = {NULL, NULL};
    		char *path, *val, *ptype;
    
    		blobmsg_parse(p, __P_MAX, tb, blobmsg_data(cur), blobmsg_len(cur));
    
    		if (!tb[P_PARAM] || !tb[P_VALUE] || !tb[P_TYPE])
    			continue;
    
    		path = blobmsg_get_string(tb[P_PARAM]);
    		val = blobmsg_get_string(tb[P_VALUE]);
    		ptype = blobmsg_get_string(tb[P_TYPE]);
    
    		bulkdata_add_data_to_list(arg->list, path, val, ptype);
    	}
    }
    
    static int uspd_call(struct ubus_context *ctx, char *method,
    		     struct blob_buf *data, ubus_data_handler_t callback,
    		     vendor_data_t *arg)
    {
    #define UBUS_TIMEOUT 5
    
    	uint32_t id;
    
    	if (ubus_lookup_id(ctx, USP_OBJECT_NAME, &id))
    		return -1;
    
    	// Invoke Ubus to get data from uspd
    	if (ubus_invoke(ctx, id, method, data->head, callback, arg, UBUS_TIMEOUT * 1000))
    		return -1;
    
    	return 0;
    }
    
    int get_value_single(vendor_data_t *arg)
    {
    	struct ubus_context *ubus_ctx = NULL;
    	struct blob_buf bb = {0};
    	int res = 0;
    
    	ubus_ctx = ubus_connect(NULL);
    	if (ubus_ctx == NULL)
    		return -1;
    
    	memset(&bb, 0, sizeof(struct blob_buf));
    	blob_buf_init(&bb, 0);
    
    	blobmsg_add_string(&bb, "path", arg->path);
    	blobmsg_add_string(&bb, "proto", "usp");
    
    	// Invoke Ubus to get data from uspd
    	res = uspd_call(ubus_ctx, "get", &bb, get_value_single_cb, arg);
    
    	blob_buf_free(&bb);
    	ubus_free(ubus_ctx);
    	return res;
    }
    
    int get_value_group(vendor_data_t *arg)
    {
    	struct ubus_context *ubus_ctx = NULL;
    	struct blob_buf bb = {0};
    	int res = 0;
    
    	ubus_ctx = ubus_connect(NULL);
    	if (ubus_ctx == NULL)
    		return -1;
    
    	memset(&bb, 0, sizeof(struct blob_buf));
    	blob_buf_init(&bb, 0);
    
    	blobmsg_add_string(&bb, "path", arg->path);
    	blobmsg_add_string(&bb, "proto", "usp");
    
    	// Invoke Ubus to get data from uspd
    	res = uspd_call(ubus_ctx, "get", &bb, get_value_group_cb, arg);
    
    	blob_buf_free(&bb);
    	ubus_free(ubus_ctx);
    	return res;
    }
    
    static void create_request_url(struct profile *profile, char *url, size_t len)
    {
    	int http_uri_number = profile->profile_http_request_uri_parameter_number;
    	char *value = NULL;
    	int res = 0;
    	unsigned pos = 0;
    
    	pos += snprintf(&url[pos], len - pos, "%s", profile->http_url);
    
    	for (int i = 0; i < http_uri_number; i++) {
    
    		struct profile_http_request_uri_parameter *p = &profile->profile_http_uri_parameter[i];
    
    		if (!p->reference ||
    			*(p->reference) == 0 ||
    			!p->name  ||
    			*(p->name) == 0)
    			continue;
    
    
    		vendor_data_t uri_data = { .path = p->reference };
    		res = get_value_single(&uri_data);
    		value = (res == 0) ? uri_data.p_value : strdup("");
    
    		pos += snprintf(&url[pos], len - pos, "&%s=%s", p->name, value);
    		FREE(value);
    	}
    }
    
    int http_send_message(struct profile *profile, char *msg_out)
    {
    #define HTTP_TIMEOUT 20
    
    	char url[1024] = {0};
    	struct curl_slist *header_list = NULL;
    	CURLcode res;
    	long http_code = 0;
    	char errbuf[CURL_ERROR_SIZE];
    
    	CURL *curl = curl_easy_init();
    	if (!curl)
    		return -1;
    
    	create_request_url(profile, url, sizeof(url));
    	INFO("ACS url: %s", url);
    
    	header_list = curl_slist_append(header_list, "User-Agent: iopsys-bulkdata");
    	if (!header_list)
    		return -1;
    
    	if (profile->http_use_date_header) {
    		if (bulkdata_get_time() != NULL) {
    			header_list = curl_slist_append(header_list, bulkdata_get_time());
    			if (!header_list) return -1;
    		}
    	}
    
    	if (strcmp(profile->encoding_type, "json") == 0) {
    		header_list = curl_slist_append(header_list, "Content-Type: application/json; charset=\"utf-8\"");
    		if (!header_list) return -1;
    
    		if (strcmp (profile->json_encoding_report_format, "objecthierarchy") == 0) {
    			header_list = curl_slist_append(header_list, "BBF-Report-Format: \"ObjectHierarchy\"");
    			if (!header_list) return -1;
    		} else if (strcmp(profile->json_encoding_report_format, "namevaluepair") == 0) {
    			header_list = curl_slist_append(header_list, "BBF-Report-Format: \"NameValuePair\"");
    			if (!header_list) return -1;
    		}
    	} else if (strcmp(profile->encoding_type, "csv") == 0) {
    		header_list = curl_slist_append(header_list, "Content-Type: text/csv; charset=\"utf-8\"");
    		if (!header_list) return -1;
    
    		if (strcmp (profile->csv_encoding_report_format, "row") == 0) {
    			header_list = curl_slist_append(header_list, "BBF-Report-Format: \"ParameterPerRow\"");
    			if (!header_list) return -1;
    		} else if (strcmp (profile->csv_encoding_report_format, "column") == 0) {
    			header_list = curl_slist_append(header_list, "BBF-Report-Format: \"ParameterPerColumn\"");
    			if (!header_list) return -1;
    		}
    	}
    
    	curl_easy_setopt(curl, CURLOPT_URL, url);
    	curl_easy_setopt(curl, CURLOPT_USERNAME, profile->http_username);
    	curl_easy_setopt(curl, CURLOPT_PASSWORD, profile->http_password);
    	curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC|CURLAUTH_DIGEST);
    	curl_easy_setopt(curl, CURLOPT_TIMEOUT, HTTP_TIMEOUT);
    	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, HTTP_TIMEOUT);
    	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    	curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
    	curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
    
    	if (strcasecmp(profile->http_compression, "gzip") == 0) {
    		curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
    		header_list = curl_slist_append(header_list, "Content-Encoding: gzip");
    	} else if (strcasecmp(profile->http_compression, "compress") == 0) {
    		curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "compress");
    		header_list = curl_slist_append(header_list, "Content-Encoding: compress");
    	} else if (strcasecmp(profile->http_compression, "deflate") == 0) {
    		curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "deflate");
    		header_list = curl_slist_append(header_list, "Content-Encoding: deflate");	
    	}
    
    	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
    	if (strcasecmp(profile->http_method, "put") == 0)
    		curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
    
    	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg_out);
    	curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, msg_out ? (long)strlen(msg_out) : 0);
    
    	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
    
    	INFO("Send the report of profile name '%s' to Bulkdata Collector", profile->profile_name);
    	res = curl_easy_perform(curl);
    
    	if (res != CURLE_OK) {
    		size_t len = strlen(errbuf);
    		if (len) {
    			if (errbuf[len - 1] == '\n') errbuf[len - 1] = '\0';
    			WARNING("libcurl: (%d) %s", res, errbuf);
    		} else {
    			ERR("libcurl: (%d) %s", res, curl_easy_strerror(res));
    		}
    	}
    
    	curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
    	if (http_code == 200)
    		INFO("Receive HTTP 200 OK from Bulkdata Collector");
    	else if (http_code == 401)
    		INFO("Receive HTTP 401 Unauthorized from Bulkdata Collector");
    	else if (http_code == 204)
    		INFO("Receive HTTP 204 No Content from Bulkdata Collector");
    	else
    		INFO("Receive HTTP %ld from Bulkdata Collector", http_code);
    
    	if (http_code != 200 && http_code != 204)
    		goto error;
    
    	curl_easy_cleanup(curl);
    	if (header_list) {
    		curl_slist_free_all(header_list);
    		header_list = NULL;
    	}
    
    	if (res)
    		goto error;
    
    	return http_code;
    
    error:
    	curl_easy_cleanup(curl);
    	if (header_list) {
    		curl_slist_free_all(header_list);
    		header_list = NULL;
    	}
    	return -1;
    }
    
    char *get_bulkdata_profile_parameter_name(char *paramref, char *paramname, char *param)
    {
    	char buf[512] = {0};
    	unsigned pos = 0;
    
    	if (paramname == NULL || strlen(paramname) == 0)
    		return strdup(param);
    	
    	pos = snprintf(buf, sizeof(buf), "%s", paramname);
    	
    	if (!strchr(paramref, '*')) {
    		bool is_same = (paramref[strlen(paramref) - 1] == param[strlen(param) - 1]);
    		snprintf(&buf[pos], sizeof(buf) - pos, "%s%s", !is_same ? "." : "", !is_same ? strstr(param, paramref) + strlen(paramref) : "");
    	} else {
    		char *pch = NULL, *spch = NULL;
    		char *idx1 = NULL, *idx2 = NULL;
    		char paramref_buf[256] = {0};
    		char tmp_buf[64] = {0};
    		char instance[8] = {0};
    		bool first_occur = true;
    		
    		snprintf(paramref_buf, sizeof(paramref_buf), "%s", paramref);
    	
    		for (pch = strtok_r(paramref_buf, "*", &spch); pch != NULL; pch = strtok_r(NULL, "*", &spch)) {
    		
    			if (first_occur) {
    				first_occur = false;
    				STRNCPY(tmp_buf, pch, sizeof(tmp_buf));
    				continue;
    			}
    
    			idx1 = strstr(param, tmp_buf);
    			idx2 = strstr(param, pch);
    
    			STRNCPY(instance, idx1 + strlen(tmp_buf), idx2 - idx1 - strlen(tmp_buf) + 1);
    			
    			pos += snprintf(&buf[pos], sizeof(buf) - pos, ".%s", instance);
    			STRNCPY(tmp_buf, pch, sizeof(tmp_buf));
    		}
    
    		bool is_same = (tmp_buf[strlen(tmp_buf) - 1] == param[strlen(param) - 1]);
    		snprintf(&buf[pos], sizeof(buf) - pos, "%s%s", !is_same ? "." : "", !is_same ? strstr(param, tmp_buf) + strlen(tmp_buf) : "");
    	}
    		
    	return strdup(buf);
    }
    
    void append_string_to_string(char *strappend, char **target)
    {
    	char *tmp = NULL;
    
    	if (strappend == NULL || strlen(strappend) == 0)
    		return;
    
    	if (*target == NULL || strlen(*target) == 0) {
    		*target = strdup(strappend);
    		return;
    	} else {
    		tmp = strdup(*target);
    		FREE(*target);
    	}
    	asprintf(target, "%s%s", tmp, strappend);
    	FREE(tmp);
    }
    
    void bulkdata_add_failed_report(struct profile *profile, char *freport)
    {
    	if (profile->nbre_failed_reports < profile->nbre_of_retained_failed_reports || profile->nbre_of_retained_failed_reports < 0) {
    		profile->nbre_failed_reports++;
    	} else {
    		struct failed_reports *retreport = NULL, *rtmp = NULL;
    
    		list_for_each_entry_safe(retreport, rtmp, profile->failed_reports, list) {
    			//bulkdata_delete_failed_report(retreport);
    			list_del(&retreport->list);
    			FREE(retreport->freport);
    			FREE(retreport);
    			break;
    		}
    	}
    
    	struct failed_reports *report = calloc(1, sizeof(struct failed_reports));
    	list_add_tail(&report->list, profile->failed_reports);
    	report->freport = strdup(freport);
    }
    
    struct failed_reports* empty_failed_reports_list(struct profile *profile)
    {
    	struct failed_reports *report = NULL, *rtmp = NULL;
    
    	if (list_empty(profile->failed_reports))
    		return NULL;
    
    	list_for_each_entry_safe(report, rtmp, profile->failed_reports, list) {
    		list_del(&report->list);
    		FREE(report->freport);
    		FREE(report);
    	}
    	return NULL;
    }
    
    void add_failed_reports_to_report_csv(struct profile *profile, char **report, int isnext)
    {
    	struct failed_reports *retreport = NULL;
    	bool j = false;
    
    	if (list_empty(profile->failed_reports))
    		return;
    	list_for_each_entry(retreport, profile->failed_reports, list) {
    		if (!j && isnext) {
    			j = true;
    			continue;
    		}
    		append_string_to_string(retreport->freport, report);
    	}
    }
    
    static void lookup_event_cb(struct ubus_context *ctx __attribute__((unused)),
    		struct ubus_event_handler *ev __attribute__((unused)),
    		const char *type, struct blob_attr *msg)
    {
    	const struct blobmsg_policy policy = {
    		"path", BLOBMSG_TYPE_STRING
    	};
    	struct blob_attr *attr;
    	const char *path;
    
    	if (strcmp(type, "ubus.object.add") != 0)
    		return;
    
    	blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
    	if (!attr)
    		return;
    
    	path = blobmsg_data(attr);
    	if (strcmp(path, USP_OBJECT_NAME) == 0) {
    		g_usp_object_available = true;
    		uloop_end();
    	}
    }
    
    static void lookup_timeout_cb(struct uloop_timeout *timeout __attribute__((unused)))
    {
    	uloop_end();
    }
    
    int wait_for_usp_raw_object(void)
    {
    #define USP_RAW_WAIT_TIMEOUT 30
    
    	struct ubus_context *uctx;
    	int ret;
    	uint32_t ubus_id;
    	struct ubus_event_handler add_event;
    	struct uloop_timeout u_timeout;
    
    	g_usp_object_available = false;
    	uctx = ubus_connect(NULL);
    	if (uctx == NULL) {
    		ERR("Can't create ubus context");
    		return -1;
    	}
    
    	uloop_init();
    	ubus_add_uloop(uctx);
    
    	// register for add event
    	memset(&add_event, 0, sizeof(struct ubus_event_handler));
    	add_event.cb = lookup_event_cb;
    	ubus_register_event_handler(uctx, &add_event, "ubus.object.add");
    
    	// check if object already present
    	ret = ubus_lookup_id(uctx, USP_OBJECT_NAME, &ubus_id);
    	if (ret == 0) {
    		g_usp_object_available = true;
    		goto end;
    	}
    
    	// Set timeout to expire lookup
    	memset(&u_timeout, 0, sizeof(struct uloop_timeout));
    	u_timeout.cb = lookup_timeout_cb;
    	uloop_timeout_set(&u_timeout, USP_RAW_WAIT_TIMEOUT * 1000);
    
    	uloop_run();
    	uloop_done();
    
    end:
    	ubus_free(uctx);
    
    	if (g_usp_object_available == false) {
    		ERR("%s object not found", USP_OBJECT_NAME);
    		return -1;
    	}
    
    	return 0;
    }