summaryrefslogtreecommitdiff
path: root/src/lib/MCCI_LoRaWAN_LMIC_library/src/lmic/lmic_compliance.h
blob: 8efe0e8e182d33f3fc4631637a34354ab6c7715d (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
135
136
137
138
/*

Module:  lmic_compliance.h

Function:
        Internal header file for compliance-related work.

Copyright notice and license info:
        See LICENSE file accompanying this project.

Author:
        Terry Moore, MCCI Corporation	March 2019

Description:
        This header file allows us to break up the compliance
        functions into multiple .c files if we wish.

*/

#ifndef _lmic_compliance_h_	/* prevent multiple includes */
#define _lmic_compliance_h_

#ifdef __cplusplus
extern "C"{
#endif

#ifndef _lmic_h_
# include "lmic.h"
#endif

#include <stdbool.h>
#include <stdint.h>

typedef struct lmic_compliance_s lmic_compliance_t;

// concrete type for the state enumeration for the compliance engine.
typedef uint8_t lmic_compliance_state_t;

enum lmic_compliance_state_e {
    LMIC_COMPLIANCE_STATE_IDLE = 0,       // app state
    LMIC_COMPLIANCE_STATE_STOPPING = 1,   // transitioning back to app
    LMIC_COMPLIANCE_STATE_ACTIVATING = 2, // transitioning to compliance state
    LMIC_COMPLIANCE_STATE_ACTIVE = 3,     // in compliance state
};

// return true if a state value indicates that the FSM is active.
static inline bool
lmic_compliance_state_IsActive(lmic_compliance_state_t s) {
    return s >= LMIC_COMPLIANCE_STATE_ACTIVATING;
}

// events from the outside world to the FSM
typedef uint8_t lmic_compliance_eventflags_t;

enum lmic_compliance_eventflags_e {
    LMIC_COMPLIANCE_EVENT_ACTIVATE      = 1u << 0,
    LMIC_COMPLIANCE_EVENT_DEACTIVATE    = 1u << 1,
    LMIC_COMPLIANCE_EVENT_TIMER_EXPIRED = 1u << 2,
    LMIC_COMPLIANCE_EVENT_UPLINK_COMPLETE = 1u << 3,
    LMIC_COMPLIANCE_EVENT_ECHO_REQUEST  = 1u << 4,
};

typedef uint8_t lmic_compliance_fsmflags_t;
enum lmic_compliance_fsmflags_e {
    LMIC_COMPLIANCE_FSM_ACTIVE          = 1u << 0,
    LMIC_COMPLIANCE_FSM_REENTERED       = 1u << 1,
    LMIC_COMPLIANCE_FSM_CONFIRM         = 1u << 2,
};

typedef uint8_t lmic_compliance_fsmstate_t;
enum lmic_compliance_fsmstate_e {
    LMIC_COMPLIANCE_FSMSTATE_INITIAL = 0,
    LMIC_COMPLIANCE_FSMSTATE_NOCHANGE = 1,
    LMIC_COMPLIANCE_FSMSTATE_ACTIVE = 2,
    LMIC_COMPLIANCE_FSMSTATE_INACTIVE = 3,
    LMIC_COMPLIANCE_FSMSTATE_TESTMODE = 4,   // sending test uplinks
    LMIC_COMPLIANCE_FSMSTATE_ECHOING = 5,
    LMIC_COMPLIANCE_FSMSTATE_REPORTING = 6,
    LMIC_COMPLIANCE_FSMSTATE_RECOVERY = 7,
    LMIC_COMPLIANCE_FSMSTATE_TXBUSY = 8,
};

#define LMIC_COMPLIANCE_FSMSTATE__NAMES   \
    "INITIAL", "NOCHANGE", "ACTIVE", "INACTIVE", "TESTMODE",   \
    "ECHOING", "REPORTING", "RECOVERY", "TXBUSY"

typedef struct lmic_compliance_eventcb_s lmic_compliance_eventcb_t;
struct lmic_compliance_eventcb_s {
	// save the user's event CB while active.
	lmic_event_cb_t		*pEventCb;
	// save the user's event data while active.
	void			*pUserData;
};

// structure for saving band settings during test
typedef struct lmic_compliance_band_s lmic_compliance_band_t;
struct lmic_compliance_band_s {
    u2_t    txcap;  // saved 1/duty cycle
};

// the state of the compliance engine.
struct lmic_compliance_s {
        // uint64
        // uintptr
        osjob_t                     timerJob;       // the job for driving uplinks
        osjob_t                     fsmJob;         // job for reevaluating the FSM.
        lmic_compliance_eventcb_t   saveEvent;      // the user's event handler.

        // uint32

        // uint16
#if CFG_LMIC_EU_like
        lmic_compliance_band_t      saveBands[MAX_BANDS];
#endif // CFG_LMIC_EU_like

        // we are required to maintain a downlink count
        // that is reset on join/test entry and incremented for
        // each valid test message.
        uint16_t                    downlinkCount;

        // uint8

        lmic_compliance_state_t         state;      // current state of compliance engine.
        lmic_compliance_eventflags_t    eventflags; // incoming events.
        lmic_compliance_fsmflags_t      fsmFlags;   // FSM operational flags
        lmic_compliance_fsmstate_t      fsmState;   // FSM current state

        uint8_t                 uplinkSize;
        uint8_t                 uplinkMessage[MAX_LEN_PAYLOAD];
};

extern lmic_compliance_t LMIC_Compliance;

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* _lmic_compliance_h_ */