Revision 5e03b78d

View differences:

mainbox/Makefile
1
SRC=main.c tool.c query.c event.c util.c
2
HDR=tool.h query.h event.h util.h ../tooltron_mb.h
1
SRC=main.c tool.c query.c event.c util.c log.c
2
HDR=tool.h query.h event.h util.h log.h ../tooltron_mb.h
3 3

  
4 4
FLAGS=-O2 -g -Wall -I.. `pkg-config --cflags --libs libmodbus libcurl`
5 5

  
mainbox/log.c
1
#include "log.h"
2
#include <stdio.h>
3
#include <stdarg.h>
4
#include <time.h>
5
#include <errno.h>
6
#include <sys/time.h>
7

  
8
void log_time() {
9
  char buf[100];
10
  time_t t;
11
  time(&t);
12
  strftime(buf, sizeof(buf), "%b %e %T %Y", localtime(&t));
13
  printf("[%s] ", buf);
14
}
15

  
16
void log_print(const char *fmt, ...) {
17
  va_list args;
18
  va_start(args, fmt);
19
  log_time();
20
  vprintf(fmt, args);
21
  printf("\n");
22
  va_end(args);
23
}
24

  
25
void log_perror(const char *s) {
26
  log_time();
27
  perror(s);
28
}
mainbox/log.h
1
#ifndef LOG_H
2
#define LOG_H
3

  
4
void log_print(const char *fmt, ...);
5
void log_perror(const char *s);
6

  
7
#endif
mainbox/main.c
1 1
#include "tool.h"
2 2
#include "query.h"
3
#include "log.h"
3 4
#include <unistd.h>
4 5
#include <signal.h>
5 6
#include <strings.h>
......
59 60
    }
60 61
  }
61 62

  
62
  printf("Serial device: %s\n", device);
63
  printf("CRM server: http://%s/\n", server);
63
  log_print("Serial device: %s", device);
64
  log_print("CRM server: http://%s/", server);
64 65

  
65 66
  bzero(&sigact, sizeof(sigact));
66 67
  sigact.sa_handler = sigint;
......
76 77
    return 1;
77 78
  }
78 79

  
79
  printf("Modbus initialized; polling tools...\n");
80
  log_print("Modbus initialized; polling tools...");
80 81

  
81 82
  i = 0;
82 83
  while (run) {
......
86 87
    i = (i+1) % N_TOOLS;
87 88
  }
88 89

  
89
  printf("\nDisabling tools\n");
90
  log_print("Disabling tools");
90 91
  for (i = 0; i < N_TOOLS; i++) {
91 92
    tool_request_disable(&tools[i]);
92 93
  }
93 94

  
94
  printf("Closing modbus connection\n");
95
  log_print("Closing modbus connection");
95 96
  tool_close_mb();
96 97

  
97
  printf("Exiting\n");
98
  log_print("Exiting");
98 99
  return 0;
99 100
}
mainbox/query.c
1 1
#include "query.h"
2 2
#include "event.h"
3 3
#include "util.h"
4
#include "log.h"
4 5
#include <stdlib.h>
5 6
#include <stdio.h>
6 7
#include <string.h>
......
20 21

  
21 22
  error_code = curl_global_init(CURL_GLOBAL_SSL);
22 23
  if (error_code) {
23
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
24
    log_print("ERROR: curl_global_init: %s\n", curl_easy_strerror(error_code));
24 25
    return error_code;
25 26
  }
26 27

  
......
90 91
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
91 92
  if (error_code) goto error;
92 93
  if (response >= 400)
93
    fprintf(stderr, "Error %ld from %s\n", response, url);
94
    log_print("ERROR: response %ld from %s", response, url);
94 95
  else if (response > 200)
95
    fprintf(stderr, "Warning: response %ld from %s\n", response, url);
96
    log_print("WARNING: response %ld from %s", response, url);
96 97

  
97 98
  curl_easy_cleanup(handle);
98 99
  return result;
99 100

  
100 101
error:
101
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
102
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
102
  log_print("ERROR: curl: %s", curl_easy_strerror(error_code));
103
  log_print("ERROR:       when authenticating user %08x on tool %d",
103 104
      user_id, tool_id);
104 105
  curl_easy_cleanup(handle);
105 106
  return 0;
......
205 206
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
206 207
  if (error_code) goto error;
207 208
  if (response >= 400)
208
    fprintf(stderr, "Error %ld from %s\n", response, buf);
209
    log_print("ERROR: response %ld from %s", response, buf);
209 210
  else if (response > 200)
210
    fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
211
    log_print("WARNING: response %ld from %s", response, buf);
211 212

  
212 213
  curl_easy_cleanup(handle);
213 214
  curl_formfree(formpost);
......
217 218
  return response >= 300;
218 219

  
219 220
error:
220
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
221
  log_print("ERROR: curl: %s", curl_easy_strerror(error_code));
221 222
  curl_easy_cleanup(handle);
222 223
  curl_formfree(formpost);
223 224
#ifdef DEBUG_EVENT_RESPONSE
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
      }
mainbox/util.c
1 1
#include "util.h"
2
#include "log.h"
2 3
#include <stdlib.h>
3 4
#include <stdio.h>
4 5
#include <unistd.h>
......
12 13
  
13 14
  fd = open(filename, O_RDONLY);
14 15
  if (fd < 0) {
15
    perror("open");
16
    log_perror("open");
16 17
    return NULL;
17 18
  }
18 19

  
......
38 39
      close(fd);
39 40
      return str;
40 41
    } else if (nread < 0) {
41
      perror("read");
42
      log_perror("read");
42 43
      free(str);
43 44
      close(fd);
44 45
      return NULL;

Also available in: Unified diff