-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_usb.c
116 lines (95 loc) · 3.21 KB
/
main_usb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* main for Bluetooth LE apps.
* For use with USB HCI dongles.
*
* Henric Lindén, rt-labs AB
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "btstack_config.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.h"
#include "btstack_run_loop_posix.h"
#include "hci.h"
#include "hci_dump.h"
#include "btstack_stdin.h"
int btstack_main(int argc, const char* argv[]);
static btstack_packet_callback_registration_t hci_event_callback_registration;
// A HCI event handler informing about the current state. Not needed
// for normal operation.
static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size)
{
UNUSED(channel);
UNUSED(size);
if (packet_type == HCI_EVENT_PACKET) {
switch (hci_event_packet_get_type(packet)) {
case BTSTACK_EVENT_STATE:
switch (btstack_event_state_get_state(packet)) {
case HCI_STATE_OFF:
fprintf(stderr, "[HCI_STATE_OFF]\n");
exit(0);
break;
case HCI_STATE_INITIALIZING:
fprintf(stderr, "[HCI_STATE_INITIALIZING]\n");
break;
case HCI_STATE_WORKING:
fprintf(stderr, "[HCI_STATE_WORKING]\n");
break;
case HCI_STATE_HALTING:
fprintf(stderr, "[HCI_STATE_HALTING]\n");
break;
case HCI_STATE_SLEEPING:
fprintf(stderr, "[HCI_STATE_SLEEPING]\n");
break;
case HCI_STATE_FALLING_ASLEEP:
fprintf(stderr, "[HCI_STATE_FALLING_ASLEEP]\n");
break;
default:
break;
}
break;
default:
break;
}
}
}
static void sigint_handler(int param)
{
UNUSED(param);
fprintf(stderr, "CTRL-C - SIGINT received, shutting down.\n");
log_info("sigint_handler: shutting down");
// Reset anyway
btstack_stdin_reset();
// Power down
hci_power_control(HCI_POWER_OFF);
hci_close();
exit(0);
}
#define USB_MAX_PATH_LEN 7
int main(int argc, const char* argv[])
{
// Do necessary stack initialisations.
btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_posix_get_instance());
// Redirect log to packetlogger for use in Wireshark.
hci_dump_open("hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
log_info("Program name: %s", argv[0]);
// Init HCI. This will use the first available Bluetooth dongle.
hci_init(hci_transport_usb_instance(), NULL);
// Add a hci event handler just for showing btstatck state to user.
hci_event_callback_registration.callback = &hci_packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
// Handle CTRL-C
signal(SIGINT, sigint_handler);
// Setup app
btstack_main(argc, argv);
// Execute runloop
btstack_run_loop_execute();
// Shouldn't end up here...
return 0;
}