PRSEV library
It provides a way to store several differently behaving applications in a single binary, and a way to define a function for each event state.
The App_Tag end device combines the handling of several sensors into a single application and switches the behaviour depending on the configuration.
Each vProcessEv_? .c file contains the definition of the application and the procedures for its individual behaviour.

Defining a callback function

Callback functions are defined statically, which is not a good way to combine multiple application behaviours into one. Here, they are defined static in the source file.
1
/**
2
* Hardware interrupt
3
* @param u32DeviceId
4
* @param u32ItemBitmap
5
* @return
6
*/
7
static uint8 cbAppToCoNet_u8HwInt(uint32 u32DeviceId, uint32 u32ItemBitmap) {
8
uint8 u8handled = FALSE;
9
switch (u32DeviceId) {
10
default:
11
break;
12
}
13
return u8handled;
14
}
15
16
/**
17
* Hardware events (delayed execution)
18
* @param u32DeviceId
19
* @param u32ItemBitmap
20
*/
21
static void cbAppToCoNet_vHwEvent(uint32 u32DeviceId, uint32 u32ItemBitmap) {
22
}
23
24
/**
25
* Main processing
26
*/
27
static void cbAppToCoNet_vMain() {
28
/* handle serial input */
29
vHandleSerialInput();
30
}
31
32
/**
33
* Network events
34
* @param eEvent
35
* @param u32arg
36
*/
37
static void cbAppToCoNet_vNwkEvent(teEvent eEvent, uint32 u32arg) {
38
}
39
40
/**
41
* RX events
42
* @param pRx
43
*/
44
static void cbAppToCoNet_vRxEvent(tsRxDataApp *pRx) {
45
}
46
47
/**
48
* TX events
49
* @param u8CbId
50
* @param bStatus
51
*/
52
static void cbAppToCoNet_vTxEvent(uint8 u8CbId, uint8 bStatus) {
53
// tx complete
54
ToCoNet_Event_Process(E_ORDER_KICK, 0, vProcessEvCore);
55
}
56
Copied!
Finally, we have the tsCbHandler structure in the list.
1
/**
2
* Application Handler Definition
3
* If it is not used, set it to `NULL`.
4
*/
5
static tsCbHandler sCbHandler = {
6
NULL, // cbAppToCoNet_u8HwInt,
7
cbAppToCoNet_vHwEvent,
8
NULL, // cbAppToCoNet_vMain,
9
NULL, // cbAppToCoNet_vNwkEvent,
10
NULL, // cbAppToCoNet_vRxEvent,
11
cbAppToCoNet_vTxEvent
12
};
13
Copied!

State definitions

You can define as many events as you wish to use.
1
/**
2
* Event processing function list
3
*/
4
static const tsToCoNet_Event_StateHandler asStateFuncTbl[] = {
5
PRSEV_HANDLER_TBL_DEF(E_STATE_IDLE),
6
PRSEV_HANDLER_TBL_DEF(E_STATE_RUNNING),
7
PRSEV_HANDLER_TBL_DEF(E_STATE_APP_WAIT_TX),
8
PRSEV_HANDLER_TBL_DEF(E_STATE_APP_SLEEP),
9
PRSEV_HANDLER_TBL_TRM
10
};
11
Copied!
An entity is defined as follows:
1
PRSEV_HANDLER_TBL_DEF(E_STATE_IDLE, tsEvent *pEv, teEvent eEvent, uint32 u32evarg) {
2
// State at the start
3
if (eEvent == E_EVENT_START_UP) {
4
// Event handling on power-up or sleep-wake
5
...
6
ToCoNet_Event_SetState(pEv, E_STATE_RUNNING);
7
}
8
}
9
10
PRSEV_HANDLER_TBL_DEF(E_STATE_RUNNING, tsEvent *pEv, teEvent eEvent, uint32 u32evarg) {
11
// Running (acquisition of sensors data, transmission requests, etc.)
12
bOk = bTransmitToParent( ... ); // Sending process. if ( bOk ) {
13
ToCoNet_Event_SetState(pEv, E_STATE_APP_WAIT_TX);
14
} else {
15
ToCoNet_Event_SetState(pEv, E_STATE_APP_SLEEP);
16
}
17
}
18
19
PRSEV_HANDLER_TBL_DEF(E_STATE_APP_WAIT_TX, tsEvent *pEv, teEvent eEvent, uint32 u32evarg) {
20
// Waiting for transmission completion
21
if (..) {
22
ToCoNet_Event_SetState(pEv, E_STATE_APP_SLEEP); // スリープ状態へ遷移
23
}
24
}
25
26
PRSEV_HANDLER_DEF(E_STATE_APP_SLEEP, tsEvent *pEv, teEvent eEvent, uint32 u32evarg) {
27
// Sleep procedure (example code below)
28
if (eEvent == E_EVENT_NEW_STATE) {
29
// Sleep API is always called in a place where it is called only once, such as in `E_EVENT_NEW_STATE`.
30
V_PRINTF(LB"Sleeping...");
31
V_FLUSH();
32
33
// In the case of Mininode, there is no special processing, but the pause processing
34
ToCoNet_Nwk_bPause(sAppData.pContextNwk);
35
36
// Enters cyclic sleep
37
// - 5 seconds after the first time, 5 seconds after the next time, based on the return to sleep
38
vSleep(sAppData.sFlash.sData.u32Slp, sAppData.u16frame_count == 1 ? FALSE : TRUE, FALSE);
39
}
40
}
Copied!

