From 25f28c16c214ce45aa10020f5a2babd25db77fcb Mon Sep 17 00:00:00 2001 From: "natalija.zlatkova@gmail.com" Date: Sun, 21 Apr 2019 19:09:50 +0200 Subject: [PATCH] fixed error message type 59 and added product ack in order to allow reliable communication --- project/Giomba/cart.c | 3 +- project/Giomba/sendrecv.c | 1 + project/Giomba/status.c | 29 +++--- project/Natalia/cassa.c | 149 +++++++++++++++++-------------- project/common/supermarket_net.h | 3 +- project/product/product.c | 2 +- 6 files changed, 105 insertions(+), 82 deletions(-) diff --git a/project/Giomba/cart.c b/project/Giomba/cart.c index 2240668ed..a5cd2a102 100644 --- a/project/Giomba/cart.c +++ b/project/Giomba/cart.c @@ -21,6 +21,7 @@ PROCESS_THREAD(cart_main_process, ev, data) { /*** Variables initialization ***/ // status = NOT_ASSOCIATED; // TODO DEBUG + status = SHOPPING; // NOT_ASSOCIATED; etimer_set(&broadcast_timer, 5 * CLOCK_SECOND); @@ -38,7 +39,7 @@ PROCESS_THREAD(cart_main_process, ev, data) { case ASSOCIATED: s_associated(ev, data); break; case SHOPPING: s_shopping(ev, data); break; case CASH_OUT_WAIT4ACK: s_cash_out_wait4ack(ev, data); break; - case CASH_OUT_SEND_LIST: s_cash_out_send_list(ev, data); break; + case CASH_OUT_SEND_LIST: s_cash_out_send_list(ev, data); break; default: printf("[E] Invalid status. Resetting status.\n"); status = NOT_ASSOCIATED; diff --git a/project/Giomba/sendrecv.c b/project/Giomba/sendrecv.c index d8d4fafb3..40b77e6ce 100644 --- a/project/Giomba/sendrecv.c +++ b/project/Giomba/sendrecv.c @@ -44,6 +44,7 @@ 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; diff --git a/project/Giomba/status.c b/project/Giomba/status.c index 62c68c5af..003b3e2b0 100644 --- a/project/Giomba/status.c +++ b/project/Giomba/status.c @@ -1,13 +1,14 @@ #include "status.h" -#define MAX_PRODUCT 10 +#define MAX_PRODUCT 20 enum CartStatus status; struct etimer broadcast_timer; linkaddr_t assigner_address; linkaddr_t cash_address; uint32_t customer_id = 1234; -uint8_t nprod; +uint8_t nprod = 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]; void s_not_associated(process_event_t ev, process_data_t data) { @@ -68,7 +69,7 @@ void s_shopping(process_event_t ev, process_data_t data) { cash_address = pkt.src; basket_msg m; m.msg_type = BASKET_MSG; - m.n_products = nprod - 1; + m.n_products = nprod; m.customer_id = customer_id; /* TODO -- is this really needed? */ net_send(&m, sizeof(m), &cash_address); status = CASH_OUT_WAIT4ACK; @@ -92,22 +93,26 @@ void s_cash_out_wait4ack(process_event_t ev, process_data_t data) { 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: id %d, price: %d\n", i, nprod - 1, (int)list[i].product_id, (int)list[i].price); + + if (nprod_index #include "../common/supermarket_net.h" - - #define MAX_CUSTOMERS 20 typedef struct user_invoice @@ -26,6 +24,13 @@ typedef struct user_invoice }user_invoice; +struct MacPkt { + char data[256]; + uint16_t len; + linkaddr_t src; + linkaddr_t dst; +}pkt; +//struct MacPkt pkt; PROCESS(cassa_main_process, "Cassa process"); AUTOSTART_PROCESSES(&cassa_main_process); @@ -58,89 +63,99 @@ static uint8_t index_free_spot(user_invoice invoices[]) static void input_callback(const void* data, uint16_t len, const linkaddr_t* source_address, const linkaddr_t* destination_address) { - msg received_msg; - + msg *received_msg; static user_invoice invoices[MAX_CUSTOMERS]; + /* fill packet to pass to upper processing layer */ + memcpy(pkt.data, data, len); + pkt.len = len; + pkt.src = *source_address; + pkt.dst = *destination_address; -// 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 - //in the case of parallel processes we wouldnt know to what client and what basket that product is assosiated 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 ) { - 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)); - // invoices[index].address_basket = source_address; + LOG_INFO("Received %d bytes from ", len); LOG_INFO_LLADDR(source_address); + LOG_INFO("type %d", ((msg*)data)->msg_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 + 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; - nullnet_buf = (uint8_t*)&start_sending_list; + 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 to "); - LOG_INFO_LLADDR(&(invoices[index].address_basket)); - nullnet_len = sizeof(start_sending_list); - NETSTACK_NETWORK.output(&(invoices[index].address_basket)); - } else + 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)); + } else 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 %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--; - } - 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"); - } -// } + } 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; + + 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"); + } } + PROCESS_THREAD(cassa_main_process, ev, data) { PROCESS_BEGIN(); - static uint32_t customer_id; - static cash_out_msg bro_customer_id; - + //initialization of the serial input line cc26xx_uart_set_input(serial_line_input_byte); serial_line_init(); - nullnet_buf = (uint8_t*)&bro_customer_id; - nullnet_set_input_callback(input_callback); //this should be moved down? - - - + //initialization of the message handler for receiving messages + nullnet_set_input_callback(input_callback); while (true) { printf("Dear customer, insert your card id\n"); - PROCESS_WAIT_EVENT_UNTIL(ev == serial_line_event_message); + PROCESS_WAIT_EVENT(); + if(ev == serial_line_event_message) { + 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; - printf("Customer's id: %s\n", (char*)data); - customer_id = atoi(data); - printf("id: %d\n", (int)customer_id); - - bro_customer_id.msg_type = CASH_OUT_MSG; - bro_customer_id.customer_id = customer_id; - - LOG_INFO("Sending BROADCAST customer id: %d\n", (int)customer_id); - LOG_INFO_LLADDR(NULL); - LOG_INFO_("\n"); - nullnet_len = sizeof(bro_customer_id); - NETSTACK_NETWORK.output(NULL); + //send the customer's id as a broadcast message + nullnet_buf = (uint8_t*)&m; + nullnet_len = sizeof(m); + NETSTACK_NETWORK.output(NULL); + } } PROCESS_END(); diff --git a/project/common/supermarket_net.h b/project/common/supermarket_net.h index 466f0bb1a..18a44fcee 100644 --- a/project/common/supermarket_net.h +++ b/project/common/supermarket_net.h @@ -12,7 +12,8 @@ enum message_type { ITEM_MSG, /* from item to cart */ PRODUCT_MSG, /* from cart to cash */ BASKET_MSG, - START_OF_LIST_PRODUCTS_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 }; typedef struct msg { diff --git a/project/product/product.c b/project/product/product.c index d4a9daa30..b42e909b5 100644 --- a/project/product/product.c +++ b/project/product/product.c @@ -18,7 +18,7 @@ /* Hardcoded MAC Address for cart */ /* This is used only to emulate the RFID tag */ -static linkaddr_t dest_addr = {{0x00, 0x12, 0x4b, 0x00, 0x0f, 0x82, 0x18, 0x04}}; +static linkaddr_t dest_addr = {{0x00, 0x12, 0x4b, 0x00, 0x0f, 0x82, 0x00, 0x04}}; product_t product_list[] = { { 1, "21/12/19", 105 },