Merge PR #29
[deb_libcec.git] / src / lib / platform / sockets / cdevsocket.h
CommitLineData
4d3a7562 1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
16f47961 5 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
4d3a7562 6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34#include "lib/platform/os.h"
35#include "lib/platform/util/buffer.h"
36
37#include <string>
38#include <stdint.h>
39
40#if !defined(__WINDOWS__)
41#include <termios.h>
42#endif
43
44#include "socket.h"
45
46namespace PLATFORM
47{
48 class CCDevSocket : public CCommonSocket<chardev_socket_t>
49 {
50 public:
51 CCDevSocket(const std::string &strName ) :
52 CCommonSocket<chardev_socket_t>(INVALID_CHARDEV_SOCKET_VALUE, strName)
53 #ifdef __WINDOWS__
54 ,m_iCurrentReadTimeout(MAXDWORD)
55 #endif
56 {}
57
58 virtual ~CCDevSocket(void)
59 {
60 Close();
61 }
62
63 virtual bool Open(uint64_t iTimeoutMs = 0)
64 {
65 (void)iTimeoutMs;
66
67 if (IsOpen())
68 return false;
69
70 m_socket = open(m_strName.c_str(), O_RDWR );
71
72 if (m_socket == INVALID_CHARDEV_SOCKET_VALUE)
73 {
74 m_strError = strerror(errno);
75 return false;
76 }
77
78 return true;
79 }
80
81 virtual void Close(void)
82 {
83 if (IsOpen())
84 {
85 SocketClose(m_socket);
86 m_socket = INVALID_CHARDEV_SOCKET_VALUE;
87 }
88 }
89
90 virtual void Shutdown(void)
91 {
92 SocketClose(m_socket);
93 }
94
95 virtual int Ioctl(int request, void* data)
96 {
97 return IsOpen() ? SocketIoctl(m_socket, &m_iError, request, data) : -1;
98 }
99
100 virtual ssize_t Write(void* data, size_t len)
101 {
102 return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1;
103 }
104
105 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
106 {
107 return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
108 }
109
110 virtual bool IsOpen(void)
111 {
112 return m_socket != INVALID_CHARDEV_SOCKET_VALUE;
113 }
114 };
115
116};
117