Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * \file csc_app.c
- * \brief Pequena descricao do arquivo
- *
- * Descricao mais detalhada do arquivo
- *
- **/
- /*- Includes -----------------------------------------------------------------------*/
- #include <asf.h>
- #include "platform.h"
- #include "console_serial.h"
- #include "at_ble_api.h"
- #include "ble_manager.h"
- #include "csc_app.h"
- #include "cscp.h"
- #include "cscs.h"
- #include "conf_extint.h"
- #include "sio2host.h"
- #include "conf_at25dfx.h"
- /* =========================== GLOBALS ============================================================ */
- /* Received notification data structure */
- csc_report_ntf_t recv_ntf_info;
- /* Data length to be send over the air */
- uint16_t send_length = 0;
- /* Buffer data to be send over the air */
- uint8_t send_data[APP_TX_BUF_SIZE];
- /* Buffer data to memory */
- //#define AT25DFX_BUFFER_SIZE (10)
- //static uint8_t read_buffer[AT25DFX_BUFFER_SIZE];
- //static uint8_t write_buffer[AT25DFX_BUFFER_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- /* Driver_instances*/
- struct spi_module at25dfx_spi;
- struct at25dfx_chip_module at25dfx_chip;
- /*
- @fn static void at25dfx_init (void)
- @brief Descricao
- @param NULL
- @return NULL
- */
- static void at25dfx_init(void)
- {
- //! [config_instances]
- struct at25dfx_chip_config at25dfx_chip_config;
- struct spi_config at25dfx_spi_config;
- //! [config_instances]
- //! [spi_setup]
- at25dfx_spi_get_config_defaults(&at25dfx_spi_config);
- at25dfx_spi_config.mode_specific.master.baudrate = AT25DFX_CLOCK_SPEED;
- at25dfx_spi_config.mux_setting = AT25DFX_SPI_PINMUX_SETTING;
- at25dfx_spi_config.pinmux_pad0 = AT25DFX_SPI_PINMUX_PAD0;
- at25dfx_spi_config.pinmux_pad1 = AT25DFX_SPI_PINMUX_PAD1;
- at25dfx_spi_config.pinmux_pad2 = AT25DFX_SPI_PINMUX_PAD2;
- at25dfx_spi_config.pinmux_pad3 = AT25DFX_SPI_PINMUX_PAD3;
- spi_init(&at25dfx_spi, AT25DFX_SPI, &at25dfx_spi_config);
- spi_enable(&at25dfx_spi);
- //! [spi_setup]
- //! [chip_setup]
- at25dfx_chip_config.type = AT25DFX_MEM_TYPE;
- at25dfx_chip_config.cs_pin = AT25DFX_CS;
- at25dfx_chip_init(&at25dfx_chip, &at25dfx_spi, &at25dfx_chip_config);
- //! [chip_setup]
- }
- static const ble_gap_event_cb_t app_gap_handle = {
- .connected = app_connected_event_handler,
- .disconnected = app_disconnected_event_handler
- };
- /**
- @fn static at_ble_status_t app_disconnected_event_handler (void *params)
- @brief app_connected_state ble manager notifies the application about state
- @param ponteiro do tipo void Explicacao do ponteiro
- @return at_ble_status_t AT_BLE_SUCCESS Explicacao do retorno
- **/
- static at_ble_status_t app_connected_event_handler(void *params)
- {
- ALL_UNUSED(params);
- return AT_BLE_SUCCESS;
- }
- /*
- @fn static at_ble_status_t app_disconnected_event_handler (void *params)
- @brief app_connected_state ble manager notifies the application about state
- @param ponteiro do tipo void Explicacao do ponteiro
- @return at_ble_status_t AT_BLE_SUCCESS Explicacao do retorno
- */
- static at_ble_status_t app_disconnected_event_handler(void *params)
- {
- /* Started advertisement */
- csc_prf_dev_adv();
- ALL_UNUSED(params);
- return AT_BLE_SUCCESS;
- }
- //uint8_t palavra[20];
- char palavra[20];
- uint8_t dadorecebido=0;
- uint8_t tamdado=0;
- /*
- @fn static void csc_app_recv_buf (uint8_t *recv_data, uint8_t recv_len)
- @brief Function used for receive data
- @param ponteiro do tipo uint8_t (unsigned char) e uint8_t (unsigned char) Explicacao dos parametros
- @return NULL
- */
- static void csc_app_recv_buf(uint8_t *recv_data, uint8_t recv_len)
- {
- uint16_t ind = 0;
- if (recv_len){
- //for (ind = 0; ind < recv_len; ind++){
- for (ind = 0; ind < 20; ind++){
- //sio2host_putchar(recv_data[ind]);
- if(ind < recv_len)
- palavra[ind] = recv_data[ind];
- else
- palavra[ind] = 0;
- }
- DBG_LOG("\r\n");
- dadorecebido=1;
- tamdado=recv_len;
- }
- }
- /*
- @fn static void csc_prf_report_ntf_cb (csc_report_ntf_t *report_info)
- @brief Callback called for new data from remote device
- @param ponteiro do tipo csc_report_ntf_t Explicacao do ponteiro
- @return NULL
- */
- static void csc_prf_report_ntf_cb(csc_report_ntf_t *report_info)
- {
- DBG_LOG("\r\n");
- csc_app_recv_buf(report_info->recv_buff, report_info->recv_buff_len);
- }
- typedef struct {
- uint8_t user[11];
- uint8_t password[5];
- uint8_t portas[6];
- uint8_t teste[3];
- } ficha_user;
- ficha_user ficha[3];
- /*
- @fn void configure_ficha ()
- @brief Descricao rapida
- @param NULL
- @return NULL
- */
- void configure_ficha()
- {
- uint8_t *aux1, *aux2, *aux3;
- //! [unprotect_sector]
- at25dfx_chip_set_sector_protect(&at25dfx_chip, 0x10000, false);
- //! [unprotect_sector]
- //! [erase_block]
- at25dfx_chip_erase_block(&at25dfx_chip, 0x10000, AT25DFX_BLOCK_SIZE_4KB);
- //! [erase_block]
- /*ficha[0].user = "diretor"; //64 bits
- ficha[0].password = "9999"; // 40 bits
- ficha[0].portas = "1,2,3"; // 48 bits
- ficha[1].user = "contador"; // 72 bits
- ficha[1].password = "4321"; // 40 bits
- ficha[1].portas = "1,3 "; // 48 bits
- ficha[2].user = "estagiario"; // 88 bits
- ficha[2].password = "1234"; //40 bits
- ficha[2].portas = "1 "; // 48 bits
- */
- aux1 = "diretor";
- aux2 = "9999";
- aux3 = "1,2,3";
- //! [write_buffer]
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x10000, aux1, (8));
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x10020, aux2, (5));
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x10040, aux3, (6));
- //! [write_buffer]
- //! [read_buffer]
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x10000, ficha[0].user, (8));
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x10020, ficha[0].password, (5));
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x10040, ficha[0].portas, (6));
- //! [read_buffer]
- aux1 = "contador";
- aux2 = "4321";
- aux3 = "1,3 ";
- //! [write_buffer]
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x10060, aux1, (9));
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x10080, aux2, (5));
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x100A0, aux3, (6));
- //! [write_buffer]
- //! [read_buffer]
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x10060, ficha[1].user, (9));
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x10080, ficha[1].password, (5));
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x100A0, ficha[1].portas, (6));
- //! [read_buffer]
- aux1 = "estagiario";
- aux2 = "1234";
- aux3 = "1 ";
- //! [write_buffer]
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x100C0, aux1, (11));
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x100E0, aux2, (5));
- at25dfx_chip_write_buffer(&at25dfx_chip, 0x10100, aux3, (6));
- //! [write_buffer]
- //! [read_buffer]
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x100C0, ficha[2].user, (11));
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x100E0, ficha[2].password, (5));
- at25dfx_chip_read_buffer(&at25dfx_chip, 0x10100, ficha[2].portas, (6));
- //! [read_buffer]
- //! [global_protect]
- at25dfx_chip_set_global_sector_protect(&at25dfx_chip, true);
- //! [global_protect]
- // sleep
- at25dfx_chip_sleep(&at25dfx_chip);
- }
- /*
- @fn int main ()
- @brief Descricao rapida
- @param NULL
- @return 0
- */
- int main ()
- {
- board_init();
- system_init();
- /* Initialize serial console */
- sio2host_init();
- //DBG_LOG("Initializing Custom Serial Chat Application");
- /* Initialize the buffer address and buffer length based on user input */
- csc_prf_buf_init(&send_data[0], APP_TX_BUF_SIZE);
- /* initialize the ble chip and Set the device mac address */
- ble_device_init(NULL);
- /* Initializing the profile */
- csc_prf_init(NULL);
- /* Started advertisement */
- csc_prf_dev_adv();
- ble_mgr_events_callback_handler(REGISTER_CALL_BACK, BLE_GAP_EVENT_TYPE, &app_gap_handle);
- /* Register the notification handler */
- notify_recv_ntf_handler(csc_prf_report_ntf_cb);
- ///////////////// MEMÓRIA /////////////////
- at25dfx_init();
- //! [wake_chip]
- at25dfx_chip_wake(&at25dfx_chip);
- //! [wake_chip]
- //! [check_presence]
- if (at25dfx_chip_check_presence(&at25dfx_chip) != STATUS_OK) {
- // Handle missing or non-responsive device
- }
- //! [check_presence]
- configure_ficha();
- ///////////////// MEMÓRIA /////////////////
- int i, estado = 0, erro = 0;
- char senha_digitada[20], user_digitado[50], *str_print;
- ficha_user user_atual;
- int inicialization=1;
- while(inicialization){
- ble_event_task();
- if(dadorecebido){
- dadorecebido=0;
- inicialization=0;
- }
- }
- /* Capturing the events */
- while(true){
- ble_event_task();
- //csc_app_send_buf();
- switch(estado) {
- //ESPERA USER//
- case 0:
- //printf("Entre com um usuario valido:\r\n");
- //scanf("%s", user_digitado);
- //printf("User: %s\r\n", user_digitado);
- str_print = "Entre com um usuario valido";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- estado = 1;
- break;
- //VERFICA USER//
- case 1:
- if(!dadorecebido){
- estado = 1;
- }
- else{
- dadorecebido=0;
- csc_prf_send_data(&palavra[0], tamdado);
- for(i=0;i<3;i++){
- if(strcmp(palavra, ficha[i].user) == 0){
- user_atual = ficha[i];
- str_print = "Usuario valido";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- estado = 2;
- break;
- }
- else{
- estado = 0;
- }
- }
- if(estado==0){
- str_print = "Usuario invalido";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- }
- }
- break;
- //ESPERA SENHA//
- case 2:
- //printf("Digite uma senha valida:\r\n");
- //scanf("%s", senha_digitada);
- //printf("Senha= %s\r\n", senha_digitada);
- str_print = "Entre com uma senha valida";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- estado = 3;
- break;
- //VERIFICA SENHA//
- case 3:
- /*
- if(strcmp(senha_digitada, user_atual.password) == 0)
- estado = 4;
- else
- estado = 5;
- */
- if(!dadorecebido){
- estado = 3;
- }
- else{
- dadorecebido=0;
- csc_prf_send_data(&palavra[0], tamdado);
- if(strcmp(palavra, user_atual.password) == 0)
- estado = 4;
- else
- estado = 5;
- }
- break;
- //SENHA VALIDA//
- case 4:
- //printf("Acesso Permitido!\r\n");
- //printf("Voce pode acessar as portas: %s\r\n", user_atual.portas);
- str_print = "Acesso Permitido!";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- str_print = "Voce pode acessar as portas:";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- csc_prf_send_data(&user_atual.portas[0], strlen(user_atual.portas));
- delay_s(5);
- erro = 0;
- estado = 0;
- break;
- //SENHA INVALIDA//
- case 5:
- //printf("Acesso Negado!\r\n");
- str_print = "Acesso Negado!";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- erro++;
- if(erro < 3){
- delay_s(2);
- estado = 2;
- }
- else{
- //printf("Muitas tentativas erradas!\r\n");
- //printf("Aguarde alguns segundos para tentar novamente!\r\n");
- str_print = "Muitas tentativas erradas!";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- str_print = "Aguarde alguns segundos para tentar novamente!";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- delay_s(6);
- erro = 0;
- estado = 0;
- }
- break;
- //ERRO INESPERADO//
- default: //printf("ERRO %d\r\n", estado);
- str_print = "Estado invalido!";
- csc_prf_send_data(&str_print[0], strlen(str_print));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement