Mistake on this page?
Report an issue in GitHub or email us
ppp_service.h
1 /*
2  * Copyright (c) 2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef PPP_SERVICE_H
19 #define PPP_SERVICE_H
20 
21 #include <stdbool.h>
22 #include "Callback.h"
23 #include "Semaphore.h"
24 #include "NetStackMemoryManager.h"
25 #include "FileHandle.h"
26 #include "events/EventQueue.h"
27 #include "netsocket/PPP.h"
28 
29 /**
30  * This interface should be used to abstract low level access to networking hardware
31  * All operations receive a `void *` hardware pointer which an ppp device provides when
32  * it is registered with a stack.
33  */
34 class ppp_service : public PPP {
35 public:
36  ppp_service();
37 
38  static ppp_service &get_instance();
39 
40  /**
41  * Callback to be registered with PPP interface and to be called for received packets
42  *
43  * @param buf Received data
44  */
45  //typedef void (*ppp_link_input_fn)(void *data, net_stack_mem_buf_t *buf);
47 
48  /**
49  * Callback to be registered with PPP interface and to be called for link status changes
50  *
51  * @param up Link status
52  */
53  //typedef void (*ppp_link_state_change_fn)(void *data, bool up);
55 
56  /**
57  * Return maximum transmission unit
58  *
59  * @return MTU in bytes
60  */
61  virtual uint32_t get_mtu_size();
62 
63  /**
64  * Gets memory buffer alignment preference
65  *
66  * Gets preferred memory buffer alignment of the ppp device.
67  * @return Memory alignment requirement in bytes
68  */
69  virtual uint32_t get_align_preference() const;
70 
71  /**
72  * Return interface name
73  *
74  * @param name Pointer to where the name should be written
75  * @param size Maximum number of characters to copy
76  */
77  virtual void get_ifname(char *name, uint8_t size) const;
78 
79  /**
80  * Sends the packet over the link
81  *
82  * That cannot be called from an interrupt context.
83  *
84  * @param buf Packet to be send
85  * @return True if the packet was send successfully, false otherwise
86  */
87  virtual bool link_out(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack);
88 
89  /**
90  * Initializes the hardware
91  *
92  * @return True on success, False in case of an error.
93  */
94  virtual bool power_up();
95  /**
96  * Deinitializes the hardware
97  *
98  */
99  virtual void power_down();
100 
101  /**
102  * Sets a callback that needs to be called for packets received for that interface
103  *
104  * @param input_cb Function to be register as a callback
105  */
106  virtual void set_link_input_cb(ppp_link_input_cb_t input_cb);
107 
108  /**
109  * Sets a callback that needs to be called on link status changes for given interface
110  *
111  * @param state_cb Function to be register as a callback
112  */
113  virtual void set_link_state_cb(ppp_link_state_change_cb_t state_cb);
114 
115  /** Sets memory manager that is used to handle memory buffers
116  *
117  * @param mem_mngr Pointer to memory manager
118  */
119  virtual void set_memory_manager(NetStackMemoryManager &mem_mngr);
120 
121  /** Sets file stream used to communicate with modem
122  *
123  * @param stream Pointer to file handle
124  */
125  virtual void set_stream(mbed::FileHandle *stream);
126 
127  /** Sets IP protocol versions of IP stack
128  *
129  * @param ip_stack IP protocol version
130  */
131  virtual void set_ip_stack(nsapi_ip_stack_t ip_stack);
132 
133  /** Sets user name and password for PPP protocol
134  *
135  * @param uname User name
136  * @param password Password
137  */
138  virtual void set_credentials(const char *uname, const char *password);
139 
140  /** Gets local IP address
141  *
142  * @param version IP address version
143  * @return IP address
144  */
145  virtual const nsapi_addr_t *get_ip_address(nsapi_version_t version);
146 
147  /** Get the local network mask.
148  *
149  * @return Local network mask or null if no network mask has been received.
150  */
151  virtual const nsapi_addr_t *get_netmask();
152 
153  /** Get the local gateway.
154  *
155  * @return Local gateway or null if no network mask has been received.
156  */
157  virtual const nsapi_addr_t *get_gateway();
158 
159  /** Gets DNS server address
160  *
161  * @param index Server index
162  */
163  virtual const nsapi_addr_t *get_dns_server(uint8_t index);
164 
165  /** Link state indication from PPP
166  *
167  * @param up Link status
168  */
169  virtual void link_state(bool up);
170 
171  /** Received IP packet from PPP to stack
172  *
173  * @param buf Received packet
174  */
175  virtual void link_input(net_stack_mem_buf_t *buf);
176 
177  /** Handle to PPP event queue
178  *
179  * @return Event queue
180  */
182 
183  /** Lock PPP resource
184  *
185  */
186  virtual void resource_lock();
187 
188  /** Unlock PPP resource
189  *
190  */
191  virtual void resource_unlock();
192 
193 private:
194 
195  // PPP library interface to service
196  bool prepare_event_queue();
197  void ppp_input();
198  void ppp_stream_sigio_callback();
199  void ppp_handle_modem_hangup();
200  static void ppp_link_status(struct ppp_pcb_s *pcb, int err_code, void *ctx);
201  void power_up_call();
202  void link_state_call(bool up);
203 
204  // PPP status functions
205  nsapi_error_t ppp_stack_if_init();
206  nsapi_error_t ppp_if_connect();
207  nsapi_error_t ppp_if_disconnect();
208 
209  // Internal data
210  mbed::FileHandle *ppp_service_stream;
211  rtos::Semaphore ppp_service_close_sem;
212  rtos::Mutex ppp_service_mutex;
213  events::EventQueue *ppp_service_event_queue;
214  NetStackMemoryManager *memory_manager; /**< Memory manager */
215  ppp_link_input_cb_t ppp_link_input_cb; /**< Callback for incoming data */
216  ppp_link_state_change_cb_t ppp_link_state_cb; /**< Link state change callback */
217 
218  struct netif *ppp_service_netif;
219  void *ppp_service_pcb;
220  nsapi_ip_stack_t ppp_service_stack;
221  const char *ppp_service_uname;
222  const char *ppp_service_password;
223  nsapi_error_t ppp_service_connect_error_code;
224  bool ppp_service_active : 1;
225  bool ppp_service_event_queued : 1;
226  bool ppp_service_terminating : 1;
227  bool ppp_link_state_up : 1;
228 };
229 
230 #endif /* PPP_SERVICE_H */
mbed::Callback< void(net_stack_mem_buf_t *buf)> ppp_link_input_cb_t
Callback to be registered with PPP interface and to be called for received packets.
Definition: ppp_service.h:46
mbed::Callback< void(bool up)> ppp_link_state_change_cb_t
Callback to be registered with PPP interface and to be called for link status changes.
Definition: ppp_service.h:54
virtual void link_input(net_stack_mem_buf_t *buf)
Received IP packet from PPP to stack.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:47
EventQueue.
Definition: EventQueue.h:60
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
Class FileHandle.
Definition: FileHandle.h:46
virtual const nsapi_addr_t * get_dns_server(uint8_t index)
Gets DNS server address.
virtual events::EventQueue * event_queue_get()
Handle to PPP event queue.
virtual void resource_unlock()
Unlock PPP resource.
virtual void set_link_state_cb(ppp_link_state_change_cb_t state_cb)
Sets a callback that needs to be called on link status changes for given interface.
virtual uint32_t get_align_preference() const
Gets memory buffer alignment preference.
virtual void set_ip_stack(nsapi_ip_stack_t ip_stack)
Sets IP protocol versions of IP stack.
virtual void set_credentials(const char *uname, const char *password)
Sets user name and password for PPP protocol.
Definition: PPP.h:26
virtual void resource_lock()
Lock PPP resource.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:68
virtual const nsapi_addr_t * get_gateway()
Get the local gateway.
virtual void link_state(bool up)
Link state indication from PPP.
virtual void set_memory_manager(NetStackMemoryManager &mem_mngr)
Sets memory manager that is used to handle memory buffers.
virtual void get_ifname(char *name, uint8_t size) const
Return interface name.
virtual bool link_out(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack)
Sends the packet over the link.
virtual const nsapi_addr_t * get_ip_address(nsapi_version_t version)
Gets local IP address.
virtual void set_stream(mbed::FileHandle *stream)
Sets file stream used to communicate with modem.
virtual void power_down()
Deinitializes the hardware.
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:188
virtual const nsapi_addr_t * get_netmask()
Get the local network mask.
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: ppp_service.h:34
virtual bool power_up()
Initializes the hardware.
virtual uint32_t get_mtu_size()
Return maximum transmission unit.
virtual void set_link_input_cb(ppp_link_input_cb_t input_cb)
Sets a callback that needs to be called for packets received for that interface.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.