Skip to content

Commit d9b6ed5

Browse files
Merge pull request #459 from Luos-io/feat/multi_auto-update
Enable multi auto_update management, fix #450
2 parents 60bd8e9 + 3b3f17f commit d9b6ed5

File tree

8 files changed

+148
-63
lines changed

8 files changed

+148
-63
lines changed

engine/IO/src/luos_io.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,7 @@ error_return_t LuosIO_ConsumeMsg(const msg_t *input)
660660
case UPDATE_PUB:
661661
// This service need to be auto updated
662662
TimeOD_TimeFromMsg(&time, input);
663-
service->auto_refresh.target = input->header.source;
664-
service->auto_refresh.time_ms = (uint16_t)TimeOD_TimeTo_ms(time);
665-
service->auto_refresh.last_update = LuosHAL_GetSystick();
663+
Service_AddAutoUpdateTarget(service, input->header.source, (uint16_t)TimeOD_TimeTo_ms(time));
666664
return SUCCEED;
667665
break;
668666
//**************************************** bootloader section ****************************************

engine/core/inc/service.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void Service_ResetStatistics(void);
3535
void Service_GenerateId(uint16_t base_id);
3636
void Service_ClearId(void);
3737
uint16_t Service_GetIndex(service_t *service);
38+
void Service_AddAutoUpdateTarget(service_t *service, uint16_t target, uint16_t time_ms);
3839
void Service_RmAutoUpdateTarget(uint16_t service_id);
3940
void Service_AutoUpdateManager(void);
4041
error_return_t Service_Deliver(phy_job_t *job);

engine/core/inc/struct_luos.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,6 @@ typedef struct __attribute__((__packed__))
133133
};
134134
} msg_t;
135135

136-
/******************************************************************************
137-
* This structure is used to manage services timed auto update
138-
* please refer to the documentation
139-
******************************************************************************/
140-
typedef struct timed_update_t
141-
{
142-
uint32_t last_update;
143-
uint16_t time_ms;
144-
uint16_t target;
145-
} timed_update_t;
146-
147136
/******************************************************************************
148137
* This structure is used to manage dead target message
149138
* Service_id or node_id can be set to 0 to ignore it.
@@ -217,11 +206,22 @@ typedef struct service_t
217206
// Private Variables
218207
uint16_t last_topic_position; /*!< Position pointer of the last topic added. */
219208
uint16_t topic_list[MAX_LOCAL_TOPIC_NUMBER]; /*!< multicast target bank. */
220-
timed_update_t auto_refresh; /*!< service auto refresh context. */
221209
void *profile_context; /*!< Pointer to the profile context. */
222210

223211
} service_t;
224212

213+
/******************************************************************************
214+
* This structure is used to manage services timed auto update
215+
* please refer to the documentation
216+
******************************************************************************/
217+
typedef struct timed_update_t
218+
{
219+
service_t *service;
220+
uint32_t last_update;
221+
uint16_t time_ms;
222+
uint16_t target;
223+
} timed_update_t;
224+
225225
typedef void (*SERVICE_CB)(service_t *service, const msg_t *msg);
226226

227227
#endif /*__LUOS_STRUCT_H */

engine/core/src/service.c

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct
2020
{
2121
service_t list[MAX_LOCAL_SERVICE_NUMBER];
2222
uint16_t number;
23+
timed_update_t auto_refresh[MAX_AUTO_REFRESH_NUMBER]; /*!< service auto refresh context. */
2324
} service_ctx_t;
2425

