summaryrefslogtreecommitdiff
path: root/pjsip/src/pjsua
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2006-02-08 22:44:25 +0000
committerBenny Prijono <bennylp@teluu.com>2006-02-08 22:44:25 +0000
commite88c16ac16d93a586406bc5503d3110f264c5263 (patch)
treed56410f7f00fc914ed26c1c0442d72040210f146 /pjsip/src/pjsua
parent66f9158fa3c12ebd3b2d317cf42e461e0b86a6aa (diff)
Finished invite session UAS implementation
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@160 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip/src/pjsua')
-rw-r--r--pjsip/src/pjsua/main.c92
-rw-r--r--pjsip/src/pjsua/pjsua_core.c96
2 files changed, 145 insertions, 43 deletions
diff --git a/pjsip/src/pjsua/main.c b/pjsip/src/pjsua/main.c
index 68140d4a..f802799f 100644
--- a/pjsip/src/pjsua/main.c
+++ b/pjsip/src/pjsua/main.c
@@ -18,15 +18,9 @@
*/
#include "pjsua.h"
#include "getopt.h"
+#include <stdlib.h>
-/* For debugging, disable threading. */
-//#define NO_WORKER_THREAD
-
-#ifdef NO_WORKER_THREAD
-#include <conio.h>
-#endif
-
#define THIS_FILE "main.c"
static pjsip_inv_session *inv_session;
@@ -68,35 +62,45 @@ static void ui_help(void)
puts("");
puts("Console keys:");
puts(" m Make a call");
+ puts(" a Answer incoming call");
puts(" h Hangup current call");
puts(" q Quit");
puts("");
fflush(stdout);
}
+static pj_bool_t input(const char *title, char *buf, pj_size_t len)
+{
+ char *p;
+
+ printf("%s (empty to cancel): ", title); fflush(stdout);
+ fgets(buf, len, stdin);
+
+ /* Remove trailing newlines. */
+ for (p=buf; ; ++p) {
+ if (*p=='\r' || *p=='\n') *p='\0';
+ else if (!*p) break;
+ }
+
+ if (!*buf)
+ return PJ_FALSE;
+
+ return PJ_TRUE;
+}
+
static void ui_console_main(void)
{
- char keyin[10];
char buf[128];
- char *p;
pjsip_inv_session *inv;
//ui_help();
for (;;) {
-#ifdef NO_WORKER_THREAD
- pj_time_val timeout = { 0, 10 };
- pjsip_endpt_handle_events (pjsua.endpt, &timeout);
-
- if (kbhit())
- fgets(keyin, sizeof(keyin), stdin);
-#else
ui_help();
- fgets(keyin, sizeof(keyin), stdin);
-#endif
+ fgets(buf, sizeof(buf), stdin);
- switch (keyin[0]) {
+ switch (buf[0]) {
case 'm':
if (inv_session != NULL) {
@@ -106,23 +110,9 @@ static void ui_console_main(void)
}
#if 1
- printf("Enter URL to call: "); fflush(stdout);
- fgets(buf, sizeof(buf), stdin);
-
- if (buf[0]=='\r' || buf[0]=='\n') {
- /* Cancelled. */
- puts("<cancelled>");
- fflush(stdout);
- continue;
- }
-
- /* Remove trailing newlines. */
- for (p=buf; ; ++p) {
- if (*p=='\r' || *p=='\n') *p='\0';
- else if (!*p) break;
- }
/* Make call! : */
-
+ if (!input("Enter URL to call", buf, sizeof(buf)))
+ continue;
pjsua_invite(buf, &inv);
#else
@@ -132,6 +122,33 @@ static void ui_console_main(void)
break;
+ case 'a':
+
+ if (inv_session == NULL || inv_session->role != PJSIP_ROLE_UAS ||
+ inv_session->state >= PJSIP_INV_STATE_CONNECTING)
+ {
+ puts("No pending incoming call");
+ fflush(stdout);
+ continue;
+
+ } else {
+ pj_status_t status;
+ pjsip_tx_data *tdata;
+
+ if (!input("Answer with code (100-699)", buf, sizeof(buf)))
+ continue;
+
+ status = pjsip_inv_answer(inv_session, atoi(buf), NULL, NULL,
+ &tdata);
+ if (status == PJ_SUCCESS)
+ status = pjsip_inv_send_msg(inv_session, tdata, NULL);
+
+ if (status != PJ_SUCCESS)
+ pjsua_perror("Unable to create/send response", status);
+ }
+
+ break;
+
case 'h':
if (inv_session == NULL) {
@@ -672,11 +689,6 @@ int main(int argc, char *argv[])
pjsua_default();
-#ifdef NO_WORKER_THREAD
- pjsua.thread_cnt = 0;
-#endif
-
-
/* Initialize pjsua (to create pool etc).
*/
diff --git a/pjsip/src/pjsua/pjsua_core.c b/pjsip/src/pjsua/pjsua_core.c
index dd4c0c57..46f7fde7 100644
--- a/pjsip/src/pjsua/pjsua_core.c
+++ b/pjsip/src/pjsua/pjsua_core.c
@@ -101,8 +101,99 @@ void pjsua_perror(const char *title, pj_status_t status)
*/
static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
{
- PJ_UNUSED_ARG(rdata);
- PJ_TODO(IMPLEMENT_UAS);
+ pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
+ pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
+ pjsip_msg *msg = rdata->msg_info.msg;
+
+ /*
+ * Handle incoming INVITE outside dialog.
+ */
+ if (dlg == NULL && tsx == NULL &&
+ msg->line.req.method.id == PJSIP_INVITE_METHOD)
+ {
+ pj_status_t status;
+ pjsip_tx_data *response = NULL;
+ unsigned options = 0;
+
+ /* Verify that we can handle the request. */
+ status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
+ pjsua.endpt, &response);
+ if (status != PJ_SUCCESS) {
+
+ /*
+ * No we can't handle the incoming INVITE request.
+ */
+
+ if (response) {
+ pjsip_response_addr res_addr;
+
+ pjsip_get_response_addr(response->pool, rdata, &res_addr);
+ pjsip_endpt_send_response(pjsua.endpt, &res_addr, response,
+ NULL, NULL);
+
+ } else {
+
+ /* Respond with 500 (Internal Server Error) */
+ pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL,
+ NULL, NULL);
+ }
+
+ } else {
+ /*
+ * Yes we can handle the incoming INVITE request.
+ */
+ pjsip_inv_session *inv;
+ pjmedia_sdp_session *answer;
+
+ /* Create dummy SDP answer: */
+
+
+ status = pjmedia_sdp_parse(pjsua.pool, PJSUA_DUMMY_SDP_ANSWER,
+ pj_native_strlen(PJSUA_DUMMY_SDP_ANSWER),
+ &answer);
+ if (status != PJ_SUCCESS) {
+
+ pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL,
+ NULL, NULL);
+ return PJ_TRUE;
+ }
+
+ /* Create dialog: */
+
+ status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
+ &pjsua.contact_uri, &dlg);
+ if (status != PJ_SUCCESS)
+ return PJ_TRUE;
+
+
+ /* Create invite session: */
+
+ status = pjsip_inv_create_uas( dlg, rdata, answer, 0, &inv);
+ if (status != PJ_SUCCESS) {
+
+ status = pjsip_dlg_create_response( dlg, rdata, 500, NULL,
+ &response);
+ if (status == PJ_SUCCESS)
+ status = pjsip_dlg_send_response(dlg,
+ pjsip_rdata_get_tsx(rdata),
+ response);
+ return PJ_TRUE;
+
+ }
+
+ /* Answer with 100 (using the dialog, not invite): */
+
+ status = pjsip_dlg_create_response(dlg, rdata, 100, NULL, &response);
+ if (status == PJ_SUCCESS)
+ status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), response);
+ }
+
+ /* This INVITE request has been handled. */
+ return PJ_TRUE;
+ }
+
+
+
return PJ_FALSE;
}
@@ -121,7 +212,6 @@ static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
{
PJ_UNUSED_ARG(rdata);
- PJ_TODO(IMPLEMENT_UAS);
return PJ_FALSE;
}