Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b895e6c26d763257935a6826cd8a4e4bc9dc65a (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2008, 2013 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution. 
 *
 * The Eclipse Public License is available at 
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial API and implementation and/or initial documentation
 *******************************************************************************/

/**
 * 
 * Description : MQTT-SN timer file 
 * 
 */


#include <stdlib.h>
#include <string.h>

#include "mqtts-timer.h"
#include "gp_api.h"
#include "mqtts_api.h"


/* Timer ids */
static unsigned char timer_ack;
static unsigned char timer_keep_alive;
static unsigned char timer_wait_searchgw;
static unsigned char timer_wait_gwinfo;

static unsigned char gAckMissedCnt=0;

/* Keep-alive timer value */
static unsigned char gKeepAliveTime[2]={0,0};

/* SEARCHGW procedure */
/* simplified procedure 
 * SEARCHGW delay: double after each time-out acc. until it reaches 255 sec
 */
static unsigned char gSearchGwDelay = SEARCHGW_MIN_DELAY;

/*callback functions when time-out */
void timeoutcb_ack(void);
void timeoutcb_keep_alive(void);
void timeoutcb_wait_searchgw(void);
void timeoutcb_wait_gwinfo(void);

/* functions prototypes, see mqtts-core.c */
void lost_gw(void);
void mqtts_pingreq(void);
void mqtts_searchgw(void);
void mqtts_gwinfo(void);
void send_backupMsg(void);

/**
 * init timer component */
void mqtts_timer_init(void) {
	/* ask gp to create timers */
	timer_ack = gp_timer_new(timeoutcb_ack);
	timer_keep_alive = gp_timer_new(timeoutcb_keep_alive);
	timer_wait_searchgw = gp_timer_new(timeoutcb_wait_searchgw);
	timer_wait_gwinfo = gp_timer_new(timeoutcb_wait_gwinfo);
}

/**
 * set the keep alive timer value, value in sec */
void mqtts_timer_set_keep_alive_time(unsigned char *time) {
	gKeepAliveTime[0] = time[0]; /*Most significant byte*/
	gKeepAliveTime[1] = time[1]; /*Least significant byte */
}

/**
 * stop and release all timers */
void mqtts_timer_end(void) {
	gp_timer_end(timer_ack);
	gp_timer_end(timer_keep_alive);
	gp_timer_end(timer_wait_searchgw);
	gp_timer_end(timer_wait_gwinfo);
	gAckMissedCnt=0;
	gSearchGwDelay=SEARCHGW_MIN_DELAY;
}

/**
 * start ACK timer */
void mqtts_timer_start_ack(void) {
	gp_timer_start(timer_ack, 0, ACK_TIME); 
}


/**
 * start keep alive timer */
void mqtts_timer_start_keep_alive(void) {
	gp_timer_start(timer_keep_alive, gKeepAliveTime[0], gKeepAliveTime[1]);
}


/**
 * start timer for SEARCHGW */
void mqtts_timer_start_wait_searchgw() {
	gp_timer_start(timer_wait_searchgw,0,gSearchGwDelay);

	/* douple time delay for SEARCHGW after every time-out
	 * until it reaches 255 sec */
	if ((gSearchGwDelay==0x80) || (gSearchGwDelay==0xFF)) {
		gSearchGwDelay = 0xFF;
	} else {
		gSearchGwDelay = gSearchGwDelay << 1;
	}
}

/**
 * start timer for GWINFO */
void mqtts_timer_start_wait_gwinfo() {
	gp_timer_start(timer_wait_gwinfo,0,GWINFO_MIN_DELAY);
}

/**
 * stop ack timer and reset gAckMissedCnt */
void mqtts_timer_stop_ack(void) {
	gp_timer_stop(timer_ack);
	gAckMissedCnt=0;
}
/**
 * stop keep alive timer */
void mqtts_timer_stop_keep_alive(void) {
	gp_timer_stop(timer_keep_alive);
}
/**
 * stop wait GWINFO timer */
void mqtts_timer_stop_wait_gwinfo(void) {
	gp_timer_stop(timer_wait_gwinfo);
}
/**
 * stop wait SEARCHGW timer */
void mqtts_timer_stop_wait_searchgw(void) {
	gp_timer_stop(timer_wait_searchgw);
	gSearchGwDelay = 0x01;
}

/**
 * ACK time-out */
void timeoutcb_ack(void) {
	/* increment counter and stop timer */
	gAckMissedCnt++;
	/*no need for stop, timer already stopped when times out*/
	/*gp_timer_stop(timer_ack);*/

	if (gAckMissedCnt > MAX_ACK_MISSED) { 
		/* too many ACKs missed */  
		lost_gw();
		gAckMissedCnt=0;
	} else { 
		/* send backup message */
		send_backupMsg();
	}
} 

/**
 * Keep alive time out: send a PINGREQ to gw */
void timeoutcb_keep_alive(void) {
	mqtts_pingreq();
}

/**
 * Wait GWINFO time out => send GWINFO */
void timeoutcb_wait_gwinfo(void) {
	mqtts_gwinfo();     
}
/**
 * Wait SEARCHGW time out => send SERACHGW */
void timeoutcb_wait_searchgw(void) {
	mqtts_searchgw();
}

Back to the top