Hellox Operating system Network function Introduction and usage and development guide

Source: Internet
Author: User
Tags network function bssid

Hellox Network function Introduction and usage and development guide

Hellox Network Function Introduction

As an Internet of things operating system, network function is one of the essential functions. By planning, Hellox implemented two different types of TCP/IP protocol stacks, a resource-constrained embedded application that ported the industry's mature LWIP protocol stack. The protocol stack is concise, relatively simple, and it is designed and optimized for the embedded domain, which is very low in hardware resource requirements. Another protocol stack from the BSD operating system protocol stack, for complex network features rich application scenarios, such as home gateways, IoT gateways. In order to adapt to the mechanism of Hellox itself, some changes and optimizations have been made to the BSD protocol stack, of course, which is within the scope permitted by BSD license.

At present, the LWIP protocol stack is mature and stable, and the BSD protocol stack for complex scenes is being ported. Regardless of which protocol stack is used, the same, BSD-compliant socket API is provided, so the protocol stack is transparent to the application. For the network card driver, Hellox itself designed a set of network card driver mechanism, shielding the difference between different IP protocol stacks, so whether the installation is the LWIP protocol stack or BSD protocol stack, the NIC driver is the same.

In this paper, the use and development of network functions of Hellox are briefly described.

Using network features on a virtual machine

