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

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.24/] [monitor/] [loadserv/] [loadserv.c] - Blame information for rev 45

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
  char *loadName;
118
  unsigned char b;
119
  unsigned char cmd;
120
  char line[LINE_SIZE];
121
  int n, i;
122
 
123 45 hellwig
  if (argc != 3) {
124
    printf("Usage: %s <serial port> <file to be loaded>\n", argv[0]);
125 44 hellwig
    exit(1);
126
  }
127 45 hellwig
  loadName = argv[2];
128 44 hellwig
  loadFile = fopen(loadName, "rt");
129
  if (loadFile == NULL) {
130
    error("cannot open file to be loaded '%s'", loadName);
131
  }
132
  /* open serial interface */
133 45 hellwig
  serialOpen(argv[1]);
134 44 hellwig
  /* wait for client to connect */
135
  printf("Waiting for client...\n");
136
  while (1) {
137
    if (serialRcv(&b) && b == SYN) {
138
      break;
139
    }
140
  }
141
  connect();
142
  fseek(loadFile, 0, SEEK_SET);
143
  /* connected, now handle requests */
144
  while (1) {
145
    while (!serialRcv(&cmd)) ;
146
    if (cmd == 'q') {
147
      /* quit */
148
      if (debugCmds) {
149
        printf("quit\n");
150
      }
151
      break;
152
    }
153
    if (cmd == SYN) {
154
      /* this happens if the client has been reset */
155
      connect();
156
      fseek(loadFile, 0, SEEK_SET);
157
      continue;
158
    }
159
    if (cmd != 'r') {
160
      /* unknown command */
161
      if (debugCmds) {
162
        printf("unknown... UNCMD\n");
163
      }
164
      continue;
165
    }
166
    /* only read requests get here */
167
    if (debugCmds) {
168
      printf("reading record... ");
169
      fflush(stdout);
170
    }
171
    if (fgets(line, LINE_SIZE, loadFile) == NULL) {
172
      if (debugCmds) {
173
        printf("RDERR\n");
174
      }
175
    } else {
176
      n = strlen(line);
177
      for (i = 0; i < n; i++) {
178
        while (!serialSnd(line[i])) ;
179
      }
180
      tcdrain(sfd);
181
      if (debugCmds) {
182
        printf("OK\n");
183
      }
184
      if (debugData) {
185
        printf("%s", line);
186
      }
187
    }
188
  }
189
  if (loadFile != NULL) {
190
    fclose(loadFile);
191
    loadFile = NULL;
192
  }
193
  if (sfd != 0) {
194
    serialClose();
195
    sfd = 0;
196
  }
197
  return 0;
198
}

powered by: WebSVN 2.1.0

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