2015-01-03 16:06:10 -05:00
|
|
|
/* irmc - internet relay morsecode client */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
2015-07-08 16:38:29 -04:00
|
|
|
#include <sys/ioctl.h>
|
2015-01-03 16:06:10 -05:00
|
|
|
#include <math.h>
|
|
|
|
#include <fcntl.h>
|
2016-09-23 16:32:24 -04:00
|
|
|
#include <pthread.h>
|
2016-09-24 10:28:30 -04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-10-03 11:06:40 -04:00
|
|
|
|
|
|
|
// raspi wiring: http://raspberrypiguide.de/howtos/raspberry-pi-gpio-how-to/
|
|
|
|
|
2016-10-03 10:53:11 -04:00
|
|
|
// Detect OSX
|
2015-01-03 16:58:48 -05:00
|
|
|
#ifdef __MACH__
|
2016-09-24 10:28:30 -04:00
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach.h>
|
2016-10-03 10:53:11 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Detect Raspi
|
|
|
|
#ifdef __ARM
|
2015-07-08 16:38:29 -04:00
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <asm-generic/ioctl.h>
|
|
|
|
#include <asm-generic/termios.h>
|
2016-10-03 10:53:11 -04:00
|
|
|
#endif
|
2015-07-08 17:24:41 -04:00
|
|
|
|
2016-09-24 09:58:47 -04:00
|
|
|
//#define DEBUG 1
|
2015-01-03 16:06:10 -05:00
|
|
|
|
|
|
|
#define MAXDATASIZE 1024 // max number of bytes we can get at once
|
|
|
|
|
2015-01-04 09:02:03 -05:00
|
|
|
#include "cwprotocol.h"
|
2016-10-03 10:23:41 -04:00
|
|
|
#include "beep.h"
|
2016-09-24 10:28:30 -04:00
|
|
|
|
2016-09-24 10:55:36 -04:00
|
|
|
int serial_status = 0, fd_serial, numbytes;
|
2015-01-03 16:06:10 -05:00
|
|
|
|
|
|
|
double tx_timeout = 0;
|
|
|
|
long tx_timer = 0;
|
|
|
|
#define TX_WAIT 5000
|
|
|
|
#define TX_TIMEOUT 240.0
|
|
|
|
#define KEEPALIVE_CYCLE 100
|
|
|
|
|
2016-09-24 09:58:47 -04:00
|
|
|
/* TX Methods */
|
|
|
|
#define TX_NONE 0
|
|
|
|
#define TX_SERIAL 1
|
|
|
|
#define TX_KEYBOARD 2
|
|
|
|
|
2015-01-03 16:06:10 -05:00
|
|
|
long key_press_t1;
|
|
|
|
long key_release_t1;
|
|
|
|
int last_message = 0;
|
|
|
|
char last_sender[16];
|
|
|
|
|
|
|
|
/* settings */
|
2016-09-24 10:21:29 -04:00
|
|
|
int translate = 1;
|
2015-01-03 16:06:10 -05:00
|
|
|
int audio_status = 1;
|
|
|
|
|
2015-01-03 16:38:23 -05:00
|
|
|
/* portable time, as listed in https://gist.github.com/jbenet/1087739 */
|
|
|
|
void current_utc_time(struct timespec *ts) {
|
|
|
|
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
|
|
|
clock_serv_t cclock;
|
|
|
|
mach_timespec_t mts;
|
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
|
|
clock_get_time(cclock, &mts);
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
ts->tv_sec = mts.tv_sec;
|
|
|
|
ts->tv_nsec = mts.tv_nsec;
|
|
|
|
#else
|
|
|
|
clock_gettime(CLOCK_REALTIME, ts);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:06:10 -05:00
|
|
|
/* a better clock() in milliseconds */
|
2015-07-08 16:38:29 -04:00
|
|
|
long fastclock(void)
|
2015-01-03 16:06:10 -05:00
|
|
|
{
|
|
|
|
struct timespec t;
|
|
|
|
long r;
|
|
|
|
|
2015-01-03 16:38:23 -05:00
|
|
|
current_utc_time (&t);
|
2015-01-03 16:06:10 -05:00
|
|
|
r = t.tv_sec * 1000;
|
|
|
|
r = r + t.tv_nsec / 1000000;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kbhit (void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set rdfs;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
FD_ZERO(&rdfs);
|
|
|
|
FD_SET (STDIN_FILENO, &rdfs);
|
|
|
|
|
|
|
|
select (STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
|
|
|
|
return FD_ISSET(STDIN_FILENO, &rdfs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get sockaddr, IPv4 or IPv6: */
|
|
|
|
void *get_in_addr(struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
if (sa->sa_family == AF_INET) {
|
|
|
|
return &(((struct sockaddr_in*)sa)->sin_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-04 08:39:51 -05:00
|
|
|
// disconnect from the server
|
2015-07-08 16:38:29 -04:00
|
|
|
void inthandler(int sig)
|
2015-01-03 16:06:10 -05:00
|
|
|
{
|
|
|
|
signal(sig, SIG_IGN);
|
2015-01-04 07:19:56 -05:00
|
|
|
send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0);
|
2015-01-03 16:06:10 -05:00
|
|
|
close(fd_socket);
|
|
|
|
close(fd_serial);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:38:29 -04:00
|
|
|
void txloop (void)
|
2015-01-03 16:06:10 -05:00
|
|
|
{
|
|
|
|
key_press_t1 = fastclock();
|
|
|
|
tx_timeout = 0;
|
|
|
|
for(;;){
|
|
|
|
tx_data_packet.n++;
|
|
|
|
tx_data_packet.code[tx_data_packet.n - 1] =
|
|
|
|
(int) ((key_press_t1 - key_release_t1) * -1);
|
|
|
|
|
|
|
|
//printf("space: %i\n", tx_data_packet.code[tx_data_packet.n -1]);
|
|
|
|
while(serial_status & TIOCM_DSR) ioctl(fd_serial, TIOCMGET, &serial_status);
|
|
|
|
key_release_t1 = fastclock();
|
|
|
|
|
|
|
|
tx_data_packet.n++;
|
|
|
|
tx_data_packet.code[tx_data_packet.n - 1] =
|
|
|
|
(int) ((key_release_t1 - key_press_t1) * 1);
|
|
|
|
|
|
|
|
//printf("mark: %i\n", tx_data_packet.code[tx_data_packet.n -1]);
|
|
|
|
while(1){
|
|
|
|
ioctl(fd_serial, TIOCMGET, &serial_status);
|
|
|
|
if(serial_status & TIOCM_DSR) break;
|
|
|
|
tx_timeout = fastclock() - key_release_t1;
|
|
|
|
if(tx_timeout > TX_TIMEOUT) return;
|
|
|
|
}
|
|
|
|
key_press_t1 = fastclock();
|
2015-01-04 09:32:43 -05:00
|
|
|
if(tx_data_packet.n == SIZE_CODE) {
|
2015-01-03 16:06:10 -05:00
|
|
|
printf("irmc: warning packet is full.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 09:58:47 -04:00
|
|
|
|
2015-07-08 16:38:29 -04:00
|
|
|
void message(int msg)
|
2015-01-03 16:06:10 -05:00
|
|
|
{
|
|
|
|
switch(msg){
|
|
|
|
case 1:
|
|
|
|
if(last_message == msg) return;
|
|
|
|
if(last_message == 2) printf("\n");
|
|
|
|
last_message = msg;
|
|
|
|
printf("irmc: transmitting...\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if(last_message == msg && strncmp(last_sender, rx_data_packet.id, 3) == 0) return;
|
|
|
|
else {
|
|
|
|
if(last_message == 2) printf("\n");
|
|
|
|
last_message = msg;
|
|
|
|
strncpy(last_sender, rx_data_packet.id, 3);
|
|
|
|
printf("irmc: receiving...(%s)\n", rx_data_packet.id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
printf("irmc: circuit was latched by %s.\n", rx_data_packet.id);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
printf("irmc: circuit was unlatched by %s.\n", rx_data_packet.id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fflush(0);
|
|
|
|
}
|
2015-07-08 16:38:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Main Loop */
|
2015-01-03 16:06:10 -05:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char buf[MAXDATASIZE];
|
|
|
|
struct addrinfo hints, *servinfo, *p;
|
|
|
|
int rv, i;
|
|
|
|
char s[INET6_ADDRSTRLEN];
|
|
|
|
int keepalive_t = 0;
|
|
|
|
char hostname[64];
|
|
|
|
char port[16];
|
|
|
|
int channel;
|
2015-01-04 09:26:52 -05:00
|
|
|
char id[SIZE_ID];
|
2015-01-03 16:06:10 -05:00
|
|
|
char serialport[64];
|
2016-09-24 09:58:47 -04:00
|
|
|
int tx_method = TX_NONE;
|
2015-01-03 16:06:10 -05:00
|
|
|
|
2015-07-08 15:06:39 -04:00
|
|
|
// Set default values
|
|
|
|
snprintf(hostname, 64, "mtc-kob.dyndns.org");
|
|
|
|
snprintf(port, 16, "7890");
|
|
|
|
channel = 103;
|
|
|
|
snprintf(id, SIZE_ID, "irmc-default");
|
2016-09-24 09:58:47 -04:00
|
|
|
snprintf(serialport, 64, "");
|
2015-07-08 15:06:39 -04:00
|
|
|
|
|
|
|
// Read commandline
|
|
|
|
opterr = 0;
|
|
|
|
int c;
|
|
|
|
while ((c = getopt (argc, argv, "h:p:c:i:s:")) != -1)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
snprintf(hostname, 64, "%s", optarg);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
snprintf(port, 16, "%s", optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
channel = atoi (optarg);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
snprintf(id, SIZE_ID, "%s", optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
snprintf(serialport, 64, "%s", optarg);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "irmc - Internet Relay Morse Code\n\n");
|
|
|
|
fprintf(stderr, "usage: irmc [arguments]\n\n");
|
|
|
|
fprintf(stderr, "Arguments:\n\n");
|
|
|
|
fprintf(stderr, " -h [hostname] Hostname of morsekob server. Default: %s\n", hostname);
|
|
|
|
fprintf(stderr, " -p [port] Port of morsekob server. Default: %s\n", port);
|
|
|
|
fprintf(stderr, " -c [channel] Channel. Default: %d\n", channel);
|
|
|
|
fprintf(stderr, " -i [id] My ID. Default: %s\n", id);
|
2016-09-24 09:58:47 -04:00
|
|
|
fprintf(stderr, " -s [serialport] Serial port device name. Example: /dev/tty.usbserial Default: \"%s\"\n", serialport);
|
2015-07-08 15:06:39 -04:00
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
2015-01-03 16:06:10 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 15:06:39 -04:00
|
|
|
// Preparing connection
|
|
|
|
fprintf(stderr, "irmc - Internet Relay Morse Code\n\n");
|
|
|
|
fprintf(stderr, "Connecting to %s:%s on channel %d with ID %s.\n", hostname, port, channel, id);
|
2015-01-03 16:06:10 -05:00
|
|
|
|
2015-01-04 09:26:52 -05:00
|
|
|
prepare_id (&id_packet, id);
|
|
|
|
prepare_tx (&tx_data_packet, id);
|
2015-01-05 18:18:10 -05:00
|
|
|
connect_packet.channel = channel;
|
2015-01-03 16:06:10 -05:00
|
|
|
|
|
|
|
signal(SIGINT, inthandler);
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
|
|
|
|
if ((rv = getaddrinfo(hostname, port, &hints, &servinfo)) != 0) {
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the first free socket */
|
|
|
|
for(p = servinfo; p != NULL; p = p->ai_next) {
|
|
|
|
if ((fd_socket = socket(p->ai_family, p->ai_socktype,
|
|
|
|
p->ai_protocol)) == -1) {
|
|
|
|
perror("irmc: socket");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(fd_socket, p->ai_addr, p->ai_addrlen) == -1) {
|
|
|
|
close(fd_socket);
|
|
|
|
perror("irmc: connect");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-23 16:32:24 -04:00
|
|
|
fcntl(fd_socket, F_SETFL, O_NONBLOCK);
|
2015-01-03 16:06:10 -05:00
|
|
|
if (p == NULL) {
|
2015-07-08 15:06:39 -04:00
|
|
|
fprintf(stderr, "Failed to connect.\n");
|
2015-01-03 16:06:10 -05:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
|
|
|
|
s, sizeof s);
|
2015-07-08 15:06:39 -04:00
|
|
|
fprintf(stderr, "Connected to %s.\n", s);
|
2016-10-03 15:08:29 -04:00
|
|
|
|
2015-01-03 16:38:23 -05:00
|
|
|
beep_init();
|
2016-10-03 15:08:29 -04:00
|
|
|
|
2016-09-24 09:58:47 -04:00
|
|
|
if ((strcmp (serialport, "")) != 0)
|
|
|
|
tx_method = TX_SERIAL;
|
|
|
|
|
|
|
|
if (tx_method == TX_SERIAL) {
|
|
|
|
fd_serial = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
|
|
|
|
if(fd_serial == -1) {
|
|
|
|
fprintf(stderr,"Unable to open serial port %s.\n", serialport);
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 16:06:10 -05:00
|
|
|
freeaddrinfo(servinfo); /* all done with this structure */
|
|
|
|
|
|
|
|
key_release_t1 = fastclock();
|
|
|
|
identifyclient();
|
|
|
|
|
|
|
|
/* Main Loop */
|
|
|
|
for(;;) {
|
|
|
|
if(tx_timer == 0)
|
|
|
|
if((numbytes = recv(fd_socket, buf, MAXDATASIZE-1, 0)) == -1)
|
|
|
|
usleep(250);
|
2015-01-04 07:19:56 -05:00
|
|
|
if(numbytes == SIZE_DATA_PACKET && tx_timer == 0){
|
|
|
|
memcpy(&rx_data_packet, buf, SIZE_DATA_PACKET);
|
2016-09-24 09:58:47 -04:00
|
|
|
|
2015-01-03 16:06:10 -05:00
|
|
|
#if DEBUG
|
|
|
|
printf("length: %i\n", rx_data_packet.length);
|
|
|
|
printf("id: %s\n", rx_data_packet.id);
|
|
|
|
printf("sequence no.: %i\n", rx_data_packet.sequence);
|
|
|
|
printf("version: %s\n", rx_data_packet.status);
|
|
|
|
printf("n: %i\n", rx_data_packet.n);
|
|
|
|
printf("code:\n");
|
2015-01-04 09:32:43 -05:00
|
|
|
for(i = 0; i < SIZE_CODE; i++)printf("%i ", rx_data_packet.code[i]); printf("\n");
|
2015-01-03 16:06:10 -05:00
|
|
|
#endif
|
2016-09-24 09:58:47 -04:00
|
|
|
|
2015-01-03 16:06:10 -05:00
|
|
|
if(rx_data_packet.n > 0 && rx_sequence != rx_data_packet.sequence){
|
|
|
|
message(2);
|
|
|
|
if(translate == 1){
|
|
|
|
printf("%s", rx_data_packet.status);
|
|
|
|
fflush(0);
|
|
|
|
}
|
|
|
|
rx_sequence = rx_data_packet.sequence;
|
|
|
|
for(i = 0; i < rx_data_packet.n; i++){
|
|
|
|
switch(rx_data_packet.code[i]){
|
|
|
|
case 1:
|
|
|
|
message(3);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
message(4);
|
|
|
|
break;
|
|
|
|
default:
|
2015-01-03 16:38:23 -05:00
|
|
|
if(audio_status == 1)
|
|
|
|
{
|
|
|
|
|
2015-01-04 09:29:26 -05:00
|
|
|
int length = rx_data_packet.code[i];
|
|
|
|
if(length == 0 || abs(length) > 2000) {
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(length < 0) {
|
2016-10-03 10:18:56 -04:00
|
|
|
beep(0.0, abs(length)/1000.);
|
2015-01-04 09:29:26 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-03 10:18:56 -04:00
|
|
|
beep(1000.0, length/1000.);
|
2015-01-04 09:29:26 -05:00
|
|
|
}
|
|
|
|
}
|
2015-01-03 16:38:23 -05:00
|
|
|
}
|
2015-01-03 16:06:10 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tx_timer > 0) tx_timer--;
|
|
|
|
if(tx_data_packet.n > 1 ){
|
|
|
|
tx_sequence++;
|
|
|
|
tx_data_packet.sequence = tx_sequence;
|
2015-01-04 07:19:56 -05:00
|
|
|
for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0);
|
2015-01-03 16:06:10 -05:00
|
|
|
#if DEBUG
|
|
|
|
printf("irmc: sent data packet.\n");
|
|
|
|
#endif
|
|
|
|
tx_data_packet.n = 0;
|
|
|
|
}
|
2016-09-24 09:58:47 -04:00
|
|
|
if (tx_method == TX_SERIAL) {
|
|
|
|
ioctl(fd_serial,TIOCMGET, &serial_status);
|
|
|
|
if(serial_status & TIOCM_DSR){
|
|
|
|
txloop();
|
|
|
|
tx_timer = TX_WAIT;
|
|
|
|
message(1);
|
|
|
|
}
|
2015-01-03 16:06:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(keepalive_t < 0 && tx_timer == 0){
|
|
|
|
#if DEBUG
|
|
|
|
printf("keep alive sent.\n");
|
|
|
|
#endif
|
|
|
|
identifyclient();
|
|
|
|
keepalive_t = KEEPALIVE_CYCLE;
|
|
|
|
}
|
|
|
|
if(tx_timer == 0) {
|
|
|
|
keepalive_t--;
|
|
|
|
usleep(50);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(kbhit() && tx_timer == 0){
|
|
|
|
getchar(); /* flush the buffer */
|
|
|
|
}
|
|
|
|
} /* End of mainloop */
|
|
|
|
|
2015-01-04 07:19:56 -05:00
|
|
|
send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0);
|
2015-01-03 16:06:10 -05:00
|
|
|
close(fd_socket);
|
|
|
|
close(fd_serial);
|
2016-10-03 10:26:58 -04:00
|
|
|
buzzer_stop();
|
2015-01-03 16:06:10 -05:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|