Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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