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 ThreadInterface.cpp Source File

ThreadInterface.cpp

00001 #include "ThreadInterface.h"
00002 #include "include/thread_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 "nsth"
00009 
00010 nsapi_error_t ThreadInterface::initialize(NanostackRfPhy *phy)
00011 {
00012     return MeshInterfaceNanostack::initialize(phy);
00013 }
00014 
00015 int ThreadInterface::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 int ThreadInterface::disconnect()
00051 {
00052     nanostack_lock();
00053 
00054     mesh_error_t status = mesh_disconnect();
00055 
00056     nanostack_unlock();
00057 
00058     return map_mesh_error(status);
00059 }
00060 
00061 mesh_error_t ThreadInterface::init()
00062 {
00063     if (eui64 == NULL) {
00064         return MESH_ERROR_PARAM;
00065     }
00066     thread_tasklet_init();
00067     __mesh_handler_set_callback(this);
00068     thread_tasklet_device_config_set(eui64, NULL);
00069     _network_interface_id = thread_tasklet_network_init(_device_id);
00070 
00071     if (_network_interface_id == -2) {
00072         return MESH_ERROR_PARAM;
00073     } else if (_network_interface_id == -3) {
00074         return MESH_ERROR_MEMORY;
00075     } else if (_network_interface_id < 0) {
00076         return MESH_ERROR_UNKNOWN;
00077     }
00078     return MESH_ERROR_NONE;
00079 }
00080 
00081 bool ThreadInterface::getOwnIpAddress(char *address, int8_t len)
00082 {
00083     tr_debug("getOwnIpAddress()");
00084     if (thread_tasklet_get_ip_address(address, len) == 0) {
00085         return true;
00086     }
00087     return false;
00088 }
00089 
00090 mesh_error_t ThreadInterface::mesh_connect()
00091 {
00092     int8_t status;
00093     tr_debug("connect()");
00094 
00095     status = thread_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id);
00096 
00097     if (status >= 0) {
00098         return MESH_ERROR_NONE;
00099     } else if (status == -1) {
00100         return MESH_ERROR_PARAM;
00101     } else if (status == -2) {
00102         return MESH_ERROR_MEMORY;
00103     } else if (status == -3) {
00104         return MESH_ERROR_STATE;
00105     } else {
00106         return MESH_ERROR_UNKNOWN;
00107     }
00108 }
00109 
00110 
00111 mesh_error_t ThreadInterface::mesh_disconnect()
00112 {
00113     int8_t status;
00114 
00115     status = thread_tasklet_disconnect(true);
00116 
00117     if (status >= 0) {
00118         return MESH_ERROR_NONE;
00119     }
00120 
00121     return MESH_ERROR_UNKNOWN;
00122 }