[asterisk-commits] dlee: trunk r384879 - in /trunk: apps/ include/asterisk/ main/ res/ tests/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Apr 8 08:27:49 CDT 2013
Author: dlee
Date: Mon Apr 8 08:27:45 2013
New Revision: 384879
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=384879
Log:
Stasis application WebSocket support
This is the API that binds the Stasis dialplan application to external
Stasis applications. It also adds the beginnings of WebSocket
application support.
This module registers a dialplan function named Stasis, which is used
to put a channel into the named Stasis app. As a channel enters and
leaves the Stasis diaplan applcation, the Stasis app receives a
'stasis-start' and 'stasis-end' events.
Stasis apps register themselves using the stasis_app_register and
stasis_app_unregister functions. Messages are sent to an appliction
using stasis_app_send.
Finally, Stasis apps control channels through the use of the
stasis_app_control object, and the family of stasis_app_control_*
functions.
Other changes along for the ride are:
* An ast_frame_dtor function that's RAII_VAR safe
* Some common JSON encoders for name/number, timeval, and
context/extension/priority
Added:
trunk/apps/app_stasis.c
- copied unchanged from r384878, team/dlee/stasis-app/apps/app_stasis.c
trunk/apps/app_stasis.exports.in
- copied unchanged from r384878, team/dlee/stasis-app/apps/app_stasis.exports.in
trunk/apps/stasis_json.c
- copied unchanged from r384878, team/dlee/stasis-app/apps/stasis_json.c
trunk/include/asterisk/app_stasis.h
- copied unchanged from r384878, team/dlee/stasis-app/include/asterisk/app_stasis.h
trunk/res/res_stasis_websocket.c
- copied unchanged from r384878, team/dlee/stasis-app/res/res_stasis_websocket.c
trunk/tests/test_app_stasis.c
- copied unchanged from r384878, team/dlee/stasis-app/tests/test_app_stasis.c
Modified:
trunk/apps/Makefile
trunk/include/asterisk/frame.h
trunk/include/asterisk/json.h
trunk/include/asterisk/localtime.h
trunk/main/frame.c
trunk/main/json.c
trunk/tests/test_abstract_jb.c
trunk/tests/test_json.c
Modified: trunk/apps/Makefile
URL: http://svnview.digium.com/svn/asterisk/trunk/apps/Makefile?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/apps/Makefile (original)
+++ trunk/apps/Makefile Mon Apr 8 08:27:45 2013
@@ -38,3 +38,4 @@
LIBS+= -lres_smdi.so
endif
+app_stasis.so: stasis_json.o
Modified: trunk/include/asterisk/frame.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/frame.h?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/include/asterisk/frame.h (original)
+++ trunk/include/asterisk/frame.h Mon Apr 8 08:27:45 2013
@@ -39,11 +39,11 @@
/*!
* \page Def_Frame AST Multimedia and signalling frames
* \section Def_AstFrame What is an ast_frame ?
- * A frame of data read used to communicate between
+ * A frame of data read used to communicate between
* between channels and applications.
* Frames are divided into frame types and subclasses.
*
- * \par Frame types
+ * \par Frame types
* \arg \b VOICE: Voice data, subclass is codec (AST_FORMAT_*)
* \arg \b VIDEO: Video data, subclass is codec (AST_FORMAT_*)
* \arg \b DTMF: A DTMF digit, subclass is the digit
@@ -88,7 +88,7 @@
*/
/*!
- * \brief Frame types
+ * \brief Frame types
*
* \note It is important that the values of each frame type are never changed,
* because it will break backwards compatability with older versions.
@@ -113,11 +113,11 @@
AST_FRAME_IMAGE,
/*! HTML Frame */
AST_FRAME_HTML,
- /*! Comfort Noise frame (subclass is level of CNG in -dBov),
+ /*! Comfort Noise frame (subclass is level of CNG in -dBov),
body may include zero or more 8-bit quantization coefficients */
AST_FRAME_CNG,
/*! Modem-over-IP data streams */
- AST_FRAME_MODEM,
+ AST_FRAME_MODEM,
/*! DTMF begin event, subclass is the digit */
AST_FRAME_DTMF_BEGIN,
};
@@ -137,24 +137,24 @@
*/
struct ast_frame {
/*! Kind of frame */
- enum ast_frame_type frametype;
+ enum ast_frame_type frametype;
/*! Subclass, frame dependent */
union ast_frame_subclass subclass;
/*! Length of data */
- int datalen;
+ int datalen;
/*! Number of samples in this frame */
- int samples;
+ int samples;
/*! Was the data malloc'd? i.e. should we free it when we discard the frame? */
- int mallocd;
+ int mallocd;
/*! The number of bytes allocated for a malloc'd frame header */
size_t mallocd_hdr_len;
/*! How many bytes exist _before_ "data" that can be used if needed */
- int offset;
+ int offset;
/*! Optional source of frame for debugging */
- const char *src;
+ const char *src;
/*! Pointer to actual data */
union { void *ptr; uint32_t uint32; char pad[8]; } data;
- /*! Global delivery time */
+ /*! Global delivery time */
struct timeval delivery;
/*! For placing in a linked list */
AST_LIST_ENTRY(ast_frame) frame_list;
@@ -197,7 +197,7 @@
* RTP header information into the space provided by AST_FRIENDLY_OFFSET instead
* of having to create a new buffer with the necessary space allocated.
*/
-#define AST_FRIENDLY_OFFSET 64
+#define AST_FRIENDLY_OFFSET 64
#define AST_MIN_OFFSET 32 /*! Make sure we keep at least this much handy */
/*! Need the header be free'd? */
@@ -353,10 +353,10 @@
#define AST_OPTION_FLAG_ANSWER 5
#define AST_OPTION_FLAG_WTF 6
-/*! Verify touchtones by muting audio transmission
+/*! Verify touchtones by muting audio transmission
* (and reception) and verify the tone is still present
* Option data is a single signed char value 0 or 1 */
-#define AST_OPTION_TONE_VERIFY 1
+#define AST_OPTION_TONE_VERIFY 1
/*! Put a compatible channel into TDD (TTY for the hearing-impared) mode
* Option data is a single signed char value 0 or 1 */
@@ -370,7 +370,7 @@
* Option data is a single signed char value 0 or 1 */
#define AST_OPTION_AUDIO_MODE 4
-/*! Set channel transmit gain
+/*! Set channel transmit gain
* Option data is a single signed char representing number of decibels (dB)
* to set gain to (on top of any gain specified in channel driver) */
#define AST_OPTION_TXGAIN 5
@@ -380,7 +380,7 @@
* to set gain to (on top of any gain specified in channel driver) */
#define AST_OPTION_RXGAIN 6
-/* set channel into "Operator Services" mode
+/* set channel into "Operator Services" mode
* Option data is a struct oprmode
*
* \note This option should never be sent over the network */
@@ -433,7 +433,7 @@
* Option data is a character buffer of suitable length */
#define AST_OPTION_DEVICE_NAME 16
-/*! Get the CC agent type from the channel (Read only)
+/*! Get the CC agent type from the channel (Read only)
* Option data is a character buffer of suitable length */
#define AST_OPTION_CC_AGENT_TYPE 17
@@ -450,12 +450,12 @@
struct ast_option_header {
/* Always keep in network byte order */
#if __BYTE_ORDER == __BIG_ENDIAN
- uint16_t flag:3;
- uint16_t option:13;
+ uint16_t flag:3;
+ uint16_t option:13;
#else
#if __BYTE_ORDER == __LITTLE_ENDIAN
- uint16_t option:13;
- uint16_t flag:3;
+ uint16_t option:13;
+ uint16_t flag:3;
#else
#error Byte order not defined
#endif
@@ -463,25 +463,31 @@
uint8_t data[0];
};
-/*! \brief Requests a frame to be allocated
- *
- * \param source
- * Request a frame be allocated. source is an optional source of the frame,
- * len is the requested length, or "0" if the caller will supply the buffer
+/*! \brief Requests a frame to be allocated
+ *
+ * \param source
+ * Request a frame be allocated. source is an optional source of the frame,
+ * len is the requested length, or "0" if the caller will supply the buffer
*/
#if 0 /* Unimplemented */
struct ast_frame *ast_fralloc(char *source, int len);
#endif
-/*!
+/*!
* \brief Frees a frame or list of frames
- *
+ *
* \param fr Frame to free, or head of list to free
* \param cache Whether to consider this frame for frame caching
*/
void ast_frame_free(struct ast_frame *fr, int cache);
#define ast_frfree(fr) ast_frame_free(fr, 1)
+
+/*!
+ * \brief NULL-safe wrapper for \ref ast_frfree, good for \ref RAII_VAR.
+ * \param frame Frame to free, or head of list to free.
+ */
+void ast_frame_dtor(struct ast_frame *frame);
/*! \brief Makes a frame independent of any static storage
* \param fr frame to act upon
@@ -498,7 +504,7 @@
*/
struct ast_frame *ast_frisolate(struct ast_frame *fr);
-/*! \brief Copies a frame
+/*! \brief Copies a frame
* \param fr frame to copy
* Duplicates a frame -- should only rarely be used, typically frisolate is good enough
* \return Returns a frame on success, NULL on error
@@ -507,7 +513,7 @@
void ast_swapcopy_samples(void *dst, const void *src, int samples);
-/* Helpers for byteswapping native samples to/from
+/* Helpers for byteswapping native samples to/from
little-endian and big-endian. */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define ast_frame_byteswap_le(fr) do { ; } while(0)
@@ -518,13 +524,13 @@
#endif
/*! \brief Parse an "allow" or "deny" line in a channel or device configuration
- and update the capabilities and pref if provided.
+ and update the capabilities and pref if provided.
Video codecs are not added to codec preference lists, since we can not transcode
\return Returns number of errors encountered during parsing
*/
int ast_parse_allow_disallow(struct ast_codec_pref *pref, struct ast_format_cap *cap, const char *list, int allowing);
-/*! \name AST_Smoother
+/*! \name AST_Smoother
*/
/*@{ */
/*! \page ast_smooth The AST Frame Smoother
@@ -584,7 +590,7 @@
/*! \brief Gets duration in ms of interpolation frame for a format */
static inline int ast_codec_interp_len(struct ast_format *format)
-{
+{
return (format->id == AST_FORMAT_ILBC) ? 30 : 20;
}
Modified: trunk/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/json.h?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/include/asterisk/json.h (original)
+++ trunk/include/asterisk/json.h Mon Apr 8 08:27:45 2013
@@ -764,4 +764,48 @@
/*!@}*/
+/*!@{*/
+
+/*!
+ * \brief Common JSON rendering functions for common 'objects'.
+ */
+
+/*!
+ * \brief Simple name/number pair.
+ * \param name Name
+ * \param number Number
+ * \return NULL if error (non-UTF8 characters, NULL inputs, etc.)
+ * \return JSON object with name and number fields
+ */
+struct ast_json *ast_json_name_number(const char *name, const char *number);
+
+/*!
+ * \brief Construct a timeval as JSON.
+ *
+ * JSON does not define a standard date format (boo), but the de facto standard
+ * is to use ISO 8601 formatted string. We build a millisecond resolution string
+ * from the \c timeval
+ *
+ * \param tv \c timeval to encode.
+ * \param zone Text string of a standard system zoneinfo file. If NULL, the system localtime will be used.
+ * \return JSON string with ISO 8601 formatted date/time.
+ * \return \c NULL on error.
+ */
+struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone);
+
+/*!
+ * \brief Construct a context/exten/priority as JSON.
+ *
+ * If a \c NULL is passed for \c context or \c exten, or -1 for \c priority,
+ * the fields is set to ast_json_null().
+ *
+ * \param context Context name.
+ * \param exten Extension.
+ * \param priority Dialplan priority.
+ * \return JSON object with \c context, \c exten and \c priority fields
+ */
+struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority);
+
+/*!@}*/
+
#endif /* _ASTERISK_JSON_H */
Modified: trunk/include/asterisk/localtime.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/localtime.h?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/include/asterisk/localtime.h (original)
+++ trunk/include/asterisk/localtime.h Mon Apr 8 08:27:45 2013
@@ -99,4 +99,9 @@
struct ast_test;
void ast_localtime_wakeup_monitor(struct ast_test *info);
+/*! \brief ast_strftime for ISO8601 formatting timestamps. */
+#define AST_ISO8601_FORMAT "%FT%T.%q%z"
+/*! \brief Max length of an null terminated, millisecond resolution, ISO8601 timestamp string. */
+#define AST_ISO8601_LEN 29
+
#endif /* _ASTERISK_LOCALTIME_H */
Modified: trunk/main/frame.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/frame.c?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/main/frame.c (original)
+++ trunk/main/frame.c Mon Apr 8 08:27:45 2013
@@ -351,6 +351,13 @@
}
}
+void ast_frame_dtor(struct ast_frame *f)
+{
+ if (f) {
+ ast_frfree(f);
+ }
+}
+
/*!
* \brief 'isolates' a frame by duplicating non-malloc'ed components
* (header, src, data).
Modified: trunk/main/json.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/json.c?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/main/json.c (original)
+++ trunk/main/json.c Mon Apr 8 08:27:45 2013
@@ -27,6 +27,7 @@
*/
/*** MODULEINFO
+ <depend>jansson</depend>
<support_level>core</support_level>
***/
@@ -35,10 +36,12 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/json.h"
+#include "asterisk/localtime.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include <jansson.h>
+#include <time.h>
/*!
* \brief Function wrapper around ast_malloc macro.
@@ -501,6 +504,37 @@
return (struct ast_json *)json_deep_copy((json_t *)value);
}
+struct ast_json *ast_json_name_number(const char *name, const char *number)
+{
+ return ast_json_pack("{s: s, s: s}",
+ "name", name,
+ "number", number);
+}
+
+struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
+{
+ return ast_json_pack("{s: o, s: o, s: o}",
+ "context", context ? ast_json_string_create(context) : ast_json_null(),
+ "exten", exten ? ast_json_string_create(exten) : ast_json_null(),
+ "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
+}
+
+struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone)
+{
+ char buf[AST_ISO8601_LEN];
+ struct ast_tm tm = {};
+
+ if (tv == NULL) {
+ return NULL;
+ }
+
+ ast_localtime(tv, &tm, zone);
+
+ ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
+
+ return ast_json_string_create(buf);
+}
+
void ast_json_init(void)
{
/* Setup to use Asterisk custom allocators */
Modified: trunk/tests/test_abstract_jb.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_abstract_jb.c?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/tests/test_abstract_jb.c (original)
+++ trunk/tests/test_abstract_jb.c Mon Apr 8 08:27:45 2013
@@ -65,17 +65,6 @@
jb->impl->destroy(jb->jbobj);
jb->impl = NULL;
jb->jbobj = NULL;
-}
-
-/*! \internal \brief Destructor for frames
- * \param f The frame to destroy
- */
-static void dispose_frame(struct ast_frame *f)
-{
- if (!f) {
- return;
- }
- ast_frfree(f);
}
/*! \internal \brief Create a test frame
@@ -217,8 +206,8 @@
RAII_VAR(struct ast_jb *, jb, &default_jb, dispose_jitterbuffer); \
const struct ast_jb_impl *impl; \
struct ast_jb_conf conf; \
- RAII_VAR(struct ast_frame *, expected_frame, NULL, dispose_frame); \
- RAII_VAR(struct ast_frame *, actual_frame, NULL, dispose_frame); \
+ RAII_VAR(struct ast_frame *, expected_frame, NULL, ast_frame_dtor); \
+ RAII_VAR(struct ast_frame *, actual_frame, NULL, ast_frame_dtor); \
int res; \
\
switch (cmd) { \
@@ -273,8 +262,8 @@
RAII_VAR(struct ast_jb *, jb, &default_jb, dispose_jitterbuffer); \
const struct ast_jb_impl *impl; \
struct ast_jb_conf conf; \
- RAII_VAR(struct ast_frame *, expected_frame, NULL, dispose_frame); \
- RAII_VAR(struct ast_frame *, actual_frame, NULL, dispose_frame); \
+ RAII_VAR(struct ast_frame *, expected_frame, NULL, ast_frame_dtor); \
+ RAII_VAR(struct ast_frame *, actual_frame, NULL, ast_frame_dtor); \
int res; \
long next; \
int i; \
@@ -336,7 +325,7 @@
RAII_VAR(struct ast_jb *, jb, &default_jb, dispose_jitterbuffer); \
const struct ast_jb_impl *impl; \
struct ast_jb_conf conf; \
- RAII_VAR(struct ast_frame *, expected_frame, NULL, dispose_frame); \
+ RAII_VAR(struct ast_frame *, expected_frame, NULL, ast_frame_dtor); \
int res; \
int i; \
\
@@ -401,8 +390,8 @@
RAII_VAR(struct ast_jb *, jb, &default_jb, dispose_jitterbuffer); \
const struct ast_jb_impl *impl; \
struct ast_jb_conf conf; \
- RAII_VAR(struct ast_frame *, actual_frame, NULL, dispose_frame); \
- RAII_VAR(struct ast_frame *, expected_frame, NULL, dispose_frame); \
+ RAII_VAR(struct ast_frame *, actual_frame, NULL, ast_frame_dtor); \
+ RAII_VAR(struct ast_frame *, expected_frame, NULL, ast_frame_dtor); \
int res; \
long next; \
int i; \
Modified: trunk/tests/test_json.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_json.c?view=diff&rev=384879&r1=384878&r2=384879
==============================================================================
--- trunk/tests/test_json.c (original)
+++ trunk/tests/test_json.c Mon Apr 8 08:27:45 2013
@@ -1607,6 +1607,102 @@
ast_test_validate(test, NULL == str);
/* Circular refs screw up reference counting, so break the cycle */
ast_json_object_clear(inner_child);
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(json_test_name_number)
+{
+ RAII_VAR(void *, alloc_debug, json_test_init(test), json_test_finish);
+ RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "name_number";
+ info->category = "/main/json/";
+ info->summary = "JSON encoding of name/number pair.";
+ info->description = "Test JSON abstraction library.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_validate(test, NULL == ast_json_name_number("name", NULL));
+ ast_test_validate(test, NULL == ast_json_name_number(NULL, "1234"));
+ ast_test_validate(test, NULL == ast_json_name_number(NULL, NULL));
+
+ expected = ast_json_pack("{s: s, s: s}",
+ "name", "Jenny",
+ "number", "867-5309");
+ uut = ast_json_name_number("Jenny", "867-5309");
+ ast_test_validate(test, ast_json_equal(expected, uut));
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(json_test_timeval)
+{
+ RAII_VAR(void *, alloc_debug, json_test_init(test), json_test_finish);
+ RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
+ struct timeval tv = {};
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "timeval";
+ info->category = "/main/json/";
+ info->summary = "JSON encoding of timevals.";
+ info->description = "Test JSON abstraction library.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_validate(test, NULL == ast_json_timeval(NULL, NULL));
+ expected = ast_json_string_create("2013-02-07T09:32:34.314-0600");
+
+ tv.tv_sec = 1360251154;
+ tv.tv_usec = 314159;
+ uut = ast_json_timeval(&tv, "America/Chicago");
+
+ ast_test_validate(test, ast_json_equal(expected, uut));
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(json_test_cep)
+{
+ RAII_VAR(void *, alloc_debug, json_test_init(test), json_test_finish);
+ RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+ RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "cep";
+ info->category = "/main/json/";
+ info->summary = "JSON with circular references cannot be encoded.";
+ info->description = "Test JSON abstraction library.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ expected = ast_json_pack("{s: o, s: o, s: o}",
+ "context", ast_json_null(),
+ "exten", ast_json_null(),
+ "priority", ast_json_null());
+ uut = ast_json_dialplan_cep(NULL, NULL, -1);
+ ast_test_validate(test, ast_json_equal(expected, uut));
+
+ ast_json_unref(expected);
+ ast_json_unref(uut);
+ expected = ast_json_pack("{s: s, s: s, s: i}",
+ "context", "main",
+ "exten", "4321",
+ "priority", 7);
+ uut = ast_json_dialplan_cep("main", "4321", 7);
+ ast_test_validate(test, ast_json_equal(expected, uut));
return AST_TEST_PASS;
}
@@ -1661,6 +1757,9 @@
AST_TEST_UNREGISTER(json_test_circular_object);
AST_TEST_UNREGISTER(json_test_circular_array);
AST_TEST_UNREGISTER(json_test_clever_circle);
+ AST_TEST_UNREGISTER(json_test_name_number);
+ AST_TEST_UNREGISTER(json_test_timeval);
+ AST_TEST_UNREGISTER(json_test_cep);
return 0;
}
@@ -1714,6 +1813,9 @@
AST_TEST_REGISTER(json_test_circular_object);
AST_TEST_REGISTER(json_test_circular_array);
AST_TEST_REGISTER(json_test_clever_circle);
+ AST_TEST_REGISTER(json_test_name_number);
+ AST_TEST_REGISTER(json_test_timeval);
+ AST_TEST_REGISTER(json_test_cep);
return AST_MODULE_LOAD_SUCCESS;
}
More information about the asterisk-commits
mailing list