• 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 "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 
workThread(void * arg)36 void *workThread(void *arg) {
37   ChppTransportState *transportContext = static_cast<ChppTransportState *>(arg);
38   struct ChppLinuxLinkState *linkContext =
39       (struct ChppLinuxLinkState *)(transportContext->linkContext);
40   pthread_setname_np(pthread_self(), linkContext->workThreadName);
41 
42   chppWorkThreadStart(transportContext);
43 
44   return nullptr;
45 }
46 
47 }  // anonymous namespace
48 
SetUp()49 void AppTestBase::SetUp() {
50   chppClearTotalAllocBytes();
51   memset(&mClientLinkContext, 0, sizeof(mClientLinkContext));
52   memset(&mServiceLinkContext, 0, sizeof(mServiceLinkContext));
53   // The linkSendThread in the link layer is a link "to" the remote end.
54   mServiceLinkContext.linkThreadName = "Link to client";
55   mServiceLinkContext.workThreadName = "Service work";
56   mServiceLinkContext.isLinkActive = true;
57   mServiceLinkContext.remoteLinkState = &mClientLinkContext;
58   mServiceLinkContext.rxInRemoteEndpointWorker = false;
59 
60   mClientLinkContext.linkThreadName = "Link to service";
61   mClientLinkContext.workThreadName = "Client work";
62   mClientLinkContext.isLinkActive = true;
63   mClientLinkContext.remoteLinkState = &mServiceLinkContext;
64   mClientLinkContext.rxInRemoteEndpointWorker = false;
65 
66   struct ChppClientServiceSet set;
67   memset(&set, 0, sizeof(set));
68   set.wifiClient = 1;
69   set.gnssClient = 1;
70   set.wwanClient = 1;
71   set.loopbackClient = 1;
72 
73   const struct ChppLinkApi *linkApi = getLinuxLinkApi();
74 
75   chppTransportInit(&mClientTransportContext, &mClientAppContext,
76                     &mClientLinkContext, linkApi);
77   chppAppInitWithClientServiceSet(&mClientAppContext, &mClientTransportContext,
78                                   set);
79   pthread_create(&mClientWorkThread, NULL, workThread,
80                  &mClientTransportContext);
81 
82   // Wait a bit to emulate the scenario where the remote is not yet up
83   std::this_thread::sleep_for(std::chrono::milliseconds(450));
84 
85   memset(&set, 0, sizeof(set));
86   set.wifiService = 1;
87   set.gnssService = 1;
88   set.wwanService = 1;
89 
90   chppTransportInit(&mServiceTransportContext, &mServiceAppContext,
91                     &mServiceLinkContext, linkApi);
92   chppAppInitWithClientServiceSet(&mServiceAppContext,
93                                   &mServiceTransportContext, set);
94   pthread_create(&mServiceWorkThread, NULL, workThread,
95                  &mServiceTransportContext);
96 
97   mClientLinkContext.linkEstablished = true;
98   mServiceLinkContext.linkEstablished = true;
99 
100   constexpr uint64_t kResetWaitTimeMs = 1500;
101   chppTransportWaitForResetComplete(&mClientTransportContext, kResetWaitTimeMs);
102 
103   constexpr uint64_t kDiscoveryWaitTimeMs = 5000;
104   chppWaitForDiscoveryComplete(&mClientAppContext, kDiscoveryWaitTimeMs);
105 }
106 
TearDown()107 void AppTestBase::TearDown() {
108   // Stop the work threads first to avoid any transient activity.
109   chppWorkThreadStop(&mClientTransportContext);
110   chppWorkThreadStop(&mServiceTransportContext);
111   pthread_join(mClientWorkThread, NULL);
112   pthread_join(mServiceWorkThread, NULL);
113 
114   chppAppDeinit(&mClientAppContext);
115   chppTransportDeinit(&mClientTransportContext);
116 
117   chppAppDeinit(&mServiceAppContext);
118   chppTransportDeinit(&mServiceTransportContext);
119 
120   EXPECT_EQ(chppGetTotalAllocBytes(), 0);
121 }
122 
123 }  // namespace chpp
124