The Hellox V1.78 implements a pcnet FastEthernet III network card driver (the corresponding NIC control chip is AMD's am79c973). Most virtual machines support this network card, so there is a wide range of versatility. If you want to experiment with other NIC drivers, you will need to manually write a NIC driver.

For example, in virtual box virtual machine, in the "Settings" screen, select "Network", this time will enumerate four network cards. Select "Nic 1" and configure as shown:


Note that connection mode selects Bridge network adapter. If you choose a different connection type, I have not tried, I do not know the line. Choose Bridge Network card meaning, is to put your PC's physical network card, and virtual machine "network card 1", with two layers of way to connect up. You can imagine VirtualBox virtual an Ethernet switch, and then put your computer's physical network card, as well as the virtual machine's network card 1, are connected to this virtual Ethernet switch.

"Interface Name," Select the physical network card name of your computer. Note that this network card must be able to use the network card, preferably you are currently on the Internet with the network card, if there are multiple network cards.

Click on the "Advanced" option, "Control Chip" line, and select it as shown. If you choose a different control chip, you need to write the corresponding control chip driver. This is a very interesting job, as long as the corresponding chip to the data sheet, basically can be written out soon. If you can successfully write a network card driver, then congratulate you, apply for any communication or embedded development company, categorization malleability can pass. Interested friends can try.

After you click OK, start the virtual machine with the latest version of Hellox. Note that by default, Hellox is getting IP addresses by DHCP, so the physical network card that is associated with the Hellox virtual machine must also obtain the IP address by means of DHCP.

Of course, if the IP address is not obtained by DHCP, it can also be configured manually, referring to the Network commands section below this article.

After you start the virtual machine, you can use the network program to view and diagnose the networks. For example, you can use the Iflist command to view the acquired IP address, and use the ping command to try to check whether the network connection is normal. The following is the result of running on my computer:


Here, a brief description of how the ping command is used to diagnose the network connection is normal. These methods, generally in the "Network Foundation" course will be involved. If you want to test Mcse,ccna and other certificates, these content is a must test content.

First, Ping the loopback address, which is 127.0.0.1. If this address can be ping, the TCP/IP protocol stack is working properly. If the ping is not, then the TCP/IP protocol stack is not up;

If the loopback address is ping, then ping the local address of the network interface, which is the NIC address. In, is 10.129.73.4. If this address can be ping, the NIC has been successfully identified and managed by the TCP/IP protocol stack. If the address is not ping, then the network card and IP protocol stack is not associated;

In the case of the local address ping, and then ping the default gateway, in the, is 10.129.73.1 this address. If this address can be ping, it means that the communication between the computer and the router is normal. If there is a network problem in the back, you can basically exclude the local computer problem. If the ping does not work, it means that the router does not communicate with the normal, this time need to check the network cable and other physical connections.

In the case of a default gateway ping, you can ping the target server. This is when the ping is over, it's all right. If the ping does not work, it is likely the problem with the target server or between the target server and the local router.

Hellox's Network Program usage guide

Hellox implements a network character interface application that can be used to debug networking functions. An abstract Ethernet management Framework (Ethernet framework) is implemented, which implements a standard network driver interface that masks the differences between different network drivers. This way, the driver code is different for different hardware, but it can be seamlessly attached to the Hellox kernel as long as you follow this standard set of interface specifications.

The following is a brief introduction to the V1.78 version of the network debugging program networks, on this basis, a brief introduction of the Hellox Network Driver programming method.

In character shell mode, enter network and return to enter the network application. The program provides some of the following network-related commands:

Scan command

This command is used to scan all available WiFi hotspots, provided that Wi-Fi hardware support is required. This command lists all the AP hotspots that were scanned, as follows:

[Network-view]scan

Available WiFi list:

-----------------------------

00:bssid = 20004e9c, RSSI =, SSID = ' hellox_hgw_ap ', channel = 1

01:bssid = 2000508C, RSSI =, SSID = ' celleden_map1600 ', channel = 6

If you find that the hotspot you want to connect to is not in the list above, you can perform a few more scan commands, and in many cases, a scan cannot scan to all hotspots.

Note that to use the Scan command, you must have a WLAN network card, otherwise it is not valid.

ASSOC command

Assoc is used to associate with a specified WiFi hotspot. Scan only scans for available hotspots, but if you need to connect to a hotspot, you must use the Assoc command. As follows:

[Network-view]assoc Hellox_hgw_ap

The above command is used to connect to a WiFi hotspot named "Hellox_hgw_ap". Note that the WiFi hotspot must be an open unencrypted hotspot because the above command does not specify a connection password.

If you are connecting to an encrypted WiFi hotspot, you can specify the password for the connection using the/k parameter:

[Network-view]assoc hellox_hgw_ap/k0123456789012

followed by a password. Only WEP encryption is currently supported, so the password must be 13 bytes.

Again, the current value supports open unencrypted WiFi hotspots, as well as WEP-based Wi-Fi hotspots, which do not yet support WPA encryption hotspots, so if you want the connection to succeed, you must modify the configuration of the WiFi hotspot, modify it to open unencrypted, or use WEP encryption (the password is set to 13 digits).

Showint command

This command is used to display statistics on all network interfaces in the system, such as the number of messages received, the number of messages sent, etc. The following is a simple output example:

[Network_view]showint

Statistics information for interface ' Marvel_wlan_int ':

Send Frame #: 17

Success Send #: 17

Send bytes size:2946

Receive Frame #: 14

Success recv #: 14

Receive bytes size:1778

The meaning of each output field is self-explanatory.

Iflist command

The ability to enumerate all the network interfaces in the system, their corresponding IP address and other information. Unlike Showint, Iflist displays static information about the network interface (IP address/mask/default gateway, etc.), while Showint shows the dynamic information of the network interface.

Here is an example of an output from iflist:

[Network_view]iflist

--------------------------------------

Inetface Name:ma

IPv4 address:192.168.43.173

IPv4 mask:255.255.255.0

IPv4 gateway:192.168.43.1

Interface mtu:1500

--------------------------------------

Inetface Name:lo

IPv4 address:127.0.0.1

IPv4 mask:255.0.0.0

IPv4 gateway:127.0.0.1

Interface mtu:0

The above shows two network interfaces, the first of which is the WLAN interface, and the name contains only 2 bytes from the beginning.

Setif command

The Setif command modifies the static configuration parameters of the interface. For example, you can set a static IP address for a network interface in the following ways:

Setif marvel_wlan_int/a 192.168.0.100/m255.255.255.0/g 192.168.0.1

Where Marvel_wlan_int is the name of the interface (the Showint command can be displayed), followed by the IP address/subnet mask/default gateway of the interface, respectively. It is important to note that, by default, DHCP is enabled on the interface, attempting to obtain an IP address automatically. Once the static IP address is set by the above command, the DHCP function on that interface is also turned off.

If you want to reopen the DHCP feature, use the following command:

Setif marvel_wlan_int/d Enable

The following commands are used to restart the DHCP functionality on the interface:

Setif marvel_wlan_int/d Enable

The purpose of restarting the DHCP feature is to immediately make a DHCP request on the interface. By default, the DHCP function sends a DHCP request message in exponential Backoff, that is, when the interface is just enabled, a DHCP request is sent, and if no response is received, it is sent again after 2 seconds, then 4 seconds, then 8 seconds ... And so on If you want to re-issue the DHCP request on the interface immediately, use the above command restart.

ping command

This is the most commonly used diagnostic command, followed directly by the IP address. Here is a simple example of output:

[Network_view]ping 192.168.43.1

Ping192.168.43.1 with bytes packet:

[0] Reply from 192.168.43.1,size = 64,time = (ms)

[1] Reply from 192.168.43.1,size = 64,time = (ms)

[2] Reply from 192.168.43.1,size = 64,time = (ms)

Pingstatistics:total send = 3,received = 3,0 loss.

If you want to change the default ping message length, you can add a parameter:

Ping 192.168.43.1/l 1024

The above command is a ping message length of 1024 bytes. By default, three messages are continuously ping and then end. If you want to ping more messages, use the following command:

Ping 192.168.43.1/c 1000

The above command can ping1000 a message. Of course, the L parameter and the C parameter can be used together.

Hellox Network Driver Authoring method

Hellox implements a thread-polling-based Ethernet driver framework that has a thread called Eth_thread that periodically polls the NIC driver (every 100ms) in an attempt to receive a data frame. If a suitable data frame arrives, Eth_thread will submit the data frame to the IP layer for processing.

Therefore, to implement an Ethernet driver, you need to follow the Hellox Ethernet driver framework, specifically, to implement the following functions:

initialization function

The prototype is as follows, this function is called by Hellox when the Ethernet driver is loaded, and is used to complete the initialization function of the hardware. Of course, if initialization is not required, it can be written in the following form:

Static Boolint_init (__ethernet_interface*pint)

{

Returntrue;

}

The initialization of the hardware can also be placed in the driver's entry function, which is mentioned below.

Message sending function

The prototype is as follows:

Static Boolsendframe (__ethernet_interface*pint);

The Ethernet driver framework calls this function when the IP layer attempts to send a message. The data frames sent are already ready at the IP level (including the source MAC address/Destination MAC address, etc.) and stored in a buffer of the pint object (code below), the driver only needs to be sent.

typedef struct tag__ethernet_interface{

Char Ethname[max_eth_name_len +1];

Char Ethmac[eth_mac_len];

Char sendbuff[eth_default_mtu];//sending buffer.

int buffsize;

__eth_interface_state ifstate;

LPVOID Pl3interface;

LPVOID pintextension; Privateinformation.

......

}__ethernet_interface;

Where Sendbuff is the buffer that holds the data frame to be sent, buffsize is the length of the data frame and must be less than ETH_DEFAULT_MTU (1500).

In the Sendframe function, you only need to manipulate the hardware, send the contents of the Sendbuff to the physical network, and then return.

Data frame receive function

The function is prototyped as follows, and is periodically called by the Hellox Ethernet management framework to determine if there is a data frame arriving:

Static Structpbuf*recvframe (__ethernet_interface* pInt);

In this function, the hardware determines that a data frame arrives, you need to create a pbuf, copy the data frame from the hardware buffer into the PBUF, and return to the PBUF. For example, the following example code:

static struct Pbuf*marvel_recvframe (__ethernet_interface*pint)

{

struct Eth_packet *rx_pkt =&pgmarvel_priv->rx_pkt;

struct Pbuf *p, *q;

U16 len = 0;

int l = 0;

char *buffer = NULL;

P=null;

/*obtain the size of the packet and put it into the "Len" variable. */

Len= lbs_rev_pkt ();

if (Len > 0) {

Buffer= rx_pkt->data;

/*we allocate a PBUF chain ofpbufs from the pool. */

p= Pbuf_alloc (Pbuf_raw, Len,pbuf_pool);

if (P! = NULL) {

for (q = p; Q! = NULL; Q =q->next) {

memcpy ((u8_t*) q->payload, (u8_t*) &buffer[l], Q->len);

L= L + q->len;

}

}

Else

{

}

}

return p;

}

If the hardware determines that no data frame arrives, only null is required.

control functions for specific functions

The prototype is as follows:

Static Booleth_ctrl (__ethernet_interface*pint,dword dwoperation,lpvoid pData);

For some Ethernet specific control functions, such as setting the MTU size, modification rate, WIFI scanning ap/attached AP, etc., through this function. Dwoperation refers to the action required.

If there is no special need, you may not need to implement the function. The recommended implementation is to implement an empty function that returns true only, such as:

Staticboolmarvel_ctrl (__ethernet_interface* pint,dword dwoperation,lpvoid pData)

{

Returntrue;

}

Implementing the Driver Entry function

After implementing the above functions, you also need to implement an Ethernet driver entry function, which is called by the Hellox Ethernet management framework to load the Ethernet driver. In the entry function, you need to do a series of work:

1. Initialize the hardware;

2. Call Addethernetinterface to register the Ethernet interface with the system.

Here is an implementation example:

BOOL marvel_initialize (LPVOID pData)

{

__ethernet_interface*pmarvelint = NULL;

Char Mac[eth_mac_len];

Initialize the hardware, get the MAC address of the hardware, and store it in the Mac array.

Call Addethernetinterface, register the interface.

Pmarvelint= Ethernetmanager.addethernetinterface (

Marvel_eth_name,//Ethernet name, arbitrary string, cannot contain spaces.

&mac[0],//mac address.

NULL,//parameter of the initialization function.

Int_init,//interface initialization function, corresponding to the above Int_init function

Sendframe,//Data frame send function

Recvframe,//Data frame receive function

Eth_ctrl); Control functions.

if (null== pmarvelint)

{

Returnfalse;

}

Returntrue;

}

When calling Addethernetinterface, you need to use the four operation functions implemented above as parameters. After the call succeeds, an Ethernet object pointer is returned, which can be saved for later uninstallation of the Ethernet interface.

Add an entry in the Ethernet drive entry array

The final step is to add an entry in the Ethernet Driver entry function array to tell the operating system that the Ethernet driver exists. When the operating system is initialized, the driver entry function is called and the driver is loaded. The entry function array is in the Network/ethernet/ethentry.c file, and here is an example:

__ethernet_driver_entryethernetdriverentry[]=

{

#ifdef __cfg_net_marvellan

{Marvel_initialize,null},

#endif

Please add your Ethernet driver ' s entryhere.

{Null,null}

};

Where Marvel_initialize is the driver entry function, NULL is the parameter of the entry function and can be any pointer. Note that the first network interface in the array is automatically set to the default network interface, the interface where the default gateway is located, or the interface where the default route is Hellox.

So the Ethernet driver is written to completion. It is recommended that the code for the Ethernet driver be stored in the Driver/stm32 directory. The Ethernet driver can be loaded by recompiling the Hellox.

After the Ethernet driver is loaded successfully, the network program can be consumed for diagnosis and debugging. You can enable code-level debugging if the command-level diagnostics do not identify the problem. Specifically, in the Ethif.h file, turn on the Ethernet Debug Switch (__eth_debug), recompile and load the run, to output more detailed information during the operation of the network.

The code for Hellox V1.78 can be downloaded from github:

Github.com/hellox-project/hellox_os

Have any questions, welcome to join QQ Group discussion: 38467832

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hellox Operating system Network function Introduction and usage and development guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.