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

Subversion Repositories eco32

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

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 SYN             ((unsigned char) 's')
16
#define ACK             ((unsigned char) 'a')
17
 
18
#define LINE_SIZE       520
19
 
20
 
21
static int debugCmds = 1;
22
static int debugData = 0;
23
 
24
static FILE *loadFile = NULL;
25
static int sfd = 0;
26
static struct termios origOptions;
27
static struct termios currOptions;
28
 
29
 
30
void serialClose(void);
31
 
32
 
33
void error(char *fmt, ...) {
34
  va_list ap;
35
 
36
  va_start(ap, fmt);
37
  printf("Error: ");
38
  vprintf(fmt, ap);
39
  printf("\n");
40
  va_end(ap);
41
  if (loadFile != NULL) {
42
    fclose(loadFile);
43
    loadFile = NULL;
44
  }
45
  if (sfd != 0) {
46
    serialClose();
47
    sfd = 0;
48
  }
49
  exit(1);
50
}
51
 
52
 
53 45 hellwig
void serialOpen(char *serialPort) {
54
  sfd = open(serialPort, O_RDWR | O_NOCTTY | O_NDELAY);
55 44 hellwig
  if (sfd == -1) {
56 45 hellwig
    error("cannot open serial port '%s'", serialPort);
57 44 hellwig
  }
58
  tcgetattr(sfd, &origOptions);
59
  currOptions = origOptions;
60
  cfsetispeed(&currOptions, B38400);
61
  cfsetospeed(&currOptions, B38400);
62
  currOptions.c_cflag |= (CLOCAL | CREAD);
63
  currOptions.c_cflag &= ~PARENB;
64
  currOptions.c_cflag &= ~CSTOPB;
65
  currOptions.c_cflag &= ~CSIZE;
66
  currOptions.c_cflag |= CS8;
67
  currOptions.c_cflag &= ~CRTSCTS;
68
  currOptions.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
69
  currOptions.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK);
70
  currOptions.c_iflag &= ~(INPCK | ISTRIP | INLCR | IGNCR | ICRNL);
71
  currOptions.c_iflag &= ~(IXON | IXOFF | IXANY);
72 45 hellwig
  currOptions.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
73 44 hellwig
  tcsetattr(sfd, TCSANOW, &currOptions);
74
}
75
 
76
 
77
void serialClose(void) {
78
  tcsetattr(sfd, TCSANOW, &origOptions);
79
  close(sfd);
80
}
81
 
82
 
83
int serialSnd(unsigned char b) {
84
  int n;
85
 
86
  n = write(sfd, &b, 1);
87
  return n == 1;
88
}
89
 
90
 
91
int serialRcv(unsigned char *bp) {
92
  int n;
93
 
94
  n = read(sfd, bp, 1);
95
  return n == 1;
96
}
97
 
98
 
99
void connect(void) {
100
  unsigned char b;
101
 
102
  printf("SYN... ");
103
  fflush(stdout);
104
  while (!serialSnd(ACK)) ;
105
  tcdrain(sfd);
106
  printf("ACK... ");
107
  fflush(stdout);
108
  while (!serialRcv(&b)) ;
109
  if (b != ACK) {
110
    error("cannot synchronize with client");
111
  }
112
  printf("connected\n");
113
}
114
 
115
 
116
int main(int argc, char *argv[]) {
117 49 hellwig
  char *serialPort;
118 44 hellwig
  char *loadName;
119
  unsigned char b;
120
  unsigned char cmd;
121
  char line[LINE_SIZE];
122
  int n, i;
123
 
124 45 hellwig
  if (argc != 3) {
125
    printf("Usage: %s <serial port> <file to be loaded>\n", argv[0]);
126 44 hellwig
    exit(1);
127
  }
128 49 hellwig
  serialPort = argv[1];
129 45 hellwig
  loadName = argv[2];
130 44 hellwig
  loadFile = fopen(loadName, "rt");
131
  if (loadFile == NULL) {
132
    error("cannot open file to be loaded '%s'", loadName);
133
  }
134
  /* open serial interface */
135 49 hellwig
  serialOpen(serialPort);
136 44 hellwig
  /* 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.