1 /*
2  * Copyright (C) 2016 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 NANOMESSAGE_H_
18 #define NANOMESSAGE_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "noncopyable.h"
25 
26 namespace android {
27 
28 /*
29  * Events types that can be pushed back and forth between the ContextHub and
30  * host software.
31  */
32 enum class EventType {
33     AppFromHostEvent = 0x000000F8,
34     FirstSensorEvent = 0x00000200,
35     LastSensorEvent  = 0x000002FF,
36     ConfigureSensor  = 0x00000300,
37     AppToHostEvent   = 0x00000401,
38     ResetReasonEvent = 0x00000403,
39     LogEvent         = 0x474F4C41,
40 };
41 
42 /*
43  * An interface for all messages passed to and from the ContextHub.
44  */
45 class NanoMessage : public NonCopyable {
46   public:
~NanoMessage()47     virtual ~NanoMessage() {};
48 
49     // Generates a string intended to be printed to a console or saved to logs.
50     // This interface requires that the string be terminated with a newline.
51     virtual std::string ToString() const = 0;
52 };
53 
54 /*
55  * An interface for requests sent to the ContextHub.
56  */
57 class NanoRequest : public NanoMessage {
58   public:
59     // Returns a payload of bytes to be packaged into a NanoPacket.
60     virtual std::vector<uint8_t> GetBytes() const = 0;
61 };
62 
63 /*
64  * An interface for responses from the ContextHub.
65  */
66 class NanoResponse : public NanoMessage {
67   public:
68     // Populates the fields of the NanoMessage given a NanoPacket. Returns
69     // false if the packet is incomplete or incorrect message.
70     virtual bool Populate(const std::vector<uint8_t>& buffer) = 0;
71 };
72 
73 /*
74  * Version information for a ContextHub.
75  */
76 class HardwareVersionInfo : public NanoResponse {
77   public:
78     bool Populate(const std::vector<uint8_t>& buffer) override;
79     std::string ToString() const override;
80 
81     struct VersionInfo {
82         uint16_t hardware_type;
83         uint16_t hardware_version;
84         uint16_t bootloader_version;
85         uint16_t operating_system_version;
86         uint32_t variant_version;
87     } __attribute__((packed)) info;
88 };
89 
90 /*
91  * The base event for all event data.
92  */
93 struct Event {
94     uint32_t event_type;
95 } __attribute__((packed));
96 
97 /*
98  * A request to write an event to the ContextHub.
99  */
100 class WriteEventRequest : public NanoRequest {
101   public:
102     virtual EventType GetEventType() const = 0;
103 };
104 
105 /*
106  * A response to writing an event to the ContextHub.
107  */
108 class WriteEventResponse : public NanoResponse {
109   public:
110     std::string ToString() const override;
111     bool Populate(const std::vector<uint8_t>& buffer) override;
112 
113     struct Response {
114         bool accepted;
115     } __attribute__((packed)) response;
116 };
117 
118 /*
119  * A response to reading an event from the ContextHub.
120  */
121 class ReadEventRequest : public NanoRequest {
122   public:
123     std::vector<uint8_t> GetBytes() const override;
124     std::string ToString() const override;
125 
126     struct Request {
127         uint64_t boot_time;
128     } __attribute__((packed)) request;
129 };
130 
131 class ReadEventResponse : public NanoResponse {
132   public:
133     virtual std::string ToString() const override;
134 
135     // Construct and populate a concrete ReadEventResponse from the given buffer
136     static std::unique_ptr<ReadEventResponse> FromBytes(
137         const std::vector<uint8_t>& buffer);
138 
139     bool Populate(const std::vector<uint8_t>& buffer) override;
140 
141     bool IsAppToHostEvent() const;
142     bool IsSensorEvent() const;
143     bool IsResetReasonEvent() const;
144     bool IsLogEvent() const;
145     uint32_t GetEventType() const;
146 
147     // Event data associated with this response.
148     std::vector<uint8_t> event_data;
149 
150   protected:
151     static uint32_t EventTypeFromBuffer(const std::vector<uint8_t>& buffer);
152     static bool IsAppToHostEvent(uint32_t event_type);
153     static bool IsSensorEvent(uint32_t event_type);
154     static bool IsResetReasonEvent(uint32_t event_type);
155     static bool IsLogEvent(uint32_t event_type);
156 };
157 
158 /*
159  * An event used to configure a sensor with specific attributes.
160  */
161 class ConfigureSensorRequest : public WriteEventRequest {
162   public:
163     enum class CommandType {
164         Disable,
165         Enable,
166         Flush,
167         ConfigData,
168         Calibrate,
169         SelfTest
170     };
171 
172     ConfigureSensorRequest();
173 
174     static uint32_t FloatRateToFixedPoint(float rate);
175     static float FixedPointRateToFloat(uint32_t rate);
176 
177     std::vector<uint8_t> GetBytes() const override;
178     std::string ToString() const override;
179     EventType GetEventType() const override;
180 
181     // Appends some data to the configuration request, e.g. for the ConfigData
182     // command
183     void SetAdditionalData(const std::vector<uint8_t>& data);
184 
185     struct Configuration : public Event {
186         uint64_t latency;
187         uint32_t rate;
188         uint8_t sensor_type;
189         uint8_t command;
190         uint16_t flags;
191     }  __attribute__((packed)) config = {};
192 
193   private:
194     std::vector<uint8_t> extra_data_;
195 };
196 
197 class BridgeVersionInfoRequest : public WriteEventRequest {
198   public:
199     //BridgeVersionInfoRequest() {};
200     std::vector<uint8_t> GetBytes() const override;
201     EventType GetEventType() const override;
202     std::string ToString() const override;
203 };
204 
205 }  // namespace android
206 
207 #endif  // NANOMESSAGE_H_
208