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