erkin yucel / mbed-os

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AbstractMesh.cpp Source File

AbstractMesh.cpp

00001 /*
00002  * Copyright (c) 2015 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 
00017 /*
00018  * Mesh networking interface.
00019  */
00020 
00021 #include "mbed-mesh-api/AbstractMesh.h"
00022 #include "mbed-mesh-api/MeshThread.h"
00023 #include "mbed-mesh-api/Mesh6LoWPAN_ND.h"
00024 #include "include/callback_handler.h"
00025 #include "include/mesh_system.h"
00026 #include "include/nd_tasklet.h"
00027 #include "include/thread_tasklet.h"
00028 
00029 #define HAVE_DEBUG 1
00030 #include "ns_trace.h"
00031 
00032 #define TRACE_GROUP  "m6La"
00033 
00034 AbstractMesh::AbstractMesh(mesh_network_type_t type) :
00035     _mesh_network_handler(NULL), _network_interface_id(-1), _device_id(-1)
00036 {
00037     __abstract_mesh_interface = this;
00038     _mesh_network_type = type;
00039     // initialize mesh networking resources, memory, timers, etc...
00040     mesh_system_init();
00041 }
00042 
00043 AbstractMesh::~AbstractMesh()
00044 {
00045     tr_debug("~AbstractMesh()");
00046     __abstract_mesh_interface = NULL;
00047 }
00048 
00049 mesh_error_t AbstractMesh::init(int8_t registered_device_id, mesh_network_handler_t callbackHandler)
00050 {
00051     tr_debug("init()");
00052 
00053     if (callbackHandler == (mesh_network_handler_t)NULL) {
00054         return MESH_ERROR_PARAM;
00055     }
00056 
00057     _device_id = registered_device_id;
00058     _mesh_network_handler = callbackHandler;
00059 
00060     // Create network interface
00061     if (_mesh_network_type == MESH_TYPE_THREAD) {
00062         thread_tasklet_init();
00063         _network_interface_id = thread_tasklet_network_init(_device_id);
00064     } else if (_mesh_network_type == MESH_TYPE_6LOWPAN_ND) {
00065         nd_tasklet_init();
00066         _network_interface_id = nd_tasklet_network_init(_device_id);
00067     }
00068 
00069     if (_network_interface_id >= 0) {
00070         return MESH_ERROR_NONE;
00071     } else if (_network_interface_id == -2) {
00072         return MESH_ERROR_PARAM;
00073     } else if (_network_interface_id == -3) {
00074         return MESH_ERROR_MEMORY;
00075     }
00076 
00077     return MESH_ERROR_UNKNOWN;
00078 }
00079 
00080 mesh_error_t AbstractMesh::connect()
00081 {
00082     int8_t status = -9; // init to unknown error
00083     tr_debug("connect()");
00084 
00085     if (_mesh_network_handler == (mesh_network_handler_t)NULL) {
00086         // initialization hasn't been made and connect gets called
00087         return MESH_ERROR_PARAM;
00088     }
00089 
00090     if (_mesh_network_type == MESH_TYPE_THREAD) {
00091         status = thread_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id);
00092     } else if (_mesh_network_type == MESH_TYPE_6LOWPAN_ND) {
00093         status = nd_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id);
00094     }
00095 
00096     if (status >= 0) {
00097         return MESH_ERROR_NONE;
00098     } else if (status == -1) {
00099         return MESH_ERROR_PARAM;
00100     } else if (status == -2) {
00101         return MESH_ERROR_MEMORY;
00102     } else if (status == -3) {
00103         return MESH_ERROR_STATE;
00104     } else {
00105         return MESH_ERROR_UNKNOWN;
00106     }
00107 }
00108 
00109 /*
00110  * Disable optimization as gcc compiler fails to return correct enum value.
00111  */
00112 #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
00113 #define DISABLE_GCC_OPT __attribute__((optimize("O0")))
00114 #else
00115 #define DISABLE_GCC_OPT
00116 #endif
00117 
00118 mesh_error_t DISABLE_GCC_OPT AbstractMesh::disconnect()
00119 {
00120     int8_t status = -1;
00121 
00122     if (_mesh_network_type == MESH_TYPE_THREAD) {
00123         status = thread_tasklet_disconnect(true);
00124     } else if (_mesh_network_type == MESH_TYPE_6LOWPAN_ND) {
00125         status = nd_tasklet_disconnect(true);
00126     }
00127 
00128     if (status >= 0) {
00129         return MESH_ERROR_NONE;
00130     }
00131 
00132     return MESH_ERROR_UNKNOWN;
00133 }
00134 
00135 void AbstractMesh::callback(mesh_connection_status_t state)
00136 {
00137     if (_mesh_network_handler) {
00138         _mesh_network_handler(state);
00139     }
00140 }