Appiko
main.c
1 /*
2  * main.c : Application for system level testing for SensePi devices
3  * Copyright (C) 2019 Appiko
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stddef.h>
32 #include <string.h>
33 
34 #include "nrf.h"
35 #include "boards.h"
36 #include "nrf_util.h"
37 
38 #include "hal_gpio.h"
39 #include "hal_uart.h"
40 #include "hal_nop_delay.h"
41 #include "hal_clocks.h"
42 
43 #include "log.h"
44 #include "ms_timer.h"
45 #include "profiler_timer.h"
46 
47 #include "nrf_nvic.h"
48 #include "ble.h"
49 #include "ble_gap.h"
50 #include "nrf_sdm.h"
51 #include "app_error.h"
52 #include "sd_evt_handler.h"
53 
54 #include "stdint.h"
55 
58 #define TRIGGER 2
59 
60 #define FOCUS 3
61 
63 #define PRODUCT_ID_LEN 16
64 
70 static void soc_evt_handler(uint32_t evt_id);
71 
77 void ble_stack_init(void);
78 
85 static void ble_evt_handler(ble_evt_t * evt);
86 
91 void conn_to_sense_pi(void);
92 
96 void extract_product_id(void);
97 
105 uint32_t if_pulse(uint32_t pin_trigger, uint32_t pin_focus);
106 
110 void scan_start();
111 
115 void check_pir(void);
116 
121 void recv_data(uint8_t * buffer);
122 
124 static uint32_t pir_flag = 0;
126 volatile uint32_t conn_terminate_flag = 0;
128 static uint8_t sense_pi_mac_addr[] = {0,0,0,0,0,0};
130 static ble_gap_addr_t my_peer_addr;
132 static ble_gap_evt_adv_report_t my_adv_report;
134 uint8_t scan_resp[BLE_GAP_SCAN_BUFFER_MAX] = {};
136 uint8_t scan_data_buffer[BLE_GAP_SCAN_BUFFER_MAX];
137 
141  (BLE_GAP_SCAN_BUFFER_MAX),
142 };
144 static ble_gap_scan_params_t const sys_lvl_app_scan_params =
145 {
146  .active = 1,
147  .interval = (BLE_GAP_SCAN_INTERVAL_MAX),
148  .window = (BLE_GAP_SCAN_WINDOW_MAX),
149  .extended = 0,
150  .report_incomplete_evts = 0,
151  .channel_mask = {0,0,0,0,0},
152  .timeout = BLE_GAP_SCAN_TIMEOUT_UNLIMITED,
153  .scan_phys = BLE_GAP_PHY_AUTO,
154  .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
155 };
157 static ble_gap_conn_params_t const sys_lvl_conn_params =
158 {
159  .min_conn_interval = BLE_GAP_CP_MIN_CONN_INTVL_MIN,
160  .max_conn_interval = BLE_GAP_CP_MAX_CONN_INTVL_MIN,
161  .slave_latency = BLE_GAP_CP_SLAVE_LATENCY_MAX,
162  .conn_sup_timeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MAX,
163 };
164 
165 
167 int main(void)
168 {
171  log_printf("\n SYSTEM LEVEL TEST\n");
172 // scan_start();
173  while (true)
174  {
175  if(conn_terminate_flag == 1)
176  {
177  uint32_t err_code;
178  err_code = sd_softdevice_disable();
179  APP_ERROR_CHECK(err_code);
180  if(err_code == 0)
181  {
182  log_printf("BLE_Status : 1\n");
183  }
184  else
185  {
186  log_printf("BLE_Status : 0\n");
187  }
188  for(uint32_t addr_byte = BLE_GAP_ADDR_LEN; addr_byte > 0; addr_byte--)
189  {
190  sense_pi_mac_addr[(addr_byte - 1)] = 0;
191  }
192  conn_terminate_flag = 0;
193  }
194  }
195 }
196 
198 static void ble_evt_handler(ble_evt_t * evt)
199 {
200  switch(evt->header.evt_id)
201  {
202  case BLE_GAP_EVT_CONNECTED:
203  {
204  uint32_t err_code;
205  err_code = sd_ble_gap_disconnect(evt->evt.gap_evt.conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
206  APP_ERROR_CHECK(err_code);
207  break;
208  }
209  case BLE_GAP_EVT_DISCONNECTED:
210  {
212  break;
213  }
214  case BLE_GAP_EVT_CONN_PARAM_UPDATE:
215 
216  break;
217  case BLE_GAP_EVT_ADV_SET_TERMINATED:
218 
219  break;
220  case BLE_GATTS_EVT_WRITE:
221 
222  break;
223  case BLE_GAP_EVT_ADV_REPORT:
224  {
225  my_adv_report = (evt->evt.gap_evt.params.adv_report);
226 // log_printf("SCANNED\n");
227  uint32_t mac_flag = 1;
228  for(uint32_t addr_byte = BLE_GAP_ADDR_LEN; addr_byte > 0; addr_byte--)
229  {
230  if(evt->evt.gap_evt.params.adv_report.peer_addr.addr[(addr_byte - 1)] != sense_pi_mac_addr[(addr_byte - 1)])
231  {
232  mac_flag = 0;
233  }
234  }
235  if(mac_flag)
236  {
237  my_peer_addr = evt->evt.gap_evt.params.adv_report.peer_addr;
238  if(my_adv_report.type.scan_response == 1 && my_adv_report.rssi >= (-65))
239  {
240  uint32_t err_code;
241  err_code = sd_ble_gap_connect(&my_peer_addr, &sys_lvl_app_scan_params, &sys_lvl_conn_params, BLE_CONN_CFG_TAG_DEFAULT);
242  APP_ERROR_CHECK(err_code);
243  memcpy(scan_resp, my_adv_report.data.p_data, my_adv_report.data.len);
245  if(err_code != 0)
246  {
247  }
248  }
249  else
250  {
252  }
253  }
254  else
255  {
257  }
258  break;
259  }
260  case BLE_GAP_EVT_TIMEOUT:
261  {
262 
263 
264  break;
265  }
266 
267  }
268  return;
269 }
270 
272 static void soc_evt_handler(uint32_t evt_id)
273 {
274  log_printf("soc evt %x\n", evt_id);
275 }
276 
278 void ble_stack_init(void)
279 {
280 // log_printf("BLE_INIT\n");
281  uint32_t err_code;
282  nrf_clock_lf_cfg_t cfg = BOARD_LFCLKSRC_STRUCT;
283 // log_printf("NRF_CLOCKS_ASSIGNMENT\n");
284  err_code = sd_softdevice_enable(&cfg, app_error_fault_handler);
285 // log_printf("Err : %d", err_code);
286  APP_ERROR_CHECK(err_code);
287  uint32_t app_ram_start = 0x200032C8;
288 // log_printf("Init %x", app_ram_start);
289  err_code = sd_ble_enable(&app_ram_start);
290 // log_printf(" RAM needed %x\n", app_ram_start);
291  APP_ERROR_CHECK(err_code);
292 
293  sd_evt_handler_init(ble_evt_handler, soc_evt_handler);
294 
295 }
296 
299 {
300 // log_printf("SCAN_START\n");
301  ble_stack_init();
302  uint32_t err_code;
303  err_code = sd_ble_gap_scan_start(&sys_lvl_app_scan_params, &sys_lvl_app_scan_buffer);
304  APP_ERROR_CHECK(err_code);
305  if (!err_code)
306  {
307  log_printf("START\n");
308  }
309 }
310 
313 {
314  static uint8_t data_to_comp[23] = {};
315  static const uint8_t sense_pi_data[23] = {0x53, 0x65, 0x6e, 0x73, 0x65, 0x50, 0x69, 0x0a, 0xde, 0xfb, 0x07, 0x74, 0x83, 0x66, 0xb0, 0x0d, 0x48, 0xf5, 0x07, 0x50, 0xdc, 0x73, 0x3c};
316  uint32_t cpy_arr_cnt = 0;
317  uint32_t pst_arr_cnt = 0;
318  uint32_t arr_cnt = 0;
319  uint32_t comp_flag = 1;
320  uint8_t * mac_addr_value = my_adv_report.peer_addr.addr;
321  for(cpy_arr_cnt = 5, pst_arr_cnt = 0; cpy_arr_cnt < 12; cpy_arr_cnt++, pst_arr_cnt++)
322  {
323  data_to_comp[pst_arr_cnt] = my_adv_report.data.p_data[cpy_arr_cnt];
324  }
325  for(cpy_arr_cnt = 14, pst_arr_cnt = 7; cpy_arr_cnt < 30; cpy_arr_cnt++, pst_arr_cnt++)
326  {
327  data_to_comp[pst_arr_cnt] = my_adv_report.data.p_data[cpy_arr_cnt];
328  }
329  for(arr_cnt = 0; arr_cnt < 23; arr_cnt++)
330  {
331  if(data_to_comp[arr_cnt] != sense_pi_data[arr_cnt])
332  {
333  comp_flag = 0;
334  }
335  }
336  if(comp_flag)
337  {
338  for(uint32_t addr_byte = BLE_GAP_ADDR_LEN; addr_byte > 0; addr_byte--)
339  {
340  sense_pi_mac_addr[(addr_byte - 1)] = mac_addr_value[(addr_byte - 1)];
341  }
342 #if 0
343  log_printf("MAC :\n");
344  for(uint32_t addr_byte = BLE_GAP_ADDR_LEN; addr_byte > 0; addr_byte--)
345  {
346  log_printf("%02x ",my_adv_report.peer_addr.addr[(addr_byte - 1)]);
347  if(addr_byte > 1)
348  {
349  log_printf(":");
350  }
351  }
352  log_printf("\n");
353  log_printf("RSSI : %d\n", (int32_t)my_adv_report.rssi);
354  log_printf("SCAN_RESPONSE : %d\n", my_adv_report.type.scan_response);
355 #endif
356  }
357  uint32_t err_code;
358  err_code = sd_ble_gap_scan_start( NULL , &sys_lvl_app_scan_buffer);
359  APP_ERROR_CHECK(err_code);
360 }
361 
364 {
365  uint8_t product_id[PRODUCT_ID_LEN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
366  memcpy(product_id,&scan_resp[5],PRODUCT_ID_LEN);
367  log_printf("Product_ID : ");
368  for(uint32_t char_cnt = 0; char_cnt < PRODUCT_ID_LEN; char_cnt++)
369  {
370  log_printf("%c", product_id[char_cnt]);
371  }
372  log_printf("\n");
373 }
374 
376 uint32_t if_pulse(uint32_t pin_trigger, uint32_t pin_focus)
377 {
378  uint32_t toggle_flag = 0;
379  uint32_t trigger_flag = 0;
380  uint32_t focus_flag = 0;
381  uint32_t prev_state_trigger = 0;
382  uint32_t prev_state_focus = 0;
383  uint32_t current_state_trigger = 0;
384  uint32_t current_state_focus = 0;
385  uint32_t focus_cnt = 0;
386  uint32_t trigger_cnt = 0;
387  prev_state_trigger = hal_gpio_pin_read(pin_trigger);
388  prev_state_focus = hal_gpio_pin_read(pin_focus);
389  current_state_trigger = hal_gpio_pin_read(pin_trigger);
390  current_state_focus = hal_gpio_pin_read(pin_focus);
393  while(read_time_us() < 16000000)
394  {
395  if(current_state_trigger != prev_state_trigger)
396  {
397  prev_state_trigger = current_state_trigger;
398  trigger_cnt++;
399  }
400  else
401  {
402  current_state_trigger = hal_gpio_pin_read(TRIGGER);
403  }
404  if(current_state_focus != prev_state_focus)
405  {
406  prev_state_focus = current_state_focus;
407  focus_cnt++;
408  }
409  else
410  {
411  current_state_focus = hal_gpio_pin_read(FOCUS);
412  }
413  }
414  if(trigger_cnt >= 2)
415  {
416  trigger_flag = 1;
417  }
418  if(focus_cnt >= 2)
419  {
420  focus_flag = 1;
421  }
422  if((trigger_flag && focus_flag) == 1)
423  {
424  toggle_flag = 1;
425  }
426  else
427  {
428  toggle_flag = 0;
429  }
431  return toggle_flag;
432 }
433 
435 void check_pir(void)
436 {
437  hal_gpio_cfg_input(TRIGGER, HAL_GPIO_PULL_DISABLED);
438  hal_gpio_cfg_input(FOCUS, HAL_GPIO_PULL_DISABLED);
439  pir_flag = if_pulse(TRIGGER, FOCUS);
440  if(pir_flag == 1)
441  {
442  log_printf("PIR_Status : 1\n");
443  pir_flag = 0;
444  }
445  else
446  {
447  log_printf("PIR_Status : 0\n");
448  }
449  log_printf("END\n");
450 }
451 
453 void recv_data(uint8_t * buffer)
454 {
455  char temp_str[] = {};
456  char * start_pir = "PIR\n";
457  char * start_scan = "SCN\n";
458  memcpy(temp_str, buffer, sizeof(start_pir));
459  if(memcmp(temp_str, start_pir,(sizeof(start_pir)) - 1) == 0)
460  {
461  check_pir();
462  }
463  else if(memcmp(temp_str, start_scan,(sizeof(start_scan) - 1)) == 0)
464  {
465  scan_start();
466  }
467  else
468  {
469  }
470  return;
471 }
void profiler_timer_init(void)
Uart baud rate of 1M.
Definition: hal_uart.h:68
void ble_stack_init(void)
Function for initializing the BLE stack by enabling the SoftDevice and the BLE event interrupt.
Definition: main.c:278
#define BOARD_LFCLKSRC_STRUCT
Definition: bluey_1v1.h:122
void recv_data(uint8_t *buffer)
IRQHandler for UART RX_DONE event.
Definition: main.c:453
void lfclk_init(lfclk_src_t lfclk_src)
Function to initialize the LF clock.
Definition: hal_clocks.c:23
void check_pir(void)
Function to check if PIR is present or not.
Definition: main.c:435
void extract_product_id(void)
Function to extract Product ID from Scan response data.
Definition: main.c:363
void profiler_timer_deinit()
Fully stop the profiling timer to save power. profiler_timer_init needs to be called again before usi...
#define APP_ERROR_CHECK(ERR_CODE)
Macro calls error handler if the provided error code is not NRF_SUCCESS. The behavior of this call de...
Definition: app_error.h:55
void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
Callback to be invoked in case of fault i.e. unrecoverable errors occurring within the application or...
Definition: app_error.c:55
void scan_start()
Function to start scanning.
Definition: main.c:298
void hal_uart_init(hal_uart_baud_t baud, void(*handler)(uint8_t *ptr))
Definition: hal_uart.c:145
uint32_t if_pulse(uint32_t pin_trigger, uint32_t pin_focus)
Function to detect pulses from trigger pin and focus pin.
Definition: main.c:376
volatile uint32_t conn_terminate_flag
Definition: main.c:126
void conn_to_sense_pi(void)
Function to generate table of scanned devices with MAC addresses.
Definition: main.c:312
#define TRIGGER
Definition: main.c:58
HAL_GPIO_PULL_DISABLED.
Definition: hal_gpio.h:37
ble_data_t sys_lvl_app_scan_buffer
Definition: main.c:139
int main(void)
Function for application main entry.
Definition: main.c:90
#define FOCUS
Definition: main.c:60
uint8_t scan_resp[BLE_GAP_SCAN_BUFFER_MAX]
Definition: main.c:134
#define PRODUCT_ID_LEN
Definition: main.c:63
External crystal.
Definition: hal_clocks.h:38
uint8_t scan_data_buffer[BLE_GAP_SCAN_BUFFER_MAX]
Definition: main.c:136
uint32_t read_time_us(void)