Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

modem.c 10.8 KB
Newer Older
deuce's avatar
deuce committed
1 2
/* Copyright (C), 2007 by Stephen Hurd */

3
/* $Id: modem.c,v 1.32 2020/06/27 08:27:39 deuce Exp $ */
deuce's avatar
deuce committed
4 5 6 7

#include <stdlib.h>

#include "comio.h"
8
#include "ciolib.h"
deuce's avatar
deuce committed
9 10 11

#include "sockwrap.h"

deuce's avatar
deuce committed
12
#include "modem.h"
deuce's avatar
deuce committed
13 14 15 16 17
#include "syncterm.h"
#include "bbslist.h"
#include "conn.h"
#include "uifcinit.h"

18
static COM_HANDLE com=COM_HANDLE_INVALID;
deuce's avatar
deuce committed
19 20 21

void modem_input_thread(void *args)
{
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    int     rd;
    int buffered;
    size_t  buffer;
    BOOL    monitor_dsr=TRUE;

    SetThreadName("Modem Input");
    conn_api.input_thread_running=1;
    if(args != NULL) {
        if((comGetModemStatus(com)&COM_DSR) == 0)
            monitor_dsr=FALSE;
    }
    while(com != COM_HANDLE_INVALID && !conn_api.terminate) {
        rd=comReadBuf(com, (char *)conn_api.rd_buf, conn_api.rd_buf_size, NULL, 100);
        buffered=0;
        while(buffered < rd) {
            pthread_mutex_lock(&(conn_inbuf.mutex));
            buffer=conn_buf_wait_free(&conn_inbuf, rd-buffered, 100);
            buffered+=conn_buf_put(&conn_inbuf, conn_api.rd_buf+buffered, buffer);
            pthread_mutex_unlock(&(conn_inbuf.mutex));
        }
        if(args==NULL) {
            if((comGetModemStatus(com)&COM_DCD) == 0)
                break;
        }
        else if(monitor_dsr) {
            if((comGetModemStatus(com)&COM_DSR) == 0)
                break;
        }
    }
    if(args != NULL)
        comLowerDTR(com);
53
    conn_api.input_thread_running=2;
deuce's avatar
deuce committed
54 55 56 57
}

void modem_output_thread(void *args)
{
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    int     wr;
    int     ret;
    int sent;
    BOOL    monitor_dsr=TRUE;

    SetThreadName("Modem Output");
    conn_api.output_thread_running=1;
    if(args != NULL) {
        if((comGetModemStatus(com)&COM_DSR) == 0)
            monitor_dsr=FALSE;
    }
    while(com != COM_HANDLE_INVALID && !conn_api.terminate) {
        pthread_mutex_lock(&(conn_outbuf.mutex));
        wr=conn_buf_wait_bytes(&conn_outbuf, 1, 100);
        if(wr) {
            wr=conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
            pthread_mutex_unlock(&(conn_outbuf.mutex));
            sent=0;
            while(sent < wr) {
                ret=comWriteBuf(com, conn_api.wr_buf+sent, wr-sent);
                sent+=ret;
                if(ret==COM_ERROR)
                    break;
            }
        }
        else
            pthread_mutex_unlock(&(conn_outbuf.mutex));
        if(args==NULL) {
            if((comGetModemStatus(com)&COM_DCD) == 0)
                break;
        }
        else if(monitor_dsr) {
            if((comGetModemStatus(com)&COM_DSR) == 0)
                break;
        }
    }
94
    conn_api.output_thread_running=2;
deuce's avatar
deuce committed
95 96 97 98
}

int modem_response(char *str, size_t maxlen, int timeout)
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    char    ch;
    size_t  len=0;
    time_t  start;

    start=time(NULL);
    while(1){
        /* Abort with keystroke */
        if(kbhit()) {
            switch(getch()) {
                case 0:
                case 0xe0:
                    getch();
            }
            return(1);
        }

        if(time(NULL)-start >= timeout)
            return(-1);
        if(len >= maxlen)
            return(-1);
        if(!comReadByte(com, (unsigned char *)&ch)) {
            YIELD();
            continue;
        }
        if(ch<' ' && len==0)    /* ignore prepended control chars */
            continue;

        if(ch=='\r') {
//          while(comReadByte(com,&ch));    /* eat trailing ctrl chars (e.g. 'LF') */
            break;
        }
        str[len++]=ch;
    }
    str[len]=0;

    return(0);
