[cart] futher adjustments for new type names

This commit is contained in:
giomba 2019-04-13 09:28:16 +02:00
parent aa708cfea8
commit 2fb95798df
5 changed files with 85 additions and 25 deletions

View File

@ -12,7 +12,6 @@
//static linkaddr_t destination_address = {{ 0x00, 0x12, 0x4b, 0x00, 0x0f, 0x8f, 0x18, 0x11 }}; //static linkaddr_t destination_address = {{ 0x00, 0x12, 0x4b, 0x00, 0x0f, 0x8f, 0x18, 0x11 }};
PROCESS(cart_main_process, "Cart Process"); PROCESS(cart_main_process, "Cart Process");
AUTOSTART_PROCESSES(&cart_main_process); AUTOSTART_PROCESSES(&cart_main_process);
@ -34,28 +33,16 @@ PROCESS_THREAD(cart_main_process, ev, data) {
PROCESS_WAIT_EVENT(); PROCESS_WAIT_EVENT();
switch(status) { switch(status) {
case NOT_ASSOCIATED: case NOT_ASSOCIATED: s_not_associated(ev, data); break;
s_not_associated(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;
default:
printf("[E] Invalid status. Resetting status.\n");
status = NOT_ASSOCIATED;
break; break;
break;
case ASSOCIATED:
s_associated(ev, data);
break;
case SHOPPING: break;
case CASHOUT: break;
default: status = NOT_ASSOCIATED; break;
} }
/*
if (ev == PROCESS_EVENT_TIMER) {
printf("Transmitting %d...\n", counter);
sprintf(message, "#%d Hello. It's me. -- by Adele", counter);
nullnet_len = strlen(message) + 1;
counter++;
NETSTACK_NETWORK.output(&destination_address);
etimer_reset(&my_timer);
}
*/
} }
PROCESS_END(); PROCESS_END();

View File

@ -3,7 +3,10 @@
enum CartEvent { enum CartEvent {
CART_EVENT_ASSOCIATED, CART_EVENT_ASSOCIATED,
CART_EVENT_ASSIGNED CART_EVENT_ASSIGNED,
CART_EVENT_CASH_OUT,
CART_EVENT_NEW_PRODUCT,
CART_EVENT_CASH_OUT_ACK
} event; } event;
#endif #endif

View File

@ -33,6 +33,18 @@ void net_recv(const void* data, uint16_t len, const linkaddr_t* src, const linka
event = CART_EVENT_ASSIGNED; event = CART_EVENT_ASSIGNED;
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL); process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
break; break;
case PRODUCT_MSG:
event = CART_EVENT_NEW_PRODUCT;
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
break;
case CASH_OUT_MSG:
event = CART_EVENT_CASH_OUT;
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
break;
case START_OF_LIST_PRODUCTS_MSG:
event = CART_EVENT_CASH_OUT_ACK;
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
break;
default: default:
LOG_INFO("[W] message type unknown\n"); LOG_INFO("[W] message type unknown\n");
break; break;

View File

@ -1,9 +1,14 @@
#include "status.h" #include "status.h"
#define MAX_PRODUCT 10
enum CartStatus status; enum CartStatus status;
struct etimer broadcast_timer; struct etimer broadcast_timer;
linkaddr_t assigner_address; linkaddr_t assigner_address;
linkaddr_t cash_address;
uint32_t customer_id; uint32_t customer_id;
uint8_t nprod;
product_t list[MAX_PRODUCT];
void s_not_associated(process_event_t ev, process_data_t data) { void s_not_associated(process_event_t ev, process_data_t data) {
if (ev == PROCESS_EVENT_TIMER) { if (ev == PROCESS_EVENT_TIMER) {
@ -17,9 +22,10 @@ void s_not_associated(process_event_t ev, process_data_t data) {
} }
} }
else /* if a msg is received from network and represents an association event, then associate */ else /* if a msg is received from network and represents an association event, then associate */
if (ev == PROCESS_EVENT_MSG && *((enum CartEvent*)data) == CART_EVENT_ASSOCIATED) { if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSOCIATED) {
printf("[I] Associated with Assigner\n"); printf("[I] Associated with Assigner\n");
assigner_address = pkt.src; assigner_address = pkt.src;
nprod = 0;
status = ASSOCIATED; status = ASSOCIATED;
} }
} }
@ -34,7 +40,7 @@ void s_associated(process_event_t ev, process_data_t data) {
net_send(&m, sizeof(m), &assigner_address); net_send(&m, sizeof(m), &assigner_address);
etimer_reset(&broadcast_timer); etimer_reset(&broadcast_timer);
} }
if (ev == PROCESS_EVENT_MSG && *((enum CartEvent*)data) == CART_EVENT_ASSIGNED) { if (ev == PROCESS_EVENT_MSG && event == CART_EVENT_ASSIGNED) {
/* cart has been assigned to a new customer */ /* cart has been assigned to a new customer */
printf("[I] Assigned to customer\n"); printf("[I] Assigned to customer\n");
customer_id = ((assign_msg*)pkt.data)->customer_id; customer_id = ((assign_msg*)pkt.data)->customer_id;
@ -43,4 +49,52 @@ void s_associated(process_event_t ev, process_data_t data) {
} }
void s_shopping(process_event_t ev, process_data_t data) {
if (ev == PROCESS_EVENT_MSG) {
if (event == CART_EVENT_NEW_PRODUCT) {
/* add product to list */
if (nprod < MAX_PRODUCT) {
printf("[I] Adding item %d to cart\n", nprod);
item_msg* m = (item_msg*)pkt.data;
memcpy(&list[nprod++], &m->p, sizeof(product_t));
} else {
printf("[W] Too many products. Dropping.\n");
}
}
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;
}
}
}
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) {
status = CASH_OUT_SEND_LIST;
}
}
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);
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;
net_send(&m, sizeof(m), &cash_address);
}
nprod = 0;
customer_id = 0;
memset(&cash_address, 0, sizeof(cash_address));
status = ASSOCIATED;
}

View File

@ -13,7 +13,8 @@ enum CartStatus {
NOT_ASSOCIATED, NOT_ASSOCIATED,
ASSOCIATED, ASSOCIATED,
SHOPPING, SHOPPING,
CASHOUT CASH_OUT_WAIT4ACK,
CASH_OUT_SEND_LIST
}; };
extern enum CartStatus status; extern enum CartStatus status;
@ -21,5 +22,8 @@ extern struct etimer broadcast_timer;
void s_not_associated(process_event_t ev, process_data_t data); void s_not_associated(process_event_t ev, process_data_t data);
void s_associated(process_event_t ev, process_data_t data); void s_associated(process_event_t ev, process_data_t data);
void s_shopping(process_event_t ev, process_data_t data);
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);
#endif #endif