• 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 #ifndef CHPP_PLATFORM_LINK_H_
18 #define CHPP_PLATFORM_LINK_H_
19 
20 #include <pthread.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 
24 #include "chpp/mutex.h"
25 #include "chpp/notifier.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define CHPP_LINUX_LINK_TX_MTU_BYTES 1280
32 #define CHPP_LINUX_LINK_RX_MTU_BYTES 1280
33 
34 // Forward declaration
35 struct ChppTransportState;
36 
37 struct ChppLinuxLinkState {
38   //! Indicates that the link to the remote endpoint has been established.
39   //! This simulates the establishment of the physical link, so
40   //! link send() will fail if this field is set to false.
41   bool linkEstablished;
42 
43   //! A pointer to the link context of the remote endpoint.
44   struct ChppLinuxLinkState *remoteLinkState;
45 
46   //! A thread to use when sending data to the remote endpoint asynchronously.
47   pthread_t linkSendThread;
48 
49   //! The notifier for linkSendThread.
50   struct ChppNotifier notifier;
51 
52   //! The notifier to unblock TX thread when RX is complete.
53   struct ChppNotifier rxNotifier;
54 
55   //! The mutex to protect buf/bufLen.
56   struct ChppMutex mutex;
57 
58   //! The buffer to use to send data to the remote endpoint.
59   uint8_t buf[CHPP_LINUX_LINK_TX_MTU_BYTES];
60   size_t bufLen;
61 
62   //! The string name of the linkSendThread.
63   const char *linkThreadName;
64 
65   //! The string name of the CHPP work thread.
66   const char *workThreadName;
67 
68   //! A flag to indicate if the link is active. Setting this value to false
69   //! will cause the CHPP link layer to fail to send/receive messages.
70   bool isLinkActive;
71 
72   //! State of the associated transport layer.
73   struct ChppTransportState *transportContext;
74 
75   //! Run the RX callback (chppRxDataCb) in the context of the remote worker.
76   //! Setting this to true will attribute the logs to the expected worker.
77   //! However this might lead to deadlock situation and is better used for
78   //! debugging only.
79   bool rxInRemoteEndpointWorker;
80 };
81 
82 /**
83  * @return a pointer to the link layer API.
84  */
85 const struct ChppLinkApi *getLinuxLinkApi(void);
86 
87 /**
88  * Waits for chppLinkSendDoneCb to invoked, indicating that a previously
89  * enqueued TX packet has been sent over the link API.
90  *
91  * It is not valid to call this function when no packets are pending.
92  */
93 void waitForLinkSendDone(void);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #endif  // CHPP_PLATFORM_LINK_H_
100