## 1 Introduction  
Wireless sensor networks (WSNs) are an enabling technology of smart environments for ambient intelligence (AmI). In AmI, WSNs perform human-centric sensing where low-level sensor data on usersand their surroundings are collated and processed to infer higher-level user context information for context-adaptive AmI applications. The term _context_ refers to information that describes the current state or situation of an entity, which can be a person (e.g., user context), place, or object. The high-level user context information is a necessity in AmI applications to deliver personalized services to the users in an intuitive and intelligent way to support their everyday activities. Moreover, we envision such user context information could be harnessed for optimizing the performance of the underlying WSNs through cross-layer (CL) interactions.  
By allowing direct information exchange between non-adjacent protocol layers via CL interaction, network performances such as energy efficiency and delay can be optimized [1]. This is particularly important for WSNs where sensor devices are energy-constrained and deployed for real-time monitoring applications. In the current literature, most research on CL optimization for WSNs have focused on interactions between lower layers of the protocol stack, _i.e._, physical, medium access control (MAC), and network layers [2]. There was also research on CL optimization that considered application requirements, e.g., quality-of-service (QoS) requirements of multimedia applications [3].  
Unlike these previous works that either were not concerned with the application layer or used the application to only define the requirements of CL optimization, this paper focuses on how application derived information, _i.e._, the user context information derived from AmI application, can optimize the underlying WSN performance through CL interactions.  
At the system level, an AmI system can adapt its intelligent services to user-related context information. However, its underlying WSN rarely considers AmI context information. If user context information can influence how an intelligent system responds, there is also a possibility for the underlying network to use the user context information. This allows the network protocols to become _smart_ by adapting their functionality to user situations.  
For the first time, we proposed a generic and customizable CL framework that utilizes AmI context information from application layer for optimizing protocol performance in WSNs [4]. This framework can be applied to any layer of the protocol stack and is sufficiently generic to be customized to different AmI applications. In this paper, we present the framework's architecture in more detail and apply the proposed framework to jointly optimize the backoff behavior of a contention-based MAC protocol, and the path selection of an _ad hoc_ On-demand Distance Vector (AODV) based routing protocol for WSNs by adapting their protocol functions in real-time to the user context information inferred from an AmI application.  
The rest of the paper is organized as follows. Section 2 outlines related work. Section 3 presents the motivating scenario. Section 4 describes the proposed framework. Section 5 illustrates a use case of the proposed framework by implementing it to optimize two existing protocols at the MAC and NET layers. Section 6 presents and discusses the evaluation results. Finally, Section 7 concludes the paper.