summaryrefslogtreecommitdiffstats
path: root/meta-facebook/meta-wedge/recipes-wedge/rackmon/rackmon/modbuscmd.c
blob: 78f62a13d8073cc95be44ed279090a9e19a6ecaa (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
/*
 * 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 <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include "modbus.h"
#include "rackmond.h"

void usage() {
  fprintf(stderr,
      "modbuscmd [-v] [-t <timeout in ms>] [-x <expected response length>] modbus_command\n"
      "\tmodbus command should be specified in hex\n"
      "\teg:\ta40300000008\n"
      "\tif an expected response length is provided, modbuscmd will stop receving and check crc immediately "
      "after receiving that many bytes\n");
  exit(1);
}


int main(int argc, char **argv) {
    int error = 0;
    char *modbus_cmd = NULL;
    size_t cmd_len = 0;
    int expected = 0;
    uint32_t timeout = 0;
    verbose = 0;
    rackmond_command *cmd = NULL;
    char *response = NULL;
    int clisock;
    uint16_t response_len_actual;
    struct sockaddr_un rackmond_addr;

    int opt;
    while((opt = getopt(argc, argv, "w:x:t:g:v")) != -1) {
      switch (opt) {
      case 'x':
        expected = atoi(optarg);
        break;
      case 't':
        timeout = atol(optarg);
        break;
      case 'v':
        verbose = 1;
        break;
      default:
        usage();
        break;
      }
    }
    if(optind < argc) {
      modbus_cmd = argv[optind];
    }
    if(modbus_cmd == NULL) {
      usage();
    }

    //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);
    cmd = malloc(sizeof(rackmond_command) + cmd_len);
    cmd->type = COMMAND_TYPE_RAW_MODBUS;
    cmd->raw_modbus.length = cmd_len;
    cmd->raw_modbus.custom_timeout = timeout;
    memcpy(cmd->raw_modbus.data, modbus_cmd, cmd_len);
    cmd->raw_modbus.expected_response_length = expected;
    response = malloc(expected ? expected : 1024);
    uint16_t wire_cmd_len = sizeof(rackmond_command) + cmd_len;

    clisock = socket(AF_UNIX, SOCK_STREAM, 0);
    CHECKP(socket, clisock);
    rackmond_addr.sun_family = AF_UNIX;
    strcpy(rackmond_addr.sun_path, "/var/run/rackmond.sock");
    int addr_len = strlen(rackmond_addr.sun_path) + sizeof(rackmond_addr.sun_family);
    CHECKP(connect, connect(clisock, (struct sockaddr*) &rackmond_addr, addr_len));
    CHECKP(send, send(clisock, &wire_cmd_len, sizeof(wire_cmd_len), 0));
    CHECKP(send, send(clisock, cmd, wire_cmd_len, 0));
    CHECKP(recv, recv(clisock, &response_len_actual, sizeof(response_len_actual), 0));
    if(response_len_actual == 0) {
      uint16_t errcode = 0;
      CHECKP(recv, recv(clisock, &errcode, sizeof(errcode), 0));
      fprintf(stderr, "modbus error: %d (%s)\n", errcode, modbus_strerror(errcode));
      error = 1;
      goto cleanup;
    }
    CHECKP(recv, recv(clisock, response, response_len_actual, 0));
    if(error == 0) {
      printf("Response: ");
      print_hex(stdout, response, response_len_actual);
      printf("\n");
    }
cleanup:
    free(cmd);
    free(response);
    if(error != 0) {
      if(errno != 0) {
        fprintf(stderr, "errno err: %s\n", strerror(errno));
      }
      error = 1;
    }
    return error;
}
OpenPOWER on IntegriCloud