init
Embed:
(wiki syntax)
Show/hide line numbers
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 if (_connect_status == NSAPI_STATUS_GLOBAL_UP || _connect_status == NSAPI_STATUS_LOCAL_UP ) { 00018 return NSAPI_ERROR_IS_CONNECTED ; 00019 } else if (_connect_status == NSAPI_STATUS_CONNECTING ) { 00020 return NSAPI_ERROR_ALREADY ; 00021 } 00022 00023 nanostack_lock(); 00024 00025 if (register_phy() < 0) { 00026 nanostack_unlock(); 00027 return NSAPI_ERROR_DEVICE_ERROR ; 00028 } 00029 // After the RF is up, we can seed the random from it. 00030 randLIB_seed_random(); 00031 00032 mesh_error_t status = init(); 00033 if (status != MESH_ERROR_NONE) { 00034 nanostack_unlock(); 00035 return map_mesh_error(status); 00036 } 00037 00038 status = mesh_connect(); 00039 if (status != MESH_ERROR_NONE) { 00040 nanostack_unlock(); 00041 return map_mesh_error(status); 00042 } 00043 00044 // Release mutex before blocking 00045 nanostack_unlock(); 00046 00047 // In Thread wait connection for ever: 00048 // -routers will create new network and get local connectivity 00049 // -end devices will get connectivity once attached to existing network 00050 // -devices without network settings gets connectivity once commissioned and attached to network 00051 _connect_status = NSAPI_STATUS_CONNECTING ; 00052 if (_connection_status_cb) { 00053 _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE , NSAPI_STATUS_CONNECTING ); 00054 } 00055 if (_blocking) { 00056 int32_t count = connect_semaphore.wait(osWaitForever); 00057 00058 if (count <= 0) { 00059 return NSAPI_ERROR_DHCP_FAILURE ; // sort of... 00060 } 00061 } 00062 return 0; 00063 } 00064 00065 int ThreadInterface::disconnect() 00066 { 00067 nanostack_lock(); 00068 00069 mesh_error_t status = mesh_disconnect(); 00070 00071 nanostack_unlock(); 00072 00073 return map_mesh_error(status); 00074 } 00075 00076 mesh_error_t ThreadInterface::init() 00077 { 00078 thread_tasklet_init(); 00079 __mesh_handler_set_callback(this); 00080 thread_tasklet_device_eui64_set(_eui64); 00081 _network_interface_id = thread_tasklet_network_init(_device_id); 00082 00083 if (_network_interface_id == -2) { 00084 return MESH_ERROR_PARAM; 00085 } else if (_network_interface_id == -3) { 00086 return MESH_ERROR_MEMORY; 00087 } else if (_network_interface_id < 0) { 00088 return MESH_ERROR_UNKNOWN; 00089 } 00090 return MESH_ERROR_NONE; 00091 } 00092 00093 bool ThreadInterface::getOwnIpAddress(char *address, int8_t len) 00094 { 00095 tr_debug("getOwnIpAddress()"); 00096 if (thread_tasklet_get_ip_address(address, len) == 0) { 00097 return true; 00098 } 00099 return false; 00100 } 00101 00102 mesh_error_t ThreadInterface::mesh_connect() 00103 { 00104 int8_t status; 00105 tr_debug("connect()"); 00106 00107 status = thread_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id); 00108 00109 if (status >= 0) { 00110 return MESH_ERROR_NONE; 00111 } else if (status == -1) { 00112 return MESH_ERROR_PARAM; 00113 } else if (status == -2) { 00114 return MESH_ERROR_MEMORY; 00115 } else if (status == -3) { 00116 return MESH_ERROR_STATE; 00117 } else { 00118 return MESH_ERROR_UNKNOWN; 00119 } 00120 } 00121 00122 void ThreadInterface::device_eui64_set(const uint8_t *eui64) 00123 { 00124 memcpy(_eui64, eui64, 8); 00125 } 00126 00127 mesh_error_t ThreadInterface::device_pskd_set(const char *pskd) 00128 { 00129 return (mesh_error_t)thread_tasklet_device_pskd_set(pskd); 00130 } 00131 00132 mesh_error_t ThreadInterface::mesh_disconnect() 00133 { 00134 int8_t status; 00135 00136 status = thread_tasklet_disconnect(true); 00137 00138 if (status >= 0) { 00139 return MESH_ERROR_NONE; 00140 } 00141 00142 return MESH_ERROR_UNKNOWN; 00143 }
Generated on Tue Jul 12 2022 13:25:18 by
1.7.2