1 /*
2 * Copyright (C) 2020 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 #include <chre.h>
18 #include <pb_encode.h>
19 #include <cinttypes>
20
21 #include "chre_test_common.nanopb.h"
22
23 #include "chre/util/nanoapp/callbacks.h"
24 #include "chre/util/nanoapp/log.h"
25
26 #define LOG_TAG "[TestShared]"
27
28 namespace chre {
29 namespace test_shared {
30 namespace {
31
encodeErrorMessage(pb_ostream_t * stream,const pb_field_t *,void * const * arg)32 bool encodeErrorMessage(pb_ostream_t *stream, const pb_field_t * /*field*/,
33 void *const *arg) {
34 const char *str = static_cast<const char *>(const_cast<const void *>(*arg));
35 size_t len = strlen(str);
36 return pb_encode_tag_for_field(
37 stream, &chre_test_common_TestResult_fields
38 [chre_test_common_TestResult_errorMessage_tag - 1]) &&
39 pb_encode_string(stream, reinterpret_cast<const pb_byte_t *>(str),
40 len);
41 }
42
43 } // namespace
44
sendTestResultWithMsgToHost(uint16_t hostEndpointId,uint32_t messageType,bool success,const char * errMessage)45 void sendTestResultWithMsgToHost(uint16_t hostEndpointId, uint32_t messageType,
46 bool success, const char *errMessage) {
47 // Unspecified endpoint is not allowed in chreSendMessageToHostEndpoint.
48 if (hostEndpointId == CHRE_HOST_ENDPOINT_UNSPECIFIED) {
49 hostEndpointId = CHRE_HOST_ENDPOINT_BROADCAST;
50 LOGE("Unspecified endpoint ID is not allowed");
51 success = false;
52 }
53
54 chre_test_common_TestResult result = chre_test_common_TestResult_init_default;
55 result.has_code = true;
56 result.code = success ? chre_test_common_TestResult_Code_PASSED
57 : chre_test_common_TestResult_Code_FAILED;
58 if (!success && errMessage != nullptr) {
59 result.errorMessage = {.funcs = {.encode = encodeErrorMessage},
60 .arg = const_cast<char *>(errMessage)};
61 LOGE("%s", errMessage);
62 }
63 size_t size;
64 if (!pb_get_encoded_size(&size, chre_test_common_TestResult_fields,
65 &result)) {
66 LOGE("Failed to get message size");
67 } else {
68 pb_byte_t *bytes = static_cast<pb_byte_t *>(chreHeapAlloc(size));
69 if (bytes == nullptr) {
70 LOG_OOM();
71 } else {
72 pb_ostream_t stream = pb_ostream_from_buffer(bytes, size);
73 if (!pb_encode(&stream, chre_test_common_TestResult_fields, &result)) {
74 LOGE("Failed to encode test result error %s", PB_GET_ERROR(&stream));
75 chreHeapFree(bytes);
76 } else {
77 chreSendMessageToHostEndpoint(bytes, size, messageType, hostEndpointId,
78 heapFreeMessageCallback);
79 }
80 }
81 }
82
83 // Abort to ensure test does not continue
84 if (!success) {
85 chreAbort(0);
86 }
87 }
88
sendTestResultToHost(uint16_t hostEndpointId,uint32_t messageType,bool success)89 void sendTestResultToHost(uint16_t hostEndpointId, uint32_t messageType,
90 bool success) {
91 sendTestResultWithMsgToHost(hostEndpointId, messageType, success,
92 nullptr /* errMessage */);
93 }
94
sendEmptyMessageToHost(uint16_t hostEndpointId,uint32_t messageType)95 void sendEmptyMessageToHost(uint16_t hostEndpointId, uint32_t messageType) {
96 // Unspecified endpoint is not allowed in chreSendMessageToHostEndpoint.
97 if (hostEndpointId == CHRE_HOST_ENDPOINT_UNSPECIFIED) {
98 hostEndpointId = CHRE_HOST_ENDPOINT_BROADCAST;
99 LOGE("Unspecified endpoint ID is not allowed");
100 // TODO: Send failure message to host
101 return;
102 }
103
104 chreSendMessageToHostEndpoint(nullptr /* message */, 0 /* messageSize */,
105 messageType, hostEndpointId,
106 nullptr /* freeCallback */);
107 }
108
109 } // namespace test_shared
110
111 } // namespace chre
112