Event handling functions

The event handling function is defined as follows. The event processing is done according to asStateFuncTbl prepared in the previous state definition.
1
/**
2
* Event handling function
3
* @param pEv
4
* @param eEvent
5
* @param u32evarg
6
*/
7
static void vProcessEvCore(tsEvent *pEv, teEvent eEvent, uint32 u32evarg) {
8
ToCoNet_Event_StateExec(asStateFuncTbl, pEv, eEvent, u32evarg);
9
}
10
Copied!

Initialisation process

We will prepare an initialization function to store values in psCbHandler and pvProcessEv1, which are prepared as global variables as follows.
1
/**
2
* Init the app.
3
*/
4
void vInitAppStandard() {
5
psCbHandler = &sCbHandler;
6
pvProcessEv1 = vProcessEvCore;
7
}
Copied!

Running the application

This section describes the main file which contains the callback functions. The main file handles the calling of the functions for the individual processes defined above.
The user-defined event processing function and the TWELITE NET callback function should be defined as follows Call the application's own callback function, specified by the function pointer, to execute the application's own user-defined event processing.
1
void cbAppColdStart(bool_t bInit) {
2
if (bInit == FALSE) {
3
...
4
} else {
5
switch(u8AppType) {
6
case 0x00: vInitAppStandard(); break;// AppStandard で起動
7
...
8
}
9
...
10
ToCoNet_Event_Register_State_Machine(pvProcessEv1);
11
}
12
}
13
14
/**
15
* main procedure
16
*/
17
void cbToCoNet_vMain(void) {
18
if (psCbHandler && psCbHandler->pf_cbToCoNet_vMain) {
19
(*psCbHandler->pf_cbToCoNet_vMain)();
20
}
21
}
22
23
/**
24
* rx events
25
*/
26
void cbToCoNet_vRxEvent(tsRxDataApp *pRx) {
27
if (psCbHandler && psCbHandler->pf_cbToCoNet_vRxEvent) {
28
(*psCbHandler->pf_cbToCoNet_vRxEvent)(pRx);
29
}
30
}
31
32
/**
33
* tx completion event
34
*/
35
void cbToCoNet_vTxEvent(uint8 u8CbId, uint8 bStatus) {
36
if (psCbHandler && psCbHandler->pf_cbToCoNet_vTxEvent) {
37
(*psCbHandler->pf_cbToCoNet_vTxEvent)(u8CbId, bStatus);
38
}
39
}
40
41
/**
42
* network event
43
* @param eEvent
44
* @param u32arg
45
*/
46
void cbToCoNet_vNwkEvent(teEvent eEvent, uint32 u32arg) {
47
if (psCbHandler && psCbHandler->pf_cbToCoNet_vNwkEvent) {
48
(*psCbHandler->pf_cbToCoNet_vNwkEvent)(eEvent, u32arg);
49
}
50
}
51
52
/**
53
* Hardware event processing (interrupt delayed execution)
54
*/
55
void cbToCoNet_vHwEvent(uint32 u32DeviceId, uint32 u32ItemBitmap) {
56
if (psCbHandler && psCbHandler->pf_cbToCoNet_vHwEvent) {
57
(*psCbHandler->pf_cbToCoNet_vHwEvent)(u32DeviceId, u32ItemBitmap);
58
}
59
}
60
61
/**
62
* Hardware interrupt handler
63
*/
64
uint8 cbToCoNet_u8HwInt(uint32 u32DeviceId, uint32 u32ItemBitmap) {
65
bool_t bRet = FALSE;
66
if (psCbHandler && psCbHandler->pf_cbToCoNet_u8HwInt) {
67
bRet = (*psCbHandler->pf_cbToCoNet_u8HwInt)(u32DeviceId, u32ItemBitmap);
68
}
69
return bRet;
70
}
Copied!
最終更新 8mo ago