Skip to content

Instantly share code, notes, and snippets.

@hollie
Created April 19, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hollie/f800bef2c0d5a1480d6d70b6b1db982d to your computer and use it in GitHub Desktop.
Save hollie/f800bef2c0d5a1480d6d70b6b1db982d to your computer and use it in GitHub Desktop.
Sync time script for AlphaClock 5 over BT serial port
#! /usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Device::SerialPort;
my $name = 'Foo';
my $bt_address = 'D5:89:1F:09:51:15';
say "PID $$";
# Fork a process to connect the rfcomm device
my $pid = fork();
die if not defined $pid;
if (not $pid) {
say "Opening RFCOMM connection over bluetooth";
exec("sudo rfcomm connect hci0 D5:89:1F:09:51:15");
exit;
}
say "In parent ($name) - PID $$ ($pid), waiting for rfcomm0 device to become available";
while (! -e '/dev/rfcomm0') {
sleep(1);
say "Waiting for rfcomm0";
}
say "Device available...";
my $alphaclock = new Device::SerialPort ('/dev/rfcomm0') || die "Can't open /dev/rfcomm0: $!\n";
$alphaclock->write("*ST" . time() . "\r\n");
$alphaclock->close();
# send CTRL+C to the child process
sleep(1);
say "Sending CTRL+C to the rfcomm process $pid";
#my $command = "sudo kill -2 $pid";
#say "Going to exec: $command";
#exec($command);
#kill('TERM', $pid );
system("sudo rfcomm release hci0");
# Wait until finished
my $finished = wait();
say "In parent ($name) - PID $$ finished $finished";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment