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

Subversion Repositories openarty

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

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

Line No. Rev Author Line
1 4 dgisselq
//
2
//
3
// Filename:    llcomms.cpp
4
//
5
// Project:     UART to WISHBONE FPGA library
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
// Creator:     Dan Gisselquist
14
//              Gisselquist Tecnology, LLC
15
//
16
// Copyright:   2015
17
//
18
//
19
#include <sys/socket.h>
20
#include <sys/types.h>
21
#include <sys/stat.h>
22
#include <fcntl.h>
23
#include <termios.h>
24
#include <netinet/in.h>
25
#include <netdb.h>
26
#include <stdio.h>
27
#include <string.h>
28
#include <stdlib.h>
29
#include <unistd.h>
30
#include <errno.h>
31
#include <arpa/inet.h> 
32
#include <assert.h> 
33
#include <strings.h> 
34
#include <poll.h> 
35
#include <ctype.h> 
36
 
37
#include "llcomms.h"
38
 
39
LLCOMMSI::LLCOMMSI(void) {
40
        m_fdw = -1;
41
        m_fdr = -1;
42
        m_total_nread = 0l;
43
        m_total_nwrit = 0l;
44
}
45
 
46
void    LLCOMMSI::write(char *buf, int len) {
47
        int     nw;
48
        nw = ::write(m_fdw, buf, len);
49
        m_total_nwrit += nw;
50
        assert(nw == len);
51
}
52
 
53
int     LLCOMMSI::read(char *buf, int len) {
54
        int     nr;
55
        nr = ::read(m_fdr, buf, len);
56
        m_total_nread += nr;
57
        return nr;
58
}
59
 
60
void    LLCOMMSI::close(void) {
61
        if(m_fdw>=0)
62
                ::close(m_fdw);
63
        if((m_fdr>=0)&&(m_fdr != m_fdw))
64
                ::close(m_fdr);
65
        m_fdw = m_fdr = -1;
66
}
67
 
68
bool    LLCOMMSI::poll(unsigned ms) {
69
        struct  pollfd  fds;
70
 
71
        fds.fd = m_fdr;
72
        fds.events = POLLIN;
73
        ::poll(&fds, 1, ms);
74
 
75
        if (fds.revents & POLLIN) {
76
                return true;
77
        } else return false;
78
}
79
 
80
int     LLCOMMSI::available(void) {
81
        return poll(0)?1:0;
82
}
83
 
84
TTYCOMMS::TTYCOMMS(const char *dev) {
85
        m_fdr = ::open(dev, O_RDWR | O_NONBLOCK);
86
        if (m_fdr < 0) {
87
                printf("\n Error : Could not open %s\n", dev);
88
                perror("O/S Err:");
89
                exit(-1);
90
        }
91
 
92
        if (isatty(m_fdr)) {
93
                struct termios tb;
94
                tcgetattr(m_fdr, &tb);
95
                cfmakeraw(&tb);
96
                // tb.c_iflag &= (~(IXON|IXOFF));
97
                tb.c_cflag &= (~(CRTSCTS));
98
                tcsetattr(m_fdr, TCSANOW, &tb);
99
                tcflow(m_fdr, TCOON);
100
        }
101
 
102
        m_fdw = m_fdr;
103
}
104
 
105
NETCOMMS::NETCOMMS(const char *host, const int port) {
106
        struct sockaddr_in serv_addr;
107
        struct  hostent *hp;
108
 
109
        if ((m_fdr = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
110
                printf("\n Error : Could not create socket \n");
111
                exit(-1);
112
        }
113
 
114
        memset(&serv_addr, '0', sizeof(serv_addr));
115
 
116
        hp = gethostbyname(host);
117
        if (hp == NULL) {
118
                printf("Could not get host entity for %s\n", host);
119
                perror("O/S Err:");
120
                exit(-1);
121
        }
122
        bcopy(hp->h_addr, &serv_addr.sin_addr.s_addr, hp->h_length);
123
 
124
        serv_addr.sin_family = AF_INET;
125
        serv_addr.sin_port = htons(port);
126
 
127
        if (connect(m_fdr,(struct sockaddr *)&serv_addr, sizeof(serv_addr))< 0){
128
                perror("Connect Failed Err");
129
                exit(-1);
130
        }
131
 
132
        m_fdw = m_fdr;
133
}
134
 

powered by: WebSVN 2.1.0

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