blob: f87d6d258b1ecf2a0e36319ecb85cde307efc501 [file] [log] [blame]
Nan Zhou14fe6692021-06-08 16:35:44 -07001// 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
Nan Zhou14fe6692021-06-08 16:35:44 -070019#include <phosphor-logging/log.hpp>
20
Willy Tubb531612023-07-16 01:30:08 -070021#include <format>
22
Nan Zhou14fe6692021-06-08 16:35:44 -070023using phosphor::logging::level;
24using phosphor::logging::log;
25
26std::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>(
Willy Tubb531612023-07-16 01:30:08 -070036 std::format("Type with ID {} not supported by "
37 "Serializer::Serialize(const NemoraDatagram*)",
38 static_cast<int>(dgram->type))
Nan Zhou14fe6692021-06-08 16:35:44 -070039 .c_str());
40 }
41
42 return result;
43}
44
45std::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
Willy Tu580abaf2023-07-06 21:27:48 -070065 log<level::INFO>(
Willy Tubb531612023-07-16 01:30:08 -070066 std::format("NemoraEvent {}", pb.DebugString().c_str()).c_str());
Nan Zhou14fe6692021-06-08 16:35:44 -070067 pb.SerializeToString(&result);
68 return result;
69}