aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorStan Rokita <srok@google.com>2020-05-14 16:05:50 -0700
committerStan Rokita <srok@google.com>2020-05-28 17:18:51 -0700
commit66f98b37c2d6042ee164388bb676f1878af00613 (patch)
tree9741c0d311240aa2d59631fc90ff77f3938d85ed /apps
parent083bf171bea85c471b248262ee8897dff128b0c6 (diff)
downloadchre-66f98b37c2d6042ee164388bb676f1878af00613.tar.gz
Implement chre_cross_validator_wifi setup phase
Create a new nanoapp for cross validation for wifi specifically. Implement the setup phase code which configures the nanoapp for wifi scan monitoring and then sends back the confirmation message to the host. Bug: 154271547 Test: New local wifi cross validation chqts test passes which ensures that the setup phase of the cross validator completes without error Change-Id: I310ff8ce3aab054900e73f0ad9839fe9ea28f6e4
Diffstat (limited to 'apps')
-rw-r--r--apps/test/common/chre_cross_validator_wifi/Makefile15
-rw-r--r--apps/test/common/chre_cross_validator_wifi/inc/chre_cross_validator_wifi_manager.h131
-rw-r--r--apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi.cc11
-rw-r--r--apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi_manager.cc187
4 files changed, 342 insertions, 2 deletions
diff --git a/apps/test/common/chre_cross_validator_wifi/Makefile b/apps/test/common/chre_cross_validator_wifi/Makefile
index f262cd13..bfcb8b2a 100644
--- a/apps/test/common/chre_cross_validator_wifi/Makefile
+++ b/apps/test/common/chre_cross_validator_wifi/Makefile
@@ -21,15 +21,30 @@ NANOAPP_VERSION = 0x00000001
NANOAPP_PATH = $(CHRE_PREFIX)/apps/test/common/chre_cross_validator_wifi
+# Protobuf Sources #############################################################
+
+NANOPB_EXTENSION = nanopb
+
+NANOPB_SRCS += $(NANOAPP_PATH)/../proto/chre_cross_validation_wifi.proto
+NANOPB_SRCS += $(NANOAPP_PATH)/../proto/chre_test_common.proto
+NANOPB_PROTO_PATH = $(NANOAPP_PATH)/../proto
+NANOPB_INCLUDES = $(NANOPB_PROTO_PATH)
+NANOPB_FLAGS += --proto_path=$(NANOPB_PROTO_PATH)
+
# Source Code ##################################################################
COMMON_SRCS += $(NANOAPP_PATH)/src/chre_cross_validator_wifi.cc
+COMMON_SRCS += $(NANOAPP_PATH)/src/chre_cross_validator_wifi_manager.cc
+COMMON_SRCS += $(CHRE_PREFIX)/util/nanoapp/callbacks.cc
# Compiler Flags ###############################################################
# Defines
COMMON_CFLAGS += -DNANOAPP_MINIMUM_LOG_LEVEL=CHRE_LOG_LEVEL_INFO
+# Includes
+COMMON_CFLAGS += -I$(NANOAPP_PATH)/inc
+
# Makefile Includes ############################################################
include $(CHRE_PREFIX)/build/nanoapp/app.mk
diff --git a/apps/test/common/chre_cross_validator_wifi/inc/chre_cross_validator_wifi_manager.h b/apps/test/common/chre_cross_validator_wifi/inc/chre_cross_validator_wifi_manager.h
new file mode 100644
index 00000000..39289210
--- /dev/null
+++ b/apps/test/common/chre_cross_validator_wifi/inc/chre_cross_validator_wifi_manager.h
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef CHRE_CROSS_VALIDATOR_WIFI_MANAGER_H_
+#define CHRE_CROSS_VALIDATOR_WIFI_MANAGER_H_
+
+#include <cinttypes>
+
+#include <chre.h>
+#include <pb_common.h>
+#include <pb_decode.h>
+#include <pb_encode.h>
+
+#include "chre/util/singleton.h"
+
+#include "chre_cross_validation_wifi.nanopb.h"
+#include "chre_test_common.nanopb.h"
+
+namespace chre {
+
+namespace cross_validator_wifi {
+
+/**
+ * Class to manage a CHRE cross validator wifi nanoapp.
+ */
+class Manager {
+ public:
+ /**
+ * Handle a CHRE event.
+ *
+ * @param senderInstanceId The instand ID that sent the event.
+ * @param eventType The type of the event.
+ * @param eventData The data for the event.
+ */
+ void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
+ const void *eventData);
+
+ private:
+ struct CrossValidatorState {
+ uint16_t hostEndpoint;
+ };
+
+ chre_cross_validation_wifi_Step mStep = chre_cross_validation_wifi_Step_INIT;
+
+ //! Struct that holds some information about the state of the cross validator
+ CrossValidatorState mCrossValidatorState;
+
+ /**
+ * Handle a message from the host.
+ * @param senderInstanceId The instance id of the sender.
+ * @param data The message from the host's data.
+ */
+ void handleMessageFromHost(uint32_t senderInstanceId,
+ const chreMessageFromHostData *data);
+
+ /**
+ * Handle a step start message from the host.
+ *
+ * @param stepStartCommand The step start command proto.
+ */
+ void handleStepStartMessage(
+ chre_cross_validation_wifi_StepStartCommand stepStartCommand);
+
+ /**
+ * @param success true if the result was success.
+ * @param errMessage The error message that should be sent to host with
+ * failure.
+ *
+ * @return The TestResult proto message that is encoded with these fields.
+ */
+ chre_test_common_TestResult makeTestResultProtoMessage(
+ bool success, const char *errMessage = nullptr);
+
+ /**
+ * Encode the proto message and send to host.
+ *
+ * @param message The proto message struct pointer.
+ * @fields The fields descriptor of the proto message to encode.
+ */
+ void encodeAndSendMessageToHost(const void *message,
+ const pb_field_t *fields);
+
+ /**
+ * Setup WiFi scan monitoring from CHRE apis.
+ *
+ * @return true if chreWifiConfigureScanMonitorAsync() returns true
+ */
+ bool setupWifiScanMonitoring();
+
+ /**
+ * Handle wifi async result event with event data.
+ *
+ * @param result The data for the event.
+ */
+ void handleWifiAsyncResult(const chreAsyncResult *result);
+
+ /**
+ * The function to pass as the encode function pointer for the errorMessage
+ * field of the TestResult message.
+ *
+ * @param stream The stream to write bytes to.
+ * @param field The field that should be encoded. Unused by us.
+ * @param arg The argument that will be set to a pointer to the string to
+ * encode as error message.
+ */
+ static bool encodeErrorMessage(pb_ostream_t *stream,
+ const pb_field_t * /*field*/,
+ void *const *arg);
+};
+
+// The chre cross validator manager singleton.
+typedef chre::Singleton<Manager> ManagerSingleton;
+
+} // namespace cross_validator_wifi
+
+} // namespace chre
+
+#endif // CHRE_CROSS_VALIDATOR_WIFI_MANAGER_H_
diff --git a/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi.cc b/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi.cc
index 9edb7213..abf2dce9 100644
--- a/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi.cc
+++ b/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi.cc
@@ -16,16 +16,23 @@
#include <chre.h>
#include <cinttypes>
+#include "chre_cross_validator_wifi_manager.h"
namespace chre {
extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
- uint16_t eventType, const void *eventData) {}
+ uint16_t eventType, const void *eventData) {
+ cross_validator_wifi::ManagerSingleton::get()->handleEvent(
+ senderInstanceId, eventType, eventData);
+}
extern "C" bool nanoappStart(void) {
+ cross_validator_wifi::ManagerSingleton::init();
return true;
}
-extern "C" void nanoappEnd(void) {}
+extern "C" void nanoappEnd(void) {
+ cross_validator_wifi::ManagerSingleton::deinit();
+}
} // namespace chre
diff --git a/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi_manager.cc b/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi_manager.cc
new file mode 100644
index 00000000..7bc82818
--- /dev/null
+++ b/apps/test/common/chre_cross_validator_wifi/src/chre_cross_validator_wifi_manager.cc
@@ -0,0 +1,187 @@
+/* * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "chre_cross_validator_wifi_manager.h"
+
+#include <stdio.h>
+#include <cinttypes>
+#include <cstring>
+
+#include "chre/util/nanoapp/callbacks.h"
+#include "chre/util/nanoapp/log.h"
+#include "chre_cross_validation_wifi.nanopb.h"
+#include "chre_test_common.nanopb.h"
+
+#define LOG_TAG "ChreCrossValidatorWifi"
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+namespace chre {
+
+namespace cross_validator_wifi {
+
+// Fake scan monitor cookie which is not used
+constexpr uint32_t kScanMonitoringCookie = 0;
+
+void Manager::handleEvent(uint32_t senderInstanceId, uint16_t eventType,
+ const void *eventData) {
+ switch (eventType) {
+ case CHRE_EVENT_MESSAGE_FROM_HOST:
+ handleMessageFromHost(
+ senderInstanceId,
+ static_cast<const chreMessageFromHostData *>(eventData));
+ break;
+ case CHRE_EVENT_WIFI_ASYNC_RESULT:
+ handleWifiAsyncResult(static_cast<const chreAsyncResult *>(eventData));
+ default:
+ LOGE("Unknown message type %" PRIu16 "received when handling event",
+ eventType);
+ }
+}
+
+void Manager::handleMessageFromHost(uint32_t senderInstanceId,
+ const chreMessageFromHostData *hostData) {
+ if (senderInstanceId != CHRE_INSTANCE_ID) {
+ LOGE("Incorrect sender instance id: %" PRIu32, senderInstanceId);
+ } else {
+ mCrossValidatorState.hostEndpoint = hostData->hostEndpoint;
+ switch (hostData->messageType) {
+ case chre_cross_validation_wifi_MessageType_STEP_START: {
+ pb_istream_t stream = pb_istream_from_buffer(
+ static_cast<const pb_byte_t *>(
+ const_cast<const void *>(hostData->message)),
+ hostData->messageSize);
+ chre_cross_validation_wifi_StepStartCommand stepStartCommand;
+ if (!pb_decode(&stream,
+ chre_cross_validation_wifi_StepStartCommand_fields,
+ &stepStartCommand)) {
+ LOGE("Error decoding StepStartCommand");
+ } else {
+ handleStepStartMessage(stepStartCommand);
+ }
+ break;
+ }
+ default:
+ LOGE("Unknown message type %" PRIu32 " for host message",
+ hostData->messageType);
+ }
+ }
+}
+
+void Manager::handleStepStartMessage(
+ chre_cross_validation_wifi_StepStartCommand stepStartCommand) {
+ chre_test_common_TestResult testResult;
+ switch (stepStartCommand.step) {
+ case chre_cross_validation_wifi_Step_INIT:
+ testResult = makeTestResultProtoMessage(
+ false, "Received StepStartCommand for INIT step");
+ break;
+ case chre_cross_validation_wifi_Step_SETUP:
+ if (!chreWifiConfigureScanMonitorAsync(true /* enable */,
+ &kScanMonitoringCookie)) {
+ LOGE("chreWifiConfigureScanMonitorAsync() failed");
+ testResult =
+ makeTestResultProtoMessage(false, "setupWifiScanMonitoring failed");
+ encodeAndSendMessageToHost(static_cast<void *>(&testResult),
+ chre_test_common_TestResult_fields);
+ } else {
+ LOGD("chreWifiConfigureScanMonitorAsync() succeeded");
+ }
+ break;
+ case chre_cross_validation_wifi_Step_VALIDATE:
+ break;
+ }
+ mStep = stepStartCommand.step;
+}
+
+bool Manager::encodeErrorMessage(pb_ostream_t *stream,
+ const pb_field_t * /*field*/,
+ void *const *arg) {
+ const char *str = static_cast<const char *>(const_cast<const void *>(*arg));
+ size_t len = strlen(str);
+ return pb_encode_string(stream, reinterpret_cast<const pb_byte_t *>(str),
+ len);
+}
+
+chre_test_common_TestResult Manager::makeTestResultProtoMessage(
+ bool success, const char *errMessage) {
+ // TODO(b/154271547): Move this implementation into
+ // common/shared/send_message.cc::sendTestResultToHost
+ chre_test_common_TestResult testResult =
+ chre_test_common_TestResult_init_default;
+ testResult.has_code = true;
+ testResult.code = success ? chre_test_common_TestResult_Code_PASSED
+ : chre_test_common_TestResult_Code_FAILED;
+ if (!success && errMessage != nullptr) {
+ testResult.errorMessage = {.funcs = {.encode = encodeErrorMessage},
+ .arg = const_cast<char *>(errMessage)};
+ }
+ return testResult;
+}
+
+void Manager::encodeAndSendMessageToHost(const void *message,
+ const pb_field_t *fields) {
+ size_t encodedSize;
+ if (!pb_get_encoded_size(&encodedSize, fields, message)) {
+ LOGE("Could not get encoded size of test result message");
+ } else {
+ pb_byte_t *buffer = static_cast<pb_byte_t *>(chreHeapAlloc(encodedSize));
+ if (buffer == nullptr) {
+ LOG_OOM();
+ } else {
+ pb_ostream_t ostream = pb_ostream_from_buffer(buffer, encodedSize);
+ if (!pb_encode(&ostream, fields, message)) {
+ LOGE("Could not encode data proto message");
+ } else if (!chreSendMessageToHostEndpoint(
+ static_cast<void *>(buffer), encodedSize,
+ chre_cross_validation_wifi_MessageType_STEP_RESULT,
+ mCrossValidatorState.hostEndpoint,
+ heapFreeMessageCallback)) {
+ LOGE("Could not send message to host");
+ }
+ }
+ }
+}
+
+void Manager::handleWifiAsyncResult(const chreAsyncResult *result) {
+ LOGI("handleWifiAsyncResult method");
+ chre_test_common_TestResult testResult;
+ if (result->requestType == CHRE_WIFI_REQUEST_TYPE_CONFIGURE_SCAN_MONITOR) {
+ if (mStep != chre_cross_validation_wifi_Step_SETUP) {
+ testResult = makeTestResultProtoMessage(
+ false, "Received scan monitor result event when step is not SETUP");
+ } else {
+ if (result->success) {
+ LOGD("Wifi scan monitoring setup successfully");
+ testResult = makeTestResultProtoMessage(true);
+ } else {
+ LOGE("Wifi scan monitoring setup failed async w/ error code %" PRIu8
+ ".",
+ result->errorCode);
+ testResult = makeTestResultProtoMessage(
+ false, "Wifi scan monitoring setup failed async.");
+ }
+ encodeAndSendMessageToHost(static_cast<void *>(&testResult),
+ chre_test_common_TestResult_fields);
+ }
+ } else {
+ testResult = makeTestResultProtoMessage(
+ false, "Unknown chre async result type received");
+ }
+}
+
+} // namespace cross_validator_wifi
+
+} // namespace chre