Mbed OS 文档翻译 之 参考(API(网络接口(Cellular)))

Cellular

                                                                        

                                                                          CellularBase 类层次结构

CellularBase 提供了一个 C++ API,用于通过蜂窝设备连接到互联网。

Arm Mbed OS 提供了 CellularBase 的参考实现,它具有更多信息。

Cellular

CellularBase 提供了一个 C++ API,用于通过蜂窝设备连接到互联网。

Arm Mbed OS 提供了 CellularBase 的参考实现。

入门

  1. 选择支持蜂窝的 Mbed 板,例如 UBLOX-C027 或 MTS-DRAGONFLY。

  2. 克隆 mbed-os-example-cellular。按照存储库中的说明进行操作。

    1. 编译代码。
    2. 下载到板子。

    您会看到类似于以下摘录的输出:

mbed-os-example-cellular
Establishing connection ......

Connection Established.
TCP: connected with echo.mbedcloudtesting.com server
TCP: Sent 4 Bytes to echo.mbedcloudtesting.com
Received from echo server 4 Bytes


Success. Exiting

基本工作原则

您可以通过各种不同方式使用和扩展蜂窝接口。例如,

  • 使用 AT 命令控制蜂窝调制解调器内置的现有 IP 栈中的套接字。

               

  • 使用 PPP(点对点协议)管道在 Mbed OS 支持的 IP 栈和蜂窝调制解调器设备之间传递 IP 数据包。

               

mbed-os-example-cellular 使用简单的蜂窝连接。它取决于调制解调器应用程序是使用 PPP 还是 AT 模式。我们可以总结这个特定的设计如下:

  • 它使用外部 IP 栈(如 LWIP)或片上网络栈(例如调制解调器不支持 PPP 时)。
  • 简单的蜂窝连接使用标准的 3GPP AT 27.007 AT 命令来建立蜂窝调制解调器并注册到网络。
  • 注册后,驱动程序使用带有蜂窝调制解调器的 LWIP 打开 PPP 管道并连接到互联网。如果正在使用仅 AT 模式,则调制解调器专用的 AT 命令用于套接字数据控制。

CellularBase 类参考

CellularBase 类参考

公共成员函数
virtual void  set_credentials (const char *apn, const char *uname=0, const char *pwd=0)=0
virtual void  set_sim_pin (const char *sim_pin)=0
virtual nsapi_error_t  connect (const char *sim_pin, const char *apn=0, const char *uname=0, const char *pwd=0)=0
virtual nsapi_error_t  connect ()=0
virtual nsapi_error_t  disconnect ()=0
virtual bool  is_connected ()=0
virtual const char *  get_ip_address ()=0
virtual const char *  get_netmask ()=0
virtual const char *  get_gateway ()=0
virtual CellularBase *  cellularBase ()
 公共成员函数继承自 NetworkInterface
virtual const char *  get_mac_address ()
virtual nsapi_error_t  set_network (const char *ip_address, const char *netmask, const char *gateway)
virtual nsapi_error_t  set_dhcp (bool dhcp)
virtual nsapi_error_t  gethostbyname (const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC)
virtual nsapi_value_or_error_t  gethostbyname_async (const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC)
virtual nsapi_error_t  gethostbyname_async_cancel (int id)
virtual nsapi_error_t  add_dns_server (const SocketAddress &address)
virtual void  attach (mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
virtual nsapi_connection_status_t  get_connection_status () const
virtual nsapi_error_t  set_blocking (bool blocking)
virtual EthInterface *  ethInterface ()
virtual WiFiInterface *  wifiInterface ()
virtual MeshInterface *  meshInterface ()
virtual EMACInterface *  emacInterface ()
静态公共成员函数
static CellularBase *  get_default_instance ()
 静态公共成员函数继承自 NetworkInterface
static NetworkInterface *  get_default_instance ()
静态保护的成员函数
static CellularBase *  get_target_default_instance ()
 静态保护的成员函数继承自 NetworkInterface
static NetworkInterface *  get_target_default_instance ()
其他继承成员
 继承自的公共类型 NetworkInterface
typedef mbed::Callback< void(nsapi_error_t result, SocketAddress *address)>  hostbyname_cb_t
 继承自的公共类型 DNS
typedef mbed::Callback< void(nsapi_error_t result, SocketAddress *address)>  hostbyname_cb_t
 受保护的成员函数继承自 NetworkInterface
virtual NetworkStack *  get_stack ()=0

用法

调出网络接口:

  1. 实例化 CellularBase 类的实现。
  2. 使用您的 SIM 卡的 PIN 码和网络的 APN 呼叫连接(pincode,apn)功能。
  3. 连接后,您可以像往常一样使用 Mbed OS 网络套接字。

Cellular 示例:连接建立

此示例使用 Mbed OS CellularInterface 建立与蜂窝网络的连接。

main.cpp                                                                                                                                                  导入到 Mbed IDE

/*
 * Copyright (c) 2017 ARM Limited. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "common_functions.h"
#include "UDPSocket.h"
#include "OnboardCellularInterface.h"

#define UDP 0
#define TCP 1

// SIM pin code goes here
#ifndef MBED_CONF_APP_SIM_PIN_CODE
# define MBED_CONF_APP_SIM_PIN_CODE    "1234"
#endif

#ifndef MBED_CONF_APP_APN
# define MBED_CONF_APP_APN         "internet"
#endif
#ifndef MBED_CONF_APP_USERNAME
# define MBED_CONF_APP_USERNAME    NULL
#endif
#ifndef MBED_CONF_APP_PASSWORD
# define MBED_CONF_APP_PASSWORD    NULL
#endif

// Number of retries /
#define RETRY_COUNT 3



// CellularInterface object
OnboardCellularInterface iface;

// Echo server hostname
const char *host_name = "echo.mbedcloudtesting.com";

// Echo server port (same for TCP and UDP)
const int port = 7;

Mutex PrintMutex;
Thread dot_thread(osPriorityNormal, 512);

#define PRINT_TEXT_LENGTH 128
char print_text[PRINT_TEXT_LENGTH];
void print_function(const char *input_string)
{
    PrintMutex.lock();
    printf("%s", input_string);
    fflush(NULL);
    PrintMutex.unlock();
}

void dot_event()
{

    while (true) {
        wait(4);
        if (!iface.is_connected()) {
            print_function(".");
        } else {
            break;
        }
    }

}


/**
 * Connects to the Cellular Network
 */
nsapi_error_t do_connect()
{
    nsapi_error_t retcode;
    uint8_t retry_counter = 0;

    while (!iface.is_connected()) {

        retcode = iface.connect();
        if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
            print_function("\n\nAuthentication Failure. Exiting application\n");
            return retcode;
        } else if (retcode != NSAPI_ERROR_OK) {
            snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
            print_function(print_text);
            retry_counter++;
            continue;
        } else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
            snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
            print_function(print_text);
            return retcode;
        }

        break;
    }

    print_function("\n\nConnection Established.\n");

    return NSAPI_ERROR_OK;
}

