static int i4l_setdev(struct ast_modem_pvt *p, int dev) { char cmd[80]; if ((dev != MODEM_DEV_TELCO) && (dev != MODEM_DEV_TELCO_SPK)) { ast_log(LOG_WARNING, "ISDN4Linux only supports telco device, not %d.\n", dev); return -1; } else /* Convert DEV to our understanding of it */ dev = 2; if (ast_modem_send(p, "AT+VLS?", 0)) { ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev); return -1; } if (ast_modem_read_response(p, 5)) { ast_log(LOG_WARNING, "Unable to select device %d\n", dev); return -1; } ast_modem_trim(p->response); strncpy(cmd, p->response, sizeof(cmd)-1); if (ast_modem_expect(p, "OK", 5)) { ast_log(LOG_WARNING, "Modem did not respond properly\n"); return -1; } if (dev == atoi(cmd)) { /* We're already in the right mode, don't bother changing for fear of hanging up */ return 0; } snprintf(cmd, sizeof(cmd), "AT+VLS=%d", dev); if (ast_modem_send(p, cmd, 0)) { ast_log(LOG_WARNING, "Unable to select device %d\n", dev); return -1; } if (ast_modem_read_response(p, 5)) { ast_log(LOG_WARNING, "Unable to select device %d\n", dev); return -1; } ast_modem_trim(p->response); if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) { ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response); return -1; } return 0; }
static int aopen_setdev(struct ast_modem_pvt *p, int dev) { char cmd[80]; if (ast_modem_send(p, "AT#VLS?", 0)) { ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev); return -1; } if (ast_modem_read_response(p, 5)) { ast_log(LOG_WARNING, "Unable to select device %d\n", dev); return -1; } ast_modem_trim(p->response); strncpy(cmd, p->response, sizeof(cmd)-1); if (ast_modem_expect(p, "OK", 5)) { ast_log(LOG_WARNING, "Modem did not respond properly\n"); return -1; } if (dev == atoi(cmd)) { /* We're already in the right mode, don't bother changing for fear of hanging up */ return 0; } snprintf(cmd, sizeof(cmd), "AT#VLS=%d", dev); if (ast_modem_send(p, cmd, 0)) { ast_log(LOG_WARNING, "Unable to select device %d\n", dev); return -1; } if (ast_modem_read_response(p, 5)) { ast_log(LOG_WARNING, "Unable to select device %d\n", dev); return -1; } ast_modem_trim(p->response); if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) { ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response); return -1; } return 0; }
static char *aopen_identify(struct ast_modem_pvt *p) { char identity[256]; char mfr[80]; char mdl[80]; char rev[80]; ast_modem_send(p, "AT#MDL?", 0); ast_modem_read_response(p, 5); strncpy(mdl, p->response, sizeof(mdl)-1); ast_modem_trim(mdl); ast_modem_expect(p, "OK", 5); ast_modem_send(p, "AT#MFR?", 0); ast_modem_read_response(p, 5); strncpy(mfr, p->response, sizeof(mfr)-1); ast_modem_trim(mfr); ast_modem_expect(p, "OK", 5); ast_modem_send(p, "AT#REV?", 0); ast_modem_read_response(p, 5); strncpy(rev, p->response, sizeof(rev)-1); ast_modem_trim(rev); ast_modem_expect(p, "OK", 5); snprintf(identity, sizeof(identity), "%s Model %s Revision %s", mfr, mdl, rev); return strdup(identity); }
static struct ast_frame *i4l_read(struct ast_modem_pvt *p) { unsigned char result[256]; short *b; struct ast_frame *f=NULL; int res; int x; if (p->ministate == STATE_COMMAND) { /* Read the first two bytes, first, in case it's a control message */ res = read(p->fd, result, 2); if (res < 2) { /* short read, means there was a hangup? */ /* (or is this also possible without hangup?) */ /* Anyway, reading from unitialized buffers is a bad idea anytime. */ if (errno == EAGAIN) return i4l_handle_escape(p, 0); return NULL; } if (result[0] == CHAR_DLE) { return i4l_handle_escape(p, result[1]); } else { if ((result[0] == '\n') || (result[0] == '\r')) return i4l_handle_escape(p, 0); /* Read the rest of the line */ fgets(result + 2, sizeof(result) - 2, p->f); ast_modem_trim(result); if (!strcasecmp(result, "VCON")) { /* If we're in immediate mode, reply now */ /* if (p->mode == MODEM_MODE_IMMEDIATE) */ return i4l_handle_escape(p, 'X'); } else if (!strcasecmp(result, "BUSY")) { /* Same as a busy signal */ return i4l_handle_escape(p, 'b'); } else if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) { strncpy(p->cid_num, result + 15, sizeof(p->cid_num)-1); return i4l_handle_escape(p, 0); } else if (!strcasecmp(result, "RINGING")) { if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "%s is ringing...\n", p->dev); return i4l_handle_escape(p, 'I'); } else if (!strncasecmp(result, "RUNG", 4)) { /* PM2002: the line was hung up before we picked it up, bye bye */ if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "%s was hung up on before we answered\n", p->dev); return NULL; } else if (!strncasecmp(result, "RING", 4)) { if (result[4]=='/') strncpy(p->dnid, result + 5, sizeof(p->dnid)-1); return i4l_handle_escape(p, 'R'); } else if (!strcasecmp(result, "NO CARRIER")) { if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev); return NULL; } else if (!strcasecmp(result, "NO DIALTONE")) { /* There's no dialtone, so the line isn't working */ ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev); return NULL; } if (option_debug) ast_log(LOG_DEBUG, "Modem said '%s'\n", result); return i4l_handle_escape(p, 0); } } else { /* We have to be more efficient in voice mode */ b = (short *)(p->obuf + p->obuflen); while (p->obuflen/2 < 240) { /* Read ahead the full amount */ res = read(p->fd, result, 240 - p->obuflen/2); if (res < 1) { /* If there's nothing there, just continue on */ if (errno == EAGAIN) return i4l_handle_escape(p, 0); ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno)); return NULL; } for (x=0; x<res; x++) { /* Process all the bytes that we've read */ switch(result[x]) { case CHAR_DLE: #if 0 ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x); #endif if (!p->escape) { /* Note that next value is an escape, and continue. */ p->escape++; break; } else { /* Send as is -- fallthrough */ p->escape = 0; } default: if (p->escape) { ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]); p->escape = 0; if (f) ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n"); f = i4l_handle_escape(p, result[x]); /* If i4l_handle_escape says NULL, say it now, doesn't matter what else is there, the connection is dead. */ if (!f) return NULL; } else { *(b++) = AST_MULAW((int)result[x]); p->obuflen += 2; } } } if (f) break; } if (f) { if( ! (!(p->dtmfmode & MODEM_DTMF_I4L) && f->frametype == AST_FRAME_DTMF)) return f; } /* If we get here, we have a complete voice frame */ p->fr.frametype = AST_FRAME_VOICE; p->fr.subclass = AST_FORMAT_SLINEAR; p->fr.samples = 240; p->fr.data = p->obuf; p->fr.datalen = p->obuflen; p->fr.mallocd = 0; p->fr.delivery.tv_sec = 0; p->fr.delivery.tv_usec = 0; p->fr.offset = AST_FRIENDLY_OFFSET; p->fr.src = __FUNCTION__; p->obuflen = 0; /* process with dsp */ if (p->dsp) { f = ast_dsp_process(p->owner, p->dsp, &p->fr); if (f && (f->frametype == AST_FRAME_DTMF)) { ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c on %s\n", f->subclass, p->dev); if (f->subclass == 'f') { /* Fax tone -- Handle and return NULL */ struct ast_channel *ast = p->owner; if (!p->faxhandled) { p->faxhandled++; if (strcmp(ast->exten, "fax")) { const char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext; if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) { if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name); /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */ pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten); if (ast_async_goto(ast, target_context, "fax", 1)) ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context); } else ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n"); } else ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n"); } else ast_log(LOG_DEBUG, "Fax already handled\n"); p->fr.frametype = AST_FRAME_NULL; p->fr.subclass = 0; f = &p->fr; } return f; } } return &p->fr; } return NULL; }
static struct ast_frame *aopen_read(struct ast_modem_pvt *p) { char result[256]; short *b; struct ast_frame *f=NULL; int res; int x; if (p->ministate == STATE_COMMAND) { /* Read the first two bytes, first, in case it's a control message */ fread(result, 1, 2, p->f); if (result[0] == CHAR_DLE) { return aopen_handle_escape(p, result[1]); } else { if ((result[0] == '\n') || (result[0] == '\r')) return aopen_handle_escape(p, 0); /* Read the rest of the line */ fgets(result + 2, sizeof(result) - 2, p->f); ast_modem_trim(result); if (!strcasecmp(result, "VCON")) { /* If we're in immediate mode, reply now */ if (p->mode == MODEM_MODE_IMMEDIATE) return aopen_handle_escape(p, 'X'); } else if (!strcasecmp(result, "BUSY")) { /* Same as a busy signal */ return aopen_handle_escape(p, 'b'); } else if (!strcasecmp(result, "RING")) { return aopen_handle_escape(p, 'R'); } else if (!strcasecmp(result, "NO DIALTONE")) { /* There's no dialtone, so the line isn't working */ ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev); return NULL; } ast_log(LOG_DEBUG, "Modem said '%s'\n", result); return aopen_handle_escape(p, 0); } } else { /* We have to be more efficient in voice mode */ b = (short *)(p->obuf + p->obuflen); while (p->obuflen/2 < 240) { /* Read ahead the full amount */ res = fread(result, 1, 240 - p->obuflen/2, p->f); if (res < 1) { /* If there's nothing there, just continue on */ if (errno == EAGAIN) return aopen_handle_escape(p, 0); ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno)); } for (x=0;x<res;x++) { /* Process all the bytes that we've read */ if (result[x] == CHAR_DLE) { /* We assume there is no more than one signal frame among our data. */ if (f) ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n"); f = aopen_handle_escape(p, result[x+1]); /* If aopen_handle_escape says NULL, say it now, doesn't matter what else is there, the connection is dead. */ if (!f) return NULL; } else { /* Generate a 16-bit signed linear value from our unsigned 8-bit value */ *(b++) = (((short)result[x]) - 127) * 0xff; p->obuflen += 2; } } if (f) break; } /* If we have a control frame, return it now */ if (f) return f; /* If we get here, we have a complete voice frame */ p->fr.frametype = AST_FRAME_VOICE; p->fr.subclass = AST_FORMAT_SLINEAR; p->fr.samples = 240; p->fr.data = p->obuf; p->fr.datalen = p->obuflen; p->fr.mallocd = 0; p->fr.delivery.tv_sec = 0; p->fr.delivery.tv_usec = 0; p->fr.offset = AST_FRIENDLY_OFFSET; p->fr.src = __FUNCTION__; if (option_debug) ast_log(LOG_DEBUG, "aopen_read(voice frame)\n"); p->obuflen = 0; return &p->fr; } return NULL; }
static struct ast_frame *bestdata_handle_escape(struct ast_modem_pvt *p, char esc) { char name[30]="",nmbr[30]=""; time_t now; /* Handle escaped characters -- but sometimes we call it directly as a quick way to cause known responses */ p->fr.frametype = AST_FRAME_NULL; p->fr.subclass = 0; p->fr.data = NULL; p->fr.datalen = 0; p->fr.samples = 0; p->fr.offset = 0; p->fr.mallocd = 0; p->fr.delivery.tv_sec = 0; p->fr.delivery.tv_usec = 0; if (esc) ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc); switch(esc) { case 'R': /* Pseudo ring */ time(&now); if (now > (p->lastring + (RINGT / 1000))) { /* if stale, treat as new */ p->gotclid = 0; } if (p->gotclid) { p->fr.frametype = AST_FRAME_CONTROL; p->fr.subclass = AST_CONTROL_RING; } p->ringt = RINGT; time(&p->lastring); return &p->fr; case 'X': /* Caller-ID Spill */ if (p->gotclid) return &p->fr; name[0] = nmbr[0] = 0; for(;;) { char res[1000]=""; if (ast_modem_read_response(p, 5)) break; strncpy(res, p->response, sizeof(res)-1); ast_modem_trim(res); if (!strncmp(res,"\020.",2)) break; if (!strncmp(res,"NAME",4)) strncpy(name,res + 7, sizeof(name) - 1); if (!strncmp(res,"NMBR",4)) strncpy(nmbr,res + 7, sizeof(nmbr) - 1); } p->gotclid = 1; if ((!strcmp(name,"O")) || (!strcmp(name,"P"))) name[0] = 0; if ((!strcmp(nmbr,"O")) || (!strcmp(nmbr,"P"))) nmbr[0] = 0; if ((name[0]) && (nmbr[0])) snprintf(p->cid,sizeof(p->cid), "\"%s\" <%s>",name,nmbr); else if (name[0]) snprintf(p->cid,sizeof(p->cid), "\"%s\"",name); else if (nmbr[0]) snprintf(p->cid,sizeof(p->cid), "%s",nmbr); if (p->owner) p->owner->callerid = strdup(p->cid); return &p->fr; case '@': /* response from "OK" in command mode */ if (p->owner) ast_setstate(p->owner, AST_STATE_UP); if (bestdata_startrec(p)) return NULL; p->fr.frametype = AST_FRAME_CONTROL; p->fr.subclass = AST_CONTROL_RING; return &p->fr; case 'b': /* Busy signal */ p->fr.frametype = AST_FRAME_CONTROL; p->fr.subclass = AST_CONTROL_BUSY; return &p->fr; case 'o': /* Overrun */ ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n"); if (ast_modem_send(p, "\0x10E", 2)) ast_log(LOG_WARNING, "Unable to flush buffers\n"); return &p->fr; case '0': /* All the DTMF characters */ case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '*': case '#': case 'A': case 'B': case 'C': case 'D': p->dtmfrx = esc; /* save this for when its done */ return &p->fr; case '/': /* Start of DTMF tone shielding */ p->dtmfrx = ' '; return &p->fr; case '~': /* DTMF transition to off */ if (p->dtmfrx > ' ') { p->fr.frametype = AST_FRAME_DTMF; p->fr.subclass = p->dtmfrx; } p->dtmfrx = 0; return &p->fr; case 'u': /* Underrun */ ast_log(LOG_WARNING, "Data underrun\n"); /* Fall Through */ case CHAR_ETX: /* End Transmission */ case 'd': /* Dialtone */ case 'c': /* Calling Tone */ case 'e': /* European version */ case 'a': /* Answer Tone */ case 'f': /* Bell Answer Tone */ case 'T': /* Timing mark */ case 't': /* Handset off hook */ case 'h': /* Handset hungup */ case 0: /* Pseudo signal */ /* Ignore */ return &p->fr; default: ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc); } return &p->fr; }