114fe6698SNan Zhou // Copyright 2021 Google LLC 214fe6698SNan Zhou // 314fe6698SNan Zhou // Licensed under the Apache License, Version 2.0 (the "License"); 414fe6698SNan Zhou // you may not use this file except in compliance with the License. 514fe6698SNan Zhou // You may obtain a copy of the License at 614fe6698SNan Zhou // 714fe6698SNan Zhou // http://www.apache.org/licenses/LICENSE-2.0 814fe6698SNan Zhou // 914fe6698SNan Zhou // Unless required by applicable law or agreed to in writing, software 1014fe6698SNan Zhou // distributed under the License is distributed on an "AS IS" BASIS, 1114fe6698SNan Zhou // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1214fe6698SNan Zhou // See the License for the specific language governing permissions and 1314fe6698SNan Zhou // limitations under the License. 1414fe6698SNan Zhou 1514fe6698SNan Zhou #include "serializer.hpp" 1614fe6698SNan Zhou 1714fe6698SNan Zhou #include "event_message.pb.h" 1814fe6698SNan Zhou 1914fe6698SNan Zhou #include <phosphor-logging/log.hpp> 2014fe6698SNan Zhou 21*bb53161dSWilly Tu #include <format> 22*bb53161dSWilly Tu 2314fe6698SNan Zhou using phosphor::logging::level; 2414fe6698SNan Zhou using phosphor::logging::log; 2514fe6698SNan Zhou Serialize(const NemoraDatagram * dgram)2614fe6698SNan Zhoustd::string Serializer::Serialize(const NemoraDatagram* dgram) 2714fe6698SNan Zhou { 2814fe6698SNan Zhou std::string result; 2914fe6698SNan Zhou switch (dgram->type) 3014fe6698SNan Zhou { 3114fe6698SNan Zhou case NemoraDatagramType::NemoraEvent: 3214fe6698SNan Zhou result = SerializeEvent(static_cast<const NemoraEvent*>(dgram)); 3314fe6698SNan Zhou break; 3414fe6698SNan Zhou default: 3514fe6698SNan Zhou log<level::ERR>( 36*bb53161dSWilly Tu std::format("Type with ID {} not supported by " 3714fe6698SNan Zhou "Serializer::Serialize(const NemoraDatagram*)", 3814fe6698SNan Zhou static_cast<int>(dgram->type)) 3914fe6698SNan Zhou .c_str()); 4014fe6698SNan Zhou } 4114fe6698SNan Zhou 4214fe6698SNan Zhou return result; 4314fe6698SNan Zhou } 4414fe6698SNan Zhou SerializeEvent(const NemoraEvent * event)4514fe6698SNan Zhoustd::string Serializer::SerializeEvent(const NemoraEvent* event) 4614fe6698SNan Zhou { 4714fe6698SNan Zhou std::string result; 4814fe6698SNan Zhou platforms::nemora::proto::EventSeries pb; 4914fe6698SNan Zhou 5014fe6698SNan Zhou pb.set_magic(NEMORA_EVENT_PB_MAGIC); 5114fe6698SNan Zhou 5214fe6698SNan Zhou const char* p_arr = reinterpret_cast<const char*>(event->mac); 5314fe6698SNan Zhou pb.set_mac(p_arr, MAC_ADDR_SIZE); 5414fe6698SNan Zhou 5514fe6698SNan Zhou pb.set_sent_time_us(event->sent_time_s * 1000000); 5614fe6698SNan Zhou 5714fe6698SNan Zhou for (auto postcode : event->postcodes) 5814fe6698SNan Zhou { 5914fe6698SNan Zhou pb.add_postcodes(postcode); 6014fe6698SNan Zhou } 6114fe6698SNan Zhou 6214fe6698SNan Zhou pb.set_postcodes_protocol( 6314fe6698SNan Zhou platforms::nemora::proto::EventSeries::NATIVE_32_BIT); 6414fe6698SNan Zhou 65580abaffSWilly Tu log<level::INFO>( 66*bb53161dSWilly Tu std::format("NemoraEvent {}", pb.DebugString().c_str()).c_str()); 6714fe6698SNan Zhou pb.SerializeToString(&result); 6814fe6698SNan Zhou return result; 6914fe6698SNan Zhou } 70