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 "app_test_base.h" 18 19 #include <gtest/gtest.h> 20 21 #include <stdbool.h> 22 #include <stddef.h> 23 #include <stdint.h> 24 #include <string.h> 25 #include <thread> 26 27 #include "chpp/app.h" 28 #include "chpp/clients/discovery.h" 29 #include "chpp/macros.h" 30 #include "chpp/platform/utils.h" 31 #include "chpp/transport.h" 32 33 namespace chpp { 34 namespace { 35 36 void *workThread(void *arg) { 37 ChppTransportState *context = static_cast<ChppTransportState *>(arg); 38 pthread_setname_np(pthread_self(), context->linkParams.workThreadName); 39 40 chppWorkThreadStart(context); 41 42 return nullptr; 43 } 44 45 } // anonymous namespace 46 47 void AppTestBase::SetUp() { 48 chppClearTotalAllocBytes(); 49 memset(&mClientTransportContext.linkParams, 0, 50 sizeof(mClientTransportContext.linkParams)); 51 memset(&mServiceTransportContext.linkParams, 0, 52 sizeof(mServiceTransportContext.linkParams)); 53 // The linkSendThread in the link layer is a link "to" the remote end. 54 mServiceTransportContext.linkParams.linkThreadName = "Link to client"; 55 mServiceTransportContext.linkParams.workThreadName = "Service work"; 56 mClientTransportContext.linkParams.linkThreadName = "Link to service"; 57 mClientTransportContext.linkParams.workThreadName = "Client work"; 58 59 mClientTransportContext.linkParams.remoteTransportContext = 60 &mServiceTransportContext; 61 mServiceTransportContext.linkParams.remoteTransportContext = 62 &mClientTransportContext; 63 64 struct ChppClientServiceSet set; 65 memset(&set, 0, sizeof(set)); 66 set.wifiClient = 1; 67 set.gnssClient = 1; 68 set.wwanClient = 1; 69 set.loopbackClient = 1; 70 71 chppTransportInit(&mClientTransportContext, &mClientAppContext); 72 chppAppInitWithClientServiceSet(&mClientAppContext, &mClientTransportContext, 73 set); 74 pthread_create(&mClientWorkThread, NULL, workThread, 75 &mClientTransportContext); 76 77 // Wait a bit to emulate the scenario where the remote is not yet up 78 std::this_thread::sleep_for(std::chrono::milliseconds(450)); 79 80 memset(&set, 0, sizeof(set)); 81 set.wifiService = 1; 82 set.gnssService = 1; 83 set.wwanService = 1; 84 85 chppTransportInit(&mServiceTransportContext, &mServiceAppContext); 86 chppAppInitWithClientServiceSet(&mServiceAppContext, 87 &mServiceTransportContext, set); 88 pthread_create(&mServiceWorkThread, NULL, workThread, 89 &mServiceTransportContext); 90 91 mClientTransportContext.linkParams.linkEstablished = true; 92 mServiceTransportContext.linkParams.linkEstablished = true; 93 94 constexpr uint64_t kResetWaitTimeMs = 1500; 95 chppTransportWaitForResetComplete(&mClientTransportContext, kResetWaitTimeMs); 96 97 constexpr uint64_t kDiscoveryWaitTimeMs = 5000; 98 chppWaitForDiscoveryComplete(&mClientAppContext, kDiscoveryWaitTimeMs); 99 } 100 101 void AppTestBase::TearDown() { 102 // Stop the work threads first to avoid any transient activity. 103 chppWorkThreadStop(&mClientTransportContext); 104 chppWorkThreadStop(&mServiceTransportContext); 105 pthread_join(mClientWorkThread, NULL); 106 pthread_join(mServiceWorkThread, NULL); 107 108 chppAppDeinit(&mClientAppContext); 109 chppTransportDeinit(&mClientTransportContext); 110 111 chppAppDeinit(&mServiceAppContext); 112 chppTransportDeinit(&mServiceTransportContext); 113 114 EXPECT_EQ(chppGetTotalAllocBytes(), 0); 115 } 116 117 } // namespace chpp 118