/* Modem emulating chat script for PPP over IrCOMM connectivity. * * Author: Ian Barry - 06/04/01 * * Seems to work for (tested with) : * * Psion Revo: Direct IR / Modem IR * Palm Vx: Direct IR / Modem IR / Serial * HP Jornada 720: Serial (Hayes compat. on COM1) * * Should work for all PalmOS, Epoch, WinCE & Pocket PC devices. * */ #include #include #include #include //#include "mydump.h" //#include "cmd_flash.h" /* Debug messages to standard error? */ //#define DEBUG static int chat (void); static int handledata (char *buf, int len); static char *argv0; /* Returns 0 on success, 1 on error. */ int main (int argc, char **argv) { int r; argv0 = argv[0]; fprintf (stderr, "%s: start\n", argv0); setbuf (stdout, NULL); r = chat (); if (r) { fprintf (stderr, "%s: failure\n", argv0); //system (CMD_FLASH_STOP FLASH_SYNC_BAD); } else { fprintf (stderr, "%s: success\n", argv0); //system (CMD_FLASH_STOP FLASH_SYNC_OK); } return r; } static int chat (void) { int finished = 0; while (1) { char buf[128]; int i=0; while (1) { char c; int r; r = read (0, &c, 1); if (r != 1) return 1; // read error if (!i && (c==13 || c==10)) break; if (c=='~' || c=='}') return 0; // chat script over: start PPP if (c==13 || c==10) { buf[i] = 0; finished = handledata (buf, i); if (finished) { sleep (1); return 0; } break; } #ifdef DEBUG mydump ((unsigned char) c); #endif buf[i] = c; i++; } } } #ifdef DEBUG # define iwrite(x) fprintf(stdout,x); fprintf(stderr,x) #else # define iwrite(x) fprintf(stdout,x) #endif #define enter "\r\n" static int handledata (char *buf, int len) { //static int first = 1; static int echo = 1; int connect = 0; char *a, *b, *c; // flash synchronisation screen //if (first) { //system (CMD_FLASH_START FLASH_SYNC); //first = 0; //} if (echo) { iwrite (buf); iwrite ("\r"); } // modem AT emulation if ((c = strstr (buf, "AT"))) { iwrite ("\r\n"); if (strstr (buf, "E1")) { echo = 1; } else if (strchr (buf, 'E')) { echo = 0; } for (a=buf; (b = strchr (a, 'Z')); a=b+1) if (b==a) { echo = 1; } else if (*(b-1)!='&') { echo = 1; } for (a=buf; (b = strchr (a, 'D')); a=b+1) if (b==a) { connect = 1; } else if (*(b-1)!='&') { connect = 1; } if (connect) { //iwrite ("CARRIER" enter ); // this sleep is required as Palm Vx gets confused // if CONNECT arrives too quickly sleep (1); iwrite (enter "CONNECT 115200" enter); //sleep (2); } else { iwrite ("OK" enter); } //sleep (1); return connect; } // for WinCE direct connections else if (strstr ("CLIENT", buf)) { iwrite ("SERVER" enter); } iwrite ("ERROR" enter); return 0; }