## 4 Implementation
### Structure of the Library  
TinyCoAP provides a CoAP library native for TinyOS. It is designed behind the idea that better performance and reliable run-time execution are both achieved integrating it with the OS core libraries. Following these design principles, the core functionalities of CoAP are provided as TinyOS components. These components are developed as part of the TinyOS network library. Differently from TinyCoAP, CoapBlip is thought as an adaptation of a C library for generic embedded systems. A TinyOS component is used as an adapter between this library and the TinyOS application.  
TinyCoAP avoids using external C libraries and relies completely on code developed in the NesC language. This allows reaching a high code optimization and having less impact on the WSN node memory. These benefits derive mainly from the different organization and functioning of C and NesC programs. Typical C programs are composed by functions that are specified in separated files. These are compiled separately and then linked together by matching global name of functions. The interaction between functions is achieved dynamically during run-time by using function pointers. Pointers are stored in the RAM memory and therefore cause a growth of its occupancy. In contrast with C, TinyOS programs are conceived as a set of components connected together to perform a specific task. The interaction between components is done using the interfaces that these provide. Applications declare at compile-time which components they use and then, they explicitly wire the interfaces they will use at run-time. Thanks to this static wiring, TinyOS programs avoid using function pointers and therefore they are able to reduce the RAM memory footprint.  
The TinyCoAP library is composed by five components. Its design follows the CoAP conceptual layering. The message layer is implemented by three components. CoapPDU, where PDU stands for Protocol Data Unit, is the main component of this sub-layer. It provides the interface used to create, read and write CoAP packets. The interface needed to create or delete options is provided by the CoapOption component. The creation, use and managing of the linked lists is performed by the interface provided by the CoapList component. Linked lists are useful for iterating the packets that are in the memory pool waiting for being processed. CoapList is also used to store and iterate the options that compose a packet and to manage retransmissions. CoapPDU is wired to CoapList and CoapOption. This allows CoapPDU to work with the options contained in a CoAP packet. Moreover, each component of the message layer is wired to the TinyOS PoolC component. This is used to allocate the memory needed to perform their operation. PoolC allocates memory according to the data structure that is specified by each component. The wiring of the message layer components is shown in Figure 4.  
The request/response matching layer of CoAP is implemented by the CoapServer and CoapClient components. The CoapClient component provides the interface used to send CoAP requests. The interface provided by CoapServer allows initializing and binding the server to a specific UDP port. The management of the retransmission mechanism and the CoAP packet processing are also implemented in these components. The CoapServer component implement the discovery of CoAP resources [41] and the observe model of CoAP [42]. The management of the resources provided by the server is implemented in a distinct interface. The resources are created through a parameterized interface. This is called CoapResource and provides commands and events to handle resources and the separate response mechanism of CoAP.