/**
 * Opens a UDP or a TCP socket with the given echo server and performs an echo
 * transaction retrieving current.
 */
nsapi_error_t test_send_recv()
{
    nsapi_size_or_error_t retcode;
#if MBED_CONF_APP_SOCK_TYPE == TCP
    TCPSocket sock;
#else
    UDPSocket sock;
#endif

    retcode = sock.open(&iface);
    if (retcode != NSAPI_ERROR_OK) {
        snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.open() fails, code: %d\n", retcode);
        print_function(print_text);
        return -1;
    }

    SocketAddress sock_addr;
    retcode = iface.gethostbyname(host_name, &sock_addr);
    if (retcode != NSAPI_ERROR_OK) {
        snprintf(print_text, PRINT_TEXT_LENGTH, "Couldn't resolve remote host: %s, code: %d\n", host_name,
               retcode);
        print_function(print_text);
        return -1;
    }

    sock_addr.set_port(port);

    sock.set_timeout(15000);
    int n = 0;
    const char *echo_string = "TEST";
    char recv_buf[4];
#if MBED_CONF_APP_SOCK_TYPE == TCP
    retcode = sock.connect(sock_addr);
    if (retcode < 0) {
        snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.connect() fails, code: %d\n", retcode);
        print_function(print_text);
        return -1;
    } else {
        snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: connected with %s server\n", host_name);
        print_function(print_text);
    }
    retcode = sock.send((void*) echo_string, sizeof(echo_string));
    if (retcode < 0) {
        snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.send() fails, code: %d\n", retcode);
        print_function(print_text);
        return -1;
    } else {
        snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: Sent %d Bytes to %s\n", retcode, host_name);
        print_function(print_text);
    }

    n = sock.recv((void*) recv_buf, sizeof(recv_buf));
#else

    retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
    if (retcode < 0) {
        snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.sendto() fails, code: %d\n", retcode);
        print_function(print_text);
        return -1;
    } else {
        snprintf(print_text, PRINT_TEXT_LENGTH, "UDP: Sent %d Bytes to %s\n", retcode, host_name);
        print_function(print_text);
    }

    n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
#endif

    sock.close();

    if (n > 0) {
        snprintf(print_text, PRINT_TEXT_LENGTH, "Received from echo server %d Bytes\n", n);
        print_function(print_text);
        return 0;
    }

    return -1;
}

int main()
{

    iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
    /* Set Pin code for SIM card */
    iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);

    /* Set network credentials here, e.g., APN*/
    iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);

    print_function("\n\nmbed-os-example-cellular\n");
    print_function("Establishing connection ");
    dot_thread.start(dot_event);

    /* Attempt to connect to a cellular network */
    if (do_connect() == NSAPI_ERROR_OK) {
        nsapi_error_t retcode = test_send_recv();
        if (retcode == NSAPI_ERROR_OK) {
            print_function("\n\nSuccess. Exiting \n\n");
            return 0;
        }
    }

    print_function("\n\nFailure. Exiting \n\n");
    return -1;
}
// EOF

  • 网络套接字 API 参考概述。
  • Cellular 架构。

你可能感兴趣的:(Mbed,OS)