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   //! Whether to wait for cycleSendThread to be called to unblock the send
73   //! thread loop.
74   bool manualSendCycle;
75 
76   //! State of the associated transport layer.
77   struct ChppTransportState *transportContext;
78 
79   //! Run the RX callback (chppRxDataCb) in the context of the remote worker.
80   //! Setting this to true will attribute the logs to the expected worker.
81   //! However this might lead to deadlock situation and is better used for
82   //! debugging only.
83   bool rxInRemoteEndpointWorker;
84 };
85 
86 /**
87  * @return a pointer to the link layer API.
88  */
89 const struct ChppLinkApi *getLinuxLinkApi(void);
90 
91 /**
92  * Starts the send thread loop when manualSendCycle is true.
93  * This function is a noop when manualSendCycle is false.
94  */
95 void cycleSendThread(void);
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif  // CHPP_PLATFORM_LINK_H_
102