Knight KE / Mbed OS Game_Master
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     /** Get the internally stored IP address
00071     /return     IP address of the interface or null if not yet connected
00072     */
00073     virtual const char *get_ip_address();
00074 
00075     /** Get the internally stored MAC address
00076     /return     MAC address of the interface
00077     */
00078     virtual const char *get_mac_address();
00079 
00080     /** Register callback for status reporting
00081      *
00082      *  The specified status callback function will be called on status changes
00083      *  on the network. The parameters on the callback are the event type and
00084      *  event-type dependent reason parameter.
00085      *
00086      *  @param status_cb The callback for status changes
00087      */
00088     virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
00089 
00090     /** Get the connection status
00091      *
00092      *  @return         The connection status according to ConnectionStatusType
00093      */
00094     virtual nsapi_connection_status_t get_connection_status() const;
00095 
00096     /** Set blocking status of connect() which by default should be blocking
00097      *
00098      *  @param blocking true if connect is blocking
00099      *  @return         0 on success, negative error code on failure
00100      */
00101     virtual nsapi_error_t set_blocking(bool blocking);
00102 
00103     /** Get the interface ID
00104     /return     Interface identifier
00105     */
00106     int8_t get_interface_id() const { return _interface->get_interface_id(); }
00107 
00108 protected:
00109     InterfaceNanostack();
00110     virtual Nanostack *get_stack(void);
00111     Nanostack::Interface *get_interface() const { return _interface; }
00112 
00113     Nanostack::Interface *_interface;
00114 
00115     char ip_addr_str[40];
00116     char mac_addr_str[24];
00117     mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
00118     bool _blocking;
00119 };
00120 
00121 class MeshInterfaceNanostack : public InterfaceNanostack, public MeshInterface, private mbed::NonCopyable<MeshInterfaceNanostack> {
00122 public:
00123 
00124     /** Attach phy and initialize the mesh
00125      *
00126      *  Initializes a mesh interface on the given phy. Not needed if
00127      *  the phy is passed to the mesh's constructor.
00128      *
00129      *  @return     0 on success, negative on failure
00130      */
00131     nsapi_error_t initialize(NanostackRfPhy *phy);
00132 
00133 protected:
00134     MeshInterfaceNanostack() : _phy(NULL) { }
00135     MeshInterfaceNanostack(NanostackRfPhy *phy) : _phy(phy) { }
00136     Nanostack::MeshInterface *get_interface() const { return static_cast<Nanostack::MeshInterface *>(_interface); }
00137     NanostackRfPhy *_phy;
00138 };
00139 
00140 #endif /* MESHINTERFACENANOSTACK_H */