Knight KE / Mbed OS Game_Master
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 (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         int32_t count = connect_semaphore.wait(30000);
00082 
00083         if (count <= 0) {
00084             return NSAPI_ERROR_DHCP_FAILURE ; // sort of...
00085         }
00086     }
00087     return 0;
00088 }
00089 
00090 int NanostackEthernetInterface::connect()
00091 {
00092     if (!_interface) {
00093         return NSAPI_ERROR_PARAMETER ;
00094     }
00095     return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking);
00096 }
00097 
00098 nsapi_error_t Nanostack::EthernetInterface::bringdown()
00099 {
00100     enet_tasklet_disconnect();
00101     return 0;
00102 }
00103 
00104 
00105 int NanostackEthernetInterface::disconnect()
00106 {
00107     if (!_interface) {
00108         return NSAPI_ERROR_NO_CONNECTION ;
00109     }
00110     return _interface->bringdown();
00111 }