Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ppp_service.h Source File

ppp_service.h

00001 /*
00002  * Copyright (c) 2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef PPP_SERVICE_H
00019 #define PPP_SERVICE_H
00020 
00021 #include <stdbool.h>
00022 #include "Callback.h"
00023 #include "Semaphore.h"
00024 #include "NetStackMemoryManager.h"
00025 #include "FileHandle.h"
00026 #include "events/EventQueue.h"
00027 #include "netsocket/PPP.h"
00028 
00029 /**
00030  * This interface should be used to abstract low level access to networking hardware
00031  * All operations receive a `void *` hardware pointer which an ppp device provides when
00032  * it is registered with a stack.
00033  */
00034 class ppp_service : public PPP {
00035 public:
00036     ppp_service();
00037 
00038     static ppp_service &get_instance();
00039 
00040     /**
00041      * Callback to be registered with PPP interface and to be called for received packets
00042      *
00043      * @param buf  Received data
00044      */
00045     //typedef void (*ppp_link_input_fn)(void *data, net_stack_mem_buf_t *buf);
00046     typedef mbed::Callback<void (net_stack_mem_buf_t *buf)> ppp_link_input_cb_t;
00047 
00048     /**
00049      * Callback to be registered with PPP interface and to be called for link status changes
00050      *
00051      * @param  up   Link status
00052      */
00053     //typedef void (*ppp_link_state_change_fn)(void *data, bool up);
00054     typedef mbed::Callback<void (bool up)> ppp_link_state_change_cb_t;
00055 
00056     /**
00057      * Return maximum transmission unit
00058      *
00059      * @return     MTU in bytes
00060      */
00061     virtual uint32_t get_mtu_size();
00062 
00063     /**
00064      * Gets memory buffer alignment preference
00065      *
00066      * Gets preferred memory buffer alignment of the ppp device.
00067      * @return         Memory alignment requirement in bytes
00068      */
00069     virtual uint32_t get_align_preference() const;
00070 
00071     /**
00072      * Return interface name
00073      *
00074      * @param name Pointer to where the name should be written
00075      * @param size Maximum number of characters to copy
00076      */
00077     virtual void get_ifname(char *name, uint8_t size) const;
00078 
00079     /**
00080      * Sends the packet over the link
00081      *
00082      * That cannot be called from an interrupt context.
00083      *
00084      * @param buf  Packet to be send
00085      * @return     True if the packet was send successfully, false otherwise
00086      */
00087     virtual bool link_out(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack);
00088 
00089     /**
00090      * Initializes the hardware
00091      *
00092      * @return True on success, False in case of an error.
00093      */
00094     virtual bool power_up();
00095     /**
00096      * Deinitializes the hardware
00097      *
00098      */
00099     virtual void power_down();
00100 
00101     /**
00102      * Sets a callback that needs to be called for packets received for that interface
00103      *
00104      * @param input_cb Function to be register as a callback
00105      */
00106     virtual void set_link_input_cb(ppp_link_input_cb_t input_cb);
00107 
00108     /**
00109      * Sets a callback that needs to be called on link status changes for given interface
00110      *
00111      * @param state_cb Function to be register as a callback
00112      */
00113     virtual void set_link_state_cb(ppp_link_state_change_cb_t state_cb);
00114 
00115     /** Sets memory manager that is used to handle memory buffers
00116      *
00117      * @param mem_mngr Pointer to memory manager
00118      */
00119     virtual void set_memory_manager(NetStackMemoryManager &mem_mngr);
00120 
00121     /** Sets file stream used to communicate with modem
00122      *
00123      * @param stream Pointer to file handle
00124      */
00125     virtual void set_stream(mbed::FileHandle *stream);
00126 
00127     /** Sets IP protocol versions of IP stack
00128      *
00129      * @param ip_stack IP protocol version
00130      */
00131     virtual void set_ip_stack(nsapi_ip_stack_t ip_stack);
00132 
00133     /** Sets user name and password for PPP protocol
00134      *
00135      * @param uname    User name
00136      * @param password Password
00137      */
00138     virtual void set_credentials(const char *uname, const char *password);
00139 
00140     /** Gets local IP address
00141      *
00142      * @param version IP address version
00143      * @return        IP address
00144      */
00145     virtual const nsapi_addr_t *get_ip_address(nsapi_version_t version);
00146 
00147     /** Get the local network mask.
00148      *
00149      *  @return    Local network mask or null if no network mask has been received.
00150      */
00151     virtual const nsapi_addr_t *get_netmask();
00152 
00153     /** Get the local gateway.
00154      *
00155      *  @return    Local gateway or null if no network mask has been received.
00156      */
00157     virtual const nsapi_addr_t *get_gateway();
00158 
00159     /** Gets DNS server address
00160      *
00161      * @param index Server index
00162      */
00163     virtual const nsapi_addr_t *get_dns_server(uint8_t index);
00164 
00165     /** Link state indication from PPP
00166      *
00167      * @param  up       Link status
00168      */
00169     virtual void link_state(bool up);
00170 
00171     /** Received IP packet from PPP to stack
00172      *
00173      * @param buf  Received packet
00174      */
00175     virtual void link_input(net_stack_mem_buf_t *buf);
00176 
00177     /** Handle to PPP event queue
00178      *
00179      *  @return    Event queue
00180      */
00181     virtual events::EventQueue *event_queue_get();
00182 
00183     /** Lock PPP resource
00184      *
00185      */
00186     virtual void resource_lock();
00187 
00188     /** Unlock PPP resource
00189      *
00190      */
00191     virtual void resource_unlock();
00192 
00193 private:
00194 
00195     // PPP library interface to service
00196     bool prepare_event_queue();
00197     void ppp_input();
00198     void ppp_stream_sigio_callback();
00199     void ppp_handle_modem_hangup();
00200     static void ppp_link_status(struct ppp_pcb_s *pcb, int err_code, void *ctx);
00201     void power_up_call();
00202     void link_state_call(bool up);
00203 
00204     // PPP status functions
00205     nsapi_error_t ppp_stack_if_init();
00206     nsapi_error_t ppp_if_connect();
00207     nsapi_error_t ppp_if_disconnect();
00208 
00209     // Internal data
00210     mbed::FileHandle *ppp_service_stream;
00211     rtos::Semaphore ppp_service_close_sem;
00212     rtos::Mutex ppp_service_mutex;
00213     events::EventQueue *ppp_service_event_queue;
00214     NetStackMemoryManager *memory_manager; /**< Memory manager */
00215     ppp_link_input_cb_t ppp_link_input_cb; /**< Callback for incoming data */
00216     ppp_link_state_change_cb_t ppp_link_state_cb; /**< Link state change callback */
00217 
00218     struct netif *ppp_service_netif;
00219     void *ppp_service_pcb;
00220     nsapi_ip_stack_t ppp_service_stack;
00221     const char *ppp_service_uname;
00222     const char *ppp_service_password;
00223     nsapi_error_t ppp_service_connect_error_code;
00224     bool ppp_service_active : 1;
00225     bool ppp_service_event_queued : 1;
00226     bool ppp_service_terminating : 1;
00227     bool ppp_link_state_up : 1;
00228 };
00229 
00230 #endif  /* PPP_SERVICE_H */