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 <phosphor-logging/log.hpp> 20 21 #include <format> 22 23 using phosphor::logging::level; 24 using phosphor::logging::log; 25 26 std::string Serializer::Serialize(const NemoraDatagram* dgram) 27 { 28 std::string result; 29 switch (dgram->type) 30 { 31 case NemoraDatagramType::NemoraEvent: 32 result = SerializeEvent(static_cast<const NemoraEvent*>(dgram)); 33 break; 34 default: 35 log<level::ERR>( 36 std::format("Type with ID {} not supported by " 37 "Serializer::Serialize(const NemoraDatagram*)", 38 static_cast<int>(dgram->type)) 39 .c_str()); 40 } 41 42 return result; 43 } 44 45 std::string Serializer::SerializeEvent(const NemoraEvent* event) 46 { 47 std::string result; 48 platforms::nemora::proto::EventSeries pb; 49 50 pb.set_magic(NEMORA_EVENT_PB_MAGIC); 51 52 const char* p_arr = reinterpret_cast<const char*>(event->mac); 53 pb.set_mac(p_arr, MAC_ADDR_SIZE); 54 55 pb.set_sent_time_us(event->sent_time_s * 1000000); 56 57 for (auto postcode : event->postcodes) 58 { 59 pb.add_postcodes(postcode); 60 } 61 62 pb.set_postcodes_protocol( 63 platforms::nemora::proto::EventSeries::NATIVE_32_BIT); 64 65 log<level::INFO>( 66 std::format("NemoraEvent {}", pb.DebugString().c_str()).c_str()); 67 pb.SerializeToString(&result); 68 return result; 69 } 70