Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#752 SD Card Streaming #933

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 48 additions & 22 deletions Friend/firmware/firmware_v1.0/src/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ LOG_MODULE_REGISTER(storage, CONFIG_LOG_DEFAULT_LEVEL);
#define READ_COMMAND 0
#define DELETE_COMMAND 1
#define NUKE 2
#define STOP_COMMAND 3

#define INVALID_FILE_SIZE 3
#define ZERO_FILE_SIZE 4
Expand All @@ -34,7 +35,7 @@ static struct bt_uuid_128 storage_write_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCO
static struct bt_uuid_128 storage_read_uuid = BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x30295782, 0x4301, 0xEABD, 0x2904, 0x2849ADFEAE43));
static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset);

K_THREAD_STACK_DEFINE(storage_stack, 2048);
K_THREAD_STACK_DEFINE(storage_stack, 4096);
static struct k_thread storage_thread;

extern uint8_t file_count;
Expand All @@ -50,12 +51,13 @@ static struct bt_gatt_attr storage_service_attr[] = {

};

static struct bt_gatt_service storage_service = BT_GATT_SERVICE(storage_service_attr);
struct bt_gatt_service storage_service = BT_GATT_SERVICE(storage_service_attr);

bool storage_is_on = false;


static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value) {
static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint16_t value)
{

storage_is_on = true;
if (value == BT_GATT_CCC_NOTIFY)
Expand All @@ -73,15 +75,14 @@ static void storage_config_changed_handler(const struct bt_gatt_attr *attr, uint

}

static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) {
static ssize_t storage_read_characteristic(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset)
{
k_msleep(10);
// char amount[1] = {file_count};
uint32_t amount[50] = {0};
for (int i = 0; i < file_count; i++) {
amount[i] = file_num_array[i];
}

ssize_t result = bt_gatt_attr_read(conn, attr, buf, len, offset, amount, file_count * sizeof(uint32_t));
ssize_t result = bt_gatt_attr_read(conn, attr, buf, len, offset, amount, 1 * sizeof(uint32_t));
return result;
}

Expand All @@ -96,7 +97,7 @@ static uint32_t offset = 0;
static uint8_t index = 0;
static uint8_t current_packet_size = 0;
static uint8_t tx_buffer_size = 0;

static uint8_t stop_started = 0;
static uint8_t delete_started = 0;
static uint8_t current_read_num = 1;
uint32_t remaining_length = 0;
Expand Down Expand Up @@ -190,6 +191,11 @@ static uint8_t parse_storage_command(void *buf,uint16_t len) {
else if (command == NUKE) {
nuke_started = 1;
}
else if (command == STOP_COMMAND)
{
remaining_length = 80;
stop_started = 1;
}
else {
LOG_INF("invalid command \n");
return 6;
Expand Down Expand Up @@ -245,6 +251,7 @@ static void write_to_gatt(struct bt_conn *conn) {

void storage_write(void) {
while (1) {
struct bt_conn *conn = get_current_connection();

if ( transport_started ) {
LOG_INF("transpor started in side : %d",transport_started);
Expand All @@ -259,9 +266,13 @@ void storage_write(void) {
if (err) {
printk("error clearing\n");
}
else{
uint8_t result_buffer[1] = {100};
bt_gatt_notify(get_current_connection(), &storage_service.attrs[1], &result_buffer,1);
else
{
uint8_t result_buffer[1] = {200};
if (conn)
{
bt_gatt_notify(get_current_connection(), &storage_service.attrs[1], &result_buffer,1);
}
}
delete_started = 0;
k_msleep(10);
Expand All @@ -270,27 +281,42 @@ void storage_write(void) {
clear_audio_directory();
nuke_started = 0;
}
if (stop_started)
{
remaining_length = 0;
stop_started = 0;

}

if(remaining_length > 0 ) {

struct bt_conn *conn = get_current_connection();

if (conn == NULL) {
LOG_ERR("invalid connection");
remaining_length = 0;
k_yield();
}
write_to_gatt(conn);

transport_started = 0;
if (remaining_length == 0) {
LOG_INF("done. attempting to download more files\n");
uint8_t stop_result[1] = {100};
int err = bt_gatt_notify(conn, &storage_service.attrs[1], &stop_result,1);
k_sleep(K_MSEC(10));

}

}
k_yield();
if (remaining_length == 0 )
{
if(stop_started)
{
stop_started = 0;
}
else
{
printk("done. attempting to download more files\n");
uint8_t stop_result[1] = {100};
int err = bt_gatt_notify(conn, &storage_service.attrs[1], &stop_result,1);
k_sleep(K_MSEC(10));
}

}
}
k_yield();

}

}
Expand Down
54 changes: 36 additions & 18 deletions Friend/firmware/firmware_v1.0/src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,13 @@ bool write_to_storage(void) {
static bool use_storage = true;
#define MAX_FILES 10
#define MAX_AUDIO_FILE_SIZE 300000
static int recent_file_size_updated = 0;


void update_file_size()
{
file_num_array[0] = get_file_size(1);
printk("file size for file count %d %d\n",file_count,file_num_array[0]);
}

void pusher(void)
{
Expand All @@ -624,7 +629,22 @@ void pusher(void)
//

struct bt_conn *conn = current_connection;
// bool use_gatt = true;
bool use_gatt = true;
//updating the most recent file size is expensive!
static bool file_size_updated = true;
static bool connection_was_true = false;
if (conn && !connection_was_true) {
k_msleep(100);
file_size_updated = false;
connection_was_true = true;
} else if (!conn) {
connection_was_true = false;
}
if (!file_size_updated) {
printk("updating file size\n");
update_file_size();
file_size_updated = true;
}
if (conn)
{
conn = bt_conn_ref(conn);
Expand All @@ -643,21 +663,19 @@ void pusher(void)
valid = bt_gatt_is_subscribed(conn, &audio_service.attrs[1], BT_GATT_CCC_NOTIFY); // Check if subscribed
}

if (!valid && !storage_is_on) {

bool result = write_to_storage();
// file_num_array[file_count-1] = get_file_size(file_count);
// printk("file size for file count %d %d\n",file_count,file_num_array[file_count-1]);
if (result)
if (!valid && !storage_is_on)
{
// if (get_file_size(9) > MAX_AUDIO_FILE_SIZE) {
// printk("Audio file size limit reached, making new file\n");
// // make_and_rebase_audio_file(get_info_file_length()+1);
// }
}
else {
k_sleep(K_MSEC(10));
}

bool result = write_to_storage();

if (result)
{

}
else
{
k_sleep(K_MSEC(10));
}
}

if (valid)
Expand All @@ -678,7 +696,7 @@ void pusher(void)
k_yield();
}
}

extern struct bt_gatt_service storage_service;


//
Expand Down Expand Up @@ -726,7 +744,7 @@ play_boot_sound();

#endif
// Start advertising

bt_gatt_service_register(&storage_service);
bt_gatt_service_register(&audio_service);
bt_gatt_service_register(&dfu_service);
memset(storage_temp_data, 0, OPUS_PADDED_LENGTH * 4);
Expand Down
8 changes: 8 additions & 0 deletions app/lib/backend/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,12 @@ class SharedPreferencesUtil {
set locationPermissionRequested(bool value) => saveBool('locationPermissionRequested', value);

bool get locationPermissionRequested => getBool('locationPermissionRequested') ?? false;

int get currentStorageBytes => getInt('currentStorageBytes') ?? 0;

set currentStorageBytes(int value) => saveInt('currentStorageBytes', value);

int get previousStorageBytes => getInt('previousStorageBytes') ?? 0;

set previousStorageBytes(int value) => saveInt('previousStorageBytes', value);
}
32 changes: 32 additions & 0 deletions app/lib/pages/memories/widgets/processing_capture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:friend_private/providers/device_provider.dart';
import 'package:friend_private/utils/analytics/mixpanel.dart';
import 'package:friend_private/utils/enums.dart';
import 'package:friend_private/utils/other/temp.dart';
import 'package:friend_private/pages/sdcard/page.dart';
import 'package:friend_private/widgets/dialog.dart';
import 'package:provider/provider.dart';

Expand Down Expand Up @@ -39,6 +40,37 @@ class _MemoryCaptureWidgetState extends State<MemoryCaptureWidget> {
provider.recordingState == RecordingState.record ||
(provider.memoryCreating && deviceProvider.connectedDevice != null);

var storageBytes = provider.timeToSend ?? 1;
var totalTimeSent = provider.timeAlreadySent ?? 1;
var totalTimeRemaining = storageBytes - totalTimeSent;
String totalTimeRemainingString = totalTimeRemaining.toStringAsFixed(2);

if(provider.storageIsReady) {
var banner = 'You have ' + totalTimeRemainingString + ' seconds of Storage Remaining. Click here to see';
Future.delayed(Duration.zero, () {
ScaffoldMessenger.of(context).hideCurrentMaterialBanner();
ScaffoldMessenger.of(context).showMaterialBanner(
MaterialBanner(
content: Text(banner),
backgroundColor: Colors.green,
actions: [
TextButton(
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentMaterialBanner();
routeToPage(context, SdCardCapturePage());
},
child: const Text('Click here'),
),
],
onVisible: () => Future.delayed(const Duration(seconds: 15), () {
ScaffoldMessenger.of(context).hideCurrentMaterialBanner();
}),
),
);
});
provider.setStorageIsReady(false);
}

return (showPhoneMic || isConnected)
? GestureDetector(
onTap: () async {
Expand Down
Loading
Loading