Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NanostackEthernetInterface.cpp Source File

NanostackEthernetInterface.cpp

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 #include "NanostackEthernetInterface.h"
00017 #include "mesh_system.h"
00018 #include "callback_handler.h"
00019 #include "enet_tasklet.h"
00020 
00021 nsapi_error_t Nanostack::EthernetInterface::initialize()
00022 {
00023     nanostack_lock();
00024 
00025     if (register_phy() < 0) {
00026         nanostack_unlock();
00027         return NSAPI_ERROR_DEVICE_ERROR ;
00028     }
00029 
00030     nanostack_unlock();
00031 
00032     return NSAPI_ERROR_OK ;
00033 }
00034 
00035 nsapi_error_t NanostackEthernetInterface::initialize(NanostackEthernetPhy *phy)
00036 {
00037     if (_interface) {
00038         return NSAPI_ERROR_PARAMETER ;
00039     }
00040 
00041     _interface = new (std::nothrow) Nanostack::EthernetInterface(*phy);
00042     if (!_interface) {
00043         return NSAPI_ERROR_NO_MEMORY ;
00044     }
00045 
00046     return get_interface()->initialize();
00047 }
00048 
00049 nsapi_error_t Nanostack::EthernetInterface::bringup(bool dhcp, const char *ip,
00050                                                     const char *netmask, const char *gw,
00051                                                     nsapi_ip_stack_t stack, bool blocking)
00052 {
00053     if (stack == IPV4_STACK) {
00054         return NSAPI_ERROR_UNSUPPORTED ;
00055     }
00056 
00057     nanostack_lock();
00058     _blocking = blocking;
00059     if (interface_id < 0) {
00060         enet_tasklet_init();
00061         __mesh_handler_set_callback(this);
00062         interface_id = enet_tasklet_network_init(_device_id);
00063     }
00064     int8_t status = -1;
00065     if (interface_id >= 0) {
00066         status = enet_tasklet_connect(&__mesh_handler_c_callback, interface_id);
00067     }
00068     nanostack_unlock();
00069 
00070     if (status == -1) {
00071         return NSAPI_ERROR_DEVICE_ERROR ;
00072     } else if (status == -2) {
00073         return NSAPI_ERROR_NO_MEMORY ;
00074     } else if (status == -3) {
00075         return NSAPI_ERROR_ALREADY ;
00076     } else if (status != 0) {
00077         return NSAPI_ERROR_DEVICE_ERROR ;
00078     }
00079 
00080     if (blocking) {
00081         bool acquired = connect_semaphore.try_acquire_for(30000);
00082 
00083         if (!acquired) {
00084             return NSAPI_ERROR_DHCP_FAILURE ; // sort of...
00085         }
00086     }
00087     return NSAPI_ERROR_OK ;
00088 }
00089 
00090 nsapi_error_t NanostackEthernetInterface::do_initialize()
00091 {
00092     if (!_interface) {
00093         return NSAPI_ERROR_PARAMETER ;
00094     }
00095     return NSAPI_ERROR_OK ;
00096 }
00097 
00098 nsapi_error_t Nanostack::EthernetInterface::bringdown()
00099 {
00100     nanostack_lock();
00101     int8_t status = enet_tasklet_disconnect(true);
00102     nanostack_unlock();
00103 
00104     if (status == -1) {
00105         return NSAPI_ERROR_DEVICE_ERROR ;
00106     } else if (status == -2) {
00107         return NSAPI_ERROR_NO_MEMORY ;
00108     } else if (status == -3) {
00109         return NSAPI_ERROR_ALREADY ;
00110     } else if (status != 0) {
00111         return NSAPI_ERROR_DEVICE_ERROR ;
00112     }
00113 
00114     if (_blocking) {
00115         int32_t count = disconnect_semaphore.try_acquire_for(30000);
00116 
00117         if (count <= 0) {
00118             return NSAPI_ERROR_TIMEOUT ;
00119         }
00120     }
00121     return NSAPI_ERROR_OK ;
00122 }