当前位置:文档之家› 用于嵌入式微处理器开发的BACnet协议栈源码

用于嵌入式微处理器开发的BACnet协议栈源码

BACstac/32v6.5 Porting Guide for Embedded

Platforms

Document Version:1.0

Cimetrics,Inc.

141Tremont Street,Floor11

BOSTON,MASSACHUSETTS02111

USA

TELEPHONE:+1(617)350-7550

FAX:+1(617)350-7552

E-MAIL:products@https://www.doczj.com/doc/b918430128.html,(sales),support@https://www.doczj.com/doc/b918430128.html,(tech.support)

WEB:https://www.doczj.com/doc/b918430128.html,

BACstac/32v6.5Porting Guide for Embedded Platforms

by Cimetrics,Inc.

Document Version:1.0Edition

Copyright?1999-2012Cimetrics,Inc.

BACstac and BACstac/32are trademarks of Cimetrics Inc.BACnet is a registered trademark of the American Society of Heating,Refrigeration and Air-Conditioning Engineers,Inc.(ASHRAE).LonTalk is a trademark of Echelon Corp.All other trademarks are owned by their respective companies.

Table of Contents

1.Introduction (1)

2.Source Code (2)

Overview (2)

arch (2)

apil (2)

examples (2)

include (3)

test (3)

tsml (3)

dl (3)

nl (3)

osa (3)

oslib (3)

utils (4)

unit_test (4)

Build variables for make?les (4)

Steps to Build BACstac/32on your Platform (8)

3.How to Write the OSA (10)

Terminology (10)

OSA Initialization and Deinitialization (11)

OSAInit() (12)

OSAClose() (12)

Threads (13)

General description (13)

OSACreateThread() (13)

OSATerminateThread() (14)

OSAWaitForThread() (14)

Semaphores (15)

General description (15)

OSAInitSemaphore() (15)

OSAWaitForSemaphore() (15)

OSAReleaseSemaphore() (16)

OSADestroySemaphore() (16)

Recursive mutexes (16)

OSAInitRecursiveMutex() (16)

OSALockRecursiveMutex() (17)

OSAUnlockRecursiveMutex() (17)

OSADestroyRecursiveMutex() (17)

Fast mutexes (18)

OSAInitFastMutex() (18)

OSALockFastMutex() (18)

OSAUnlockFastMutex() (19)

OSADestroyFastMutex() (19)

Atomic variables (19)

General description (19)

OSAAtomicGet() (20)

OSAAtomicSet() (20)

OSAAtomicInc() (20)

OSAAtomicIncTest() (20)

OSAAtomicDec() (20)

OSAAtomicDecTest() (21)

OSAAtomicAdd() (21)

OSAAtomicSub() (21)

Dynamic memory (21)

General description (22)

OSAAlloc() (22)

OSAFree() (22)

Time (22)

General description (23)

OSAGetTickCount() (23)

OSASleep() (23)

System logging utilities (23)

General description (24)

OSALogMessage() (24)

Byte ordering and?oating point format utilities (25)

General description (25)

Cardinal byte order (25)

IEEE-754compliance (26)

OSAFPClassify() (26)

OSAGenerateSpecFPValue() (27)

Debugging support utilities (27)

General description (27)

OSAPanic() (27)

4.How to Write the Con?guration Module (29)

Using Con?guration Structures (29)

OSA Con?guration (29)

Tuning Parameters (30)

5.How to Add a Data Link Layer (31)

Abbreviations (31)

What is a sub-DL? (32)

How Does the Sub-DL Operate? (34)

System resources (35)

Interface with hardware (35)

Sub-DL?DL-common interface (36)

Data Message (36)

Data Message Interface (36)

List of sub-DLs (42)

Sub-DL operations (42)

Operation Init (43)

Operation Close (43)

Operation Accept (44)

Operation Bind (44)

Operation Unbind (46)

Operation SendTo (46)

Operations Connect and Disconnect (47)

Operation Ioctl (47)

Support functions (48)

Null sub-DL example (49)

Porting Ethernet Sub-DL (52)

Porting B/IP(Addendum A)Sub-DL (60)

Porting PTP Sub-DL (64)

Porting ARCNET sub-DL (68)

Porting MS/TP Sub-DL (81)

6.Building Unit Tests&Examples (85)

Building the Test Programs (85)

OSA Tests (85)

Data Link Layer Tests (86)

Network Layer Test (86)

Example Server Application-FEEDBACK (86)

Example Client Application-DUMPALL (87)

List of Figures

5-1.Simpli?ed BACstac Architecture (33)

5-2.Internal Sub-DL Structure (34)

5-3.Sub-DL and Its Environment (34)

5-4.Simpli?ed Sub-DL?DL Interface Structure (36)

5-5.PTP Sub-DL Operation in the“Null-modem”Mode (64)

5-6.PTP Sub-DL Operation in the“Dial-up”Mode (65)

Chapter1.Introduction

Thank you for your interest in Cimetrics’BACstac?software.The BACstac Networking Software provides you with the ability to create embedded controls which use ASHRAE’s BACnet?protocol for communication.Implementa-tions of the BACstac are currently available for32-bit versions of Windows XP,Windows Vista,Windows7,Windows Server2003,Windows Server2008,Windows Server2008R2,Linux2.6,and some32-bit embedded platforms.

This document describes how to port the BACstac/32source code to your platform.For a description of how to construct applications using the BACstac software,please refer to the BACstac Users'Guide.For a description of the data types and API routines available in the BACstac software,please refer to the BACstac Programmers'Reference.

In order to use this software you will need the following:

?Embedded32-bit platform

?32-bit RTOS with network interface drivers

?ANSI C compiler

?Make utility

?The BACnet Standard(ANSI/ASHRAE135-2010or later)

BACstac/32is shipped with all source code and make?les needed to build it as monolith Routing BACstac Edition for Win32or Linux platform with the following data link included:BACnet/IP,Point-To-Point,Ethernet.To build BACstac on Win32platform you can use either Borland C++5.5or Microsoft Visual C++2003,2005,or2008. If you choose Linux option,you will need Linux with glibc2.1or better and gcc2.95or higher with all set of standard developer tools.Ability to run the original BACstac version in the same network with your target platform will signi?cant reduce porting time at the testing stage.BACnet Protocol Analyzer(BAS-o-matic)is also very helpful with testing.It not only allows to see sent BACnet requests and responses,but also to send modi?ed packets if necessary to verify the implementation reaction.

This document contains the following sections to help you port the BACstac to your platform:

1.an overview of the source code layout of the BACstac

2.a description of the modules that you will need to rewrite for your platform

3.recommended steps in your porting project,using the BACstac module and library test applications

Chapter2.Source Code

The root directory of the source tree includes directories for modules and projects and also arch directory.

The modules are libraries(such as osa,nl,etc),which can be shared by different projects.

The source code for BACstac32includes only one project:BACstac.It contains all BACstac speci?c modules,such as apil,tsml,etc.

The arch directory is the place where all platform speci?c?les are located.Each platform has its own subdirectory inside of it.

Overview

The source code has the following directories:

arch

This directory contains all system dependent code,make?les and everything else what is platform speci?c.The di-rectory has two subdirectories:linux and win32,where system dependent?les for the corresponding platform are located.The structure of this subdirectories is same as the root directory but without arch.

Make?les to build some module for speci?c project are placed under directory with the project name.For ex-ample,main Make?le to build BACstac for Win32platform is placed in arch\win32\BACstac directory.The project directory has a few subdirectories containing make?les used to build some module for BACstac project. The apil module,which can be build as client,server,or gateway library,has three corresponding subdirecto-ries:cli,srv,and gtw inside of arch\win32\BACstac\apil\api,arch\win32\BACstac\apil\objects,and arch\win32\BACstac\apil\services directories.

apil

API Layer(Upper Application Layer)includes subdirectories for Access Routines,BACstac API,Parser(ASN syntax validator),Coder(encoder/decoder),Objects(object database),CBNOBJ(the CBNOBJ utility and examples of.TPI ?les for some devices),and Services(Encoder,Request Dispatch,Hook Dispatch and Default Actions)containing the corresponding source code.

