3 * Copyright (c) 2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * @file PigweedLogger.cpp
22 * This file contains basic string writting function, based on Pigweed HDLC
23 * over UART transport. It allows to send log messages even if the application
24 * needs to use HDLC/UART for another purpose like the RPC server.
30 #include <pw_hdlc/encoder.h>
31 #include <pw_stream/sys_io_stream.h>
32 #include <pw_sys_io_efr32/init.h>
37 #include <string_view>
39 namespace PigweedLogger {
42 constexpr uint8_t kLogHdlcAddress = 1; // Send log messages to HDLC address 1 (other than RPC communication)
43 constexpr size_t kWriteBufferSize = 128; // Buffer for constructing HDLC frames
45 // Exclusive access to the backend is needed to make sure that log messages coming
46 // from different threads are not interwoven.
47 SemaphoreHandle_t sLoggerLock;
49 static pw::stream::SysIoWriter sWriter;
50 static size_t sWriteBufferPos;
51 static char sWriteBuffer[kWriteBufferSize];
53 static void send(void)
55 pw::hdlc::WriteUIFrame(kLogHdlcAddress, std::as_bytes(std::span(sWriteBuffer, sWriteBufferPos)), sWriter);
63 sLoggerLock = xSemaphoreCreateMutex();
64 assert(sLoggerLock != NULL);
69 int putString(const char * buffer, size_t size)
71 assert(sWriteBufferPos < kWriteBufferSize);
73 xSemaphoreTake(sLoggerLock, portMAX_DELAY);
75 for (size_t i = 0; i < size; ++i)
77 // Send each line excluding "\r\n" in a separate frame
79 if (buffer[i] == '\r')
82 if (buffer[i] == '\n')
88 sWriteBuffer[sWriteBufferPos++] = buffer[i];
90 if (sWriteBufferPos == kWriteBufferSize)
94 xSemaphoreGive(sLoggerLock);
98 SemaphoreHandle_t * GetSemaphore()
103 } // namespace PigweedLogger