/* ==================================================================== 
 * The Kannel Software License, Version 1.0 
 * 
 * Copyright (c) 2001-2010 Kannel Group  
 * Copyright (c) 1998-2001 WapIT Ltd.   
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in 
 *    the documentation and/or other materials provided with the 
 *    distribution. 
 * 
 * 3. The end-user documentation included with the redistribution, 
 *    if any, must include the following acknowledgment: 
 *       "This product includes software developed by the 
 *        Kannel Group (http://www.kannel.org/)." 
 *    Alternately, this acknowledgment may appear in the software itself, 
 *    if and wherever such third-party acknowledgments normally appear. 
 * 
 * 4. The names "Kannel" and "Kannel Group" must not be used to 
 *    endorse or promote products derived from this software without 
 *    prior written permission. For written permission, please  
 *    contact org@kannel.org. 
 * 
 * 5. Products derived from this software may not be called "Kannel", 
 *    nor may "Kannel" appear in their name, without prior written 
 *    permission of the Kannel Group. 
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 * ==================================================================== 
 * 
 * This software consists of voluntary contributions made by many 
 * individuals on behalf of the Kannel Group.  For more information on  
 * the Kannel Group, please see <http://www.kannel.org/>. 
 * 
 * Portions of this software are based upon software originally written at  
 * WapIT Ltd., Helsinki, Finland for the Kannel project.  
 */ 

/*
 * wtp_init_state.h: Macro calls for implementing wtp initiator state tables
 * See documentation for guidance how to use and update these.
 *
 * Only classes 0 and 1 are implemented. State NULL is called INITIATOR_NULL_
 * STATE. 1 in the action field means that action is unconditional.
 *
 * Class 0 service is here a stateless invoke message (used for disconnection
 * or unconfirmed push).
 *
 * Basic class 1 transaction, without timers, is following:
 *               - initiator sends an invoke message to the responder
 *               - responder acknowledges it, with an pdu with tid verification
 *                 off (if it is on, we have a tid verification transaction, 
 *                 see below).
 *
 * Retransmission until acknowledgement is implemented using timers and 
 * retransmission counters. When the initiator sends an invoke it starts a 
 * timer. When it expires, it resends the packet (either ack or invoke), until
 * counter reaches the maximum value. Then the transaction is aborted.
 *
 * If user acknowledgement is on, timers have different values.
 *
 * When the initiator aborts the transaction, it sends an abort pdu. When the
 * responder does it, the initiator wtp user is indicated.
 *
 * Tid verification in the initiator means answering the question posed by the
 * responder: "Have you an outstanding transaction having this tid". If we do
 * not have it, we have already, before feeding the event into the state 
 * machine, sent an abort with reason INVALIDTID. So here we answer to  an
 * ack pdu with tidve-flag set with an ack pdu with tidok-flag set. See WTP
 * 5.6, table 2; WTP 8.9; WTP 9.3.4.1.
 *
 * By Aarno Syvänen for Wapit Ltd. 
 */

INIT_STATE_NAME(INITIATOR_NULL_STATE)
INIT_STATE_NAME(INITIATOR_RESULT_WAIT)

/*
 * We do not use transaction class 2 here: Server is initiator only when it is 
 * pushing (class 1 or class 0) or disconnecting (class 0). First and second 
 * rows are similar, with exception of timer period.
 */
ROW(INITIATOR_NULL_STATE,
    TR_Invoke_Req,
    event->u.TR_Invoke_Req.tcl == 1,
    {
     WAPEvent *invoke;
/*
 * A special counter is used for storing value used (1) for tidnew flag when
 * restarting (See WTP 8.8.3.2)
 */
     init_machine->tidnew = tidnew;
     
     wap_event_destroy(init_machine->invoke);
     init_machine->rid = 0;
     init_machine->rcr = 0;
        
     invoke = wtp_pack_invoke(init_machine, event);
     init_machine->invoke = wap_event_duplicate(invoke);
     dispatch_to_wdp(invoke);
     init_machine->rid = 1;
/*
 * Turn the tidnew-flag off if it was on. (This can happen when tid was 
 * wrapped or when we are restarting, see WTP 8.8.3.2) 
 */     
     if (init_machine->tidnew) {
         init_machine->tidnew = 0;
         tidnew = 0;
     }
     init_machine->u_ack = event->u.TR_Invoke_Req.up_flag;
     init_machine->rcr = 0;
     start_initiator_timer_R(init_machine);
    }, 
    INITIATOR_RESULT_WAIT)

