V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hiplon
V2EX  ›  宽带症候群

OpenWRT 通过 3G Modem 加 asterisk 将 GSM 通话转为 SIP

  •  
  •   hiplon · 2019-12-31 11:28:02 +08:00 · 5830 次点击
    这是一个创建于 1550 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本文主要实现 OpenWRT 系统通过 Huawei 3G Modem 加 asterisk 套件将 GSM 通话转为 SIP 通话

    安装 openwrt 下的 asterisk16 套件

    opkg update
    opkg install asterisk16-app-system asterisk16-chan-dongle asterisk16-pjsip asterisk16-codec-ulaw asterisk16-codec-alaw asterisk16-res-rtp-asterisk asterisk16-bridge-simple
    

    调整 PJSIP 作为默认服务,并且新增几个 PJSIP 账户,用以测试内线通

    /etc/asterisk/pjsip.conf

    [transport-udp]                                                        
    type=transport   
    protocol=udp    ;udp,tcp,tls,ws,wss                                        
    bind=0.0.0.0
    
    [6003]                                                                          
    type=endpoint                                                                 
    transport=transport-udp                                                      
    context=from-internal                                 
    disallow=all                                
    allow=ulaw                                                              
    auth=6003-auth                                                                
    aors=6003                                                                    
                                                                                   
    [6003-auth]                                                                             
    type=auth                                                                       
    auth_type=userpass                                                      
    password=6003                                                                     
    username=6003                                                                 
                                                                                
    [6003]                                                                        
    type=aor                                                                       
    max_contacts=1
    
    
    
    [6004]                                                                 
    type=endpoint                                                                 
    transport=transport-udp                                                        
    context=from-internal                                                              
    disallow=all                                                                    
    allow=ulaw                                                              
    auth=6004-auth                                                                     
    aors=6004                                                                     
                                                                                
    [6004-auth]                                                                   
    type=auth                                                                      
    auth_type=userpass                                                              
    password=6004                                                                   
    username=6004                                                                  
                                                                                   
    [6004]                                                                      
    type=aor                                                                              
    max_contacts=1 
    
    

    新增分机拨打模板,

    /etc/asterisk/extension.conf

    [from-internal]                                                               
    exten => _Z.,1,Dial(PJSIP/${EXTEN},60,Trg)                                        
    same => n,Hangup() 
    

    用 asterisk 查看分机状态,拨打过程中pjsip show endpoints中显示的状态会从Not in use转换为In use

    asterisk -rvvvv
    OpenWrt*CLI> pjsip show contacts
    
      Contact:  <Aor/ContactUri..............................> <Hash....> <Status> <RTT(ms)..>
    ==========================================================================================
    
      Contact:  6001/sip:[email protected]:53117;transport= 378e2db08b NonQual         nan
      Contact:  6001/sip:[email protected]:53117;transport= 378e2db08b NonQual         nan
      Contact:  6003/sip:[email protected]:49989;transport= 2ec100f865 NonQual         nan
      Contact:  6003/sip:[email protected]:49989;transport= 2ec100f865 NonQual         nan
      Contact:  6004/sip:[email protected]:5060              586381001a NonQual         nan
      Contact:  6004/sip:[email protected]:5060              586381001a NonQual         nan
    
    Objects found: 6
    
        -- PJSIP/6001-00000005 is ringing
        -- PJSIP/6001-00000005 is ringing
           > 0x2262b00 -- Strict RTP learning after remote address set to: 192.168.234.127:52518
        -- PJSIP/6001-00000005 answered PJSIP/6004-00000004
           > 0x2270c60 -- Strict RTP learning after remote address set to: 192.168.104.11:11866
        -- Channel PJSIP/6001-00000005 joined 'simple_bridge' basic-bridge <ee120657-8627-4868-b677-cb0d896a2b5a>
        -- Channel PJSIP/6004-00000004 joined 'simple_bridge' basic-bridge <ee120657-8627-4868-b677-cb0d896a2b5a>
           > 0x2262b00 -- Strict RTP switching to RTP target address 192.168.234.127:52518 as source
           > 0x2270c60 -- Strict RTP switching to RTP target address 192.168.104.11:11866 as source
    OpenWrt*CLI> pjsip show endpoints
    
     Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>
        I/OAuth:  <AuthId/UserName...........................................................>
            Aor:  <Aor............................................>  <MaxContact>
          Contact:  <Aor/ContactUri..........................> <Hash....> <Status> <RTT(ms)..>
      Transport:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress..................>
       Identify:  <Identify/Endpoint.........................................................>
            Match:  <criteria.........................>
        Channel:  <ChannelId......................................>  <State.....>  <Time.....>
            Exten: <DialedExten...........>  CLCID: <ConnectedLineCID.......>
    ==========================================================================================
    
     Endpoint:  6001                                                 In use        1 of inf
         InAuth:  6001-auth/6001
            Aor:  6001                                               1
          Contact:  6001/sip:[email protected]:53117;transp 378e2db08b NonQual         nan
      Transport:  transport-udp             udp      0      0  0.0.0.0:5060
        Channel: PJSIP/6001-00000005/AppDial                         Up            00:00:04   
            Exten:                           CLCID: "6004" <6004>
    
     Endpoint:  6002                                                 Unavailable   0 of inf
         InAuth:  6002-auth/6002
            Aor:  6002                                               1
      Transport:  transport-udp             udp      0      0  0.0.0.0:5060
    
     Endpoint:  6003                                                 Not in use    0 of inf
         InAuth:  6003-auth/6003
            Aor:  6003                                               1
          Contact:  6003/sip:[email protected]:49989;transp 2ec100f865 NonQual         nan
      Transport:  transport-udp             udp      0      0  0.0.0.0:5060
    
     Endpoint:  6004                                                 In use        1 of inf
         InAuth:  6004-auth/6004
            Aor:  6004                                               1
          Contact:  6004/sip:[email protected]:5060          586381001a NonQual         nan
      Transport:  transport-udp             udp      0      0  0.0.0.0:5060
        Channel: PJSIP/6004-00000004/Dial                            Up            00:00:04   
            Exten: 6001                      CLCID: "" <6001>
    
    
    Objects found: 4
    
           > 0x2270c60 -- Strict RTP learning complete - Locking on source address 192.168.104.11:11866
           > 0x2262b00 -- Strict RTP learning complete - Locking on source address 192.168.234.127:52518
    
    
    

    有条件的情况下建议可以考虑使用 IAX 分机替代 SIP 分机,这样只需要 NAT 打通一个 UDP 端口就能通话,而不用像 SIP 那样要考虑 ALG,ICE,STUN 等方案

    下面是新增一个 IAX 分机的用例

    opkg update
    opkg install asterisk16-chan-iax2
    

    /etc/asterisk/iax.conf

    [general]
    bindport=4569
    bindaddr=0.0.0.0
    iaxcompat=yes
    nochecksums=yes
    disallow=all
    allow=ulaw
    
    [6010]
    type=friend
    username=6010
    secret=6010
    context=from-internal
    host=dynamic
    callerid=6010<6010>
    

    /etc/asterisk/extensions.conf

    [from-internal]
    exten => 6010,1,Dial(IAX2/6010,60,Trg)
    

    由于 3G modem 还没到货,所以目前先更新到这里,等到货后继续配置。

    12 月 27 号收货,继续更,用到的型号是 Huawei K3765.

    K3765 3G modem 1

    K3765 3G modem 2 K3765 3G modem

    在 openwrt 下配置 dongle 设备,请结合实际数据配置

    /etc/asterisk/dongle.conf

    [general]
    interval=20
    [defaults]
    context=dongle-in
    group=0
    exten=+862022221234
    [dongle0]
    imei=123451234512345
    

    通过 asterisk 控制台查一下设备状态,

    asterisk -rvvvv
    RiWifi*CLI> dongle show devices
    ID           Group State      RSSI Mode Submode Provider Name  Model      Firmware          IMEI             IMSI             Number        
    dongle0      0     Free       16   3    3       FFFFFFFFFFFFFF K3765      11.126.03.04.521  123451234512345  123451234512345  Unknown 
    

    接下来新增呼出、呼入的分机配置

    /etc/asterisk/extensions.conf

    [from-internal]
    exten => _1.,1,Dial(Dongle/g0/${EXTEN:1}) ;呼出设置,结合实际,我这边是加了"1"这个前缀,例如我的 SIP 分机要拨打 10011,那么拨号就是 110011
    [dongle-in]
    exten => +862022221234,1,Dial(IAX2/6010,60,Trg) ;呼入设置,我这边就是配置成所有呼叫直接转到 IAX-6010 分机,复杂点的可以做 IVR,号码本,不过只有一路的电话就不需要搞这么复杂了。
    

    最后,拨号测试

    呼入测试

    呼入测试 1

    呼入测试 2

    呼出测试

    呼出测试 1

    呼出测试 2

    refer: https://www.ppuu.org/2019/12/openwrt-asterisk-dongle-gsm-to-sip/

    32 条回复    2021-07-26 20:46:09 +08:00
    Archeb
        1
    Archeb  
       2019-12-31 11:51:15 +08:00 via Android   ❤️ 1
    好复杂,对我来说转发个短信就够了

    不过感谢楼主分享精神
    tallest
        2
    tallest  
       2019-12-31 15:18:39 +08:00   ❤️ 1
    看着好像很好玩儿的样子!!
    privil
        3
    privil  
       2019-12-31 15:39:06 +08:00   ❤️ 1
    优秀!好厉害,之前找过类似的方案
    hiplon
        4
    hiplon  
    OP
       2019-12-31 15:56:06 +08:00
    @privil #3 多谢,写的比较简约
    MidLinn
        5
    MidLinn  
       2019-12-31 16:00:24 +08:00   ❤️ 1
    大佬!真会折腾,佩服一下。
    hiplon
        6
    hiplon  
    OP
       2019-12-31 16:02:24 +08:00
    @MidLinn #5 哈哈,主要还是因为我这边有张联通固话 SIM 卡,出了所在地就会没有信号了,所以才会想起用这个去解决
    cyang
        7
    cyang  
       2019-12-31 16:04:09 +08:00   ❤️ 1
    优秀!
    tallest
        8
    tallest  
       2019-12-31 17:08:28 +08:00
    话说,大佬,这玩意儿手机卡能不能玩儿啊?
    拨号用的啥拨号诶?系统自带的拨号盘可以嘛?
    LiYanHong
        9
    LiYanHong  
       2019-12-31 17:19:42 +08:00
    之前也想这样,出国把卡放家里,还能通过网络用卡,但没找到现成方案,只好用了双享号。
    想问下是虽然是 3g dongle,但是用的 gsm 网络?是否有短信方案,这样就完美了。
    hiplon
        10
    hiplon  
    OP
       2019-12-31 17:26:03 +08:00
    @LiYanHong #9 用的是 GSM 网络,处理短信的话在 extensions.conf 上面加处理指令就行
    下面单纯举个例子,还没测试可用性
    [dongle-in-sms]
    exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})})
    exten => sms,n,System(smstodingtalk '${CALLERID(num)}: ${BASE64_DECODE(${SMS_BASE64})} ${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME} -
    ${CALLERID(num)}: ${BASE64_DECODE(${SMS_BASE64})}' >> /var/log/asterisk/sms.txt) ;调用 smstodingtalk 脚本处理短信内容
    exten => sms,n,Hangup()

    [dongle-in-ussd]
    exten => ussd,1,Noop(Incoming USSD: ${BASE64_DECODE(${USSD_BASE64})})
    exten => ussd,n,System(smstodingtalk '${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME}: ${BASE64_DECODE(${USSD_BASE64})}')
    exten => ussd,n,Hangup()
    hiplon
        11
    hiplon  
    OP
       2019-12-31 17:32:17 +08:00
    @tallest #8 移动、联通手机卡可以的,符合制式就行,拨号我用的是支持 IAX 的 zoiper
    LiYanHong
        12
    LiYanHong  
       2019-12-31 17:32:26 +08:00
    我觉得把功能整合下,加个 webui,需求应该很大,国外党的福音。
    hiplon
        13
    hiplon  
    OP
       2019-12-31 17:37:06 +08:00
    @LiYanHong #12 如果用 WebUI 建议直接买个树莓派刷 RasPBX 就行啦,一般 OpenWRT 性能带不动这么大的 WebUI
    NSAgold
        14
    NSAgold  
       2019-12-31 18:53:57 +08:00 via Android
    近期不是说 2g 准备退网了么...这类 modem 现在应该没有支持 VoLTE 的吧
    ericFork
        15
    ericFork  
       2019-12-31 19:12:54 +08:00
    类似方案折腾过一整轮了,我来问个角度奇怪的问题,楼主的联通固话 SIM 是哪里办的?现在还能办吗?
    hiplon
        16
    hiplon  
    OP
       2019-12-31 20:23:49 +08:00
    @ericFork #15 现在好像比较难找了
    ericFork
        17
    ericFork  
       2019-12-31 20:43:25 +08:00
    @hiplon #16 你当年是在营业厅办的么?
    ziseyinzi
        18
    ziseyinzi  
       2019-12-31 20:47:35 +08:00 via Android
    联通要推 2G 了,有没有 VoHSPA 的方案
    isyu
        19
    isyu  
       2020-01-01 08:32:40 +08:00 via Android
    还有类似 iax 这样的语音标准,是易于通过 nat 的吗? SIP 和 h323 之类的,在 nat 环境下都有不少问题。
    hiplon
        20
    hiplon  
    OP
       2020-01-01 10:18:48 +08:00
    @isyu #19 应该没有了,毕竟 SIP 是事实上的行业标准,IAX 其实也很少会用于 UE 终端上面,平时更多的是做 trunk 用
    fengjueming
        21
    fengjueming  
       2020-01-01 11:38:31 +08:00
    自己的解决方案是 Raspbx+dongle,频段对得上的话是能走 WCDMA 的。
    关于 VoLTE,现在能直接买到的大厂的 USB Dongle 不支持语音,虽然可以买 Modem 自己魔改,但成本太高了。偷懒一点的办法就是找个支持 VoLTE 的手机,用 chan_mobile 走蓝牙。
    端口穿透的话我用的 WireGuard 接到服务器上转发的。
    ihipop
        22
    ihipop  
       2020-01-05 07:48:14 +08:00 via Android
    @fengjueming 联通电信都要退 2G 网了,尤其电信强推 VoLte 了,就没有办法了只能走蓝牙?
    fengjueming
        23
    fengjueming  
       2020-01-08 11:42:43 +08:00
    @ihipop 这款应该是支持 WCDMA 的,至少短时间内联通问题不大,长期来看真的只能走蓝牙(或者魔改一下 LTE 模块)
    hiplon
        24
    hiplon  
    OP
       2020-01-08 11:47:54 +08:00
    @fengjueming #23 测试过是支持 WCDMA 的,在 PC 端或者 usb_switchmode 修改一下制式就行,不过我这张固话卡只支持 GSM
    ihipop
        25
    ihipop  
       2020-01-08 17:48:38 +08:00 via Android
    @fengjueming 站内有相关帖子说有个别地方联通在逐步关闭 3G 语音通话了。。
    JingKeWu
        26
    JingKeWu  
       2020-01-10 10:22:20 +08:00
    有没有支持 4g 的网关
    qfdk
        27
    qfdk  
       2020-01-13 23:04:59 +08:00 via iPhone
    最近研究发短信的方案 现在买了个 SIM800C, 打算写个 api 来调用, 不知道老哥有没有现成的方案? 就是模拟个短信🐱发订单信息, 第三方的简直太贵了
    hiplon
        28
    hiplon  
    OP
       2020-01-14 08:24:50 +08:00
    @qfdk #27 如果 asterisk 能支持该模块用 asterisk 发短信很简单,类似 asterisk -rx 'gsm send sms 3 1357080XXXX "hello, this is openvox gsm card"'就行;
    如果 asterisk 不直接支持可能要看一下用 AT 指令去发。
    skydrive
        29
    skydrive  
       2020-06-20 13:35:40 +08:00
    @hiplon 大佬,按你的教程安装、配置了 asterisk,用的也是华为的 K3765,但是接收短信时报错了:

    WARNING[4523]: at_response.c:1311 at_response_cmgr: [dongle0] Error parsing incoming message: Cannot parse UCS-2

    不知道你有没有碰到过这样的问题

    dongle show devices
    ID Group State RSSI Mode Submode Provider Name Model Firmware IMEI IMSI Number
    dongle0 0 Free 24 3 3 CHINA MOBILE K3765 11.126.03.09.00 xxxxxxxxxx xxxxxxxx Unknown

    dongle show device state dongle0
    -------------- Status -------------
    Device : dongle0
    State : Free
    Audio : /dev/ttyUSB1
    Data : /dev/ttyUSB2
    Voice : Yes
    SMS : Yes
    Manufacturer : huawei
    Model : K3765
    Firmware : 11.126.03.09.00
    IMEI : xxxxxxxxxxxx
    IMSI : xxxxxxxxxxxxx
    GSM Registration Status : Registered, roaming
    RSSI : 24, -65 dBm
    Mode : GSM/GPRS
    Submode : EDGE
    Provider Name : CHINA MOBILE
    Location area code : 2400A5
    Cell ID : 104C
    Subscriber Number : Unknown
    SMS Service Center : +86138xxxxxxxx
    Use UCS-2 encoding : No
    Tasks in queue : 0
    Commands in queue : 0
    Call Waiting : Disabled
    Current device state : start
    Desired device state : start
    When change state : now
    Calls/Channels : 0
    Active : 0
    Held : 0
    Dialing : 0
    Alerting : 0
    Incoming : 0
    Waiting : 0
    Releasing : 0
    skydrive
        30
    skydrive  
       2020-06-20 18:46:27 +08:00
    忽略上一条吧... 通过降级 asterisk16-chan-dongle 解决了,最新版的有 bug...
    acbot
        31
    acbot  
       2021-07-26 12:30:25 +08:00
    openwrt-asterisk-dongle-gsm-to-sip 这个咋我不能打开
    hiplon
        32
    hiplon  
    OP
       2021-07-26 20:46:09 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2625 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 15:36 · PVG 23:36 · LAX 08:36 · JFK 11:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.