Skip to content

Commit

Permalink
add dongles command
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Jan 31, 2017
1 parent a9752e9 commit e519633
Show file tree
Hide file tree
Showing 10 changed files with 1,140 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

type Node interface {
Begin() int
End() int
Text() string
}

type Context struct {
Head Node
Templates []Node
Assignments []AsignStmt
Objects []Object
}

type Template Context

type AsignStmt struct {
Left []Node
Equal Node // =
Right []Node
}

type Object struct {
Left []Node
Assign Node // =>
Right []Node
}

type File struct {
Comments []Node
Contextx []Context
Assignments []AsignStmt
Objects []Object
Templates []Template
}
148 changes: 148 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package main

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/urfave/cli"
)

type DongleConfig map[string]struct {
IMEI string `json:"imei"`
IMSI string `json:"imsi"`
}

func ToAST(c DongleConfig) *Ast {
a := &Ast{}
for k, v := range c {
s := &NodeSection{name: k}
s.values = append(s.values, &nodeIdent{
key: "imei",
value: v.IMEI,
})
s.values = append(s.values, &nodeIdent{
key: "imsi",
value: v.IMSI,
})
a.Sections = append(a.Sections, s)
}
return a
}

func Dongles(ctx *cli.Context) error {
src := ctx.Args().First()
var b []byte
var err error
if src == "stdin" {
b, err = ReadFromStdin()
if err != nil {
return err
}
} else {
if src == "" {
return errors.New("either supply a config file or pip stuff to stdin")
}
b, err = ioutil.ReadFile(src)
if err != nil {
return err
}
}
c := make(DongleConfig)
err = json.Unmarshal(b, &c)
if err != nil {
return err
}
a := ToAST(c)
o, err := PatchAst(a)
if err != nil {
return err
}
var buf bytes.Buffer
PrintAst(&buf, o)
fmt.Println(&buf)
return nil
}

func PatchAst(dst *Ast) (*Ast, error) {
name := filepath.Join(asteriskDir(), "modem.conf")
b, err := ioutil.ReadFile(name)
if err != nil {
return nil, err
}
p, err := NewParser(bytes.NewReader(b))
if err != nil {
return nil, err
}

a, err := p.Parse()
if err != nil {
return nil, err
}

patch := &Ast{}
for _, s := range a.Sections {
for _, v := range dst.Sections {
if v.name == s.name {
patch.Sections = append(patch.Sections, v)
} else {
patch.Sections = append(patch.Sections, s)
}
}
}

var buf bytes.Buffer
PrintAst(&buf, patch)
fmt.Println(&buf)
o := &Ast{}
fmt.Println(len(patch.Sections))
for _, v := range patch.Sections {
for _, i := range v.values {
if i.key == "imei" {
if n := byIMEI(dst, i.value); n != nil {
if n.name == v.name {
continue
}
o.Sections = append(o.Sections, n)
continue
}
}
}
o.Sections = append(o.Sections, v)
}
fmt.Println("HERE")
return o, nil
}

func byIMEI(a *Ast, imei string) *NodeSection {
for _, s := range a.Sections {
for _, v := range s.values {
if v.key == "imei" && v.value == imei {
return s
}
}
}
return nil
}
func bySection(a *Ast, name string) *NodeSection {
for _, s := range a.Sections {
if s.name == name {
return s
}
}
return nil
}

func asteriskDir() string {
return os.Getenv("ASTERISK_CONFIG")
}

func ReadFromStdin() ([]byte, error) {
r := bufio.NewReader(os.Stdin)
return r.ReadBytes('\n')
}
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"log"
"os"

"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()
app.Version = "0.4.7"
app.Name = "fastc"
app.Usage = "configures asterisk using json"
app.Commands = []cli.Command{
{
Name: "dongles",
Aliases: []string{"e"},
Usage: "configures asterisk dongles with json",
Action: Dongles,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatalf("fconf: %v", err)
}
}
101 changes: 101 additions & 0 deletions modem.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
; example configuration file for the 4G modems[general]


interval=15 ; Number of seconds between trying to connect to devices

