Browse code

pv: added functions for xavu params explode/implode

Daniel-Constantin Mierla authored on 24/09/2021 06:32:31
Showing 4 changed files
... ...
@@ -398,6 +398,9 @@ xavp_params_explode("a=b;c=d;e=d", "x");
398 398
 			<para>
399 399
 				Serialize the subfields in an XAVP to a parameters string format.
400 400
 			</para>
401
+			<para>
402
+				Number values are serialized as unsigned integer string format.
403
+			</para>
401 404
 			<para>
402 405
 				The first parameter has to be the name of XAVP (only the string
403 406
 				name, not the in $xavp(name)). The second parameter
... ...
@@ -418,6 +421,71 @@ $xavp(x[0]=&gt;c) = "d";
418 421
 $xavp(x[0]=&gt;a) = "b";
419 422
 xavp_params_implode("x", "$var(out)");
420 423
 # results in: $var(out) is "a=b;c=d;e=f;"
424
+...
425
+				</programlisting>
426
+			</example>
427
+		</section>
428
+		<section id="pv.f.xavu_params_explode">
429
+			<title>
430
+				<function moreinfo="none">xavu_params_explode(sparams, xname)</function>
431
+			</title>
432
+			<para>
433
+				Convert a parameters string in xavu atributes.
434
+			</para>
435
+			<para>
436
+				The first parameter has to be a string in the format of SIP header
437
+				parameters (name1=value1;...;nameN=valueN). The second parameter
438
+				is the name of the root xavu to hold the pairs (nameX,valueX).
439
+			</para>
440
+			<para>
441
+				The values are stored as string type.
442
+			</para>
443
+			<para>
444
+			Function can be used from ANY ROUTE.
445
+			</para>
446
+			<example>
447
+				<title><function>xavu_params_explode</function> usage</title>
448
+				<programlisting format="linespecific">
449
+...
450
+xavu_params_explode("a=b;c=d;e=d", "x");
451
+# results in:
452
+#    $xavu(x=&gt;a) = "b";
453
+#    $xavu(x=&gt;c) = "d";
454
+#    $xavu(x=&gt;e) = "f";
455
+...
456
+				</programlisting>
457
+			</example>
458
+		</section>
459
+		<section id="pv.f.xavu_params_implode">
460
+			<title>
461
+				<function moreinfo="none">xavu_params_implode(xname, pvname)</function>
462
+			</title>
463
+			<para>
464
+				Serialize the subfields in an XAUP to a parameters string format.
465
+			</para>
466
+			<para>
467
+				Number values are serialized as unsigned integer string format.
468
+			</para>
469
+			<para>
470
+				The first parameter has to be the name of XAVU (only the string
471
+				name, not the in $xavu(name)). The second parameter
472
+				is the name of output variable (in full name, like $var(output)).
473
+			</para>
474
+			<para>
475
+				The value is stored as string type.
476
+			</para>
477
+			<para>
478
+			Function can be used from ANY ROUTE.
479
+			</para>
480
+			<example>
481
+				<title><function>xavu_params_implode</function> usage</title>
482
+				<programlisting format="linespecific">
483
+...
484
+$xavu(x=&gt;e) = "f";
485
+$xavu(x=&gt;c) = "d";
486
+$xavu(x=&gt;a) = "b";
487
+xavu_params_implode("x", "$var(out)");
488
+# results in: $var(out) is "a=b;c=d;e=f;"
421 489
 ...
422 490
 				</programlisting>
423 491
 			</example>
... ...
@@ -579,6 +579,8 @@ static int w_xavp_copy_dst(sip_msg_t *msg, char *src_name, char *src_idx,
579 579
 		char *dst_name, char *dst_idx);
580 580
 static int w_xavp_params_explode(sip_msg_t *msg, char *pparams, char *pxname);
581 581
 static int w_xavp_params_implode(sip_msg_t *msg, char *pxname, char *pvname);
582
+static int w_xavu_params_explode(sip_msg_t *msg, char *pparams, char *pxname);
583
+static int w_xavu_params_implode(sip_msg_t *msg, char *pxname, char *pvname);
582 584
 static int w_xavp_slist_explode(sip_msg_t *msg, char *pslist, char *psep,
583 585
 		char *pmode, char *pxname);
