Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
test_cases.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 #include "mbed.h" 00018 #include "mbed-mesh-api/Mesh6LoWPAN_ND.h" 00019 #include "mbed-mesh-api/MeshThread.h" 00020 #include "mbed-mesh-api/MeshInterfaceFactory.h" 00021 #include "mbed-mesh-api/mesh_interface_types.h" 00022 #include "sal/test/ctest_env.h" 00023 #include "atmel-rf-driver/driverRFPhy.h" // rf_device_register 00024 #include "test_cases.h" 00025 #include "mbed-drivers/test_env.h" 00026 #define HAVE_DEBUG 1 00027 #include "ns_trace.h" 00028 00029 #define TRACE_GROUP "main" // for traces 00030 00031 #define TEST_RESULT_PRINT()\ 00032 do {\ 00033 TEST_PRINT("{{summary %s [%s] (%d/%d FAILED)}}\r\n", __func__, (TEST_RESULT()?"PASS":"FAIL"), tests_failed_global, tests_total_global);\ 00034 }while (0) 00035 00036 static uint8_t mesh_network_state = MESH_DISCONNECTED; 00037 AbstractMesh *mesh_api = NULL; 00038 00039 extern void test_result_notify(int result, AbstractMesh *meshAPI); 00040 00041 // Thread device type, currently defined statically, change also 00042 // YOTTA_CFG_MBED_MESH_API_THREAD_DEVICE_TYPE from static_config.h to match selection 00043 #define TEST_THREAD_DEVICE_TYPE_SED false 00044 00045 /* 00046 * Callback from mesh network. Called when network state changes. 00047 */ 00048 void test_callback_connect_disconnect(mesh_connection_status_t mesh_state) 00049 { 00050 tr_info("test_callback_connect_disconnect() %d", mesh_state); 00051 mesh_network_state = mesh_state; 00052 if (mesh_network_state == MESH_CONNECTED) { 00053 tr_info("Connected to mesh network!"); 00054 mesh_error_t err = mesh_api->disconnect(); 00055 TEST_EQ(err, MESH_ERROR_NONE); 00056 } else if (mesh_network_state == MESH_DISCONNECTED) { 00057 tr_info("Disconnected from mesh network!"); 00058 TEST_RESULT_PRINT(); 00059 test_result_notify(test_pass_global, mesh_api); 00060 } else { 00061 // untested branch, catch erros by bad state checking... 00062 TEST_EQ(mesh_network_state, MESH_CONNECTED); 00063 tr_error("Networking error!"); 00064 TEST_RESULT_PRINT(); 00065 test_result_notify(test_pass_global, mesh_api); 00066 } 00067 } 00068 00069 void test_mesh_api_connect_disconnect(int8_t rf_device_id, mesh_network_type_t type) 00070 { 00071 mesh_error_t err; 00072 TEST_PRINT("\r\nBegin %s, type=%d\r\n", __func__, type); 00073 00074 mesh_api = MeshInterfaceFactory::createInterface(type); 00075 // initialize interface 00076 if (type == MESH_TYPE_THREAD) { 00077 uint8_t eui64[8]; 00078 char *pskd; 00079 rf_read_mac_address(eui64); 00080 pskd = (char *)"Secret password"; 00081 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_connect_disconnect, eui64, pskd); 00082 } else { 00083 err = mesh_api->init(rf_device_id, test_callback_connect_disconnect); 00084 } 00085 00086 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00087 TEST_RESULT_PRINT(); 00088 test_result_notify(test_pass_global, mesh_api); 00089 return; 00090 } 00091 00092 err = mesh_api->connect(); 00093 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00094 TEST_RESULT_PRINT(); 00095 test_result_notify(test_pass_global, mesh_api); 00096 return; 00097 } 00098 // control goes to test callback 00099 } 00100 00101 void test_callback_init(mesh_connection_status_t mesh_state) 00102 { 00103 tr_info("test_callback_init() %d", mesh_state); 00104 } 00105 00106 void test_mesh_api_init(int8_t rf_device_id) 00107 { 00108 mesh_error_t err; 00109 TEST_PRINT("\r\nBegin %s\r\n", __func__); 00110 mesh_api = (Mesh6LoWPAN_ND *)MeshInterfaceFactory::createInterface(MESH_TYPE_6LOWPAN_ND); 00111 00112 do { 00113 // no callback set 00114 err = mesh_api->init(rf_device_id, NULL); 00115 if (!TEST_EQ(err, MESH_ERROR_PARAM)) { 00116 break; 00117 } 00118 00119 // bad rf-device_id 00120 err = mesh_api->init(rf_device_id + 1, test_callback_init); 00121 if (!TEST_EQ(err, MESH_ERROR_MEMORY)) { 00122 break; 00123 } 00124 00125 // successful re-initialization 00126 err = mesh_api->init(rf_device_id, test_callback_init); 00127 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00128 break; 00129 } 00130 00131 /* Thread can't be instantiated if ND is initialized */ 00132 MeshThread *meshThread = (MeshThread *)MeshInterfaceFactory::createInterface(MESH_TYPE_THREAD); 00133 if (!TEST_EQ(meshThread, NULL)) { 00134 break; 00135 } 00136 break; 00137 } while (1); 00138 00139 TEST_RESULT_PRINT(); 00140 test_result_notify(test_pass_global, mesh_api); 00141 } 00142 00143 void test_mesh_api_init_thread(int8_t rf_device_id) 00144 { 00145 mesh_error_t err; 00146 uint8_t eui64[8]; 00147 char *pskd; 00148 00149 TEST_PRINT("\r\nBegin %s\r\n", __func__); 00150 rf_read_mac_address(eui64); 00151 pskd = (char *)"Secret password"; 00152 00153 mesh_api = MeshInterfaceFactory::createInterface(MESH_TYPE_THREAD); 00154 00155 do { 00156 // no callback set 00157 err = ((MeshThread *)mesh_api)->init(rf_device_id, NULL); 00158 if (!TEST_EQ(err, MESH_ERROR_PARAM)) { 00159 break; 00160 } 00161 00162 // No address in eui64 00163 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_init, NULL, pskd); 00164 if (!TEST_EQ(err, MESH_ERROR_PARAM)) { 00165 break; 00166 } 00167 00168 // bad rf-device_id 00169 err = ((MeshThread *)mesh_api)->init(rf_device_id + 1, test_callback_init, eui64, pskd); 00170 if (!TEST_EQ(err, MESH_ERROR_MEMORY)) { 00171 break; 00172 } 00173 00174 // successful re-initialization 00175 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_init, eui64, pskd); 00176 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00177 break; 00178 } 00179 00180 /* Thread can't be instantiated if it has been initialized already */ 00181 MeshThread *apiThread = (MeshThread *)MeshInterfaceFactory::createInterface(MESH_TYPE_THREAD); 00182 if (!TEST_EQ(apiThread, NULL)) { 00183 break; 00184 } 00185 00186 /* 6LoWPAN ND can't be instantiated if Thread has been initialized */ 00187 Mesh6LoWPAN_ND *apiND = (Mesh6LoWPAN_ND *)MeshInterfaceFactory::createInterface(MESH_TYPE_6LOWPAN_ND); 00188 if (!TEST_EQ(apiND, NULL)) { 00189 break; 00190 } 00191 00192 break; 00193 } while (1); 00194 00195 TEST_RESULT_PRINT(); 00196 test_result_notify(test_pass_global, mesh_api); 00197 } 00198 00199 /* 00200 * Callback from mesh network for mesh_api_connect test 00201 */ 00202 void test_callback_connect(mesh_connection_status_t mesh_state) 00203 { 00204 mesh_error_t err; 00205 tr_info("test_callback_connect() %d", mesh_state); 00206 mesh_network_state = mesh_state; 00207 00208 if (mesh_network_state == MESH_CONNECTED) { 00209 tr_info("Connected to mesh network!"); 00210 // try to connect again, should fail 00211 err = mesh_api->connect(); 00212 if (!TEST_EQ(err, MESH_ERROR_STATE)) { 00213 TEST_RESULT_PRINT(); 00214 test_result_notify(test_pass_global, mesh_api); 00215 } else { 00216 // disconnect 00217 err = mesh_api->disconnect(); 00218 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00219 TEST_RESULT_PRINT(); 00220 test_result_notify(test_pass_global, mesh_api); 00221 } 00222 } 00223 } else if (mesh_network_state == MESH_DISCONNECTED) { 00224 tr_info("Disconnected from mesh network!"); 00225 TEST_RESULT_PRINT(); 00226 test_result_notify(test_pass_global, mesh_api); 00227 } else { 00228 // untested branch, catch errors by bad state checking... 00229 TEST_EQ(true, false); 00230 tr_error("Networking error!"); 00231 TEST_RESULT_PRINT(); 00232 test_result_notify(test_pass_global, mesh_api); 00233 } 00234 } 00235 00236 void test_mesh_api_connect(int8_t rf_device_id, mesh_network_type_t type) 00237 { 00238 mesh_error_t err; 00239 00240 TEST_PRINT("\r\nBegin %s, type=%d\r\n", __func__, type); 00241 mesh_api = MeshInterfaceFactory::createInterface(type); 00242 00243 do { 00244 // connect uninitialized 00245 err = mesh_api->connect(); 00246 if (!TEST_EQ(err, MESH_ERROR_PARAM)) { 00247 TEST_RESULT_PRINT(); 00248 test_result_notify(test_pass_global, mesh_api); 00249 break; 00250 } 00251 00252 // initialize interface 00253 if (type == MESH_TYPE_THREAD) { 00254 uint8_t eui64[8]; 00255 char *pskd; 00256 rf_read_mac_address(eui64); 00257 pskd = (char *)"Secret password"; 00258 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_connect, eui64, pskd); 00259 } else { 00260 err = mesh_api->init(rf_device_id, test_callback_connect); 00261 } 00262 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00263 TEST_RESULT_PRINT(); 00264 test_result_notify(test_pass_global, mesh_api); 00265 break; 00266 } 00267 00268 // successful connect 00269 err = mesh_api->connect(); 00270 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00271 TEST_RESULT_PRINT(); 00272 test_result_notify(test_pass_global, mesh_api); 00273 break; 00274 } 00275 //test continues in callback 00276 break; 00277 } while (1); 00278 } 00279 00280 /* 00281 * Callback from mesh network for mesh_api_connect test 00282 */ 00283 void test_callback_disconnect(mesh_connection_status_t mesh_state) 00284 { 00285 tr_info("test_callback_disconnect() %d", mesh_state); 00286 TEST_EQ(true, false); 00287 tr_error("Networking error, callback received!"); 00288 TEST_RESULT_PRINT(); 00289 test_result_notify(test_pass_global, mesh_api); 00290 } 00291 00292 void test_mesh_api_disconnect(int8_t rf_device_id, mesh_network_type_t type) 00293 { 00294 mesh_error_t err; 00295 00296 TEST_PRINT("\r\nBegin %s\r\n", __func__); 00297 mesh_api = MeshInterfaceFactory::createInterface(type); 00298 00299 do { 00300 // disconnect not initialized, callback not called 00301 err = mesh_api->disconnect(); 00302 if (!TEST_EQ(err, MESH_ERROR_UNKNOWN)) { 00303 break; 00304 } 00305 00306 // initialize interface 00307 if (type == MESH_TYPE_THREAD) { 00308 uint8_t eui64[8]; 00309 char *pskd; 00310 rf_read_mac_address(eui64); 00311 pskd = (char *)"Secret password"; 00312 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_disconnect, eui64, pskd); 00313 } else { 00314 err = mesh_api->init(rf_device_id, test_callback_disconnect); 00315 } 00316 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00317 break; 00318 } 00319 00320 // disconnect not connected 00321 err = mesh_api->disconnect(); 00322 if (!TEST_EQ(err, MESH_ERROR_UNKNOWN)) { 00323 break; 00324 } 00325 break; 00326 } while (1); 00327 00328 TEST_RESULT_PRINT(); 00329 test_result_notify(test_pass_global, mesh_api); 00330 } 00331 00332 /* 00333 * Callback from mesh network for test_mesh_api_delete_connected test 00334 */ 00335 void test_callback_delete_connected(mesh_connection_status_t mesh_state) 00336 { 00337 tr_info("test_callback_delete() %d", mesh_state); 00338 if (mesh_state == MESH_CONNECTED) { 00339 delete mesh_api; 00340 mesh_api = NULL; 00341 TEST_RESULT_PRINT(); 00342 test_result_notify(test_pass_global, mesh_api); 00343 } else { 00344 tr_error("Networking error!"); 00345 TEST_EQ(true, false); 00346 TEST_RESULT_PRINT(); 00347 test_result_notify(test_pass_global, mesh_api); 00348 } 00349 } 00350 00351 void test_mesh_api_delete_connected(int8_t rf_device_id, mesh_network_type_t type) 00352 { 00353 mesh_error_t err; 00354 00355 TEST_PRINT("\r\nBegin %s, type=%d\r\n", __func__, type); 00356 mesh_api = MeshInterfaceFactory::createInterface(type); 00357 00358 do { 00359 // initialize interface 00360 if (type == MESH_TYPE_THREAD) { 00361 uint8_t eui64[8]; 00362 char *pskd; 00363 rf_read_mac_address(eui64); 00364 pskd = (char *)"Secret password"; 00365 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_delete_connected, eui64, pskd); 00366 } else { 00367 err = mesh_api->init(rf_device_id, test_callback_delete_connected); 00368 } 00369 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00370 TEST_RESULT_PRINT(); 00371 test_result_notify(test_pass_global, mesh_api); 00372 break; 00373 } 00374 00375 // successful connect 00376 err = mesh_api->connect(); 00377 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00378 TEST_RESULT_PRINT(); 00379 test_result_notify(test_pass_global, mesh_api); 00380 break; 00381 } 00382 //test continues in callback 00383 break; 00384 } while (1); 00385 } 00386 00387 00388 /* 00389 * Callback from mesh network for test_mesh_api_data_poll_rate_set_thread test 00390 */ 00391 void test_callback_data_poll_rate_set_thread(mesh_connection_status_t mesh_state) 00392 { 00393 tr_info("test_callback_data_poll_rate_set() %d", mesh_state); 00394 mesh_network_state = mesh_state; 00395 00396 if (mesh_network_state == MESH_CONNECTED) { 00397 tr_info("Connected to mesh network!"); 00398 00399 #if (TEST_THREAD_DEVICE_TYPE_SED == true) 00400 mesh_error_t err; 00401 // set data polling rate to 0 (enables fast polling mode) 00402 err = ((MeshThread*)mesh_api)->data_poll_rate_set(0); 00403 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00404 TEST_RESULT_PRINT(); 00405 test_result_notify(test_pass_global, mesh_api); 00406 return; 00407 } 00408 00409 // try to set data polling rate to 100000 00410 err = ((MeshThread*)mesh_api)->data_poll_rate_set(864001+1); 00411 if (!TEST_EQ(err, MESH_ERROR_PARAM)) { 00412 TEST_RESULT_PRINT(); 00413 test_result_notify(test_pass_global, mesh_api); 00414 return; 00415 } 00416 00417 // OK case, set data polling rate to 5 00418 err = ((MeshThread*)mesh_api)->data_poll_rate_set(5); 00419 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00420 TEST_RESULT_PRINT(); 00421 test_result_notify(test_pass_global, mesh_api); 00422 return; 00423 } 00424 #endif 00425 TEST_RESULT_PRINT(); 00426 test_result_notify(test_pass_global, mesh_api); 00427 } else if (mesh_network_state == MESH_DISCONNECTED) { 00428 tr_info("Disconnected from mesh network!"); 00429 TEST_RESULT_PRINT(); 00430 test_result_notify(test_pass_global, mesh_api); 00431 } else { 00432 // untested branch, catch errors by bad state checking... 00433 TEST_EQ(true, false); 00434 tr_error("Networking error!"); 00435 TEST_RESULT_PRINT(); 00436 test_result_notify(test_pass_global, mesh_api); 00437 } 00438 } 00439 00440 void test_mesh_api_data_poll_rate_set_thread(int8_t rf_device_id) 00441 { 00442 mesh_error_t err; 00443 00444 TEST_PRINT("\r\nBegin %s\r\n", __func__); 00445 00446 mesh_api = MeshInterfaceFactory::createInterface(MESH_TYPE_THREAD); 00447 00448 do { 00449 // try to set data polling rate when not initialized 00450 err = ((MeshThread*)mesh_api)->data_poll_rate_set(1); 00451 if (!TEST_EQ(err, MESH_ERROR_UNKNOWN)) { 00452 TEST_RESULT_PRINT(); 00453 test_result_notify(test_pass_global, mesh_api); 00454 break; 00455 } 00456 00457 uint8_t eui64[8]; 00458 rf_read_mac_address(eui64); 00459 err = ((MeshThread *)mesh_api)->init(rf_device_id, test_callback_data_poll_rate_set_thread, eui64, NULL); 00460 00461 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00462 TEST_RESULT_PRINT(); 00463 test_result_notify(test_pass_global, mesh_api); 00464 break; 00465 } 00466 00467 // try to set data polling rate when not connected 00468 err = ((MeshThread*)mesh_api)->data_poll_rate_set(1); 00469 if (!TEST_EQ(err, MESH_ERROR_UNKNOWN)) { 00470 TEST_RESULT_PRINT(); 00471 test_result_notify(test_pass_global, mesh_api); 00472 break; 00473 } 00474 00475 // successful connect 00476 err = mesh_api->connect(); 00477 if (!TEST_EQ(err, MESH_ERROR_NONE)) { 00478 TEST_RESULT_PRINT(); 00479 test_result_notify(test_pass_global, mesh_api); 00480 break; 00481 } 00482 //test continues in callback 00483 break; 00484 } while (1); 00485 } 00486
Generated on Tue Jul 12 2022 13:16:13 by
