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 APP_TO_HOST_EVENT_H_
18 #define APP_TO_HOST_EVENT_H_
19 
20 #include "contexthub.h"
21 #include "nanomessage.h"
22 
23 namespace android {
24 
25 // Copied from nanohub eventnums.h
26 struct HostHubRawPacket {
27     uint64_t appId;
28     uint8_t dataLen; //not incl this header, 128 bytes max
29     //raw data in unspecified format here
30 } __attribute((packed));
31 
32 // The u64 appId used in nanohub is 40 bits vendor ID + 24 bits app ID (see seos.h)
MakeAppId(uint64_t vendorId,uint32_t appId)33 constexpr uint64_t MakeAppId(uint64_t vendorId, uint32_t appId) {
34     return (vendorId << 24) | (appId & 0x00FFFFFF);
35 }
36 
37 constexpr uint64_t kAppIdVendorGoogle = 0x476f6f676cULL; // "Googl"
38 
39 constexpr uint64_t kAppIdBoschBmi160Bmm150 = MakeAppId(kAppIdVendorGoogle, 2);
40 constexpr uint64_t kAppIdBoschBmp280       = MakeAppId(kAppIdVendorGoogle, 5);
41 constexpr uint64_t kAppIdAmsTmd2772        = MakeAppId(kAppIdVendorGoogle, 9);
42 constexpr uint64_t kAppIdRohmRpr0521       = MakeAppId(kAppIdVendorGoogle, 10);
43 constexpr uint64_t kAppIdAmsTmd4903        = MakeAppId(kAppIdVendorGoogle, 12);
44 
45 /*
46  * These classes represent events sent with event type EVT_APP_TO_HOST. This is
47  * a generic container for arbitrary application-specific data, and is used for
48  * passing back sensor calibration results, implementing app download, etc. The
49  * parser must know the application ID to determine the data format.
50  */
51 
52 class AppToHostEvent : public ReadEventResponse {
53   public:
54     /*
55      * Constructs and populates an AppToHostEvent instance. Returns nullptr if
56      * the packet is malformed. The rest of the methods in this class are not
57      * guaranteed to be safe unless the object is constructed from this
58      * function.
59      */
60     static std::unique_ptr<AppToHostEvent> FromBytes(
61         const std::vector<uint8_t>& buffer);
62 
63     uint64_t GetAppId() const;
64     // Gets the length of the application-specific data segment
65     uint8_t GetDataLen() const;
66     // Returns a pointer to the application-specific data (i.e. past the header)
67     const uint8_t *GetDataPtr() const;
68 
69     bool IsCalibrationEventForSensor(SensorType sensor_type) const;
70     virtual bool IsValid() const;
71 
72   protected:
73     const HostHubRawPacket *GetTypedData() const;
74 };
75 
76 #define SENSOR_APP_MSG_CALIBRATION_RESULT (0)
77 
78 struct SensorAppEventHeader {
79     uint8_t msgId;
80     uint8_t sensorType;
81     uint8_t status; // 0 for success
82 } __attribute__((packed));
83 
84 struct SingleAxisCalibrationResult : public SensorAppEventHeader {
85     int32_t bias;
86 } __attribute__((packed));
87 
88 struct TripleAxisCalibrationResult : public SensorAppEventHeader {
89     int32_t xBias;
90     int32_t yBias;
91     int32_t zBias;
92 } __attribute__((packed));
93 
94 struct FloatCalibrationResult : public SensorAppEventHeader {
95     float value;
96 } __attribute__((packed));
97 
98 struct FourAxisCalibrationResult : public SensorAppEventHeader {
99     int32_t xBias;
100     int32_t yBias;
101     int32_t zBias;
102     int32_t wBias;
103 } __attribute__((packed));
104 
105 
106 }  // namespace android
107 
108 #endif // APP_TO_HOST_EVENT_H_
109