diff --git a/Makefile b/Makefile index f61e331..9e46c36 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,9 @@ $(PKG_CONFIG_PATH)/libpjproject.pc: $(MAKE) -C $(PJSIP_DIR) && \ $(MAKE) -C $(PJSIP_DIR) install -d-modem: d-modem.c $(PKG_CONFIG_PATH)/libpjproject.pc - $(CC) -o $@ $< `PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --static --cflags --libs libpjproject` - +d-modem: d-modem.c dmodem2.c $(PKG_CONFIG_PATH)/libpjproject.pc + $(CC) -g -o $@ $< `PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --static --cflags --libs libpjproject` + $(CC) -g -o dmodem2 dmodem2.c `PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)" pkg-config --static --cflags --libs libpjproject` slmodemd: $(MAKE) -C slmodemd diff --git a/d-modem.c b/d-modem.c index 5a352b4..79bc028 100644 --- a/d-modem.c +++ b/d-modem.c @@ -22,10 +22,17 @@ #include #include #include - +// test #include #define SIGNATURE PJMEDIA_SIG_CLASS_PORT_AUD('D','M') +#define DMODEM_DIAL_MODE 0 +#define DMODEM_ANSWER_MODE 2 +#define DMODEM_RING_DETECT_MODE 3 + +uint8_t mode = DMODEM_DIAL_MODE; +uint8_t ringing = 0; +pjsua_call_id incoming; struct dmodem { pjmedia_port base; @@ -92,15 +99,30 @@ static void on_call_state(pjsua_call_id call_id, pjsip_event *e) { ci.state_text.ptr)); if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { - close(port.sock); - if (!destroying) { - destroying = true; - pjsua_destroy(); - exit(0); + if(mode != DMODEM_RING_DETECT_MODE) { + close(port.sock); + if (!destroying) { + destroying = true; + pjsua_destroy(); + exit(0); + } } + ringing = 0; } } +static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, pjsip_rx_data *rdata) { + pjsua_call_info ci; + pjsua_call_get_info(call_id, &ci); + char* tmp = malloc(pj_strlen(&ci.remote_contact)+1); + strcpy(tmp, ci.remote_contact.ptr); + tmp[pj_strlen(&ci.remote_contact)] = '\0'; + printf("Incoming call from: %s\n", tmp); + free(tmp); + incoming = call_id; + ringing = 1; +} + /* Callback called by the library when call's media state has changed */ static void on_call_media_state(pjsua_call_id call_id) { pjsua_call_info ci; @@ -126,30 +148,22 @@ static void on_call_media_state(pjsua_call_id call_id) { int main(int argc, char *argv[]) { pjsua_acc_id acc_id; pj_status_t status; - if (argc != 3) { return -1; } - + ringing = 0; + if(!strncmp(argv[1], "++", 2)) { + mode = DMODEM_ANSWER_MODE; + } else if(!strncmp(argv[1], "rr", 2)) { + mode = DMODEM_RING_DETECT_MODE; + } signal(SIGPIPE,SIG_IGN); - char *dialstr = argv[1]; - char *sip_user = getenv("SIP_LOGIN"); - if (!sip_user) { - return -1; - } - char *sip_domain = strchr(sip_user,'@'); - if (!sip_domain) { - return -1; - } - *sip_domain++ = '\0'; - char *sip_pass = strchr(sip_user,':'); - if (!sip_pass) { - return -1; - } - *sip_pass++ = '\0'; - + char *sip_user = "dialupuser"; + char *sip_domain = "192.168.1.2"; + char *sip_pass = "pppasswdModem1"; + printf("sip data: user: %s, passwd: %s, server: %s\nMODE: %d\n", sip_user, sip_pass, sip_domain, mode); status = pjsua_create(); if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status); @@ -163,6 +177,10 @@ int main(int argc, char *argv[]) { cfg.cb.on_call_media_state = &on_call_media_state; cfg.cb.on_call_state = &on_call_state; + if(mode == DMODEM_RING_DETECT_MODE) { + cfg.cb.on_incoming_call = &on_incoming_call; + } + pjsua_logging_config_default(&log_cfg); log_cfg.console_level = 4; @@ -204,24 +222,25 @@ int main(int argc, char *argv[]) { status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL); if (status != PJ_SUCCESS) error_exit("Error creating transport", status); } - + char buf[384]; + printf("Initializing pool\n"); pj_caching_pool cp; pj_caching_pool_init(&cp, NULL, 1024*1024); pool = pj_pool_create(&cp.factory, "pool1", 4000, 4000, NULL); pj_str_t name = pj_str("dmodem"); - + memset(&port,0,sizeof(port)); port.sock = atoi(argv[2]); // inherited from parent + if(mode == DMODEM_RING_DETECT_MODE) + port.sock = NULL; pjmedia_port_info_init(&port.base.info, &name, SIGNATURE, 9600, 1, 16, 192); port.base.put_frame = dmodem_put_frame; port.base.get_frame = dmodem_get_frame; port.base.on_destroy = dmodem_on_destroy; - char buf[384]; memset(buf,0,sizeof(buf)); write(port.sock, buf, sizeof(buf)); - /* Initialization is done, now start pjsua */ status = pjsua_start(); if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status); @@ -233,7 +252,7 @@ int main(int argc, char *argv[]) { pj_strdup2(pool,&cfg.id,buf); snprintf(buf,sizeof(buf),"sip:%s",sip_domain); pj_strdup2(pool,&cfg.reg_uri,buf); - cfg.register_on_acc_add = false; + cfg.register_on_acc_add = mode ? true : false; cfg.cred_count = 1; cfg.cred_info[0].realm = pj_str("*"); cfg.cred_info[0].scheme = pj_str("digest"); @@ -245,16 +264,33 @@ int main(int argc, char *argv[]) { if (status != PJ_SUCCESS) error_exit("Error adding account", status); } - snprintf(buf,sizeof(buf),"sip:%s@%s",dialstr,sip_domain); - printf("calling %s\n",buf); - pj_str_t uri = pj_str(buf); - - pjsua_call_id callid; - status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, &callid); - if (status != PJ_SUCCESS) error_exit("Error making call", status); - + if(mode == DMODEM_DIAL_MODE) { + snprintf(buf,sizeof(buf),"sip:%s@%s",dialstr,sip_domain); + printf("calling %s\n",buf); + pj_str_t uri = pj_str(buf); + pjsua_call_id callid; + status = pjsua_call_make_call(acc_id, &uri, 0, NULL, NULL, &callid); + if (status != PJ_SUCCESS) error_exit("Error making call", status); + } + if(mode == DMODEM_ANSWER_MODE) { + pjsua_call_id id; + char* cid = strrchr(argv[1], '+'); + id = atoi(cid); + status = pjsua_call_answer(id, 200, NULL, NULL); + if (status != PJ_SUCCESS) error_exit("Error answering call", status); + } struct timespec ts = {100, 0}; + if(mode == DMODEM_RING_DETECT_MODE) + ts.tv_sec = 1; + time_t now = time(NULL); while(1) { + if(mode == DMODEM_RING_DETECT_MODE) { + if(ringing) { + char cid[11]; + snprintf(cid, 10, "%d", incoming); + write(atoi(argv[2]), cid, strlen(cid)); + } + } nanosleep(&ts,NULL); } diff --git a/slmodemd/modem.c b/slmodemd/modem.c index 3a4c711..07a3911 100644 --- a/slmodemd/modem.c +++ b/slmodemd/modem.c @@ -1203,7 +1203,7 @@ static int modem_stop (struct modem *m) } #ifdef MODEM_CONFIG_RING_DETECTOR if(m->rd_obj) { - RD_delete(m->rd_obj); + free(m->rd_obj); m->rd_obj = NULL; } #endif @@ -1316,6 +1316,11 @@ static int modem_cid_start(struct modem *m, unsigned timeout) #ifdef MODEM_CONFIG_RING_DETECTOR static void modem_ring_detector_process(struct modem *m, void *in, void *out, int count) { + if(count != 0) { + char callid[10]; + strncpy(callid, in, 10); + printf("Ringing! callid:%s\n", callid); + }/* int ret; memset(out, 0, count*2); ret = RD_process(m->rd_obj, in, count); @@ -1330,7 +1335,7 @@ static void modem_ring_detector_process(struct modem *m, void *in, void *out, in } else if (freq > 0) { MODEM_DBG("report ring end...\n"); - /* ring finishing */ + /* ring finishing *//* m->event |= MDMEVENT_RING_CHECK; if (m->ring_count <= 1) m->ring_count = duration * freq / 1000 ; @@ -1339,7 +1344,7 @@ static void modem_ring_detector_process(struct modem *m, void *in, void *out, in else MODEM_ERR("RD returns %ld freq. (duration %ld)\n", freq, duration); - } + }*/ #ifdef MODEM_CONFIG_CID if(m->cid) modem_cid_process(m, in, out, count); @@ -1353,10 +1358,11 @@ int modem_ring_detector_start(struct modem *m) MODEM_ERR("modem_ring_detector_start: rd_obj already exists!\n"); return -1; } - m->rd_obj = RD_create(m, m->srate); + m->rd_obj = malloc(sizeof(char)); m->process = modem_ring_detector_process; modem_set_hook(m, MODEM_HOOK_SNOOPING); return do_modem_start(m); + } #endif /* MODEM_CONFIG_RING_DETECTOR */ diff --git a/slmodemd/modem.h b/slmodemd/modem.h index 3550d83..59465d3 100644 --- a/slmodemd/modem.h +++ b/slmodemd/modem.h @@ -91,7 +91,7 @@ enum MODEM_MODE { #define MODEM_DEFAULT_COUNTRY_CODE 0xb5 /* USA */ -#define MODEM_CONFIG_CID 1 +#define MODEM_CONFIG_CID 0 #define MODEM_CONFIG_VOICE 1 #define MODEM_CONFIG_FAX 1 #define MODEM_CONFIG_FAX_CLASS1 1 diff --git a/slmodemd/modem_main.c b/slmodemd/modem_main.c index 2a95097..e924249 100644 --- a/slmodemd/modem_main.c +++ b/slmodemd/modem_main.c @@ -600,6 +600,8 @@ static int modemap_ioctl(struct modem *m, unsigned int cmd, unsigned long arg) if (cmd == MDMCTL_IODELAY && ret > 0) { ret >>= MFMT_SHIFT(m->format); ret += dev->delay; + + } return ret; } @@ -635,7 +637,10 @@ static int socket_start (struct modem *m) char str[16]; snprintf(str,sizeof(str),"%d",sockets[0]); close(sockets[1]); - ret = execl(modem_exec,modem_exec,m->dial_string,str,NULL); + if(m->hook == MODEM_HOOK_SNOOPING) + ret = execl(modem_exec,modem_exec,"rr",str,NULL); + else + ret = execl(modem_exec,modem_exec,m->dial_string,str,NULL); if (ret == -1) { ERR("prog: %s\n", modem_exec); perror("execl"); @@ -945,7 +950,7 @@ static int modem_run(struct modem *m, struct device_struct *dev) } if(FD_ISSET(dev->fd, &rset)) { count = device_read(dev,inbuf,sizeof(inbuf)/2); - if(count <= 0) { + if((count <= 0) && (m->hook != MODEM_HOOK_SNOOPING)) { if (errno == ECONNRESET) { DBG("lost connection to child socket process\n"); } else {