mbed-os
Fork of mbed-os by
features/nanostack/FEATURE_NANOSTACK/mbed-mesh-api/source/AbstractMesh.cpp@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
elessair | 0:f269e3021894 | 3 | * SPDX-License-Identifier: Apache-2.0 |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
elessair | 0:f269e3021894 | 5 | * not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
elessair | 0:f269e3021894 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | /* |
elessair | 0:f269e3021894 | 18 | * Mesh networking interface. |
elessair | 0:f269e3021894 | 19 | */ |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #include "mbed-mesh-api/AbstractMesh.h" |
elessair | 0:f269e3021894 | 22 | #include "mbed-mesh-api/MeshThread.h" |
elessair | 0:f269e3021894 | 23 | #include "mbed-mesh-api/Mesh6LoWPAN_ND.h" |
elessair | 0:f269e3021894 | 24 | #include "include/callback_handler.h" |
elessair | 0:f269e3021894 | 25 | #include "include/mesh_system.h" |
elessair | 0:f269e3021894 | 26 | #include "include/nd_tasklet.h" |
elessair | 0:f269e3021894 | 27 | #include "include/thread_tasklet.h" |
elessair | 0:f269e3021894 | 28 | |
elessair | 0:f269e3021894 | 29 | #define HAVE_DEBUG 1 |
elessair | 0:f269e3021894 | 30 | #include "ns_trace.h" |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | #define TRACE_GROUP "m6La" |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | AbstractMesh::AbstractMesh(mesh_network_type_t type) : |
elessair | 0:f269e3021894 | 35 | _mesh_network_handler(NULL), _network_interface_id(-1), _device_id(-1) |
elessair | 0:f269e3021894 | 36 | { |
elessair | 0:f269e3021894 | 37 | __abstract_mesh_interface = this; |
elessair | 0:f269e3021894 | 38 | _mesh_network_type = type; |
elessair | 0:f269e3021894 | 39 | // initialize mesh networking resources, memory, timers, etc... |
elessair | 0:f269e3021894 | 40 | mesh_system_init(); |
elessair | 0:f269e3021894 | 41 | } |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | AbstractMesh::~AbstractMesh() |
elessair | 0:f269e3021894 | 44 | { |
elessair | 0:f269e3021894 | 45 | tr_debug("~AbstractMesh()"); |
elessair | 0:f269e3021894 | 46 | __abstract_mesh_interface = NULL; |
elessair | 0:f269e3021894 | 47 | } |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | mesh_error_t AbstractMesh::init(int8_t registered_device_id, mesh_network_handler_t callbackHandler) |
elessair | 0:f269e3021894 | 50 | { |
elessair | 0:f269e3021894 | 51 | tr_debug("init()"); |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | if (callbackHandler == (mesh_network_handler_t)NULL) { |
elessair | 0:f269e3021894 | 54 | return MESH_ERROR_PARAM; |
elessair | 0:f269e3021894 | 55 | } |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | _device_id = registered_device_id; |
elessair | 0:f269e3021894 | 58 | _mesh_network_handler = callbackHandler; |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | // Create network interface |
elessair | 0:f269e3021894 | 61 | if (_mesh_network_type == MESH_TYPE_THREAD) { |
elessair | 0:f269e3021894 | 62 | thread_tasklet_init(); |
elessair | 0:f269e3021894 | 63 | _network_interface_id = thread_tasklet_network_init(_device_id); |
elessair | 0:f269e3021894 | 64 | } else if (_mesh_network_type == MESH_TYPE_6LOWPAN_ND) { |
elessair | 0:f269e3021894 | 65 | nd_tasklet_init(); |
elessair | 0:f269e3021894 | 66 | _network_interface_id = nd_tasklet_network_init(_device_id); |
elessair | 0:f269e3021894 | 67 | } |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | if (_network_interface_id >= 0) { |
elessair | 0:f269e3021894 | 70 | return MESH_ERROR_NONE; |
elessair | 0:f269e3021894 | 71 | } else if (_network_interface_id == -2) { |
elessair | 0:f269e3021894 | 72 | return MESH_ERROR_PARAM; |
elessair | 0:f269e3021894 | 73 | } else if (_network_interface_id == -3) { |
elessair | 0:f269e3021894 | 74 | return MESH_ERROR_MEMORY; |
elessair | 0:f269e3021894 | 75 | } |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | return MESH_ERROR_UNKNOWN; |
elessair | 0:f269e3021894 | 78 | } |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | mesh_error_t AbstractMesh::connect() |
elessair | 0:f269e3021894 | 81 | { |
elessair | 0:f269e3021894 | 82 | int8_t status = -9; // init to unknown error |
elessair | 0:f269e3021894 | 83 | tr_debug("connect()"); |
elessair | 0:f269e3021894 | 84 | |
elessair | 0:f269e3021894 | 85 | if (_mesh_network_handler == (mesh_network_handler_t)NULL) { |
elessair | 0:f269e3021894 | 86 | // initialization hasn't been made and connect gets called |
elessair | 0:f269e3021894 | 87 | return MESH_ERROR_PARAM; |
elessair | 0:f269e3021894 | 88 | } |
elessair | 0:f269e3021894 | 89 | |
elessair | 0:f269e3021894 | 90 | if (_mesh_network_type == MESH_TYPE_THREAD) { |
elessair | 0:f269e3021894 | 91 | status = thread_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id); |
elessair | 0:f269e3021894 | 92 | } else if (_mesh_network_type == MESH_TYPE_6LOWPAN_ND) { |
elessair | 0:f269e3021894 | 93 | status = nd_tasklet_connect(&__mesh_handler_c_callback, _network_interface_id); |
elessair | 0:f269e3021894 | 94 | } |
elessair | 0:f269e3021894 | 95 | |
elessair | 0:f269e3021894 | 96 | if (status >= 0) { |
elessair | 0:f269e3021894 | 97 | return MESH_ERROR_NONE; |
elessair | 0:f269e3021894 | 98 | } else if (status == -1) { |
elessair | 0:f269e3021894 | 99 | return MESH_ERROR_PARAM; |
elessair | 0:f269e3021894 | 100 | } else if (status == -2) { |
elessair | 0:f269e3021894 | 101 | return MESH_ERROR_MEMORY; |
elessair | 0:f269e3021894 | 102 | } else if (status == -3) { |
elessair | 0:f269e3021894 | 103 | return MESH_ERROR_STATE; |
elessair | 0:f269e3021894 | 104 | } else { |
elessair | 0:f269e3021894 | 105 | return MESH_ERROR_UNKNOWN; |
elessair | 0:f269e3021894 | 106 | } |
elessair | 0:f269e3021894 | 107 | } |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | /* |
elessair | 0:f269e3021894 | 110 | * Disable optimization as gcc compiler fails to return correct enum value. |
elessair | 0:f269e3021894 | 111 | */ |
elessair | 0:f269e3021894 | 112 | #if defined(__GNUC__) && !defined(__ARMCC_VERSION) |
elessair | 0:f269e3021894 | 113 | #define DISABLE_GCC_OPT __attribute__((optimize("O0"))) |
elessair | 0:f269e3021894 | 114 | #else |
elessair | 0:f269e3021894 | 115 | #define DISABLE_GCC_OPT |
elessair | 0:f269e3021894 | 116 | #endif |
elessair | 0:f269e3021894 | 117 | |
elessair | 0:f269e3021894 | 118 | mesh_error_t DISABLE_GCC_OPT AbstractMesh::disconnect() |
elessair | 0:f269e3021894 | 119 | { |
elessair | 0:f269e3021894 | 120 | int8_t status = -1; |
elessair | 0:f269e3021894 | 121 | |
elessair | 0:f269e3021894 | 122 | if (_mesh_network_type == MESH_TYPE_THREAD) { |
elessair | 0:f269e3021894 | 123 | status = thread_tasklet_disconnect(true); |
elessair | 0:f269e3021894 | 124 | } else if (_mesh_network_type == MESH_TYPE_6LOWPAN_ND) { |
elessair | 0:f269e3021894 | 125 | status = nd_tasklet_disconnect(true); |
elessair | 0:f269e3021894 | 126 | } |
elessair | 0:f269e3021894 | 127 | |
elessair | 0:f269e3021894 | 128 | if (status >= 0) { |
elessair | 0:f269e3021894 | 129 | return MESH_ERROR_NONE; |
elessair | 0:f269e3021894 | 130 | } |
elessair | 0:f269e3021894 | 131 | |
elessair | 0:f269e3021894 | 132 | return MESH_ERROR_UNKNOWN; |
elessair | 0:f269e3021894 | 133 | } |
elessair | 0:f269e3021894 | 134 | |
elessair | 0:f269e3021894 | 135 | void AbstractMesh::callback(mesh_connection_status_t state) |
elessair | 0:f269e3021894 | 136 | { |
elessair | 0:f269e3021894 | 137 | if (_mesh_network_handler) { |
elessair | 0:f269e3021894 | 138 | _mesh_network_handler(state); |
elessair | 0:f269e3021894 | 139 | } |
elessair | 0:f269e3021894 | 140 | } |