1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef android_hardware_automotive_vehicle_V2_0_VmsUtils_H_
18#define android_hardware_automotive_vehicle_V2_0_VmsUtils_H_
19
20#include <memory>
21#include <string>
22
23#include <android/hardware/automotive/vehicle/2.0/types.h>
24
25namespace android {
26namespace hardware {
27namespace automotive {
28namespace vehicle {
29namespace V2_0 {
30namespace vms {
31
32// VmsUtils are a set of abstractions for creating and parsing Vehicle Property
33// updates to VehicleProperty::VEHICLE_MAP_SERVICE. The format for parsing a
34// VehiclePropValue update with a VMS message is specified in the Vehicle HIDL.
35//
36// This interface is meant for use by HAL clients of VMS; corresponding
37// functionality is also provided by VMS in the embedded car service.
38
39// A VmsLayer is comprised of a type, subtype, and version.
40struct VmsLayer {
41    VmsLayer(int type, int subtype, int version) : type(type), subtype(subtype), version(version) {}
42    int type;
43    int subtype;
44    int version;
45};
46
47struct VmsLayerAndPublisher {
48    VmsLayer layer;
49    int publisher_id;
50};
51
52// A VmsAssociatedLayer is used by subscribers to specify which publisher IDs
53// are acceptable for a given layer.
54struct VmsAssociatedLayer {
55    VmsLayer layer;
56    std::vector<int> publisher_ids;
57};
58
59// A VmsLayerOffering refers to a single layer that can be published, along with
60// its dependencies. Dependencies can be empty.
61struct VmsLayerOffering {
62    VmsLayerOffering(VmsLayer layer, std::vector<VmsLayer> dependencies)
63        : layer(layer), dependencies(dependencies) {}
64    VmsLayerOffering(VmsLayer layer) : layer(layer), dependencies() {}
65    VmsLayer layer;
66    std::vector<VmsLayer> dependencies;
67};
68
69// A VmsSubscriptionsState is delivered in response to a
70// VmsMessageType.SUBSCRIPTIONS_REQUEST or on the first SUBSCRIBE or last
71// UNSUBSCRIBE for a layer. It indicates which layers or associated_layers are
72// currently being subscribed to in the system.
73struct VmsSubscriptionsState {
74    int sequence_number;
75    std::vector<VmsLayer> layers;
76    std::vector<VmsAssociatedLayer> associated_layers;
77};
78
79struct VmsAvailabilityState {
80    int sequence_number;
81    std::vector<VmsAssociatedLayer> associated_layers;
82};
83
84// Creates a VehiclePropValue containing a message of type
85// VmsMessageType.SUBSCRIBE, specifying to the VMS service
86// which layer to subscribe to.
87std::unique_ptr<VehiclePropValue> createSubscribeMessage(const VmsLayer& layer);
88
89// Creates a VehiclePropValue containing a message of type
90// VmsMessageType.SUBSCRIBE_TO_PUBLISHER, specifying to the VMS service
91// which layer and publisher_id to subscribe to.
92std::unique_ptr<VehiclePropValue> createSubscribeToPublisherMessage(
93    const VmsLayerAndPublisher& layer);
94
95// Creates a VehiclePropValue containing a message of type
96// VmsMessageType.UNSUBSCRIBE, specifying to the VMS service
97// which layer to unsubscribe from.
98std::unique_ptr<VehiclePropValue> createUnsubscribeMessage(const VmsLayer& layer);
99
100// Creates a VehiclePropValue containing a message of type
101// VmsMessageType.UNSUBSCRIBE_TO_PUBLISHER, specifying to the VMS service
102// which layer and publisher_id to unsubscribe from.
103std::unique_ptr<VehiclePropValue> createUnsubscribeToPublisherMessage(
104    const VmsLayerAndPublisher& layer);
105
106// Creates a VehiclePropValue containing a message of type
107// VmsMessageType.OFFERING, specifying to the VMS service which layers are being
108// offered and their dependencies, if any.
109std::unique_ptr<VehiclePropValue> createOfferingMessage(
110    const std::vector<VmsLayerOffering>& offering);
111
112// Creates a VehiclePropValue containing a message of type
113// VmsMessageType.AVAILABILITY_REQUEST.
114std::unique_ptr<VehiclePropValue> createAvailabilityRequest();
115
116// Creates a VehiclePropValue containing a message of type
117// VmsMessageType.AVAILABILITY_REQUEST.
118std::unique_ptr<VehiclePropValue> createSubscriptionsRequest();
119
120// Creates a VehiclePropValue containing a message of type VmsMessageType.DATA.
121// Returns a nullptr if the byte string in bytes is empty.
122//
123// For example, to build a VehiclePropMessage containing a proto, the caller
124// should convert the proto to a byte string using the SerializeToString proto
125// API, then use this inteface to build the VehicleProperty.
126std::unique_ptr<VehiclePropValue> createDataMessage(const std::string& bytes);
127
128// Returns true if the VehiclePropValue pointed to by value contains a valid Vms
129// message, i.e. the VehicleProperty, VehicleArea, and VmsMessageType are all
130// valid. Note: If the VmsMessageType enum is extended, this function will
131// return false for any new message types added.
132bool isValidVmsMessage(const VehiclePropValue& value);
133
134// Returns the message type. Expects that the VehiclePropValue contains a valid
135// Vms message, as verified by isValidVmsMessage.
136VmsMessageType parseMessageType(const VehiclePropValue& value);
137
138// Constructs a string byte array from a message of type VmsMessageType.DATA.
139// Returns an empty string if the message type doesn't match or if the
140// VehiclePropValue does not contain a byte array.
141//
142// A proto message can then be constructed by passing the result of this
143// function to ParseFromString.
144std::string parseData(const VehiclePropValue& value);
145
146// TODO(aditin): Need to implement additional parsing functions per message
147// type.
148
149}  // namespace vms
150}  // namespace V2_0
151}  // namespace vehicle
152}  // namespace automotive
153}  // namespace hardware
154}  // namespace android
155
156#endif  // android_hardware_automotive_vehicle_V2_0_VmsUtils_H_
157