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

Subversion Repositories openarty

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

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

Line No. Rev Author Line
1 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 4 dgisselq
//
3
// Filename:    llcomms.cpp
4
//
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 30 dgisselq
// Copyright (C) 2015-2016, 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
// You should have received a copy of the GNU General Public License along
32
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
33
// 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
        m_total_nwrit += nw;
74
        assert(nw == len);
75
}
76
 
77
int     LLCOMMSI::read(char *buf, int len) {
78
        int     nr;
79
        nr = ::read(m_fdr, buf, len);
80
        m_total_nread += nr;
81
        return nr;
82
}
83
 
84
void    LLCOMMSI::close(void) {
85
        if(m_fdw>=0)
86
                ::close(m_fdw);
87
        if((m_fdr>=0)&&(m_fdr != m_fdw))
88
                ::close(m_fdr);
89
        m_fdw = m_fdr = -1;
90
}
91
 
92
bool    LLCOMMSI::poll(unsigned ms) {
93
        struct  pollfd  fds;
94
 
95
        fds.fd = m_fdr;
96
        fds.events = POLLIN;
97
        ::poll(&fds, 1, ms);
98
 
99
        if (fds.revents & POLLIN) {
100
                return true;
101
        } else return false;
102
}
103
 
104
int     LLCOMMSI::available(void) {
105
        return poll(0)?1:0;
106
}
107
 
108
TTYCOMMS::TTYCOMMS(const char *dev) {
109
        m_fdr = ::open(dev, O_RDWR | O_NONBLOCK);
110
        if (m_fdr < 0) {
111
                printf("\n Error : Could not open %s\n", dev);
112
                perror("O/S Err:");
113
                exit(-1);
114
        }
115
 
116
        if (isatty(m_fdr)) {
117
                struct termios tb;
118
                tcgetattr(m_fdr, &tb);
119
                cfmakeraw(&tb);
120
                // tb.c_iflag &= (~(IXON|IXOFF));
121
                tb.c_cflag &= (~(CRTSCTS));
122
                tcsetattr(m_fdr, TCSANOW, &tb);
123
                tcflow(m_fdr, TCOON);
124
        }
125
 
126
        m_fdw = m_fdr;
127
}
128
 
129
NETCOMMS::NETCOMMS(const char *host, const int port) {
130
        struct sockaddr_in serv_addr;
131
        struct  hostent *hp;
132
 
133
        if ((m_fdr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
134
                printf("\n Error : Could not create socket \n");
135
                exit(-1);
136
        }
137
 
138
        memset(&serv_addr, '0', sizeof(serv_addr));
139
 
140
        hp = gethostbyname(host);
141
        if (hp == NULL) {
142
                printf("Could not get host entity for %s\n", host);
143
                perror("O/S Err:");
144
                exit(-1);
145
        }
146
        bcopy(hp->h_addr, &serv_addr.sin_addr.s_addr, hp->h_length);
147
 
148
        serv_addr.sin_family = AF_INET;
149
        serv_addr.sin_port = htons(port);
150
 
151
        if (connect(m_fdr,(struct sockaddr *)&serv_addr, sizeof(serv_addr))< 0){
152
                perror("Connect Failed Err");
153
                exit(-1);
154
        }
155
 
156
        m_fdw = m_fdr;
157
}
158
 

powered by: WebSVN 2.1.0

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