;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
;jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of a
; Dongle channel. Defaults to "no". An enabled jitterbuffer will
; be used only if the sending side can create and the receiving
; side can not accept jitter. The Dongle channel can't accept jitter,
; thus an enabled jitterbuffer on the receive Dongle side will always
; be used if the sending side can create jitter.

;jbforce = no ; Forces the use of a jitterbuffer on the receive side of a Dongle
; channel. Defaults to "no".

;jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.

;jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
; resynchronized. Useful to improve the quality of the voice, with
; big jumps in/broken timestamps, usually sent from exotic devices
; and programs. Defaults to 1000.

;jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of a Dongle
; channel. Two implementations are currently available - "fixed"
; (with size always equals to jbmaxsize) and "adaptive" (with
; variable size, actually the new jb of IAX2). Defaults to fixed.

;jbtargetextra = 40 ; This option only affects the jb when 'jbimpl = adaptive' is set.
; The option represents the number of milliseconds by which the new jitter buffer
; will pad its size. the default is 40, so without modification, the new
; jitter buffer will set its size to the jitter value plus 40 milliseconds.
; increasing this value may help if your network normally has low jitter,
; but occasionally has spikes.

;jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
;-----------------------------------------------------------------------------------

[defaults]
; now you can set here any not required device settings as template
; sure you can overwrite in any [device] section this default values

context=from-trunk ; context for incoming calls
group=0 ; calling group
rxgain=2 ; increase the incoming volume; may be negative
txgain=1 ; increase the outgoint volume; may be negative
autodeletesms=yes ; auto delete incoming sms
resetdongle=yes ; reset dongle during initialization with ATZ command
u2diag=0 ; set ^U2DIAG parameter on device (0 = disable everything except modem function) ; -1 not use ^U2DIAG command
usecallingpres=yes ; use the caller ID presentation or not
callingpres=allowed_passed_screen ; set caller ID presentation by default use default network settings
disablesms=no ; disable of SMS reading from device when received
; chan_dongle has currently a bug with SMS reception. When a SMS gets in during a
; call chan_dongle might crash. Enable this option to disable sms reception.
; default = no

language=en ; set channel default language
smsaspdu=yes ; if 'yes' send SMS in PDU mode, feature implementation incomplete and we strongly recommend say 'yes'
mindtmfgap=45 ; minimal interval from end of previews DTMF from begining of next in ms
mindtmfduration=80 ; minimal DTMF tone duration in ms
mindtmfinterval=200 ; minimal interval between ends of DTMF of same digits in ms

callwaiting=no ; if 'yes' allow incoming calls waiting; by default use network settings
; if 'no' waiting calls just ignored
disable=no ; OBSOLETED by initstate: if 'yes' no load this device and just ignore this section

initstate=start ; specified initial state of device, must be one of 'stop' 'start' 'remote'
; 'remove' same as 'disable=yes'

exten=+1234567890 ; exten for start incoming calls, only in case of Subscriber Number not available!, also set to CALLERID(ndid)

dtmf=relax ; control of incoming DTMF detection, possible values:
; off - off DTMF tones detection, voice data passed to asterisk unaltered
; use this value for gateways or if not use DTMF for AVR or inside dialplan
; inband - do DTMF tones detection
; relax - like inband but with relaxdtmf option
; default is 'relax' by compatibility reason

; dongle required settings
;[dongle0]
;audio=/dev/ttyUSB1 ; tty port for audio connection; no default value
;data=/dev/ttyUSB2 ; tty port for AT commands; no default value
; or you can omit both audio and data together and use imei=123456789012345 and/or imsi=123456789012345
; imei and imsi must contain exactly 15 digits !
; imei/imsi discovery is available on Linux only
;imei=123456789012345
;imsi=123456789012345
; if audio and data set together with imei and/or imsi audio and data has precedence
; you can use both imei and imsi together in this case exact match by imei and imsi required

[airtel1]
imei=353220047976425
;imsi=640021046580298

[tigo1]
imei=352215045819420
;imei=354369047238739

[vodacom1]
imei=354369047238580

Loading

0 comments on commit e519633

Please sign in to comment.