Advertisement
Guest User

Untitled

a guest
Jul 1st, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. /**
  2. * \file csc_app.c
  3. * \brief Pequena descricao do arquivo
  4. *
  5. * Descricao mais detalhada do arquivo
  6. *
  7. **/
  8.  
  9. /*- Includes -----------------------------------------------------------------------*/
  10. #include <asf.h>
  11. #include "platform.h"
  12. #include "console_serial.h"
  13. #include "at_ble_api.h"
  14. #include "ble_manager.h"
  15. #include "csc_app.h"
  16. #include "cscp.h"
  17. #include "cscs.h"
  18. #include "conf_extint.h"
  19. #include "sio2host.h"
  20. #include "conf_at25dfx.h"
  21. /* =========================== GLOBALS ============================================================ */
  22.  
  23. /* Received notification data structure */
  24. csc_report_ntf_t recv_ntf_info;
  25.  
  26. /* Data length to be send over the air */
  27. uint16_t send_length = 0;
  28.  
  29. /* Buffer data to be send over the air */
  30. uint8_t send_data[APP_TX_BUF_SIZE];
  31.  
  32. /* Buffer data to memory */
  33. //#define AT25DFX_BUFFER_SIZE (10)
  34. //static uint8_t read_buffer[AT25DFX_BUFFER_SIZE];
  35. //static uint8_t write_buffer[AT25DFX_BUFFER_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  36.  
  37.  
  38. /* Driver_instances*/
  39. struct spi_module at25dfx_spi;
  40. struct at25dfx_chip_module at25dfx_chip;
  41.  
  42. /*
  43. @fn static void at25dfx_init (void)
  44. @brief Descricao
  45. @param NULL
  46. @return NULL
  47. */
  48. static void at25dfx_init(void)
  49. {
  50. //! [config_instances]
  51. struct at25dfx_chip_config at25dfx_chip_config;
  52. struct spi_config at25dfx_spi_config;
  53. //! [config_instances]
  54.  
  55. //! [spi_setup]
  56. at25dfx_spi_get_config_defaults(&at25dfx_spi_config);
  57. at25dfx_spi_config.mode_specific.master.baudrate = AT25DFX_CLOCK_SPEED;
  58. at25dfx_spi_config.mux_setting = AT25DFX_SPI_PINMUX_SETTING;
  59. at25dfx_spi_config.pinmux_pad0 = AT25DFX_SPI_PINMUX_PAD0;
  60. at25dfx_spi_config.pinmux_pad1 = AT25DFX_SPI_PINMUX_PAD1;
  61. at25dfx_spi_config.pinmux_pad2 = AT25DFX_SPI_PINMUX_PAD2;
  62. at25dfx_spi_config.pinmux_pad3 = AT25DFX_SPI_PINMUX_PAD3;
  63.  
  64. spi_init(&at25dfx_spi, AT25DFX_SPI, &at25dfx_spi_config);
  65. spi_enable(&at25dfx_spi);
  66.  
  67. //! [spi_setup]
  68.  
  69. //! [chip_setup]
  70. at25dfx_chip_config.type = AT25DFX_MEM_TYPE;
  71. at25dfx_chip_config.cs_pin = AT25DFX_CS;
  72.  
  73. at25dfx_chip_init(&at25dfx_chip, &at25dfx_spi, &at25dfx_chip_config);
  74.  
  75. //! [chip_setup]
  76. }
  77.  
  78. static const ble_gap_event_cb_t app_gap_handle = {
  79. .connected = app_connected_event_handler,
  80. .disconnected = app_disconnected_event_handler
  81. };
  82.  
  83.  
  84. /**
  85. @fn static at_ble_status_t app_disconnected_event_handler (void *params)
  86. @brief app_connected_state ble manager notifies the application about state
  87. @param ponteiro do tipo void Explicacao do ponteiro
  88. @return at_ble_status_t AT_BLE_SUCCESS Explicacao do retorno
  89. **/
  90. static at_ble_status_t app_connected_event_handler(void *params)
  91. {
  92. ALL_UNUSED(params);
  93. return AT_BLE_SUCCESS;
  94. }
  95.  
  96. /*
  97. @fn static at_ble_status_t app_disconnected_event_handler (void *params)
  98. @brief app_connected_state ble manager notifies the application about state
  99. @param ponteiro do tipo void Explicacao do ponteiro
  100. @return at_ble_status_t AT_BLE_SUCCESS Explicacao do retorno
  101. */
  102. static at_ble_status_t app_disconnected_event_handler(void *params)
  103. {
  104. /* Started advertisement */
  105. csc_prf_dev_adv();
  106. ALL_UNUSED(params);
  107. return AT_BLE_SUCCESS;
  108. }
  109.  
  110.  
  111. //uint8_t palavra[20];
  112. char palavra[20];
  113. uint8_t dadorecebido=0;
  114. uint8_t tamdado=0;
  115.  
  116.  
  117. /*
  118. @fn static void csc_app_recv_buf (uint8_t *recv_data, uint8_t recv_len)
  119. @brief Function used for receive data
  120. @param ponteiro do tipo uint8_t (unsigned char) e uint8_t (unsigned char) Explicacao dos parametros
  121. @return NULL
  122. */
  123. static void csc_app_recv_buf(uint8_t *recv_data, uint8_t recv_len)
  124. {
  125. uint16_t ind = 0;
  126. if (recv_len){
  127. //for (ind = 0; ind < recv_len; ind++){
  128. for (ind = 0; ind < 20; ind++){
  129. //sio2host_putchar(recv_data[ind]);
  130. if(ind < recv_len)
  131. palavra[ind] = recv_data[ind];
  132. else
  133. palavra[ind] = 0;
  134. }
  135. DBG_LOG("\r\n");
  136.  
  137. dadorecebido=1;
  138. tamdado=recv_len;
  139. }
  140. }
  141.  
  142. /*
  143. @fn static void csc_prf_report_ntf_cb (csc_report_ntf_t *report_info)
  144. @brief Callback called for new data from remote device
  145. @param ponteiro do tipo csc_report_ntf_t Explicacao do ponteiro
  146. @return NULL
  147. */
  148. static void csc_prf_report_ntf_cb(csc_report_ntf_t *report_info)
  149. {
  150. DBG_LOG("\r\n");
  151. csc_app_recv_buf(report_info->recv_buff, report_info->recv_buff_len);
  152. }
  153.  
  154. typedef struct {
  155. uint8_t user[11];
  156. uint8_t password[5];
  157. uint8_t portas[6];
  158. uint8_t teste[3];
  159. } ficha_user;
  160.  
  161. ficha_user ficha[3];
  162.  
  163. /*
  164. @fn void configure_ficha ()
  165. @brief Descricao rapida
  166. @param NULL
  167. @return NULL
  168. */
  169. void configure_ficha()
  170. {
  171.  
  172. uint8_t *aux1, *aux2, *aux3;
  173.  
  174. //! [unprotect_sector]
  175. at25dfx_chip_set_sector_protect(&at25dfx_chip, 0x10000, false);
  176. //! [unprotect_sector]
  177.  
  178. //! [erase_block]
  179. at25dfx_chip_erase_block(&at25dfx_chip, 0x10000, AT25DFX_BLOCK_SIZE_4KB);
  180. //! [erase_block]
  181.  
  182. /*ficha[0].user = "diretor"; //64 bits
  183. ficha[0].password = "9999"; // 40 bits
  184. ficha[0].portas = "1,2,3"; // 48 bits
  185.  
  186. ficha[1].user = "contador"; // 72 bits
  187. ficha[1].password = "4321"; // 40 bits
  188. ficha[1].portas = "1,3 "; // 48 bits
  189.  
  190. ficha[2].user = "estagiario"; // 88 bits
  191. ficha[2].password = "1234"; //40 bits
  192. ficha[2].portas = "1 "; // 48 bits
  193. */
  194. aux1 = "diretor";
  195. aux2 = "9999";
  196. aux3 = "1,2,3";
  197.  
  198. //! [write_buffer]
  199. at25dfx_chip_write_buffer(&at25dfx_chip, 0x10000, aux1, (8));
  200. at25dfx_chip_write_buffer(&at25dfx_chip, 0x10020, aux2, (5));
  201. at25dfx_chip_write_buffer(&at25dfx_chip, 0x10040, aux3, (6));
  202. //! [write_buffer]
  203.  
  204. //! [read_buffer]
  205. at25dfx_chip_read_buffer(&at25dfx_chip, 0x10000, ficha[0].user, (8));
  206. at25dfx_chip_read_buffer(&at25dfx_chip, 0x10020, ficha[0].password, (5));
  207. at25dfx_chip_read_buffer(&at25dfx_chip, 0x10040, ficha[0].portas, (6));
  208. //! [read_buffer]
  209.  
  210. aux1 = "contador";
  211. aux2 = "4321";
  212. aux3 = "1,3 ";
  213.  
  214. //! [write_buffer]
  215. at25dfx_chip_write_buffer(&at25dfx_chip, 0x10060, aux1, (9));
  216. at25dfx_chip_write_buffer(&at25dfx_chip, 0x10080, aux2, (5));
  217. at25dfx_chip_write_buffer(&at25dfx_chip, 0x100A0, aux3, (6));
  218. //! [write_buffer]
  219.  
  220. //! [read_buffer]
  221. at25dfx_chip_read_buffer(&at25dfx_chip, 0x10060, ficha[1].user, (9));
  222. at25dfx_chip_read_buffer(&at25dfx_chip, 0x10080, ficha[1].password, (5));
  223. at25dfx_chip_read_buffer(&at25dfx_chip, 0x100A0, ficha[1].portas, (6));
  224. //! [read_buffer]
  225.  
  226. aux1 = "estagiario";
  227. aux2 = "1234";
  228. aux3 = "1 ";
  229.  
  230. //! [write_buffer]
  231. at25dfx_chip_write_buffer(&at25dfx_chip, 0x100C0, aux1, (11));
  232. at25dfx_chip_write_buffer(&at25dfx_chip, 0x100E0, aux2, (5));
  233. at25dfx_chip_write_buffer(&at25dfx_chip, 0x10100, aux3, (6));
  234. //! [write_buffer]
  235.  
  236. //! [read_buffer]
  237. at25dfx_chip_read_buffer(&at25dfx_chip, 0x100C0, ficha[2].user, (11));
  238. at25dfx_chip_read_buffer(&at25dfx_chip, 0x100E0, ficha[2].password, (5));
  239. at25dfx_chip_read_buffer(&at25dfx_chip, 0x10100, ficha[2].portas, (6));
  240. //! [read_buffer]
  241.  
  242. //! [global_protect]
  243. at25dfx_chip_set_global_sector_protect(&at25dfx_chip, true);
  244. //! [global_protect]
  245.  
  246. // sleep
  247. at25dfx_chip_sleep(&at25dfx_chip);
  248. }
  249.  
  250. /*
  251. @fn int main ()
  252. @brief Descricao rapida
  253. @param NULL
  254. @return 0
  255. */
  256. int main ()
  257. {
  258. board_init();
  259. system_init();
  260.  
  261. /* Initialize serial console */
  262. sio2host_init();
  263.  
  264. //DBG_LOG("Initializing Custom Serial Chat Application");
  265.  
  266. /* Initialize the buffer address and buffer length based on user input */
  267. csc_prf_buf_init(&send_data[0], APP_TX_BUF_SIZE);
  268.  
  269. /* initialize the ble chip and Set the device mac address */
  270. ble_device_init(NULL);
  271.  
  272. /* Initializing the profile */
  273. csc_prf_init(NULL);
  274.  
  275. /* Started advertisement */
  276. csc_prf_dev_adv();
  277.  
  278. ble_mgr_events_callback_handler(REGISTER_CALL_BACK, BLE_GAP_EVENT_TYPE, &app_gap_handle);
  279.  
  280. /* Register the notification handler */
  281. notify_recv_ntf_handler(csc_prf_report_ntf_cb);
  282.  
  283. ///////////////// MEMÓRIA /////////////////
  284. at25dfx_init();
  285.  
  286. //! [wake_chip]
  287. at25dfx_chip_wake(&at25dfx_chip);
  288. //! [wake_chip]
  289.  
  290. //! [check_presence]
  291. if (at25dfx_chip_check_presence(&at25dfx_chip) != STATUS_OK) {
  292. // Handle missing or non-responsive device
  293. }
  294. //! [check_presence]
  295.  
  296. configure_ficha();
  297. ///////////////// MEMÓRIA /////////////////
  298.  
  299.  
  300. int i, estado = 0, erro = 0;
  301. char senha_digitada[20], user_digitado[50], *str_print;
  302. ficha_user user_atual;
  303.  
  304. int inicialization=1;
  305. while(inicialization){
  306. ble_event_task();
  307. if(dadorecebido){
  308. dadorecebido=0;
  309. inicialization=0;
  310.  
  311. }
  312. }
  313.  
  314. /* Capturing the events */
  315. while(true){
  316. ble_event_task();
  317. //csc_app_send_buf();
  318.  
  319. switch(estado) {
  320. //ESPERA USER//
  321. case 0:
  322. //printf("Entre com um usuario valido:\r\n");
  323. //scanf("%s", user_digitado);
  324. //printf("User: %s\r\n", user_digitado);
  325.  
  326. str_print = "Entre com um usuario valido";
  327. csc_prf_send_data(&str_print[0], strlen(str_print));
  328.  
  329. estado = 1;
  330.  
  331. break;
  332.  
  333. //VERFICA USER//
  334. case 1:
  335. if(!dadorecebido){
  336. estado = 1;
  337. }
  338. else{
  339. dadorecebido=0;
  340.  
  341. csc_prf_send_data(&palavra[0], tamdado);
  342.  
  343. for(i=0;i<3;i++){
  344. if(strcmp(palavra, ficha[i].user) == 0){
  345. user_atual = ficha[i];
  346.  
  347. str_print = "Usuario valido";
  348. csc_prf_send_data(&str_print[0], strlen(str_print));
  349.  
  350. estado = 2;
  351. break;
  352. }
  353. else{
  354. estado = 0;
  355. }
  356. }
  357. if(estado==0){
  358. str_print = "Usuario invalido";
  359. csc_prf_send_data(&str_print[0], strlen(str_print));
  360. }
  361. }
  362.  
  363. break;
  364.  
  365. //ESPERA SENHA//
  366. case 2:
  367. //printf("Digite uma senha valida:\r\n");
  368. //scanf("%s", senha_digitada);
  369. //printf("Senha= %s\r\n", senha_digitada);
  370.  
  371. str_print = "Entre com uma senha valida";
  372. csc_prf_send_data(&str_print[0], strlen(str_print));
  373.  
  374. estado = 3;
  375.  
  376. break;
  377.  
  378. //VERIFICA SENHA//
  379. case 3:
  380. /*
  381. if(strcmp(senha_digitada, user_atual.password) == 0)
  382. estado = 4;
  383. else
  384. estado = 5;
  385. */
  386.  
  387. if(!dadorecebido){
  388. estado = 3;
  389. }
  390. else{
  391. dadorecebido=0;
  392.  
  393. csc_prf_send_data(&palavra[0], tamdado);
  394.  
  395.  
  396. if(strcmp(palavra, user_atual.password) == 0)
  397. estado = 4;
  398. else
  399. estado = 5;
  400. }
  401.  
  402. break;
  403.  
  404. //SENHA VALIDA//
  405. case 4:
  406. //printf("Acesso Permitido!\r\n");
  407. //printf("Voce pode acessar as portas: %s\r\n", user_atual.portas);
  408.  
  409. str_print = "Acesso Permitido!";
  410. csc_prf_send_data(&str_print[0], strlen(str_print));
  411. str_print = "Voce pode acessar as portas:";
  412. csc_prf_send_data(&str_print[0], strlen(str_print));
  413. csc_prf_send_data(&user_atual.portas[0], strlen(user_atual.portas));
  414.  
  415.  
  416. delay_s(5);
  417.  
  418. erro = 0;
  419.  
  420. estado = 0;
  421.  
  422. break;
  423.  
  424. //SENHA INVALIDA//
  425. case 5:
  426. //printf("Acesso Negado!\r\n");
  427.  
  428. str_print = "Acesso Negado!";
  429. csc_prf_send_data(&str_print[0], strlen(str_print));
  430.  
  431. erro++;
  432.  
  433. if(erro < 3){
  434. delay_s(2);
  435.  
  436. estado = 2;
  437. }
  438. else{
  439. //printf("Muitas tentativas erradas!\r\n");
  440. //printf("Aguarde alguns segundos para tentar novamente!\r\n");
  441.  
  442. str_print = "Muitas tentativas erradas!";
  443. csc_prf_send_data(&str_print[0], strlen(str_print));
  444. str_print = "Aguarde alguns segundos para tentar novamente!";
  445. csc_prf_send_data(&str_print[0], strlen(str_print));
  446.  
  447. delay_s(6);
  448.  
  449. erro = 0;
  450. estado = 0;
  451. }
  452.  
  453. break;
  454.  
  455. //ERRO INESPERADO//
  456. default: //printf("ERRO %d\r\n", estado);
  457. str_print = "Estado invalido!";
  458. csc_prf_send_data(&str_print[0], strlen(str_print));
  459. }
  460. }
  461. return 0;
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement