summaryrefslogtreecommitdiffstats
path: root/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbussim.c
blob: e2765019a0cc49627c1d7c55be2495dba085f46d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * 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"

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;
}
OpenPOWER on IntegriCloud