2526
/*******************************************************************************
@@ -107,11 +108,9 @@ void Service_ClearId(void)
107108
{
108109
for (uint16_t i = 0; i < service_ctx.number; i++)
109110
{
110-
service_ctx.list[i].id = DEFAULTID;
111-
service_ctx.list[i].auto_refresh.target = 0;
112-
service_ctx.list[i].auto_refresh.time_ms = 0;
113-
service_ctx.list[i].auto_refresh.last_update = 0;
111+
service_ctx.list[i].id = DEFAULTID;
114112
}
113+
memset((void *)service_ctx.auto_refresh, 0, sizeof(timed_update_t) * MAX_AUTO_REFRESH_NUMBER);
115114
}
116115

117116
/******************************************************************************
@@ -125,20 +124,41 @@ uint16_t Service_GetIndex(service_t *service)
125124
return ((uintptr_t)service - (uintptr_t)service_ctx.list) / sizeof(service_t);
126125
}
127126

127+
/******************************************************************************
128+
* @brief Remove all services auto update targetting this service_id
129+
* @param service_id
130+
* @return None
131+
******************************************************************************/
132+
void Service_AddAutoUpdateTarget(service_t *service, uint16_t target, uint16_t time_ms)
133+
{
134+
LUOS_ASSERT(service && (time_ms > 0) && (target != 0));
135+
for (uint16_t i = 0; i < MAX_AUTO_REFRESH_NUMBER; i++)
136+
{
137+
if (service_ctx.auto_refresh[i].time_ms == 0)
138+
{
139+
service_ctx.auto_refresh[i].service = service;
140+
service_ctx.auto_refresh[i].target = target;
141+
service_ctx.auto_refresh[i].time_ms = time_ms;
142+
service_ctx.auto_refresh[i].last_update = LuosHAL_GetSystick();
143+
return;
144+
}
145+
}
146+
// No more space for auto update
147+
LUOS_ASSERT(0);
148+
}
149+
128150
/******************************************************************************
129151
* @brief Remove all services auto update targetting this service_id
130152
* @param service_id
131153
* @return None
132154
******************************************************************************/
133155
void Service_RmAutoUpdateTarget(uint16_t service_id)
134156
{
135-
for (uint16_t i = 0; i < service_ctx.number; i++)
157+
for (uint16_t i = 0; i < MAX_AUTO_REFRESH_NUMBER; i++)
136158
{
137-
if (service_ctx.list[i].auto_refresh.target == service_id)
159+
if (service_ctx.auto_refresh[i].target == service_id)
138160
{
139-
service_ctx.list[i].auto_refresh.target = 0;
140-
service_ctx.list[i].auto_refresh.time_ms = 0;
141-
service_ctx.list[i].auto_refresh.last_update = 0;
161+
memset((void *)&service_ctx.auto_refresh[i], 0, sizeof(timed_update_t));
142162
}
143163
}
144164
}
@@ -150,45 +170,44 @@ void Service_RmAutoUpdateTarget(uint16_t service_id)
150170
******************************************************************************/
151171
void Service_AutoUpdateManager(void)
152172
{
153-
// Check all services timed_update_t contexts
154-
for (uint16_t i = 0; i < service_ctx.number; i++)
173+
// Check if detection is OK
174+
if (Node_GetState() != DETECTION_OK)
155175
{
156-
// check if services have an actual ID. If not, we are in detection mode and should reset the auto refresh
157-
if (service_ctx.list[i].id == DEFAULTID)
158-
{
159-
// this service have not been detected or is in detection mode. remove auto_refresh parameters
160-
service_ctx.list[i].auto_refresh.target = 0;
161-
service_ctx.list[i].auto_refresh.time_ms = 0;
162-
service_ctx.list[i].auto_refresh.last_update = 0;
163-
}
164-
else
176+
// We have not been detected or we are in detection mode. remove auto_refresh parameters
177+
memset((void *)&service_ctx.auto_refresh, 0, sizeof(timed_update_t) * MAX_AUTO_REFRESH_NUMBER);
178+
}
179+
else
180+
{
181+
// Check all timed_update_t
182+
for (uint16_t i = 0; i < MAX_AUTO_REFRESH_NUMBER; i++)
165183
{
166184
// check if there is a timed update setted and if it's time to update it.
167-
if (service_ctx.list[i].auto_refresh.time_ms)
185+
if (service_ctx.auto_refresh[i].time_ms)
168186
{
169-
if ((LuosHAL_GetSystick() - service_ctx.list[i].auto_refresh.last_update) >= service_ctx.list[i].auto_refresh.time_ms)
187+
if ((LuosHAL_GetSystick() - service_ctx.auto_refresh[i].last_update) >= service_ctx.auto_refresh[i].time_ms)
170188
{
171189
// This service need to send an update
172190
// Create a fake message for it from the service asking for update
173191
msg_t updt_msg;
192+
LUOS_ASSERT(service_ctx.auto_refresh[i].service);
174193
updt_msg.header.config = BASE_PROTOCOL;
175-
updt_msg.header.target = service_ctx.list[i].id;
176-
updt_msg.header.source = service_ctx.list[i].auto_refresh.target;
194+
updt_msg.header.target = service_ctx.auto_refresh[i].service->id;
195+
updt_msg.header.source = service_ctx.auto_refresh[i].target;
177196
updt_msg.header.target_mode = SERVICEIDACK;
178197
updt_msg.header.cmd = GET_CMD;
179198
updt_msg.header.size = 0;
180-
if ((service_ctx.list[i].service_cb != 0))
199+
if ((service_ctx.auto_refresh[i].service->service_cb != 0))
181200
{
182-
service_ctx.list[i].service_cb(&service_ctx.list[i], &updt_msg);
201+
service_ctx.auto_refresh[i].service->service_cb(service_ctx.auto_refresh[i].service, &updt_msg);
183202
}
184203
else
185204
{
186205
if (Node_GetState() == DETECTION_OK)
187206
{
188-
Luos_SendMsg(&service_ctx.list[i], &updt_msg);
207+
Luos_SendMsg(service_ctx.auto_refresh[i].service, &updt_msg);
189208
}
190209
}
191-
service_ctx.list[i].auto_refresh.last_update = LuosHAL_GetSystick();
210+
service_ctx.auto_refresh[i].last_update = LuosHAL_GetSystick();
192211
}
193212
}
194213
}

engine/engine_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#define MAX_LOCAL_SERVICE_NUMBER 5 // The maximum number of local services
2323
#endif
2424

25+
#ifndef MAX_AUTO_REFRESH_NUMBER
26+
#define MAX_AUTO_REFRESH_NUMBER MAX_LOCAL_SERVICE_NUMBER // The maximum number of auto refresh in the node
27+
#endif
28+
2529
#ifndef MAX_SERVICE_NUMBER
2630
#define MAX_SERVICE_NUMBER 20 // The maximum number of services in the complete architecture
2731
#endif

network/ws_network/HAL/ARDUINO/ws_hal_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef SECRET_SSID
1313
#warning "You must define SECRET_SSID and SECRET_PASS in your node_config.h"
1414
#endif
15-
#ifndef SECRESECRET_PASST_SSID
15+
#ifndef SECRET_PASS
1616
#warning "You must define SECRET_SSID and SECRET_PASS in your node_config.h"
1717
#endif
1818
#endif /* _WS_CONFIG_H_ */

test/tests_core/test_service/main.c

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,80 @@ void unittest_Service_GetIndex(void)
211211
}
212212
}
213213

