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_settings_test.nanopb.h"
22 #include "chre_settings_test_manager.h"
23 
24 #include "chre/util/nanoapp/callbacks.h"
25 #include "chre/util/nanoapp/log.h"
26 
27 #define LOG_TAG "ChreSettingsTest"
28 
29 namespace chre {
30 
31 namespace settings_test {
32 
sendTestResultToHost(uint16_t hostEndpointId,bool success)33 void sendTestResultToHost(uint16_t hostEndpointId, bool success) {
34   // Unspecified endpoint is not allowed in chreSendMessageToHostEndpoint.
35   if (hostEndpointId == CHRE_HOST_ENDPOINT_UNSPECIFIED) {
36     hostEndpointId = CHRE_HOST_ENDPOINT_BROADCAST;
37   }
38 
39   chre_settings_test_TestResult result =
40       chre_settings_test_TestResult_init_default;
41   result.has_code = true;
42   result.code = success ? chre_settings_test_TestResult_Code_PASSED
43                         : chre_settings_test_TestResult_Code_FAILED;
44   size_t size;
45   if (!pb_get_encoded_size(&size, chre_settings_test_TestResult_fields,
46                            &result)) {
47     LOGE("Failed to get message size");
48   } else {
49     pb_byte_t *bytes = static_cast<pb_byte_t *>(chreHeapAlloc(size));
50     if (bytes == nullptr) {
51       LOG_OOM();
52     } else {
53       pb_ostream_t stream = pb_ostream_from_buffer(bytes, size);
54       if (!pb_encode(&stream, chre_settings_test_TestResult_fields, &result)) {
55         LOGE("Failed to encode test result error %s", PB_GET_ERROR(&stream));
56         chreHeapFree(bytes);
57       } else {
58         chreSendMessageToHostEndpoint(
59             bytes, size, chre_settings_test_MessageType_TEST_RESULT,
60             hostEndpointId, heapFreeMessageCallback);
61       }
62     }
63   }
64 }
65 
sendEmptyMessageToHost(uint16_t hostEndpointId,uint32_t messageType)66 void sendEmptyMessageToHost(uint16_t hostEndpointId, uint32_t messageType) {
67   // Unspecified endpoint is not allowed in chreSendMessageToHostEndpoint.
68   if (hostEndpointId == CHRE_HOST_ENDPOINT_UNSPECIFIED) {
69     hostEndpointId = CHRE_HOST_ENDPOINT_BROADCAST;
70   }
71 
72   chreSendMessageToHostEndpoint(nullptr /* message */, 0 /* messageSize */,
73                                 messageType, hostEndpointId,
74                                 nullptr /* freeCallback */);
75 }
76 
77 }  // namespace settings_test
78 
79 }  // namespace chre
80