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 <fmt/format.h> 2014fe6698SNan Zhou 2114fe6698SNan Zhou #include <phosphor-logging/log.hpp> 2214fe6698SNan Zhou 2314fe6698SNan Zhou using fmt::format; 2414fe6698SNan Zhou using phosphor::logging::level; 2514fe6698SNan Zhou using phosphor::logging::log; 2614fe6698SNan Zhou 2714fe6698SNan Zhou std::string Serializer::Serialize(const NemoraDatagram* dgram) 2814fe6698SNan Zhou { 2914fe6698SNan Zhou std::string result; 3014fe6698SNan Zhou switch (dgram->type) 3114fe6698SNan Zhou { 3214fe6698SNan Zhou case NemoraDatagramType::NemoraEvent: 3314fe6698SNan Zhou result = SerializeEvent(static_cast<const NemoraEvent*>(dgram)); 3414fe6698SNan Zhou break; 3514fe6698SNan Zhou default: 3614fe6698SNan Zhou log<level::ERR>( 3714fe6698SNan Zhou format("Type with ID {} not supported by " 3814fe6698SNan Zhou "Serializer::Serialize(const NemoraDatagram*)", 3914fe6698SNan Zhou static_cast<int>(dgram->type)) 4014fe6698SNan Zhou .c_str()); 4114fe6698SNan Zhou } 4214fe6698SNan Zhou 4314fe6698SNan Zhou return result; 4414fe6698SNan Zhou } 4514fe6698SNan Zhou 4614fe6698SNan Zhou std::string Serializer::SerializeEvent(const NemoraEvent* event) 4714fe6698SNan Zhou { 4814fe6698SNan Zhou std::string result; 4914fe6698SNan Zhou platforms::nemora::proto::EventSeries pb; 5014fe6698SNan Zhou 5114fe6698SNan Zhou pb.set_magic(NEMORA_EVENT_PB_MAGIC); 5214fe6698SNan Zhou 5314fe6698SNan Zhou const char* p_arr = reinterpret_cast<const char*>(event->mac); 5414fe6698SNan Zhou pb.set_mac(p_arr, MAC_ADDR_SIZE); 5514fe6698SNan Zhou 5614fe6698SNan Zhou pb.set_sent_time_us(event->sent_time_s * 1000000); 5714fe6698SNan Zhou 5814fe6698SNan Zhou for (auto postcode : event->postcodes) 5914fe6698SNan Zhou { 6014fe6698SNan Zhou pb.add_postcodes(postcode); 6114fe6698SNan Zhou } 6214fe6698SNan Zhou 6314fe6698SNan Zhou pb.set_postcodes_protocol( 6414fe6698SNan Zhou platforms::nemora::proto::EventSeries::NATIVE_32_BIT); 6514fe6698SNan Zhou 66*580abaffSWilly Tu log<level::INFO>( 67*580abaffSWilly Tu format("NemoraEvent {}", pb.DebugString().c_str()).c_str()); 6814fe6698SNan Zhou pb.SerializeToString(&result); 6914fe6698SNan Zhou return result; 7014fe6698SNan Zhou } 71