Skip to content

Commit 90cce18

Browse files
committed
libcami: Add a variadic version of ami_action.
Add and expose a version of ami_action that accepts a va_list. If the user application wants to have its own wrapper around ami_action, then it would be more efficient to pass us a va_list, rather than formatting it there only to have us format it again in ami_action.
1 parent 3662256 commit 90cce18

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

cami.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,12 +1135,11 @@ static int __attribute__ ((format (printf, 3, 4))) ami_send(struct ami_session *
11351135
return res;
11361136
}
11371137

1138-
struct ami_response * __attribute__ ((format (printf, 3, 4))) ami_action(struct ami_session *ami, const char *action, const char *fmt, ...)
1138+
struct ami_response * __attribute__ ((format (printf, 3, 0))) ami_action_va(struct ami_session *ami, const char *action, const char *fmt, va_list ap)
11391139
{
11401140
struct ami_response *resp = NULL;
11411141
/* Remember: no trailing \r\n in fmt !*/
11421142
int res, actionid;
1143-
va_list ap;
11441143

11451144
if (ami->ami_socket < 0) {
11461145
/* Connection got shutdown */
@@ -1156,10 +1155,8 @@ struct ami_response * __attribute__ ((format (printf, 3, 4))) ami_action(struct
11561155
/* Nobody sends anything else until we get our response. */
11571156
pthread_mutex_lock(&ami->ami_read_lock);
11581157

1159-
va_start(ap, fmt);
11601158
/* If we don't have a user-supplied format string, don't add \r\n after ActionID or we'll get 3 sets in a row and cause Asterisk to whine. */
11611159
res = __ami_send(ami, ap, fmt, fmt && *fmt ? "Action:%s\r\nActionID:%d\r\n" : "Action:%s\r\nActionID:%d", action, ++ami->ami_msg_id);
1162-
va_end(ap);
11631160

11641161
actionid = ami->ami_msg_id; /* This is the ActionID we expect in our response */
11651162

@@ -1195,6 +1192,17 @@ struct ami_response * __attribute__ ((format (printf, 3, 4))) ami_action(struct
11951192
return resp;
11961193
}
11971194

1195+
struct ami_response * __attribute__ ((format (printf, 3, 4))) ami_action(struct ami_session *ami, const char *action, const char *fmt, ...)
1196+
{
1197+
struct ami_response *resp = NULL;
1198+
va_list ap;
1199+
1200+
va_start(ap, fmt);
1201+
resp = ami_action_va(ami, action, fmt, ap);
1202+
va_end(ap);
1203+
return resp;
1204+
}
1205+
11981206
int ami_action_login(struct ami_session *ami, const char *username, const char *password)
11991207
{
12001208
struct ami_response *resp;

include/cami.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818

1919
#define CAMI_VERSION_MAJOR 0
20-
#define CAMI_VERSION_MINOR 2
21-
#define CAMI_VERSION_PATCH 1
20+
#define CAMI_VERSION_MINOR 3
21+
#define CAMI_VERSION_PATCH 0
2222

2323
/* Max wait time in ms. Don't be tempted to make this too big, as this blocks all AMI traffic. Most of the time, it shouldn't really matter though. */
2424
#define AMI_MAX_WAIT_TIME 1000
@@ -162,6 +162,9 @@ int ami_action_login(struct ami_session *ami, const char *username, const char *
162162
*/
163163
int ami_auto_detect_ami_pass(const char *amiusername, char *buf, size_t buflen);
164164

165+
/*! \brief Same as ami_action, but accepts a va_list, for convenience when calling from a variadic function */
166+
struct ami_response *ami_action_va(struct ami_session *ami, const char *action, const char *fmt, va_list ap);
167+
165168
/*!
166169
* \brief Request a custom AMI action
167170
* \param ami

0 commit comments

Comments
 (0)