diff options
author | Tian Fang <tfang@fb.com> | 2015-03-09 22:53:57 -0700 |
---|---|---|
committer | Tian Fang <tfang@fb.com> | 2015-03-09 22:53:57 -0700 |
commit | 2a51b7c1c2165ddb188c511e192b75f0aa0fbead (patch) | |
tree | bb42aeac00a8b986c325cd70d5cca6c13bc0c23a /meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon | |
download | ast2050-yocto-openbmc-2a51b7c1c2165ddb188c511e192b75f0aa0fbead.zip ast2050-yocto-openbmc-2a51b7c1c2165ddb188c511e192b75f0aa0fbead.tar.gz |
Initial open source release of OpenBMC
Diffstat (limited to 'meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon')
6 files changed, 684 insertions, 0 deletions
diff --git a/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/Makefile b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/Makefile new file mode 100644 index 0000000..4a3c25d --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/Makefile @@ -0,0 +1,16 @@ +# Copyright 2014-present Facebook. All Rights Reserved. +all: modbuscmd gpiowatch modbussim + +modbuscmd: modbuscmd.c modbus.c + $(CC) -D_BSD_SOURCE -Wall -Werror -std=c99 -o $@ $^ $(LDFLAGS) + +modbussim: modbussim.c modbus.c + $(CC) -D_BSD_SOURCE -Wall -Werror -std=c99 -o $@ $^ $(LDFLAGS) + +gpiowatch: gpiowatch.c + $(CC) -D_BSD_SOURCE -Wall -Werror -std=c99 -o $@ $^ $(LDFLAGS) + +.PHONY: clean + +clean: + rm -rf *.o modbuscmd gpiowatch modbussim diff --git a/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/gpiowatch.c b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/gpiowatch.c new file mode 100644 index 0000000..11b5471 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/gpiowatch.c @@ -0,0 +1,93 @@ +/* + * Copyright 2014-present Facebook. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> + +#define CHECK(x) { if((x) < 0) { \ + error = x; \ + goto cleanup; \ +} } + +#define CHECKNULL(x) { if((x) == NULL) { \ + error = -1; \ + goto cleanup; \ +} } + +typedef struct { + int fd; + int gpio; + char value; +} wgpio; + +int main(int argc, char **argv) { + int error = 0; + int nfds = argc - 1; + int i = 0; + int polliv = 10000; + wgpio* wgpios = NULL; + + if(argc < 2) { + fprintf(stderr, "Usage: %s <gpio num> [gpio num] [gpio num...]\n", argv[0]); + exit(1); + } + if(getenv("POLL_US")) { + polliv = atoi(getenv("POLL_US")); + } + + wgpios = calloc(nfds, sizeof(wgpio)); + CHECKNULL(wgpios) + + fprintf(stderr, "Watching %d gpios:", nfds); + for(i = 1; i < argc; i++) { + char filename[255]; + int gpio_num = atoi(argv[i]); + wgpios[i - 1].gpio = gpio_num; + snprintf(filename, 255, "/sys/class/gpio/gpio%d/value", gpio_num); + wgpios[i - 1].fd = open(filename, O_RDONLY); + CHECK(wgpios[i - 1].fd); + CHECK(read(wgpios[i - 1].fd, &(wgpios[i - 1].value), 1)); + fprintf(stderr, " %d (currently: %c)", gpio_num, wgpios[i - 1].value); + } + fprintf(stderr, "\n"); + + do { + usleep(polliv); + for(i = 0; i < nfds; i++) { + wgpio* w = &wgpios[i]; + char value; + lseek(w->fd, 0, SEEK_SET); + CHECK(read(w->fd, &value, 1)); + if(value != w->value) { + printf("GPIO%d: %c -> %c\n", w->gpio, w->value, value); + w->value = value; + } + } + } while (1); + +cleanup: + if(error != 0) { + fprintf(stderr, "Error %d: %s\n", errno, strerror(errno)); + error = 1; + } + return error; +} diff --git a/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbus.c b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbus.c new file mode 100644 index 0000000..46618a9 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbus.c @@ -0,0 +1,183 @@ +/* + * Copyright 2014-present Facebook. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "modbus.h" +#include <termios.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <sys/ioctl.h> + +static int loops = 0; +void waitfd(int fd) { + while(1) { + int lsr; + int ret = ioctl(fd, TIOCSERGETLSR, &lsr); + if(ret == -1) { + fprintf(stderr, "Error checking ioctl: %s\n", strerror(errno)); + break; + } + if(lsr & TIOCSER_TEMT) break; + loops++; + } +} + +void gpio_on(int fd) { + lseek(fd, 0, SEEK_SET); + write(fd, "1", 1); +} + +void gpio_off(int fd) { + lseek(fd, 0, SEEK_SET); + write(fd, "0", 1); +} + +void decode_hex_in_place(char* buf, size_t* len) { + for(int i = 0; i < *len; i+=2) { + sscanf(buf + i, "%2hhx", buf + (i / 2)); + } + *len = *len / 2; +} + +void append_modbus_crc16(char* buf, size_t* len) { + uint16_t crc = modbus_crc16(buf, *len); + if (verbose) + fprintf(stderr, "[*] Append Modbus CRC16 %04x\n", crc); + buf[(*len)++] = crc >> 8; + buf[(*len)++] = crc & 0x00FF; +} + +void print_hex(FILE* f, char* buf, size_t len) { + for(int i = 0; i < len; i++) + fprintf(f, "%02x ", buf[i]); +} + +size_t read_wait(int fd, char* dst, size_t maxlen, int mdelay_us) { + fd_set fdset; + struct timeval timeout; + char read_buf[16]; + size_t read_size = 0; + size_t pos = 0; + memset(dst, 0, maxlen); + for(;;) { + FD_ZERO(&fdset); + FD_SET(fd, &fdset); + timeout.tv_sec = 0; + timeout.tv_usec = mdelay_us; + int rv = select(fd + 1, &fdset, NULL, NULL, &timeout); + if(rv == -1) { + perror("select()"); + } else if (rv == 0) { + break; + } + read_size = read(fd, read_buf, 16); + if(read_size < 0) { + if(errno == EAGAIN) continue; + fprintf(stderr, "read error: %s\n", strerror(errno)); + exit(1); + } + if((pos + read_size) < maxlen) { + memcpy(dst + pos, read_buf, read_size); + pos += read_size; + } else { + fprintf(stderr, "Response buffer overflowed!\n"); + } + } + return pos; +} + +/* From libmodbus, https://github.com/stephane/libmodbus + * Under LGPL. */ +/* Table of CRC values for high-order byte */ +static const uint8_t table_crc_hi[] = { + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, + 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, + 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +}; + +/* Table of CRC values for low-order byte */ +static const uint8_t table_crc_lo[] = { + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, + 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, + 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, + 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, + 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, + 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, + 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, + 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, + 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, + 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, + 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, + 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, + 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, + 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, + 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, + 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, + 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, + 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, + 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, + 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, + 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, + 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, + 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, + 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, + 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, + 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 +}; + +uint16_t modbus_crc16(char* buffer, size_t buffer_length) { + uint8_t crc_hi = 0xFF; /* high CRC byte initialized */ + uint8_t crc_lo = 0xFF; /* low CRC byte initialized */ + unsigned int i; /* will index into CRC lookup */ + + /* pass through message buffer */ + while (buffer_length--) { + i = crc_hi ^ *((uint8_t*)(buffer++)); /* calculate the CRC */ + crc_hi = crc_lo ^ table_crc_hi[i]; + crc_lo = table_crc_lo[i]; + } + + return (crc_hi << 8 | crc_lo); +} diff --git a/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbus.h b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbus.h new file mode 100644 index 0000000..435d518 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbus.h @@ -0,0 +1,46 @@ +/* + * Copyright 2014-present Facebook. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef MODBUS_H_ +#define MODBUS_H_ +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +uint16_t modbus_crc16(char* buffer, size_t length); + +#define DEFAULT_TTY "/dev/ttyS3" +#define DEFAULT_GPIO 45 + +#define CHECK(x) { if((x) < 0) { \ + error = x; \ + goto cleanup; \ +} } + +void waitfd(int fd); +void gpio_on(int fd); +void gpio_off(int fd); +void decode_hex_in_place(char* buf, size_t* len); +void append_modbus_crc16(char* buf, size_t* len); +void print_hex(FILE* f, char* buf, size_t len); + +// Read until maxlen bytes or no bytes in mdelay_us microseconds +size_t read_wait(int fd, char* dst, size_t maxlen, int mdelay_us); + +extern int verbose; + +#endif diff --git a/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbuscmd.c b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbuscmd.c new file mode 100644 index 0000000..2d33039 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbuscmd.c @@ -0,0 +1,163 @@ +/* + * Copyright 2014-present Facebook. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include <termios.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <sys/ioctl.h> +#include <getopt.h> +#include "modbus.h" + +int verbose; + +void usage() { + fprintf(stderr, + "modbuscmd [-v] [-t <tty>] [-g <gpio>] modbus_command\n" + "\ttty defaults to %s\n" + "\tgpio defaults to %d\n" + "\tmodbus command should be specified in hex\n" + "\teg:\ta40300000008\n", + DEFAULT_TTY, DEFAULT_GPIO); + exit(1); +} + +int main(int argc, char **argv) { + int error = 0; + int fd; + struct termios tio; + char gpio_filename[255]; + int gpio_fd = 0; + int gpio_n = DEFAULT_GPIO; + char *tty = DEFAULT_TTY; + char *modbus_cmd = NULL; + size_t cmd_len = 0; + verbose = 0; + + int opt; + while((opt = getopt(argc, argv, "t:g:v")) != -1) { + switch (opt) { + case 't': + tty = optarg; + break; + case 'g': + gpio_n = atoi(optarg); + break; + case 'v': + verbose = 1; + break; + default: + usage(); + break; + } + } + if(optind < argc) { + modbus_cmd = argv[optind]; + } + if(modbus_cmd == NULL) { + usage(); + } + + if (verbose) + fprintf(stderr, "[*] Opening TTY\n"); + fd = open(tty, O_RDWR | O_NOCTTY); + CHECK(fd); + + if (verbose) + fprintf(stderr, "[*] Opening GPIO %d\n", gpio_n); + snprintf(gpio_filename, 255, "/sys/class/gpio/gpio%d/value", gpio_n); + gpio_fd = open(gpio_filename, O_WRONLY | O_SYNC); + CHECK(gpio_fd); + + if (verbose) + fprintf(stderr, "[*] Setting TTY flags!\n"); + memset(&tio, 0, sizeof(tio)); + cfsetspeed(&tio,B19200); + tio.c_cflag |= PARENB; + tio.c_cflag |= CLOCAL; + tio.c_cflag |= CS8; + tio.c_iflag |= INPCK; + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + CHECK(tcsetattr(fd,TCSANOW,&tio)); + + //convert hex to bytes + cmd_len = strlen(modbus_cmd); + if(cmd_len < 4) { + fprintf(stderr, "Modbus command too short!\n"); + exit(1); + } + decode_hex_in_place(modbus_cmd, &cmd_len); + append_modbus_crc16(modbus_cmd, &cmd_len); + // print command as sent + if (verbose) { + fprintf(stderr, "Will send: "); + print_hex(stderr, modbus_cmd, cmd_len); + fprintf(stderr, "\n"); + } + + if (verbose) + fprintf(stderr, "[*] Writing!\n"); + + // gpio on, write, wait, gpio off + gpio_on(gpio_fd); + write(fd, modbus_cmd, cmd_len); + waitfd(fd); + gpio_off(gpio_fd); + + // Enable UART read + tio.c_cflag |= CREAD; + CHECK(tcsetattr(fd,TCSANOW,&tio)); + + if(verbose) + fprintf(stderr, "[*] reading any response...\n"); + // Read back response + char modbus_buf[255]; + size_t mb_pos = 0; + memset(modbus_buf, 0, sizeof(modbus_buf)); + mb_pos = read_wait(fd, modbus_buf, sizeof(modbus_buf), 90000); + if(mb_pos >= 4) { + uint16_t crc = modbus_crc16(modbus_buf, mb_pos - 2); + if(verbose) + fprintf(stderr, "Modbus response CRC: %04X\n ", crc); + if((modbus_buf[mb_pos - 2] == (crc >> 8)) && + (modbus_buf[mb_pos - 1] == (crc & 0x00FF))) { + if(verbose) + fprintf(stderr, "CRC OK!\n"); + print_hex(stdout, modbus_buf, mb_pos); + printf("\n"); + } else { + fprintf(stderr, "BAD CRC :(\n"); + return 5; + } + } else { + fprintf(stderr, "No response :(\n"); + return 4; + } + +cleanup: + if(error != 0) { + error = 1; + fprintf(stderr, "%s\n", strerror(errno)); + } + return error; +} diff --git a/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbussim.c b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbussim.c new file mode 100644 index 0000000..bf8c6c8 --- /dev/null +++ b/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbussim.c @@ -0,0 +1,183 @@ +/* + * Copyright 2014-present Facebook. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include <termios.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <sys/ioctl.h> +#include <getopt.h> +#include "modbus.h" + +int verbose = 0; + +void usage() { + fprintf(stderr, + "modbussim [-v] [-t <tty>] [-g <gpio>] modbus_request modbus_reply\n" + "\ttty defaults to %s\n" + "\tgpio defaults to %d\n" + "\tmodbus request/reply should be specified in hex\n" + "\teg:\ta40300000008\n", + DEFAULT_TTY, DEFAULT_GPIO); + exit(1); +} + +int main(int argc, char **argv) { + int error = 0; + int fd; + struct termios tio; + char gpio_filename[255]; + int gpio_fd = 0; + int gpio_n = DEFAULT_GPIO; + char *tty = DEFAULT_TTY; + char *modbus_cmd = NULL; + char *modbus_reply = NULL; + size_t cmd_len = 0; + size_t reply_len = 0; + verbose = 0; + + int opt; + while((opt = getopt(argc, argv, "t:g:v"))) { + if (opt == -1) break; + switch (opt) { + case 't': + tty = optarg; + break; + case 'g': + gpio_n = atoi(optarg); + break; + case 'v': + verbose = 1; + break; + default: + usage(); + break; + } + } + if(optind < argc) { + modbus_cmd = argv[optind++]; + modbus_reply = argv[optind++]; + } + if(modbus_cmd == NULL || modbus_reply == NULL) { + usage(); + } + + if (verbose) + fprintf(stderr, "[*] Opening TTY\n"); + fd = open(tty, O_RDWR | O_NOCTTY); + CHECK(fd); + + if (verbose) + fprintf(stderr, "[*] Opening GPIO %d\n", gpio_n); + snprintf(gpio_filename, 255, "/sys/class/gpio/gpio%d/value", gpio_n); + gpio_fd = open(gpio_filename, O_WRONLY | O_SYNC); + CHECK(gpio_fd); + + if (verbose) + fprintf(stderr, "[*] Setting TTY flags!\n"); + memset(&tio, 0, sizeof(tio)); + cfsetspeed(&tio,B19200); + tio.c_cflag |= PARENB; + tio.c_cflag |= CLOCAL; + tio.c_cflag |= CS8; + tio.c_iflag |= INPCK; + tio.c_cc[VMIN] = 1; + tio.c_cc[VTIME] = 0; + CHECK(tcsetattr(fd,TCSANOW,&tio)); + + //convert hex to bytes + cmd_len = strlen(modbus_cmd); + if(cmd_len < 2) { + fprintf(stderr, "Command too short!\n"); + exit(1); + } + decode_hex_in_place(modbus_cmd, &cmd_len); + append_modbus_crc16(modbus_cmd, &cmd_len); + if (verbose) { + fprintf(stderr, "expect: "); + print_hex(stderr, modbus_cmd, cmd_len); + fprintf(stderr, "\n"); + } + reply_len = strlen(modbus_reply); + decode_hex_in_place(modbus_reply, &reply_len); + append_modbus_crc16(modbus_reply, &reply_len); + // print full expected reply + if (verbose) { + fprintf(stderr, "reply: "); + print_hex(stderr, modbus_reply, reply_len); + fprintf(stderr, "\n"); + } + + // Enable UART read + tio.c_cflag |= CREAD; + CHECK(tcsetattr(fd,TCSANOW,&tio)); + gpio_off(gpio_fd); + + if(verbose) + fprintf(stderr, "[*] Wait for matching command...\n"); + + char modbus_buf[255]; + size_t mb_pos; +wait_for_command: + mb_pos = read_wait(fd, modbus_buf, sizeof(modbus_buf), 30000); + if(mb_pos >= 4) { + printf("Received: "); + print_hex(stdout, modbus_buf, mb_pos); + uint16_t crc = modbus_crc16(modbus_buf, mb_pos - 2); + if((modbus_buf[mb_pos - 2] == (crc >> 8)) && + (modbus_buf[mb_pos - 1] == (crc & 0x00FF))) { + if(verbose) + fprintf(stderr, "CRC OK!\n"); + if(memcmp(modbus_buf, modbus_cmd, cmd_len) == 0) { + fprintf(stderr, "Command matched!\n"); + } else { + fprintf(stderr, "Got modbus cmd that didn't match.\n"); + goto wait_for_command; + } + } else { + fprintf(stderr, "Got data that failed modbus CRC.\n"); + goto wait_for_command; + } + } else { + goto wait_for_command; + } + + + if (verbose) + fprintf(stderr, "[*] Writing reply!\n"); + + // Disable UART read + tio.c_cflag &= ~CREAD; + CHECK(tcsetattr(fd,TCSANOW,&tio)); + // gpio on, write, wait, gpio off + gpio_on(gpio_fd); + write(fd, modbus_reply, reply_len); + waitfd(fd); + gpio_off(gpio_fd); + +cleanup: + if(error != 0) { + error = 1; + fprintf(stderr, "%s\n", strerror(errno)); + } + return error; +} |