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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [llcomms.cpp] - Blame information for rev 45

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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