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