deuce's avatar
deuce committed
135 136 137 138
}

int modem_connect(struct bbslist *bbs)
{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    int     ret;
    char    respbuf[1024];

    if (!bbs->hidepopups)
        init_uifc(TRUE, TRUE);

    if(bbs->conn_type == CONN_TYPE_SERIAL) {
        if((com=comOpen(bbs->addr)) == COM_HANDLE_INVALID) {
            if (!bbs->hidepopups)
                uifcmsg("Cannot Open Port", "`Cannot Open Port`\n\n"
                    "Cannot open the specified serial device.\n");
            conn_api.terminate=-1;
            return(-1);
        }
        if(bbs->bpsrate) {
            if(!comSetBaudRate(com, bbs->bpsrate)) {
                if (!bbs->hidepopups)
                    uifcmsg("Cannot Set Baud Rate", "`Cannot Set Baud Rate`\n\n"
                        "Cannot open the specified serial device.\n");
                conn_api.terminate=-1;
                comClose(com);
                return(-1);
            }
        }
        if(!comRaiseDTR(com)) {
            if (!bbs->hidepopups)
                uifcmsg("Cannot Raise DTR", "`Cannot Raise DTR`\n\n"
                    "comRaiseDTR() returned an error.\n");
            conn_api.terminate=-1;
            comClose(com);
            return(-1);
        }
    }
    else {
        if((com=comOpen(settings.mdm.device_name)) == COM_HANDLE_INVALID) {
            if (!bbs->hidepopups)
                uifcmsg("Cannot Open Modem",    "`Cannot Open Modem`\n\n"
                    "Cannot open the specified modem device.\n");
            conn_api.terminate=-1;
            return(-1);
        }
        if(settings.mdm.com_rate) {
            if(!comSetBaudRate(com, settings.mdm.com_rate)) {
                if (!bbs->hidepopups)
                    uifcmsg("Cannot Set Baud Rate", "`Cannot Set Baud Rate`\n\n"
                        "Cannot open the specified modem device.\n");
                conn_api.terminate=-1;
                comClose(com);
                return(-1);
            }
        }
        if(!comRaiseDTR(com)) {
            if (!bbs->hidepopups)
                uifcmsg("Cannot Raise DTR", "`Cannot Raise DTR`\n\n"
                    "comRaiseDTR() returned an error.\n");
            conn_api.terminate=-1;
            comClose(com);
            return(-1);
        }

        /* drain keyboard input to avoid accidental cancel */
        while(kbhit())
            getch();

        if (!bbs->hidepopups)
            uifc.pop("Initializing...");

        comWriteString(com, settings.mdm.init_string);
        comWriteString(com, "\r");

        /* Wait for "OK" */
        while(1) {
            if((ret=modem_response(respbuf, sizeof(respbuf), 5))!=0) {
                modem_close();
                if (!bbs->hidepopups) {
                    uifc.pop(NULL);
                    if(ret<0)
                        uifcmsg("Modem Not Responding", "`Modem Not Responding`\n\n"
                            "The modem did not respond to the initializtion string\n"
                            "Check your init string and phone number.\n");
                }
                conn_api.terminate=-1;
                return(-1);
            }
            if(strstr(respbuf, settings.mdm.init_string))   /* Echo is on */
                continue;
            break;
        }

        if(!strstr(respbuf, "OK")) {
            modem_close();
            if (!bbs->hidepopups) {
                uifc.pop(NULL);
                uifcmsg(respbuf,    "`Initialization Error`\n\n"
                    "The modem did not respond favorably to your initialization string.\n");
            }
            conn_api.terminate=-1;
            return(-1);
        }

        if (!bbs->hidepopups) {
            uifc.pop(NULL);
            uifc.pop("Dialing...");
        }
        comWriteString(com, settings.mdm.dial_string);
        comWriteString(com, bbs->addr);
        comWriteString(com, "\r");

        /* Wait for "CONNECT" */
        while(1) {
            if((ret=modem_response(respbuf, sizeof(respbuf), 60))!=0) {
                modem_close();
                if (!bbs->hidepopups) {
                    uifc.pop(NULL);
                    if(ret<0)
                        uifcmsg(respbuf,    "`No Answer`\n\n"
                            "The modem did not connect within 60 seconds.\n");
                }
                conn_api.terminate=-1;
                return(-1);
            }
            if(strstr(respbuf, bbs->addr))  /* Dial command echoed */
                continue;
            break;
        }

        if(!strstr(respbuf, "CONNECT")) {
            modem_close();
            if (!bbs->hidepopups) {
                uifc.pop(NULL);
                uifcmsg(respbuf,    "`Connection Failed`\n\n"
                    "SyncTERM was unable to establish a connection.\n");
            }
            conn_api.terminate=-1;
            return(-1);
        }

        if (!bbs->hidepopups) {
            uifc.pop(NULL);
            uifc.pop(respbuf);
            SLEEP(1000);
            uifc.pop(NULL);
        }
    }

    if (!comSetFlowControl(com, bbs->flow_control)) {
        conn_api.close();
        if (!bbs->hidepopups) {
287
            uifcmsg("Failed to set Flow Control",    "`Failed to set Flow Control`\n\n"
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
                "SyncTERM was unable to set flow control.\n");
        }
        return(-1);
    }

    if(!create_conn_buf(&conn_inbuf, BUFFER_SIZE)) {
        conn_api.close();
        return(-1);
    }
    if(!create_conn_buf(&conn_outbuf, BUFFER_SIZE)) {
        conn_api.close();
        destroy_conn_buf(&conn_inbuf);
        return(-1);
    }
    if(!(conn_api.rd_buf=(unsigned char *)malloc(BUFFER_SIZE))) {
        conn_api.close();
        destroy_conn_buf(&conn_inbuf);
        destroy_conn_buf(&conn_outbuf);
        return(-1);
    }
    conn_api.rd_buf_size=BUFFER_SIZE;
    if(!(conn_api.wr_buf=(unsigned char *)malloc(BUFFER_SIZE))) {
        conn_api.close();
        destroy_conn_buf(&conn_inbuf);
        destroy_conn_buf(&conn_outbuf);
        FREE_AND_NULL(conn_api.rd_buf);
        return(-1);
    }
    conn_api.wr_buf_size=BUFFER_SIZE;

    if(bbs->conn_type == CONN_TYPE_SERIAL) {
        _beginthread(modem_output_thread, 0, (void *)-1);
        _beginthread(modem_input_thread, 0, (void *)-1);
    }
    else {
        _beginthread(modem_output_thread, 0, NULL);
        _beginthread(modem_input_thread, 0, NULL);
    }

    if (!bbs->hidepopups)
        uifc.pop(NULL);

    return(0);
deuce's avatar
deuce committed
331 332
}

