summaryrefslogtreecommitdiff
path: root/cp/station/src/station_apivs.c
blob: e5dd770cba0f2e556f97538581179c2a86ea1368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/station/src/station_apivs.c
 * \brief   here are all the functions to control the visual state fsm
 * \ingroup cp_station
 */


#include "common/std.h"
#include "cp/station/inc/station_apivs.h"



// Define SEM_CONTEXT storage
static SEM_CONTEXT *pSEMContext;


void
station_init_fsm(void)
{
    // Define completion code storage
    unsigned char cc;
    
    //printf("%s\n", __FUNCTION__);
    // Initialize the VS System.
    if ((cc = SMP_Connect(&pSEMContext, &main_fsm)) != SES_OKAY)
        dbg_assert(0); // error handling
    // Initialize all needed data
    main_fsmSMP_InitAll(&pSEMContext);
    // send the reset event to the fsm
    if(sta_add_event(SE_RESET, NULL) != EV_OK) dbg_assert(0);
}



void
station_process_fsm_event(void)
{
    event_t event;    
    // Define completion code storage
    unsigned char cc;
    // Define action expression variable.
    SEM_ACTION_EXPRESSION_TYPE actionExpressNo;

    dbg_assert(pSEMContext);
    //printf("%s\n", __FUNCTION__);
    // get the event from the handler
    while(sta_get_event(&event) != EV_QUEUE_EMPTY)
    {
        //printf("event type : %i\n", event.event_type);
        // Deduct the event
        if ((cc = main_fsmSMP_Deduct(pSEMContext, event.event_type, event.mme_address)) != SES_OKAY)          
            dbg_assert(0); // error handling
        // Get resulting action expressions and execute them.
        while ((cc = SMP_GetOutput(pSEMContext, &actionExpressNo)) == SES_FOUND)
        {
            //printf("SMP_GetOutput : context %i, mainVSaction %i, actionexpressno %i\n", pSEMContext, MainVSAction , actionExpressNo);        
            SMP_TableAction(pSEMContext, MainVSAction , actionExpressNo);
        }
        if (cc != SES_OKAY)
            dbg_assert(0); // error handling
        // Change the next state vector.
        if ((cc = SMP_NextState(pSEMContext)) != SES_OKAY)
            dbg_assert(0); // error handling
        // finaly, free the message buffer (if ir exist)
        if(event.mme_address)
        {
            dbg_assert ( !msg_check_wrong_mme_const_values (event.mme_address));
            interf_release_buf(event.mme_address);
        }        
    }
}


// this function is for test purpose only
void
station_test_fsm(void)
{
/*    msg_mme_t *msg;
    msg_param_t msg_param;

    msg = msg_sending_common_part("ABCEFG", &msg_param);


    printf("start of fsm test\n");
    // send the reset event to the fsm
    if(sta_add_event(SE_RESET, NULL) != EV_OK) dbg_assert(0);
    station_process_fsm_event();
    station_print_fsm_states();
    // send the USTA event to the fsm
    if(sta_add_event(TO_USTA, NULL) != EV_OK) dbg_assert(0);
    station_process_fsm_event();
    station_print_fsm_states();
    // set the fsm to UCCO
    if(sta_add_event(BEACON_TIMER_EXPIRES, NULL) != EV_OK) dbg_assert(0);
    station_process_fsm_event();
    station_print_fsm_states();
    // go to associated sta state
    if(sta_add_event(SE_RESET, NULL) != EV_OK) dbg_assert(0);
    if(sta_add_event(TO_STA, msg) != EV_OK) dbg_assert(0);
    station_process_fsm_event();
    station_print_fsm_states();
    // go to CCO
    if(sta_add_event(BECOME_BACKUP_CCO, NULL) != EV_OK) dbg_assert(0);
    if(sta_add_event(BEACON_TIMER_EXPIRES, NULL) != EV_OK) dbg_assert(0);
    station_process_fsm_event();
    station_print_fsm_states();
    */            
}


// this function is for test purpose only
void station_print_fsm_states(void)
{
    SEM_STATE_MACHINE_TYPE i;
    SEM_STATE_TYPE StateNo = STATE_UNDEFINED;

    for (i = 0 ; i < VS_NOF_STATE_MACHINES ; i++)
    {
        if (SMP_State (pSEMContext, i, &StateNo) != SES_FOUND)
          printf ("\nState machine is in undefined state\n");
        else
          printf ("%2d ; ", StateNo);
    }
    printf("\n");
}