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