fixed error message type 59 and added product ack in order to allow reliable communication

This commit is contained in:
natalija.zlatkova@gmail.com 2019-04-21 19:09:50 +02:00
parent 1f27de6ca6
commit 25f28c16c2
6 changed files with 105 additions and 82 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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<nprod) {
LOG_INFO("[I] Sending product %d of %d: id %d, price: %d\n", nprod_index, nprod - 1, (int)list[nprod_index].product_id, (int)list[nprod_index].price);
product_msg m;
m.msg_type = PRODUCT_MSG;
m.customer_id = customer_id;
m.product_id = list[i].product_id;
m.price = list[i].price;
m.product_id = list[nprod_index].product_id;
m.price = list[nprod_index].price;
net_send(&m, sizeof(m), &cash_address);
nprod_index++;
}
if (nprod_index == nprod) {
nprod = 0;
customer_id = 0;
nprod_index = 0;
customer_id = 1234;
memset(&cash_address, 0, sizeof(cash_address));
printf("[I] END. Go back to ASSOCIATED status\n");
etimer_restart(&broadcast_timer);
status = ASSOCIATED;
status = SHOPPING;
}
}

View File

@ -12,8 +12,6 @@
#include <stdlib.h>
#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,27 +63,33 @@ 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("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);
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));
// invoices[index].address_basket = source_address;
msg start_sending_list;
start_sending_list.msg_type = START_OF_LIST_PRODUCTS_MSG;
@ -90,15 +101,23 @@ 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");
}
if (received_msg.msg_type == PRODUCT_MSG) {
product_msg *product = (product_msg*)(&received_msg);
} 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);
@ -107,41 +126,37 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou
}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);
printf("Customer's id: %s\n", (char*)data);
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);
printf("id: %d\n", (int)customer_id);
bro_customer_id.msg_type = CASH_OUT_MSG;
bro_customer_id.customer_id = customer_id;
cash_out_msg m;
m.msg_type = CASH_OUT_MSG;
m.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);
//send the customer's id as a broadcast message
nullnet_buf = (uint8_t*)&m;
nullnet_len = sizeof(m);
NETSTACK_NETWORK.output(NULL);
}
}
PROCESS_END();
}

View File

@ -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 {

View File

@ -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 },