Commit 2753a5ab authored by Bc. Petr Elexa's avatar Bc. Petr Elexa
Browse files

panel: refactor and rename "panel" to "reader"

parent 0ced4a8f
// ACS protocol definitions
// ACS CAN protocol
//
// Contains defines and data types for the protocol
//
#ifndef CAN_TERM_PROTOCOL_H_
#define CAN_TERM_PROTOCOL_H_
#ifndef ACS_CAN_PROTOCOL_H_
#define ACS_CAN_PROTOCOL_H_
// Used Message object numbers
#define ACS_MSGOBJ_SEND_DOOR_A 0
......@@ -35,43 +38,46 @@
#define ACS_FC_MASK (((1 << ACS_FC_BITS) - 1) << ACS_FC_OFFSET)
#define ACS_PRIO_MASK (((1 << ACS_PRIO_BITS) - 1) << ACS_PRIO_OFFSET)
#define ACC_MAX_PRIO 0
#define ACS_MAX_PRIO 0
#define ACS_LOW_PRIO ((1 << ACS_PRIO_BITS) - 1)
// ACS protocol function codes
#define FC_RESERVED 0x0
#define FC_USER_AUTH_REQ 0x1
#define FC_USER_NOT_AUTH_RESP 0x2
#define FC_USER_AUTH_RESP 0x3
#define FC_PANEL_CTRL 0x4
#define FC_NEW_USER 0x5
#define FC_DOOR_STATUS 0x6
#define FC_ALIVE 0x7
#define FC_USER_AUTH_REQ 0x1 // S -> M
#define FC_USER_NOT_AUTH_RESP 0x2 // M -> S
#define FC_USER_AUTH_RESP 0x3 // M -> S
#define FC_DOOR_CTRL 0x4 // M -> S
#define FC_NEW_USER 0x5 // S -> M
#define FC_DOOR_STATUS 0x6 // S -> M
#define FC_ALIVE 0x7 // M -> S
// priorities for each function code (0-7)
#define PRIO_RESERVED 0x0
#define PRIO_USER_AUTH_REQ 0x2
#define PRIO_USER_AUTH_RESP_FAIL 0x2
#define PRIO_USER_AUTH_RESP_OK 0x2
#define PRIO_PANEL_CTRL 0x3
#define PRIO_DOOR_CTRL 0x3
#define PRIO_NEW_USER 0x3
#define PRIO_DOOR_STATUS 0x4
#define PRIO_ALIVE 0x1
// Data for FC_DOOR_CTRL
#define DATA_DOOR_CTRL_MODE_DEF 0x00
#define DATA_DOOR_CTRL_UNLCK 0x01
#define DATA_DOOR_CTRL_LOCK 0x02
#define DATA_DOOR_CTRL_LEARN 0x03
#define DATA_DOOR_CTRL_CLR_CACHE 0x04
#define PANEL_CTRL_DATA_DEF 0x00
#define PANEL_CTRL_DATA_UNLCK 0x01
#define PANEL_CTRL_DATA_LOCK 0x02
#define PANEL_CTRL_DATA_LEARN 0x03
#define PANEL_CTRL_DATA_CLR_CACHE 0x04
// Data for FC_DOOR_STATUS
#define DATA_DOOR_STATUS_CLOSED 0x0
#define DATA_DOOR_STATUS_OPEN 0x1
#define DOOR_STATUS_DATA_CLOSED 0x0
#define DOOR_STATUS_DATA_OPEN 0x1
// Setting for master communication status
#define ACS_MASTER_ALIVE_PERIOD_MS 5000
#define ACS_MASTER_ALIVE_TIMEOUT_MS 10000
#define MASTER_ALIVE_PERIOD 5000 // ms
#define MASTER_ALIVE_TIMEOUT 10000 // ms
// data types
// Data type definitions
#pragma pack(push,1)
......@@ -86,28 +92,28 @@ typedef union
uint32_t flags : 3;
};
uint32_t scalar;
} msg_head_t;
} acs_msg_head_t;
typedef struct
{
uint32_t user_id;
} msg_data_auth_req_t;
} acs_msg_data_auth_req_t;
typedef struct
{
uint32_t user_id;
} msg_data_auth_resp_t;
} acs_msg_data_auth_resp_t;
typedef struct
{
uint32_t user_id;
} msg_data_new_user_t;
} acs_msg_data_new_user_t;
typedef struct
{
uint8_t ctrl_command;
} msg_data_panel_ctrl_t;
} acs_msg_data_door_ctrl_t;
#pragma pack(pop)
#endif /* CAN_TERM_PROTOCOL_H_ */
#endif /* ACS_CAN_PROTOCOL_H_ */
......@@ -62,7 +62,7 @@ int main(void)
Board_Print_Reset_Reason();
WDT_Init(PANEL_WATCHDOG_TIMEOUT);
WDT_Init(HW_WATCHDOG_TIMEOUT);
storage_init();
......
/*
* Access control system panel
* Access control system terminal
* Created on: 24. 8. 2018
* Author: Petr
*/
......@@ -12,7 +12,7 @@
#include "task.h"
#include "stream_buffer.h"
#include "can/can_term_driver.h"
#include "can/can_term_protocol.h"
#include "acs_can_protocol.h"
#include <stdio.h>
#include <string.h>
......@@ -20,12 +20,12 @@
typedef cache_item_t term_cache_item_t; // 4B
// Used cache values
enum term_cache_panel
enum term_cache_reader
{
cache_panel_none = 0,
cache_panel_door_A = 1,
cache_panel_door_B = 2,
cache_panel_all = 3
cache_reader_none = 0,
cache_reader_A = 1,
cache_reader_B = 2,
cache_reader_all = 3
};
// Address for currently active master
......@@ -41,20 +41,20 @@ static TimerHandle_t _act_timer = NULL;
static const uint32_t _act_timer_id = TERMINAL_TIMER_ID;
static inline uint8_t map_panel_id_to_cache(uint8_t panel_id)
static inline uint8_t map_reader_idx_to_cache(uint8_t reader_idx)
{
return (panel_id == ACC_PANEL_A ? cache_panel_door_A : cache_panel_door_B);
return (reader_idx == ACS_READER_A_IDX ? cache_reader_A : cache_reader_B);
}
static inline void terminal_user_authorized(uint8_t panel_id)
static inline void terminal_user_authorized(uint8_t reader_idx)
{
DEBUGSTR("auth OK\n");
panel_unlock(panel_id, BEEP_ON_SUCCESS, OK_LED_ON_SUCCESS);
reader_unlock(reader_idx, BEEP_ON_SUCCESS, OK_LED_ON_SUCCESS);
}
static inline void terminal_user_not_authorized(uint8_t panel_id)
static inline void terminal_user_not_authorized(uint8_t reader_id)
{
(void)panel_id;
(void)reader_id;
DEBUGSTR("auth FAIL\n");
}
......@@ -129,20 +129,20 @@ void term_can_recv(uint8_t msg_obj_num)
/* Now load up the msg_obj structure with the CAN message */
LPC_CCAN_API->can_receive(&msg_obj);
msg_head_t head;
acs_msg_head_t head;
head.scalar = msg_obj.mode_id;
uint8_t panel_id;
uint8_t reader_idx;
// get target door if message is for us
if (msg_obj.msgobj == ACS_MSGOBJ_RECV_DOOR_A)
{
panel_id = ACC_PANEL_A;
reader_idx = ACS_READER_A_IDX;
DEBUGSTR("for door A\n");
}
else if (msg_obj.msgobj == ACS_MSGOBJ_RECV_DOOR_B)
{
panel_id = ACC_PANEL_B;
reader_idx = ACS_READER_B_IDX;
DEBUGSTR("for door B\n");
}
else if (msg_obj.msgobj == ACS_MSGOBJ_RECV_BCAST)
......@@ -168,12 +168,12 @@ void term_can_recv(uint8_t msg_obj_num)
else return;
// stop processing if card reader not configured
if (panel_id >= DOOR_ACC_PANEL_COUNT) return;
if (reader_idx >= ACS_READER_COUNT) return;
// continue deducing action and execute it
if (head.fc == FC_USER_AUTH_RESP)
{
terminal_user_authorized(panel_id);
terminal_user_authorized(reader_idx);
#if CACHING_ENABLED
uint32_t user_id;
......@@ -181,13 +181,13 @@ void term_can_recv(uint8_t msg_obj_num)
memcpy(&user_id, msg_obj.data, len);
term_cache_item_t user;
user.key = user_id;
user.value = map_panel_id_to_cache(panel_id);
user.value = map_reader_idx_to_cache(reader_idx);
static_cache_insert(user);
#endif
}
else if (head.fc == FC_USER_NOT_AUTH_RESP)
{
terminal_user_not_authorized(panel_id);
terminal_user_not_authorized(reader_idx);
#if CACHING_ENABLED
uint32_t user_id;
......@@ -195,31 +195,31 @@ void term_can_recv(uint8_t msg_obj_num)
memcpy(&user_id, msg_obj.data, len);
term_cache_item_t user;
user.key = user_id;
user.value = cache_panel_none;
user.value = cache_reader_none;
static_cache_insert(user);
#endif
}
else if (head.fc == FC_PANEL_CTRL)
else if (head.fc == FC_DOOR_CTRL)
{
switch (msg_obj.data[0])
{
case PANEL_CTRL_DATA_DEF:
case DATA_DOOR_CTRL_MODE_DEF:
DEBUGSTR("mode DEF\n");
panel_conf[panel_id].mode = PANEL_MODE_DEF;
reader_conf[reader_idx].mode = READER_MODE_DEF;
break;
case PANEL_CTRL_DATA_UNLCK:
case DATA_DOOR_CTRL_UNLCK:
DEBUGSTR("cmd UNLOCK\n");
terminal_user_authorized(panel_id);
terminal_user_authorized(reader_idx);
break;
case PANEL_CTRL_DATA_LOCK:
case DATA_DOOR_CTRL_LOCK:
DEBUGSTR("mode LOCK\n");
panel_conf[panel_id].mode = PANEL_MODE_LOCKED;
reader_conf[reader_idx].mode = READER_MODE_LOCKED;
break;
case PANEL_CTRL_DATA_LEARN:
case DATA_DOOR_CTRL_LEARN:
DEBUGSTR("mode LEARN\n");
panel_conf[panel_id].mode = PANEL_MODE_LEARN;
reader_conf[reader_idx].mode = READER_MODE_LEARN;
break;
case PANEL_CTRL_DATA_CLR_CACHE:
case DATA_DOOR_CTRL_CLR_CACHE:
DEBUGSTR("cmd CLR CACHE\n");
#if CACHING_ENABLED
static_cache_reset();
......@@ -232,7 +232,7 @@ void term_can_recv(uint8_t msg_obj_num)
else return;
}
static void terminal_register_user(uint32_t user_id, uint8_t panel_id)
static void terminal_register_user(uint32_t user_id, uint8_t reader_idx)
{
//check if master online
if (_act_master == ACS_RESERVED_ADDR)
......@@ -242,25 +242,25 @@ static void terminal_register_user(uint32_t user_id, uint8_t panel_id)
}
// Prepare msg head to send request on CAN
msg_head_t head;
acs_msg_head_t head;
head.scalar = CAN_MSGOBJ_EXT;
head.prio = PRIO_NEW_USER;
head.fc = FC_NEW_USER;
head.dst = _act_master;
if (panel_id == ACC_PANEL_A)
if (reader_idx == ACS_READER_A_IDX)
{
head.src = get_acs_panel_a_addr();
head.src = get_reader_a_addr();
CAN_send_once(ACS_MSGOBJ_SEND_DOOR_A, head.scalar, (void *)&user_id, sizeof(user_id));
}
else if (panel_id == ACC_PANEL_B)
else if (reader_idx == ACS_READER_B_IDX)
{
head.src = get_acs_panel_b_addr();
head.src = get_reader_b_addr();
CAN_send_once(ACS_MSGOBJ_SEND_DOOR_B, head.scalar, (void *)&user_id, sizeof(user_id));
}
}
static void terminal_request_auth(uint32_t user_id, uint8_t panel_id)
static void terminal_request_auth(uint32_t user_id, uint8_t reader_idx)
{
//check if master online
if (_act_master == ACS_RESERVED_ADDR)
......@@ -270,53 +270,53 @@ static void terminal_request_auth(uint32_t user_id, uint8_t panel_id)
}
// Prepare msg head to send request on CAN
msg_head_t head;
acs_msg_head_t head;
head.scalar = CAN_MSGOBJ_EXT;
head.prio = PRIO_USER_AUTH_REQ;
head.fc = FC_USER_AUTH_REQ;
head.dst = _act_master;
if (panel_id == ACC_PANEL_A)
if (reader_idx == ACS_READER_A_IDX)
{
head.src = get_acs_panel_a_addr();
head.src = get_reader_a_addr();
CAN_send_once(ACS_MSGOBJ_SEND_DOOR_A, head.scalar, (void *)&user_id, sizeof(user_id));
}
else if (panel_id == ACC_PANEL_B)
else if (reader_idx == ACS_READER_B_IDX)
{
head.src = get_acs_panel_b_addr();
head.src = get_reader_b_addr();
CAN_send_once(ACS_MSGOBJ_SEND_DOOR_B, head.scalar, (void *)&user_id, sizeof(user_id));
}
}
static void terminal_user_identified(uint32_t user_id, uint8_t panel_id)
static void terminal_user_identified(uint32_t user_id, uint8_t reader_idx)
{
#if CACHING_ENABLED
term_cache_item_t user = {.key = user_id};
#endif
if (panel_id < DOOR_ACC_PANEL_COUNT)
if (reader_idx < ACS_READER_COUNT)
{
if (panel_conf[panel_id].mode == PANEL_MODE_LOCKED)
if (reader_conf[reader_idx].mode == READER_MODE_LOCKED)
{
terminal_user_not_authorized(panel_id);
terminal_user_not_authorized(reader_idx);
}
else if (panel_conf[panel_id].mode == PANEL_MODE_LEARN)
else if (reader_conf[reader_idx].mode == READER_MODE_LEARN)
{
terminal_register_user(user_id, panel_id);
terminal_register_user(user_id, reader_idx);
}
#if CACHING_ENABLED
else if (static_cache_get(&user))
{
if (map_panel_id_to_cache(panel_id) & user.value)
if (map_reader_idx_to_cache(reader_idx) & user.value)
{
terminal_user_authorized(panel_id);
terminal_user_authorized(reader_idx);
}
}
#endif
else
{
terminal_request_auth(user_id, panel_id);
terminal_request_auth(user_id, reader_idx);
}
}
}
......@@ -330,11 +330,11 @@ static void terminal_task(void *pvParameters)
while (true)
{
uint32_t user_id;
uint8_t panel_id = panel_get_request_from_buffer(&user_id);
if (panel_id < DOOR_ACC_PANEL_COUNT)
uint8_t reader_idx = reader_get_request_from_buffer(&user_id);
if (reader_idx < ACS_READER_COUNT)
{
DEBUGSTR("user identified\n");
terminal_user_identified(user_id, panel_id);
terminal_user_identified(user_id, reader_idx);
}
}
}
......@@ -362,25 +362,25 @@ void terminal_init(void)
// CAN msg filter for door A
CAN_recv_filter(ACS_MSGOBJ_RECV_DOOR_A,
get_acs_panel_a_addr() << ACS_DST_ADDR_OFFSET,
get_reader_a_addr() << ACS_DST_ADDR_OFFSET,
ACS_DST_ADDR_MASK, true);
// CAN msg filter for door B
CAN_recv_filter(ACS_MSGOBJ_RECV_DOOR_B,
get_acs_panel_b_addr() << ACS_DST_ADDR_OFFSET,
get_reader_b_addr() << ACS_DST_ADDR_OFFSET,
ACS_DST_ADDR_MASK, true);
// CAN msg filter for broadcast
CAN_recv_filter(ACS_MSGOBJ_RECV_BCAST,
ACS_BROADCAST_ADDR << ACS_DST_ADDR_OFFSET,
ACS_DST_ADDR_MASK, true);
// initialize panels
for (size_t id = 0; id < DOOR_ACC_PANEL_COUNT; ++id)
// initialize card readers
for (size_t id = 0; id < ACS_READER_COUNT; ++id)
{
if (panel_conf[id].enabled) terminal_reconfigure(NULL, id);
if (reader_conf[id].enabled) terminal_reconfigure(NULL, id);
}
// create timer for master alive status timeout
_act_timer = xTimerCreate("MAT", (MASTER_ALIVE_TIMEOUT / portTICK_PERIOD_MS),
_act_timer = xTimerCreate("MAT", (ACS_MASTER_ALIVE_TIMEOUT_MS / portTICK_PERIOD_MS),
pdTRUE, (void *)_act_timer_id, _timer_callback);
configASSERT(_act_timer);
......@@ -388,32 +388,32 @@ void terminal_init(void)
xTaskCreate(terminal_task, "term_tsk", configMINIMAL_STACK_SIZE + 128, NULL, (tskIDLE_PRIORITY + 1UL), NULL);
}
void terminal_reconfigure(panel_conf_t * panel_cfg, uint8_t panel_id)
void terminal_reconfigure(reader_conf_t * reader_cfg, uint8_t reader_idx)
{
if (panel_id >= DOOR_ACC_PANEL_COUNT) return;
if (reader_idx >= ACS_READER_COUNT) return;
portENTER_CRITICAL();
if (panel_cfg != NULL)
if (reader_cfg != NULL)
{
memcpy(&panel_conf[panel_id], panel_cfg, sizeof(panel_conf_t));
memcpy(&reader_conf[reader_idx], reader_cfg, sizeof(reader_conf_t));
//reconfigure interface to card reader
if (panel_conf[panel_id].enabled)
if (reader_conf[reader_idx].enabled)
{
panel_init(panel_id);
DEBUGSTR("panel enabled\n");
reader_init(reader_idx);
DEBUGSTR("reader enabled\n");
}
else
{
//disable interface
panel_deinit(panel_id);
DEBUGSTR("panel disabled\n");
reader_deinit(reader_idx);
DEBUGSTR("reader disabled\n");
}
}
else
{
panel_init(panel_id);
reader_init(reader_idx);
}
portEXIT_CRITICAL();
......
......@@ -8,11 +8,11 @@
#ifndef BSP_TERMINAL_H_
#define BSP_TERMINAL_H_
#include <reader.h>
#include <stdint.h>
#include "panel.h"
void terminal_init(void);
void terminal_reconfigure(panel_conf_t * panel_cfg, uint8_t id);
void terminal_reconfigure(reader_conf_t * reader_cfg, uint8_t id);
#endif /* BSP_TERMINAL_H_ */
......@@ -6,22 +6,22 @@
#include "terminal_config.h"
#include "storage.h"
#include "can/can_term_protocol.h"
#include "acs_can_protocol.h"
#define ACS_ADDR_BIT_MASK ((1 << ACS_ADDR_BITS) - 1)
// door addresses in ACS
uint16_t _ACS_PANEL_A_ADDR = 0x4; // even
uint16_t _ACS_PANEL_B_ADDR = 0x5; // odd
uint16_t _READER_A_ADDR = 0x4; // even
uint16_t _READER_B_ADDR = 0x5; // odd
inline uint16_t get_acs_panel_a_addr(void)
inline uint16_t get_reader_a_addr(void)
{
return _ACS_PANEL_A_ADDR;
return _READER_A_ADDR;
}
inline uint16_t get_acs_panel_b_addr(void)
inline uint16_t get_reader_b_addr(void)
{
return _ACS_PANEL_B_ADDR;
return _READER_B_ADDR;
}
static bool _load_acs_addrs_from_ext_stor(void)
......@@ -30,11 +30,11 @@ static bool _load_acs_addrs_from_ext_stor(void)
uint16_t first_addr = 0;
ret_val &= storage_read_word_le(PTR_ACS_PANEL_FIRST_ADDR, &first_addr);
ret_val &= storage_read_word_le(PTR_READER_FIRST_ADDR, &first_addr);
if (ret_val)
{
set_acs_panel_addr(first_addr);
set_reader_addr(first_addr);
}
return ret_val;
......@@ -44,24 +44,24 @@ static bool _save_acs_addrs_from_ext_stor(void)
{
bool ret_val = true;
ret_val &= storage_write_word_le(PTR_ACS_PANEL_FIRST_ADDR, _ACS_PANEL_A_ADDR);
ret_val &= storage_write_word_le(PTR_READER_FIRST_ADDR, _READER_A_ADDR);
return ret_val;
}
void set_acs_panel_addr(uint16_t first_acs_addr)
void set_reader_addr(uint16_t first_acs_addr)
{
first_acs_addr &= ACS_ADDR_BIT_MASK;
if (first_acs_addr & 0x1) // even address
{
_ACS_PANEL_B_ADDR = first_acs_addr;
_ACS_PANEL_A_ADDR = first_acs_addr - 1;
_READER_B_ADDR = first_acs_addr;
_READER_A_ADDR = first_acs_addr - 1;
}
else // odd address
{
_ACS_PANEL_A_ADDR = first_acs_addr;
_ACS_PANEL_B_ADDR = first_acs_addr + 1;
_READER_A_ADDR = first_acs_addr;
_READER_B_ADDR = first_acs_addr + 1;
}
}
......
......@@ -30,17 +30,17 @@
extern unsigned int TERMINAL_UID[5]; // 0 - status code, 1 - least significant word
//-------------------------------------------------------------
// Door addresses in ACS system
// Door addresses in a network for ACS system
//
// Read from external storage on startup
// Designed to be even (address A) and odd (address B)
// Always true: ADDR_B = ADDR_A + 1
// Address getters
extern uint16_t get_acs_panel_a_addr(void);
extern uint16_t get_acs_panel_b_addr(void);
extern uint16_t get_reader_a_addr(void);
extern uint16_t get_reader_b_addr(void);
// Address setter
void set_acs_panel_addr(const uint16_t first_acs_addr);
void set_reader_addr(const uint16_t first_acs_addr);
// Expected organization in external address space:
// | 0x00 | A_ADDR [7:0]
......@@ -49,7 +49,7 @@ void set_acs_panel_addr(const uint16_t first_acs_addr);
// The address actually uses less then 16 bits. See address bit width in ACS protocol.
#define PTR_ACS_PANEL_FIRST_ADDR 0x0 // pointer to external memory
#define PTR_READER_FIRST_ADDR 0x0 // pointer to external memory
// Setting for I2C external storage for door adresses
......@@ -60,64 +60,64 @@ void set_acs_panel_addr(const uint16_t first_acs_addr);
//-------------------------------------------------------------
// internal number of card readers
#define ACC_PANEL_A 0
#define ACC_PANEL_B 1
#define ACS_READER_A_IDX 0
#define ACS_READER_B_IDX 1
// card reader count 1/2
#define DOOR_ACC_PANEL_COUNT 2
#define ACS_READER_COUNT 2
#define BEEP_ON_SUCCESS false
#define OK_LED_ON_SUCCESS true
// communication status led
#define ACS_PANEL_STATUS_LED_PORT 0
#define ACS_PANEL_STATUS_LED_PIN 6
#define ACS_COMM_STATUS_LED_PORT 0
#define ACS_COMM_STATUS_LED_PIN 6
//---------------------------------------------------------------------------------------------------------------------
// Settings for panel A
// Settings for RFID reader A
//---------------------------------------------------------------------------------------------------------------------
#define DOOR_1_ACC_PANEL_ON true
#define DOOR_1_ACC_PANEL_PORT 3 // Port must be unique for each access panel (reader)
// Can be only 2 or 3.
#define DOOR_1_ACC_PANEL_D0_PIN 2
#define DOOR_1_ACC_PANEL_D1_PIN 1
#define DOOR_1_ACC_PANEL_BEEP_PORT 2
#define DOOR_1_ACC_PANEL_BEEP_PIN 7
#define DOOR_1_ACC_PANEL_GLED_PORT 2
#define DOOR_1_ACC_PANEL_GLED_PIN 1
#define DOOR_1_ACC_PANEL_RLED_PORT 2
#define DOOR_1_ACC_PANEL_RLED_PIN 0
#define DOOR_1_RELAY_PORT 1