## 3 Application Protocol for Wireless Intelligent Sensor Networks
### Protocol Description
#### 3.2.3 Protocol State Machine  
The state machine is needed to keep track of several parameters to support the different services provided by the application protocol in order to avoid unnecessary transmissions as possible. Figure 2 presents the state machine, using boxes to represent states and arrow lines for transitions. For simplicity, not every possible minor transition is shown.  
As shown in Figure 2, both the sensor and the base station share some states. Transitions in the base station are noted as solid lines, while dashed lines indicate sensor state changes. Each transition is labeled with additional information, including event type (bold text above the line) and action taken (text below the line). The kind of event and action is shown with a letter in brackets. Internal events can be produced by timers, battery charge, FBRS results, and values from probes. Communication events or actions always imply the sending or receiving of data by radio. Primitives are interface functions called by the sensor agents to access services. A summarized review of the states is presented below.  
State description:  
1. Idle: For sensors, the idle state is also called sleep mode, and keeps only minimal system processes active and switches off most of the devices. In a base station, this is a listening mode.
2. Isolated: The sensor does not have a base station to work with or has lost contact with it.
3. Sensor lost: This state exists only in the base station, and is active when a SNR_ISOLATED is received. If the sensor belongs to the base station, it sends a BST_LINK message; otherwise, BST_REJECT is sent.
4. Waiting base station: When the primitive Link is executed, a SNR_ISOLATED message is sent and the sensor goes into this state. If the sensor receives the BST_LINK message from a base station, it become active and starts working. If only BST_REJECT messages are received or the timeout B triggers, the sensor goes into the isolated state again.  
Figure 2: Protocol state machine.  
* Waiting connection: The sensor has executed the primitive Awake and waits for incoming requests or commands from the base station.
* Operating: In this state, sensors and base stations exchange messages and run their internal applications to accomplish their tasks.  
Primitives:  
There are two types of primitives: confirmed and unconfirmed. Confirmed primitives have four steps, including request, indication, response, and confirm. Unconfirmed primitives do not need a response and confirm, so they only have request and indication. The type of primitive is distinguished by a 'C' or a 'U' in the descriptions below.  
* Link (_maxhops_), C: Request a base station to form part of its WSN. Links send a broadcast message with a maximum number of hops, _maxhops_. This parameter is controlled by the communication agent.
* Command (_cid_, _agent_), C: Requires the execution of command _cid_ by the desired _agent_.
* Request (_rid_, _agent_), C: Requires or sets some information of the desired _agent_.
* Awake (_timeoutA_, _maxhops_), U: The primitive broadcasts an \"I am awake\" beacon that informs that it is ready to receive a request. Like Link() primitive, Awake() has a _maxhops_ parameter.
* Alarm (_aid_, _atime_), U: Sets an alarm of _aid_ type produced at _atime_.