584 586
 static int w_xavp_child_seti(sip_msg_t *msg, char *prname, char *pcname,
... ...
@@ -633,17 +635,23 @@ static cmd_export_t cmds[]={
633 635
 	{"not_empty", (cmd_function)pv_not_empty, 1, fixup_pvar_null,
634 636
 		fixup_free_pvar_null,
635 637
 		ANY_ROUTE},
638
+	{"xavp_copy", (cmd_function)w_xavp_copy, 3, pv_xavp_copy_fixup, 0,
639
+		ANY_ROUTE},
640
+	{"xavp_copy", (cmd_function)w_xavp_copy_dst, 4, pv_xavp_copy_fixup, 0,
641
+		ANY_ROUTE},
636 642
 	{"xavp_slist_explode", (cmd_function)w_xavp_slist_explode,
637 643
 		4, fixup_spve_all, fixup_free_spve_all,
638 644
 		ANY_ROUTE},
639 645
 	{"xavp_params_explode", (cmd_function)w_xavp_params_explode,
640 646
 		2, fixup_spve_spve, fixup_free_spve_spve,
641 647
 		ANY_ROUTE},
642
-	{"xavp_copy", (cmd_function)w_xavp_copy, 3, pv_xavp_copy_fixup, 0,
648
+	{"xavp_params_implode", (cmd_function)w_xavp_params_implode,
649
+		2, fixup_spve_str, fixup_free_spve_str,
643 650
 		ANY_ROUTE},
644
-	{"xavp_copy", (cmd_function)w_xavp_copy_dst, 4, pv_xavp_copy_fixup, 0,
651
+	{"xavu_params_explode", (cmd_function)w_xavu_params_explode,
652
+		2, fixup_spve_spve, fixup_free_spve_spve,
645 653
 		ANY_ROUTE},
646
-	{"xavp_params_implode", (cmd_function)w_xavp_params_implode,
654
+	{"xavu_params_implode", (cmd_function)w_xavu_params_implode,
647 655
 		2, fixup_spve_str, fixup_free_spve_str,
648 656
 		ANY_ROUTE},
649 657
 	{"xavp_child_seti", (cmd_function)w_xavp_child_seti,
... ...
@@ -1083,6 +1091,40 @@ static int ki_xavp_params_explode(sip_msg_t *msg, str *sparams, str *sxname)
1083 1091
 	return 1;
1084 1092
 }
1085 1093
 
1094
+/**
1095
+ *
1096
+ */
1097
+static int w_xavu_params_explode(sip_msg_t *msg, char *pparams, char *pxname)
1098
+{
1099
+	str sparams;
1100
+	str sxname;
1101
+
1102
+	if(fixup_get_svalue(msg, (gparam_t*)pparams, &sparams)!=0) {
1103
+		LM_ERR("cannot get the params\n");
1104
+		return -1;
1105
+	}
1106
+	if(fixup_get_svalue(msg, (gparam_t*)pxname, &sxname)!=0) {
1107
+		LM_ERR("cannot get the xavp name\n");
1108
+		return -1;
1109
+	}
1110
+
1111
+	if(xavu_params_explode(&sparams, &sxname)<0)
1112
+		return -1;
1113
+
1114
+	return 1;
1115
+}
1116
+
1117
+/**
1118
+ *
1119
+ */
1120
+static int ki_xavu_params_explode(sip_msg_t *msg, str *sparams, str *sxname)
1121
+{
1122
+	if(xavu_params_explode(sparams, sxname)<0)
1123
+		return -1;
1124
+
1125
+	return 1;
1126
+}
1127
+
1086 1128
 /**
1087 1129
  *
1088 1130
  */
... ...
@@ -1140,6 +1182,63 @@ static int w_xavp_params_implode(sip_msg_t *msg, char *pxname, char *pvname)
1140 1182
 	return ki_xavp_params_implode(msg, &sxname, (str*)pvname);
1141 1183
 }
1142 1184
 
1185
+/**
1186
+ *
1187
+ */
1188
+static int ki_xavu_params_implode(sip_msg_t *msg, str *sxname, str *svname)
1189
+{
1190
+	pv_spec_t *vspec=NULL;
1191
+	pv_value_t val;
1192
+
1193
+	if(sxname==NULL || sxname->s==NULL || sxname->len<=0) {
1194
+		LM_ERR("invalid xavp name\n");
1195
+		return -1;
1196
+	}
1197
+	if(svname==NULL || svname->s==NULL || svname->len<=0) {
1198
+		LM_ERR("invalid output var name\n");
1199
+		return -1;
1200
+	}
1201
+
1202
+	vspec = pv_cache_get(svname);
1203
+	if(vspec==NULL) {
1204
+		LM_ERR("cannot get pv spec for [%.*s]\n", svname->len, svname->s);
1205
+		return -1;
1206
+	}
1207
+	if(vspec->setf==NULL) {
1208
+		LM_ERR("read only output variable [%.*s]\n", svname->len, svname->s);
1209
+		return -1;
1210
+	}
1211
+
1212
+	val.rs.s = pv_get_buffer();
1213
+	val.rs.len = xavu_serialize_fields(sxname, val.rs.s, pv_get_buffer_size());
1214
+	if(val.rs.len<=0) {
1215
+		return -1;
1216
+	}
1217
+
1218
+	val.flags = PV_VAL_STR;
1219
+	if(vspec->setf(msg, &vspec->pvp, EQ_T, &val)<0) {
1220
+		LM_ERR("setting PV failed [%.*s]\n", svname->len, svname->s);
1221
+		return -1;
1222
+	}
1223
+
1224
+	return 1;
1225
+}
1226
+
1227
+/**
1228
+ *
1229
+ */
1230
+static int w_xavu_params_implode(sip_msg_t *msg, char *pxname, char *pvname)
1231
+{
1232
+	str sxname;
1233
+
1234
+	if(fixup_get_svalue(msg, (gparam_t*)pxname, &sxname)!=0) {
1235
+		LM_ERR("cannot get the xavp name\n");
1236
+		return -1;
1237
+	}
1238
+
1239
+	return ki_xavu_params_implode(msg, &sxname, (str*)pvname);
1240
+}
1241
+
1143 1242
 /**
1144 1243
  *
1145 1244
  */
... ...
@@ -2763,6 +2862,16 @@ static sr_kemi_t sr_kemi_pvx_exports[] = {
2763 2862
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
2764 2863
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2765 2864
 	},
2865
+	{ str_init("pvx"), str_init("xavu_params_explode"),
2866
+		SR_KEMIP_INT, ki_xavu_params_explode,
2867
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
2868
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2869
+	},
2870
+	{ str_init("pvx"), str_init("xavu_params_implode"),
2871
+		SR_KEMIP_INT, ki_xavu_params_implode,
2872
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
2873
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2874
+	},
2766 2875
 	{ str_init("pvx"), str_init("xavp_slist_explode"),
2767 2876
 		SR_KEMIP_INT, ki_xavp_slist_explode,
2768 2877
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
... ...
@@ -741,6 +741,64 @@ int xavp_params_explode(str *params, str *xname)
741 741
 	return 0;
742 742
 }
743 743
 
744
+/**
745
+ *
746
+ */
747
+int xavu_params_explode(str *params, str *xname)
748
+{
749
+	param_t* params_list = NULL;
750
+	param_hooks_t phooks;
751
+	param_t *pit=NULL;
752
+	str s;
753
+	sr_xavp_t *xavp=NULL;
754
+	sr_xval_t xval;
755
+
756
+	if(params==NULL || xname==NULL || params->s==NULL || xname->s==NULL
757
+			|| params->len<=0 || xname->len<=0)
758
+	{
759
+		LM_ERR("invalid parameters\n");
760
+		return -1;
761
+	}
762
+
763
+	s.s = params->s;
764
+	s.len = params->len;
765
+	if(s.s[s.len-1]==';')
766
+		s.len--;
767
+	if (parse_params(&s, CLASS_ANY, &phooks, &params_list)<0) {
768
+		LM_DBG("invalid formatted values [%.*s]\n", params->len, params->s);
769
+		return -1;
770
+	}
771
+
772
+	if(params_list==NULL) {
773
+		return -1;
774
+	}
775
+
776
+
777
+	for (pit = params_list; pit; pit=pit->next)
778
+	{
779
+		memset(&xval, 0, sizeof(sr_xval_t));
780
+		xval.type = SR_XTYPE_STR;
781
+		xval.v.s = pit->body;
782
+		if(xavu_set_xval(&pit->name, &xval, &xavp)==NULL) {
783
+			free_params(params_list);
784
+			xavu_destroy_list(&xavp);
785
+			return -1;
786
+		}
787
+	}
788
+	free_params(params_list);
789
+
790
+	/* add main xavp in root list */
791
+	memset(&xval, 0, sizeof(sr_xval_t));
792
+	xval.type = SR_XTYPE_XAVP;
793
+	xval.v.xavp = xavp;
794
+	if(xavu_set_xval(xname, &xval, NULL)==NULL) {
795
+		xavu_destroy_list(&xavp);
796
+		return -1;
797
+	}
798
+
799
+	return 0;
800
+}
801
+
744 802
 int pv_var_to_xavp(str *varname, str *xname)
745 803
 {
746 804
 	script_var_t *it;
... ...
@@ -44,6 +44,7 @@ int pv_xavi_print(struct sip_msg* msg, char* s1, char *s2);
44 44
 
45 45
 int xavp_slist_explode(str *slist, str *sep, str *mode, str *xname);
46 46
 int xavp_params_explode(str *params, str *xname);
47
+int xavu_params_explode(str *params, str *xname);
47 48
 
48 49
 int pv_var_to_xavp(str *varname, str *xname);
49 50
 int pv_xavp_to_var(str *xname);