1*14fe6698SNan Zhou // Copyright 2021 Google LLC
2*14fe6698SNan Zhou //
3*14fe6698SNan Zhou // Licensed under the Apache License, Version 2.0 (the "License");
4*14fe6698SNan Zhou // you may not use this file except in compliance with the License.
5*14fe6698SNan Zhou // You may obtain a copy of the License at
6*14fe6698SNan Zhou //
7*14fe6698SNan Zhou //      http://www.apache.org/licenses/LICENSE-2.0
8*14fe6698SNan Zhou //
9*14fe6698SNan Zhou // Unless required by applicable law or agreed to in writing, software
10*14fe6698SNan Zhou // distributed under the License is distributed on an "AS IS" BASIS,
11*14fe6698SNan Zhou // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*14fe6698SNan Zhou // See the License for the specific language governing permissions and
13*14fe6698SNan Zhou // limitations under the License.
14*14fe6698SNan Zhou 
15*14fe6698SNan Zhou #include "serializer.hpp"
16*14fe6698SNan Zhou 
17*14fe6698SNan Zhou #include "event_message.pb.h"
18*14fe6698SNan Zhou 
19*14fe6698SNan Zhou #include <fmt/format.h>
20*14fe6698SNan Zhou 
21*14fe6698SNan Zhou #include <phosphor-logging/log.hpp>
22*14fe6698SNan Zhou 
23*14fe6698SNan Zhou using fmt::format;
24*14fe6698SNan Zhou using phosphor::logging::level;
25*14fe6698SNan Zhou using phosphor::logging::log;
26*14fe6698SNan Zhou 
27*14fe6698SNan Zhou std::string Serializer::Serialize(const NemoraDatagram* dgram)
28*14fe6698SNan Zhou {
29*14fe6698SNan Zhou     std::string result;
30*14fe6698SNan Zhou     switch (dgram->type)
31*14fe6698SNan Zhou     {
32*14fe6698SNan Zhou         case NemoraDatagramType::NemoraEvent:
33*14fe6698SNan Zhou             result = SerializeEvent(static_cast<const NemoraEvent*>(dgram));
34*14fe6698SNan Zhou             break;
35*14fe6698SNan Zhou         default:
36*14fe6698SNan Zhou             log<level::ERR>(
37*14fe6698SNan Zhou                 format("Type with ID {} not supported by "
38*14fe6698SNan Zhou                        "Serializer::Serialize(const NemoraDatagram*)",
39*14fe6698SNan Zhou                        static_cast<int>(dgram->type))
40*14fe6698SNan Zhou                     .c_str());
41*14fe6698SNan Zhou     }
42*14fe6698SNan Zhou 
43*14fe6698SNan Zhou     return result;
44*14fe6698SNan Zhou }
45*14fe6698SNan Zhou 
46*14fe6698SNan Zhou std::string Serializer::SerializeEvent(const NemoraEvent* event)
47*14fe6698SNan Zhou {
48*14fe6698SNan Zhou     std::string result;
49*14fe6698SNan Zhou     platforms::nemora::proto::EventSeries pb;
50*14fe6698SNan Zhou 
51*14fe6698SNan Zhou     pb.set_magic(NEMORA_EVENT_PB_MAGIC);
52*14fe6698SNan Zhou 
53*14fe6698SNan Zhou     const char* p_arr = reinterpret_cast<const char*>(event->mac);
54*14fe6698SNan Zhou     pb.set_mac(p_arr, MAC_ADDR_SIZE);
55*14fe6698SNan Zhou 
56*14fe6698SNan Zhou     pb.set_sent_time_us(event->sent_time_s * 1000000);
57*14fe6698SNan Zhou 
58*14fe6698SNan Zhou     for (auto postcode : event->postcodes)
59*14fe6698SNan Zhou     {
60*14fe6698SNan Zhou         pb.add_postcodes(postcode);
61*14fe6698SNan Zhou     }
62*14fe6698SNan Zhou 
63*14fe6698SNan Zhou     pb.set_postcodes_protocol(
64*14fe6698SNan Zhou         platforms::nemora::proto::EventSeries::NATIVE_32_BIT);
65*14fe6698SNan Zhou 
66*14fe6698SNan Zhou     log<level::INFO>(format("NemoraEvent {}", pb.DebugString()).c_str());
67*14fe6698SNan Zhou     pb.SerializeToString(&result);
68*14fe6698SNan Zhou     return result;
69*14fe6698SNan Zhou }
70