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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [sw/] [llcomms.cpp] - Blame information for rev 88

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

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    llcomms.cpp
4
//
5
// Project:     XuLA2 board
6
//
7
// Purpose:     This is the C++ program on the command side that will interact
8
//              with a UART on an FPGA, both sending and receiving characters.
9
//              Any bus interaction will call routines from this lower level
10
//              library to accomplish the actual connection to and
11
//              transmission to/from the board.
12
//
13
//
14
// Creator:     Dan Gisselquist, Ph.D.
15
//              Gisselquist Technology, LLC
16
//
17
////////////////////////////////////////////////////////////////////////////////
18
//
19
// Copyright (C) 2015, Gisselquist Technology, LLC
20
//
21
// This program is free software (firmware): you can redistribute it and/or
22
// modify it under the terms of  the GNU General Public License as published
23
// by the Free Software Foundation, either version 3 of the License, or (at
24
// your option) any later version.
25
//
26
// This program is distributed in the hope that it will be useful, but WITHOUT
27
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
28
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29
// for more details.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
//
39
#include <sys/socket.h>
40
#include <sys/types.h>
41
#include <sys/stat.h>
42
#include <fcntl.h>
43
#include <termios.h>
44
#include <netinet/in.h>
45
#include <netdb.h>
46
#include <stdio.h>
47
#include <string.h>
48
#include <stdlib.h>
49
#include <unistd.h>
50
#include <errno.h>
51
#include <arpa/inet.h> 
52
#include <assert.h> 
53
#include <strings.h> 
54
#include <poll.h> 
55
#include <ctype.h> 
56
 
57
#include "llcomms.h"
58
 
59
LLCOMMSI::LLCOMMSI(void) {
60
        m_fdw = -1;
61
        m_fdr = -1;
62
        m_total_nread = 0l;
63
        m_total_nwrit = 0l;
64
}
65
 
66
void    LLCOMMSI::write(char *buf, int len) {
67
        int     nw;
68
        nw = ::write(m_fdw, buf, len);
69
        m_total_nwrit += nw;
70
        assert(nw == len);
71
}
72
 
73
int     LLCOMMSI::read(char *buf, int len) {
74
        int     nr;
75
        nr = ::read(m_fdr, buf, len);
76
        m_total_nread += nr;
77
        return nr;
78
}
79
 
80
void    LLCOMMSI::close(void) {
81
        if(m_fdw>=0) ::close(m_fdw);
82
        if((m_fdr>=0)&&(m_fdr != m_fdw)) ::close(m_fdr);
83
        m_fdw = m_fdr = -1;
84
}
85
 
86
bool    LLCOMMSI::poll(unsigned ms) {
87
        struct  pollfd  fds;
88
 
89
        fds.fd = m_fdr;
90
        fds.events = POLLIN;
91
        ::poll(&fds, 1, ms);
92
 
93
        if (fds.revents & POLLIN) {
94
                return true;
95
        } else return false;
96
}
97
 
98
TTYCOMMS::TTYCOMMS(const char *dev) {
99
        m_fdr = ::open(dev, O_RDWR | O_NONBLOCK);
100
        if (m_fdr < 0) {
101
                printf("\n Error : Could not open %s\n", dev);
102
                perror("O/S Err:");
103
                exit(-1);
104
        }
105
 
106
        if (isatty(m_fdr)) {
107
                struct termios tb;
108
                tcgetattr(m_fdr, &tb);
109
                cfmakeraw(&tb);
110
                // tb.c_iflag &= (~(IXON|IXOFF));
111
                tb.c_cflag &= (~(CRTSCTS));
112
                tcsetattr(m_fdr, TCSANOW, &tb);
113
                tcflow(m_fdr, TCOON);
114
        }
115
 
116
        m_fdw = m_fdr;
117
}
118
 
119
NETCOMMS::NETCOMMS(const char *host, const int port) {
120
        struct sockaddr_in serv_addr;
121
        struct  hostent *hp;
122
 
123
        if ((m_fdr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
124
                printf("\n Error : Could not create socket \n");
125
                exit(-1);
126
        }
127
 
128
        memset(&serv_addr, '0', sizeof(serv_addr));
129
 
130
        hp = gethostbyname(host);
131
        if (hp == NULL) {
132
                printf("Could not get host entity for %s\n", host);
133
                perror("O/S Err:");
134
                exit(-1);
135
        }
136
        bcopy(hp->h_addr, &serv_addr.sin_addr.s_addr, hp->h_length);
137
 
138
        serv_addr.sin_family = AF_INET;
139
        serv_addr.sin_port = htons(port);
140
 
141
        if (connect(m_fdr,(struct sockaddr *)&serv_addr, sizeof(serv_addr))< 0){
142
                perror("Connect Failed Err");
143
                exit(-1);
144
        }
145
 
146
        m_fdw = m_fdr;
147
}
148
 

powered by: WebSVN 2.1.0

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