Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_mwi_body_generator.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Mark Michelson <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*** MODULEINFO
20  <depend>pjproject</depend>
21  <depend>res_pjsip</depend>
22  <depend>res_pjsip_pubsub</depend>
23  <support_level>core</support_level>
24  ***/
25 
26 #include "asterisk.h"
27 
28 #include <pjsip.h>
29 #include <pjsip_simple.h>
30 #include <pjlib.h>
31 
32 #include "asterisk/res_pjsip.h"
35 #include "asterisk/module.h"
36 #include "asterisk/strings.h"
37 
38 #define MWI_TYPE "application"
39 #define MWI_SUBTYPE "simple-message-summary"
40 
41 static void *mwi_allocate_body(void *data)
42 {
43  struct ast_str **mwi_str;
44 
45  mwi_str = ast_malloc(sizeof(*mwi_str));
46  if (!mwi_str) {
47  return NULL;
48  }
49  *mwi_str = ast_str_create(128);
50  if (!*mwi_str) {
51  ast_free(mwi_str);
52  return NULL;
53  }
54  return mwi_str;
55 }
56 
57 static int mwi_generate_body_content(void *body, void *data)
58 {
59  struct ast_str **mwi = body;
60  struct ast_sip_message_accumulator *counter = data;
61 
62  ast_str_append(mwi, 0, "Messages-Waiting: %s\r\n",
63  counter->new_msgs ? "yes" : "no");
64  if (!ast_strlen_zero(counter->message_account)) {
65  ast_str_append(mwi, 0, "Message-Account: %s\r\n", counter->message_account);
66  }
67  ast_str_append(mwi, 0, "Voice-Message: %d/%d (0/0)\r\n",
68  counter->new_msgs, counter->old_msgs);
69 
70  return 0;
71 }
72 
73 static void mwi_to_string(void *body, struct ast_str **str)
74 {
75  struct ast_str **mwi = body;
76 
77  ast_str_set(str, 0, "%s", ast_str_buffer(*mwi));
78 }
79 
80 static void mwi_destroy_body(void *body)
81 {
82  struct ast_str **mwi = body;
83 
84  ast_free(*mwi);
85  ast_free(mwi);
86 }
87 
89  .type = MWI_TYPE,
90  .subtype = MWI_SUBTYPE,
91  .body_type = AST_SIP_MESSAGE_ACCUMULATOR,
92  .allocate_body = mwi_allocate_body,
93  .generate_body_content = mwi_generate_body_content,
94  .to_string = mwi_to_string,
95  .destroy_body = mwi_destroy_body,
96 };
97 
98 static int load_module(void)
99 {
100  if (ast_sip_pubsub_register_body_generator(&mwi_generator)) {
102  }
104 }
105 
106 static int unload_module(void)
107 {
109  return 0;
110 }
111 
113  .support_level = AST_MODULE_SUPPORT_CORE,
114  .load = load_module,
115  .unload = unload_module,
116  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
117  .requires = "res_pjsip,res_pjsip_pubsub",
118 );
#define AST_SIP_MESSAGE_ACCUMULATOR
Asterisk main include file. File version handling, generic pbx functions.
String manipulation functions.
Pubsub body generator.
const char * type
Content type In "plain/text", "plain" is the type.
static int load_module(void)
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
Message counter used for message-summary XML bodies.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define MWI_SUBTYPE
const char * str
Definition: app_jack.c:147
int ast_sip_pubsub_register_body_generator(struct ast_sip_pubsub_body_generator *generator)
Register a body generator with the pubsub core.
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1065
static int unload_module(void)
#define MWI_TYPE
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
static int mwi_generate_body_content(void *body, void *data)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static struct ast_sip_pubsub_body_generator mwi_generator
#define ast_free(a)
Definition: astmm.h:182
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static void mwi_to_string(void *body, struct ast_str **str)
static void mwi_destroy_body(void *body)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
void ast_sip_pubsub_unregister_body_generator(struct ast_sip_pubsub_body_generator *generator)
Unregister a body generator with the pubsub core.
static void * mwi_allocate_body(void *data)