        -:    1:/*
        -:    2:  This file is a part of the nesccov extensions to the nesc compiler.
        -:    3:
        -:    4:  Copyright(c) 2011-2014 University of Warsaw. All rights reserved.
        -:    5:  All rights reserved.
        -:    6:
        -:    7:  Redistribution and use in source and binary forms, with or without 
        -:    8:  modification, are permitted provided that the following conditions 
        -:    9:  are met:
        -:   10:
        -:   11:    * Redistributions of source code must retain the above copyright 
        -:   12:      notice, this list of conditions and the following disclaimer.
        -:   13:    * Redistributions in binary form must reproduce the above copyright 
        -:   14:      notice, this list of conditions and the following disclaimer in 
        -:   15:      the documentation and/or other materials provided with the 
        -:   16:      distribution.
        -:   17:    * Neither the name of University of Warsaw nor the names of its 
        -:   18:      contributors may be used to endorse or promote products derived 
        -:   19:      from this software without specific prior written permission.
        -:   20:
        -:   21:  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
        -:   22:  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
        -:   23:  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
        -:   24:  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
        -:   25:  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
        -:   26:  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
        -:   27:  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
        -:   28:  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
        -:   29:  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
        -:   30:  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
        -:   31:  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        -:   32:*/
        -:   33:
        -:   34:
        -:   35:/**
        -:   36: * The main module of the application demonstrating
        -:   37: * the use of the nesccov functionality.
        -:   38: *
        -:   39: * @author Konrad Iwanicki <iwanicki@mimuw.edu.pl>
        -:   40: */
        -:   41:module NescCovDemoMainP @safe()
        -:   42:{
        -:   43:    uses
        -:   44:    {
        -:   45:        interface Boot;
        -:   46:        interface Timer<TMilli> as FrequentTimer;
        -:   47:        interface Timer<TMilli> as InfrequentTimer;
        -:   48:        interface Timer<TMilli> as InactiveTimer;
        -:   49:        interface Leds;
        -:   50:#if (defined(NESCCOV_LINE_COVERAGE) || defined(NESCCOV_COND_COVERAGE))
        -:   51:        // Dumps the run-time coverage information,
        -:   52:        // either periodically or on demand.
        -:   53:        interface NescCovInfoDumper;
        -:   54:#endif
        -:   55:    }
        -:   56:}
        -:   57:implementation
        -:   58:{
        -:   59:
        -:   60:    uint8_t   counterEqualToZeroOrOne = 0;
        -:   61:
        -:   62:    task void coveredTask();
        -:   63:    task void uncoveredTask();
        -:   64:    void coveredFunction();
        -:   65:    void uncoveredFunction();
        -:   66:
        -:   67:
        1:   68:    event void Boot.booted()
        1:   69:    {
        1:   70:#if (defined(NESCCOV_LINE_COVERAGE) || defined(NESCCOV_COND_COVERAGE))
        1:   71:        // Initialize periodic dumping of the coverage data.
        1:   72:        call NescCovInfoDumper.setInterval(2048);
        1:   73:#endif
        1:   74:
        1:   75:        call FrequentTimer.startPeriodic(256);
        1:   76:        call InfrequentTimer.startPeriodic(512);
        1:   77:        if (counterEqualToZeroOrOne > 0)
        -:   78:        {
        -:   79:            // The code below should not be covered.
    #####:   80:            call InactiveTimer.startPeriodic(1024);
    #####:   81:            post uncoveredTask();
        -:   82:        }
        -:   83:    }
        -:   84:
        -:   85:
        1:   86:    event void FrequentTimer.fired()
        1:   87:    {
        1:   88:        // This event should be covered.
        1:   89:        coveredFunction();
        -:   90:    }
        -:   91:
        -:   92:
        1:   93:    event void InfrequentTimer.fired()
        1:   94:    {
        1:   95:        // This event should be covered.
        1:   96:        call Leds.led2Toggle();
        1:   97:        post coveredTask();
        -:   98:    }
        -:   99:
        -:  100:
        1:  101:    task void coveredTask()
        1:  102:    {
        1:  103:        counterEqualToZeroOrOne = 1 - counterEqualToZeroOrOne;
        1:  104:        counterEqualToZeroOrOne = 1 - counterEqualToZeroOrOne;
        -:  105:    }
        -:  106:
        -:  107:
        1:  108:    void coveredFunction()
        1:  109:    {
        1:  110:        ++counterEqualToZeroOrOne;
        1:  111:        if (counterEqualToZeroOrOne == 1 || counterEqualToZeroOrOne == 2)
        -:  112:        {
        1:  113:            call Leds.led1Toggle();
        1:  114:            if (counterEqualToZeroOrOne == 2)
        -:  115:            {
        1:  116:                counterEqualToZeroOrOne = 0;
        -:  117:            }
        -:  118:        }
        1:  119:        if (counterEqualToZeroOrOne > 1)
        -:  120:        {
        -:  121:            // This code should not be covered.
    #####:  122:            counterEqualToZeroOrOne = 0;
        -:  123:        }
        -:  124:    }
        -:  125:
        -:  126:
    #####:  127:    event void InactiveTimer.fired()
    #####:  128:    {
    #####:  129:        // This code should not be covered.
    #####:  130:        post uncoveredTask();
        -:  131:    }
        -:  132:
        -:  133:
    #####:  134:    task void uncoveredTask()
    #####:  135:    {
    #####:  136:        // This task should never be posted,
    #####:  137:        // hence its code should not be covered.
    #####:  138:        call Leds.led0Toggle();
    #####:  139:        if (counterEqualToZeroOrOne == 0)
        -:  140:        {
    #####:  141:            uncoveredFunction();
        -:  142:        }
        -:  143:    }
        -:  144:
        -:  145:
    #####:  146:    void uncoveredFunction()
    #####:  147:    {
    #####:  148:        // This function should be completely uncovered.
    #####:  149:        counterEqualToZeroOrOne = 1;        
        -:  150:    }
        -:  151:}
        -:  152:
