#include #include #include #include #include #define AUDIO_DEV "/dev/snd/pcm0" static void call(struct _LinphoneCore *lc) { linphone_core_set_playback_device(lc, AUDIO_DEV); linphone_core_set_capture_device(lc, AUDIO_DEV); linphone_core_accept_call(lc, NULL); } static int allocate_sip_port(LinphoneCore *lc) { unsigned int i_port; int b_found = -1; for( i_port = 5060; i_port < 6060; i_port++ ) { unsigned int i_tmp = 0; linphone_core_set_sip_port(lc, i_port); i_tmp = linphone_core_get_sip_port(lc); if (i_port == i_tmp ) { b_found = 1; break; } } if (b_found) { printf( "listening on port %d\n", i_port ); return i_port; } return -1; } static LinphoneProxyConfig *proxy_register(LinphoneCore *lc, const char *url, const char *id, const int duration) { LinphoneProxyConfig *proxy = NULL; proxy = linphone_proxy_config_new(url); if (!proxy) return NULL; // register on SIP server linphone_proxy_config_set_identity(proxy, id); linphone_proxy_config_enableregister(proxy, TRUE); linphone_proxy_config_expires(proxy, duration); linphone_core_add_proxy_config(lc, proxy); linphone_core_set_default_proxy(lc, proxy); return proxy; } /* Callback functions */ static void show(struct _LinphoneCore *lc) { printf( "show\n" ); } static void inv_recv(struct _LinphoneCore *lc, const char *from) { printf( "invite from %s\n", from ); if (lc) call(lc); } static void bye_recv(struct _LinphoneCore *lc, const char *from) { printf( "bye from %s\n", from ); linphone_core_terminate_call(lc, NULL); } static void notify_recv(struct _LinphoneCore *lc, LinphoneFriend * fid, const char *url, const char *status, const char *img) { printf( "notify: %s status %s\n", url, status ); } static void new_unknown_subscriber(struct _LinphoneCore *lc, LinphoneFriend *lf, const char *url) { printf( "unknonw subscriber from: %s\n", url ); } static void auth_info_requested(struct _LinphoneCore *lc, const char *realm, const char *username) { printf( "auth: user %s realm %s\n", username, realm ); } static void display_status(struct _LinphoneCore *lc, const char *message) { printf( "status: %s\n", message ); } static void display_message(struct _LinphoneCore *lc, const char *message) { printf( "message: %s\n", message ); } static void display_warning(struct _LinphoneCore *lc, const char *message) { printf( "warning: %s\n", message ); } static void display_url(struct _LinphoneCore *lc, const char *message, const char *url) { printf( "url: %s\n", url ); } static void display_question(struct _LinphoneCore *lc, const char *message) { printf( "question: %s\n", message ); } static void call_log_updated(struct _LinphoneCore *lc, struct _LinphoneCallLog *newcl) { printf( "call_log_updated\n" ); } static void text_received(struct _LinphoneCore *lc, LinphoneChatRoom *room, const char *from, const char *message) { printf( "text from %s: %s\n", from, message ); } static void dtmf_received(struct _LinphoneCore* lc, int dtmf) { printf( "dtmf\n" ); } int main(int argc, char* argv[]) { LinphoneCore *lc = NULL; LinphoneCoreVTable lv; LinphoneAuthInfo *authinfo = NULL; LinphoneProxyConfig *proxy = NULL; char *user = "1300"; char *userid = "1300"; char *passwd = "1300"; char *realm = "asterisk"; char *server_addr = "192.168.3.6"; char *machine = NULL; char *s = NULL; char url[1024]; char id[1024]; int duration = 30; int i_port = -1; int quit = 5000; lv.show = show; lv.inv_recv = inv_recv; lv.bye_recv = bye_recv; lv.notify_recv = notify_recv; lv.new_unknown_subscriber = new_unknown_subscriber; lv.auth_info_requested = auth_info_requested; lv.display_status = display_status; lv.display_message = display_message; lv.display_warning = display_warning; lv.display_url = display_url; lv.display_question = display_question; lv.call_log_updated = call_log_updated; lv.text_received = text_received; lv.dtmf_received = dtmf_received; linphone_core_disable_logs(); lc = linphone_core_new(&lv, ".SipConfig", NULL); if (!lc) { printf( "out of memory\n" ); return EXIT_FAILURE; } linphone_core_get_sound_devices(lc); i_port = allocate_sip_port(lc); if (i_port < 0) { printf( "failed allocating listening port %d\n", i_port ); return EXIT_FAILURE; } authinfo = linphone_auth_info_new(user, userid, passwd, NULL, realm); if (authinfo) { linphone_core_add_auth_info(lc, authinfo); } memset(&id[0], 0, 1024); memset(&url[0], 0, 1024); machine = strdup(lc->sip_conf.contact); s = strpbrk(machine, "@"); if (!s) { printf( "No machine name found.\n" ); return EXIT_FAILURE; } snprintf(&id[0], 1024, "sip:address@hidden", userid, &s[1]); snprintf(&url[0], 1024, "sip:address@hidden:%d", realm, server_addr, i_port); printf( "Using: id = %s, url = %s\n", &id[0], &url[0] ); proxy = proxy_register(lc, url, id, duration); if (!proxy) { printf( "Invalid sipserver address: %s.\n", url); return EXIT_FAILURE; } /* mainloop */ while(quit--) { linphone_core_iterate(lc); usleep(200000); /* 200 ms*/ } /* Cleanup */ linphone_proxy_config_destroy(proxy); if (authinfo) { linphone_core_remove_auth_info(lc, authinfo); linphone_core_clear_all_auth_info(lc); } linphone_core_destroy(lc); lc = NULL; free(machine); return EXIT_SUCCESS; }