OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [monitor/] [loadserv/] [loadserv.c] - Blame information for rev 44

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 44 hellwig
/*
2
 * loadserv.c -- serial line load server
3
 */
4
 
5
 
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdarg.h>
10
#include <fcntl.h>
11
#include <unistd.h>
12
#include <termios.h>
13
 
14
 
15
#define SERIAL_PORT     "/dev/ttyS0"
16
 
17
#define SYN             ((unsigned char) 's')
18
#define ACK             ((unsigned char) 'a')
19
 
20
#define LINE_SIZE       520
21
 
22
 
23
static int debugCmds = 1;
24
static int debugData = 0;
25
 
26
static FILE *loadFile = NULL;
27
static int sfd = 0;
28
static struct termios origOptions;
29
static struct termios currOptions;
30
 
31
 
32
void serialClose(void);
33
 
34
 
35
void error(char *fmt, ...) {
36
  va_list ap;
37
 
38
  va_start(ap, fmt);
39
  printf("Error: ");
40
  vprintf(fmt, ap);
41
  printf("\n");
42
  va_end(ap);
43
  if (loadFile != NULL) {
44
    fclose(loadFile);
45
    loadFile = NULL;
46
  }
47
  if (sfd != 0) {
48
    serialClose();
49
    sfd = 0;
50
  }
51
  exit(1);
52
}
53
 
54
 
55
void serialOpen(void) {
56
  sfd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
57
  if (sfd == -1) {
58
    error("cannot open serial port '%s'", SERIAL_PORT);
59
  }
60
  tcgetattr(sfd, &origOptions);
61
  currOptions = origOptions;
62
  cfsetispeed(&currOptions, B38400);
63
  cfsetospeed(&currOptions, B38400);
64
  currOptions.c_cflag |= (CLOCAL | CREAD);
65
  currOptions.c_cflag &= ~PARENB;
66
  currOptions.c_cflag &= ~CSTOPB;
67
  currOptions.c_cflag &= ~CSIZE;
68
  currOptions.c_cflag |= CS8;
69
  currOptions.c_cflag &= ~CRTSCTS;
70
  currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
71
  currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
72
  currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
73
  currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
74
  currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET | OFILL);
75
  tcsetattr(sfd, TCSANOW, &currOptions);
76
}
77
 
78
 
79
void serialClose(void) {
80
  tcsetattr(sfd, TCSANOW, &origOptions);
81
  close(sfd);
82
}
83
 
84
 
85
int serialSnd(unsigned char b) {
86
  int n;
87
 
88
  n = write(sfd, &b, 1);
89
  return n == 1;
90
}
91
 
92
 
93
int serialRcv(unsigned char *bp) {
94
  int n;
95
 
96
  n = read(sfd, bp, 1);
97
  return n == 1;
98
}
99
 
100
 
101
void connect(void) {
102
  unsigned char b;
103
 
104
  printf("SYN... ");
105
  fflush(stdout);
106
  while (!serialSnd(ACK)) ;
107
  tcdrain(sfd);
108
  printf("ACK... ");
109
  fflush(stdout);
110
  while (!serialRcv(&b)) ;
111
  if (b != ACK) {
112
    error("cannot synchronize with client");
113
  }
114
  printf("connected\n");
115
}
116
 
117
 
118
int main(int argc, char *argv[]) {
119
  char *loadName;
120
  unsigned char b;
121
  unsigned char cmd;
122
  char line[LINE_SIZE];
123
  int n, i;
124
 
125
  if (argc != 2) {
126
    printf("Usage: %s <file to be loaded>\n", argv[0]);
127
    exit(1);
128
  }
129
  loadName = argv[1];
130
  loadFile = fopen(loadName, "rt");
131
  if (loadFile == NULL) {
132
    error("cannot open file to be loaded '%s'", loadName);
133
  }
134
  /* open serial interface */
135
  serialOpen();
136
  /* wait for client to connect */
137
  printf("Waiting for client...\n");
138
  while (1) {
139
    if (serialRcv(&b) && b == SYN) {
140
      break;
141
    }
142
  }
143
  connect();
144
  fseek(loadFile, 0, SEEK_SET);
145
  /* connected, now handle requests */
146
  while (1) {
147
    while (!serialRcv(&cmd)) ;
148
    if (cmd == 'q') {
149
      /* quit */
150
      if (debugCmds) {
151
        printf("quit\n");
152
      }
153
      break;
154
    }
155
    if (cmd == SYN) {
156
      /* this happens if the client has been reset */
157
      connect();
158
      fseek(loadFile, 0, SEEK_SET);
159
      continue;
160
    }
161
    if (cmd != 'r') {
162
      /* unknown command */
163
      if (debugCmds) {
164
        printf("unknown... UNCMD\n");
165
      }
166
      continue;
167
    }
168
    /* only read requests get here */
169
    if (debugCmds) {
170
      printf("reading record... ");
171
      fflush(stdout);
172
    }
173
    if (fgets(line, LINE_SIZE, loadFile) == NULL) {
174
      if (debugCmds) {
175
        printf("RDERR\n");
176
      }
177
    } else {
178
      n = strlen(line);
179
      for (i = 0; i < n; i++) {
180
        while (!serialSnd(line[i])) ;
181
      }
182
      tcdrain(sfd);
183
      if (debugCmds) {
184
        printf("OK\n");
185
      }
186
      if (debugData) {
187
        printf("%s", line);
188
      }
189
    }
190
  }
191
  if (loadFile != NULL) {
192
    fclose(loadFile);
193
    loadFile = NULL;
194
  }
195
  if (sfd != 0) {
196
    serialClose();
197
    sfd = 0;
198
  }
199
  return 0;
200
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.