CBNOBJ is Cimetrics BACnet Object Compiler used to create device templates for use in BACstac server and gateway applications which use the BACstac object database and default actions.CBNOBJ parses a.TPI?le(Text PICS-a standard speci?ed in ASHRAE Standard135.1:"Method of Test for Conformance to BACnet)and creates a template of a device with its objects,properties and initial values.

BACstac API,Objects,and Services can be compiled as a client,service,or getaway library.All other are shared are same for all three types of APIL.

examples

This directory contains25examples of BACstac application.Each of them has its own directory with corresponding name.The utils subdirectory includes source code of library shared by all BACstac examples.

There are three types of examples:client,server,and gateway.Each of them must be linked to the corresponding BACstac version of apil library.Besides,examples use OSL and syslib libraries.

For more information see examples\readme.txt

include

This directory contains header?les used by different BACstac modules.The api subdirectory includes BACstac API available for BACstac applications.

test

The directory contains collection of integral tests for BACstac.

tsml

The Transaction State Machine Layer(Lower Application Layer)implements the transmission and reception protocol for BACnet APDU.The overview of it is given in clause5.3of ANSI/ASHRAE Standard135-2010or later and formal description in clause5.4.Besides,the TSM layer for the workstation version implements application multiplexing to allow multiple BACstac applications to work at the same time on one computer.

dl

Data Link Layer.Contains common code for multiplexing ports and data link layer types.Includes subdirectories for the BACnet/IP and PTP(Point-To-Point)data link layers.Normally code for a data link layer is non-portable since it interfaces to particular device drivers on a platform.The BACnet/IP data link layer code is reasonably portable and therefore it is often a good choice for starting the porting process.The PTP data link layer code is written in terms of a serial port abstraction module(implemented in arch\win32\dl\ptp\comm\comm.c and arch/linux/dl/ptp/comm/comm.c).The PTP code is also reasonably portable.

nl

Network Layer.Contains common code for both Routing(gateway)and Nonrouting(vanilla)network layer imple-mentations.The Routing NL should be used for controls which will support multiple BACnet network interfaces(for example,to link a building backbone to?eld network),or which will support PTP,or which will wish to use a virtual network to implement an embedded gateway.The Vanilla NL should be used for controls which have a single net-work interface.The Routing NL in BACstac/32is based on the same code found in Cimetrics Routers and Tunnelling Routers.

osa

Operating System Abstraction Layer.Contains a document describing Application Program Interface(API)of the OSA and unit tests for its functions.(The non-portable code implementing the OSA on Win32and Linux platforms is located in arch\win32\osa and arch/linux/osa directories.)

oslib

Operating System Library.This is scaledown version of OSA used by examples.Its API is a subset of OSA API,and its functions has OSL pre?x instead of OSA.

utils

The Utilities Module is a library used by many other modules,which is built on the OSA primitives to provide general purpose data and structure(such as thread-safe buffer pools)and operation on them.

unit_test

There is no such as directory in the root of the source tree but some modules(OSA,DL,NL)have such subdirectory. It contains a set of unit tests for respective module intended to test the module implementation with minimal using of other libraries.See Chapter6for details.

Build variables for make?les

The BACstac source code comes with make?les for Borland C/C++and Microsoft Visual C/C++for Win32platform, and GNU make?les for Linux.This make?les can be a start point to create your own make?les or IDE project for your target platform.The supplied make?les contain important information which you may interesting in:

?Build con?guration options

?the list of?les to build

?C preprocessor macro depending on building con?guration

The building con?guration option for Win32platform are set in the?le arch\win32\BACstac\Autoconf.make. Many of them are for debug purposes,others are useful in different ways.Below is their list:

CONFIG_DEBUG=0

Includes BACstac debug code,such as ASSERTs and tracing.If enabled then the DEBUG preprocessor macro is de?ned,otherwise the NDEBUG preprocessor macro is de?ned.Allowed values:0(disable),1(enable). CONFIG_DEBUG_INFO=0

Includes compiler debugging information(source line numbers,etc)during compilation and linking.Allowed values:0(disable),1(enable).

CONFIG_STACK_ARCH=SINGLE_PROCESS

Protocol stack architecture:either"monolithic"or"single-process"(the TSML and AL run in the same process), or"client/server"or"daemon"(the AL(s)and TSML run in separate processes).For now this option also de-termines presence of the user multiplexor,which is only included in multiprocessor build.In the future these options might be controlled independently.Allowed values:SINGLE_PROCESS,MULTI_PROCESS.De?nes

BACSTAC_MULTIPLEXOR and BACSTAC_DAEMON_PROCESS preprocessor macros.BACstac/32is pro-vided to work in single process con?guration only.

CONFIG_DL_NIC,CONFIG_DL_BIP,CONFIG_DL_PTP=n,y,y

Datalink technologies:de?ne one or more sub-DLs to be included.In the data link layer code,there is a jump table whose entries are controlled by these settings.DL_NIC(Ethernet),DL_BIP(BACnet/IP),DL_PTP(Point-To-Point).Allowed values:y,n.De?nes BACSTAC_DL_NIC,BACSTAC_DL_BIP,BACSTAC_DL_PTP pre-processor macros.

CONFIG_GENERATE_MAP=0

MAP option:turns.MAP?le generation by the linker on or off.Speci?c to the Borland and Microsoft make?les.

Allowed values:0(off),1(on).

CONFIG_RC_STRICT=0

Resource leak checking:turns on internal checks of resource usage(memory,system objects,PDUs)(ignored in non-DEBUG build).Allowed values:0(disable),1(enable).De?nes RC_STRICT preprocessor macro.

The building con?guration option for Linux platform are described below.Many of them are for debug propose only.

Cross compile pre?x(CROSS_COMPILE)

Speci?es the pre?x used for gcc and related bin-utils executables used during compilation.Leave it empty for a non-cross build.

Routing NL(CONFIG_NL_ROUTER)

If you want to build a routing version of the Network layer,say Y.

Establish remote PTP connection(CONFIG_NL_CONNECT)

If you want to enable establishing remote PTP connection(i.e.sending Establish-Connection-To-Network to routers that responded with I-Could-Be-Router-To-Network),say Y.If you are not sure,say Y here.

Debug Routing(CONFIG_DEBUG_ROUTING)

If you want to enable debug log information about all changes to the routing table,say Y.

Support Segmentation(CONFIG_SEGMENTATION)

Enable support for sending or receiving segmented APDU messages

BACstac daemon(BACSTAC_DAEMON_PROCESS)

BACstac can be compiled either as"monolithic"(a single process that contains the whole BACnet stack and user application),or"client/server"(a.k.a."daemon"),where one or more BACstac based programs may use the same BACstac stack simultaneously.

Enable ioctl(CONFIG_IOCTL)

If you want to enable ioctl functionality in BACstac,say Y.

Build shared libraries(CONFIG_BUILD_SO)

If you want to build BACstac shared libraries instead of static ones,say Y.

ARCNET=Clause8(CONFIG_ARCNET)

If you want BACnet over"raw"ARCNET link,say Y.Don’t forget to enable"arc0s"interface in the Linux kernel.

Ethernet=Clause7(CONFIG_ETHERNET)

If you want BACnet over"raw"Ethernet link,say Y.

Tunneled IP(T/IP)=Annex H(CONFIG_ANNEXH)

If you want BACnet over IP tunnel as described in Annex H,say Y.

BACnet over IP(B/IP)=Annex J(CONFIG_BIP)

If you want BACnet over IP,say Y.

Con?g BBMD(CONFIG_BBMD)

If you want to enable BBMD functionality in this device,say Y.

Don’t bind to the physical interface(CONFIG_BIP_NO_BINDTODEVICE)

This disables binding a socket used by B/IP data link to a particular device(like"eth0"),which is speci?ed in the con?guration.If a socket is bound to an interface,only packets received from that particular interface are processed by the socket.Binding to the device requires the CAP_NET_RAW privilege(which only’root’has by default).If binding to the device is disabled then BACstac creates two UDP sockets and bound them to the device IP address and network broadcast mask correspondingly.

Note:Binding to the device is the default mode for all versions of BACstac for Linux,but it may not work correctly in present two or more cards connected to the same network(e.g.Ethernet and Wi-Fi).Also,it blocks all traf?c from other interfaces,which may be not desirable when BACtac is used as a BBMD for devices that are located on another network.

Point to Point(PTP)=Clause10(CONFIG_PTP)

If you want BACnet over Point-To-Point protocol as described in Chapter10of the BACnet standard,say Y. Null-network(B/Null)(CONFIG_NULL)

Null-network is extension to the ANSI/ASHRAE135-1995and its subsequent annexes.Null-network interface does nothing except assigns a network number and MAC address to the BACnet device.All messages which are sent to the null-network are quietly disappeared;the is no incoming messages arrived from null-network.

However,null-net sub-DL can be useful if you want to make BACnet device which is connected to the world via PTP or T/IP only(neither PTP nor T/IP interface has no associated network number).It is recommended to say Y.

Enable debug info in binary(CONFIG_DEBUG_INFO)

In other words,pass-g switch to the C compiler or not.If you are a developer,you may say Y here.Otherwise it is recommended to say N.

Optimization(CONFIG_OPT_XXX)

This option speci?ed the desired level of optimization:

?"None"-Do not optimize.

?"Base"-Perform all supported optimizations that do not involve a space-speed trade-off.(-O2option for gcc)?"Speed"-It turns on all optimizations speci?ed by"Base"and also turns on some additional optimization that can make the code faster but may signi?cantly increase the code size.(-O3option for gcc)

?"Size"-Optimize for size(-Os option for gcc)

Omit frame pointer(CONFIG_OMIT_FRAME_PTR)

Don’t keep the frame pointer in a register for functions that don’t need one.This avoids the instructions to save, set up and restore frame pointers;it also makes an extra register available in many functions.

Enable debugging code(CONFIG_DEBUG)

This option enables internal checks and Assert in BACstac.It is necessary to enable other debugging functionality such as debug tracing.Due to additional checks,BACstac may work considerably slower when this option is set. Initilize allocated memory(CONFIG_FILL_MEMORY)

All allocated memory is initialized with0xCD byte.It may be useful to detect the use of initialized memory inside of BACstac,but it is less reliable than using specialized tools as’valgrind’and is not compatible with them.

Debug IPC buffers(CONFIG_DEBUG_IPCBUF)

This option allows additional expensive validation of IPC buffers to detect corruption in structures used to manage them.It can be used only with CONFIG_DEBUG.

Alloc save callers(CONFIG_SA VE_CALLERS)

If you try to?nd a memory leak inside of BACstac,this option allows to save stack addresses at the moment when memory is allocated.This option is only supported on x86architecture and may be incompatible with optimization.

Enable tracing(this produce while syslog)(CONFIG_DEBUG_TRACE)

If you want to see log messages about each packet received or transmitted and many other events(such as byte transmission in PTP sub-DL),say Y.

IMPORTANT:This trace may?ood the system log with many cryptic messages that is impossible to understand without the studying the source code.This log is not intended for normal use.

Trace OSA calls(CONFIG_TRACE_OSA_CALL)

If you want to trace all OSA calls inside of BACstac,say Y.It may be helpful with debugging,but the log generated by this option may be huge.

Suppress tracing for DL(CONFIG_DL_NO_TRACE)

If you isn’t BACstac(especially,MS/TP)developer,or you want to know nothing about what data link layer does in some details,say Y here.

Suppress tracing for T/IP(CONFIG_ANNEXH_NO_TRACE)

If you isn’t BACstac developer,or you don’t need to know what T/TP ports do in details,say Y here.

Suppress tracing for PTP(CONFIG_PTP_NO_TRACE)

If you isn’t BACstac developer,or you don’t need to see PTP?nite state machines transitions,say Y here.

Pro?ling(CONFIG_GPROF)

Generate extra code to write pro?le information suitable for the analysis program’gprof’.During execution BACstac records how many times each branch and call is executed and how many times it is taken or returns.At exist,all collected data is written to a?le called’AUXNAME.gcda’for each source?le.

NOTES:-This option is necessary to collect coverage information.-This option is not compatible with omitting frame pointer.

Test coverage(CONFIG_GCOV)

Produce a?le that the’gcov’can use to show program coverage.

Wait synchronizer statistics(CONFIG_WAIT_STAT)

Collect statistics about different synchronizers that caused the current thread to suspend.This statistics is useful because it shows the number of involuntarily context switches caused by each synchronizer.

This option is currently incompatible with tracing OSA calls.

Con?g SMP support(CONFIG_SMP)

If you want to run BACstac on SMP or multi-core system,say Y.If you say N here,BACstac may work marginally faster on uniprocessor systems but will get broken in subtle way when it runs on SMP.If you are not sure,say Y here.

OSA Log identi?cator(OSA_LOG_IDENT)

The string prepended to every system log message.The default value is’bacstac’.

PID?le(CONFIG_PID_FILE)

The location of PID?le created by the BACstac service.

Steps to Build BACstac/32on your Platform

To complete a port of BACstac/32to your platform,the following steps are recommended:

?Write the OSA

?Write the con?guration subsystem(OSA or Utils)

?Unit Test the OSA

?Unit Test the OSA+Utils+DL

?Unit Test the OSA+Utils+DL+NL

?Build the full library(OSA+Utils+DL+NL+TSML+APIL)

?Test with Examples or Library tests

The following chapters discuss these steps in more detail.Once the library has been successfully ported to your platform you can proceed with developing your embedded BACnet applications.However,the examples and unit tests will continue to be useful for tuning and troubleshooting.

Chapter3.How to Write the OSA

Writing your own version of the OSA means creating the implementation underneath the module interface de?ned in arch\win32\include\osa.h.The interface consists of methods for a series of abstract RTOS objects(like threads and semaphores).The example?les in the arch\win32\osa subdirectory could provide a starting point,since they organize the implementation by abstract RTOS object type.The following sections describe the required behavior of the routines in that interface.

Note that the current version of the OSA uses a naming convention that is different from the rest of the BACstac. The BACstac6.5refers to both old-style and new-style OSA de?nitions,and a complete transition to the new-style de?nitions will take some time.New OSA ports should use the style de?ned in this chapter but provide a wrapper header?le(arch\win32\include\osa.h)enabling the continued use of the old style. Terminology

BACnet

Building Automation and Control network protocol,ANSI/ASHRAE Standard135-2010or later.

BACstac

A BACnet protocol stack implementation by Cimetrics,Inc.?.

Event

A synchronization primitive.There are W AIT and SIGNAL operations de?ned on events.The semantics of events

is identical to that of binary semaphores.

Handle

An opaque reference to an object(thread,semaphore,mutex and so on).It can be passed into methods,but cannot be used to access the object directly.

Mutex

MUTual EXclusion device.There are recursive and non-recursive mutexes.The OSA deals only with recur-sive mutexes.The LOCK(WAIT)and UNLOCK(RELEASE)operations are de?ned on mutexes.Unlike a semaphore,a recursive mutex doesn’t block the thread which owns it.A mutex can only be unlocked by the thread which has locked it(some implementations may allow a thread to unlock a mutex which is owned by another thread but this behavior isn’t a mutex property).The mutex’s main role is to synchronize access to a shared resource(or a critical region of code).

OSA

Operating System Abstraction Layer.It de?nes a platform-independent API for system services for the BACstac.

Ideally,only the OSA implementation(and data link layers)needs to be rewritten to port BACstac to a new platform.

Process

A unit(atom)of system resources consumption.For every existing process an OS handles allocation,mainte-

nance and deallocation of resources(CPU,memory,?les,etc.),protection from undesired intervention by other processes(optional),and possibly other services.All programs(either user or system,single-threaded or multi-threaded)run in context of a process.A process may or may not share address space with other processes in the system;this depends on particular hardware and OS.

Semaphore

A semaphore is a basic synchronization primitive.In general,every semaphore holds an integer counter which

may take on values in the range from0to some positive value(the semaphore’s limit).There are semaphores of two kinds:binary(limit is equal to the value1)and general(non-binary,limit is greater than1).INIT,W AIT and RELEASE operations are de?ned on semaphores.The semaphore’s main role in BACstac is as a counter(to count messages in an interface queue,for instance).It is not used to synchronize access to shared data;a mutex is used for that purpose instead.Unlike a mutex(recursive),there is no notion of"semaphore owner".This means that any thread will block on a semaphore whose count is equal to zero.

Thread

A minimal execution unit from the system perspective.A thread shares an address space with other threads of the

same process.A process which has only one thread is called single-threaded(also known as regular process);a process which has more than one thread is called multithreaded.There is an essential difference between threads and processes that share an address space:An OS treats all threads of the same process as a single system resource consumer.However,sometimes threads are implemented as processes having a common address space,possibly with certain restrictions(LinuxThreads,for example).Sometimes,threads are called"lightweight processes". Timer

A timer counts a time interval.When the interval expires,the timer signals this event using some method.A timer

always counts real(astronomic)time independently of other timers and threads.

Unpredictable results(unde?ned behavior)

In this case the OSA port developer should make OSA implementation as robust as possible.The BACstac

(i.e.OSA user)should assume nothing about unde?ned OSA behavior and avoid conditions that may cause

unpredictable results.

OSA Initialization and Deinitialization

OSAInit()

OSAFastMutex*OSAInit(void);

OSAInit does all the necessary initialization to bring OSA into action.On success,it returns a pointer to the fast mutex that can be used to perform initialization of the current component atomically.If the function fails,it returns NULL.

OSAInit must be called before any other OSA function.OSAInit may be called more than once.If?rst call to OSAInit has been successful,each subsequent OSAInit call does nothing but increment an internal reference counter.

A matching call to OSAClose must be made for each successful OSAInit call.

If OSAInit returns NULL,it is recommended that the application terminates itself as soon as possible because OSAInit may put the program into an unstable state(for example,an interval timer may generate signals that will not be handled).

OSA initialization scope is one process.Every process that uses OSA must call OSAInit/OSAClose functions.After a successful call to OSAInit OSA may be used by any number of threads within the process.

OSAClose()

bool OSAClose(void);

OSAClose function terminates use of the OSA.It returns TRUE on success and FALSE on error.All internal resources allocated by OSAInit call are freed.OSAClose must be called before a process terminates.For each OSAInit call a corresponding OSAClose call must be made.If OSAClose is called more times then OSAInit the result will be unpredictable.

IMPORTANT:All thread created by OSACreateThread function call must be terminated before calling OSAClose; an attempt to call OSAClose before terminating all OSA-created threads may cause unpredictable results.

If OSAClose is not called an appropriate number of times before the process terminates,or if OSAClose returns FALSE,some systemwide resources(such as shared memory,semaphores,mutexes)may not be freed correctly and thus wasted.It is possible to initialize and reinitialize OSA more than once:

int RunFoo()

{

static int init_cnt=0;

OSAFastMutex*mut=OSAInit();

if(!mut)

return EXIT_FAILURE;

OSALockFastMutex(mut);

if(init_cnt++==0){

//it is the first instance

//initialize global resource here

}

OSAUnlockFastMutex(mut);

//start normal work here

//....

//deinitialize OSA

OSALockFastMutex(mut);

if(--init_cnt==0){

//it is the last instance

//destroy global resources here

}

OSAUnlockFastMutex(mut);

return OSAClose()?EXIT_SUCCESS:EXIT_FAILURE;

}

OSA initialization scope is one process.Every process that uses OSA must call OSAInit/OSAClose functions.After a successful call to OSAInit OSA may be used by any number of threads within the process.

Threads

General description

A thread is represented by a handle.Various data types used by the thread object are:

typedef enum

{

osaWaitOK,

osaWaitTimeout,

osaWaitFailure

}

OSAWaitStatus;

typedef void(*OSAThreadProc)(void*);

typedef...OSAInterval;//unsigned integer32-bit or more

typedef...OSAThreadHandle;

#define OSA_INVALID_THREAD...//invalid thread handle

#define OSA_INFINITE...//wait infinitely

OSAThreadHandle OSACreateThread(OSAThreadProc pThreadEntry,void*pThreadParam);

bool OSATerminateThread(OSAThreadHandle hThread);

OSAWaitStatus OSAWaitForThread(OSAThreadHandle hThread,OSAInterval timeout); OSACreateThread()

OSAThreadHandle OSACreateThread(

OSAThreadProc pThreadEntry,

void*pThreadParam);

The OSACreateThread function creates a new thread within the virtual address space of the calling process.

The new thread executes the function pThreadEntry,passing it pThreadParam as an argument.

It is unde?ned whether a newly created thread begins to execute before OSACreateThread returns.A thread can terminate itself only by returning from the pThreadEntry function.A thread may also be terminated by another thread through OSATerminateThread call.A thread cannot return an exit code.

On success,OSACreateThread returns a handle to the newly created thread.The handle is unique within the process. On error,an invalid thread handle OSA_INV ALID_THREAD is returned.

OSATerminateThread()

bool OSATerminateThread(

OSAThreadHandle hThread);

The OSATerminateThread function terminates the thread denoted by hThread(if it is still running)and frees its handle.If there are threads waiting for this thread to terminate,all of them will be awakened.A thread cannot terminate itself by calling OSATerminateThread;an attempt to do so may cause unpredictable results.

TerminateThread does is not guaranteed to free any all resources such as mutex,etc.correctly for a running thread. Therefore,it is a dangerous function that should only be used for a running thread in the most extreme cases.

On success,OSATerminateThread returns TRUE,otherwise FALSE.Successful call of OSATerminateThread function terminates the thread,which invalidates handle to it.

OSAWaitForThread()

OSAWaitStatus OSAWaitForThread(

OSAThreadHandle hThread,

OSAInterval timeout);

The OSAWaitForThread function returns when of the following occurs:

?The speci?ed thread terminated

?The time-out interval elapsed

The?rst argument hThread is a handle to the thread.The second argument timeout speci?es the time-out interval, in milliseconds.OSAWaitForThread returns osaWaitTimeout if the thread doesn’t terminate within the speci?ed timeout.If hThread is an invalid handle,OSAWaitForThread returns osaWaitFailure immediately.

If timeout equals OSA_INFINITE,the wait duration is unlimited.It is guaranteed that the value of OSA_INFINITE is greater than any timeout value,which can be represented by type OSAInterval.

The OSAWaitForThread function may not be called for a handle closed by OSATerminateThread call.

This routine is typically used to synchronize events during shutdown.

分析BACnet协议栈1

从现在开始分析BACnet协议栈了,版本号是bacnet-stack-0.7.1。目录是bacnet-stack-0.7.1\ports\linux\rs485.c rs485.c文件主要要解决在物理层发送和接收数据的作用。不同的开发板需要移植该文件。 #include #include #include #include #include #include #include /* Linux includes */ #include #include #include #include #include #include /* Local includes */ #include "mstp.h" #include "rs485.h" #include "fifo.h" #include #include /* Posix serial programming reference: https://www.doczj.com/doc/b918430128.html,/~mike/serial/serial.html */ /* Use ionice wrapper to improve serial performance: $ sudo ionice -c 1 -n 0 ./bin/bacserv 12345 */ /* handle returned from open() */ static int RS485_Handle = -1; /* baudrate settings are defined in , which is included by */ static unsigned int RS485_Baud = B38400;//波特率选择38400 bps /* serial port name, /dev/ttyS0, /dev/ttyUSB0 for USB->RS485 from B&B Electronics USOPTL4 */ static char *RS485_Port_Name = "/dev/ttyUSB0"; /*系统默认是通过USB转485的,根据需要设置,若你的开发板用485接口,则用static char *RS485_Port_Name = "/dev/ttyS0";代替*/ /* some terminal I/O have RS-485 specific functionality */

Xmodem协议详解以及源代码剖析

研究 Xmodem 协议必看的 11个问题 Xmodem 协议作为串口数据传输主要的方式之一,恐怕只有做过 bootloader 的才有机会接触一下, 网上有关该协议的内容要么是英语要么讲解不详细。笔者以前写 bootloader 时研究过 1k-Xmodem ,参考了不少相关资料。这里和大家交流一下我对 Xmodem 的理解,多多指教! 1. Xmodem 协议是什么? XMODEM协议是一种串口通信中广泛用到的异步文件传输协议。分为标准Xmodem 和 1k-Xmodem 两种,前者以 128字节块的形式传输数据,后者字节块为 1k 即 1024字节,并且每个块都使用一个校验和过程来进行错误检测。在校验过程中如果接收方关于一个块的校验和与它在发送方的校验和相同时,接收方就向发送方发送一个确认字节 (ACK。由于 Xmodem 需要对每个块都进行认可, 这将导致性能有所下降, 特别是延时比较长的场合, 这种协议显得效率更低。 除了 Xmodem ,还有 Ymodem , Zmodem 协议。他们的协议内容和 Xmodem 类似,不同的是 Ymodem 允许批处理文件传输,效率更高; Zmodem 则是改进的了Xmodem ,它只需要对损坏的块进行重发,其它正确的块不需要发送确认字节。减少了通信量。 2. Xmodem 协议相关控制字符 SOH 0x01 STX 0x02 EOT 0x04 ACK 0x06 NAK 0x15

CAN 0x18 CTRLZ 0x1A 3.标准 Xmodem 协议(每个数据包含有 128字节数据帧格式 _______________________________________________________________ | SOH | 信息包序号 | 信息包序号的补码 | 数据区段 | 校验和 | |_____|____________|___________________|__________|____________| 4. 1k-Xmodem (每个数据包含有 1024字节数据帧格式 _______________________________________________________________ | STX | 信息包序号 | 信息包序号的补码 | 数据区段 | 校验和 | |_____|____________|___________________|__________|____________| 5.数据包说明 对于标准 Xmodem 协议来说,如果传送的文件不是 128的整数倍,那么最后一个数据包的有效内容肯定小于帧长,不足的部分需要用 CTRL- Z(0x1A来填充。这里可能有人会问,如果我传送的是 bootloader 工程生成的 .bin 文件, mcu 收到后遇到0x1A 字符会怎么处理?其实如果传送的是文本文件,那么接收方对于接收的内容是很容易识别的,因为 CTRL-Z 不是前 128个 ascii 码, 不是通用可见字符, 如果是二进制文件, mcu 其实也不会把它当作代码来执行。哪怕是 excel 文件等,由于其内部会有些结构表示各个字段长度等,所以不会读取多余的填充字符。否则 Xmodem太弱了。对于 1k-Xmodem ,同上理。 6.如何启动传输?

LWIP协议栈的分析和设计

---《计算机网络与控制》论文 LWIP协议栈的分析

摘要 近些年来,随着互联网和通讯技术的迅猛发展,除了计算机之外,大量的嵌入式设备也需求接入网络。目前,互联网中使用的通讯协议基本是TCP/IP协议族,可运行于不同的网络上,本文研究的就是嵌入式TCP/IP协议栈LWIP。文章首先分析了LWIP的整体结构和协议栈的实现,再介绍协议栈的内存管理,最后讲解协议栈应用程序接口。 关键词: 嵌入式系统;协议;LWIP;以太网 Abstract With the rapid development of internet and communication technology, Not only computers but also embeded equipments are need to connect networks. At present, the basic communication protocol using in internet is TCP/IP, it can run in different network. This paper analyses the Light-Weight TCP/IP. The process model of a protocol implementation and processing of every layer are described first, and then gives the detailed management of Buffer and memory. At last, a reference lwIP API is given. Key words: Embedded System, Protocol, Light weight TCP/IP,Ethernet 引言

Zigbee协议栈原理基础

1Zigbee协议栈相关概念 1.1近距离通信技术比较: 近距离无线通信技术有wifi、蓝牙、红外、zigbee,在无线传感网络中需求的网络通信恰是近距离需求的,故,四者均可用做无线传感网络的通信技术。而,其中(1)红外(infrared):能够包含的信息过少;频率低波衍射性不好只能视距通信;要求位置固定;点对点传输无法组网。(2)蓝牙(bluetooth):可移动,手机支持;通信距离10m;芯片价格贵;高功耗(3)wifi:高带宽;覆盖半径100m;高功耗;不能自组网;(4)zigbee:价格便宜;低功耗;自组网规模大。?????WSN中zigbee通信技术是最佳方案,但它连接公网需要有专门的网关转换→进一步学习stm32。 1.2协议栈 协议栈是网络中各层协议的总和,其形象的反映了一个网络中文件传输的过程:由上层协议到底层协议,再由底层协议到上层协议。 1.2.1Zigbee协议规范与zigbee协议栈 Zigbee各层协议中物理层(phy)、介质控制层(mac)规范由IEEE802.15.4规定,网络层(NWK)、应用层(apl)规范由zigbee联盟推出。Zigbee联盟推出的整套zigbee规范:2005年第一版ZigBeeSpecificationV1.0,zigbee2006,zigbee2007、zigbeepro zigbee协议栈:很多公司都有自主研发的协议栈,如TI公司的:RemoTI,Z-Stack,SimpliciTI、freakz、msstatePAN 等。 1.2.2z-stack协议栈与zigbee协议栈 z-stack协议栈与zigbee协议栈的关系:z-stack是zigbee协议栈的一种具体实现,或者说是TI公司读懂了zigbee 协议栈,自己用C语言编写了一个软件—---z-stack,是由全球几千名工程师共同开发的。ZStack-CC2530-2.3.1-1.4.0软件可与TI的SmartRF05平台协同工作,该平台包括MSP430超低功耗微控制器(MCU)、CC2520RF收发器以及CC2591距离扩展器,通信连接距离可达数公里。 Z-Stack中的很多关键的代码是以库文件的形式给出来,也就是我们只能用它们,而看不到它们的具体的实现。其中核心部分的代码都是编译好的,以库文件的形式给出的,比如安全模块,路由模块,和Mesh自组网模块。与z-stack 相比msstatePAN、freakz协议栈都是全部真正的开源的,它们的所有源代码我们都可以看到。但是由于它们没有大的商业公司的支持,开发升级方面,性能方面和z-stack相比差距很大,并没有实现商业应用,只是作为学术研究而已。 还可以配备TI的一个标准兼容或专有的网络协议栈(RemoTI,Z-Stack,或SimpliciTI)来简化开发,当网络节点要求不多在30个以内,通信距离500m-1000m时用simpliciti。 1.2.3IEEE802.15.4标准概述 IEEE802.15.4是一个低速率无线个人局域网(LowRateWirelessPersonalAreaNetworks,LR-WPAN)标准。定义了物理层(PHY)和介质访问控制层(MAC)。 LR-WPAN网络具有如下特点: ◆实现250kb/s,40kb/s,20kb/s三种传输速率。 ◆支持星型或者点对点两种网络拓扑结构。 ◆具有16位短地址或者64位扩展地址。 ◆支持冲突避免载波多路侦听技术(carriersensemultipleaccesswithcollisionavoidance,CSMA/CA)。(mac层) ◆用于可靠传输的全应答协议。(RTS-CTS) ◆低功耗。 ◆能量检测(EnergyDetection,ED)。 ◆链路质量指示(LinkQualityIndication,LQI)。 ◆在2.45GHz频带内定义了16个通道;在915MHz频带内定义了10个通道;在868MHz频带内定义了1个通道。 为了使供应商能够提供最低可能功耗的设备,IEEE(InstituteofElectricalandElectronicsEngineers,电气及电子工程师学会)定义了两种不同类型的设备:一种是完整功能设备(full.functionaldevice,FFD),另一种是简化功能设备

BACnet协议的体系结构

B A C n e t协议的体系结 构 Revised by Petrel at 2021

B A C n e t 协议的体系结构 国际标准化组织在制定计算机网络通讯协议标准时定义了一个模型,称为开放系统互联参考模型(OSI )(ISO7498)。模型的目的是解决计算机与计算机之间普遍的通信问题。在这个模型中,将计算机通信这样一个复杂的问题分解成7个小的、容易解决的子问题,每个子问题只与某些通信功能相关,并且把这些子问题称为协议体系结构的一层,整个模型是一个七层的体系结构。在图4-1中给出这七层的体系结构图。 层。信的单个计算机之间可能相距很远,因此报文要通过一系列中间点才能到达。 而这些中间点相应地可能需要实现路由选择功能、某种解析功能,以及复杂的同步和差错恢复功能。 实现OSI 模型协议所需的费用较高,在绝大部分楼宇自动控制系统中,并不需要实现OSI 模型的所有内容。不过只从OSI 的功能性方面来考虑,经过简化,OSI 模型仍然是楼宇自动控制协议的一个很好的参考。如果只选择OSI 模型中需要的层次,形成一个简化的模型,作为楼宇自动控制系统的协议体系结构,就可以减少报文的长度,降低通信处理的开销,并且也满足楼宇自动控制系统的需要。这个简化的体系结构降低了楼宇自动控制工业的生产成本,同时处理器的大批量生产、局域网技术的发展,也为过程控制和办公自动化工业的发展起到了推动作用。另一方面,可以充分利用现有的、易用的、应用广泛的局域网技术,如以太网、ARCNET 和LonTalk 。这样不但可以降低成本,而且也有利于提高性能,为系统集成开辟新的途径。 用户1 用户2 图4-1开放系统互联基本参考模型

ZigBee协议栈OSAL介绍

讨论ZigBee协议栈的构成以及内部OSAL的工作机理。 ZigBee协议栈OSAL介绍 操作系统抽象层 OSAL常用术语: 1.资源(Resource):任何任务所占用的实体都叫资源,如变量、数组、结构体 2.共享资源(Shared Resource):两个或两个以上任务使用的资源,为防止破坏资源,任务在操作共享资源时是独占状态。 3.任务(Task):即线程,简单的程序的执行过程。任务设计时将问题尽可能分成多个任务,每个任务独立完成某项功能,同时赋予优先级、CPU寄存器和堆栈空间。一般一个任务设计为一个无限循环。 4.多任务运行(Muti-task Running):其实同一时刻只有一个任务运行。 5.内核(Kernel):内核负责管理各个任务。包括:分配CPU时间;任务调度;任务间的通信。 6.互斥(Mutual Exclusion):多任务通信最常用方法是共享数据结构。 保护共享资源常用的方法: 关中断; 使用测试并置位指令(T&S指令); 禁止任务切换; 使用信号量; 7.消息队列(Message Queue):用于任务间传递消息。 OSAL提供如下功能: 任务注册、初始化和启动; 任务间的同步、互斥; 中断处理; 储存器分配和管理; OSAL运行机理: OSAL就是一种支持多任务运行的系统资源分配机制。 OSAL是一种基于事件驱动的轮询式操作系统。、 void osal_start_system(void)是ZigBee协议栈的灵魂,不断的查看事件列表,如果有事件发生就调用相应的事件处理函数。 SYS_EVENT_MSG是一个事件集合,是由协议栈定义的事件,即系统强制事件(Mandatory Events),它的定义为: #define SYS_EVENT_MSG 0x8000; 它包含如下事件: AF_INCOMING_MSG_CMD 收到一个新的无线数据

楼宇自控协议BACnet 简介

BACnet 简介 BACnet 是 A Data Communication Protocol for Building Automation and Control Network 的简称,是一种为楼宇自控网络制定的数据通讯协议。 1987年,美国暖通空调工程师协会组织(ASHARE)的标准项目委员会调集了全球20多位业内著名专家,经过8年半时间,在1995年6月,ASHARE正式通过全球首个楼宇自控行业通讯标准--BACnet,标准编号为ANSI/ASHARE Standard135-1995,同年12月成为美国国家标准,并且还得到欧盟委员会的承认,成为欧盟标准草案。 BACnet是一个标准通讯和数据交换协议。各厂家按照这一协议标准开发与楼宇自控网兼容的控制器与接口,最终达到不同厂家生产的控制器都可以相互交换数据,实现互操作性。换言之,它确立了在不必考虑生产厂家,不依赖任何专用芯片组的情况下,各种兼容系统实 现开放性与互操作性的基本规则。 根据1999年底楼宇自控网在互联网页里的资料,目前世界上已有数百家国际知名的厂家支持BACnet ,其中包括楼宇自控系统厂家、消防系统厂家、冷冻机厂家、配电照明系统厂 家和安保系统厂家等。 〖 BACnet技术特点〗 BACnet采用了面向对象的技术,它定义了一组具有属性的对象(Object)来表示任意的楼宇自控设备的功能,从而提供了一种标准的表示楼宇自控设备的方式。同时BACnet定义了四种服务原语来传递某些特定的服务参数。目前BACnet共定义了18个对象,123 个属性和35个服务。由于一个楼宇自控系统中并不是所有的设备都要有必要支持BACnet所有的功能,BACnet协议还定义了6个性能级别和13个功能组。 〖 BACnet协议优点〗 BACnet是一个完全开放性的楼宇自控网协议,其开放性表现在: 独立于任何制造商,不需要专门芯片,并得到众多制造商的支持; 有完善和良好的数据表示和交换方法; 按标准制造的产品有严格的性能等级和完整的说明;

BACnet协议

BACnet、楼宇自动化系统与Internet的集成    徐正元 王盛卫  香港理工大学屋宇设备工程系  注:本文作者徐正元可以通过bacman@https://www.doczj.com/doc/b918430128.html,联系。  摘要:BACnet即“楼宇自控网络的数据通讯协议” ,是一种使不同厂家生产的楼宇自动化控制器能够互相通讯和共享信息的开放协议。本文论述了BACnet系统与Internet集成的几种方式,从而能够利用Internet的现有资源,采用统一的浏览器界面对分布在各地的BACnet系统进行监控。  关键字:BACnet,Internet,LonWorks,集成,BA,楼宇自动化通信协议    一、 什么是BACnet?  BACnet,即A Data Communication Protocol for Building Automation and Control Networks[2],简称BACnet协议,是美国采暖、制冷和空调工程师协会(ASHRAE)经过近9年时间的研究制定的世界上第一个开放的楼宇自动控制网络协议。它详细地阐述了楼宇自控网的功能,阐明了系统组成单元相互分享数据实现的途径、使用的通讯媒介、可以使用的功能以及信息如何翻译的全部规则。它确立了不同生产厂家生产的各种BACnet兼容系统在不依赖任何专用芯片的情况下,相互开放通讯的基本规则。它使不同厂商生产的设备与系统在互连和互操作的基础上实现无缝集成成为可能。  该协议1995年一经颁布,即引起了各国政府、专家学者和企业的广泛关注。1995年由美国国家标准学院通过成为美国国家标准,目前韩国、日本已将其列为国家标准,也成为欧盟的预标准、ISO草案标准(关于BACnet成为ISO正式BA标准的投票正在进行之中)。  BACnet提供了一个描述各种自动化系统的精确模型,模型包括以下这些组成部分:23种对象(2001年版)、40个服务、137种属性、6种数据链路层、网络功能、互操作一致性。  BACnet是一个实现控制系统互操作的应用独立模型,它有着众多的互操作性类型和通用的对象,这使它不仅可以用于HVAC,还可以用于照明、安防、消防、门禁、交通等领域,事实证明它的应用领域还在不断的扩展之中[3]。    二、 关于BA系统与Internet的集成  Internet作为一个覆盖全世界的超级计算机网际网,在上个世纪九十年代得到了非常广泛的使用。无论是对人们的生活还是各个行业的发展都产生了极大的影响,楼宇自动化行业也不例外。楼宇自动化系统与Internet结合起来,就能够利用Internet的现有资源条件,实现分布在各地的BA设备及网络的低成本互

2020年Zigbee协议栈中文说明免费

1.概述 1.1解析ZigBee堆栈架构 ZigBee堆栈是在IEEE 802.15.4标准基础上建立的,定义了协议的MAC和PHY层。ZigBee设备应该包括IEEE802.15.4(该标准定义了RF射频以及与相邻设备之间的通信)的PHY和MAC层,以及ZigBee堆栈层:网络层(NWK)、应用层和安全服务提供层。图1-1给出了这些组件的概况。 1.1.1ZigBee堆栈层 每个ZigBee设备都与一个特定模板有关,可能是公共模板或私有模板。这些模板定义了设备的应用环境、设备类型以及用于设备间通信的簇。公共模板可以确保不同供应商的设备在相同应用领域中的互操作性。 设备是由模板定义的,并以应用对象(Application Objects)的形式实现(见图1-1)。每个应用对象通过一个端点连接到ZigBee堆栈的余下部分,它们都是器件中可寻址的组件。 图1-1 zigbe堆栈框架 从应用角度看,通信的本质就是端点到端点的连接(例如,一个带开关组件的设备与带一个或多个灯组件的远端设备进行通信,目的是将这些灯点亮)。 端点之间的通信是通过称之为簇的数据结构实现的。这些簇是应用对象之间共享信息所需的全部属性的容器,在特殊应用中使用的簇在模板中有定义。图1-1-2就是设备及其接口的一个例子:

图1-1-2 每个接口都能接收(用于输入)或发送(用于输出)簇格式的数据。一共有二个特殊的端点,即端点0和端点255。端点0用于整个ZigBee设备的配置和管理。应用程序可以通过端点0与ZigBee 堆栈的其它层通信,从而实现对这些层的初始化和配置。附属在端点0的对象被称为ZigBee设备对象 (ZD0)。端点255用于向所有端点的广播。端点241到254是保留端点。 所有端点都使用应用支持子层(APS)提供的服务。APS通过网络层和安全服务提供层与端点相接,并为数据传送、安全和绑定提供服务,因此能够适配不同但兼容的设备,比如带灯的开关。APS使用网络层(NWK)提供的服务。NWK负责设备到设备的通信,并负责网络中设备初始化所包含的活动、消息路由和网络发现。应用层可以通过ZigBee设备对象(ZD0)对网络层参数进行配置和访问。 1.1.2 80 2.15.4 MAC层 IEEE 802.15.4标准为低速率无线个人域网(LR-WPAN)定义了OSI模型开始的两层。PHY层定义了无线射频应该具备的特征,它支持二种不同的射频信号,分别位于2450MHz波段和868/915MHz 波段。2450MHz波段射频可以提供250kbps的数据速率和16个不同的信道。868 /915MHz波段中,868MHz支持1个数据速率为20kbps的信道,915MHz支持10个数据速率为40kbps的信道。MAC层负责相邻设备间的单跳数据通信。它负责建立与网络的同步,支持关联和去关联以及MAC 层安全:它能提供二个设备之间的可靠链接。 1.1.3 关于服务接入点 ZigBee堆栈的不同层与802.15.4 MAC通过服务接入点(SAP)进行通信。SAP是某一特定层提供的服务与上层之间的接口。 ZigBee堆栈的大多数层有两个接口:数据实体接口和管理实体接口。数据实体接口的目标是向上层提供所需的常规数据服务。管理实体接口的目标是向上层提供访问内部层参数、配置和管理数据的机制。 1.1.4 ZigBee的安全性 安全机制由安全服务提供层提供。然而值得注意的是,系统的整体安全性是在模板级定义的,这意味着模板应该定义某一特定网络中应该实现何种类型的安全。 每一层(MAC、网络或应用层)都能被保护,为了降低存储要求,它们可以分享安全钥匙。SSP是通过ZD0进行初始化和配置的,要求实现高级加密标准(AES)。ZigBee规范定义了信任中心的用

BACnet协议

BACnet是用于智能建筑的通信协议,是国际标准化组织(ISO)、美国国家标准协会(ANSI)及美国采暖、制冷与空调工程师学会(ASHRAE)定义的通信协议。BACnet针对智能建筑及控制系统的应用所设计的通信,可用在暖通空调系统(HVAC,包括暖气、通风、空气调节)也可以用在照明控制、门禁系统、火警侦测系统及其相关的设备。优点在于能降低维护系统所需成本并且安装比一般工业通信协议更为简易,而且提供有五种业界常用的标准协议,此可防止设备供应商及系统业者的垄断,也因此未来系统扩充性与兼容性大为增加[1]。 BACnet为Building Automation and Control networks 的简称,台湾通常翻译为“建筑自动化控制网路通讯协定”[5][6],而中国大陆则译为“楼宇自动化与控制网络”[7]。 协议简介[编辑]BACnet通信协议中定义了几种不同的数据链结层/物理层,包括: 1.ARCNET(ARCNET)。 2.以太网。 3.BACnet/IP。 4.RS-232上的点对点通信(Point-to-point_(telecommunications))。 5.RS-485上的主站-从站/令牌传递(Master-Slave/Token-Passing,简称MS/TP)通信。 6.LonTalk(LonTalk) BACnet通信协议中定义了许多服务(service),可供各设备之间的通信,服务可以分为五类:有关设备对象管理的服务包括Who-Is、I-Am、Who-Has及I-Have等服务,有关对象访问的服务包括读取属性、写入属性等服务,有关报警与事件的服务包括确认报警、属性改变(change of state)报告等,此外也有有关文件读写及虚拟终端的服务[8]。

lwip各层协议栈详解

竭诚为您提供优质文档/双击可除lwip各层协议栈详解 篇一:lwip协议栈源码分析 lwip源码分析 -----caoxw 1lwip的结构 lwip(lightweightinternetprotocol)的主要模块包括:配置模块、初始化模块、netif模块、mem(memp)模块、netarp模块、ip模块、udp模块、icmp模块、igmp模块、dhcp模块、tcp模块、snmp模块等。下面主要对我们需要关心的协议处理进行说明和梳理。配置模块: 配置模块通过各种宏定义的方式对系统、子模块进行了配置。比如,通过宏,配置了mem管理模块的参数。该配置模块还通过宏,配置了协议栈所支持的协议簇,通过宏定制的方式,决定了支持那些协议。主要的文件是opt.h。 初始化模块: 初始化模块入口的文件为tcpip.c,其初始化入口函数为: voidtcpip_init(void(*initfunc)(void*),void*arg)

该入口通过调用lwip_init()函数,初始化了所有的子模块,并启动了协议栈管理进程。同时,该函数还带有回调钩子及其参数。可以在需要的地方进行调用。 协议栈数据分发管理进程负责了输入报文的处理、超时处理、api函数以及回调的处理,原型如下: staticvoidtcpip_thread(void*arg) netif模块: netif模块为协议栈与底层驱动的接口模块,其将底层的一个网口设备描述成协议栈的一个接口设备(netinterface)。该模块的主要文件为netif.c。其通过链表的方式描述了系统中的所有网口设备。 netif的数据结构描述了网口的参数,包括ip地址、mac 地址、link状态、网口号、收发函数等等参数。一个网口设备的数据收发主要通过该结构进行。 mem(memp)模块: mem模块同一管理了协议栈使用的内容缓冲区,并管理pbuf结构以及报文的字段处理。主要的文件包括mem.c、memp.c、pbuf.c。 netarp模块: netarp模块是处理arp协议的模块,主要源文件为etharp.c。其主要入口函数为: err_tethernet_input(structpbuf*p,structnetif*netif)

ZigBee测试与协议分析

ZigBee测试与协议分析 1 前言 ZigBee协议栈包括物理层协议(IEEE802.15.4)和上层软件协议(ZigBee 2007以及其他的ZigBee网络协议)。本文将从这两方面来了解这些协议,通过介绍如何捕获及如何理解关键参数,深层次剖析ZigBee技术。有了这些本质性的认识,对于分析解决无线产品应用问题,会有很大的帮助。 2 物理层分析 ZigBee的物理层为IEEE802.15.4标准所规定,定义了ZigBee底层的调制编码方式。这些规约大多是芯片设计者需要关心的,对于应用开发来说,更关心的是衡量一个芯片、一个射频系统性能的参数。在过去的文章中,已介绍了输出功率、接收灵敏度和链路预算等参数,这一讲将更深入地介绍一个调制质量的参数:EVM。EVM指的是误差向量(包括幅度和相位的矢量),表征在一个给定时刻理想无误差基准信号与实际发射信号的向量差,。从EVM参数中,可以了解到一个输出信号的幅度误差及相位误差。 EVM是衡量一个RF系统总体调制质量的指标,定义为信号星座图上测量信号与理想信号之间的误差,它用来表示发射器的调制精度,调制解调器、PA、混频器、收发器等对它都会有影响。EVM数据和眼图。 了解完这个参数之后,再看看实际测试中是如何获取EVM参数的。 ZigBee物理层的测试,在产品研发、生产和维护阶段,可以分别采用不同的仪器。 (1)产品研发阶段要测量EVM参数,需要使用带协议解析的频谱仪,最好是自带相应协议插件的仪器,可以使用安捷伦PXA N9030A频谱分析仪+8960B插件(选配了ZigBee分析插件)。这些仪器可以测试出ZigBee调制信号的星座图、实时数据和眼图等信息,在芯片级开发过程中,需要考量高频电容电感以及滤波器等的单个及组合性能,特别需要注意的是ZigBee信号的临道抑制参数,利用PXA N9030A的高分辨率,可以查看点频的带外信号,这些细节在更换射频器件供应商时,需要仔细测量,一般数字电路抄板比较容易,因为器件性能的影响不是很大,只要值和封装对了就可以,但是射频前端的设计上,即使原样的封装、容值和感值,供应商不一样,射频参数也是不一样的,板材的选用也极大地影响着阻抗匹配,因此复制和再开发都有较大难度。合格的测试工具,加上有质量保证的射频器件供应商资源,方能真正具备RF设计能力。安捷伦PXA N9030A频谱分析仪。 (2)批量生产阶段在批量生产中,不可能将实验室的研发测试仪器搬到工厂,因此,需要便携小巧的测试设备,这时可用罗德与斯瓦茨公司的热功率探头,如NRP-Z22,做一个2.4 GHz的输出功率测试,保证能够输出公差允许的功率信号即可,因为在生产中,射频器件的焊接不良、馈线连接头的接触不良,都会造成输出功率的下降甚至消失。需要注意的是,探头非常容易被静电损坏,必须要带上防静电手套进行操作,返修过程如需要经过德国,则时间长,经费也不便宜,不是很严重的损坏倒是可以在深圳维修中心处理。NRP-Z22。 (3)应用阶段在现场出现问题时,ZigBee节点已经安装到现场,不能逐一拆下来测试,并且周围的电磁环境也是没办法在单个节点上检测到,这时就需要手持式的频谱仪进行现场勘查了,例如安捷伦公司的N9912A手持式频谱仪。使用该频谱仪,可以完成无线系统设计初期的现场勘查工作,检测现场各个地点是否有异常电磁干扰,对于ZigBee来说,当然是检测是否有持续的WIFI信号干扰了。同时,更为详细的现场勘查,还包括在定点进行数据发送,预期覆盖点进行信号强度分析,以实地评估墙体等障碍物的信号衰减,在已经架设好的ZigBee网络中,也可以检测信号覆盖,数据通信是否正常等。N9912A。

BA系统通讯协议:BACnet

BA系统通讯协议:BACnet 目录 一.I SO/OSI开放系统模型 二.BACnet简况 三.BACnet协议的基本思路 四. BACnet的具体规定 一.ISO/OSI开放系统模型 楼宇自动化系统(BAS,Building Automation System)出现于20世纪70年代末期。由于各个生产厂家开发的都是自己专有的通信协议(Proprietary Communication protocols),因此,不同厂家控制设备之间的通信需要"网关"(gateways)来解决;这使得应用工程师和用户在同一个BAS系统中选用不同厂家的产品变的非常复杂和昂贵,应用工程师、用户的选择范围和灵活性受到很大限制,甚至被"琐在"一个供应商的产品上,最终是用户的系统性能和投资效益受到损失。 社会需求推动着技术向前发展。人们期待着开放的、统一的通信协议,亦即不同厂家的产品能够采用共同的"语言"和"语法"轻松的进行"交谈"。最终的目标则是希望形成一个"即插即用"(plug-and-play)的环境,使得BAS系统可以容易地进行组态和变更。 国际标准化组织ISO于1984年发布的"开放系统互连模型"(OSI,Open Systems Interconnection model),是推进通信协议标准化的重要一步。ISO/OSI把通信协议在数据通信中的任务分解为一种七层结构: 1.1物理联接(Physical link) 网络设备和网络之间实际的硬件联接。本层规定界面的物理特性(比如机械部件和联结),电力特性(比如电压水平)和功能特性(比如安装和拆装物理联接)。 1.2数据联接(Data link) 规定数据如何编码,如何从每一种网络设备中传出传进。网桥(Bridges)工作于此层。 1.3网络(Network) 规定整个网络的路由,开关和电子访问。路由器(routers)工作哟于此层。路由器中保持一个网络中相棱邻路由器和网段(segment)的表。路由器联接网段,控制"交通"。 1.4传输(Transport) 保证网络节点和设备之间的数据传输。 1.5进程(Session) 在网络节点和设备之间进行"会话"时做通信控制。 1.6表达(Presentation)

从Zigbee协议栈底层添加自己的按键配置

本实验是基于ZStack-CC2530-2.5.1a版本的协议栈来进行实验的,整个实验需要改动 hal_board_cfg.h、hal_board_cfg.h、hal_key.c、hal_key.h和自己定义的Coordinator.c这5个文件。 注意:添加自己的按键时尽量不要修改协议栈里面的按键程序,自己另行添加即可。 1、hal_key.h 在/* Switches (keys) */下面添加自己的按键定义 #define HAL_KEY_SW_8 0x80 图1: ---------------------------------------------------------------------------------------- 2、hal_board_cfg.h 在/* S6 */ #define PUSH1_BV BV(1) #define PUSH1_SBIT P0_1 #if defined (HAL_BOARD_CC2530EB_REV17) #define PUSH1_POLARITY ACTIVE_LOW #elif defined (HAL_BOARD_CC2530EB_REV13) #define PUSH1_POLARITY ACTIVE_LOW #else #error Unknown Board Indentifier #endif 下面模仿/* S6 */下的程序定义自己的按键值: /* S8 */ #define PUSH8_BV BV(4)//修改 #define PUSH8_SBIT P0_4//修改 #if defined (HAL_BOARD_CC2530EB_REV17)

LwIP协议栈源码详解

LwIP协议栈源码详解 ——TCP/IP协议的实现 Created by.. 老衲五木 at.. UESTC Contact me.. for_rest@https://www.doczj.com/doc/b918430128.html, 540535649@https://www.doczj.com/doc/b918430128.html,

前言 最近一个项目用到LwIP,恰好看到网上讨论的人比较多,所以有了写这篇学习笔记的冲动,一是为了打发点发呆的时间,二是为了吹过的那些NB。往往决定做一件事是简单的,而坚持做完这件事却是漫长曲折的,但终究还是写完了,时间开销大概为四个月,内存开销无法估计。。 这篇文章覆盖了LwIP协议大部分的内容,但是并不全面。它主要讲解了LwIP协议最重要也是最常被用到的部分,包括内存管理,底层网络接口管理,ARP层,IP层,TCP层,API 层等,这些部分是LwIP的典型应用中经常涉及到的。而LwIP协议的其他部分,包括UDP,DHCP,DNS,IGMP,SNMP,PPP等不具有使用共性的部分,这篇文档暂时未涉及。 原来文章是发在空间中的,每节每节依次更新,后来又改发为博客,再后来就干脆懒得发了。现在终于搞定,于是将所有文章汇总。绞尽脑汁的想写一段空前绝后,人见人爱的序言,但越写越觉得像是猫儿抓的一样。就这样,PS:由于本人文笔有限,情商又低,下里巴人一枚,所以文中的很多语句可能让您很纠结,您可以通过邮箱与我联系。共同探讨才是进步的关键。 最后,欢迎读者以任何方式使用与转载,但请保留作者相关信息,酱紫!码字。。。世界上最痛苦的事情莫过于此。。。 ——老衲五木

目录 1 移植综述------------------------------------------------------------------------------------------------------4 2 动态内存管理------------------------------------------------------------------------------------------------6 3 数据包pbuf--------------------------------------------------------------------------------------------------9 4 pbuf释放---------------------------------------------------------------------------------------------------13 5 网络接口结构-----------------------------------------------------------------------------------------------16 6 以太网数据接收--------------------------------------------------------------------------------------------20 7 ARP表-----------------------------------------------------------------------------------------------------23 8 ARP表查询-----------------------------------------------------------------------------------------------26 9 ARP层流程-----------------------------------------------------------------------------------------------28 10 IP层输入-------------------------------------------------------------------------------------------------31 11 IP分片重装1--------------------------------------------------------------------------------------------34 12 IP分片重装2--------------------------------------------------------------------------------------------37 13 ICMP处理-----------------------------------------------------------------------------------------------40 14 TCP建立与断开----------------------------------------------------------------------------------------43 15 TCP状态转换-------------------------------------------------------------------------------------------46 16 TCP控制块----------------------------------------------------------------------------------------------49 17 TCP建立流程-------------------------------------------------------------------------------------------53 18 TCP状态机----------------------------------------------------------------------------------------------56 19 TCP输入输出函数1-----------------------------------------------------------------------------------60 20 TCP输入输出函数2-----------------------------------------------------------------------------------63 21 TCP滑动窗口-------------------------------------------------------------------------------------------66 22 TCP超时与重传----------------------------------------------------------------------------------------69 23 TCP慢启动与拥塞避免-------------------------------------------------------------------------------73 24 TCP快速恢复重传和Nagle算法-------------------------------------------------------------------76 25 TCP坚持与保活定时器-------------------------------------------------------------------------------80 26 TCP定时器----------------------------------------------------------------------------------------------84 27 TCP终结与小结----------------------------------------------------------------------------------------88 28 API实现及相关数据结构-----------------------------------------------------------------------------91 29 API消息机制--------------------------------------------------------------------------------------------94 30 API函数及编程实例-----------------------------------------------------------------------------------97

相关主题
文本预览
相关文档 最新文档