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