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

Subversion Repositories xulalx25soc

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

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 91 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
20 5 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
// 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 91 dgisselq
        if(m_fdw>=0)
82
                ::close(m_fdw);
83
        if((m_fdr>=0)&&(m_fdr != m_fdw))
84
                ::close(m_fdr);
85 5 dgisselq
        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 91 dgisselq
int     LLCOMMSI::available(void) {
101
        return poll(0)?1:0;
102
}
103
 
104 5 dgisselq
TTYCOMMS::TTYCOMMS(const char *dev) {
105
        m_fdr = ::open(dev, O_RDWR | O_NONBLOCK);
106
        if (m_fdr < 0) {
107
                printf("\n Error : Could not open %s\n", dev);
108
                perror("O/S Err:");
109
                exit(-1);
110
        }
111
 
112
        if (isatty(m_fdr)) {
113
                struct termios tb;
114
                tcgetattr(m_fdr, &tb);
115
                cfmakeraw(&tb);
116
                // tb.c_iflag &= (~(IXON|IXOFF));
117
                tb.c_cflag &= (~(CRTSCTS));
118
                tcsetattr(m_fdr, TCSANOW, &tb);
119
                tcflow(m_fdr, TCOON);
120
        }
121
 
122
        m_fdw = m_fdr;
123
}
124
 
125
NETCOMMS::NETCOMMS(const char *host, const int port) {
126
        struct sockaddr_in serv_addr;
127
        struct  hostent *hp;
128
 
129
        if ((m_fdr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
130
                printf("\n Error : Could not create socket \n");
131
                exit(-1);
132
        }
133
 
134
        memset(&serv_addr, '0', sizeof(serv_addr));
135
 
136
        hp = gethostbyname(host);
137
        if (hp == NULL) {
138
                printf("Could not get host entity for %s\n", host);
139
                perror("O/S Err:");
140
                exit(-1);
141
        }
142
        bcopy(hp->h_addr, &serv_addr.sin_addr.s_addr, hp->h_length);
143
 
144
        serv_addr.sin_family = AF_INET;
145
        serv_addr.sin_port = htons(port);
146
 
147
        if (connect(m_fdr,(struct sockaddr *)&serv_addr, sizeof(serv_addr))< 0){
148
                perror("Connect Failed Err");
149
                exit(-1);
150
        }
151
 
152
        m_fdw = m_fdr;
153
}
154
 

powered by: WebSVN 2.1.0

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