Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / util / ember-print.cpp
1 /**
2  *
3  *    Copyright (c) 2020-2021 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 #include <stddef.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "debug-printing.h"
22
23 #include <platform/CHIPDeviceConfig.h>
24 #include <support/CodeUtils.h>
25 #include <support/logging/CHIPLogging.h>
26
27 bool emberAfPrintReceivedMessages = true;
28
29 using namespace chip::Logging;
30
31 void emberAfPrint(int category, const char * format, ...)
32 {
33     if (format != nullptr)
34     {
35         va_list args;
36         va_start(args, format);
37         chip::Logging::LogV(chip::Logging::kLogModule_Zcl, chip::Logging::kLogCategory_Progress, format, args);
38         va_end(args);
39     }
40 }
41
42 void emberAfPrintln(int category, const char * format, ...)
43 {
44     if (format != nullptr)
45     {
46         va_list args;
47         va_start(args, format);
48         chip::Logging::LogV(chip::Logging::kLogModule_Zcl, chip::Logging::kLogCategory_Progress, format, args);
49         va_end(args);
50     }
51 }
52
53 // TODO: add unit tests.
54
55 void emberAfPrintBuffer(int category, const uint8_t * buffer, uint16_t length, bool withSpace)
56 {
57     if (buffer != nullptr && length > 0)
58     {
59         constexpr uint16_t kBufferSize = CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE;
60         const char * perByteFormatStr  = withSpace ? "%02X " : "%02X";
61         const uint8_t perByteCharCount = withSpace ? 3 : 2;
62         const uint16_t bytesPerBuffer  = static_cast<uint16_t>((kBufferSize - 1) / perByteCharCount);
63         char result[kBufferSize];
64
65         uint16_t index = 0;
66         while (index < length)
67         {
68             const uint16_t remainingBytes = static_cast<uint16_t>(length - index);
69             const uint16_t segmentLength  = chip::min(bytesPerBuffer, remainingBytes);
70             const uint16_t segmentEnd     = static_cast<uint16_t>(index + segmentLength);
71             const uint32_t outStringEnd   = segmentLength * perByteCharCount;
72             for (uint32_t dst_idx = 0; dst_idx < outStringEnd && index < segmentEnd; dst_idx += perByteCharCount, index++)
73             {
74                 snprintf(result + dst_idx, outStringEnd - dst_idx + 1, perByteFormatStr, buffer[index]);
75             }
76             result[outStringEnd] = 0;
77             emberAfPrint(category, "%s", result);
78         }
79     }
80     else
81     {
82         emberAfPrint(EMBER_AF_PRINT_CORE, "NULL");
83     }
84 }
85
86 void emberAfPrintString(int category, const uint8_t * string)
87 {
88     emberAfPrint(category, "%s", string);
89 }