takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MeshInterfaceNanostack.h Source File

MeshInterfaceNanostack.h

00001 /*
00002  * Copyright (c) 2016 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef MESHINTERFACENANOSTACK_H
00018 #define MESHINTERFACENANOSTACK_H
00019 #include "mbed.h"
00020 
00021 #include "MeshInterface.h"
00022 #include "NanostackRfPhy.h"
00023 #include "Nanostack.h"
00024 #include "mesh_interface_types.h"
00025 
00026 class Nanostack::Interface : public OnboardNetworkStack::Interface, private mbed::NonCopyable<Nanostack::Interface> {
00027 public:
00028     virtual char *get_ip_address(char *buf, nsapi_size_t buflen);
00029     virtual char *get_mac_address(char *buf, nsapi_size_t buflen);
00030     virtual char *get_netmask(char *buf, nsapi_size_t buflen);
00031     virtual char *get_gateway(char *buf, nsapi_size_t buflen);
00032     virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
00033     virtual nsapi_connection_status_t get_connection_status() const;
00034 
00035     void get_mac_address(uint8_t *buf) const { interface_phy.get_mac_address(buf); }
00036 
00037     /**
00038      * \brief Callback from C-layer
00039      * \param status state of the network
00040      * */
00041     void network_handler(mesh_connection_status_t status);
00042 
00043     int8_t get_interface_id() const { return interface_id; }
00044     int8_t get_driver_id() const { return _device_id; }
00045 
00046 private:
00047     NanostackPhy &interface_phy;
00048 protected:
00049     Interface(NanostackPhy &phy);
00050     virtual nsapi_error_t register_phy();
00051     NanostackPhy &get_phy() const { return interface_phy; }
00052     int8_t interface_id;
00053     int8_t _device_id;
00054     Semaphore connect_semaphore;
00055 
00056     Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
00057     nsapi_connection_status_t _connect_status;
00058     bool _blocking;
00059 };
00060 
00061 class Nanostack::MeshInterface : public Nanostack::Interface {
00062 protected:
00063     MeshInterface(NanostackRfPhy &phy) : Interface(phy) { }
00064     NanostackRfPhy &get_phy() const { return static_cast<NanostackRfPhy &>(Interface::get_phy()); }
00065 };
00066 
00067 
00068 class InterfaceNanostack : public virtual NetworkInterface {
00069 public:
00070     /** Start the interface
00071      *
00072      *  @return     0 on success, negative error code on failure
00073      */
00074     virtual nsapi_error_t connect();
00075 
00076     /** Stop the interface
00077      *
00078      *  @return     0 on success, negative error code on failure
00079      */
00080     virtual nsapi_error_t disconnect();
00081 
00082     /** Get the internally stored IP address
00083     /return     IP address of the interface or null if not yet connected
00084     */
00085     virtual const char *get_ip_address();
00086 
00087     /** Get the internally stored MAC address
00088     /return     MAC address of the interface
00089     */
00090     virtual const char *get_mac_address();
00091 
00092     /** Register callback for status reporting
00093      *
00094      *  The specified status callback function will be called on status changes
00095      *  on the network. The parameters on the callback are the event type and
00096      *  event-type dependent reason parameter.
00097      *
00098      *  @param status_cb The callback for status changes
00099      */
00100     virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
00101 
00102     /** Get the connection status
00103      *
00104      *  @return         The connection status according to ConnectionStatusType
00105      */
00106     virtual nsapi_connection_status_t get_connection_status() const;
00107 
00108     /** Set blocking status of connect() which by default should be blocking
00109      *
00110      *  @param blocking true if connect is blocking
00111      *  @return         0 on success, negative error code on failure
00112      */
00113     virtual nsapi_error_t set_blocking(bool blocking);
00114 
00115     /** Get the interface ID
00116     /return     Interface identifier
00117     */
00118     int8_t get_interface_id() const { return _interface->get_interface_id(); }
00119 
00120 protected:
00121     InterfaceNanostack();
00122     virtual Nanostack *get_stack(void);
00123     Nanostack::Interface *get_interface() const { return _interface; }
00124     virtual nsapi_error_t do_initialize() = 0;
00125 
00126     Nanostack::Interface *_interface;
00127 
00128     char ip_addr_str[40];
00129     char mac_addr_str[24];
00130     mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
00131     bool _blocking;
00132 };
00133 
00134 class MeshInterfaceNanostack : public InterfaceNanostack, public MeshInterface, private mbed::NonCopyable<MeshInterfaceNanostack> {
00135 public:
00136 
00137     /** Attach phy and initialize the mesh
00138      *
00139      *  Initializes a mesh interface on the given phy. Not needed if
00140      *  the phy is passed to the mesh's constructor.
00141      *
00142      *  @return     0 on success, negative on failure
00143      */
00144     nsapi_error_t initialize(NanostackRfPhy *phy);
00145 
00146 protected:
00147     MeshInterfaceNanostack() : _phy(NULL) { }
00148     MeshInterfaceNanostack(NanostackRfPhy *phy) : _phy(phy) { }
00149     Nanostack::MeshInterface *get_interface() const { return static_cast<Nanostack::MeshInterface *>(_interface); }
00150     NanostackRfPhy *_phy;
00151 };
00152 
00153 #endif /* MESHINTERFACENANOSTACK_H */