📄 hw_slinke.c
字号:
/* $Id: hw_slinke.c,v 5.8 2002/05/04 09:36:27 lirc Exp $ *//**************************************************************************** ** hw_slinke.c *********************************************************** **************************************************************************** * * routines for Slinke receiver * * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de> * modified for logitech receiver by Isaac Lauer <inl101@alumni.psu.edu> * modified for Slink-e receiver Max Spring <mspring@employees.org> * * 07/01/2000 0.0 Early first cut: * - Slink-e must be configured for 19200,8N1. * - Only receiving is implemented so far, no sending of IR codes. * - Existing remote control definition files may not work with Slink-e. * * 07/02/2000 0.1 Made memory allocations safer; Freeing allocations. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <limits.h>#include <signal.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/ioctl.h>#include "hardware.h"#include "serial.h"#include "ir_remote.h"#include "lircd.h"#include "receive.h"#include "hw_slinke.h"void *slinke_malloc(size_t size){ void *ptr = malloc(size); if (ptr == NULL){ logprintf(LOG_ERR,"slinke_malloc: out of memory"); return NULL; }else{ memset(ptr,0,size); return ptr; } /* if */} /* slinke_malloc */void *slinke_realloc(void *optr, size_t size){ void *nptr; nptr = realloc(optr,size); if (nptr == NULL){ logprintf(LOG_ERR,"realloc: out of memory"); return NULL; }else{ return nptr; } /* if */} /* slinke_realloc */#define TIMEOUT 200000#define MAX_PORT_COUNT 8#define QUEUE_BUF_INIT_SIZE 32#define QUEUE_BUF_MAX_SIZE 4096struct port_queue_rec{ unsigned char port_id,msg_id; int length,bufsize; unsigned char *buf;};struct port_queue_rec queue[MAX_PORT_COUNT];/* values read from Slink-e*/struct slinke_settings_rec{ int sample_period; /* number of 1/5 microseconds */ int timeout_samples; /* number of sample periods */ char *version;};struct slinke_settings_rec slinke_settings = {0,0,NULL};extern struct ir_remote *repeat_remote,*last_remote;struct timeval start,end,last;lirc_t gap,signal_length;ir_code pre,code;struct hardware hw_slinke = { LIRC_DRIVER_DEVICE, /* default device */ -1, /* fd */ LIRC_CAN_REC_MODE2, /* features */ 0, /* send_mode */ LIRC_MODE_MODE2, /* rec_mode */ 0, /* code_length */ slinke_init, /* init_func */ NULL, /* config_func */ slinke_deinit, /* deinit_func */ NULL, /* send_func */ slinke_rec, /* rec_func */ slinke_decode, /* decode_func */ slinke_readdata, /* readdata */ "slinke"};/*****************************************************************************//* Slink-e constants */#define CMD_PORT_DONE 0x00#define CMD_PORT_SM 0x1F #define CMD_DISABLE 0x02#define CMD_ENABLE 0x03#define CMD_SENDBITMODE 0x04#define CMD_SETIRFS 0x04#define CMD_GETIRFS 0x05#define CMD_SETIRCFS 0x06#define CMD_GETIRCFS 0x07#define CMD_SETIRTIMEOUT 0x0C#define CMD_GETIRTIMEOUT 0x0D#define CMD_SETIRMINLEN 0x0E#define CMD_GETIRMINLEN 0x0F#define CMD_SETIRTXPORTS 0x08#define CMD_GETIRTXPORTS 0x13#define CMD_SETIRRXPORTEN 0x09#define CMD_GETIRRXPORTEN 0x12#define CMD_SETIRPORTECHO 0x0A#define CMD_GETIRPORTECHO 0x10#define CMD_SETIRRXPORTPOL 0x0B#define CMD_GETIRRXPORTPOL 0x11#define CMD_SETBAUD 0x08#define CMD_GETBAUD 0x09#define CMD_SETHSMODE 0x10#define CMD_GETHSMODE 0x11#define CMD_SETDIR 0x12#define CMD_GETDIR 0x13#define CMD_SAMPLE 0x14#define CMD_GETVERSION 0x0B#define CMD_GETSERIALNO 0x0C#define CMD_SETSERIALNO 0x0D#define CMD_SAVEDEFAULTS 0x0E#define CMD_LOADDEFAULTS 0x0F#define CMD_RESUME 0xAA#define CMD_RESET 0xFF #define RSP_PORT_DONE 0x00#define RSP_PORT_SM 0x1F#define RSP_DISABLE 0x02#define RSP_ENABLE 0x03#define RSP_TX_TIMEOUT 0x81#define RSP_CMD_ILLEGAL 0xFF#define RSP_RX_ERROR 0x80#define RSP_RX_BITMODE 0x04#define RSP_EQRXPORT 0x01#define RSP_EQIRFS 0x04#define RSP_EQIRCFS 0x06#define RSP_EQIRPORTECHO 0x0A#define RSP_EQIRTIMEOUT 0x0C#define RSP_EQIRMINLEN 0x0E#define RSP_EQIRRXPORTEN 0x09#define RSP_EQIRRXPORTPOL 0x0B#define RSP_EQIRTXPORTS 0x08#define RSP_IRFS_ILLEGAL 0x82#define RSP_EQBAUD 0x08#define RSP_SERIALIN_OVERFLOW 0x83#define RSP_SERIALIN_OVERRUN 0x86#define RSP_SERIALIN_FRAMEERROR 0x85#define RSP_BAUD_ILLEGAL 0x84#define RSP_EQHSMODE 0x10#define RSP_EQDIR 0x12#define RSP_EQVERSION 0x0B#define RSP_EQSERIALNO 0x0C#define RSP_DEFAULTSSAVED 0x0E#define RSP_DEFAULTSLOADED 0x0F#define RSP_SEEPROMWRERR 0x8F#define TEST_SLINK 1#define TEST_IR 2#define TEST_PAR 3#define TEST_BAUD 4#define TEST_IRMULTI 5#define MAXDATABLOCK 30#define RX_STATE_IDLE 0#define RX_STATE_RECEIVE 1#define RX_STATE_PSM 2#define RX_STATE_PSM_PAR 3#define PORT_SL0 0#define PORT_SL1 1#define PORT_SL2 2#define PORT_SL3 3#define PORT_IR0 4#define PORT_PAR 5#define PORT_SER 6#define PORT_SYS 7#define MSG_ID_UNKNOWN 0#define MSG_ID_PORT_RECEIVE 1#define MSG_ID_PORT_DISABLED 2#define MSG_ID_PORT_ENABLED 3#define MSG_ID_TRANSMISSION_TIMEOUT 4#define MSG_ID_ILLEGAL_COMMAND 5#define MSG_ID_RECEIVE_ERROR 6#define MSG_ID_SAMPLING_PERIOD_EQUALS 7#define MSG_ID_CARRIER_PERIOD_EQUALS 8#define MSG_ID_TIMEOUT_PERIOD_EQUALS 9#define MSG_ID_MINIMUM_MESSAGE_LENGTH_EQUALS 10#define MSG_ID_TRANSMIT_PORTS_EQUAL 11#define MSG_ID_RECEIVE_PORTS_EQUAL 12#define MSG_ID_LAST_RECEIVE_PORT_EQUALS 13#define MSG_ID_RECEIVE_PORT_POLARITIES_EQUAL 14#define MSG_ID_IR_ROUTING_TABLE_EQUALS 15#define MSG_ID_INVALID_SAMPLE_PERIOD 16#define MSG_ID_HANDSHAKING_MODE_EQUALS 17#define MSG_ID_CONFIGURATION_DIRECTION_EQUALS 18#define MSG_ID_BAUD_RATE_EQUALS 19#define MSG_ID_SERIAL_PORT_RECEIVE_BUFFER_OVERFLOW 20#define MSG_ID_SERIAL_PORT_RECEIVE_BUFFER_OVERRUN 21#define MSG_ID_SERIAL_PORT_RECEIVE_FRAMING_ERROR 22#define MSG_ID_BAUD_RATE_ILLEGAL 23#define MSG_ID_VERSION_EQUALS 24#define MSG_ID_DEFAULTS_LOADED 25#define MSG_ID_DEFAULTS_SAVED 26#define MSG_ID_SERIAL_NUMBER_EQUALS 27#define MSG_ID_SEEPROM_WRITE_ERROR 28#ifdef DEBUG/*****************************************************************************/char *to_byte_string(unsigned char *b, int n){ static char *buf = NULL; static int buflen = 0; int i,reqlen = 3*n+1; char t[10]; if (buf == NULL || reqlen > buflen){ buflen = reqlen; buf = (char*)slinke_realloc(buf,buflen); if (buf == NULL) return ""; } /* if */ sprintf(buf,"%02x",b[0]); for (i=1; i<n; i++){ sprintf(t,":%02x",b[i]); strcat(buf,t); } /* for */ return buf;} /* to_byte_string */static int signal_to_int(lirc_t signal){ return ((signal & PULSE_BIT) == 0) ? (signal & PULSE_MASK) : -(signal & PULSE_MASK);} /* signal_to_int */#endif/*****************************************************************************/static void tx_bytes(unsigned char *b, int n){ LOGPRINTF(3,"sending %s",to_byte_string(b,n)); write(hw.fd,b,n); } /* tx_bytes */static void enable_port(unsigned char port){ unsigned char d[2]; d[0] = ((port%7) << 5) + CMD_PORT_SM; d[1] = CMD_ENABLE; tx_bytes(d,sizeof(d));} /* enable_port */static void set_IR_receive_ports(unsigned char ports){ unsigned char d[3]; d[0] = (PORT_IR0 << 5) + CMD_PORT_SM; d[1] = CMD_SETIRRXPORTEN; d[2] = ports; tx_bytes(d,sizeof(d));} /* set_IR_receive_ports */static void set_sample_period(unsigned period){ unsigned char d[4]; d[0] = (PORT_IR0 << 5) + CMD_PORT_SM; d[1] = CMD_SETIRFS; d[2] = (unsigned char)((period >> 8) & 0xff); d[3] = (unsigned char)( period & 0xff); tx_bytes(d,sizeof(d));} /* set_sample_period */static void set_IR_timeout_period(unsigned samples){ unsigned char d[4]; d[0] = (PORT_IR0 << 5) + CMD_PORT_SM; d[1] = CMD_SETIRTIMEOUT; d[2] = (unsigned char)((samples >> 8) & 0xff); d[3] = (unsigned char)( samples & 0xff); tx_bytes(d,sizeof(d));} /* set_IR_timeout_period */static void get_version(){ unsigned char d[2]; d[0] = (PORT_SYS << 5) + CMD_PORT_SM; d[1] = CMD_GETVERSION; tx_bytes(d,sizeof(d));} /* get_version *//*****************************************************************************/int slinke_init(void){ int i; logprintf(LOG_INFO,"slinke_init"); signal_length=hw.code_length*1000000/1200; if(!tty_create_lock(hw.device)){ logprintf(LOG_ERR,"could not create lock files"); return(0); } /* if */ if((hw.fd=open(hw.device,O_RDWR|O_NOCTTY))<0){ logprintf(LOG_ERR,"could not open %s",hw.device); logperror(LOG_ERR,"slinke_init()"); tty_delete_lock(); return(0); } /* if */ if(!tty_reset(hw.fd)){ logprintf(LOG_ERR,"could not reset tty"); slinke_deinit(); return(0); } /* if */ if(!tty_setbaud(hw.fd,19200)){ logprintf(LOG_ERR,"could not set baud rate"); slinke_deinit(); return(0); } /* if */ get_version(); enable_port(PORT_IR0); set_IR_receive_ports(0xff); set_sample_period(250); set_IR_timeout_period(1000); for (i=0; i<MAX_PORT_COUNT; i++){ queue[i].port_id = (unsigned char)i; queue[i].length = 0; queue[i].bufsize = QUEUE_BUF_INIT_SIZE; queue[i].buf = (unsigned char*)slinke_malloc(QUEUE_BUF_INIT_SIZE); if (queue[i].buf == NULL){ logprintf(LOG_ERR,"could not create port queue buffer"); slinke_deinit(); return(0); } /* if */ } /* for */ return(1);} /* slinke_init *//*****************************************************************************/static int signal_queue_rd_idx,signal_queue_bufsize,signal_queue_length = 0;static lirc_t *signal_queue_buf = NULL; /*****************************************************************************/int slinke_deinit(void){ int i; close(hw.fd); tty_delete_lock(); if (signal_queue_buf != NULL) { free(signal_queue_buf); signal_queue_buf=NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -