Skip to content

Commit 0edfe2c

Browse files
authored
Merge pull request #1 from yperess/zephyr
Pull latest changes from ToT
2 parents 2e1ba54 + bb94995 commit 0edfe2c

File tree

26 files changed

+541
-132
lines changed

26 files changed

+541
-132
lines changed

apps/test/common/chre_cross_validator_sensor/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ endif
1818
NANOAPP_NAME = chre_cross_validator_sensor
1919
NANOAPP_ID = 0x476f6f6754000002
2020
NANOAPP_NAME_STRING = \"Chre\ Cross\ Validator\ Sensor\"
21-
NANOAPP_VERSION = 0x00000001
21+
NANOAPP_VERSION = 0x00000002
2222

2323
NANOAPP_PATH = $(CHRE_PREFIX)/apps/test/common/chre_cross_validator_sensor
2424

apps/test/common/chre_cross_validator_sensor/inc/chre_cross_validator_sensor_manager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace cross_validator_sensor {
3232
// TODO(b/154271551): Break up the Manager class into more fine-grained classes
3333
// to avoid it becoming to complex.
3434

35+
//! The maximum size of a sensor name.
36+
constexpr size_t kMaxSensorNameSize = 128;
37+
3538
/**
3639
* Class to manage a CHRE cross validator nanoapp.
3740
*/
@@ -99,6 +102,9 @@ class Manager {
99102
//! message.
100103
chre::Optional<CrossValidatorState> mCrossValidatorState;
101104

105+
//! A temporary global buffer where the sensor name is stored.
106+
char mSensorNameArray[kMaxSensorNameSize];
107+
102108
/**
103109
* Make a SensorDatapoint proto message.
104110
*
@@ -383,6 +389,16 @@ class Manager {
383389
* during validation.
384390
*/
385391
void cleanup();
392+
393+
/**
394+
* @param sensorType The CHRE sensor type.
395+
* @param sensorIndex The CHRE sensor index as defined in chreSensorFind.
396+
* @param handle A non-null pointer where the sensor handle is stored, if
397+
* found.
398+
*
399+
* @return true if the sensor corresponding to the input is available.
400+
*/
401+
bool getSensor(uint32_t sensorType, uint32_t sensorIndex, uint32_t *handle);
386402
};
387403

388404
// The chre cross validator manager singleton.

apps/test/common/chre_cross_validator_sensor/src/chre_cross_validator_sensor_manager.cc

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,29 @@
2929
#include "chre/util/time.h"
3030
#include "chre_cross_validation_sensor.nanopb.h"
3131

32-
#define LOG_TAG "ChreCrossValidator"
32+
#define LOG_TAG "[ChreCrossValidator]"
3333

3434
namespace chre {
3535

3636
namespace cross_validator_sensor {
3737

38+
namespace {
39+
40+
bool decodeSensorName(pb_istream_t *stream, const pb_field_s *field,
41+
void **arg) {
42+
unsigned char *name = static_cast<unsigned char *>(*arg);
43+
44+
if (stream->bytes_left > kMaxSensorNameSize - 1) return false;
45+
46+
size_t bytesToCopy = stream->bytes_left;
47+
if (!pb_read(stream, name, stream->bytes_left)) return false;
48+
name[bytesToCopy] = '\0';
49+
50+
return true;
51+
}
52+
53+
} // namespace
54+
3855
Manager::~Manager() {
3956
cleanup();
4057
}
@@ -98,6 +115,9 @@ void Manager::handleEvent(uint32_t senderInstanceId, uint16_t eventType,
98115
handleStepCounterData(
99116
static_cast<const chreSensorUint64Data *>(eventData));
100117
break;
118+
case CHRE_EVENT_SENSOR_SAMPLING_CHANGE:
119+
// Ignore sampling state changes
120+
break;
101121
default:
102122
LOGE("Got unknown event type from senderInstanceId %" PRIu32
103123
" and with eventType %" PRIu16,
@@ -312,11 +332,17 @@ bool Manager::handleStartSensorMessage(
312332
uint64_t latencyInNs =
313333
startSensorCommand.latencyInMs * kOneMillisecondInNanoseconds;
314334
bool isContinuous = startSensorCommand.isContinuous;
335+
uint32_t sensorIndex = startSensorCommand.sensorIndex;
336+
315337
uint32_t handle;
316-
if (!chreSensorFindDefault(sensorType, &handle)) {
317-
LOGE("Could not find default sensor for sensorType %" PRIu8, sensorType);
338+
if (!getSensor(sensorType, sensorIndex, &handle)) {
339+
LOGE("Could not find default sensor for sensorType %" PRIu8
340+
" index %" PRIu32,
341+
sensorType, sensorIndex);
318342
// TODO(b/146052784): Test other sensor configure modes
319343
} else {
344+
LOGI("Starting x-validation for sensor type %" PRIu8 " index %" PRIu32,
345+
sensorType, sensorIndex);
320346
chreSensorInfo sensorInfo;
321347
if (!chreGetSensorInfo(handle, &sensorInfo)) {
322348
LOGE("Error getting sensor info for sensor");
@@ -391,6 +417,10 @@ void Manager::handleInfoMessage(uint16_t hostEndpoint,
391417
static_cast<const pb_byte_t *>(hostData->message), hostData->messageSize);
392418
chre_cross_validation_sensor_SensorInfoCommand infoCommand =
393419
chre_cross_validation_sensor_SensorInfoCommand_init_default;
420+
421+
infoCommand.sensorName.funcs.decode = decodeSensorName;
422+
infoCommand.sensorName.arg = mSensorNameArray;
423+
394424
if (!pb_decode(&istream,
395425
chre_cross_validation_sensor_SensorInfoCommand_fields,
396426
&infoCommand)) {
@@ -400,8 +430,31 @@ void Manager::handleInfoMessage(uint16_t hostEndpoint,
400430
infoResponse.has_chreSensorType = true;
401431
infoResponse.chreSensorType = infoCommand.chreSensorType;
402432
infoResponse.has_isAvailable = true;
403-
infoResponse.isAvailable =
404-
chreSensorFindDefault(infoResponse.chreSensorType, &handle);
433+
infoResponse.isAvailable = false;
434+
infoResponse.has_sensorIndex = false;
435+
436+
bool supportsMultiSensors =
437+
chreSensorFind(infoCommand.chreSensorType, 1, &handle);
438+
for (uint8_t i = 0; chreSensorFind(infoCommand.chreSensorType, i, &handle);
439+
i++) {
440+
struct chreSensorInfo info;
441+
if (!chreGetSensorInfo(handle, &info)) {
442+
LOGE("Failed to get sensor info");
443+
} else {
444+
bool equal = true;
445+
if (supportsMultiSensors) {
446+
equal = (strcmp(info.sensorName, mSensorNameArray) == 0);
447+
LOGI("Got sensor name %s in-name %s, equal %d", info.sensorName,
448+
mSensorNameArray, equal);
449+
}
450+
if (equal) {
451+
infoResponse.isAvailable = true;
452+
infoResponse.has_sensorIndex = true;
453+
infoResponse.sensorIndex = i;
454+
break;
455+
}
456+
}
457+
}
405458
}
406459

407460
sendInfoResponse(hostEndpoint, infoResponse);
@@ -609,6 +662,22 @@ bool Manager::sensorTypeIsValid(uint8_t sensorType) {
609662
return sensorType == mCrossValidatorState->sensorType;
610663
}
611664

665+
bool Manager::getSensor(uint32_t sensorType, uint32_t sensorIndex,
666+
uint32_t *handle) {
667+
bool success = false;
668+
669+
bool supportsMultiSensor = (chreGetApiVersion() >= CHRE_API_VERSION_1_5);
670+
if (sensorIndex > UINT8_MAX) {
671+
LOGE("CHRE only supports max of 255 sensor indices");
672+
} else if (!supportsMultiSensor && sensorIndex != 0) {
673+
LOGW("CHRE API does not support multi-sensors");
674+
} else {
675+
success = chreSensorFind(sensorType, sensorIndex, handle);
676+
}
677+
678+
return success;
679+
}
680+
612681
} // namespace cross_validator_sensor
613682

614683
} // namespace chre

apps/test/common/proto/chre_cross_validation_sensor.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ message StartSensorCommand {
4343
optional uint64 intervalInMs = 2;
4444
optional uint64 latencyInMs = 3;
4545
optional bool isContinuous = 4;
46+
optional uint32 sensorIndex = 5;
4647
}
4748

4849
/*
4950
* Asks for the nanoapp to provide stats about the provided CHRE sensor type.
5051
*/
5152
message SensorInfoCommand {
5253
optional uint32 chreSensorType = 1;
54+
// The sensor name given by android.hardware.Sensor#getSensorName()
55+
optional bytes sensorName = 2;
5356
}
5457

5558
/*
@@ -58,6 +61,7 @@ message SensorInfoCommand {
5861
message SensorInfoResponse {
5962
optional uint32 chreSensorType = 1;
6063
optional bool isAvailable = 2;
64+
optional uint32 sensorIndex = 3;
6165
}
6266

6367
message Data {

chpp/Android.bp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ cc_library_static {
5656
"-DCHPP_SERVICE_ENABLED_TRANSPORT_LOOPBACK",
5757
"-DCHPP_MAX_REGISTERED_CLIENTS=3",
5858
"-DCHPP_MAX_REGISTERED_SERVICES=3",
59+
"-DCHPP_ENABLE_WORK_MONITOR",
5960
"-DCHPP_DEBUG_ASSERT_ENABLED",
6061
// clock_gettime() requires _POSIX_C_SOURCE >= 199309L
6162
"-D_POSIX_C_SOURCE=199309L",
@@ -114,6 +115,7 @@ cc_test_host {
114115
"-DCHPP_CHECKSUM_ENABLED",
115116
"-DCHPP_MAX_REGISTERED_CLIENTS=3",
116117
"-DCHPP_MAX_REGISTERED_SERVICES=3",
118+
"-DCHPP_ENABLE_WORK_MONITOR",
117119
],
118120
srcs: [
119121
"test/app_test_base.cpp",

chpp/clients/wifi.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,20 @@ static void chppWifiRequestScanResult(struct ChppWifiClientState *clientContext,
497497
} else {
498498
struct ChppWifiRequestScanResponseParameters *result =
499499
&((struct ChppWifiRequestScanResponse *)buf)->params;
500+
501+
// TODO(b/193540354): Remove when resolved
502+
{
503+
static uint32_t sNumConsecutiveError = 0;
504+
if (result->errorCode != CHRE_ERROR_NONE) {
505+
sNumConsecutiveError++;
506+
} else {
507+
sNumConsecutiveError = 0;
508+
}
509+
if (sNumConsecutiveError > 20) {
510+
CHPP_ASSERT("Too many consecutive WiFi scan errors");
511+
}
512+
}
513+
500514
CHPP_LOGI("Scan request success=%d (at service)", result->pending);
501515
gCallbacks->scanResponseCallback(result->pending, result->errorCode);
502516
}

chpp/include/chpp/transport.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "chpp/mutex.h"
2828
#include "chpp/notifier.h"
2929
#include "chpp/transport_signals.h"
30+
#include "chpp/work_monitor.h"
3031

3132
#ifdef __cplusplus
3233
extern "C" {
@@ -444,6 +445,10 @@ struct ChppTransportState {
444445
struct ChppConditionVariable
445446
resetCondVar; // Condvar specifically to wait for resetState
446447

448+
#ifdef CHPP_ENABLE_WORK_MONITOR
449+
struct ChppWorkMonitor workMonitor; // Monitor used for the transport thread
450+
#endif
451+
447452
//! This MUST be the last field in the ChppTransportState structure, otherwise
448453
//! chppResetTransportContext() will not work properly.
449454
struct ChppPlatformLinkParameters linkParams; // For corresponding link layer

chpp/include/chpp/work_monitor.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (C) 2021 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+
/*
18+
* Implementation Notes
19+
* Platforms must only supply chpp/platform/platform_work_monitor.h if they
20+
* set the CHPP_ENABLE_WORK_MONITOR macro in their builds.
21+
*/
22+
23+
#ifndef CHPP_WORK_MONITOR_H_
24+
#define CHPP_WORK_MONITOR_H_
25+
26+
#ifdef CHPP_ENABLE_WORK_MONITOR
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/*
33+
* Platform-specific work monitor struct, defined in the platform's
34+
* platform_work_monitor.h file.
35+
*/
36+
struct ChppWorkMonitor;
37+
38+
/*
39+
* Initializes a specified platform-specific work monitor.
40+
*
41+
* @param workMonitor points to the ChppWorkMonitor mutex struct.
42+
*/
43+
static void chppWorkMonitorInit(struct ChppWorkMonitor *workMonitor);
44+
45+
/*
46+
* Deinitializes a specified platform-specific work monitor.
47+
*
48+
* @param workMonitor points to the ChppWorkMonitor mutex struct.
49+
*/
50+
static void chppWorkMonitorDeinit(struct ChppWorkMonitor *workMonitor);
51+
52+
/*
53+
* Called before CHPP starts doing work on the transport thread. This allows
54+
* platforms to start something like a watchdog to ensure the CHPP thread
55+
* is caught if it gets stuck.
56+
*
57+
* @param workMonitor points to the ChppWorkMonitor mutex struct.
58+
*/
59+
static void chppWorkMonitorPreProcess(struct ChppWorkMonitor *workMonitor);
60+
61+
/*
62+
* Called after CHPP finished doing work on the transport thread. This allows
63+
* platforms to stop any watchdog tracking the thread.
64+
*
65+
* @param workMonitor points to the ChppWorkMonitor mutex struct.
66+
*/
67+
static void chppWorkMonitorPostProcess(struct ChppWorkMonitor *workMonitor);
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#include "chpp/platform/platform_work_monitor.h"
74+
75+
#endif // CHPP_ENABLE_WORK_MONITOR
76+
#endif // CHPP_WORK_MONITOR_H_
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2021 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 CHPP_PLATFORM_WORK_MONITOR_H_
18+
#define CHPP_PLATFORM_WORK_MONITOR_H_
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
struct ChppWorkMonitor {
25+
uint32_t numPreProcessCalls;
26+
uint32_t numPostProcessCalls;
27+
};
28+
29+
static inline void chppWorkMonitorInit(struct ChppWorkMonitor *workMonitor) {
30+
workMonitor->numPreProcessCalls = 0;
31+
workMonitor->numPostProcessCalls = 0;
32+
}
33+
34+
static inline void chppWorkMonitorDeinit(struct ChppWorkMonitor *workMonitor) {
35+
UNUSED_VAR(workMonitor);
36+
}
37+
38+
static inline void chppWorkMonitorPreProcess(
39+
struct ChppWorkMonitor *workMonitor) {
40+
++workMonitor->numPreProcessCalls;
41+
}
42+
43+
static inline void chppWorkMonitorPostProcess(
44+
struct ChppWorkMonitor *workMonitor) {
45+
++workMonitor->numPostProcessCalls;
46+
}
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif
51+
52+
#endif // CHPP_PLATFORM_WORK_MONITOR_H_

0 commit comments

Comments
 (0)