333 334
int serial_close(void)
{
335 336
    conn_api.terminate=1;

337
    while(conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1)
338 339 340 341 342 343 344
        SLEEP(1);
    comClose(com);
    destroy_conn_buf(&conn_inbuf);
    destroy_conn_buf(&conn_outbuf);
    FREE_AND_NULL(conn_api.rd_buf);
    FREE_AND_NULL(conn_api.wr_buf);
    return(0);
345 346
}

deuce's avatar
deuce committed
347 348
int modem_close(void)
{
349 350
    time_t start;
    char garbage[1024];
deuce's avatar
deuce committed
351

352
    conn_api.terminate=1;
deuce's avatar
deuce committed
353

354 355
    if((comGetModemStatus(com)&COM_DCD)==0) /* DCD already low */
        goto CLOSEIT;
deuce's avatar
deuce committed
356

357 358
    /* TODO:  We need a drain function */
    SLEEP(500);
deuce's avatar
deuce committed
359

360 361
    if(!comLowerDTR(com))
        goto CLOSEIT;
deuce's avatar
deuce committed
362

363 364 365 366 367 368
    start=time(NULL);
    while(time(NULL)-start <= 10) {
        if((comGetModemStatus(com)&COM_DCD) == 0)
            goto CLOSEIT;
        SLEEP(1000);
    }
deuce's avatar
deuce committed
369 370

CLOSEIT:
371
    while(conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) {
372 373 374 375 376 377 378 379 380 381
        conn_recv_upto(garbage, sizeof(garbage), 0);
        SLEEP(1);
    }
    comClose(com);

    destroy_conn_buf(&conn_inbuf);
    destroy_conn_buf(&conn_outbuf);
    FREE_AND_NULL(conn_api.rd_buf);
    FREE_AND_NULL(conn_api.wr_buf);
    return(0);
deuce's avatar
deuce committed
382
}