Dummy commit to test new ssh key
[eleutheria.git] / fsm / states.c
blob1b4526876d230222fa02e7615047f19f3696a55c
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 /* Initialize flags */
40 STATE_MARK_AS_UNREACHABLE(*ppstate);
42 return ST_OK;
45 stret_t state_add_evt(state_t *pstate, unsigned int key, const char *pdesc,
46 void (*pactionf)(void *pdata), state_t *pnewstate)
48 event_t *pevt;
49 unsigned int *pkey;
51 /* Allocate memory for new key */
52 if ((pkey = malloc(sizeof *pkey)) == NULL)
53 return ST_NOMEM;
55 *pkey = key;
57 /* Allocate memory for new event */
58 if ((pevt = malloc(sizeof *pevt)) == NULL) {
59 free(pkey);
60 return ST_NOMEM;
63 /* Fill in structure's members */
64 strncpy(pevt->evt_desc, pdesc, MAX_EVT_DESC);
65 pevt->evt_actionf = pactionf;
66 pevt->evt_newstate = pnewstate;
68 /* Insert event to hash table */
69 if (htable_insert(pstate->evttable, pkey, pevt) == HT_EXISTS) {
70 free(pkey);
71 free(pevt);
72 return ST_EXISTS;
75 return ST_OK;
78 stret_t state_rem_evt(state_t *pstate, unsigned int key)
80 if (htable_free_obj(pstate->evttable, &key, HT_FREEKEY | HT_FREEDATA) == HT_NOTFOUND)
81 return ST_NOTFOUND;
83 return ST_OK;
86 unsigned int state_get_key(state_t *pstate)
88 return *pstate->st_key;
91 stret_t state_free(state_t *pstate)
93 htable_free_all_obj(pstate->evttable, HT_FREEKEY | HT_FREEDATA);
94 htable_free(pstate->evttable);
95 free(pstate->evttable);
96 free(pstate);
98 return ST_OK;
101 void state_print_evts(const state_t *pstate, FILE *fp)
103 htable_print(pstate->evttable, fp);
106 /* Callback funtions */
107 static unsigned int state_hashf(const void *pkey)
109 return *(const unsigned int *) pkey;
112 static int state_cmpf(const void *parg1, const void *parg2)
114 unsigned int a = *(const unsigned int *) parg1;
115 unsigned int b = *(const unsigned int *) parg2;
117 if (a > b)
118 return -1;
119 else if (a == b)
120 return 0;
121 else
122 return 1;
125 static void state_printf(const void *pkey, const void *pdata)
127 const event_t *pevt;
129 pevt = (const event_t *) pdata;
130 printf("evtkey: %u\tdesc: %s\tnewstate: %u",
131 *(const unsigned int *) pkey,
132 pevt->evt_desc,
133 *(const unsigned int *) pevt->evt_newstate->st_key);