/*
 * No need to turn tidnew flag when sending class 0 message; tid validation is
 * not invoked in this case.
 */
ROW(INITIATOR_NULL_STATE,
    TR_Invoke_Req,
    event->u.TR_Invoke_Req.tcl == 0,
    {
     WAPEvent *invoke;

     wap_event_destroy(init_machine->invoke);
     invoke = wtp_pack_invoke(init_machine, event);
     init_machine->invoke = wap_event_duplicate(invoke);
     dispatch_to_wdp(invoke);
    },
    INITIATOR_NULL_STATE)

ROW(INITIATOR_RESULT_WAIT,
    TR_Abort_Req, 
    1,
    {
     send_abort(init_machine, USER, event->u.TR_Abort_Req.abort_reason);
    },
    INITIATOR_NULL_STATE)

/*
 * Neither we check transaction class here: this can only be acknowledgement of
 * class 1 transaction.
 */
ROW(INITIATOR_RESULT_WAIT,
    RcvAck,
    event->u.RcvAck.tid_ok == 0,
    {
     stop_initiator_timer(init_machine->timer);

     wsp_event = create_tr_invoke_cnf(init_machine);
     dispatch_to_wsp(wsp_event);     
    },
    INITIATOR_NULL_STATE)

/*
 * This is a positive answer to a tid verification (negative one being 
 * already sent by init_machine_find_or_create).
 */
ROW(INITIATOR_RESULT_WAIT,
    RcvAck,
    event->u.RcvAck.tid_ok == 1 && init_machine->rcr < MAX_RCR,
    {
     send_ack(init_machine, TID_VERIFICATION, init_machine->rid);
     init_machine->tidok_sent = 1;

     ++init_machine->rcr;

     start_initiator_timer_R(init_machine);
    },
    INITIATOR_RESULT_WAIT)

/*
 * RCR must not be greater than RCR_MAX. One of corrections from MOT_WTP_CR_01.
 */ 
   ROW(INITIATOR_RESULT_WAIT,
       RcvAck,
       event->u.RcvAck.tid_ok,
       { },
       INITIATOR_RESULT_WAIT)

ROW(INITIATOR_RESULT_WAIT,
    RcvAbort,
    1,
    {
     wsp_event = create_tr_abort_ind(init_machine, 
                 event->u.RcvAbort.abort_reason);
     dispatch_to_wsp(wsp_event);
    },
    INITIATOR_NULL_STATE)

ROW(INITIATOR_RESULT_WAIT,
    RcvErrorPDU,
    1,
    {
     send_abort(init_machine, USER, PROTOERR);

     wsp_event = create_tr_abort_ind(init_machine, PROTOERR);
     dispatch_to_wsp(wsp_event);
    },
    INITIATOR_NULL_STATE)

ROW(INITIATOR_RESULT_WAIT,
    TimerTO_R,
    init_machine->rcr < MAX_RCR && !init_machine->tidok_sent,
    {
      WAPEvent *resend;

      ++init_machine->rcr;

     start_initiator_timer_R(init_machine);

     resend = wap_event_duplicate(init_machine->invoke);
     wtp_pack_set_rid(resend, init_machine->rid);
     dispatch_to_wdp(resend);
    },
    INITIATOR_RESULT_WAIT)

ROW(INITIATOR_RESULT_WAIT,
    TimerTO_R,
    init_machine->rcr < MAX_RCR && init_machine->tidok_sent,
    {
     ++init_machine->rcr;

     start_initiator_timer_R(init_machine);

     send_ack(init_machine, TID_VERIFICATION, init_machine->tidok_sent);
    },
    INITIATOR_RESULT_WAIT)

ROW(INITIATOR_RESULT_WAIT,
    TimerTO_R,
    init_machine->rcr == MAX_RCR, 
    {
     wsp_event = create_tr_abort_ind(init_machine, NORESPONSE);
     dispatch_to_wsp(wsp_event);
    },
    INITIATOR_NULL_STATE)

#undef ROW
#undef INIT_STATE_NAME
