Skip to content

Commit 58cded6

Browse files
authored
Add files via upload
1 parent 06b055a commit 58cded6

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

Bluetooth/app.c

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/***************************************************************************//**
2+
* @file app.c
3+
* @brief Silicon Labs Empty Example Project
4+
*
5+
* This example demonstrates the bare minimum needed for a Blue Gecko C application
6+
* that allows Over-the-Air Device Firmware Upgrading (OTA DFU). The application
7+
* starts advertising after boot and restarts advertising after a connection is closed.
8+
*******************************************************************************
9+
* # License
10+
* <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
11+
*******************************************************************************
12+
*
13+
* The licensor of this software is Silicon Laboratories Inc. Your use of this
14+
* software is governed by the terms of Silicon Labs Master Software License
15+
* Agreement (MSLA) available at
16+
* www.silabs.com/about-us/legal/master-software-license-agreement. This
17+
* software is distributed to you in Source Code format and is governed by the
18+
* sections of the MSLA applicable to Source Code.
19+
*
20+
******************************************************************************/
21+
22+
/* Bluetooth stack headers */
23+
#include "bg_types.h"
24+
#include "native_gecko.h"
25+
#include "gatt_db.h"
26+
27+
#include "app.h"
28+
29+
/* Print boot message */
30+
static void bootMessage(struct gecko_msg_system_boot_evt_t *bootevt);
31+
32+
/* Flag for indicating DFU Reset must be performed */
33+
static uint8_t boot_to_dfu = 0;
34+
35+
/* Main application */
36+
void appMain(gecko_configuration_t *pconfig)
37+
{
38+
#if DISABLE_SLEEP > 0
39+
pconfig->sleep.flags = 0;
40+
#endif
41+
42+
/* Initialize debug prints. Note: debug prints are off by default. See DEBUG_LEVEL in app.h */
43+
initLog();
44+
45+
/* Initialize stack */
46+
gecko_init(pconfig);
47+
48+
while (1) {
49+
/* Event pointer for handling events */
50+
struct gecko_cmd_packet* evt;
51+
52+
/* if there are no events pending then the next call to gecko_wait_event() may cause
53+
* device go to deep sleep. Make sure that debug prints are flushed before going to sleep */
54+
if (!gecko_event_pending()) {
55+
flushLog();
56+
}
57+
58+
/* Check for stack event. This is a blocking event listener. If you want non-blocking please see UG136. */
59+
evt = gecko_wait_event();
60+
61+
/* Handle events */
62+
switch (BGLIB_MSG_ID(evt->header)) {
63+
/* This boot event is generated when the system boots up after reset.
64+
* Do not call any stack commands before receiving the boot event.
65+
* Here the system is set to start advertising immediately after boot procedure. */
66+
case gecko_evt_system_boot_id:
67+
68+
bootMessage(&(evt->data.evt_system_boot));
69+
printLog("boot event - starting advertising\r\n");
70+
71+
/* Set advertising parameters. 100ms advertisement interval.
72+
* The first parameter is advertising set handle
73+
* The next two parameters are minimum and maximum advertising interval, both in
74+
* units of (milliseconds * 1.6).
75+
* The last two parameters are duration and maxevents left as default. */
76+
gecko_cmd_le_gap_set_advertise_timing(0, 160, 160, 0, 0);
77+
78+
/* Start general advertising and enable connections. */
79+
gecko_cmd_le_gap_start_advertising(0, le_gap_general_discoverable, le_gap_connectable_scannable);
80+
break;
81+
82+
case gecko_evt_le_connection_opened_id:
83+
84+
printLog("connection opened\r\n");
85+
86+
break;
87+
88+
case gecko_evt_le_connection_closed_id:
89+
90+
printLog("connection closed, reason: 0x%2.2x\r\n", evt->data.evt_le_connection_closed.reason);
91+
92+
/* Check if need to boot to OTA DFU mode */
93+
if (boot_to_dfu) {
94+
/* Enter to OTA DFU mode */
95+
gecko_cmd_system_reset(2);
96+
} else {
97+
/* Restart advertising after client has disconnected */
98+
gecko_cmd_le_gap_start_advertising(0, le_gap_general_discoverable, le_gap_connectable_scannable);
99+
}
100+
break;
101+
102+
/* Events related to OTA upgrading
103+
----------------------------------------------------------------------------- */
104+
105+
/* Check if the user-type OTA Control Characteristic was written.
106+
* If ota_control was written, boot the device into Device Firmware Upgrade (DFU) mode. */
107+
case gecko_evt_gatt_server_user_write_request_id:
108+
109+
if (evt->data.evt_gatt_server_user_write_request.characteristic == gattdb_ota_control) {
110+
/* Set flag to enter to OTA mode */
111+
boot_to_dfu = 1;
112+
/* Send response to Write Request */
113+
gecko_cmd_gatt_server_send_user_write_response(
114+
evt->data.evt_gatt_server_user_write_request.connection,
115+
gattdb_ota_control,
116+
bg_err_success);
117+
118+
/* Close connection to enter to DFU OTA mode */
119+
gecko_cmd_le_connection_close(evt->data.evt_gatt_server_user_write_request.connection);
120+
}
121+
break;
122+
123+
/* Add additional event handlers as your application requires */
124+
125+
default:
126+
break;
127+
}
128+
}
129+
}
130+
131+
/* Print stack version and local Bluetooth address as boot message */
132+
static void bootMessage(struct gecko_msg_system_boot_evt_t *bootevt)
133+
{
134+
#if DEBUG_LEVEL
135+
bd_addr local_addr;
136+
int i;
137+
138+
printLog("stack version: %u.%u.%u\r\n", bootevt->major, bootevt->minor, bootevt->patch);
139+
local_addr = gecko_cmd_system_get_bt_address()->address;
140+
141+
printLog("local BT device address: ");
142+
for (i = 0; i < 5; i++) {
143+
printLog("%2.2x:", local_addr.addr[5 - i]);
144+
}
145+
printLog("%2.2x\r\n", local_addr.addr[0]);
146+
#endif
147+
}

