ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoWPANNDInterface.cpp Source File

LoWPANNDInterface.cpp

00001 #include "LoWPANNDInterface.h"
00002 #include "include/nd_tasklet.h"
00003 #include "callback_handler.h"
00004 #include "mesh_system.h"
00005 #include "randLIB.h"
00006 
00007 #include "ns_trace.h"
00008 #define TRACE_GROUP "nslp"
00009 
00010 nsapi_error_t LoWPANNDInterface::initialize(NanostackRfPhy *phy)
00011 {
00012     return MeshInterfaceNanostack::initialize(phy);
00013 }
00014 
00015 int LoWPANNDInterface::connect()
00016 {
00017     nanostack_lock();
00018 
00019     if (register_phy() < 0) {
00020         nanostack_unlock();
00021         return NSAPI_ERROR_DEVICE_ERROR ;
00022     }
00023 
00024     // After the RF is up, we can seed the random from it.
00025     randLIB_seed_random();
00026 
00027     mesh_error_t status = init();
00028     if (status != MESH_ERROR_NONE) {
00029         nanostack_unlock();
00030         return map_mesh_error(status);
00031     }
00032 
00033     status = mesh_connect();
00034     if (status != MESH_ERROR_NONE) {
00035         nanostack_unlock();
00036         return map_mesh_error(status);
00037     }
00038 
00039     // Release mutex before blocking
00040     nanostack_unlock();
00041 
00042     int32_t count = connect_semaphore.wait(30000);
00043 
00044     if (count <= 0) {
00045         return NSAPI_ERROR_DHCP_FAILURE ; // sort of...
00046     }
00047     return 0;
00048 
00049 }
00050 
00051 int LoWPANNDInterface::disconnect()
00052 {
00053     nanostack_lock();
00054 
00055     mesh_error_t status = mesh_disconnect();
00056 
00057     nanostack_unlock();
00058 
00059     return map_mesh_error(status);
00060 }
00061 
00062 mesh_error_t LoWPANNDInterface::init()
00063 {
00064     nd_tasklet_init();
00065     __mesh_handler_set_callback(this);
00066     _network_interface_id = nd_tasklet_network_init(_device_id);
00067 
00068     if (_network_interface_id == -2) {
00069         return MESH_ERROR_PARAM;
00070     } else if (_network_interface_id == -3) {
00071         return MESH_ERROR_MEMORY;
00072     } else if (_network_interface_id < 0) {
00073         return MESH_ERROR_UNKNOWN;
00074     }
00075     return MESH_ERROR_NONE;
00076 }
00077 
00078 mesh_error_t LoWPANNDInterface::mesh_connect()
00079 {
00080     int8_t status = -9; // init to unknown error
00081     tr_debug("connect()");
00082 
00083     status = nd_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id);
00084 
00085     if (status >= 0) {
00086         return MESH_ERROR_NONE;
00087     } else if (status == -1) {
00088         return MESH_ERROR_PARAM;
00089     } else if (status == -2) {
00090         return MESH_ERROR_MEMORY;
00091     } else if (status == -3) {
00092         return MESH_ERROR_STATE;
00093     } else {
00094         return MESH_ERROR_UNKNOWN;
00095     }
00096 }
00097 
00098 mesh_error_t LoWPANNDInterface::mesh_disconnect()
00099 {
00100     int8_t status = -1;
00101 
00102     status = nd_tasklet_disconnect(true);
00103 
00104     if (status >= 0) {
00105         return MESH_ERROR_NONE;
00106     }
00107 
00108     return MESH_ERROR_UNKNOWN;
00109 }
00110 
00111 bool LoWPANNDInterface::getOwnIpAddress(char *address, int8_t len)
00112 {
00113     tr_debug("getOwnIpAddress()");
00114     if (nd_tasklet_get_ip_address(address, len) == 0) {
00115         return true;
00116     }
00117     return false;
00118 }
00119 
00120 bool LoWPANNDInterface::getRouterIpAddress(char *address, int8_t len)
00121 {
00122     tr_debug("getRouterIpAddress()");
00123     if (nd_tasklet_get_router_ip_address(address, len) == 0) {
00124         return true;
00125     }
00126     return false;
00127 }