xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/headers/KPPPDevice.h (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright 2003-2004, Haiku Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _K_PPP_DEVICE__H
7 #define _K_PPP_DEVICE__H
8 
9 #include <KPPPDefs.h>
10 #include <KPPPLayer.h>
11 
12 #include <net_buffer.h>
13 
14 #ifndef _K_PPP_INTERFACE__H
15 #include <KPPPInterface.h>
16 #endif
17 
18 
19 class KPPPDevice : public KPPPLayer {
20 		friend class KPPPStateMachine;
21 
22 	protected:
23 		// KPPPDevice must be subclassed
24 		KPPPDevice(const char *name, uint32 overhead, KPPPInterface& interface,
25 			driver_parameter *settings);
26 
27 	public:
28 		virtual ~KPPPDevice();
29 
30 		//!	Returns the interface that owns this device.
31 		KPPPInterface& Interface() const
32 			{ return fInterface; }
33 		//!	Returns the device's settings.
34 		driver_parameter *Settings() const
35 			{ return fSettings; }
36 
37 		virtual status_t Control(uint32 op, void *data, size_t length);
38 
39 		//!	Returns the device's MTU.
40 		uint32 MTU() const
41 			{ return fMTU; }
42 
43 		/*!	\brief This brings the device up.
44 
45 			ATTENTION: This method must not block! \n
46 			Call UpStarted() to check if you are allowed to go down. After UpStarted()
47 			is called the connection attempt may be aborted by calling Down(). \n
48 			In server mode you should listen for incoming connections.
49 			On error: \e Either call \c UpFailedEvent() and return \c true \e or
50 			return \c false only. \e Never call \c UpFailedEvent() and return
51 			\c false at the same time!
52 
53 			\sa UpStarted()
54 			\sa UpFailedEvent()
55 		*/
56 		virtual bool Up() = 0;
57 		/*!	\brief Bring the interface down.
58 
59 			Call DownStarted() to check if you are allowed to go down. \n
60 			The return value of this method is currently ignored.
61 
62 			\sa DownStarted()
63 		*/
64 		virtual bool Down() = 0;
65 		//!	Is the connection established?
66 		bool IsUp() const
67 			{ return fConnectionPhase == PPP_ESTABLISHED_PHASE; }
68 		//!	Is the interface disconnected and ready to connect?
69 		bool IsDown() const
70 			{ return fConnectionPhase == PPP_DOWN_PHASE; }
71 		//!	Is the interface connecting at the moment?
72 		bool IsGoingUp() const
73 			{ return fConnectionPhase == PPP_ESTABLISHMENT_PHASE; }
74 		//!	Is the interface disconnecting at the moment?
75 		bool IsGoingDown() const
76 			{ return fConnectionPhase == PPP_TERMINATION_PHASE; }
77 
78 		/*!	\brief Input speed in bytes per second.
79 
80 			The biggest of the two tranfer rates will be set in ifnet. \n
81 			Should return default value when disconnected.
82 		*/
83 		virtual uint32 InputTransferRate() const = 0;
84 		/*!	\brief Output speed in bytes per second.
85 
86 			The biggest of the two tranfer rates will be set in ifnet. \n
87 			Should return default value when disconnected.
88 		*/
89 		virtual uint32 OutputTransferRate() const = 0;
90 
91 		//!	Number of bytes waiting to be sent (i.e.: waiting in output queue).
92 		virtual uint32 CountOutputBytes() const = 0;
93 
94 		virtual bool IsAllowedToSend() const;
95 
96 		/*!	\brief Sends a packet.
97 
98 			This should enqueue the packet and return immediately (never block).
99 			The device is responsible for freeing the packet by calling \c m_freem().
100 		*/
101 		virtual status_t Send(net_buffer *packet, uint16 protocolNumber) = 0;
102 		virtual status_t Receive(net_buffer *packet, uint16 protocolNumber);
103 
104 	protected:
105 		//!	Use this to set the device's maximum transfer unit (in bytes).
106 		void SetMTU(uint32 MTU)
107 			{ fMTU = MTU; }
108 
109 		bool UpStarted();
110 		bool DownStarted();
111 
112 		// report up/down events
113 		void UpFailedEvent();
114 		void UpEvent();
115 		void DownEvent();
116 
117 	protected:
118 		uint32 fMTU;
119 			// always hold this value up-to-date!
120 
121 	private:
122 		char *fName;
123 		KPPPInterface& fInterface;
124 		driver_parameter *fSettings;
125 
126 		ppp_phase fConnectionPhase;
127 };
128 
129 
130 #endif
131