From e4537117123a6b7710f69c5d692fc0c3bced074b Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 14 Apr 2019 15:22:19 +0200 Subject: [PATCH 1/5] [assigner] some minor fix here and there plus deleted trailing spaces from lines sorry Daniela, my editor trims them automagically --- project/Assigner/assigner.c | 67 ++++++++++++++++----------------- project/Assigner/assigner_fun.c | 13 +++---- project/Assigner/assigner_fun.h | 25 ++++++++++++ 3 files changed, 64 insertions(+), 41 deletions(-) diff --git a/project/Assigner/assigner.c b/project/Assigner/assigner.c index 5880b7503..fb9f921ef 100644 --- a/project/Assigner/assigner.c +++ b/project/Assigner/assigner.c @@ -11,7 +11,6 @@ #include "os/dev/serial-line.h" #include "arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h" -#include "../msg.h" #include "assigner_fun.h" #define LOG_MODULE "App" @@ -46,9 +45,9 @@ 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 +//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; @@ -61,7 +60,7 @@ static cart* cart_selection() } current = current->next; } - return selected; + return selected; } //Insert a new cart in the list with the battery info just arrived @@ -95,32 +94,32 @@ static bool bat_upgrade(linkaddr_t* src_cart_addr, uint8_t battery_level) 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; + 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 input_callback(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; - - //if(len == sizeof((a_msg *)data)) - + linkaddr_t* src = (linkaddr_t*)source_address; + + //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_LLADDR(source_address); LOG_INFO("\n"); - - if(received_msg.msg_type == ASSOCIATION_REQUEST_MSG) + + if(received_msg.msg_type == ASSOCIATION_REQUEST_MSG) { //accendere led blu (x mes broadcast) if(insert_cart(received_msg.battery_percentage, src)) @@ -128,7 +127,7 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou a_msg notification; notification.msg_type = ASSOCIATION_REPLY_MSG; LOG_INFO("Sending acknowledgment of successfull association\n"); - + nullnet_buf = (uint8_t*)¬ification; nullnet_len = sizeof(notification); NETSTACK_NETWORK.output(src); @@ -136,17 +135,17 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou LOG_INFO("New cart associated\n"); } - if(received_msg.msg_type == BATTERY_STATUS_MSG) + 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"); - } + LOG_INFO("\n"); + } } - + } //callback function for the ctimer that checks if all the carts have been replaced when the supermarket close @@ -155,10 +154,10 @@ void check(void *ptr) supermarket_open = !supermarket_open; if(!supermarket_open) { - printf("Supermarket closed\n"); + 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; @@ -167,17 +166,17 @@ void check(void *ptr) 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 struct ctimer opening_timer; - + PROCESS_BEGIN(); cc26xx_uart_set_input(serial_line_input_byte); @@ -185,12 +184,12 @@ PROCESS_THREAD(assigner_process, ev, data) nullnet_set_input_callback(input_callback); - ctimer_set(&opening_timer, OPENING_PERIOD, check, NULL); - + ctimer_set(&opening_timer, OPENING_PERIOD, check, NULL); + printf("Supermarket is open!\n"); printf("Welcome! Please, insert your card id\n"); - while (true) + while (true) { PROCESS_WAIT_EVENT(); if(ev == serial_line_event_message) @@ -205,26 +204,26 @@ PROCESS_THREAD(assigner_process, ev, data) printf("Customer's id: %s\n", (char*)data); customer_id = atoi(data); printf("id: %d\n", (int)customer_id); - + cart* cart_selected = cart_selection(); if(!cart_selected) { printf("No cart available!\n"); leds_on(LEDS_RED); } - + else { cart_selected->assigned = true; cart_selected->customer_id = customer_id; - + //send a notification to the 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; - + nullnet_buf = (uint8_t*)&selection_msg; nullnet_len = sizeof(selection_msg); NETSTACK_NETWORK.output(dest_addr); @@ -234,7 +233,7 @@ PROCESS_THREAD(assigner_process, ev, data) } } } - + else if(ev == PROCESS_EVENT_POLL) ctimer_reset(&opening_timer); } diff --git a/project/Assigner/assigner_fun.c b/project/Assigner/assigner_fun.c index 9b726b3af..a35e6f996 100644 --- a/project/Assigner/assigner_fun.c +++ b/project/Assigner/assigner_fun.c @@ -11,7 +11,6 @@ #include "os/dev/serial-line.h" #include "arch/cpu/cc26x0-cc13x0/dev/cc26xx-uart.h" -#include "../msg.h" #include "assigner_fun.h" #define LOG_MODULE "App" @@ -21,9 +20,9 @@ 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 looking 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; @@ -36,7 +35,7 @@ cart* cart_selection() } current = current->next; } - return selected; + return selected; } //Insert a new cart in the list with the battery info just arrived @@ -70,15 +69,15 @@ bool bat_upgrade(linkaddr_t* src_cart_addr, uint8_t battery_level) 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; + 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; } diff --git a/project/Assigner/assigner_fun.h b/project/Assigner/assigner_fun.h index aecd013ba..2c427c48a 100644 --- a/project/Assigner/assigner_fun.h +++ b/project/Assigner/assigner_fun.h @@ -11,6 +11,8 @@ #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; @@ -25,3 +27,26 @@ 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; +*/ From ccaac68102bb7f943c8f9c6fb3ea0c7a9ed9adba Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 14 Apr 2019 15:23:27 +0200 Subject: [PATCH 2/5] [cash] pointer issue basket_address + ids as uint32_t - basket_address held the pointer to a value that is replaced for every new packet coming from the network layer, so the value itself it should be copied, not the pointer; this lead to board reset when dereferencing it - product_id and customer_id are on 32 bits (no supermarket has less than 256 products and/or customers), otherwise I think it would fail --- project/Natalia/cassa.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/project/Natalia/cassa.c b/project/Natalia/cassa.c index e0c16d2d1..c87f8a95e 100644 --- a/project/Natalia/cassa.c +++ b/project/Natalia/cassa.c @@ -20,9 +20,9 @@ typedef struct user_invoice { uint8_t n_prods; float total_sum; - uint8_t customer_id; + uint32_t customer_id; uint8_t empty; - linkaddr_t* address_basket; + linkaddr_t address_basket; }user_invoice; @@ -63,9 +63,9 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou static user_invoice invoices[MAX_CUSTOMERS]; - if (len == sizeof(*data)) { - memcpy (&received_msg, data, sizeof ((msg *)data)); - LOG_INFO("Received data"); +// if (len == sizeof(*data)) { + memcpy (&received_msg, data, sizeof(received_msg)); + LOG_INFO("Received data "); LOG_INFO_LLADDR(source_address); //this is the link layer address LOG_INFO("\n"); //we need to receive an additional message to start the process of receiving the products because if we start receiving the products immediately @@ -77,25 +77,23 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou invoices[index].n_prods = basket->n_products; invoices[index].total_sum = 0; invoices[index].customer_id = basket->customer_id; - memcpy(&invoices[index].address_basket, source_address, sizeof(*source_address)); + memcpy(&invoices[index].address_basket, source_address, sizeof(linkaddr_t)); // invoices[index].address_basket = source_address; msg start_sending_list; start_sending_list.msg_type = START_OF_LIST_PRODUCTS_MSG; nullnet_buf = (uint8_t*)&start_sending_list; - LOG_INFO("Sending acknowledgment to start sending list of products"); - LOG_INFO_("\n"); + LOG_INFO("Sending acknowledgment to start sending list of products to "); + LOG_INFO_LLADDR(&(invoices[index].address_basket)); nullnet_len = sizeof(start_sending_list); - NETSTACK_NETWORK.output((invoices[index].address_basket)); - - - + NETSTACK_NETWORK.output(&(invoices[index].address_basket)); } else - printf("Reached max number of customers"); + printf("Reached max number of customers\n"); } if (received_msg.msg_type == PRODUCT_MSG) { product_msg *product = (product_msg*)(&received_msg); + printf("Received id: %d, price %f\n", (int)product->product_id, product->price); uint8_t index = invoice_index(product->customer_id, invoices); if (index != -1) { if (invoices[index].n_prods > 0) { @@ -107,15 +105,15 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou invoices[index].empty = 1; } }else - printf("Customer with that id is not associated to any basket!"); + printf("Customer with that id is not associated to any basket!\n"); } - } +// } } PROCESS_THREAD(cassa_main_process, ev, data) { PROCESS_BEGIN(); - static uint8_t customer_id; + static uint32_t customer_id; static cash_out_msg bro_customer_id; cc26xx_uart_set_input(serial_line_input_byte); From 6b6a2c019f8e275ed06e3733531f3fb41239ffff Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 14 Apr 2019 15:26:16 +0200 Subject: [PATCH 3/5] [product] hardcoded cart address this simply emulates RFID tag which we do not have --- project/product/product.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project/product/product.c b/project/product/product.c index 33aff393c..20b48dec7 100644 --- a/project/product/product.c +++ b/project/product/product.c @@ -16,9 +16,9 @@ #define LOG_MODULE "App" #define LOG_LEVEL LOG_LEVEL_INFO -// !! TO INIT WITH CART MAC ADDRESS !! // -static linkaddr_t dest_addr = {{0x00, 0x12, 0x4b, 0x00, 0x0f, 0x82, 0x18, 0x04}}; - +/* Hardcoded MAC Address for cart */ +/* This is used only to emulate the RFID tag */ +static linkaddr_t dest_addr = {{0x00, 0x12, 0x4b, 0x00, 0x0f, 0x24, 0x18, 0x04}}; product_t product_list[] = { { 1, "21/12/19", 1.05 }, From eba8e6540e9eeefa6a1c5db71fdadc7ffba726b4 Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 14 Apr 2019 15:27:32 +0200 Subject: [PATCH 4/5] [product] ids from uint8_t to uint32_t --- project/common/supermarket_net.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project/common/supermarket_net.h b/project/common/supermarket_net.h index 50d42cb74..6fd5fe17f 100644 --- a/project/common/supermarket_net.h +++ b/project/common/supermarket_net.h @@ -51,7 +51,7 @@ typedef struct basket_msg { enum message_type msg_type; uint8_t n_products; - uint8_t customer_id; /* TODO: why? you already know it */ + uint32_t customer_id; }basket_msg; typedef struct item_msg @@ -63,15 +63,15 @@ typedef struct item_msg typedef struct cash_out_msg { enum message_type msg_type; - uint8_t customer_id; + uint32_t customer_id; }cash_out_msg; typedef struct product_msg { enum message_type msg_type; - uint8_t customer_id; - uint8_t product_id; + uint32_t customer_id; + uint32_t product_id; float price; }product_msg; From 9357d6987685e06058035e7dcbd7d52c868d150a Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 14 Apr 2019 15:28:10 +0200 Subject: [PATCH 5/5] [cart] fixes here and there --- project/Giomba/cart.c | 3 ++- project/Giomba/sendrecv.c | 2 +- project/Giomba/status.c | 31 +++++++++++++++++++++---------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/project/Giomba/cart.c b/project/Giomba/cart.c index 65b830467..5a0955646 100644 --- a/project/Giomba/cart.c +++ b/project/Giomba/cart.c @@ -20,7 +20,8 @@ PROCESS_THREAD(cart_main_process, ev, data) { // SENSORS_ACTIVATE(batmon_sensor); /*** Variables initialization ***/ - status = NOT_ASSOCIATED; + // status = NOT_ASSOCIATED; // TODO DEBUG + status = SHOPPING; etimer_set(&broadcast_timer, 5 * CLOCK_SECOND); /*** Subsystem initialization ***/ diff --git a/project/Giomba/sendrecv.c b/project/Giomba/sendrecv.c index 10ed7da54..955cda0fd 100644 --- a/project/Giomba/sendrecv.c +++ b/project/Giomba/sendrecv.c @@ -33,7 +33,7 @@ void net_recv(const void* data, uint16_t len, const linkaddr_t* src, const linka event = CART_EVENT_ASSIGNED; process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); break; - case PRODUCT_MSG: + case ITEM_MSG: event = CART_EVENT_NEW_PRODUCT; process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); break; diff --git a/project/Giomba/status.c b/project/Giomba/status.c index e043f2710..9b9913e5f 100644 --- a/project/Giomba/status.c +++ b/project/Giomba/status.c @@ -6,7 +6,7 @@ enum CartStatus status; struct etimer broadcast_timer; linkaddr_t assigner_address; linkaddr_t cash_address; -uint32_t customer_id; +uint32_t customer_id = 1234; uint8_t nprod; product_t list[MAX_PRODUCT]; @@ -42,11 +42,10 @@ void s_associated(process_event_t ev, process_data_t data) { } if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSIGNED) { /* cart has been assigned to a new customer */ - printf("[I] Assigned to customer\n"); customer_id = ((assign_msg*)pkt.data)->customer_id; + printf("[I] Assigned to customer id %d\n", (int)customer_id); status = SHOPPING; } - } void s_shopping(process_event_t ev, process_data_t data) { @@ -63,26 +62,36 @@ void s_shopping(process_event_t ev, process_data_t data) { } if (event == CART_EVENT_CASH_OUT) { /* answer the cash if you are the one with that customer_id */ - basket_msg m; - m.n_products = nprod - 1; - m.customer_id = customer_id; /* TODO -- is this really needed? */ - net_send(&m, sizeof(m), &cash_address); - status = CASH_OUT_WAIT4ACK; + if (((cash_out_msg*)pkt.data)->customer_id == customer_id) { + printf("[I] It's me! I'm cashing out :-)\n"); + basket_msg m; + m.msg_type = BASKET_MSG; + m.n_products = nprod - 1; + m.customer_id = customer_id; /* TODO -- is this really needed? */ + net_send(&m, sizeof(m), &cash_address); + status = CASH_OUT_WAIT4ACK; + } + else { + printf("[I] I am customer id %d; customer id %d is cashing out nearby\n", (int)customer_id, (int)((cash_out_msg*)pkt.data)->customer_id ); + } } } } void s_cash_out_wait4ack(process_event_t ev, process_data_t data) { /* Just wait for cash ack */ - if (ev == CART_EVENT_CASH_OUT_ACK) { + if (event == CART_EVENT_CASH_OUT_ACK) { + printf("[I] Acknoweledgment received fromc cash. Now I'll send the list.\n"); status = CASH_OUT_SEND_LIST; + /* this wakes up the process that otherwise would wait indefinitely for an event that will never occurr */ + process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); } } void s_cash_out_send_list(process_event_t ev, process_data_t data) { /* Send list, then go back to initial state */ for (uint8_t i = 0; i < nprod; ++i) { - printf("[I] Sending product %d of %d...", i, nprod - 1); + printf("[I] Sending product %d of %d...\n", i, nprod - 1); product_msg m; m.msg_type = PRODUCT_MSG; m.customer_id = customer_id; @@ -95,6 +104,8 @@ void s_cash_out_send_list(process_event_t ev, process_data_t data) { customer_id = 0; memset(&cash_address, 0, sizeof(cash_address)); + printf("[I] END. Go back to ASSOCIATED status\n"); + etimer_reset(&broadcast_timer); status = ASSOCIATED; }