diff --git a/libs/openzap/mod_openzap/mod_openzap.c b/libs/openzap/mod_openzap/mod_openzap.c index 8f1abc3ecf..ef755a4772 100644 --- a/libs/openzap/mod_openzap/mod_openzap.c +++ b/libs/openzap/mod_openzap/mod_openzap.c @@ -554,7 +554,14 @@ static switch_status_t channel_receive_message_b(switch_core_session_t *session, assert(tech_pvt != NULL); switch (msg->message_id) { + case SWITCH_MESSAGE_INDICATE_RINGING: + if (!switch_channel_test_flag(channel, CF_OUTBOUND)) { + zap_set_state_locked(tech_pvt->zchan, ZAP_CHANNEL_STATE_PROGRESS); + } case SWITCH_MESSAGE_INDICATE_PROGRESS: + if (!switch_channel_test_flag(channel, CF_OUTBOUND)) { + zap_set_state_locked(tech_pvt->zchan, ZAP_CHANNEL_STATE_EARLY_MEDIA); + } case SWITCH_MESSAGE_INDICATE_ANSWER: if (!switch_channel_test_flag(channel, CF_OUTBOUND)) { zap_set_state_locked(tech_pvt->zchan, ZAP_CHANNEL_STATE_UP); @@ -697,14 +704,17 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER; } + dest = outbound_profile->destination_number; + if ((p = strchr(outbound_profile->destination_number, '/'))) { dest = p + 1; span_id = atoi(outbound_profile->destination_number); chan_id = atoi(dest); + if ((p = strchr(dest, '/'))) { + dest = p + 1; + } } - dest = outbound_profile->destination_number; - if (!dest) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid dial string\n"); return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER; @@ -972,6 +982,23 @@ static ZIO_SIGNAL_CB_FUNCTION(on_isdn_signal) switch_core_session_rwunlock(session); } } + case ZAP_SIGEVENT_PROGRESS_MEDIA: + { + if ((session = zap_channel_get_session(sigmsg->channel, 0))) { + channel = switch_core_session_get_channel(session); + switch_channel_mark_pre_answered(channel); + switch_core_session_rwunlock(session); + } + } + break; + case ZAP_SIGEVENT_PROGRESS: + { + if ((session = zap_channel_get_session(sigmsg->channel, 0))) { + channel = switch_core_session_get_channel(session); + switch_channel_mark_ring_ready(channel); + switch_core_session_rwunlock(session); + } + } break; } diff --git a/libs/openzap/src/include/openzap.h b/libs/openzap/src/include/openzap.h index b68596770b..c95849df43 100644 --- a/libs/openzap/src/include/openzap.h +++ b/libs/openzap/src/include/openzap.h @@ -482,6 +482,7 @@ void zap_global_set_logger(zap_logger_t logger); void zap_global_set_default_logger(int level); uint32_t zap_separate_string(char *buf, char delim, char **array, int arraylen); void print_bits(uint8_t *b, int bl, char *buf, int blen, int e, uint8_t ss); +void print_hex_bytes(uint8_t *data, zap_size_t dlen, uint8_t *buf, zap_size_t blen); ZIO_CODEC_FUNCTION(zio_slin2ulaw); ZIO_CODEC_FUNCTION(zio_ulaw2slin); ZIO_CODEC_FUNCTION(zio_slin2alaw); diff --git a/libs/openzap/src/include/zap_types.h b/libs/openzap/src/include/zap_types.h index 7eb9bbff0d..900c29c9b5 100644 --- a/libs/openzap/src/include/zap_types.h +++ b/libs/openzap/src/include/zap_types.h @@ -263,11 +263,13 @@ typedef enum { ZAP_CHANNEL_STATE_CALLWAITING, ZAP_CHANNEL_STATE_TERMINATING, ZAP_CHANNEL_STATE_RESTART, + ZAP_CHANNEL_STATE_EARLY_MEDIA, + ZAP_CHANNEL_STATE_PROGRESS, ZAP_CHANNEL_STATE_INVALID } zap_channel_state_t; #define CHANNEL_STATE_STRINGS "DOWN", "UP", "HANGUP", "HOLD", "DIALTONE", "COLLECT", \ "RING", "BUSY", "ATTN", "IDLE", "GENRING", "DIALING", "GET_CALLERID", "CALLWAITING", \ - "TERMINATING", "RESTART", "INVALID" + "TERMINATING", "RESTART", "EARLY_MEDIA", "PROGRESS", "INVALID" ZAP_STR2ENUM_P(zap_str2zap_channel_state, zap_channel_state2str, zap_channel_state_t) typedef enum { @@ -288,7 +290,8 @@ typedef enum { ZAP_CHANNEL_OFFHOOK = (1 << 14), ZAP_CHANNEL_RINGING = (1 << 15), ZAP_CHANNEL_PROGRESS_DETECT = (1 << 16), - ZAP_CHANNEL_CALLERID_DETECT = (1 << 17) + ZAP_CHANNEL_CALLERID_DETECT = (1 << 17), + ZAP_CHANNEL_OUTBOUND = (1 << 18) } zap_channel_flag_t; typedef struct zap_channel zap_channel_t; diff --git a/libs/openzap/src/isdn/Q931api.c b/libs/openzap/src/isdn/Q931api.c index 2e4e0063a6..541b28d428 100644 --- a/libs/openzap/src/isdn/Q931api.c +++ b/libs/openzap/src/isdn/Q931api.c @@ -217,7 +217,7 @@ L3INT Q931GetUniqueCRV(Q931_TrunkInfo_t *pTrunk) L3INT Q931InitMesGeneric(Q931mes_Generic *pMes) { memset(pMes, 0, sizeof(*pMes)); - pMes->ProtDisc = 0x80; + pMes->ProtDisc = 0x08; pMes->Size = Q931GetMesSize(pMes); return 0; @@ -225,7 +225,7 @@ L3INT Q931InitMesGeneric(Q931mes_Generic *pMes) L3INT Q931InitMesResume(Q931mes_Generic * pMes) { - pMes->ProtDisc = 0x80; + pMes->ProtDisc = 0x08; pMes->CRV = 0; /* CRV to be allocated, might be receive*/ pMes->MesType = Q931mes_RESUME; @@ -236,7 +236,7 @@ L3INT Q931InitMesResume(Q931mes_Generic * pMes) L3INT Q931InitMesRestartAck(Q931mes_Generic * pMes) { - pMes->ProtDisc = 0x80; + pMes->ProtDisc = 0x08; pMes->CRV = 0; /* CRV to be allocated, might be receive*/ pMes->MesType = Q931mes_RESTART_ACKNOWLEDGE; diff --git a/libs/openzap/src/isdn/Q931ie.c b/libs/openzap/src/isdn/Q931ie.c index 0ea4251dbf..9ed580568b 100644 --- a/libs/openzap/src/isdn/Q931ie.c +++ b/libs/openzap/src/isdn/Q931ie.c @@ -322,17 +322,12 @@ L3INT Q931Pie_BearerCap(Q931_TrunkInfo_t *pTrunk, L3UCHAR *IBuf, L3UCHAR *OBuf, li=(*Octet)++; /* remember length position */ /* Octet 3 - Coding standard / Information transfer capability */ - OBuf[(*Octet)++] = 0x80 | (pIE->CodStand<<5) | (pIE->ITR & 0x1f); + OBuf[(*Octet)++] = 0x80 | (pIE->CodStand<<5) | (pIE->ITC & 0x1f); /* Octet 4 - Transfer mode / Information transfer rate */ + OBuf[(*Octet)++] = 0x80 | (pIE->TransMode<<5) | (pIE->ITR & 0x1f); if(pIE->ITR == 0x18) { - OBuf[(*Octet)++] = 0x80 | (pIE->TransMode<<5) | (pIE->ITR & 0x1f); - } - else - { - OBuf[(*Octet)++] = 0x00 | (pIE->TransMode<<5) | (pIE->ITR & 0x1f); - /* Octet 4.1 - Rate Multiplier */ OBuf[(*Octet)++] = 0x80 | pIE->RateMul; } @@ -382,7 +377,7 @@ L3INT Q931Pie_BearerCap(Q931_TrunkInfo_t *pTrunk, L3UCHAR *IBuf, L3UCHAR *OBuf, } else { - OBuf[(*Octet)++] = 0x80 | (pIE->Layer1Ident<<5) | (pIE->UIL1Prot & 0x15); + OBuf[(*Octet)++] = 0x80 | (pIE->Layer1Ident<<5) | (pIE->UIL1Prot & 0x1F); } } @@ -748,11 +743,11 @@ L3INT Q931Uie_CalledNum(Q931_TrunkInfo_t *pTrunk, Q931mes_Generic *pMsg, L3UCHAR *****************************************************************************/ L3INT Q931Pie_CalledNum(Q931_TrunkInfo_t *pTrunk, L3UCHAR *IBuf, L3UCHAR *OBuf, L3INT *Octet) { - Q931ie_CalledSub * pIE = (Q931ie_CalledSub*)IBuf; + Q931ie_CalledNum * pIE = (Q931ie_CalledNum*)IBuf; L3INT rc=0; L3INT Beg=*Octet; L3INT li; - L3INT sN = pIE->Size - sizeof(Q931ie_CalledSub) + 1; + L3INT sN = pIE->Size - sizeof(Q931ie_CalledNum); L3INT x; /* Octet 1 */ @@ -760,7 +755,7 @@ L3INT Q931Pie_CalledNum(Q931_TrunkInfo_t *pTrunk, L3UCHAR *IBuf, L3UCHAR *OBuf, li=(*Octet)++; /* Octet 3 */ - OBuf[(*Octet)++] = 0x80 | (pIE->TypNum << 4) | (pIE->OddEvenInd << 3); + OBuf[(*Octet)++] = 0x80 | (pIE->TypNum << 4) | (pIE->NumPlanID); /* Octet 4 */ for(x=0;xDigit[x]; } - OBuf[(*Octet)-1] |= 0x80; /* Terminate bit */ - OBuf[li] = (L3UCHAR)((*Octet)-Beg) - 2; return rc; } diff --git a/libs/openzap/src/priserver.c b/libs/openzap/src/priserver.c index 6bb5e02965..377ded7325 100644 --- a/libs/openzap/src/priserver.c +++ b/libs/openzap/src/priserver.c @@ -290,6 +290,7 @@ int main(int argc, char *argv[]) debug = PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE; + printf("WTF %d\n", debug); if (sangoma_init_pri(&spri, 1, // span diff --git a/libs/openzap/src/zap_io.c b/libs/openzap/src/zap_io.c index 416562c087..d5b525bcdd 100644 --- a/libs/openzap/src/zap_io.c +++ b/libs/openzap/src/zap_io.c @@ -658,6 +658,7 @@ static zap_status_t zap_channel_reset(zap_channel_t *zchan) zap_clear_flag(zchan, ZAP_CHANNEL_SUPRESS_DTMF); zap_channel_done(zchan); zap_clear_flag_locked(zchan, ZAP_CHANNEL_HOLD); + memset(zchan->tokens, 0, sizeof(zchan->tokens)); zchan->token_count = 0; @@ -747,9 +748,15 @@ zap_status_t zap_channel_open(uint32_t span_id, uint32_t chan_id, zap_channel_t zap_status_t zap_channel_outgoing_call(zap_channel_t *zchan) { + zap_status_t status; + assert(zchan != NULL); + if (zchan->span->outgoing_call) { - return zchan->span->outgoing_call(zchan); + if ((status = zchan->span->outgoing_call(zchan)) == ZAP_SUCCESS) { + zap_set_flag(zchan, ZAP_CHANNEL_OUTBOUND); + } + return status; } else { zap_log(ZAP_LOG_ERROR, "outgoing_call method not implemented!\n"); } @@ -765,6 +772,7 @@ zap_status_t zap_channel_done(zap_channel_t *zchan) memset(&zchan->caller_data, 0, sizeof(zchan->caller_data)); zap_clear_flag_locked(zchan, ZAP_CHANNEL_INUSE); + zap_clear_flag_locked(zchan, ZAP_CHANNEL_OUTBOUND); for (i = 0; i < 2; i++) { if (zchan->fds[i] > -1) { close(zchan->fds[i]); @@ -1967,6 +1975,28 @@ int8_t zap_bitstream_get_bit(zap_bitstream_t *bsp) return bit; } +void print_hex_bytes(uint8_t *data, zap_size_t dlen, uint8_t *buf, zap_size_t blen) +{ + char *bp = buf; + uint8_t *byte = data; + uint32_t i, j = 0; + + if (blen < (dlen * 3) + 2) { + return; + } + + *bp++ = '['; + j++; + + for(i = 0; i < dlen; i++) { + snprintf(bp, blen-j, "%02x ", *byte++); + bp += 3; + j += 3; + } + + *--bp = ']'; + +} void print_bits(uint8_t *b, int bl, char *buf, int blen, zap_endian_t e, uint8_t ss) { diff --git a/libs/openzap/src/zap_isdn.c b/libs/openzap/src/zap_isdn.c index 058c76c9e1..ec42c2e43f 100644 --- a/libs/openzap/src/zap_isdn.c +++ b/libs/openzap/src/zap_isdn.c @@ -108,6 +108,21 @@ static L3INT zap_isdn_931_34(void *pvt, L2UCHAR *msg, L2INT mlen) zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_TERMINATING); } break; + case Q931mes_ALERTING: + { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_EARLY_MEDIA); + } + break; + case Q931mes_PROGRESS: + { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_PROGRESS); + } + break; + case Q931mes_CONNECT: + { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_UP); + } + break; case Q931mes_SETUP: { @@ -155,7 +170,12 @@ static L3INT zap_isdn_931_34(void *pvt, L2UCHAR *msg, L2INT mlen) static int zap_isdn_921_23(void *pvt, L2UCHAR *msg, L2INT mlen) { - int ret = Q931Rx23(pvt, msg, mlen); + int ret; + char bb[4096] = ""; + print_hex_bytes(msg, mlen, bb, sizeof(bb)); + zap_log(ZAP_LOG_DEBUG, "READ %d\n%s\n%s\n\n", (int)mlen, LINE, bb); + + ret = Q931Rx23(pvt, msg, mlen); if (ret != 0) zap_log(ZAP_LOG_DEBUG, "931 parse error [%d] [%s]\n", ret, q931_error_to_name(ret)); return ((ret >= 0) ? 1 : 0); @@ -166,9 +186,10 @@ static int zap_isdn_921_21(void *pvt, L2UCHAR *msg, L2INT mlen) zap_span_t *span = (zap_span_t *) pvt; zap_size_t len = (zap_size_t) mlen; #ifdef IODEBUG - char bb[4096] = ""; - print_bits(msg, (int)len, bb, sizeof(bb), ZAP_ENDIAN_LITTLE, 0); - zap_log(ZAP_LOG_DEBUG, "WRITE %d\n%s\n%s\n\n", (int)len, LINE, bb); + //char bb[4096] = ""; + //print_hex_bytes(msg, len, bb, sizeof(bb)); + //print_bits(msg, (int)len, bb, sizeof(bb), ZAP_ENDIAN_LITTLE, 0); + //zap_log(ZAP_LOG_DEBUG, "WRITE %d\n%s\n%s\n\n", (int)len, LINE, bb); #endif @@ -192,11 +213,31 @@ static __inline__ void state_advance(zap_channel_t *zchan) sig.channel = zchan; switch (zchan->state) { + case ZAP_CHANNEL_STATE_DOWN: + { + zap_channel_done(zchan); + } + break; + case ZAP_CHANNEL_STATE_PROGRESS: + { + if (zap_test_flag(zchan, ZAP_CHANNEL_OUTBOUND)) { + sig.event_id = ZAP_SIGEVENT_PROGRESS; + if ((status = data->sig_cb(&sig) != ZAP_SUCCESS)) { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_HANGUP); + } + } else { + gen->MesType = Q931mes_PROGRESS; + Q931Rx43(&data->q931, (void *)gen, gen->Size); + } + } + break; case ZAP_CHANNEL_STATE_RING: { - sig.event_id = ZAP_SIGEVENT_START; - if ((status = data->sig_cb(&sig) != ZAP_SUCCESS)) { - zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_HANGUP); + if (!zap_test_flag(zchan, ZAP_CHANNEL_OUTBOUND)) { + sig.event_id = ZAP_SIGEVENT_START; + if ((status = data->sig_cb(&sig) != ZAP_SUCCESS)) { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_HANGUP); + } } } @@ -207,11 +248,31 @@ static __inline__ void state_advance(zap_channel_t *zchan) zap_channel_close(&zchan); } break; + case ZAP_CHANNEL_STATE_EARLY_MEDIA: + { + if (zap_test_flag(zchan, ZAP_CHANNEL_OUTBOUND)) { + sig.event_id = ZAP_SIGEVENT_PROGRESS_MEDIA; + if ((status = data->sig_cb(&sig) != ZAP_SUCCESS)) { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_HANGUP); + } + } else { + gen->MesType = Q931mes_ALERTING; + Q931Rx43(&data->q931, (void *)gen, gen->Size); + } + } + break; case ZAP_CHANNEL_STATE_UP: { - gen->MesType = Q931mes_CONNECT; - gen->BearerCap = 0; - Q931Rx43(&data->q931, (void *)gen, zchan->caller_data.raw_data_len); + if (zap_test_flag(zchan, ZAP_CHANNEL_OUTBOUND)) { + sig.event_id = ZAP_SIGEVENT_UP; + if ((status = data->sig_cb(&sig) != ZAP_SUCCESS)) { + zap_set_state_locked(zchan, ZAP_CHANNEL_STATE_HANGUP); + } + } else { + gen->MesType = Q931mes_CONNECT; + gen->BearerCap = 0; + Q931Rx43(&data->q931, (void *)gen, zchan->caller_data.raw_data_len); + } } break; case ZAP_CHANNEL_STATE_DIALING: @@ -220,25 +281,75 @@ static __inline__ void state_advance(zap_channel_t *zchan) Q931ie_ChanID ChanID; Q931ie_CallingNum CallingNum; Q931ie_CalledNum CalledNum; + Q931ie_CalledNum *ptrCalledNum; Q931InitIEBearerCap(&BearerCap); Q931InitIEChanID(&ChanID); Q931InitIECallingNum(&CallingNum); Q931InitIECalledNum(&CalledNum); - - //is cast right here? - ChanID.ChanSlot = (unsigned char)zchan->chan_id; - - //CallingNum.Digits - Q931InitMesGeneric(gen); gen->MesType = Q931mes_SETUP; + + BearerCap.CodStand = 0; /* ITU-T = 0, ISO/IEC = 1, National = 2, Network = 3 */ + BearerCap.ITC = 0; /* Speech */ + BearerCap.TransMode = 0; /* Circuit = 0, Packet = 1 */ + BearerCap.ITR = 16; /* 64k */ + BearerCap.Layer1Ident = 1; + BearerCap.UIL1Prot = 2; /* U-law (a-law = 3)*/ +#if 0 + BearerCap.SyncAsync = ; + BearerCap.Negot = ; + BearerCap.UserRate = ; + BearerCap.InterRate = ; + BearerCap.NIConTx = ; + BearerCap.FlowCtlTx = ; + BearerCap.HDR = ; + BearerCap.MultiFrame = ; + BearerCap.Mode = ; + BearerCap.LLInegot = ; + BearerCap.Assignor = ; + BearerCap.InBandNeg = ; + BearerCap.NumStopBits = ; + BearerCap.NumDataBits = ; + BearerCap.Parity = ; + BearerCap.DuplexMode = ; + BearerCap.ModemType = ; + BearerCap.Layer2Ident = ; + BearerCap.UIL2Prot = ; + BearerCap.Layer3Ident = ; + BearerCap.UIL3Prot = ; + BearerCap.AL3Info1 = ; + BearerCap.AL3Info2 = ; +#endif + gen->BearerCap = Q931AppendIE((L3UCHAR *) gen, (L3UCHAR *) &BearerCap); + + //is cast right here? + ChanID.IntType = 1; /* PRI = 1, BRI = 0 */ + ChanID.InfoChanSel = 1; + ChanID.ChanMapType = 3; /* B-Chan */ + ChanID.ChanSlot = (unsigned char)zchan->chan_id; gen->ChanID = Q931AppendIE((L3UCHAR *) gen, (L3UCHAR *) &ChanID); - gen->CallingNum = Q931AppendIE((L3UCHAR *) gen, (L3UCHAR *) &CallingNum); +#if 0 + CallingNum.Size += strlen(zchan->caller_data.cid_num); + gen->CallingNum = Q931AppendIE((L3UCHAR *) gen, (L3UCHAR *) &CallingNum); + zap_set_string((char *)CallingNum.Digit, zchan->caller_data.cid_num); + gen->Size += strlen(zchan->caller_data.cid_num); + + //zap_set_string(zchan->caller_data.dnis, (char *)callednum->Digit); + +#endif + CalledNum.TypNum = 2; + CalledNum.NumPlanID = 1; + printf("WTF: [%s]\n", zchan->caller_data.ani); + CalledNum.Size += strlen(zchan->caller_data.ani); gen->CalledNum = Q931AppendIE((L3UCHAR *) gen, (L3UCHAR *) &CalledNum); - + ptrCalledNum = Q931GetIEPtr(gen->CalledNum, gen->buf); + zap_copy_string((char *)ptrCalledNum->Digit, zchan->caller_data.ani, strlen(zchan->caller_data.ani)+1); + + //gen->Size += strlen(zchan->caller_data.ani); + Q931Rx43(&data->q931, (L3UCHAR *) gen, gen->Size); } break; case ZAP_CHANNEL_STATE_HANGUP: @@ -328,9 +439,11 @@ static void *zap_isdn_run(zap_thread_t *me, void *obj) len = sizeof(buf); if (zap_channel_read(data->dchan, buf, &len) == ZAP_SUCCESS) { #ifdef IODEBUG - char bb[4096] = ""; - print_bits(buf, (int)len, bb, sizeof(bb), ZAP_ENDIAN_LITTLE, 0); - zap_log(ZAP_LOG_DEBUG, "READ %d\n%s\n%s\n\n", (int)len, LINE, bb); + //char bb[4096] = ""; + //print_hex_bytes(buf, len, bb, sizeof(bb)); + + //print_bits(buf, (int)len, bb, sizeof(bb), ZAP_ENDIAN_LITTLE, 0); + //zap_log(ZAP_LOG_DEBUG, "READ %d\n%s\n%s\n\n", (int)len, LINE, bb); #endif Q921QueueHDLCFrame(&data->q921, buf, (int)len); @@ -374,6 +487,16 @@ zap_status_t zap_isdn_start(zap_span_t *span) return zap_thread_create_detached(zap_isdn_run, span); } +static int q931_rx_32(void *pvt,L3UCHAR *msg, L3INT mlen) +{ + int ret; + char bb[4096] = ""; + ret = Q921Rx32(pvt, msg, mlen); + print_hex_bytes(msg, mlen, bb, sizeof(bb)); + zap_log(ZAP_LOG_DEBUG, "WRITE %d\n%s\n%s\n\n", (int)mlen, LINE, bb); + return ret; +} + zap_status_t zap_isdn_configure_span(zap_span_t *span, Q921NetUser_t mode, Q931Dialect_t dialect, zio_signal_cb_t sig_cb) { uint32_t i, x = 0; @@ -428,12 +551,13 @@ zap_status_t zap_isdn_configure_span(zap_span_t *span, Q921NetUser_t mode, Q931D mode, span->trunk_type, zap_isdn_931_34, - (Q931TxCB_t)Q921Rx32, + (Q931TxCB_t)q931_rx_32, zap_isdn_931_err, &span->isdn_data->q921, span); span->isdn_data->q931.autoRestartAck = 1; + span->isdn_data->q931.autoConnectAck = 1; span->signal_type = ZAP_SIGTYPE_ISDN; span->outgoing_call = isdn_outgoing_call;