The Pedigree Project 0.1
hosted/Serial.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "Serial.h"
21#include "pedigree/kernel/utilities/Cord.h"
22#include "pedigree/kernel/utilities/StaticString.h"
23
24#include <fcntl.h>
25#include <stdio.h>
26#include <unistd.h>
27
28HostedSerial::HostedSerial() : m_File(-1), m_nFileNumber(0) {}
29
30HostedSerial::~HostedSerial() {
31 if (m_File >= 0) {
32 close(m_File);
33 m_File = -1;
34 }
35}
36
37void HostedSerial::setBase(uintptr_t nBaseAddr) {
38 m_nFileNumber = nBaseAddr;
39 if (m_File >= 0)
40 close(m_File);
41
43 s.clear();
44 s.append("serial");
45 s.append(m_nFileNumber);
46 s.append(".log");
47 m_File = open(static_cast<const char*>(s), O_TRUNC | O_CREAT | O_WRONLY, 0644);
48}
49
50char HostedSerial::read() {
51 // Cannot do.
52 return '\0';
53}
54
55bool HostedSerial::hasData() {
56 return false;
57}
58
59char HostedSerial::readNonBlock() {
60 return read();
61}
62
63void HostedSerial::write(char c) {
64 char buf[2] = {c, 0};
65 ::write(m_File, buf, 1);
66 ::fsync(m_File);
67 ::write(2, buf, 1);
68 ::fsync(2);
69}
70
71void HostedSerial::write_str(const char* c) {
72 ::write(m_File, c, StringLength(c));
73 ::fsync(m_File);
74 ::write(2, c, StringLength(c));
75 ::fsync(2);
76}
77
78void HostedSerial::write_str(const char* c, size_t len) {
79 ::write(m_File, c, len);
80 ::fsync(m_File);
81 ::write(2, c, len);
82 ::fsync(2);
83}
84
85void HostedSerial::write_str(const Cord& cord) {
86 for (auto it = cord.segbegin(); it != cord.segend(); ++it) {
87 ::write(m_File, it.ptr(), it.length());
88 ::write(2, it.ptr(), it.length());
89 }
90
91 ::fsync(m_File);
92 ::fsync(2);
93}
94
95bool HostedSerial::isConnected() {
96 return m_File >= 0;
97}
Definition Cord.h:33
virtual void setBase(uintptr_t nBaseAddr)