#ifndef _CAN_MESSAGE_H__
#define _CAN_MESSAGE_H__
#include <stdint.h>
#include "libs/node_list.h"
#include "os/os_types.h"
#include "bsp/bsp_driver.h"
#define can_payload_offset  2

typedef struct {
	uint16_t key;
	uint8_t dest;
	uint8_t src;
	uint8_t *data;
	uint16_t len;
	uint8_t idx;
	uint8_t total_frame;	
	uint8_t type;	
}can_message_t;

#define CAN_MESSAGE_MAX_FRAMES		32

#define CAN_SEND_SUCCESS 0
#define CAN_SEND_TIMEOUT -1
#define CAN_SEND_ERROR   -2
#define CAN_SEND_ACK_FAILT  -3 
#define CAN_SEND_NO_WAIT_QUEUE -4

void can_message_init(void);
s32 can_send_message(uint32_t can_id, u8 *data, int len, s32 timeout);

static __inline__ uint32 get_can_id(u8 dest, u8 src, u8 type, u8 retry){
	can_id_t id;
	id.id = 0;
	id.dest = dest;
	id.src = src;
	id.type = type;
	id.retry = retry;
	return id.id;
}

static __inline__ uint32 get_request_can_id(u8 dest, u8 retry){
	return get_can_id(dest,CAN_MY_ADDRESS, ptype_request, retry);
}

static __inline__ uint32 get_indicator_can_id(u8 dest){
	return get_can_id(dest,CAN_MY_ADDRESS, ptype_indicater, 1);
}

static __inline__ uint32 get_reponse_can_id(u8 dest){
	return get_can_id(dest,CAN_MY_ADDRESS, ptype_response, 1);
}


//EXPORT FUNCTIONS
extern void can_send_message_ack(can_message_t *msg, uint8_t success);
extern void can_send_ack(uint8_t can_addr, uint16_t key, uint8_t data);
extern void can_send_response(uint8_t can_add, uint8_t *data, int len);
extern void can_send_indicator(uint8_t can_add, uint8_t *data, int len);
extern void can_send_request(uint8_t can_add, uint8_t *data, int len);
#endif /* _CAN_MESSAGE_H__ */

