1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
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
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 //==============================================================================
15
16 #include "pw_trace_tokenized/trace_rpc_service_nanopb.h"
17
18 #include "pw_log/log.h"
19 #include "pw_preprocessor/util.h"
20 #include "pw_trace_tokenized/trace_buffer.h"
21 #include "pw_trace_tokenized/trace_tokenized.h"
22
23 namespace pw::trace {
24
Enable(ServerContext &,const pw_trace_TraceEnableMessage & request,pw_trace_TraceEnableMessage & response)25 pw::Status TraceService::Enable(ServerContext&,
26 const pw_trace_TraceEnableMessage& request,
27 pw_trace_TraceEnableMessage& response) {
28 TokenizedTrace::Instance().Enable(request.enable);
29 response.enable = TokenizedTrace::Instance().IsEnabled();
30 return PW_STATUS_OK;
31 }
32
IsEnabled(ServerContext &,const pw_trace_Empty &,pw_trace_TraceEnableMessage & response)33 pw::Status TraceService::IsEnabled(ServerContext&,
34 const pw_trace_Empty&,
35 pw_trace_TraceEnableMessage& response) {
36 response.enable = TokenizedTrace::Instance().IsEnabled();
37 return PW_STATUS_OK;
38 }
39
GetTraceData(ServerContext &,const pw_trace_Empty &,ServerWriter<pw_trace_TraceDataMessage> & writer)40 void TraceService::GetTraceData(
41 ServerContext&,
42 const pw_trace_Empty&,
43 ServerWriter<pw_trace_TraceDataMessage>& writer) {
44 pw_trace_TraceDataMessage buffer = pw_trace_TraceDataMessage_init_default;
45 size_t size = 0;
46 pw::ring_buffer::PrefixedEntryRingBuffer* trace_buffer =
47 pw::trace::GetBuffer();
48
49 while (trace_buffer->PeekFront(
50 std::as_writable_bytes(std::span(buffer.data.bytes)), &size) !=
51 pw::Status::OutOfRange()) {
52 trace_buffer->PopFront();
53 buffer.data.size = size;
54 pw::Status status = writer.Write(buffer);
55 if (!status.ok()) {
56 PW_LOG_ERROR("Error sending trace; abandoning trace dump. Error: %s",
57 status.str());
58 break;
59 }
60 }
61 writer.Finish();
62 }
63 } // namespace pw::trace