Merge branch 'master' of git.giomba.it:giomba/nes-proj

This commit is contained in:
giomba 2019-04-16 15:49:32 +02:00
commit 5c0f7aec2f
5 changed files with 135 additions and 218 deletions

View File

@ -11,185 +11,69 @@
#include "os/dev/serial-line.h"
#include "arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h"
#include "../common/supermarket_net.h"
#include "assigner_fun.h"
#define LOG_MODULE "App"
#define LOG_MODULE "Assigner"
#define LOG_LEVEL LOG_LEVEL_INFO
#define OPENING_PERIOD (300*CLOCK_SECOND)
/*typedef struct cart
{
linkaddr_t* cart_address;
uint8_t battery_status;
bool assigned;
uint32_t customer_id;
struct cart *next;
}cart;*/
/*typedef struct assigner_msg
{
enum message_type msg_type;
//assoc_req_msg request;
//assoc_reply_msg reply;
uint8_t battery_percentage;
uint32_t customer_id;
}assigner_msg;
*/
PROCESS(assigner_process, "Assigner process");
AUTOSTART_PROCESSES(&assigner_process);
cart* cart_list = NULL;
static bool supermarket_open = true;
/*
//function invoked in order to looking for the most charged cart to assign to the new arrived client
static cart* cart_selection()
{
uint8_t highest_battery = 0;
cart* selected = NULL;
cart* current = cart_list;
while(current)
{
if(!current->assigned && current->battery_status > highest_battery)
{
highest_battery = current->battery_status;
selected = current;
}
current = current->next;
}
return selected;
}
cart *cart_list = NULL;
bool supermarket_open = true;
//Insert a new cart in the list with the battery info just arrived
static bool insert_cart(uint8_t new_req_battery, linkaddr_t* mac_cart_addr)
{
cart* new_arrived_cart = (cart*)malloc(sizeof(cart));
if(new_arrived_cart==NULL)
{
printf("Association Failed");
return false;
}
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;
}
return true;
}
//Upgrade the battery status of a cart
static bool bat_upgrade(linkaddr_t* src_cart_addr, uint8_t battery_level)
{
cart* c = cart_list;
//for(c; c && (c->cart_address != src_cart_addr); c = c->next);
while(c)
{
if(c->cart_address != src_cart_addr)
c = c->next;
else
break;
}
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;
}
*/
//Handle the incoming messages, according to the msg_type
static void input_callback(const void* data, uint16_t len, const linkaddr_t* source_address, const linkaddr_t* destination_address)
static void msg_recv(const void* data, uint16_t len, const linkaddr_t* source_address, const linkaddr_t* destination_address)
{
a_msg received_msg;
linkaddr_t* src = (linkaddr_t*)source_address;
linkaddr_t src = *source_address;
LOG_INFO("Received data from: ");
LOG_INFO_LLADDR(&src);
printf("\n");
//if(len == sizeof((a_msg *)data))
memcpy (&received_msg, data, sizeof ((a_msg *)data));
LOG_INFO("Received data from: ");
LOG_INFO_LLADDR(source_address);
LOG_INFO("\n");
if(received_msg.msg_type == ASSOCIATION_REQUEST_MSG)
{
//accendere led blu (x mes broadcast)
if(insert_cart(received_msg.battery_percentage, src))
{
a_msg notification;
notification.msg_type = ASSOCIATION_REPLY_MSG;
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");
}
if(received_msg.msg_type == BATTERY_STATUS_MSG)
{
//accendere led purple (mex unicast)
if(bat_upgrade(src, received_msg.battery_percentage))
{
LOG_INFO("Battery level upgraded of ");
LOG_INFO_LLADDR(src);
LOG_INFO("\n");
}
}
}
//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;
if(!supermarket_open)
enum message_type type = ((msg*)data)->msg_type;
switch(type)
{
printf("Supermarket closed\n");
cart* c = cart_list;
while(c)
{
if(c->assigned)
printf("Customer id %d hasn't replaced his cart\n", (int)c->customer_id);
c = c->next;
}
case ASSOCIATION_REQUEST_MSG:
handle_association_request(src,data);
break;
case BATTERY_STATUS_MSG:
handle_battery_msg(src,data);
break;
default:
printf("Invalide type! This message is not for the Assigner");
break;
}
else
printf("Supermarket is open!\n");
process_poll(&assigner_process);
}
PROCESS_THREAD(assigner_process, ev, data)
PROCESS_THREAD(assigner_process, ev, data)
{
static uint8_t customer_id;
static a_msg selection_msg;
linkaddr_t* dest_addr;
static uint32_t customer_id;
static assign_msg selection_msg;
linkaddr_t dest_addr;
static struct ctimer opening_timer;
PROCESS_BEGIN();
cc26xx_uart_set_input(serial_line_input_byte);
serial_line_init();
nullnet_set_input_callback(input_callback);
ctimer_set(&opening_timer, OPENING_PERIOD, check, NULL);
nullnet_set_input_callback(msg_recv);
ctimer_set(&opening_timer, OPENING_PERIOD, check, NULL);
printf("Supermarket is open!\n");
leds_on(LEDS_GREEN);
printf("Welcome! Please, insert your card id\n");
while (true)
while (true)
{
PROCESS_WAIT_EVENT();
if(ev == serial_line_event_message)
@ -197,43 +81,48 @@ PROCESS_THREAD(assigner_process, ev, data)
if(!supermarket_open)
{
printf("Supermarket is closed! Please, come back tomorrow!\n");
leds_on(LEDS_RED);
//leds_on(LEDS_RED);
}
else
{
printf("Customer's id: %s\n", (char*)data);
customer_id = atoi(data);
printf("id: %d\n", (int)customer_id);
//printf("id: %lu\n", (uint32_t)customer_id);
cart* cart_selected = cart_selection();
if(!cart_selected)
{
printf("No cart available!\n");
leds_on(LEDS_RED);
//leds_on(LEDS_RED);
}
else
{
{
cart_selected->assigned = true;
cart_selected->customer_id = customer_id;
cart_selected->customer_id = (uint32_t)customer_id;
//send a notification to the selected cart with the associated customer id
//send a notification to selected cart with the associated customer id
selection_msg.msg_type = ASSIGNMENT_MSG;
selection_msg.customer_id = customer_id;
dest_addr = cart_selected->cart_address;
LOG_INFO("Selected cart with address: ");
LOG_INFO_LLADDR(&dest_addr);
printf("\n");
nullnet_buf = (uint8_t*)&selection_msg;
nullnet_len = sizeof(selection_msg);
NETSTACK_NETWORK.output(dest_addr);
NETSTACK_NETWORK.output(&dest_addr);
printf("Cart unblocked!\n");
leds_on(LEDS_GREEN);
//leds_on(LEDS_GREEN);
}
}
}
else if(ev == PROCESS_EVENT_POLL)
ctimer_reset(&opening_timer);
}

View File

@ -2,44 +2,37 @@
#include "net/netstack.h"
#include "net/nullnet/nullnet.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sys/log.h"
#include "sys/clock.h"
#include "sys/ctimer.h"
#include "os/dev/leds.h"
#include "os/dev/serial-line.h"
#include "arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h"
#include "../common/supermarket_net.h"
#include "assigner_fun.h"
#define LOG_MODULE "App"
#define LOG_LEVEL LOG_LEVEL_INFO
extern cart* cart_list;
//struct cart cart;
//function invoked in order to looking for the most charged cart to assign to the new arrived client
//function invoked in order to look for the most charged cart to assign to the new arrived client
cart* cart_selection()
{
{
uint8_t highest_battery = 0;
cart* selected = NULL;
cart* current = cart_list;
while(current)
{
if(!current->assigned && current->battery_status > highest_battery)
if(!(current->assigned) && (current->battery_status > highest_battery))
{
highest_battery = current->battery_status;
selected = current;
}
current = current->next;
}
return selected;
return selected;
}
//Insert a new cart in the list with the battery info just arrived
bool insert_cart(uint8_t new_req_battery, linkaddr_t* mac_cart_addr)
bool insert_cart(uint8_t new_req_battery, linkaddr_t mac_cart_addr)
{
cart* new_arrived_cart = (cart*)malloc(sizeof(cart));
if(new_arrived_cart==NULL)
@ -54,18 +47,21 @@ bool insert_cart(uint8_t new_req_battery, linkaddr_t* mac_cart_addr)
new_arrived_cart->assigned = false;
new_arrived_cart->next = cart_list;
cart_list = new_arrived_cart;
LOG_INFO("Nuovo carrello inserito con mac_address: ");
LOG_INFO_LLADDR(&(new_arrived_cart->cart_address));
printf("\n");
}
return true;
}
//Upgrade the battery status of a cart
bool bat_upgrade(linkaddr_t* src_cart_addr, uint8_t battery_level)
bool bat_update(linkaddr_t src_cart_addr, uint8_t battery_level)
{
cart* c = cart_list;
//for(c; c && (c->cart_address != src_cart_addr); c = c->next);
//looking for cart->address = address of who sent the message with the battery
while(c)
{
if(c->cart_address != src_cart_addr)
if(linkaddr_cmp(&(c->cart_address),&src_cart_addr) == 0)
c = c->next;
else
break;
@ -76,8 +72,64 @@ bool bat_upgrade(linkaddr_t* src_cart_addr, uint8_t battery_level)
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;
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;
}
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);
}

