├── .github ├── ISSUE_TEMPLATE │ └── bug_report.md └── workflows │ └── main.yml ├── .gitignore ├── CMakeLists.txt ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── include └── cellular_demo_tracing.h ├── mbed-os.lib ├── mbed_app.json ├── resources └── official_armmbed_example_badge.png └── source └── main.cpp /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: 'type: bug' 6 | assignees: '' 7 | 8 | --- 9 | 10 | 28 | 29 | ### Description of defect 30 | 31 | 35 | 36 | 37 | #### Target(s) affected by this defect ? 38 | 39 | 40 | #### Toolchain(s) (name and version) displaying this defect ? 41 | 42 | 43 | #### What version of Mbed-os are you using (tag or sha) ? 44 | 53 | 54 | 55 | #### What version(s) of tools are you using. List all that apply (E.g. mbed-cli) 56 | 57 | 58 | #### How is this defect reproduced ? 59 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Build example application 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | build-cli-v1: 9 | container: 10 | image: ghcr.io/armmbed/mbed-os-env:master-latest 11 | 12 | runs-on: ubuntu-latest 13 | 14 | strategy: 15 | matrix: 16 | target: ["DISCO_l496AG", "WIO_3G", "NRF52840_DK"] 17 | profile: [release, debug, develop] 18 | 19 | 20 | steps: 21 | - 22 | name: Checkout 23 | uses: actions/checkout@v2 24 | 25 | - 26 | name: build-example 27 | run: | 28 | set -e 29 | mbed deploy 30 | mbed compile -t GCC_ARM -m ${{ matrix.target }} --profile ${{ matrix.profile }} 31 | 32 | 33 | build-cli-v2: 34 | container: 35 | image: ghcr.io/armmbed/mbed-os-env:master-latest 36 | 37 | runs-on: ubuntu-latest 38 | 39 | strategy: 40 | matrix: 41 | target: ["DISCO_l496AG", "WIO_3G", "NRF52840_DK"] 42 | profile: [release, debug, develop] 43 | 44 | 45 | steps: 46 | - 47 | name: Checkout 48 | uses: actions/checkout@v2 49 | 50 | - 51 | name: build-example-application 52 | run: | 53 | set -e 54 | mbed-tools deploy 55 | mbed-tools compile -t GCC_ARM -m ${{ matrix.target }} --profile ${{ matrix.profile }} 56 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | 24 | # Executables 25 | *.exe 26 | *.out 27 | *.app 28 | 29 | .yotta.json 30 | BUILD 31 | yotta_modules/ 32 | yotta_targets/ 33 | .DS_Store 34 | 35 | # clion 36 | .idea/ 37 | cmake-build-*/ 38 | 39 | # exporters 40 | GettingStarted.html 41 | 42 | # mbed build system 43 | mbed-os/ 44 | mbed_settings.py 45 | mbed_config.h 46 | *.pyc 47 | TARGET_CORDIO_BLUENRG/ 48 | 49 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020 ARM Limited. All rights reserved. 2 | # SPDX-License-Identifier: Apache-2.0 3 | 4 | cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR) 5 | 6 | set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "") 7 | set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "") 8 | set(APP_TARGET mbed-os-example-cellular) 9 | 10 | include(${MBED_PATH}/tools/cmake/app.cmake) 11 | 12 | project(${APP_TARGET}) 13 | 14 | add_subdirectory(${MBED_PATH}) 15 | 16 | add_executable(${APP_TARGET}) 17 | 18 | mbed_configure_app_target(${APP_TARGET}) 19 | 20 | target_include_directories(${APP_TARGET} 21 | PUBLIC 22 | ${CMAKE_CURRENT_SOURCE_DIR}/include 23 | ) 24 | 25 | target_sources(${APP_TARGET} 26 | PRIVATE 27 | source/main.cpp 28 | ) 29 | 30 | target_link_libraries(${APP_TARGET} 31 | PRIVATE 32 | mbed-os 33 | mbed-netsocket 34 | mbed-cellular 35 | ) 36 | 37 | mbed_set_post_build(${APP_TARGET}) 38 | 39 | option(VERBOSE_BUILD "Have a verbose build process") 40 | if(VERBOSE_BUILD) 41 | set(CMAKE_VERBOSE_MAKEFILE ON) 42 | endif() 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Mbed OS 2 | 3 | Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor. 4 | 5 | To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html). 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](./resources/official_armmbed_example_badge.png) 2 | # Cellular Example 3 | 4 | This is an example based on `mbed-os` cellular APIs that demonstrates a TCP or UDP echo transaction with a public echo server. 5 | 6 | (Note: To see this example in a rendered form you can import into the Arm Mbed Online Compiler, please see 7 | [the documentation](https://os.mbed.com/docs/mbed-os/latest/apis/cellular-api.html#cellular-example-connection-establishment).) 8 | 9 | ## Getting started 10 | 11 | This particular cellular application uses a cellular network and network-socket APIs that are part of [`mbed-os`](https://github.com/ARMmbed/mbed-os). 12 | 13 | The program uses a [cellular modem driver](https://github.com/ARMmbed/mbed-os/tree/master/connectivity/cellular/include/cellular/framework/API) 14 | using an external IP stack standard 3GPP AT 27.007 AT commands to setup the cellular modem and registers to the network. 15 | 16 | After registration, the driver opens a point-to-point protocol (PPP) pipe with the cellular modem and connects 17 | to internet. This driver currently supports UART data connection type only between your cellular modem and MCU. 18 | 19 | For more information on Arm Mbed OS cellular APIs and porting guide, please visit the 20 | [Mbed OS cellular API](https://os.mbed.com/docs/mbed-os/latest/apis/cellular-networking.html) and 21 | [Mbed OS cellular porting guide](https://os.mbed.com/docs/mbed-os/latest/porting/cellular-device-porting.html). 22 | 23 | ### Board support 24 | 25 | Currently supported boards with onboard modem chips can be found under Mbed OS 26 | [/targets folder](https://github.com/ARMmbed/mbed-os/tree/master/targets). 27 | You can find all cellular specific onboard modems by searching an overridden function 28 | `CellularDevice::get_target_default_instance()`. 29 | 30 | Currently supported modem drivers can be found under cellular 31 | [/drivers folder](https://github.com/ARMmbed/mbed-os/tree/master/connectivity/drivers/cellular). 32 | 33 | For a cellular shield, you need to define which shield to use with `provide-default`, and also how the shield is connected 34 | to the Mbed OS board. For example, a generic AT/PPP modem would add from the `GENERIC_AT3GPP/mbed_lib.json` file to your 35 | `mbed_app.json`: 36 | 37 | ``` 38 | "target_overrides": { 39 | "GENERIC_AT3GPP.provide-default": true, 40 | "GENERIC_AT3GPP.tx": "", 41 | "GENERIC_AT3GPP.rx": "" 42 | } 43 | ``` 44 | 45 | ## Building and flashing the example 46 | 47 | ### To build the example 48 | 49 | Clone the repository containing example: 50 | 51 | ``` 52 | git clone https://github.com/ARMmbed/mbed-os-example-cellular.git 53 | ``` 54 | 55 | **Tip:** If you don't have git installed, you can 56 | [download a zip file](https://github.com/ARMmbed/mbed-os-example-cellular/archive/master.zip) of the repository. 57 | 58 | Update the source tree: 59 | 60 | ``` 61 | cd mbed-os-example-cellular 62 | mbed deploy 63 | ``` 64 | 65 | Run the build: 66 | 67 | ```mbed compile -t -m ``` 68 | 69 | ### To flash the example onto your board 70 | 71 | Connect your mbed board to your computer over USB. It appears as removable storage. 72 | 73 | When you run the `mbed compile` command above, mbed cli creates a .bin or a .hex file (depending on your target) in 74 | ```BUILD//``` under the example's directory. Drag and drop the file to the removable storage. 75 | 76 | Alternatively you may launch compilation with `-f` flag to have mbed tools attempt to flash your board. 77 | The tools will flash the binary to all targets that match the board specified by '-m' parameter. 78 | 79 | ### Change the network and SIM credentials 80 | 81 | See the file `mbed_app.json` in the root directory of your application. This file contains all the user specific 82 | configurations your application needs. Provide the pin code for your SIM card, as well as any other cellular settings, 83 | or `null` if not used. For example: 84 | 85 | ```json 86 | "target_overrides": { 87 | "*": { 88 | "nsapi.default-cellular-sim-pin": "\"1234\"", 89 | ``` 90 | 91 | ### Selecting socket type (TCP, UDP or NONIP) 92 | 93 | You can choose which socket type the application should use; however, please note that TCP is a more reliable 94 | transmission protocol. For example: 95 | 96 | ```json 97 | 98 | "sock-type": "TCP", 99 | 100 | ``` 101 | 102 | ### Turning modem AT echo trace on 103 | 104 | If you like details and wish to know about all the AT interactions between the modem and your driver, turn on the modem 105 | AT echo trace: 106 | 107 | ```json 108 | "cellular.debug-at": true 109 | ``` 110 | 111 | ### Turning on the tracing and trace level 112 | 113 | If you like to add more traces or follow the current ones you can turn traces on by changing `mbed-trace.enable` in 114 | mbed_app.json: 115 | 116 | ```"target_overrides": { 117 | "*": { 118 | "mbed-trace.enable": true, 119 | ``` 120 | 121 | After you have defined `mbed-trace.enable: true`, you can set trace levels by changing value in `trace-level`: 122 | 123 | ```"trace-level": { 124 | "help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG", 125 | "macro_name": "MBED_TRACE_MAX_LEVEL", 126 | "value": "TRACE_LEVEL_INFO" 127 | } 128 | ``` 129 | 130 | ## Running the example 131 | 132 | When example application is running information about activity is printed over the serial connection. 133 | 134 | **Note:** The default serial baudrate has been set to 9600. 135 | 136 | Please have a client open and connected to the board. You may use: 137 | 138 | - [Tera Term](https://ttssh2.osdn.jp/index.html.en) for windows 139 | 140 | - screen or minicom for Linux (example usage: `screen /dev/serial/ 9600`) 141 | 142 | - mbed tools has a terminal command `mbed term -b 9600` 143 | 144 | ### Expected output 145 | 146 | You should see an output similar to this: 147 | 148 | ``` 149 | mbed-os-example-cellular 150 | Establishing connection 151 | Connection Established. 152 | TCP: connected with echo.mbedcloudtesting.com server 153 | TCP: Sent 4 Bytes to echo.mbedcloudtesting.com 154 | Received from echo server 4 Bytes 155 | Success. Exiting 156 | ``` 157 | 158 | ### Troubleshooting 159 | 160 | * Make sure the fields `nsapi.default-cellular-sim-pin`, `nsapi.default-cellular-plmn`, `nsapi.default-cellular-apn`, 161 | `nsapi.default-cellular-username` and `nsapi.default-cellular-password` from the `mbed_app.json` file are filled in 162 | correctly. The correct values should appear in the user manual of the board if using eSIM or in the details of the 163 | SIM card if using normal SIM. 164 | * Enable trace flag to have access to debug information `"mbed-trace.enable": true` and `"cellular.debug-at": true`. 165 | * Error Message: Assertion failed: iface usually means that a default modem is not defined, e.g. 166 | `"GENERIC_AT3GPP.provide-default": true` 167 | * If the modem does not respond to (AT) queries, check that UART pins (tx, rx, rts, cts) are connected and defined, 168 | e.g. `"GENERIC_AT3GPP.tx": ""`, ... 169 | * It is a common case that a modem seems to connect fine with just USB power, but actually it needs to have an external 170 | power supply for a data connection. 171 | * Try both `TCP` and `UDP` socket types. 172 | * Try both `"lwip.ppp-enabled": true` and `"lwip.ppp-enabled": false`. 173 | * The modem may support only a fixed baud-rate, such as `"platform.default-serial-baud-rate": 9600`. 174 | * The modem and network may only support IPv6 in which case `"lwip.ipv6-enabled": true` shall be defined. 175 | * The SIM and modem must have compatible cellular technology (3G, 4G, NB-IoT, ...) supported and cellular network available. 176 | * Enable CIoT optimization for NONIP socket `control-plane-opt: true`. 177 | 178 | If you have problems to get started with debugging, you can review the 179 | [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it. 180 | 181 | ## License and contributions 182 | 183 | The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. 184 | Please see [contributing.md](CONTRIBUTING.md) for more info. 185 | 186 | This project contains code from other projects. The original license text is included in those source files. 187 | They must comply with our license guide 188 | -------------------------------------------------------------------------------- /include/cellular_demo_tracing.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 ARM Limited. All rights reserved. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * Licensed under the Apache License, Version 2.0 (the License); you may 5 | * not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "mbed.h" 18 | #include "CellularLog.h" 19 | 20 | #ifndef CELLULAR_DEMO_TRACING_H_ 21 | #define CELLULAR_DEMO_TRACING_H_ 22 | 23 | #if MBED_CONF_MBED_TRACE_ENABLE 24 | static PlatformMutex trace_mutex; 25 | 26 | static void trace_wait() 27 | { 28 | trace_mutex.lock(); 29 | } 30 | 31 | static void trace_release() 32 | { 33 | trace_mutex.unlock(); 34 | } 35 | 36 | static char* trace_time(size_t ss) 37 | { 38 | static char time_st[50]; 39 | auto ms = std::chrono::time_point_cast(Kernel::Clock::now()).time_since_epoch().count(); 40 | snprintf(time_st, 49, "[%08llums]", ms); 41 | return time_st; 42 | } 43 | 44 | static void trace_open() 45 | { 46 | mbed_trace_init(); 47 | mbed_trace_prefix_function_set( &trace_time ); 48 | 49 | mbed_trace_mutex_wait_function_set(trace_wait); 50 | mbed_trace_mutex_release_function_set(trace_release); 51 | 52 | mbed_cellular_trace::mutex_wait_function_set(trace_wait); 53 | mbed_cellular_trace::mutex_release_function_set(trace_release); 54 | 55 | #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN 56 | printf("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL")); 57 | #endif 58 | } 59 | 60 | static void trace_close() 61 | { 62 | mbed_cellular_trace::mutex_wait_function_set(NULL); 63 | mbed_cellular_trace::mutex_release_function_set(NULL); 64 | 65 | mbed_trace_free(); 66 | } 67 | #else 68 | static void trace_open() 69 | { 70 | } 71 | 72 | static void trace_close() 73 | { 74 | } 75 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE 76 | 77 | #endif // CELLULAR_DEMO_TRACING_H_ 78 | -------------------------------------------------------------------------------- /mbed-os.lib: -------------------------------------------------------------------------------- 1 | https://github.com/ARMmbed/mbed-os/#17dc3dc2e6e2817a8bd3df62f38583319f0e4fed 2 | -------------------------------------------------------------------------------- /mbed_app.json: -------------------------------------------------------------------------------- 1 | { 2 | "config": { 3 | "sock-type": "TCP", 4 | "echo-server-hostname": { 5 | "help": "Echo server host name.", 6 | "value": "\"echo.mbedcloudtesting.com\"" 7 | }, 8 | "echo-server-port": { 9 | "help": "Echo server port number.", 10 | "value": 7 11 | }, 12 | "trace-level": { 13 | "help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG", 14 | "macro_name": "MBED_TRACE_MAX_LEVEL", 15 | "value": "TRACE_LEVEL_INFO" 16 | } 17 | }, 18 | "target_overrides": { 19 | "*": { 20 | "target.network-default-interface-type": "CELLULAR", 21 | "mbed-trace.enable": false, 22 | "lwip.ipv4-enabled": true, 23 | "ppp.ipv4-enabled": true, 24 | "lwip.ipv6-enabled": true, 25 | "ppp.ipv6-enabled": true, 26 | "lwip.ethernet-enabled": false, 27 | "lwip.ppp-enabled": true, 28 | "lwip.tcp-enabled": true, 29 | "platform.stdio-convert-newlines": true, 30 | "platform.stdio-baud-rate": 9600, 31 | "platform.default-serial-baud-rate": 9600, 32 | "platform.stdio-buffered-serial": true, 33 | "cellular.debug-at": false, 34 | "cellular.use-apn-lookup": true, 35 | "nsapi.default-cellular-sim-pin": null, 36 | "nsapi.default-cellular-plmn": null, 37 | "nsapi.default-cellular-apn": null, 38 | "nsapi.default-cellular-username": null, 39 | "nsapi.default-cellular-password": null 40 | }, 41 | "DISCO_L496AG": { 42 | "target.macros_add": [ 43 | "CELLULAR_DEVICE=STModCellular" 44 | ], 45 | "target.components_add": ["STMOD_CELLULAR"], 46 | "stmod_cellular.provide-default": "true" 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /resources/official_armmbed_example_badge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ARMmbed/mbed-os-example-cellular/075e8668ada84a139367a446369eae95e45480b8/resources/official_armmbed_example_badge.png -------------------------------------------------------------------------------- /source/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 ARM Limited. All rights reserved. 3 | * SPDX-License-Identifier: Apache-2.0 4 | * Licensed under the Apache License, Version 2.0 (the License); you may 5 | * not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "mbed.h" 18 | #include "CellularNonIPSocket.h" 19 | #include "UDPSocket.h" 20 | #include "TCPSocket.h" 21 | #include "cellular_demo_tracing.h" 22 | 23 | /* configuration choices in mbed_app.json */ 24 | #define UDP 0 25 | #define TCP 1 26 | #define NONIP 2 27 | 28 | #if MBED_CONF_APP_SOCK_TYPE == TCP 29 | static constexpr char SOCKET_TYPE[] = "TCP"; 30 | #elif MBED_CONF_APP_SOCK_TYPE == UDP 31 | static constexpr char SOCKET_TYPE[] = "UDP"; 32 | #elif MBED_CONF_APP_SOCK_TYPE == NONIP 33 | static constexpr char SOCKET_TYPE[] = "CellularNonIP"; 34 | #endif 35 | static const char ECHO_HOSTNAME[] = MBED_CONF_APP_ECHO_SERVER_HOSTNAME; 36 | 37 | 38 | class CellularDemo { 39 | static constexpr uint8_t RETRY_COUNT = 3; 40 | 41 | public: 42 | CellularDemo(NetworkInterface &network) 43 | : _net(network) 44 | { } 45 | 46 | ~CellularDemo() { } 47 | 48 | /** Run the cellular demo. */ 49 | void run() 50 | { 51 | /* sim pin, apn, credentials and possible plmn are taken automatically from json 52 | * when using NetworkInterface::set_default_parameters() */ 53 | _net.set_default_parameters(); 54 | 55 | nsapi_size_or_error_t ret = NSAPI_ERROR_NO_CONNECTION; 56 | 57 | if (connect_cellular()) { 58 | /* ping echo server */ 59 | if (!test_send_and_receive()) { 60 | printf("Sending and received data failed.\n"); 61 | } 62 | 63 | ret = _net.disconnect(); 64 | 65 | if (ret != NSAPI_ERROR_OK) { 66 | printf("Disconnect failed (error: %d).\n", ret); 67 | } 68 | } 69 | 70 | if (ret == NSAPI_ERROR_OK) { 71 | printf("Success. Exiting\n"); 72 | } else { 73 | printf("Failure. Exiting\n"); 74 | } 75 | } 76 | 77 | private: 78 | /** 79 | * For UDP or TCP it opens a socket with the given echo server and performs an echo transaction. 80 | * For Cellular Non-IP it opens a socket for which the data delivery path is decided 81 | * by network's control plane CIoT optimisation setup, for the given APN. 82 | */ 83 | bool test_send_and_receive() 84 | { 85 | nsapi_size_or_error_t ret; 86 | 87 | ret = _socket.open(&_net); 88 | 89 | if (ret != NSAPI_ERROR_OK) { 90 | printf("%sSocket.open() fails, code: %d\n", SOCKET_TYPE, ret); 91 | return false; 92 | } 93 | 94 | _socket.set_timeout(15000); 95 | 96 | if (!resolve_hostname()) { 97 | return false; 98 | } 99 | 100 | if (!connect_socket()) { 101 | return false; 102 | } 103 | 104 | ret = send_test_data(); 105 | 106 | if (ret < 0) { 107 | printf("%sSocket.send() fails, code: %d\n", SOCKET_TYPE, ret); 108 | return false; 109 | } else { 110 | printf("%s: Sent %d Bytes to %s\n", SOCKET_TYPE, ret, ECHO_HOSTNAME); 111 | } 112 | 113 | ret = receive_test_data(); 114 | 115 | if (ret < 0) { 116 | printf("%sSocket.recv() fails, code: %d\n", SOCKET_TYPE, ret); 117 | return false; 118 | } else { 119 | printf("Received from echo server %d Bytes\n", ret); 120 | } 121 | 122 | ret = _socket.close(); 123 | 124 | if (ret != NSAPI_ERROR_OK) { 125 | printf("%sSocket.close() fails, code: %d\n", SOCKET_TYPE, ret); 126 | return false; 127 | } 128 | 129 | return true; 130 | } 131 | 132 | /** Connects to the Cellular Network */ 133 | bool connect_cellular() 134 | { 135 | printf("Establishing connection\n"); 136 | 137 | /* check if we're already connected */ 138 | if (_net.get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { 139 | return true; 140 | } 141 | 142 | nsapi_error_t ret; 143 | 144 | for (uint8_t retry = 0; retry <= RETRY_COUNT; retry++) { 145 | ret = _net.connect(); 146 | 147 | if (ret == NSAPI_ERROR_OK) { 148 | printf("Connection Established.\n"); 149 | return true; 150 | } else if (ret == NSAPI_ERROR_AUTH_FAILURE) { 151 | printf("Authentication Failure.\n"); 152 | return false; 153 | } else { 154 | printf("Couldn't connect: %d, will retry\n", ret); 155 | } 156 | } 157 | 158 | printf("Fatal connection failure: %d\n", ret); 159 | 160 | return false; 161 | } 162 | 163 | /** Connects to the Cellular Network */ 164 | bool resolve_hostname() 165 | { 166 | #if MBED_CONF_APP_SOCK_TYPE != NONIP 167 | nsapi_error_t ret = _net.gethostbyname(ECHO_HOSTNAME, &_socket_address); 168 | 169 | if (ret != NSAPI_ERROR_OK) { 170 | printf("Couldn't resolve remote host: %s, code: %d\n", ECHO_HOSTNAME, ret); 171 | return false; 172 | } 173 | 174 | _socket_address.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); 175 | #endif 176 | return true; 177 | } 178 | 179 | bool connect_socket() 180 | { 181 | #if MBED_CONF_APP_SOCK_TYPE == TCP 182 | nsapi_error_t ret = _socket.connect(_socket_address); 183 | if (ret < 0) { 184 | printf("TCPSocket.connect() fails, code: %d\n", ret); 185 | return false; 186 | } else { 187 | printf("TCP: connected with %s server\n", ECHO_HOSTNAME); 188 | } 189 | #endif 190 | return true; 191 | } 192 | 193 | nsapi_error_t send_test_data() 194 | { 195 | const char *echo_string = "TEST"; 196 | #if MBED_CONF_APP_SOCK_TYPE == UDP 197 | return _socket.sendto(_socket_address, (void*)echo_string, strlen(echo_string)); 198 | #else 199 | return _socket.send((void*)echo_string, strlen(echo_string)); 200 | #endif 201 | } 202 | 203 | nsapi_error_t receive_test_data() 204 | { 205 | char receive_buffer[4]; 206 | #if MBED_CONF_APP_SOCK_TYPE == UDP 207 | return _socket.recvfrom(&_socket_address, (void*)receive_buffer, sizeof(receive_buffer)); 208 | #else 209 | return _socket.recv((void*)receive_buffer, sizeof(receive_buffer)); 210 | #endif 211 | } 212 | 213 | private: 214 | NetworkInterface &_net; 215 | 216 | #if MBED_CONF_APP_SOCK_TYPE == TCP 217 | TCPSocket _socket; 218 | SocketAddress _socket_address; 219 | #elif MBED_CONF_APP_SOCK_TYPE == UDP 220 | UDPSocket _socket; 221 | SocketAddress _socket_address; 222 | #elif MBED_CONF_APP_SOCK_TYPE == NONIP 223 | CellularNonIPSocket _socket; 224 | #endif 225 | }; 226 | 227 | int main() { 228 | printf("\nmbed-os-example-cellular\n"); 229 | 230 | trace_open(); 231 | 232 | #if MBED_CONF_APP_SOCK_TYPE == NONIP 233 | NetworkInterface *net = CellularContext::get_default_nonip_instance(); 234 | #else 235 | NetworkInterface *net = CellularContext::get_default_instance(); 236 | #endif 237 | 238 | if (net) { 239 | CellularDemo example(*net); 240 | example.run(); 241 | } else { 242 | printf("Failed to get_default_instance()\n"); 243 | } 244 | 245 | trace_close(); 246 | 247 | return 0; 248 | } 249 | --------------------------------------------------------------------------------