blob: 8e013c3b2a88de580d084d352fa636f21df5ba69 [file] [log] [blame]
Armando Montanez68de0712019-11-14 18:29:39 -08001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Armando Montanez68de0712019-11-14 18:29:39 -08006//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Wyatt Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Armando Montanez68de0712019-11-14 18:29:39 -080014
Armando Montanezf7a5a742020-03-02 14:58:59 -080015#include "pw_sys_io/sys_io.h"
Armando Montanez68de0712019-11-14 18:29:39 -080016
Armando Montanezf7a5a742020-03-02 14:58:59 -080017namespace pw::sys_io {
Armando Montanez68de0712019-11-14 18:29:39 -080018
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070019StatusWithSize ReadBytes(std::span<std::byte> dest) {
Armando Montanez68de0712019-11-14 18:29:39 -080020 for (size_t i = 0; i < dest.size_bytes(); ++i) {
Armando Montanez759ff772019-11-18 15:05:53 -080021 Status result = ReadByte(&dest[i]);
Armando Montanez68de0712019-11-14 18:29:39 -080022 if (!result.ok()) {
23 return StatusWithSize(result, i);
24 }
25 }
26 return StatusWithSize(dest.size_bytes());
27}
28
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070029StatusWithSize WriteBytes(std::span<const std::byte> src) {
Armando Montanez68de0712019-11-14 18:29:39 -080030 for (size_t i = 0; i < src.size_bytes(); ++i) {
Armando Montanez759ff772019-11-18 15:05:53 -080031 Status result = WriteByte(src[i]);
Armando Montanez68de0712019-11-14 18:29:39 -080032 if (!result.ok()) {
33 return StatusWithSize(result, i);
34 }
35 }
36 return StatusWithSize(src.size_bytes());
37}
38
Armando Montanezf7a5a742020-03-02 14:58:59 -080039} // namespace pw::sys_io