Skip to content

Commit

Permalink
mi_script: Add support for array parameters
Browse files Browse the repository at this point in the history
(cherry picked from commit fb686b8)
  • Loading branch information
liviuchircu committed Jan 26, 2023
1 parent 5e4c0b9 commit 11dc11e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
22 changes: 21 additions & 1 deletion modules/mi_script/doc/mi_script_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,15 @@ modparam("mi_stream", "trace_bwlist", "w: sip_trace")
only makes sense if the <emphasis>params_avp</emphasis>
is set, and has to contain the same number
of values as there are parameters.
</para></listitem>
</para>
<para>
To specify <emphasis>array values</emphasis>, enclose your
space-separated array elements in the <emphasis>__array()</emphasis>
pseudo-function call. For example:
<emphasis>"__array(HEARTBEAT BACKGROUND_JOB)"</emphasis>

</para>
</listitem>
</itemizedlist>
</para>
<example>
Expand Down Expand Up @@ -290,6 +298,18 @@ $avp(params) = "from_tag";
$avp(vals) = "SEARCH_FOR_THIS_FROM_TAG";
mi("dlg_list", $var(dlg), $avp(params), $avp(vals));
...
</programlisting>
</example>
<example>
<title><function>mi</function> without return, with an array parameter value</title>
<programlisting format="linespecific">
...
$avp(params) = "freeswitch_url";
$avp(vals) = "fs://:ClueCon@192.168.20.8:8021";
$avp(params) = "events";
$avp(vals) = "__array(HEARTBEAT BACKGROUND_JOB)";
mi("fs_subscribe", , $avp(params), $avp(vals));
...
</programlisting>
</example>
</section>
Expand Down
37 changes: 34 additions & 3 deletions modules/mi_script/mi_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,41 @@ static mi_request_t *mi_script_parse_request(str *method, str *params,
LM_ERR("missing attribute\n");
goto error;
}
if (a_avp->flags & AVP_VAL_STR)
val = cJSON_CreateStr(avp_val_v.s.s, avp_val_v.s.len);
else
if (a_avp->flags & AVP_VAL_STR) {
if (avp_val_v.s.len >= strlen("__array()")
&& avp_val_v.s.s[avp_val_v.s.len-1] == ')'
&& !memcmp(avp_val_v.s.s, STR_L("__array("))) {

val = cJSON_CreateArray();
if (!val) {
LM_ERR("oom\n");
goto error;
}

char *p = avp_val_v.s.s + strlen("__array("),
*end = avp_val_v.s.s + avp_val_v.s.len - 1, *arg;
do {
cJSON *arr_val;

for (arg = p; p < end && *p != ' '; p++) ;

arr_val = cJSON_CreateStr(arg, p - arg);
if (!arr_val) {
cJSON_Delete(val);
LM_ERR("oom\n");
goto error;
}

cJSON_AddItemToArray(val, arr_val);
} while (++p < end);

} else {
val = cJSON_CreateStr(avp_val_v.s.s, avp_val_v.s.len);
}
} else {
val = cJSON_CreateNumber(avp_val_v.n);
}

/* avp is always null terminated */
cJSON_AddItemToObject(req->params, avp_val.s.s, val);
} else {
Expand Down

0 comments on commit 11dc11e

Please sign in to comment.