214+
void unittest_Service_AddAutoUpdateTarget(void)
215+
{
216+
NEW_TEST_CASE("Test Service_AddAutoUpdateTarget assert conditions");
217+
{
218+
TRY
219+
{
220+
Service_AddAutoUpdateTarget(NULL, 1, 10);
221+
}
222+
TEST_ASSERT_TRUE(IS_ASSERT());
223+
END_TRY;
224+
225+
TRY
226+
{
227+
Service_AddAutoUpdateTarget(&service_ctx.list[0], 0, 10);
228+
}
229+
TEST_ASSERT_TRUE(IS_ASSERT());
230+
END_TRY;
231+
232+
TRY
233+
{
234+
Service_AddAutoUpdateTarget(&service_ctx.list[0], 1, 0);
235+
}
236+
TEST_ASSERT_TRUE(IS_ASSERT());
237+
END_TRY;
238+
}
239+
NEW_TEST_CASE("Test Service_AddAutoUpdateTarget");
240+
{
241+
TRY
242+
{
243+
Service_AddAutoUpdateTarget(&service_ctx.list[0], 2, 20);
244+
TEST_ASSERT_EQUAL(2, service_ctx.auto_refresh[0].target);
245+
TEST_ASSERT_EQUAL(20, service_ctx.auto_refresh[0].time_ms);
246+
TEST_ASSERT_EQUAL(&service_ctx.list[0], service_ctx.auto_refresh[0].service);
247+
Service_AddAutoUpdateTarget(&service_ctx.list[0], 3, 10);
248+
TEST_ASSERT_EQUAL(2, service_ctx.auto_refresh[0].target);
249+
TEST_ASSERT_EQUAL(20, service_ctx.auto_refresh[0].time_ms);
250+
TEST_ASSERT_EQUAL(&service_ctx.list[0], service_ctx.auto_refresh[0].service);
251+
TEST_ASSERT_EQUAL(3, service_ctx.auto_refresh[1].target);
252+
TEST_ASSERT_EQUAL(10, service_ctx.auto_refresh[1].time_ms);
253+
TEST_ASSERT_EQUAL(&service_ctx.list[0], service_ctx.auto_refresh[1].service);
254+
Service_AddAutoUpdateTarget(&service_ctx.list[1], 1, 5);
255+
TEST_ASSERT_EQUAL(2, service_ctx.auto_refresh[0].target);
256+
TEST_ASSERT_EQUAL(20, service_ctx.auto_refresh[0].time_ms);
257+
TEST_ASSERT_EQUAL(&service_ctx.list[0], service_ctx.auto_refresh[0].service);
258+
TEST_ASSERT_EQUAL(3, service_ctx.auto_refresh[1].target);
259+
TEST_ASSERT_EQUAL(10, service_ctx.auto_refresh[1].time_ms);
260+
TEST_ASSERT_EQUAL(&service_ctx.list[0], service_ctx.auto_refresh[1].service);
261+
TEST_ASSERT_EQUAL(1, service_ctx.auto_refresh[2].target);
262+
TEST_ASSERT_EQUAL(5, service_ctx.auto_refresh[2].time_ms);
263+
TEST_ASSERT_EQUAL(&service_ctx.list[1], service_ctx.auto_refresh[2].service);
264+
}
265+
CATCH
266+
{
267+
TEST_ASSERT_TRUE(false);
268+
}
269+
END_TRY;
270+
}
271+
}
272+
214273
void unittest_Service_RmAutoUpdateTarget(void)
215274
{
216275
NEW_TEST_CASE("Test Service_RmAutoUpdateTarget");
217276
{
218277
TRY
219278
{
220-
service_ctx.number = 10;
221-
service_ctx.list[2].auto_refresh.target = 2;
222-
service_ctx.list[2].auto_refresh.time_ms = 20;
223-
service_ctx.list[2].auto_refresh.last_update = 30;
279+
service_ctx.number = 10;
280+
service_ctx.auto_refresh[0].service = &service_ctx.list[0];
281+
service_ctx.auto_refresh[0].target = 2;
282+
service_ctx.auto_refresh[0].time_ms = 20;
283+
service_ctx.auto_refresh[0].last_update = 30;
224284
Service_RmAutoUpdateTarget(2);
225-
TEST_ASSERT_EQUAL(0, service_ctx.list[2].auto_refresh.target);
226-
TEST_ASSERT_EQUAL(0, service_ctx.list[2].auto_refresh.time_ms);
227-
TEST_ASSERT_EQUAL(0, service_ctx.list[2].auto_refresh.last_update);
285+
TEST_ASSERT_EQUAL(0, service_ctx.auto_refresh[0].target);
286+
TEST_ASSERT_EQUAL(0, service_ctx.auto_refresh[0].time_ms);
287+
TEST_ASSERT_EQUAL(0, service_ctx.auto_refresh[0].last_update);
228288
}
229289
CATCH
230290
{
@@ -243,11 +303,12 @@ void unittest_Service_AutoUpdateManager(void)
243303
// Init default scenario context
244304
Init_Context();
245305
Luos_Loop();
246-
service_ctx.list[2].auto_refresh.target = 1;
247-
service_ctx.list[2].auto_refresh.time_ms = 10;
248-
service_ctx.list[2].auto_refresh.last_update = 30;
306+
service_ctx.auto_refresh[0].service = default_sc.App_3.app;
307+
service_ctx.auto_refresh[0].target = 1;
308+
service_ctx.auto_refresh[0].time_ms = 10;
309+
service_ctx.auto_refresh[0].last_update = 30;
249310
Service_AutoUpdateManager();
250-
TEST_ASSERT_NOT_EQUAL(30, service_ctx.list[2].auto_refresh.last_update);
311+
TEST_ASSERT_NOT_EQUAL(30, service_ctx.auto_refresh[0].last_update);
251312
TEST_ASSERT_EQUAL(GET_CMD, default_sc.App_3.last_rx_msg.header.cmd);
252313
TEST_ASSERT_EQUAL(1, default_sc.App_3.last_rx_msg.header.source);
253314
TEST_ASSERT_EQUAL(3, default_sc.App_3.last_rx_msg.header.target);
@@ -534,6 +595,7 @@ int main(int argc, char **argv)
534595
UNIT_TEST_RUN(unittest_Service_GenerateId);
535596
UNIT_TEST_RUN(unittest_Service_ClearId);
536597
UNIT_TEST_RUN(unittest_Service_GetIndex);
598+
UNIT_TEST_RUN(unittest_Service_AddAutoUpdateTarget);
537599
UNIT_TEST_RUN(unittest_Service_RmAutoUpdateTarget);
538600
UNIT_TEST_RUN(unittest_Service_AutoUpdateManager);
539601
UNIT_TEST_RUN(unittest_Service_GetConcerned);

test/tests_io/test_luos_io/main.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,13 @@ void unittest_luosIO_ConsumeMsg()
723723
Luos_handled_job = NULL;
724724
Robus_handled_job = NULL;
725725

726-
Node_Get()->node_id = 1;
727-
service_ctx.number = 2;
728-
service_ctx.list[0].id = 1;
729-
service_ctx.list[0].auto_refresh.target = 0;
730-
service_ctx.list[0].auto_refresh.time_ms = 0;
731-
service_ctx.list[0].auto_refresh.last_update = 0;
726+
Node_Get()->node_id = 1;
727+
service_ctx.number = 2;
728+
service_ctx.list[0].id = 1;
729+
service_ctx.auto_refresh[0].service = &service_ctx.list[0];
730+
service_ctx.auto_refresh[0].target = 0;
731+
service_ctx.auto_refresh[0].time_ms = 0;
732+
service_ctx.auto_refresh[0].last_update = 0;
732733
// Generate the filters
733734
Service_GenerateId(1);
734735

@@ -742,8 +743,8 @@ void unittest_luosIO_ConsumeMsg()
742743

743744
// Check received message content
744745
TEST_ASSERT_EQUAL(SUCCEED, ret_val);
745-
TEST_ASSERT_EQUAL(1, service_ctx.list[0].auto_refresh.target);
746-
TEST_ASSERT_EQUAL((uint16_t)TimeOD_TimeTo_ms(time), service_ctx.list[0].auto_refresh.time_ms);
746+
TEST_ASSERT_EQUAL(1, service_ctx.auto_refresh[0].target);
747+
TEST_ASSERT_EQUAL((uint16_t)TimeOD_TimeTo_ms(time), service_ctx.auto_refresh[0].time_ms);
747748
}
748749
CATCH
749750
{

0 commit comments

Comments
 (0)