What is the best way to connect multiple FreePBX together?

The subject was raised earlier about CID issues with peer trunks. Since it has come up recently for me, I thought I would share a bit of wisdom. We all know that extensions have two CID numbers, the local extension number for internal calls, and the Outbound CID for PSTN calls. When a call leaves the system, only one CID is used and which one depends on whether the outbound route is configured as intra-company or not. The other CID is lost unless steps are taken to preserve it.

The following macro can be used on each end of an intra-company IAX trunk. The idea is that local ext to ext calls between the systems use the local extension number as expected, but should the call go out an outbound route on the far end, the CID will be rewritten to the Outbound CID of the extension making the call (easier to do than explain).

[macro-dialout-trunk-predial-hook]
exten => s,1,Noop(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf)
;exten => s,n,DumpChan   ; uncomment for debug

; determine if call is intracompany or not
exten => s,n,GotoIf($["${INTRACOMPANYROUTE}"="YES"]?intra:pstn)

; calls out intracompany routes come here
exten => s,n(intra),Noop(This call uses an Intra-Company route)
; if the call is from a local extension store the outbound CID in IAXVAR: X-OUTBOUND-CID
exten => s,n,ExecIf($[${DB_EXISTS(AMPUSER/${AMPUSER}/outboundcid)}]?set(IAXVAR(X-OUTBOUND-CID)=${DB(AMPUSER/${AMPUSER}/outboundcid)}))
exten => s,n,ExecIf($[${DB_EXISTS(AMPUSER/${AMPUSER}/outboundcid)}]?Noop(Storing outbound cid for remote use: ${IAXVAR(X-OUTBOUND-CID)}))
exten => s,n,goto(end)

; calls out pstn routes come here
exten => s,n(pstn),Noop(This call uses a PSTN route)
; calls from local extensions don't need any changes
exten => s,n,ExecIf($[${DB_EXISTS(AMPUSER/${AMPUSER}/cidnum)}]?goto(end))
; non-local pstn calls need CID rewritten if IAXVAR: X-OUTBOUND-CID has been set previously
exten => s,n,ExecIf($["${IAXVAR(X-OUTBOUND-CID)}"!=""]?set(CALLERID(number)=${IAXVAR(X-OUTBOUND-CID)}))
exten => s,n,ExecIf($["${IAXVAR(X-OUTBOUND-CID)}"!=""]?Noop(Rewriting outbound CID to ${IAXVAR(X-OUTBOUND-CID)}))
exten => s,n,goto(end)

exten => s,n(end),MacroExit

;  --==> end of context [macro-dialout-trunk-predial-hook] <==--
3 Likes