Bluetooth/app.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************//**
2+
* @brief app.h
3+
*******************************************************************************
4+
* # License
5+
* <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
6+
*******************************************************************************
7+
*
8+
* The licensor of this software is Silicon Laboratories Inc. Your use of this
9+
* software is governed by the terms of Silicon Labs Master Software License
10+
* Agreement (MSLA) available at
11+
* www.silabs.com/about-us/legal/master-software-license-agreement. This
12+
* software is distributed to you in Source Code format and is governed by the
13+
* sections of the MSLA applicable to Source Code.
14+
*
15+
******************************************************************************/
16+
17+
#ifndef APP_H_
18+
#define APP_H_
19+
20+
#include "gecko_configuration.h"
21+
22+
/* DEBUG_LEVEL is used to enable/disable debug prints. Set DEBUG_LEVEL to 1 to enable debug prints */
23+
#define DEBUG_LEVEL 0
24+
25+
/* Set this value to 1 if you want to disable deep sleep completely */
26+
#define DISABLE_SLEEP 0
27+
28+
#if DEBUG_LEVEL
29+
#include "retargetserial.h"
30+
#include <stdio.h>
31+
#endif
32+
33+
#if DEBUG_LEVEL
34+
#define initLog() RETARGET_SerialInit()
35+
#define flushLog() RETARGET_SerialFlush()
36+
#define printLog(...) printf(__VA_ARGS__)
37+
#else
38+
#define initLog()
39+
#define flushLog()
40+
#define printLog(...)
41+
#endif
42+
43+
/* Main application */
44+
void appMain(gecko_configuration_t *pconfig);
45+
46+
#endif

0 commit comments

Comments
 (0)