Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / efr32 / PigweedLogger.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /*
20  * @file PigweedLogger.cpp
21  *
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.
25  */
26
27 #include <FreeRTOS.h>
28
29 #include "semphr.h"
30 #include <pw_hdlc/encoder.h>
31 #include <pw_stream/sys_io_stream.h>
32 #include <pw_sys_io_efr32/init.h>
33
34 #include <cassert>
35 #include <cstdint>
36 #include <span>
37 #include <string_view>
38
39 namespace PigweedLogger {
40 namespace {
41
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
44
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;
48
49 static pw::stream::SysIoWriter sWriter;
50 static size_t sWriteBufferPos;
51 static char sWriteBuffer[kWriteBufferSize];
52
53 static void send(void)
54 {
55     pw::hdlc::WriteUIFrame(kLogHdlcAddress, std::as_bytes(std::span(sWriteBuffer, sWriteBufferPos)), sWriter);
56     sWriteBufferPos = 0;
57 }
58
59 } // namespace
60
61 void init(void)
62 {
63     sLoggerLock = xSemaphoreCreateMutex();
64     assert(sLoggerLock != NULL);
65
66     pw_sys_io_Init();
67 }
68
69 int putString(const char * buffer, size_t size)
70 {
71     assert(sWriteBufferPos < kWriteBufferSize);
72
73     xSemaphoreTake(sLoggerLock, portMAX_DELAY);
74
75     for (size_t i = 0; i < size; ++i)
76     {
77         // Send each line excluding "\r\n" in a separate frame
78
79         if (buffer[i] == '\r')
80             continue;
81
82         if (buffer[i] == '\n')
83         {
84             send();
85             continue;
86         }
87
88         sWriteBuffer[sWriteBufferPos++] = buffer[i];
89
90         if (sWriteBufferPos == kWriteBufferSize)
91             send();
92     }
93
94     xSemaphoreGive(sLoggerLock);
95     return size;
96 }
97
98 SemaphoreHandle_t * GetSemaphore()
99 {
100     return &sLoggerLock;
101 }
102
103 } // namespace PigweedLogger