~vibhavp/ubuntu/saucy/urg/merge-from-debian

« back to all changes in this revision

Viewing changes to .pc/fix-ftbfs-with-gcc-4.7/src/cpp/connection/SerialDevice_lin.cpp

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2012-05-23 19:55:01 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120523195501-ggk1ajny0dkuatwz
Tags: 0.8.12-4ubuntu1
* Merge from Debian testing.  Remaining changes:
  - debian/patches/fix-as-needed-build.patch:
    fix build with ld --as-needed (LP: #803212)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \file
 
3
  \brief �V���A���ʐM�̎����� (Linux, Mac)
 
4
 
 
5
  \author Satofumi KAMIMURA
 
6
 
 
7
  $Id: SerialDevice_lin.cpp 1811 2010-04-30 16:12:05Z satofumi $
 
8
*/
 
9
 
 
10
#include <fcntl.h>
 
11
#include <termios.h>
 
12
#include <cerrno>
 
13
#include <cstring>
 
14
#include <cstdio>
 
15
 
 
16
 
 
17
class RawSerialDevice
 
18
{
 
19
    enum {
 
20
        InvalidFd = -1,
 
21
    };
 
22
 
 
23
    string error_message_;
 
24
    int fd_;
 
25
    struct termios sio_;        //!< �ʐM�^�[�~�i������
 
26
    fd_set rfds_;               //!< �^�C���A�E�g����
 
27
 
 
28
 
 
29
    bool waitReceive(int timeout)
 
30
    {
 
31
        // �^�C���A�E�g�ݒ�
 
32
        FD_ZERO(&rfds_);
 
33
        FD_SET(fd_, &rfds_);
 
34
 
 
35
        struct timeval tv;
 
36
        tv.tv_sec = timeout / 1000;
 
37
        tv.tv_usec = (timeout % 1000) * 1000;
 
38
 
 
39
        if (select(fd_ + 1, &rfds_, NULL, NULL,
 
40
                   (timeout < 0) ? NULL : &tv) <= 0) {
 
41
            /* �^�C���A�E�g���� */
 
42
            return false;
 
43
        }
 
44
        return true;
 
45
    }
 
46
 
 
47
 
 
48
public:
 
49
    RawSerialDevice(void) : error_message_("no error."), fd_(InvalidFd)
 
50
    {
 
51
    }
 
52
 
 
53
 
 
54
    const char* what(void)
 
55
    {
 
56
        return error_message_.c_str();
 
57
    }
 
58
 
 
59
 
 
60
    bool connect(const char* device, long baudrate)
 
61
    {
 
62
#ifndef MAC_OS
 
63
        enum { O_EXLOCK = 0x0 }; // Linux �ł͎g���Ȃ��̂Ń_�~�[���쐬���Ă���
 
64
#endif
 
65
        fd_ = open(device, O_RDWR | O_EXLOCK | O_NONBLOCK | O_NOCTTY);
 
66
        if (fd_ < 0) {
 
67
            // �ڑ��Ɏ��s
 
68
            error_message_ = string(device) + ": " + strerror(errno);
 
69
            return false;
 
70
        }
 
71
        int flags = fcntl(fd_, F_GETFL, 0);
 
72
        fcntl(fd_, F_SETFL, flags & ~O_NONBLOCK);
 
73
 
 
74
        // �V���A���ʐM�̏�����
 
75
        tcgetattr(fd_, &sio_);
 
76
        sio_.c_iflag = 0;
 
77
        sio_.c_oflag = 0;
 
78
        sio_.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
 
79
        sio_.c_cflag |= CS8 | CREAD | CLOCAL;
 
80
        sio_.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
 
81
 
 
82
        sio_.c_cc[VMIN] = 0;
 
83
        sio_.c_cc[VTIME] = 0;
 
84
 
 
85
        // �{�[���[�g�̕ύX
 
86
        if (! setBaudrate(baudrate)) {
 
87
            return false;
 
88
        }
 
89
        return true;
 
90
    }
 
91
 
 
92
 
 
93
    void disconnect(void)
 
94
    {
 
95
        if (fd_ != InvalidFd) {
 
96
            close(fd_);
 
97
            fd_ = InvalidFd;
 
98
        }
 
99
    }
 
100
 
 
101
 
 
102
    bool isConnected(void)
 
103
    {
 
104
        return (fd_ == InvalidFd) ? false : true;
 
105
    }
 
106
 
 
107
 
 
108
    bool setBaudrate(long baudrate)
 
109
    {
 
110
        long baudrate_value = -1;
 
111
        enum { ErrorMessageSize = 256 };
 
112
        char error_message[ErrorMessageSize];
 
113
 
 
114
        switch (baudrate) {
 
115
 
 
116
        case 4800:
 
117
            baudrate_value = B4800;
 
118
            break;
 
119
 
 
120
        case 9600:
 
121
            baudrate_value = B9600;
 
122
            break;
 
123
 
 
124
        case 19200:
 
125
            baudrate_value = B19200;
 
126
            break;
 
127
 
 
128
        case 38400:
 
129
            baudrate_value = B38400;
 
130
            break;
 
131
 
 
132
        case 57600:
 
133
            baudrate_value = B57600;
 
134
            break;
 
135
 
 
136
        case 115200:
 
137
            baudrate_value = B115200;
 
138
            break;
 
139
 
 
140
        default:
 
141
            sprintf(error_message, "No handle baudrate value: %ld", baudrate);
 
142
            error_message_ = string(error_message);
 
143
            return false;
 
144
        }
 
145
 
 
146
        /* �{�[���[�g�ύX */
 
147
        cfsetospeed(&sio_, baudrate_value);
 
148
        cfsetispeed(&sio_, baudrate_value);
 
149
        tcsetattr(fd_, TCSANOW, &sio_);
 
150
        flush();
 
151
 
 
152
        return true;
 
153
    }
 
154
 
 
155
 
 
156
    int send(const char* data, int count)
 
157
    {
 
158
        if (! isConnected()) {
 
159
            error_message_ = "no connection.";
 
160
            return 0;
 
161
        }
 
162
        return write(fd_, data, count);
 
163
    }
 
164
 
 
165
 
 
166
    int receive(char buffer[], int count, int timeout)
 
167
    {
 
168
        int filled = 0;
 
169
 
 
170
        // �w��T�C�Y�̓ǂݏo�����s��
 
171
        int n = read(fd_, buffer, count);
 
172
        if (n < 0) {
 
173
            return n;
 
174
        }
 
175
        filled += n;
 
176
 
 
177
        // ��M���������Ă�����A�߂�
 
178
        if (filled >= count) {
 
179
            return filled;
 
180
        }
 
181
 
 
182
        // �^�C���A�E�g�t���œǂݏo�����s��
 
183
        while (filled < count) {
 
184
            if (! waitReceive(timeout)) {
 
185
                break;
 
186
            }
 
187
 
 
188
            int required_n = count - filled;
 
189
            n = read(fd_, &buffer[filled], required_n);
 
190
            if (n <= 0) {
 
191
                /* �ǂݏo���G���[�B���݂܂ł̎�M���e�Ŗ߂� */
 
192
                break;
 
193
            }
 
194
            filled += n;
 
195
        }
 
196
        return filled;
 
197
    }
 
198
 
 
199
 
 
200
    void flush(void)
 
201
    {
 
202
        tcdrain(fd_);
 
203
        tcflush(fd_, TCIOFLUSH);
 
204
    }
 
205
};