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 // From brHostEvent.h
33 #define BRIDGE_HOST_EVENT_MSG_VERSION_INFO (0)
34 
35 struct BrHostEventData {
36     uint8_t msgId;
37     uint8_t reserved;
38     uint8_t status;
39     uint8_t payload[];
40 } __attribute__((packed));
41 
42 struct BrHostEventTx {
43     struct HostHubRawPacket hdr;
44     struct BrHostEventData  data;
45 } __attribute__((packed));
46 
47 // From brPkt.h
48 struct BrVersionInfoRsp {
49     uint16_t hwType;
50     uint16_t osVer;
51     uint32_t variantVer;
52     uint32_t bridgeVer;
53 } __attribute__((packed));
54 
55 // 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)56 constexpr uint64_t MakeAppId(uint64_t vendorId, uint32_t appId) {
57     return (vendorId << 24) | (appId & 0x00FFFFFF);
58 }
59 
60 constexpr uint64_t kAppIdVendorGoogle = 0x476f6f676cULL; // "Googl"
61 
62 constexpr uint64_t kAppIdBoschBmi160Bmm150 = MakeAppId(kAppIdVendorGoogle, 2);
63 constexpr uint64_t kAppIdBoschBmp280       = MakeAppId(kAppIdVendorGoogle, 5);
64 constexpr uint64_t kAppIdAmsTmd2772        = MakeAppId(kAppIdVendorGoogle, 9);
65 constexpr uint64_t kAppIdRohmRpr0521       = MakeAppId(kAppIdVendorGoogle, 10);
66 constexpr uint64_t kAppIdAmsTmd4903        = MakeAppId(kAppIdVendorGoogle, 12);
67 
68 constexpr uint64_t kAppIdBridge = MakeAppId(kAppIdVendorGoogle, 50);
69 
70 /*
71  * These classes represent events sent with event type EVT_APP_TO_HOST. This is
72  * a generic container for arbitrary application-specific data, and is used for
73  * passing back sensor calibration results, implementing app download, etc. The
74  * parser must know the application ID to determine the data format.
75  */
76 
77 class AppToHostEvent : public ReadEventResponse {
78   public:
79     /*
80      * Constructs and populates an AppToHostEvent instance. Returns nullptr if
81      * the packet is malformed. The rest of the methods in this class are not
82      * guaranteed to be safe unless the object is constructed from this
83      * function.
84      */
85     static std::unique_ptr<AppToHostEvent> FromBytes(
86         const std::vector<uint8_t>& buffer);
87 
88     uint64_t GetAppId() const;
89     // Gets the length of the application-specific data segment
90     uint8_t GetDataLen() const;
91     // Returns a pointer to the application-specific data (i.e. past the header)
92     const uint8_t *GetDataPtr() const;
93 
94     bool IsCalibrationEventForSensor(SensorType sensor_type) const;
95     bool IsTestEventForSensor(SensorType sensor_type) const;
96     virtual bool IsValid() const;
97 
98   protected:
99     const HostHubRawPacket *GetTypedData() const;
100     bool CheckAppId(SensorType sensor_type) const;
101     bool CheckEventHeader(SensorType sensor_type) const;
102 };
103 
104 #define SENSOR_APP_MSG_CALIBRATION_RESULT (0)
105 #define SENSOR_APP_MSG_TEST_RESULT        (1)
106 
107 struct SensorAppEventHeader {
108     uint8_t msgId;
109     uint8_t sensorType;
110     uint8_t status; // 0 for success
111 } __attribute__((packed));
112 
113 struct SingleAxisCalibrationResult : public SensorAppEventHeader {
114     int32_t bias;
115 } __attribute__((packed));
116 
117 struct TripleAxisCalibrationResult : public SensorAppEventHeader {
118     int32_t xBias;
119     int32_t yBias;
120     int32_t zBias;
121 } __attribute__((packed));
122 
123 struct FloatCalibrationResult : public SensorAppEventHeader {
124     float value;
125 } __attribute__((packed));
126 
127 struct FourAxisCalibrationResult : public SensorAppEventHeader {
128     int32_t xBias;
129     int32_t yBias;
130     int32_t zBias;
131     int32_t wBias;
132 } __attribute__((packed));
133 
134 
135 }  // namespace android
136 
137 #endif // APP_TO_HOST_EVENT_H_
138