1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker *
4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker *
8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker *
10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker */
16*84e33947SAndroid Build Coastguard Worker
17*84e33947SAndroid Build Coastguard Worker #include "app_test_base.h"
18*84e33947SAndroid Build Coastguard Worker
19*84e33947SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*84e33947SAndroid Build Coastguard Worker
21*84e33947SAndroid Build Coastguard Worker #include <stdbool.h>
22*84e33947SAndroid Build Coastguard Worker #include <stddef.h>
23*84e33947SAndroid Build Coastguard Worker #include <stdint.h>
24*84e33947SAndroid Build Coastguard Worker #include <string.h>
25*84e33947SAndroid Build Coastguard Worker #include <thread>
26*84e33947SAndroid Build Coastguard Worker
27*84e33947SAndroid Build Coastguard Worker #include "chpp/app.h"
28*84e33947SAndroid Build Coastguard Worker #include "chpp/clients/discovery.h"
29*84e33947SAndroid Build Coastguard Worker #include "chpp/macros.h"
30*84e33947SAndroid Build Coastguard Worker #include "chpp/platform/utils.h"
31*84e33947SAndroid Build Coastguard Worker #include "chpp/transport.h"
32*84e33947SAndroid Build Coastguard Worker
33*84e33947SAndroid Build Coastguard Worker namespace chpp {
34*84e33947SAndroid Build Coastguard Worker namespace {
35*84e33947SAndroid Build Coastguard Worker
workThread(void * arg)36*84e33947SAndroid Build Coastguard Worker void *workThread(void *arg) {
37*84e33947SAndroid Build Coastguard Worker ChppTransportState *transportContext = static_cast<ChppTransportState *>(arg);
38*84e33947SAndroid Build Coastguard Worker struct ChppLinuxLinkState *linkContext =
39*84e33947SAndroid Build Coastguard Worker (struct ChppLinuxLinkState *)(transportContext->linkContext);
40*84e33947SAndroid Build Coastguard Worker pthread_setname_np(pthread_self(), linkContext->workThreadName);
41*84e33947SAndroid Build Coastguard Worker
42*84e33947SAndroid Build Coastguard Worker chppWorkThreadStart(transportContext);
43*84e33947SAndroid Build Coastguard Worker
44*84e33947SAndroid Build Coastguard Worker return nullptr;
45*84e33947SAndroid Build Coastguard Worker }
46*84e33947SAndroid Build Coastguard Worker
47*84e33947SAndroid Build Coastguard Worker } // anonymous namespace
48*84e33947SAndroid Build Coastguard Worker
SetUp()49*84e33947SAndroid Build Coastguard Worker void AppTestBase::SetUp() {
50*84e33947SAndroid Build Coastguard Worker chppClearTotalAllocBytes();
51*84e33947SAndroid Build Coastguard Worker memset(&mClientLinkContext, 0, sizeof(mClientLinkContext));
52*84e33947SAndroid Build Coastguard Worker memset(&mServiceLinkContext, 0, sizeof(mServiceLinkContext));
53*84e33947SAndroid Build Coastguard Worker // The linkSendThread in the link layer is a link "to" the remote end.
54*84e33947SAndroid Build Coastguard Worker mServiceLinkContext.linkThreadName = "Link to client";
55*84e33947SAndroid Build Coastguard Worker mServiceLinkContext.workThreadName = "Service work";
56*84e33947SAndroid Build Coastguard Worker mServiceLinkContext.isLinkActive = true;
57*84e33947SAndroid Build Coastguard Worker mServiceLinkContext.remoteLinkState = &mClientLinkContext;
58*84e33947SAndroid Build Coastguard Worker mServiceLinkContext.rxInRemoteEndpointWorker = false;
59*84e33947SAndroid Build Coastguard Worker
60*84e33947SAndroid Build Coastguard Worker mClientLinkContext.linkThreadName = "Link to service";
61*84e33947SAndroid Build Coastguard Worker mClientLinkContext.workThreadName = "Client work";
62*84e33947SAndroid Build Coastguard Worker mClientLinkContext.isLinkActive = true;
63*84e33947SAndroid Build Coastguard Worker mClientLinkContext.remoteLinkState = &mServiceLinkContext;
64*84e33947SAndroid Build Coastguard Worker mClientLinkContext.rxInRemoteEndpointWorker = false;
65*84e33947SAndroid Build Coastguard Worker
66*84e33947SAndroid Build Coastguard Worker struct ChppClientServiceSet set;
67*84e33947SAndroid Build Coastguard Worker memset(&set, 0, sizeof(set));
68*84e33947SAndroid Build Coastguard Worker set.wifiClient = 1;
69*84e33947SAndroid Build Coastguard Worker set.gnssClient = 1;
70*84e33947SAndroid Build Coastguard Worker set.wwanClient = 1;
71*84e33947SAndroid Build Coastguard Worker set.loopbackClient = 1;
72*84e33947SAndroid Build Coastguard Worker
73*84e33947SAndroid Build Coastguard Worker const struct ChppLinkApi *linkApi = getLinuxLinkApi();
74*84e33947SAndroid Build Coastguard Worker
75*84e33947SAndroid Build Coastguard Worker chppTransportInit(&mClientTransportContext, &mClientAppContext,
76*84e33947SAndroid Build Coastguard Worker &mClientLinkContext, linkApi);
77*84e33947SAndroid Build Coastguard Worker chppAppInitWithClientServiceSet(&mClientAppContext, &mClientTransportContext,
78*84e33947SAndroid Build Coastguard Worker set);
79*84e33947SAndroid Build Coastguard Worker pthread_create(&mClientWorkThread, NULL, workThread,
80*84e33947SAndroid Build Coastguard Worker &mClientTransportContext);
81*84e33947SAndroid Build Coastguard Worker
82*84e33947SAndroid Build Coastguard Worker // Wait a bit to emulate the scenario where the remote is not yet up
83*84e33947SAndroid Build Coastguard Worker std::this_thread::sleep_for(std::chrono::milliseconds(450));
84*84e33947SAndroid Build Coastguard Worker
85*84e33947SAndroid Build Coastguard Worker memset(&set, 0, sizeof(set));
86*84e33947SAndroid Build Coastguard Worker set.wifiService = 1;
87*84e33947SAndroid Build Coastguard Worker set.gnssService = 1;
88*84e33947SAndroid Build Coastguard Worker set.wwanService = 1;
89*84e33947SAndroid Build Coastguard Worker
90*84e33947SAndroid Build Coastguard Worker chppTransportInit(&mServiceTransportContext, &mServiceAppContext,
91*84e33947SAndroid Build Coastguard Worker &mServiceLinkContext, linkApi);
92*84e33947SAndroid Build Coastguard Worker chppAppInitWithClientServiceSet(&mServiceAppContext,
93*84e33947SAndroid Build Coastguard Worker &mServiceTransportContext, set);
94*84e33947SAndroid Build Coastguard Worker pthread_create(&mServiceWorkThread, NULL, workThread,
95*84e33947SAndroid Build Coastguard Worker &mServiceTransportContext);
96*84e33947SAndroid Build Coastguard Worker
97*84e33947SAndroid Build Coastguard Worker mClientLinkContext.linkEstablished = true;
98*84e33947SAndroid Build Coastguard Worker mServiceLinkContext.linkEstablished = true;
99*84e33947SAndroid Build Coastguard Worker
100*84e33947SAndroid Build Coastguard Worker constexpr uint64_t kResetWaitTimeMs = 5000;
101*84e33947SAndroid Build Coastguard Worker EXPECT_TRUE(chppTransportWaitForResetComplete(&mClientTransportContext,
102*84e33947SAndroid Build Coastguard Worker kResetWaitTimeMs));
103*84e33947SAndroid Build Coastguard Worker EXPECT_TRUE(chppTransportWaitForResetComplete(&mServiceTransportContext,
104*84e33947SAndroid Build Coastguard Worker kResetWaitTimeMs));
105*84e33947SAndroid Build Coastguard Worker
106*84e33947SAndroid Build Coastguard Worker constexpr uint64_t kDiscoveryWaitTimeMs = 5000;
107*84e33947SAndroid Build Coastguard Worker EXPECT_TRUE(
108*84e33947SAndroid Build Coastguard Worker chppWaitForDiscoveryComplete(&mClientAppContext, kDiscoveryWaitTimeMs));
109*84e33947SAndroid Build Coastguard Worker EXPECT_TRUE(
110*84e33947SAndroid Build Coastguard Worker chppWaitForDiscoveryComplete(&mServiceAppContext, kDiscoveryWaitTimeMs));
111*84e33947SAndroid Build Coastguard Worker }
112*84e33947SAndroid Build Coastguard Worker
TearDown()113*84e33947SAndroid Build Coastguard Worker void AppTestBase::TearDown() {
114*84e33947SAndroid Build Coastguard Worker // Stop the work threads first to avoid any transient activity.
115*84e33947SAndroid Build Coastguard Worker chppWorkThreadStop(&mClientTransportContext);
116*84e33947SAndroid Build Coastguard Worker chppWorkThreadStop(&mServiceTransportContext);
117*84e33947SAndroid Build Coastguard Worker pthread_join(mClientWorkThread, NULL);
118*84e33947SAndroid Build Coastguard Worker pthread_join(mServiceWorkThread, NULL);
119*84e33947SAndroid Build Coastguard Worker
120*84e33947SAndroid Build Coastguard Worker chppAppDeinit(&mClientAppContext);
121*84e33947SAndroid Build Coastguard Worker chppTransportDeinit(&mClientTransportContext);
122*84e33947SAndroid Build Coastguard Worker
123*84e33947SAndroid Build Coastguard Worker chppAppDeinit(&mServiceAppContext);
124*84e33947SAndroid Build Coastguard Worker chppTransportDeinit(&mServiceTransportContext);
125*84e33947SAndroid Build Coastguard Worker
126*84e33947SAndroid Build Coastguard Worker EXPECT_EQ(chppGetTotalAllocBytes(), 0);
127*84e33947SAndroid Build Coastguard Worker }
128*84e33947SAndroid Build Coastguard Worker
129*84e33947SAndroid Build Coastguard Worker } // namespace chpp
130