(* Ringwatcher *) (* $Id: ringwatcher.ml,v 1.1 2003/01/12 15:33:00 berke Exp $ *) (* By Berke Durak *) open Unix let modem_device = "/dev/cua01" let escape_string s = let m = String.length s in let b = Buffer.create m in for i = 0 to m - 1 do Buffer.add_string b (Char.escaped s.[i]) done; Buffer.contents b let swap a i j = let x = a.(i) in a.(i) <- a.(j); a.(j) <- x let shuffle_array a = let m = Array.length a in for i = 0 to m - 2 do let j = i + (Random.int (m - 1 - i)) in swap a i j done let string_of_date () = let t = localtime (time ()) in Printf.sprintf "%02d.%02d.%04d %02d:%02d:%02d" t.Unix.tm_mday (t.Unix.tm_mon + 1) (t.Unix.tm_year + 1900) t.Unix.tm_hour t.Unix.tm_min t.Unix.tm_sec let _ = let fd = openfile modem_device [O_RDWR] 0 in let ta = tcgetattr fd in let ta' = { ta with c_istrip = false; c_inlcr = false; c_igncr = false; c_icrnl = false; c_ixoff = false; c_obaud = 115200; c_ibaud = 115200; c_csize = 8; c_cstopb = 1; c_cread = true; c_parenb = false; c_hupcl = true; c_clocal = false; c_isig = false; c_icanon = false; c_noflsh = true; c_echo = false; c_echoe = false; c_echok = false; c_echonl = false; c_vmin = 4096; c_vtime = 5; } in tcsetattr fd TCSAFLUSH ta'; let ic = in_channel_of_descr fd and oc = out_channel_of_descr fd and fl = Pervasives.flush in let expect x = let y = input_line ic in Printf.eprintf "modem >>> [%s]\n" (escape_string y); fl Pervasives.stderr; if not (List.mem y x) then failwith ("Not expecting ["^(escape_string y)^"]") and usleep x = ignore (select [] [] [] x) in let out x = Printf.eprintf "[%s] >>> modem\n" (escape_string x); fl Pervasives.stderr; output_string oc x; fl oc; ignore (input_line ic) in out "ATZ\r\n"; expect ["OK\r"]; out "ATS0=255\r\n"; expect ["OK\r"]; while true do try match input_line ic with "RING\r" -> Printf.eprintf "%s Ring\n" (string_of_date ()); flush Pervasives.stderr; ignore (system ("/usr/local/bin/waveplay -S 8000 -B 8 -r /usr/home/berke/bin/alarm.raw >/dev/null 2>&1")); | _ -> () with End_of_file -> () done; tcsetattr fd TCSAFLUSH ta; close fd