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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [llcomms.cpp] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 4 dgisselq
//
3 51 dgisselq
// Filename:    llcomms.cpp
4 4 dgisselq
//
5 30 dgisselq
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6 4 dgisselq
//
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 30 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
15
//              Gisselquist Technology, LLC
16 4 dgisselq
//
17 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
18 4 dgisselq
//
19 51 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
20 30 dgisselq
//
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
// You should have received a copy of the GNU General Public License along
32 51 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
33 30 dgisselq
// target there if the PDF file isn't present.)  If not, see
34
// <http://www.gnu.org/licenses/> for a copy.
35
//
36
// License:     GPL, v3, as defined and found on www.gnu.org,
37
//              http://www.gnu.org/licenses/gpl.html
38
//
39
//
40
////////////////////////////////////////////////////////////////////////////////
41
//
42
//
43 4 dgisselq
#include <sys/socket.h>
44
#include <sys/types.h>
45
#include <sys/stat.h>
46
#include <fcntl.h>
47
#include <termios.h>
48
#include <netinet/in.h>
49
#include <netdb.h>
50
#include <stdio.h>
51
#include <string.h>
52
#include <stdlib.h>
53
#include <unistd.h>
54
#include <errno.h>
55
#include <arpa/inet.h> 
56
#include <assert.h> 
57
#include <strings.h> 
58
#include <poll.h> 
59
#include <ctype.h> 
60
 
61
#include "llcomms.h"
62
 
63
LLCOMMSI::LLCOMMSI(void) {
64
        m_fdw = -1;
65
        m_fdr = -1;
66
        m_total_nread = 0l;
67
        m_total_nwrit = 0l;
68
}
69
 
70
void    LLCOMMSI::write(char *buf, int len) {
71
        int     nw;
72
        nw = ::write(m_fdw, buf, len);
73 51 dgisselq
        if (nw <= 0) {
74
                throw "Write-Failure";
75
        }
76 4 dgisselq
        m_total_nwrit += nw;
77
        assert(nw == len);
78
}
79
 
80
int     LLCOMMSI::read(char *buf, int len) {
81
        int     nr;
82
        nr = ::read(m_fdr, buf, len);
83 51 dgisselq
        if (nr <= 0) {
84
                throw "Read-Failure";
85
        }
86 4 dgisselq
        m_total_nread += nr;
87
        return nr;
88
}
89
 
90
void    LLCOMMSI::close(void) {
91
        if(m_fdw>=0)
92
                ::close(m_fdw);
93
        if((m_fdr>=0)&&(m_fdr != m_fdw))
94
                ::close(m_fdr);
95
        m_fdw = m_fdr = -1;
96
}
97
 
98
bool    LLCOMMSI::poll(unsigned ms) {
99
        struct  pollfd  fds;
100
 
101
        fds.fd = m_fdr;
102
        fds.events = POLLIN;
103
        ::poll(&fds, 1, ms);
104
 
105
        if (fds.revents & POLLIN) {
106
                return true;
107
        } else return false;
108
}
109
 
110
int     LLCOMMSI::available(void) {
111
        return poll(0)?1:0;
112
}
113
 
114
TTYCOMMS::TTYCOMMS(const char *dev) {
115
        m_fdr = ::open(dev, O_RDWR | O_NONBLOCK);
116
        if (m_fdr < 0) {
117
                printf("\n Error : Could not open %s\n", dev);
118
                perror("O/S Err:");
119
                exit(-1);
120
        }
121
 
122
        if (isatty(m_fdr)) {
123
                struct termios tb;
124
                tcgetattr(m_fdr, &tb);
125
                cfmakeraw(&tb);
126
                // tb.c_iflag &= (~(IXON|IXOFF));
127
                tb.c_cflag &= (~(CRTSCTS));
128
                tcsetattr(m_fdr, TCSANOW, &tb);
129
                tcflow(m_fdr, TCOON);
130
        }
131
 
132
        m_fdw = m_fdr;
133
}
134
 
135
NETCOMMS::NETCOMMS(const char *host, const int port) {
136
        struct sockaddr_in serv_addr;
137
        struct  hostent *hp;
138
 
139
        if ((m_fdr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
140
                printf("\n Error : Could not create socket \n");
141
                exit(-1);
142
        }
143
 
144
        memset(&serv_addr, '0', sizeof(serv_addr));
145
 
146
        hp = gethostbyname(host);
147
        if (hp == NULL) {
148
                printf("Could not get host entity for %s\n", host);
149
                perror("O/S Err:");
150
                exit(-1);
151
        }
152
        bcopy(hp->h_addr, &serv_addr.sin_addr.s_addr, hp->h_length);
153
 
154
        serv_addr.sin_family = AF_INET;
155
        serv_addr.sin_port = htons(port);
156
 
157
        if (connect(m_fdr,(struct sockaddr *)&serv_addr, sizeof(serv_addr))< 0){
158
                perror("Connect Failed Err");
159
                exit(-1);
160
        }
161
 
162
        m_fdw = m_fdr;
163
}
164
 

powered by: WebSVN 2.1.0

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