<commit_before>/*
 * (C) 2009-2011 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;
using FsoFramework;

const bool MAINLOOP_CALL_AGAIN = true;
const bool MAINLOOP_DONT_CALL_AGAIN = false;

const int BUFFER_SIZE = 8192;

char[] buffer;

const int LINE_DISCIPLINE_N_GSM = 21;
const uchar MKNOD_TTYGSM_MAJOR = 249;

//========================================================================//
public class Terminal : Object
//========================================================================//
{
    Transport transport;
    weak Options options;
    Reader reader;
    int fd;
    int oldisc;

    string transportname;
    string portname;

    public Terminal( Options option )
    {
        buffer = new char[BUFFER_SIZE];
        this.options = option;
    }

    public void close()
    {
        if ( Options.muxmode != -1 )
        {
            fsoMessage( @"Stopping MUX session" );
            transport.thaw();
            transport.close();
            transport.open();
            fd = transport.freeze();
            fsoMessage( @"Resetting line discipline" );
            Linux.ioctl( fd, Linux.Termios.TIOCSETD, &oldisc );
            transport.thaw();
            transport.close();
            transport = null;
            return;
        }
    }

    public void setMuxMode( int mode )
    {
        fsoMessage( "Opening port..." );

        transport = new SerialTransport( portname, options.portspeed );
        if ( !transport.open() )
        {
            quitWithMessage( @"Can't open transport: $(strerror(errno))" );
            return;
        }

        fsoMessage( "Entering MUX mode..." );

        var buffer = new char[128];
        var command = "\r\nATE0Q0V1\r\n";
        var bread = transport.writeAndRead( command, command.length, buffer, 128 );
        command = mode == 0 ? "AT+CMUX=0\r\n" : "AT+CMUX=1,0,5,64\r\n";
        bread = transport.writeAndRead( command, command.length, buffer, 128 );
        buffer[bread] = '\0';
        var response = ( (string)buffer ).strip();
        if ( response != "OK" )
        {
            quitWithMessage( @"Can't enter MUX mode: Modem answered '$response'" );
            return;
        }

        fsoMessage( "Setting line discipline..." );

        fd = transport.freeze();
        int ldisc = LINE_DISCIPLINE_N_GSM;

        if ( Linux.ioctl( fd, Linux.Termios.TIOCGETD, &oldisc ) == -1 )
        {
            quitWithMessage( @"Can't get old line discipline: $(strerror(errno))" );
            return;
        }

        if ( Linux.ioctl( fd, Linux.Termios.TIOCSETD, &ldisc ) == -1 )
        {
            quitWithMessage( @"Can't set N_GSM line discipline: $(strerror(errno))" );
            return;
        }

        var muxconfig = LinuxExt.Gsm.Config();
        if ( Linux.ioctl( fd, LinuxExt.Gsm.GSMIOC_GETCONF, &muxconfig ) == -1 )
        {
            quitWithMessage( @"Can't get N_GSM configuration: $(strerror(errno))" );
            Linux.ioctl( fd, Linux.Termios.TIOCSETD, &oldisc );
            return;
        }
        muxconfig.initiator = 1;
        muxconfig.encapsulation = mode;
        muxconfig.mru = 127;
        muxconfig.mtu = 127;
        if ( Linux.ioctl( fd, LinuxExt.Gsm.GSMIOC_SETCONF, &muxconfig ) == -1 )
        {
            quitWithMessage( @"Can't set N_GSM configuration: $(strerror(errno))" );
            Linux.ioctl( fd, Linux.Termios.TIOCSETD, &oldisc );
            return;
        }

        fsoMessage( "Creating device nodes... (needs root privileges)" );
        for ( int i = 1; i < 8; ++i )
        {
            Posix.mknod( @"/dev/ttygsm$i", Posix.S_IFCHR | 0666, Linux.makedev( MKNOD_TTYGSM_MAJOR, i ) );
        }

        fsoMessage( "MUX mode established, you can now use /dev/ttygsm[1-n]" );
    }

    public bool open()<commit_msg>mdbus2: fix compilation with older vala versions<commit_after>/*
 * (C) 2009-2011 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;
using FsoFramework;

const bool MAINLOOP_CALL_AGAIN = true;
const bool MAINLOOP_DONT_CALL_AGAIN = false;

const int BUFFER_SIZE = 8192;

char[] buffer;

const int LINE_DISCIPLINE_N_GSM = 21;
const uchar MKNOD_TTYGSM_MAJOR = 249;

//========================================================================//
public class Terminal : Object
//========================================================================//
{
    Transport transport;
    weak Options options;
    Reader reader;
    int fd;
    int oldisc;

    string transportname;
    string portname;

    public Terminal( Options option )
    {
        buffer = new char[BUFFER_SIZE];
        this.options = option;
    }

    public void close()
    {
        if ( Options.muxmode != -1 )
        {
            fsoMessage( @"Stopping MUX session" );
            transport.thaw();
            transport.close();
            transport.open();
            fd = transport.freeze();
            fsoMessage( @"Resetting line discipline" );
            Linux.ioctl( fd, Linux.Termios.TIOCSETD, &oldisc );
            transport.thaw();
            transport.close();
            transport = null;
            return;
        }
    }

    public void setMuxMode( int mode )
    {
        fsoMessage( "Opening port..." );

        transport = new SerialTransport( portname, options.portspeed );
        if ( !transport.open() )
        {
            quitWithMessage( @"Can't open transport: $(strerror(errno))" );
            return;
        }

        fsoMessage( "Entering MUX mode..." );

        var buffer = new char[128];
        var command = "\r\nATE0Q0V1\r\n";
        var bread = transport.writeAndRead( command, command.length, buffer, 128 );
        command = mode == 0 ? "AT+CMUX=0\r\n" : "AT+CMUX=1,0,5,64\r\n";
        bread = transport.writeAndRead( command, command.length, buffer, 128 );
        buffer[bread] = '\0';
        var response = ( (string)buffer ).strip();
        if ( response != "OK" )
        {
            quitWithMessage( @"Can't enter MUX mode: Modem answered '$response'" );
            return;
        }

        fsoMessage( "Setting line discipline..." );

        fd = transport.freeze();
        int ldisc = LINE_DISCIPLINE_N_GSM;

        if ( Linux.ioctl( fd, Linux.Termios.TIOCGETD, &oldisc ) == -1 )
        {
            quitWithMessage( @"Can't get old line discipline: $(strerror(errno))" );
            return;
        }

        if ( Linux.ioctl( fd, Linux.Termios.TIOCSETD, &ldisc ) == -1 )
        {
            quitWithMessage( @"Can't set N_GSM line discipline: $(strerror(errno))" );
            return;
        }

        var muxconfig = LinuxExt.Gsm.Config();
        if ( Linux.ioctl( fd, LinuxExt.Gsm.GSMIOC_GETCONF, &muxconfig ) == -1 )
        {
            quitWithMessage( @"Can't get N_GSM configuration: $(strerror(errno))" );
            Linux.ioctl( fd, Linux.Termios.TIOCSETD, &oldisc );
            return;
        }
        muxconfig.initiator = 1;
        muxconfig.encapsulation = mode;
        muxconfig.mru = 127;
        muxconfig.mtu = 127;
        if ( Linux.ioctl( fd, LinuxExt.Gsm.GSMIOC_SETCONF, &muxconfig ) == -1 )
        {
            quitWithMessage( @"Can't set N_GSM configuration: $(strerror(errno))" );
            Linux.ioctl( fd, Linux.Termios.TIOCSETD, &oldisc );
            return;
        }

        fsoMessage( "Creating device nodes... (needs root privileges)" );
        for ( int i = 1; i < 8; ++i )
        {
            Posix.mknod( @"/dev/ttygsm$i", Posix.S_IFCHR | 0666, LinuxExt.makedev( MKNOD_TTYGSM_MAJOR, i ) );
        }

        fsoMessage( "MUX mode established, you can now use /dev/ttygsm[1-n]" );
    }

    public bool open()
{'fraction_non_alphanumeric': 0.14426048565121413, 'fraction_numerical': 0.015121412803532009, 'mean_word_length': 2.2535008976660684, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6177235', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2640, 'n_words': 775}