Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

src/net/threebit/jvr/util/T1Diag.java

Go to the documentation of this file.
00001 package net.threebit.jvr.util;
00002 
00003 /*
00004  * $Id: T1Diag.java,v 1.6 2005/05/24 02:54:32 kevino Exp $
00005  *
00006  * Copyright (c) 2003,2004 Kevin O'Donnell
00007  * 
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307,  USA.
00021  */
00022 
00023 import java.io.*;
00024 import java.util.*;
00025 import java.util.logging.Logger;
00026 import net.threebit.jvr.*;
00027 
00028 public class T1Diag implements MetaEventListener {
00029 
00030   public static Logger logger = Logger.getLogger("net.threebit.jvr.util");
00031   public static long start = System.currentTimeMillis();
00032 
00033   int boardCount;
00034   int[] boardDevs = new int[1024];
00035   String[] boardNames = new String[1024];
00036   int[][] dtDevs = new int[1024][24];
00037   String[][] dtNames = new String[1024][24];
00038 
00039   DX_IOTT[] iottArray;
00040   DV_TPT[] tpt = new DV_TPT[1];
00041   DX_XPB xpb;
00042 
00043   int dxDev1;
00044   int dxDev2;
00045   boolean recording = false;
00046   boolean printEvents = false;
00047 
00048   Map alarms = new HashMap();
00049 
00050   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
00051 
00052   public void printAlarms() {
00053     synchronized (alarms) {
00054       for (int x = 0; x < boardCount; x++) {
00055         String name = boardNames[x];
00056         List l = (List) alarms.get(name);
00057         if (l == null) {
00058           l = new ArrayList();
00059           alarms.put(name,l);
00060         }
00061         String line = name + ":";
00062         if (l.size() == 0) {
00063           line += " no alarms";
00064         }
00065         for (Iterator i = l.iterator(); i.hasNext(); ) {
00066           line += " " + i.next();
00067         }
00068         print(line);
00069       }
00070     }
00071   }
00072 
00073   public void setAlarm (String board, String alarm, boolean on) {
00074     synchronized (alarms) {
00075       List l = (List) alarms.get(board);
00076       if (l == null) {
00077         l = new ArrayList();
00078         alarms.put(board,l);
00079       }
00080       if (on) {
00081         l.remove(alarm);
00082       }
00083       else if (!l.contains(alarm)) {
00084         l.add(alarm);
00085       }
00086     }
00087   }
00088 
00089   public boolean handleEvent (MetaEvent event) throws JVRException {
00090     String message = null;
00091     String device = null;
00092     boolean handled = false;
00093 
00094     if (event.evtdev == dxDev1 || event.evtdev == dxDev2) {
00095       // TODO
00096       handled = true;
00097     }
00098 
00099     // Check boards first.
00100     for (int x = 0; !handled && x < boardCount; x++) {
00101       // Event on the board?
00102       if (event.evtdev == boardDevs[x]) {
00103         device = boardNames[x];
00104         handled = true;
00105         if (event.evttype == dt.DTEV_T1ERRC) {
00106 
00107           int i = ((Integer) event.evtdata).intValue();
00108           message = jvr.symbolName("DTT1_",i);
00109     
00110           if (message.startsWith("DTT1_")) {
00111             String base = null;
00112             if (message.indexOf("OK") > -1) {
00113               base = message.substring(0,message.length()-2);
00114               setAlarm(boardNames[x],base,true);
00115             }
00116             else {
00117               base = message;
00118               setAlarm(boardNames[x],base,false);
00119             }
00120           }
00121         }
00122         break;
00123       }
00124       // Event on a board timeslot?
00125       for (int y = 0; !handled && y < 24; y++) {
00126         if (event.evtdev == dtDevs[x][y]) {
00127           handled = true;
00128 
00129           device = dtNames[x][y];
00130 
00131           message = jvr.symbolName("DTEV_",event.evttype);
00132 
00133           if (event.evttype == dt.DTEV_SIG) {
00134             message += " " + tsBits((int)event.evtdev);
00135           }
00136 
00137           x = 999;
00138           break;
00139         }
00140       }
00141     }
00142 
00143     if (message == null) {
00144       message = ""+event;
00145     }
00146 
00147     print("(event) " + device + " " + message);
00148     return true;
00149   }
00150 
00151   public boolean handleEvent (JVREvent event) throws JVRException {
00152     print("Ignored: " + event);
00153     return true;
00154   }
00155 
00156   public void run (String[] args) {
00157     // Add self as event listener
00158     jvr.addEventListener(this);
00159 
00160     // Open all of the available T1 boards.
00161     int index = 0;
00162     print("Opening T1 Boards:");
00163     while (true) {
00164       index ++;
00165       String name = "dtiB"+index;
00166       int dev;
00167       try {
00168         dev = dt.open(name,0);
00169         boardCount++;
00170       }
00171       catch (Throwable t) {
00172         index--;
00173         break;
00174       }
00175       boardDevs[index-1] = dev;
00176       boardNames[index-1] = name;
00177       print("  " + name);
00178       try {
00179         dt.setevtmsk(
00180           dev,
00181           dt.DTG_T1ERREVT,
00182           dt.DTEC_OOF | dt.DTEC_LOS | dt.DTEC_DPM | dt.DTEC_BPVS | dt.DTEC_BVCS | 
00183           dt.DTEC_ECS | dt.DTEC_RYEL | dt.DTEC_RCL | dt.DTEC_RCLX | dt.DTEC_FERR | 
00184           dt.DTEC_B8ZSD | dt.DTEC_RBL | dt.DTEC_RLOS | dt.DTEC_RED | dt.DTEC_LOOPBACK_CFA | dt.DTEC_DCHAN_CFA,
00185           dt.DTA_SETMSK
00186         );
00187       }
00188       catch (Throwable t) {
00189         print("Error setting up board: " + name);
00190         logger.throwing(getClass().getName(),"run",t);
00191       }
00192       String dtName = null;
00193       try {
00194         // open the 24 timeslots
00195         dtDevs[index-1] = new int[24];
00196         dtNames[index-1] = new String[24];
00197         for (int x = 1; x <= 24; x++) { // <= 24
00198           dtName = name + "T" + x;
00199           dtDevs[index-1][x-1] = dt.open(dtName,0);
00200           dtNames[index-1][x-1] = dtName;
00201           dt.setevtmsk(
00202             dtDevs[index-1][x-1],
00203             dt.DTG_SIGEVT,
00204             dt.DTMM_AON | dt.DTMM_AOFF | dt.DTMM_BON | dt.DTMM_BOFF | dt.DTMM_WINK,
00205             dt.DTA_SETMSK
00206           );
00207         }
00208       }
00209       catch (Throwable t) {
00210         print("Error setting up channel: " + dtName);
00211         logger.throwing(getClass().getName(),"run",t);
00212       }
00213     }
00214     print("");
00215 
00216     try {
00217       print("Opening dxxxB1C1");
00218       dxDev1 = dx.open("dxxxB1C1",0);
00219       dx.stopch(dxDev1,dx.EV_SYNC);
00220       print("Opening dxxxB1C2");
00221       dxDev2 = dx.open("dxxxB1C2",0);
00222       dx.stopch(dxDev2,dx.EV_SYNC);
00223     }
00224     catch (Throwable t) {
00225       print("Error opening DX resources");
00226       logger.throwing(getClass().getName(),"run",t);
00227     }
00228 
00229     print("");
00230     while (true) {
00231       try {
00232         prompt("t1diag");
00233         String command = in.readLine();
00234         if (command == null) { continue; }
00235         if (command.equals("")) { continue; }
00236         if (command.equals("h")) {
00237           printHelp();
00238         }
00239         if (command.equals("q")) {
00240           System.exit(0);
00241         }
00242         if (command.equals("e")) {
00243           printEvents = !printEvents;
00244           print("Print Events: " + printEvents);
00245         }
00246         if (command.equals("b")) {
00247           printBits();
00248         }
00249         if (command.equals("a")) {
00250           printAlarms();
00251         }
00252         if (command.equals("s")) {
00253           setTSBits();
00254         }
00255         if (command.equals("l")) {
00256           doListen();
00257         }
00258         if (command.equals("d")) {
00259           doScriptedDial();
00260         }
00261         if (command.equals("m")) {
00262           doDial();
00263         }
00264         if (command.equals("r")) {
00265           doRecord();
00266         }
00267       }
00268       catch (Throwable t) {
00269         t.printStackTrace();
00270         try { Thread.sleep(100); } catch (Throwable ignore) { }
00271       }
00272     }
00273   }
00274 
00275   public void doScriptedDial() throws JVRException {
00276   }
00277 
00278   public void doDial() throws JVRException {
00279     prompt("Number");
00280     String number = getString();
00281 
00282     DX_CAP cap;
00283     cap = new DX_CAP();
00284     cap.ca_nbrdna = 3;
00285     cap.ca_intflg = dx.DX_PAMDOPTEN;
00286     cap.ca_pamd_spdval = (byte) dx.PAMD_ACCU;
00287     cap.ca_hedge = 1;
00288 
00289     print("Dialing '"+number+"'");
00290     dx.dial(dxDev1,number,cap,dx.EV_ASYNC|dx.DX_CALLP);
00291   }
00292 
00293   public void doRecord() throws JVRException {
00294     if (recording) {
00295       dx.stopch(dxDev2, dx.EV_SYNC);
00296       print("Recording stopped");
00297       recording = false;
00298     }
00299     else {
00300       prompt("Filename");
00301       String filename = getString();
00302 
00303       tpt[0] = new DV_TPT();
00304       tpt[0].tp_type = dx.IO_EOT;
00305       tpt[0].tp_termno = dx.DX_MAXTIME;
00306       tpt[0].tp_length = 9000;
00307       tpt[0].tp_flags = dx.TF_MAXTIME;
00308 
00309       xpb = new DX_XPB();
00310       xpb.wFileFormat = dx.FILE_FORMAT_WAVE;
00311       xpb.wDataFormat = dx.DATA_FORMAT_PCM;
00312       xpb.nSamplesPerSec = dx.DRT_11KHZ;
00313       xpb.wBitsPerSample = 8;
00314 
00315       iottArray = DX_IOTT.newArray(1);
00316       iottArray[0].io_type = dx.IO_DEV;
00317       iottArray[0].io_fhandle = dx.fileopen(
00318         filename,
00319         dx.WIN32_O_CREAT|dx.WIN32_O_RDWR|dx.WIN32_O_BINARY,
00320         dx.WIN32_S_IREAD|dx.WIN32_S_IWRITE
00321       );
00322 
00323       dx.reciottdata(dxDev2,iottArray,tpt,xpb,dx.EV_ASYNC);
00324       recording = true;
00325       print("Recording started");
00326     }
00327   }
00328 
00329   public void doListen() throws JVRException {
00330     prompt("DX resource 1 (dialing) or 2 (recording)");
00331     int i = getInt(); if (i == -1) { return; }
00332     int dev = dxDev1;
00333     if (i == 2) {
00334       dev = dxDev2; 
00335       prompt("  Listen to DT device or voice device? [DT,v]");
00336       String s = getString();
00337       if (s.equals("v")) {
00338         dx.listen(dxDev2, dx.getxmitslot(dxDev1));
00339         return;
00340       }
00341     }
00342     print("i = " + i + ", dev = " + dev);
00343 
00344     print("Set Timeslot TX bits");
00345     print("");
00346     printBoards();
00347     print("");
00348     prompt("Enter board");
00349     int board = getInt(); if (board == -1) { return; }
00350     if (board < 0 || board >= boardCount) { return; }
00351     prompt("Enter timeslot (1-24)");
00352     int ts = getInt(); if (board == -1) { return; }
00353     ts --;
00354 
00355     dx.listen(dev, dt.getxmitslot(dtDevs[board][ts]));
00356     if (i == 1) {
00357       dt.listen(dtDevs[board][ts], dx.getxmitslot(dev));
00358     }
00359   }
00360 
00361   public void printHelp() {
00362     print("h - Help");
00363     print("a - Print current alarms");
00364     print("b - Print TS bits (all)");
00365     print("d - Perform a test dial");
00366     print("e - Toggle event printing");
00367     print("l - Connect DX resource to a timeslot");
00368     print("m - Dial using a DX resource (manual)");
00369     print("q - Quit");
00370     print("r - Start/Stop recording");
00371     print("s - Set TX bits on a timeslot");
00372   }
00373 
00374   public void printBits() throws JVRException {
00375     print("Status of RX/TX bits");
00376     for (int x = 0; x < boardCount; x++) {
00377       for (int y = 0; y < 24; y++) {
00378         String name = dtNames[x][y];
00379         int dev = dtDevs[x][y];
00380         print("  " + name + " " + tsBits(dev));
00381       }
00382     }
00383   }
00384 
00385   public static void main (String[] args) {
00386     T1Diag t1Diag = new T1Diag();
00387     t1Diag.run(args);
00388   }
00389 
00390   public String tsBits (int dev) throws JVRException {
00391     long tsbits = dt.ATDT_TSSGBIT(dev);
00392     int ar, br, ax, bx;
00393     ar = (tsbits & dt.DTSG_RCVA)>0 ? 1 : 0;
00394     br = (tsbits & dt.DTSG_RCVB)>0 ? 1 : 0;
00395     ax = (tsbits & dt.DTSG_XMTA)>0 ? 1 : 0;
00396     bx = (tsbits & dt.DTSG_XMTB)>0 ? 1 : 0;
00397     return "{rx: " + ar + "/" + br + " tx: " + ax + "/" + bx + "}";
00398   }
00399 
00400   public static double dur() {
00401     double d = System.currentTimeMillis() - start;
00402     return d/1000;
00403   }
00404 
00405   public static void prompt (String prompt) {
00406     System.out.print(dur() + " " + prompt + " > ");
00407   }
00408 
00409   public static void prompt() {
00410     System.out.print(dur() + " > ");
00411   }
00412 
00413   public static void print (String line) {
00414     System.out.println(dur() + " " + line);
00415   }
00416 
00417   public void setTSBits() throws JVRException {
00418     print("Set Timeslot TX bits");
00419     print("");
00420     printBoards();
00421     print("");
00422     prompt("Enter board");
00423     int board = getInt(); if (board == -1) { return; }
00424     if (board < 0 || board >= boardCount) { return; }
00425     prompt("Enter timeslot (1-24)");
00426     int ts = getInt(); if (board == -1) { return; }
00427     ts --;
00428     print("");
00429     print("Select TX combination to set");
00430     print("");
00431     print("[1] A=0, B=0");
00432     print("[2] A=0, B=1");
00433     print("[3] A=1, B=0");
00434     print("[4] A=1, B=1");
00435     int setting = 0;
00436     while (setting != -1) {
00437       String bits = tsBits(dtDevs[board][ts]);
00438       print("");
00439       prompt("Q to finish " + bits + " ");
00440       setting = getInt(); if (setting == -1) { return; }
00441       int bitmask = 0;
00442       switch (setting) {
00443         case 1:
00444           bitmask = dt.DTB_AOFF|dt.DTB_BOFF;
00445           break;
00446         case 2:
00447           bitmask = dt.DTB_AOFF|dt.DTB_BBIT;
00448           break;
00449         case 3:
00450           bitmask = dt.DTB_ABIT|dt.DTB_BOFF;
00451           break;
00452         case 4:
00453           bitmask = dt.DTB_ABIT|dt.DTB_BBIT;
00454           break;
00455       }
00456       dt.settssig(dtDevs[board][ts],bitmask,dt.DTA_SETMSK);
00457     }
00458   }
00459 
00460   public String getString() {
00461     try {
00462       return in.readLine();
00463     }
00464     catch (IOException e) {
00465       logger.throwing(getClass().getName(),"getString",e);
00466       return null;
00467     }
00468   }
00469 
00470   public int getInt() {
00471     try {
00472       return Integer.valueOf(in.readLine()).intValue();
00473     }
00474     catch (Throwable t) {
00475       return -1;
00476     }
00477   }
00478 
00479   public void printBoards() {
00480     for (int x = 0; x < boardCount; x++) {
00481       String s = "";
00482       if (x < 10) { s += " "; }
00483       print("[" + s + x + "] " + boardNames[x]);
00484     }
00485   }
00486 
00487 }
00488 

Generated on Sat Jul 16 03:47:35 2005 for JVR by doxygen 1.3.5