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

Subversion Repositories s6soc

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

Go to most recent revision | 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
        if(m_fdw>=0) ::close(m_fdw);
84
        if((m_fdr>=0)&&(m_fdr != m_fdw)) ::close(m_fdr);
85
        m_fdw = m_fdr = -1;
86
}
87
 
88
bool    LLCOMMSI::poll(unsigned ms) {
89
        struct  pollfd  fds;
90
 
91
        fds.fd = m_fdr;
92
        fds.events = POLLIN;
93
        ::poll(&fds, 1, ms);
94
 
95
        if (fds.revents & POLLIN) {
96
                return true;
97
        } else return false;
98
}
99
 
100
TTYCOMMS::TTYCOMMS(const char *dev) {
101
        m_fdr = ::open(dev, O_RDWR | O_NONBLOCK);
102
        if (m_fdr < 0) {
103
                printf("\n Error : Could not open %s\n", dev);
104
                perror("O/S Err:");
105
                exit(-1);
106
        }
107
 
108
        if (isatty(m_fdr)) {
109
                struct termios tb;
110
                tcgetattr(m_fdr, &tb);
111
                cfmakeraw(&tb);
112
                // tb.c_iflag &= (~(IXON|IXOFF));
113
                tb.c_cflag &= (~(CRTSCTS));
114
                tcsetattr(m_fdr, TCSANOW, &tb);
115
                tcflow(m_fdr, TCOON);
116
        }
117
 
118
        m_fdw = m_fdr;
119
}
120
 
121
NETCOMMS::NETCOMMS(const char *host, const int port) {
122
        struct sockaddr_in serv_addr;
123
        struct  hostent *hp;
124
 
125
        if ((m_fdr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
126
                printf("\n Error : Could not create socket \n");
127
                exit(-1);
128
        }
129
 
130
        memset(&serv_addr, '0', sizeof(serv_addr));
131
 
132
        hp = gethostbyname(host);
133
        if (hp == NULL) {
134
                printf("Could not get host entity for %s\n", host);
135
                perror("O/S Err:");
136
                exit(-1);
137
        }
138
        bcopy(hp->h_addr, &serv_addr.sin_addr.s_addr, hp->h_length);
139
 
140
        serv_addr.sin_family = AF_INET;
141
        serv_addr.sin_port = htons(port);
142
 
143
        if (connect(m_fdr,(struct sockaddr *)&serv_addr, sizeof(serv_addr))< 0){
144
                perror("Connect Failed Err");
145
                exit(-1);
146
        }
147
 
148
        m_fdw = m_fdr;
149
}
150
 

powered by: WebSVN 2.1.0

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