Revision 5e03b78d mainbox/tool.c

View differences:

mainbox/tool.c
1 1
#include "tool.h"
2 2
#include "query.h"
3 3
#include "event.h"
4
#include "log.h"
4 5
#include "tooltron_mb.h"
5 6
#include <modbus.h>
6 7
#include <stdio.h>
......
13 14

  
14 15
  ctx = modbus_new_rtu(device, MB_BAUD, 'N', 8, 1);
15 16
  if (ctx == NULL) {
16
    fprintf(stderr, "modbus_new_rtu: %s\n", modbus_strerror(errno));
17
    log_print("ERROR: modbus_new_rtu: %s", modbus_strerror(errno));
17 18
    return 1;
18 19
  }
19 20

  
20 21
  if (modbus_connect(ctx) == -1) {
21
    fprintf(stderr, "modbus_connect: %s\n", modbus_strerror(errno));
22
    log_print("ERROR: modbus_connect: %s", modbus_strerror(errno));
22 23
    modbus_free(ctx);
23 24
    return 1;
24 25
  }
25 26

  
26 27
  modbus_get_response_timeout(ctx, &timeout);
27
  printf("Response timeout is %lus %luus\n", timeout.tv_sec, timeout.tv_usec);
28
  log_print("Modbus response timeout is %lus %luus",
29
      timeout.tv_sec, timeout.tv_usec);
28 30

  
29 31
  return 0;
30 32
}
......
36 38

  
37 39
static void tool_write_coil(int addr, int bit) {
38 40
  if (modbus_write_bit(ctx, addr, bit) == -1) {
39
    fprintf(stderr, "modbus_write_bit: %s\n", modbus_strerror(errno));
41
    log_print("ERROR: modbus_write_bit: %s", modbus_strerror(errno));
40 42
  }
41 43
}
42 44

  
......
49 51
static void tool_read_user(struct tool_t *tool) {
50 52
  unsigned short serno[2];
51 53
  if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) {
52
    fprintf(stderr, "modbus_read_registers: %s\n", modbus_strerror(errno));
54
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
53 55
    tool->user = 0;
54 56
  } else {
55 57
    tool->user = MODBUS_GET_INT32_FROM_INT16(serno, 0);
......
58 60

  
59 61
static void tool_grant_access(struct tool_t *tool) {
60 62

  
61
  printf("Granting access to %08x on %s (%d)\n", tool->user, tool->name,
63
  log_print("Granting access to %08x on %s (%d)", tool->user, tool->name,
62 64
      tool->address);
63 65

  
64 66
  tool_write_coil(MB_COIL_EN, 1);
......
74 76
static void tool_deny_access(struct tool_t *tool) {
75 77
  struct event_t *event;
76 78

  
77
  printf("Denying access to %08x on %s (%d)\n", tool->user, tool->name,
79
  log_print("Denying access to %08x on %s (%d)", tool->user, tool->name,
78 80
      tool->address);
79 81

  
80 82
  tool_write_coil(MB_COIL_EN, 0);
......
97 99
}
98 100

  
99 101
void tool_request_disable(struct tool_t *tool) {
100
  printf("Requesting disable on %s (%d)\n", tool->name, tool->address);
102
  log_print("Requesting disable on %s (%d)", tool->name, tool->address);
101 103
  tool_write_coil(MB_COIL_REQ_DIS, 1);
102 104
  tool->state = TS_REQ_DIS;
103 105
}
......
106 108
  unsigned char status[N_COILS];
107 109

  
108 110
  if (modbus_set_slave(ctx, tool->address) == -1) {
109
    fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno));
111
    log_print("ERROR: modbus_set_slave: %s", modbus_strerror(errno));
110 112
  }
111 113

  
112 114
  /* If we can't read from the tool, we only want this error message to print
113 115
   * once, thus tool->connected */
114 116
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
115 117
    if (tool->connected) {
116
      fprintf(stderr, "Cannot connect to %s (%d): %s\n", tool->name,
118
      log_print("Cannot connect to %s (%d): %s", tool->name,
117 119
          tool->address, modbus_strerror(errno));
118 120
      tool->connected = 0;
119 121
    }
120 122
    return;
121 123
  } else if (!tool->connected) {
122
    fprintf(stderr, "Reconnected to %s (%d)\n", tool->name, tool->address);
124
    log_print("Reconnected to %s (%d)", tool->name, tool->address);
123 125
    tool->connected = 1;
124 126
  }
125 127

  
126 128
  /*uint16_t current;
127 129
  if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, &current) == -1) {
128
    fprintf(stderr, "modbus_read_registers: %s\n", modbus_strerror(errno));
130
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
129 131
  } else {
130 132
    printf("Current: %d\n", current);
131 133
  }*/
......
156 158

  
157 159
    case TS_ON:
158 160
      if (!status[MB_COIL_EN]) {
159
        printf("Tool %s (%d) is off\n", tool->name, tool->address);
161
        log_print("Tool %s (%d) is off", tool->name, tool->address);
160 162
        tool_off(tool);
161 163
      }
162 164
      break;
163 165

  
164 166
    case TS_REQ_DIS:
165 167
      if (!status[MB_COIL_EN]) {
166
        printf("Tool %s (%d) is off after requested disable\n", tool->name,
168
        log_print("Tool %s (%d) is off after requested disable", tool->name,
167 169
            tool->address);
168 170
        tool_off(tool);
169 171
      }

Also available in: Unified diff