Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers network.cpp Source File

network.cpp

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "CellularTests.h"
00018 #ifdef CELLULAR_DEVICE
00019 
00020 using namespace mbed;
00021 
00022 static bool wait_register()
00023 {
00024     tr_info("Try registering to network...");
00025     if (network->set_registration() != NSAPI_ERROR_OK ) {
00026         tr_error("Network registration request failed.");
00027         return false;
00028     }
00029 
00030     CellularNetwork::RegistrationStatus status;
00031     for (int i=0; i<180; i++) {
00032         tr_info("Register to network %d...", i);
00033         for (int type = 0; type < CellularNetwork::C_MAX; type++) {
00034             if (network->get_registration_status((CellularNetwork::RegistrationType)type, status) ==  NSAPI_ERROR_OK ) {
00035                 tr_info("status %d...", status);
00036                 switch (status) {
00037                     case CellularNetwork::RegisteredRoaming:
00038                     // fall-through
00039                     case CellularNetwork::RegisteredHomeNetwork:
00040                         tr_info("Registered to network.");
00041                         return true;
00042                     case CellularNetwork::RegisteredSMSOnlyRoaming:
00043                     // fall-through
00044                     case CellularNetwork::RegisteredSMSOnlyHome:
00045                         tr_warn("SMS only network registration!");
00046                         return true;
00047                     case CellularNetwork::RegisteredCSFBNotPreferredRoaming:
00048                     // fall-through
00049                     case CellularNetwork::RegisteredCSFBNotPreferredHome:
00050                         tr_warn("Not preferred network registration!");
00051                         return true;
00052                     case CellularNetwork::AttachedEmergencyOnly:
00053                         tr_warn("Emergency only network registration!");
00054                         return true;
00055                     case CellularNetwork::RegistrationDenied:
00056                         tr_warn("Network registration denied!");
00057                         wait(i);
00058                         break;
00059                     case CellularNetwork::NotRegistered:
00060                     case CellularNetwork::Unknown:
00061                     case CellularNetwork::SearchingNetwork:
00062                     default:
00063                         break;
00064                 }
00065             }
00066         }
00067         wait(1);
00068     }
00069     return false;
00070 }
00071 
00072 void test_attach()
00073 {
00074     cellularDevice.set_timeout(120*1000); // 120 second timeout for at commands after power is up. It might take time to register, attach and connect
00075     tr_info("Register to network.");
00076     TEST_ASSERT(wait_register());
00077     tr_info("Attach to network.");
00078     nsapi_error_t err = network->set_attach();
00079     TEST_ASSERT(!err);
00080     CellularNetwork::AttachStatus status;
00081     err = network->get_attach(status);
00082     TEST_ASSERT(!err);
00083 }
00084 
00085 void test_connect()
00086 {
00087     nsapi_error_t err = network->connect();
00088     TEST_ASSERT(!err);
00089 }
00090 
00091 void test_get_ip_address()
00092 {
00093     const char *ip = network->get_ip_address();
00094     TEST_ASSERT(ip && ip[0]);
00095     tr_info("IP: %s\r\n", ip);
00096 }
00097 
00098 void test_disconnect()
00099 {
00100     nsapi_error_t err = network->disconnect();
00101     TEST_ASSERT(!err);
00102 }
00103 
00104 #endif // CELLULAR_DEVICE