Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / tool.c @ 14688426

History | View | Annotate | Download (2.92 KB)

1
#include "tool.h"
2
#include "tooltron_mb.h"
3
#include <modbus.h>
4
#include <stdio.h>
5
#include <errno.h>
6
#include <stdlib.h> // TODO temporary
7

    
8
modbus_t *ctx;
9

    
10
int tool_init_mb(const char *device) {
11
  struct timeval timeout;
12

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

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

    
25
  modbus_get_response_timeout(ctx, &timeout);
26
  printf("Response timeout is %lus %luus\n", timeout.tv_sec, timeout.tv_usec);
27

    
28
  return 0;
29
}
30

    
31
void tool_close_mb() {
32
  modbus_close(ctx);
33
  modbus_free(ctx);
34
}
35

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

    
42
static void tool_init(struct tool_t *tool) {
43
  // TODO write current limit values
44
  tool_write_coil(MB_COIL_INIT, 1);
45
  tool->state = TS_OFF;
46
}
47

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

    
58
static void tool_grant_access(struct tool_t *tool) {
59
  printf("Granting access to %08x on %s\n", tool->user, tool->name);
60
  tool_write_coil(MB_COIL_EN, 1);
61
}
62

    
63
static void tool_deny_access(struct tool_t *tool) {
64
  printf("Denying access to %08x on %s\n", tool->user, tool->name);
65
  tool_write_coil(MB_COIL_EN, 0);
66
}
67

    
68
void tool_request_disable(struct tool_t *tool) {
69
  printf("Requesting disable on %s\n", tool->name);
70
  tool_write_coil(MB_COIL_REQ_DIS, 1);
71
}
72

    
73
void tool_poll(struct tool_t *tool) {
74
  unsigned char status[N_COILS];
75

    
76
  if (modbus_set_slave(ctx, tool->address) == -1) {
77
    fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno));
78
  }
79

    
80
  tool_read_user(tool);
81
  printf("%08x\n", tool->user);
82
  return;
83

    
84
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
85
    fprintf(stderr, "modbus_read_bits: %s\n", modbus_strerror(errno));
86
    return;
87
  }
88

    
89
  if (!status[MB_COIL_INIT]) {
90
    tool->state = TS_INIT;
91
  }
92

    
93
  switch (tool->state) {
94

    
95
    case TS_INIT:
96
      tool_init(tool);
97
      break;
98

    
99
    case TS_OFF:
100
      if (status[MB_COIL_NEW]) {
101
        tool_read_user(tool);
102
        // TODO check actual credentials
103
        if (rand() & 1) {
104
          tool_grant_access(tool);
105
        } else {
106
          tool_deny_access(tool);
107
        }
108
      }
109
      break;
110

    
111
    case TS_ON:
112
      if (!status[MB_COIL_EN]) {
113
        printf("Tool %s is off\n", tool->name);
114
        tool->state = TS_OFF;
115
      }
116
      break;
117

    
118
    case TS_REQ_DIS:
119
      if (!status[MB_COIL_EN]) {
120
        printf("Tool %s is off after requested disable\n", tool->name);
121
        tool->state = TS_OFF;
122
      }
123
      break;
124

    
125
  }
126

    
127
}