Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / nrfconnect / pw_sys_io / sys_io_nrfconnect.cc
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 #include <cinttypes>
19
20 #include "console/console.h"
21 #include "pw_sys_io/sys_io.h"
22 #include <cassert>
23 #include <zephyr.h>
24
25 #ifdef CONFIG_USB
26 #include <usb/usb_device.h>
27 #endif
28
29 extern "C" void pw_sys_io_Init()
30 {
31     int err;
32
33 #ifdef CONFIG_USB
34     err = usb_enable(nullptr);
35     assert(err == 0);
36 #endif
37
38     err = console_init();
39     assert(err == 0);
40 }
41
42 namespace pw::sys_io {
43
44 Status ReadByte(std::byte * dest)
45 {
46     if (!dest)
47         return Status::InvalidArgument();
48
49     const int c = console_getchar();
50     *dest       = static_cast<std::byte>(c);
51
52     return c < 0 ? Status::FailedPrecondition() : OkStatus();
53 }
54
55 Status WriteByte(std::byte b)
56 {
57     return console_putchar(static_cast<char>(b)) < 0 ? Status::FailedPrecondition() : OkStatus();
58 }
59
60 // Writes a string using pw::sys_io, and add newline characters at the end.
61 StatusWithSize WriteLine(const std::string_view & s)
62 {
63     size_t chars_written  = 0;
64     StatusWithSize result = WriteBytes(std::as_bytes(std::span(s)));
65     if (!result.ok())
66     {
67         return result;
68     }
69     chars_written += result.size();
70
71     // Write trailing newline.
72     result = WriteBytes(std::as_bytes(std::span("\r\n", 2)));
73     chars_written += result.size();
74
75     return StatusWithSize(result.status(), chars_written);
76 }
77
78 } // namespace pw::sys_io