View File

@ -5,48 +5,24 @@
#include <string.h>
#include <stdlib.h>
#include "sys/log.h"
#include "sys/clock.h"
#include "sys/ctimer.h"
#include "os/dev/leds.h"
#include "os/dev/serial-line.h"
#include "arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h"
#include "../common/supermarket_net.h"
typedef struct cart
{
linkaddr_t* cart_address;
linkaddr_t cart_address;
uint8_t battery_status;
bool assigned;
uint32_t customer_id;
struct cart *next;
}cart;
extern bool supermarket_open;
extern cart* cart_list;
extern struct process assigner_process;
cart* cart_selection();
bool insert_cart(uint8_t new_req_battery, linkaddr_t* mac_cart_addr);
bool bat_upgrade(linkaddr_t* src_cart_addr, uint8_t battery_level);
//static void input_callback(const void* data, uint16_t len, const linkaddr_t* source_address, const linkaddr_t* destination_address);
//void check(void *ptr);
//-----------------------Definition of the type of messages exchanged between the modules, with their useful informations. The significant fields are discriminated by the msg_type ---------
typedef struct assigner_msg //Message for communications between assigner and carts
{
enum message_type msg_type;
//assoc_req_msg request;
//assoc_reply_msg reply;
uint8_t battery_percentage;
uint32_t customer_id;
}a_msg;
/*
typedef struct cash_desk_msg
{
enum message_type msg_type;
cash_out_msg cash_out;
product_msg product;
basket_msg basket;
}cd_msg;
*/
bool insert_cart(uint8_t new_req_battery, linkaddr_t mac_cart_addr);
bool bat_update(linkaddr_t src_cart_addr, uint8_t battery_level);
void handle_association_request(linkaddr_t src, const void* data);
void handle_battery_msg(linkaddr_t src, const void* data);
void check(void *ptr);

View File

@ -4,6 +4,6 @@
typedef struct product_t {
uint32_t product_id;
char expiration_date[8]; // gg/mm/yy
float price;
uint32_t price;
} product_t;
#endif

View File

@ -21,9 +21,9 @@
static linkaddr_t dest_addr = {{0x00, 0x12, 0x4b, 0x00, 0x0f, 0x24, 0x18, 0x04}};
product_t product_list[] = {
{ 1, "21/12/19", 1.05 },
{ 2, "12/05/19", 3.25 },
{ 3, "05/05/21", 2.50 }
{ 1, "21/12/19", 105 },
{ 2, "12/05/19", 325 },
{ 3, "05/05/21", 250 }
};
PROCESS(product_proc, "product random generator");
@ -38,7 +38,7 @@ void scan_product(product_t* p){
nullnet_len = sizeof(m);
NETSTACK_NETWORK.output(&dest_addr);
LOG_INFO("Product id [%d] scanned from ", (int)p->product_id);
LOG_INFO("Product id [%d, %d] scanned from ", (int)p->product_id, (int)p->price);
LOG_INFO_LLADDR(&dest_addr);
LOG_INFO_("\n");
}