From 35f5041b7f71f2c9cc06fff6cc6cc29c13540fb5 Mon Sep 17 00:00:00 2001 From: giomba Date: Tue, 7 May 2019 21:39:28 +0200 Subject: [PATCH] [cart + cash] partial product arrays when you cash out, instead of sending many packets, you can aggregate multiple products in the same packet, doing an array; thus you have better power efficiency, and less delay; so both Anastasi and Marcelloni would be very proud of us --- project/Giomba/cart.c | 2 +- project/Giomba/event.h | 3 +- project/Giomba/sendrecv.c | 5 ++- project/Giomba/status.c | 60 +++++++++++++++++---------- project/Natalia/cassa.c | 69 ++++++++++++++++++-------------- project/common/supermarket_net.h | 17 ++++---- 6 files changed, 93 insertions(+), 63 deletions(-) diff --git a/project/Giomba/cart.c b/project/Giomba/cart.c index 020d864e9..3bb1bf108 100644 --- a/project/Giomba/cart.c +++ b/project/Giomba/cart.c @@ -21,8 +21,8 @@ PROCESS_THREAD(cart_main_process, ev, data) { /*** Variables initialization ***/ // status = NOT_ASSOCIATED; // TODO DEBUG + status = SHOPPING; - status = NOT_ASSOCIATED; // SHOPPING; // NOT_ASSOCIATED; etimer_set(&broadcast_timer, 5 * CLOCK_SECOND); /*** Subsystem initialization ***/ diff --git a/project/Giomba/event.h b/project/Giomba/event.h index db05b14cf..d4349520c 100644 --- a/project/Giomba/event.h +++ b/project/Giomba/event.h @@ -6,7 +6,8 @@ enum CartEvent { CART_EVENT_ASSIGNED, CART_EVENT_CASH_OUT, CART_EVENT_NEW_PRODUCT, - CART_EVENT_CASH_OUT_ACK + CART_EVENT_CASH_OUT_ACK, + CART_EVENT_CASH_OUT_PARTIAL_LIST_ACK } event; #endif diff --git a/project/Giomba/sendrecv.c b/project/Giomba/sendrecv.c index 40b77e6ce..32a633804 100644 --- a/project/Giomba/sendrecv.c +++ b/project/Giomba/sendrecv.c @@ -44,10 +44,13 @@ void net_recv(const void* data, uint16_t len, const linkaddr_t* src, const linka process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); break; case START_OF_LIST_PRODUCTS_MSG: - case PRODUCT_ACK: event = CART_EVENT_CASH_OUT_ACK; process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); break; + case PRODUCT_PARTIAL_LIST_ACK: + event = CART_EVENT_CASH_OUT_PARTIAL_LIST_ACK; + process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); + break; default: LOG_INFO("[W] message type %d unknown\n", ((msg*)data)->msg_type); break; diff --git a/project/Giomba/status.c b/project/Giomba/status.c index c4f73be1f..003683509 100644 --- a/project/Giomba/status.c +++ b/project/Giomba/status.c @@ -8,6 +8,7 @@ linkaddr_t assigner_address; linkaddr_t cash_address; uint32_t customer_id = 1234; uint8_t nprod = 0; +uint8_t remaining = 0; uint16_t totalPrice = 0; uint8_t nprod_index = 0; //variable used to keep track of the index of the product to be sent product_t list[MAX_PRODUCT]; @@ -86,37 +87,52 @@ void s_shopping(process_event_t ev, process_data_t data) { void s_cash_out_wait4ack(process_event_t ev, process_data_t data) { /* Just wait for cash ack */ if (event == CART_EVENT_CASH_OUT_ACK) { - printf("[I] Acknoweledgment received fromc cash. Now I'll send the list.\n"); + printf("[I] Acknoweledgment received from cash. Total price is %d. Now I'll send the list.\n", totalPrice); + remaining = nprod; status = CASH_OUT_SEND_LIST; - /* this wakes up the process that otherwise would wait indefinitely for an event that will never occurr */ + /* this wakes up the process that otherwise would wait indefinitely for an event that will never occur */ 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 */ - printf("[I] Total price is %d\n", totalPrice); - if (nprod_indexmsg_type); LOG_INFO("\n"); received_msg = (msg*) (&pkt.data); - + //we need to receive an additional message to start the process of receiving the products because if we start receiving the products immediately - //in the case of parallel processes we wouldnt know to what client and what basket that product is assosiated with + //in the case of parallel processes we wouldnt know to what client and what basket that product is associated with if (received_msg->msg_type == BASKET_MSG) { basket_msg *basket = (basket_msg*) (received_msg); uint8_t index = index_free_spot(invoices); if (index != -1 ) { printf("basket->n_products %d\n", basket->n_products); 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(linkaddr_t)); - + msg start_sending_list; start_sending_list.msg_type = START_OF_LIST_PRODUCTS_MSG; @@ -101,31 +101,40 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou NETSTACK_NETWORK.output(&(invoices[index].address_basket)); } else printf("Reached max number of customers\n"); - } else if (received_msg->msg_type == PRODUCT_MSG) { - product_msg *product = (product_msg*)(received_msg); - printf("Received id: %d, price %d\n", (int)product->product_id, (int)product->price); - uint8_t index = invoice_index(product->customer_id, invoices); - if (index != -1) { - if (invoices[index].n_prods > 0) { - invoices[index].total_sum += product->price; - invoices[index].n_prods--; - //send an ack to the cart so that it knows the cassa has received the product and can send the next product - msg product_ack; - product_ack.msg_type = PRODUCT_ACK; - nullnet_buf = (uint8_t*)&product_ack; + } else if (received_msg->msg_type == PRODUCT_PARTIAL_LIST_MSG) { + product_partial_list_msg* product_list = (product_partial_list_msg*)(received_msg); - LOG_INFO("Sending acknowledgment for the product\n "); - nullnet_len = sizeof(product_ack); - NETSTACK_NETWORK.output(&(invoices[index].address_basket)); - - } - if (invoices[index].n_prods == 0) { - printf("Total sum for client %d is %d\n", (int)invoices[index].customer_id, (int)invoices[index].total_sum); - invoices[index].empty = 1; - } - }else - printf("Customer with that id is not associated to any basket!\n"); - } + uint8_t index = invoice_index(product_list->customer_id, invoices); + + printf("Receiving %d items from customer %d\n", ((int)product_list->array_len) & 0xff, (int)product_list->customer_id); + + for (uint8_t i = 0; i < product_list->array_len; ++i) { + product_t p = product_list->p[i]; + + printf("Received id: %d, price %d\n", (int)p.product_id, (int)p.price); + + if (index != -1) { + if (invoices[index].n_prods > 0) { + invoices[index].total_sum += p.price; + invoices[index].n_prods--; + } + if (invoices[index].n_prods == 0) { + printf("Total sum for client %d is %d\n", (int)invoices[index].customer_id, (int)invoices[index].total_sum); + invoices[index].empty = 1; + } + } else + printf("Customer with that id is not associated to any basket!\n"); + } + + //send an ack to the cart so that it knows the cash register has received the partial product list and can send the next part + msg product_ack; + product_ack.msg_type = PRODUCT_PARTIAL_LIST_ACK; + nullnet_buf = (uint8_t*)&product_ack; + LOG_INFO("Sending acknowledgment for the partial product list\n "); + nullnet_len = sizeof(product_ack); + NETSTACK_NETWORK.output(&(invoices[index].address_basket)); + + } } @@ -137,7 +146,7 @@ PROCESS_THREAD(cassa_main_process, ev, data) { serial_line_init(); //initialization of the message handler for receiving messages - nullnet_set_input_callback(input_callback); + nullnet_set_input_callback(input_callback); while (true) { printf("Dear customer, insert your card id\n"); @@ -146,7 +155,7 @@ PROCESS_THREAD(cassa_main_process, ev, data) { uint32_t customer_id; printf("[CASSA INFO] Customer's id: %s\n", (char*)data); customer_id = atoi(data); - + cash_out_msg m; m.msg_type = CASH_OUT_MSG; m.customer_id = customer_id; diff --git a/project/common/supermarket_net.h b/project/common/supermarket_net.h index 18a44fcee..acaea11a0 100644 --- a/project/common/supermarket_net.h +++ b/project/common/supermarket_net.h @@ -3,6 +3,8 @@ #include "product.h" +#define PRODUCT_ARRAY_MAX_LEN 5 + enum message_type { ASSOCIATION_REQUEST_MSG, ASSOCIATION_REPLY_MSG, @@ -10,10 +12,10 @@ enum message_type { ASSIGNMENT_MSG, CASH_OUT_MSG, ITEM_MSG, /* from item to cart */ - PRODUCT_MSG, /* from cart to cash */ + PRODUCT_PARTIAL_LIST_MSG, /* from cart to cash */ BASKET_MSG, START_OF_LIST_PRODUCTS_MSG, - PRODUCT_ACK //everytime the cassa receives a product message , it sends an ack to the cart so that the cart can send the next product message + PRODUCT_PARTIAL_LIST_ACK /* everytime the cash register receives a product message, it sends an ack to the cart so that the cart can send the next product message */ }; typedef struct msg { @@ -68,14 +70,13 @@ typedef struct cash_out_msg }cash_out_msg; -typedef struct product_msg +typedef struct product_partial_list_msg { enum message_type msg_type; - uint32_t customer_id; - uint32_t product_id; - uint32_t price; - -}product_msg; + uint32_t customer_id; + uint8_t array_len; + product_t p[PRODUCT_ARRAY_MAX_LEN]; +}product_partial_list_msg; #endif