2019-04-12 13:14:52 +00:00
# include "contiki.h"
# include "net/netstack.h"
# include "net/nullnet/nullnet.h"
# include <stdio.h>
# include <stdlib.h>
# include "sys/log.h"
# include "os/dev/leds.h"
2019-04-14 16:24:52 +00:00
# include "../common/supermarket_net.h"
2019-04-12 13:14:52 +00:00
# include "assigner_fun.h"
2019-04-14 16:24:52 +00:00
//function invoked in order to look for the most charged cart to assign to the new arrived client
2019-04-12 13:14:52 +00:00
cart * cart_selection ( )
{
uint8_t highest_battery = 0 ;
cart * selected = NULL ;
cart * current = cart_list ;
while ( current )
{
2019-04-14 16:24:52 +00:00
if ( ! ( current - > assigned ) & & ( current - > battery_status > highest_battery ) )
2019-04-12 13:14:52 +00:00
{
highest_battery = current - > battery_status ;
selected = current ;
}
current = current - > next ;
}
return selected ;
}
//Insert a new cart in the list with the battery info just arrived
2019-04-14 16:24:52 +00:00
bool insert_cart ( uint8_t new_req_battery , linkaddr_t mac_cart_addr )
2019-04-12 13:14:52 +00:00
{
cart * new_arrived_cart = ( cart * ) malloc ( sizeof ( cart ) ) ;
if ( new_arrived_cart = = NULL )
{
printf ( " Association Failed " ) ;
return false ;
}
2019-04-16 13:51:46 +00:00
cart * c = cart_list ;
2019-04-16 14:29:18 +00:00
while ( c )
{
if ( linkaddr_cmp ( & ( c - > cart_address ) , & mac_cart_addr ) = = 0 )
c = c - > next ;
}
2019-04-16 13:51:46 +00:00
if ( c )
2019-04-16 13:53:28 +00:00
printf ( " Cart already associated! \n " ) ;
2019-04-12 13:14:52 +00:00
else
{
new_arrived_cart - > cart_address = mac_cart_addr ;
new_arrived_cart - > battery_status = new_req_battery ;
new_arrived_cart - > assigned = false ;
new_arrived_cart - > next = cart_list ;
cart_list = new_arrived_cart ;
2019-04-16 13:51:46 +00:00
LOG_INFO ( " New cart added with mac_address: " ) ;
2019-04-14 16:24:52 +00:00
LOG_INFO_LLADDR ( & ( new_arrived_cart - > cart_address ) ) ;
printf ( " \n " ) ;
2019-04-12 13:14:52 +00:00
}
return true ;
}
//Upgrade the battery status of a cart
2019-04-14 16:24:52 +00:00
bool bat_update ( linkaddr_t src_cart_addr , uint8_t battery_level )
2019-04-12 13:14:52 +00:00
{
cart * c = cart_list ;
2019-04-14 16:24:52 +00:00
//looking for cart->address = address of who sent the message with the battery
2019-04-12 13:14:52 +00:00
while ( c )
{
2019-04-14 17:06:47 +00:00
if ( linkaddr_cmp ( & ( c - > cart_address ) , & src_cart_addr ) = = 0 )
2019-04-12 13:14:52 +00:00
c = c - > next ;
else
break ;
2019-04-14 16:24:52 +00:00
}
2019-04-12 13:14:52 +00:00
if ( ! c )
{
LOG_INFO ( " Cart not associated yet! \n " ) ;
return false ;
}
c - > battery_status = battery_level ;
c - > assigned = false ; //a battery status is sent only when the cart is in his place, not with a client. So if the cart was out and the the battery status is received, it is now come back in place.
return true ;
}
2019-04-14 16:24:52 +00:00
void handle_association_request ( linkaddr_t src , const void * data )
{
//turn on the led for association msg received
assoc_req_msg ar_msg ;
memcpy ( & ar_msg , data , sizeof ( ( assoc_req_msg * ) data ) ) ;
if ( insert_cart ( ar_msg . battery_percentage , src ) )
{
assoc_reply_msg notification ;
notification . msg_type = ASSOCIATION_REPLY_MSG ;
LOG_INFO ( " \n " ) ;
LOG_INFO ( " Sending acknowledgment of successfull association \n " ) ;
nullnet_buf = ( uint8_t * ) & notification ;
nullnet_len = sizeof ( notification ) ;
NETSTACK_NETWORK . output ( & src ) ;
}
LOG_INFO ( " New cart associated \n " ) ;
}
void handle_battery_msg ( linkaddr_t src , const void * data )
{
//turn on the led for battery msg received
battery_msg bt_msg ;
memcpy ( & bt_msg , data , sizeof ( ( battery_msg * ) data ) ) ;
if ( bat_update ( src , bt_msg . battery_percentage ) )
{
LOG_INFO ( " Battery level upgraded of " ) ;
LOG_INFO_LLADDR ( & src ) ;
printf ( " \n " ) ;
printf ( " Battery: %d \n " , bt_msg . battery_percentage ) ;
}
}
//callback function for the ctimer that checks if all the carts have been replaced when the supermarket close
void check ( void * ptr )
{
supermarket_open = ! supermarket_open ;
leds_toggle ( LEDS_ALL ) ;
if ( ! supermarket_open )
{
printf ( " Supermarket is closed! \n " ) ;
cart * c = cart_list ;
//the assigner checks if all the carts are back in their place or are still out
while ( c )
{
if ( c - > assigned )
printf ( " Customer id %d hasn't replaced his cart \n " , ( int ) c - > customer_id ) ;
c = c - > next ;
}
}
else
printf ( " Supermarket is open! \n " ) ;
process_poll ( & assigner_process ) ;
}
2019-04-12 13:14:52 +00:00