[Asterisk-Users] Grandstream bin cfg.txt generator

Leon de Rooij leon at scarlet-internet.nl
Wed Sep 22 02:05:02 MST 2004


Hi,

I needed to create config files for downloading to Grandstream devices and made a little script for it. It seems to work fine for the HT486.
The script needs a config file (cfg.in) which is in this format:

P2 = blah
P10   = hrm
...etc...

The configfile may contain comments (starting with '#') and empty lines. Mind that the 'gnkey=0b82' shouldn't be in the configfile, as it's already appended by the script.

Hope it's useful..

Thanks to Stephen R. Besch for information about the format of this file !

(One thing I am not 100% sure of: do I have to append zeros to the end of the body until it has an even amount of bytes, or an even amount of words ? Right now, I do both.)

Regards,

Leon de Rooij

--------------

#!/usr/bin/perl -w
use strict;

my $h_mac  = '000b82014e20'; # hexadecimal mac address
my $f_in   = 'cfg.in';       # file body, configfile containing all parameters
my $f_out  = 'cfg.txt';      # the configfile that will be written to

my $h_crlf = '0d0a';         # hexadecimal crlf

# convert some things to binary
my $b_mac  = pack("H12", $h_mac); # convert 12 hex numbers to bin
my $b_crlf = pack("H4", $h_crlf); # convert 4 hex numbers to bin

# open configfile and make body in ascii (a_body)
my $a_body;
open F,$f_in;
while (<F>) {
	chomp;      # remove trailing lf
	s/\#.*$//g; # remove comments
	s/\s//g;    # remove all whitespace
	$a_body .= $_.'&' if length > 0;
}
close F;
$a_body .='gnkey=0b82';

# add an extra byte to make the body even (bytewise)
$a_body .= "\0" if ((length($a_body) % 2) ne 0);

# add an extra word ( = two bytes) to make the body even (wordwise)
$a_body .= "\0\0" if ((length($a_body) % 4) ne 0);

# generate a d_length (length of the complete message, counting words, in dec)
# ( header is always 8 words lang ) + ( body in ascii (bytes) / 2 = in words )
my $d_length = 8 + (length($a_body)/2);

# make that a binary dword
my $b_length = pack("N", $d_length);

# generate a checksum
my $d_checksum;
foreach ($b_length,$b_mac,$b_crlf,$b_crlf,$a_body) {
	$d_checksum += unpack("%16n*", $_);
}
#$d_checksum %= 65536;

$d_checksum = 65536-$d_checksum;

# and make a binary word of that
my $b_checksum = pack("n", $d_checksum);

# and write the config back to disk, in a grandstream readable format
open F,">$f_out";
binmode F;
print F $b_length;
print F $b_checksum;
print F $b_mac;
print F $b_crlf;
print F $b_crlf;
print F $a_body;
close F;




More information about the asterisk-users mailing list