test
[eleutheria.git] / fsm / states.c
blob929983cb35f26f23e1c59e66787af3ac93eed5e9
1 #include "types.h"
2 #include "states.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 /* Callback functions prototypes */
9 static unsigned int state_hashf(const void *pkey);
10 static int state_cmpf(const void *parg1, const void *parg2);
11 static void state_printf(const void *pkey, const void *pdata);
13 stret_t state_init(state_t **ppstate, size_t size, unsigned int factor)
15 /* Allocate memory state's event table */
16 if ((*ppstate = malloc(sizeof **ppstate)) == NULL)
17 return ST_NOMEM;
19 if (((*ppstate)->evttable = malloc(sizeof *(*ppstate)->evttable)) == NULL) {
20 free(*ppstate);
21 return ST_NOMEM;
24 /* Initialize hash table that stores the events the state can process */
25 if (htable_init((*ppstate)->evttable, size, factor,
26 state_hashf, state_cmpf, state_printf) == HT_NOMEM) {
27 free((*ppstate)->evttable);
28 free(*ppstate);
29 return ST_NOMEM;
32 /* Allocate memory for state's key */
33 if (((*ppstate)->st_key = malloc(sizeof *(*ppstate)->st_key)) == NULL) {
34 free((*ppstate)->evttable);
35 free(*ppstate);
36 return ST_NOMEM;
39 return ST_OK;
42 stret_t state_add_evt(state_t *pstate, unsigned int key, const char *pdesc,
43 void (*pactionf)(void *pdata), state_t *pnewstate)
45 event_t *pevt;
46 unsigned int *pkey;
48 /* Allocate memory for new key */
49 if ((pkey = malloc(sizeof *pkey)) == NULL)
50 return ST_NOMEM;
52 *pkey = key;
54 /* Allocate memory for new event */
55 if ((pevt = malloc(sizeof *pevt)) == NULL) {
56 free(pkey);
57 return ST_NOMEM;
60 /* Fill in structure's members */
61 strncpy(pevt->evt_desc, pdesc, MAX_EVT_DESC);
62 pevt->evt_actionf = pactionf;
63 pevt->evt_newstate = pnewstate;
65 /* Insert event to hash table */
66 if (htable_insert(pstate->evttable, pkey, pevt) == HT_EXISTS) {
67 free(pkey);
68 free(pevt);
69 return ST_EXISTS;
72 return ST_OK;
75 stret_t state_rem_evt(state_t *pstate, unsigned int key)
77 if (htable_free_obj(pstate->evttable, &key, HT_FREEKEY | HT_FREEDATA) == HT_NOTFOUND)
78 return ST_NOTFOUND;
80 return ST_OK;
83 unsigned int state_get_key(state_t *pstate)
85 return *pstate->st_key;
88 stret_t state_free(state_t *pstate)
90 htable_free_all_obj(pstate->evttable, HT_FREEKEY | HT_FREEDATA);
91 htable_free(pstate->evttable);
92 free(pstate->evttable);
93 free(pstate);
95 return ST_OK;
98 void state_print_evts(const state_t *pstate, FILE *fp)
100 htable_print(pstate->evttable, fp);
103 /* Callback funtions */
104 static unsigned int state_hashf(const void *pkey)
106 return *(const unsigned int *) pkey;
109 static int state_cmpf(const void *parg1, const void *parg2)
111 unsigned int a = *(const unsigned int *) parg1;
112 unsigned int b = *(const unsigned int *) parg2;
114 if (a > b)
115 return -1;
116 else if (a == b)
117 return 0;
118 else
119 return 1;
122 static void state_printf(const void *pkey, const void *pdata)
124 const event_t *pevt;
126 pevt = (const event_t *) pdata;
127 printf("evtkey: %u\tdesc: %s\tnewstate: %u",
128 *(const unsigned int *) pkey,
129 pevt->evt_desc,
130 *(const unsigned int *) pevt->evt_newstate->st_key);