Skip to content

Commit 6935b62

Browse files
committed
Ajout, Mise à Jour et correction
1 parent 8820fe6 commit 6935b62

File tree

195 files changed

+9108
-2894
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

195 files changed

+9108
-2894
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
App(
2+
appid="example_ble_beacon",
3+
name="Example: BLE Beacon",
4+
apptype=FlipperAppType.EXTERNAL,
5+
entry_point="ble_beacon_app",
6+
requires=["gui"],
7+
stack_size=1 * 1024,
8+
fap_icon="example_ble_beacon_10px.png",
9+
fap_category="Examples",
10+
fap_icon_assets="images",
11+
)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include "ble_beacon_app.h"
2+
3+
#include <extra_beacon.h>
4+
#include <furi_hal_version.h>
5+
6+
#include <string.h>
7+
8+
#define TAG "ble_beacon_app"
9+
10+
static bool ble_beacon_app_custom_event_callback(void* context, uint32_t event) {
11+
furi_assert(context);
12+
BleBeaconApp* app = context;
13+
return scene_manager_handle_custom_event(app->scene_manager, event);
14+
}
15+
16+
static bool ble_beacon_app_back_event_callback(void* context) {
17+
furi_assert(context);
18+
BleBeaconApp* app = context;
19+
return scene_manager_handle_back_event(app->scene_manager);
20+
}
21+
22+
static void ble_beacon_app_tick_event_callback(void* context) {
23+
furi_assert(context);
24+
BleBeaconApp* app = context;
25+
scene_manager_handle_tick_event(app->scene_manager);
26+
}
27+
28+
static void ble_beacon_app_restore_beacon_state(BleBeaconApp* app) {
29+
// Restore beacon data from service
30+
GapExtraBeaconConfig* local_config = &app->beacon_config;
31+
const GapExtraBeaconConfig* config = furi_hal_bt_extra_beacon_get_config();
32+
if(config) {
33+
// We have a config, copy it
34+
memcpy(local_config, config, sizeof(app->beacon_config));
35+
} else {
36+
// No config, set up default values - they will stay until overriden or device is reset
37+
local_config->min_adv_interval_ms = 50;
38+
local_config->max_adv_interval_ms = 150;
39+
40+
local_config->adv_channel_map = GapAdvChannelMapAll;
41+
local_config->adv_power_level = GapAdvPowerLevel_0dBm;
42+
43+
local_config->address_type = GapAddressTypePublic;
44+
memcpy(
45+
local_config->address, furi_hal_version_get_ble_mac(), sizeof(local_config->address));
46+
// Modify MAC address to make it different from the one used by the main app
47+
local_config->address[0] ^= 0xFF;
48+
local_config->address[3] ^= 0xFF;
49+
50+
furi_check(furi_hal_bt_extra_beacon_set_config(local_config));
51+
}
52+
53+
// Get beacon state
54+
app->is_beacon_active = furi_hal_bt_extra_beacon_is_active();
55+
56+
// Restore last beacon data
57+
app->beacon_data_len = furi_hal_bt_extra_beacon_get_data(app->beacon_data);
58+
}
59+
60+
static BleBeaconApp* ble_beacon_app_alloc() {
61+
BleBeaconApp* app = malloc(sizeof(BleBeaconApp));
62+
63+
app->gui = furi_record_open(RECORD_GUI);
64+
65+
app->scene_manager = scene_manager_alloc(&ble_beacon_app_scene_handlers, app);
66+
app->view_dispatcher = view_dispatcher_alloc();
67+
68+
app->status_string = furi_string_alloc();
69+
70+
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
71+
view_dispatcher_set_custom_event_callback(
72+
app->view_dispatcher, ble_beacon_app_custom_event_callback);
73+
view_dispatcher_set_navigation_event_callback(
74+
app->view_dispatcher, ble_beacon_app_back_event_callback);
75+
view_dispatcher_set_tick_event_callback(
76+
app->view_dispatcher, ble_beacon_app_tick_event_callback, 100);
77+
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
78+
view_dispatcher_enable_queue(app->view_dispatcher);
79+
80+
app->submenu = submenu_alloc();
81+
view_dispatcher_add_view(
82+
app->view_dispatcher, BleBeaconAppViewSubmenu, submenu_get_view(app->submenu));
83+
84+
app->dialog_ex = dialog_ex_alloc();
85+
view_dispatcher_add_view(
86+
app->view_dispatcher, BleBeaconAppViewDialog, dialog_ex_get_view(app->dialog_ex));
87+
88+
app->byte_input = byte_input_alloc();
89+
view_dispatcher_add_view(
90+
app->view_dispatcher, BleBeaconAppViewByteInput, byte_input_get_view(app->byte_input));
91+
92+
ble_beacon_app_restore_beacon_state(app);
93+
94+
return app;
95+
}
96+
97+
static void ble_beacon_app_free(BleBeaconApp* app) {
98+
view_dispatcher_remove_view(app->view_dispatcher, BleBeaconAppViewByteInput);
99+
view_dispatcher_remove_view(app->view_dispatcher, BleBeaconAppViewSubmenu);
100+
view_dispatcher_remove_view(app->view_dispatcher, BleBeaconAppViewDialog);
101+
102+
free(app->byte_input);
103+
free(app->submenu);
104+
free(app->dialog_ex);
105+
106+
free(app->scene_manager);
107+
free(app->view_dispatcher);
108+
109+
free(app->status_string);
110+
111+
furi_record_close(RECORD_NOTIFICATION);
112+
furi_record_close(RECORD_GUI);
113+
app->gui = NULL;
114+
115+
free(app);
116+
}
117+
118+
int32_t ble_beacon_app(void* args) {
119+
UNUSED(args);
120+
121+
BleBeaconApp* app = ble_beacon_app_alloc();
122+
123+
scene_manager_next_scene(app->scene_manager, BleBeaconAppSceneRunBeacon);
124+
125+
view_dispatcher_run(app->view_dispatcher);
126+
127+
ble_beacon_app_free(app);
128+
return 0;
129+
}
130+
131+
void ble_beacon_app_update_state(BleBeaconApp* app) {
132+
furi_hal_bt_extra_beacon_stop();
133+
134+
furi_check(furi_hal_bt_extra_beacon_set_config(&app->beacon_config));
135+
136+
app->beacon_data_len = 0;
137+
while((app->beacon_data[app->beacon_data_len] != 0) &&
138+
(app->beacon_data_len < sizeof(app->beacon_data))) {
139+
app->beacon_data_len++;
140+
}
141+
142+
FURI_LOG_I(TAG, "beacon_data_len: %d", app->beacon_data_len);
143+
144+
furi_check(furi_hal_bt_extra_beacon_set_data(app->beacon_data, app->beacon_data_len));
145+
146+
if(app->is_beacon_active) {
147+
furi_check(furi_hal_bt_extra_beacon_start());
148+
}
149+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include "extra_beacon.h"
4+
#include <furi.h>
5+
#include <gui/gui.h>
6+
#include <gui/view.h>
7+
#include <gui/scene_manager.h>
8+
#include <gui/view_dispatcher.h>
9+
10+
#include <gui/modules/widget.h>
11+
#include <gui/modules/submenu.h>
12+
#include <gui/modules/byte_input.h>
13+
#include <gui/modules/dialog_ex.h>
14+
15+
#include <rpc/rpc_app.h>
16+
#include <notification/notification_messages.h>
17+
18+
#include <furi_hal_bt.h>
19+
20+
#include "scenes/scenes.h"
21+
#include <stdint.h>
22+
23+
typedef struct {
24+
Gui* gui;
25+
SceneManager* scene_manager;
26+
ViewDispatcher* view_dispatcher;
27+
28+
Submenu* submenu;
29+
ByteInput* byte_input;
30+
DialogEx* dialog_ex;
31+
32+
FuriString* status_string;
33+
34+
GapExtraBeaconConfig beacon_config;
35+
uint8_t beacon_data[EXTRA_BEACON_MAX_DATA_SIZE];
36+
uint8_t beacon_data_len;
37+
bool is_beacon_active;
38+
} BleBeaconApp;
39+
40+
typedef enum {
41+
BleBeaconAppViewSubmenu,
42+
BleBeaconAppViewByteInput,
43+
BleBeaconAppViewDialog,
44+
} BleBeaconAppView;
45+
46+
typedef enum {
47+
BleBeaconAppCustomEventDataEditResult = 100,
48+
} BleBeaconAppCustomEvent;
49+
50+
void ble_beacon_app_update_state(BleBeaconApp* app);
Loading
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ADD_SCENE(ble_beacon_app, menu, Menu)
2+
ADD_SCENE(ble_beacon_app, input_mac_addr, InputMacAddress)
3+
ADD_SCENE(ble_beacon_app, input_beacon_data, InputBeaconData)
4+
ADD_SCENE(ble_beacon_app, run_beacon, RunBeacon)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "../ble_beacon_app.h"
2+
3+
static void ble_beacon_app_scene_add_type_byte_input_callback(void* context) {
4+
BleBeaconApp* ble_beacon = context;
5+
view_dispatcher_send_custom_event(
6+
ble_beacon->view_dispatcher, BleBeaconAppCustomEventDataEditResult);
7+
}
8+
9+
void ble_beacon_app_scene_input_beacon_data_on_enter(void* context) {
10+
BleBeaconApp* ble_beacon = context;
11+
byte_input_set_header_text(ble_beacon->byte_input, "Enter beacon data");
12+
13+
byte_input_set_result_callback(
14+
ble_beacon->byte_input,
15+
ble_beacon_app_scene_add_type_byte_input_callback,
16+
NULL,
17+
context,
18+
ble_beacon->beacon_data,
19+
sizeof(ble_beacon->beacon_data));
20+
21+
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewByteInput);
22+
}
23+
24+
bool ble_beacon_app_scene_input_beacon_data_on_event(void* context, SceneManagerEvent event) {
25+
BleBeaconApp* ble_beacon = context;
26+
SceneManager* scene_manager = ble_beacon->scene_manager;
27+
28+
if(event.type == SceneManagerEventTypeCustom) {
29+
if(event.event == BleBeaconAppCustomEventDataEditResult) {
30+
ble_beacon_app_update_state(ble_beacon);
31+
scene_manager_previous_scene(scene_manager);
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
39+
void ble_beacon_app_scene_input_beacon_data_on_exit(void* context) {
40+
BleBeaconApp* ble_beacon = context;
41+
42+
byte_input_set_result_callback(ble_beacon->byte_input, NULL, NULL, NULL, NULL, 0);
43+
byte_input_set_header_text(ble_beacon->byte_input, NULL);
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "../ble_beacon_app.h"
2+
3+
static void ble_beacon_app_scene_add_type_byte_input_callback(void* context) {
4+
BleBeaconApp* ble_beacon = context;
5+
view_dispatcher_send_custom_event(
6+
ble_beacon->view_dispatcher, BleBeaconAppCustomEventDataEditResult);
7+
}
8+
9+
void ble_beacon_app_scene_input_mac_addr_on_enter(void* context) {
10+
BleBeaconApp* ble_beacon = context;
11+
byte_input_set_header_text(ble_beacon->byte_input, "Enter MAC (reversed)");
12+
13+
byte_input_set_result_callback(
14+
ble_beacon->byte_input,
15+
ble_beacon_app_scene_add_type_byte_input_callback,
16+
NULL,
17+
context,
18+
ble_beacon->beacon_config.address,
19+
sizeof(ble_beacon->beacon_config.address));
20+
21+
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewByteInput);
22+
}
23+
24+
bool ble_beacon_app_scene_input_mac_addr_on_event(void* context, SceneManagerEvent event) {
25+
BleBeaconApp* ble_beacon = context;
26+
SceneManager* scene_manager = ble_beacon->scene_manager;
27+
28+
if(event.type == SceneManagerEventTypeCustom) {
29+
if(event.event == BleBeaconAppCustomEventDataEditResult) {
30+
ble_beacon_app_update_state(ble_beacon);
31+
scene_manager_previous_scene(scene_manager);
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
39+
void ble_beacon_app_scene_input_mac_addr_on_exit(void* context) {
40+
BleBeaconApp* ble_beacon = context;
41+
42+
byte_input_set_result_callback(ble_beacon->byte_input, NULL, NULL, NULL, NULL, 0);
43+
byte_input_set_header_text(ble_beacon->byte_input, NULL);
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "../ble_beacon_app.h"
2+
3+
enum SubmenuIndex {
4+
SubmenuIndexSetMac,
5+
SubmenuIndexSetData,
6+
};
7+
8+
static void ble_beacon_app_scene_menu_submenu_callback(void* context, uint32_t index) {
9+
BleBeaconApp* ble_beacon = context;
10+
view_dispatcher_send_custom_event(ble_beacon->view_dispatcher, index);
11+
}
12+
13+
void ble_beacon_app_scene_menu_on_enter(void* context) {
14+
BleBeaconApp* ble_beacon = context;
15+
Submenu* submenu = ble_beacon->submenu;
16+
17+
submenu_add_item(
18+
submenu,
19+
"Set MAC",
20+
SubmenuIndexSetMac,
21+
ble_beacon_app_scene_menu_submenu_callback,
22+
ble_beacon);
23+
submenu_add_item(
24+
submenu,
25+
"Set Data",
26+
SubmenuIndexSetData,
27+
ble_beacon_app_scene_menu_submenu_callback,
28+
ble_beacon);
29+
30+
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewSubmenu);
31+
}
32+
33+
bool ble_beacon_app_scene_menu_on_event(void* context, SceneManagerEvent event) {
34+
BleBeaconApp* ble_beacon = context;
35+
SceneManager* scene_manager = ble_beacon->scene_manager;
36+
37+
bool consumed = false;
38+
39+
if(event.type == SceneManagerEventTypeCustom) {
40+
const uint32_t submenu_index = event.event;
41+
if(submenu_index == SubmenuIndexSetMac) {
42+
scene_manager_next_scene(scene_manager, BleBeaconAppSceneInputMacAddress);
43+
consumed = true;
44+
} else if(submenu_index == SubmenuIndexSetData) {
45+
scene_manager_next_scene(scene_manager, BleBeaconAppSceneInputBeaconData);
46+
consumed = true;
47+
}
48+
}
49+
50+
return consumed;
51+
}
52+
53+
void ble_beacon_app_scene_menu_on_exit(void* context) {
54+
BleBeaconApp* ble_beacon = context;
55+
submenu_reset(ble_beacon->submenu);
56+
}

0 commit comments

Comments
 (0)