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 constexpr uint64_t kAppIdVendorSTMicro = 0x53544d6963ULL; // "STMic"
62 constexpr uint64_t kAppIdVendorInvn = 0x496E76656EULL; // "Inven"
63 
64 constexpr uint64_t kAppIdBoschBmi160Bmm150 = MakeAppId(kAppIdVendorGoogle, 2);
65 constexpr uint64_t kAppIdBoschBmp280       = MakeAppId(kAppIdVendorGoogle, 5);
66 constexpr uint64_t kAppIdAmsTmd2772        = MakeAppId(kAppIdVendorGoogle, 9);
67 constexpr uint64_t kAppIdRohmRpr0521       = MakeAppId(kAppIdVendorGoogle, 10);
68 constexpr uint64_t kAppIdAmsTmd4903        = MakeAppId(kAppIdVendorGoogle, 12);
69 constexpr uint64_t kAppIdSTMicroLsm6dsm    = MakeAppId(kAppIdVendorSTMicro, 0);
70 constexpr uint64_t kAppIdSTMicroLps22hb    = MakeAppId(kAppIdVendorSTMicro, 1);
71 constexpr uint64_t kAppIdSTMicroMag40      = MakeAppId(kAppIdVendorSTMicro, 3);
72 constexpr uint64_t kAppIdInvnIcm40600      = MakeAppId(kAppIdVendorInvn, 2);
73 
74 constexpr uint64_t kAppIdBridge = MakeAppId(kAppIdVendorGoogle, 50);
75 
76 /*
77  * These classes represent events sent with event type EVT_APP_TO_HOST. This is
78  * a generic container for arbitrary application-specific data, and is used for
79  * passing back sensor calibration results, implementing app download, etc. The
80  * parser must know the application ID to determine the data format.
81  */
82 
83 class AppToHostEvent : public ReadEventResponse {
84   public:
85     /*
86      * Constructs and populates an AppToHostEvent instance. Returns nullptr if
87      * the packet is malformed. The rest of the methods in this class are not
88      * guaranteed to be safe unless the object is constructed from this
89      * function.
90      */
91     static std::unique_ptr<AppToHostEvent> FromBytes(
92         const std::vector<uint8_t>& buffer);
93 
94     uint64_t GetAppId() const;
95     // Gets the length of the application-specific data segment
96     uint8_t GetDataLen() const;
97     // Returns a pointer to the application-specific data (i.e. past the header)
98     const uint8_t *GetDataPtr() const;
99 
100     bool IsCalibrationEventForSensor(SensorType sensor_type) const;
101     bool IsTestEventForSensor(SensorType sensor_type) const;
102     virtual bool IsValid() const;
103 
104   protected:
105     const HostHubRawPacket *GetTypedData() const;
106     bool CheckAppId(SensorType sensor_type) const;
107     bool CheckEventHeader(SensorType sensor_type) const;
108 };
109 
110 #define SENSOR_APP_MSG_CALIBRATION_RESULT (0)
111 #define SENSOR_APP_MSG_TEST_RESULT        (1)
112 
113 struct SensorAppEventHeader {
114     uint8_t msgId;
115     uint8_t sensorType;
116     uint8_t status; // 0 for success
117 } __attribute__((packed));
118 
119 struct SingleAxisCalibrationResult : public SensorAppEventHeader {
120     int32_t bias;
121 } __attribute__((packed));
122 
123 struct TripleAxisCalibrationResult : public SensorAppEventHeader {
124     int32_t xBias;
125     int32_t yBias;
126     int32_t zBias;
127 } __attribute__((packed));
128 
129 struct FloatCalibrationResult : public SensorAppEventHeader {
130     float value;
131 } __attribute__((packed));
132 
133 struct FourAxisCalibrationResult : public SensorAppEventHeader {
134     int32_t xBias;
135     int32_t yBias;
136     int32_t zBias;
137     int32_t wBias;
138 } __attribute__((packed));
139 
140 
141 }  // namespace android
142 
143 #endif // APP_TO_HOST_EVENT_H_
144