Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sim.cpp Source File

sim.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 
00019 #ifdef CELLULAR_DEVICE
00020 
00021 #ifdef MBED_CONF_APP_CELLULAR_SIM_PIN
00022 
00023 using namespace mbed;
00024 
00025 void test_get_sim_state()
00026 {
00027     wait(1);
00028     CellularSIM::SimState state = CellularSIM::SimStateUnknown;
00029     tr_info("Wait SIM for 180 seconds...");
00030     for (int i = 0; i < 180; i++) {
00031         CellularSIM::SimState tmp_state;
00032         if ((sim->get_sim_state(tmp_state) == NSAPI_ERROR_OK ) && tmp_state != CellularSIM::SimStateUnknown) {
00033             state = tmp_state;
00034             break;
00035         }
00036     }
00037     TEST_ASSERT_MESSAGE(state == CellularSIM::SimStateReady || state == CellularSIM::SimStatePinNeeded ||
00038                         state == CellularSIM::SimStatePukNeeded, "Invalid SIM state");
00039 }
00040 
00041 // creates PIN which is different than one defined in MBED_CONF_APP_CELLULAR_SIM_PIN
00042 static void create_random_pin(char* random_pin)
00043 {
00044 
00045     char s[11];
00046     do {
00047         sprintf(s,"%d", rand());
00048 
00049     } while (strncmp(s, MBED_CONF_APP_CELLULAR_SIM_PIN, 4) == 0);
00050 
00051     strncpy(random_pin, s, 4);
00052     random_pin[4] = '\0';
00053 }
00054 
00055 void test_set_pin()
00056 {
00057     // run test only if sim is not in ready state as then sim interface will return NSAPI_ERROR_OK
00058     nsapi_error_t err;
00059     CellularSIM::SimState state = CellularSIM::SimStateUnknown;
00060     if ((sim->get_sim_state(state) == NSAPI_ERROR_OK ) && (state != CellularSIM::SimStateReady)) {
00061         char random_pin[5];
00062         create_random_pin(random_pin);
00063         err = sim->set_pin(random_pin);
00064         TEST_ASSERT_MESSAGE(err != 0, "Setting random pin should fail");
00065     }
00066 
00067     err = sim->set_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
00068     char err_msg[60];
00069     sprintf(err_msg, "Setting correct pin: %s failed with: %d", MBED_CONF_APP_CELLULAR_SIM_PIN, err);
00070     TEST_ASSERT_MESSAGE(err == 0, err_msg);
00071 }
00072 
00073 void test_change_pin()
00074 {
00075     char random_pin[5];
00076     create_random_pin(random_pin);
00077     nsapi_error_t err = sim->change_pin(MBED_CONF_APP_CELLULAR_SIM_PIN, random_pin);
00078 
00079     char err_msg[60];
00080     sprintf(err_msg, "Change from original pin failed with: %d", err);
00081     TEST_ASSERT_MESSAGE(err == NSAPI_ERROR_OK , err_msg);
00082 
00083     err = sim->change_pin(random_pin, MBED_CONF_APP_CELLULAR_SIM_PIN);
00084     sprintf(err_msg, "Change back original pin failed with: %d", err);
00085     TEST_ASSERT_MESSAGE(err == NSAPI_ERROR_OK , err_msg);
00086 }
00087 
00088 #endif
00089 
00090 #endif // CELLULAR_DEVICE