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 struct netif;
30 struct ppp_pcb_s;
31 
32 /**
33  * This interface should be used to abstract low level access to networking hardware
34  * All operations receive a `void *` hardware pointer which an ppp device provides when
35  * it is registered with a stack.
36  */
37 class ppp_service final : public PPP {
38 public:
39  ppp_service();
40 
41  static ppp_service &get_instance();
42 
43  /**
44  * Callback to be registered with PPP interface and to be called for received packets
45  *
46  * @param buf Received data
47  */
48  //typedef void (*ppp_link_input_fn)(void *data, net_stack_mem_buf_t *buf);
50 
51  /**
52  * Callback to be registered with PPP interface and to be called for link status changes
53  *
54  * @param up Link status
55  */
56  //typedef void (*ppp_link_state_change_fn)(void *data, bool up);
58 
59  /**
60  * Return maximum transmission unit
61  *
62  * @return MTU in bytes
63  */
64  uint32_t get_mtu_size() override;
65 
66  /**
67  * Gets memory buffer alignment preference
68  *
69  * Gets preferred memory buffer alignment of the ppp device.
70  * @return Memory alignment requirement in bytes
71  */
72  uint32_t get_align_preference() const override;
73 
74  /**
75  * Return interface name
76  *
77  * @param name Pointer to where the name should be written
78  * @param size Maximum number of characters to copy
79  */
80  void get_ifname(char *name, uint8_t size) const override;
81 
82  /**
83  * Sends the packet over the link
84  *
85  * That cannot be called from an interrupt context.
86  *
87  * @param buf Packet to be send
88  * @return True if the packet was send successfully, false otherwise
89  */
90  bool link_out(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack) override;
91 
92  /**
93  * Initializes the hardware
94  *
95  * @return True on success, False in case of an error.
96  */
97  bool power_up() override;
98  /**
99  * Deinitializes the hardware
100  *
101  */
102  void power_down() override;
103 
104  /**
105  * Sets a callback that needs to be called for packets received for that interface
106  *
107  * @param input_cb Function to be register as a callback
108  */
109  void set_link_input_cb(ppp_link_input_cb_t input_cb) override;
110 
111  /**
112  * Sets a callback that needs to be called on link status changes for given interface
113  *
114  * @param state_cb Function to be register as a callback
115  */
116  void set_link_state_cb(ppp_link_state_change_cb_t state_cb) override;
117 
118  /** Sets memory manager that is used to handle memory buffers
119  *
120  * @param mem_mngr Pointer to memory manager
121  */
122  void set_memory_manager(NetStackMemoryManager &mem_mngr) override;
123 
124  /** Sets file stream used to communicate with modem
125  *
126  * @param stream Pointer to file handle
127  */
128  void set_stream(mbed::FileHandle *stream) override;
129 
130  /** Sets IP protocol versions of IP stack
131  *
132  * @param ip_stack IP protocol version
133  */
134  void set_ip_stack(nsapi_ip_stack_t ip_stack) override;
135 
136  /** Sets user name and password for PPP protocol
137  *
138  * @param uname User name
139  * @param password Password
140  */
141  void set_credentials(const char *uname, const char *password) override;
142 
143  /** Gets local IP address
144  *
145  * @param version IP address version
146  * @return IP address
147  */
148  const nsapi_addr_t *get_ip_address(nsapi_version_t version) override;
149 
150  /** Get the local network mask.
151  *
152  * @return Local network mask or null if no network mask has been received.
153  */
154  const nsapi_addr_t *get_netmask() override;
155 
156  /** Get the local gateway.
157  *
158  * @return Local gateway or null if no network mask has been received.
159  */
160  const nsapi_addr_t *get_gateway() override;
161 
162  /** Gets DNS server address
163  *
164  * @param index Server index
165  */
166  const nsapi_addr_t *get_dns_server(uint8_t index) override;
167 
168  /** Link state indication from PPP
169  *
170  * @param up Link status
171  */
172  void link_state(bool up);
173 
174  /** Received IP packet from PPP to stack
175  *
176  * @param buf Received packet
177  */
178  void link_input(net_stack_mem_buf_t *buf);
179 
180  /** Handle to PPP event queue
181  *
182  * @return Event queue
183  */
185 
186  /** Lock PPP resource
187  *
188  */
189  void resource_lock();
190 
191  /** Unlock PPP resource
192  *
193  */
194  void resource_unlock();
195 
196 private:
197 
198  // PPP library interface to service
199  bool prepare_event_queue();
200  void ppp_input();
201  void ppp_stream_sigio_callback();
202  void ppp_handle_modem_hangup();
203  static void ppp_link_status(struct ppp_pcb_s *pcb, int err_code, void *ctx);
204  void power_up_call();
205  void link_state_call(bool up);
206 
207  // PPP status functions
208  nsapi_error_t ppp_stack_if_init();
209  nsapi_error_t ppp_if_connect();
210  nsapi_error_t ppp_if_disconnect();
211 
212  // Internal data
213  mbed::FileHandle *ppp_service_stream = nullptr;
214  rtos::Semaphore ppp_service_close_sem;
215  rtos::Mutex ppp_service_mutex;
216  events::EventQueue *ppp_service_event_queue = nullptr;
217  NetStackMemoryManager *memory_manager = nullptr; /**< Memory manager */
218  ppp_link_input_cb_t ppp_link_input_cb; /**< Callback for incoming data */
219  ppp_link_state_change_cb_t ppp_link_state_cb; /**< Link state change callback */
220 
221  netif *ppp_service_netif;
222  ppp_pcb_s *ppp_service_pcb = nullptr;
223  nsapi_ip_stack_t ppp_service_stack = IPV4_STACK;
224  const char *ppp_service_uname = nullptr;
225  const char *ppp_service_password = nullptr;
226  nsapi_error_t ppp_service_connect_error_code;
227  bool ppp_service_active : 1;
228  bool ppp_service_event_queued : 1;
229  bool ppp_service_terminating : 1;
230  bool ppp_link_state_up : 1;
231 };
232 
233 #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:49
void set_link_input_cb(ppp_link_input_cb_t input_cb) override
Sets a callback that needs to be called for packets received for that interface.
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:57
void resource_lock()
Lock PPP resource.
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:50
void set_memory_manager(NetStackMemoryManager &mem_mngr) override
Sets memory manager that is used to handle memory buffers.
EventQueue.
Definition: EventQueue.h:62
const nsapi_addr_t * get_netmask() override
Get the local network mask.
void get_ifname(char *name, uint8_t size) const override
Return interface name.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:140
Class FileHandle.
Definition: FileHandle.h:46
Definition: PPP.h:26
void set_credentials(const char *uname, const char *password) override
Sets user name and password for PPP protocol.
const nsapi_addr_t * get_dns_server(uint8_t index) override
Gets DNS server address.
void resource_unlock()
Unlock PPP resource.
bool power_up() override
Initializes the hardware.
Generic data structure used for all lwIP network interfaces.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
events::EventQueue * event_queue_get()
Handle to PPP event queue.
const nsapi_addr_t * get_gateway() override
Get the local gateway.
void link_state(bool up)
Link state indication from PPP.
void set_stream(mbed::FileHandle *stream) override
Sets file stream used to communicate with modem.
const nsapi_addr_t * get_ip_address(nsapi_version_t version) override
Gets local IP address.
void set_link_state_cb(ppp_link_state_change_cb_t state_cb) override
Sets a callback that needs to be called on link status changes for given interface.
bool link_out(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack) override
Sends the packet over the link.
uint32_t get_mtu_size() override
Return maximum transmission unit.
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:235
void link_input(net_stack_mem_buf_t *buf)
Received IP packet from PPP to stack.
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: ppp_service.h:37
void power_down() override
Deinitializes the hardware.
void set_ip_stack(nsapi_ip_stack_t ip_stack) override
Sets IP protocol versions of IP stack.
uint32_t get_align_preference() const override
Gets memory buffer alignment preference.
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.