Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
josancamon19 authored Sep 30, 2024
2 parents a31141a + 1b9f2c8 commit f1db67e
Show file tree
Hide file tree
Showing 62 changed files with 2,391 additions and 1,263 deletions.
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
18 changes: 17 additions & 1 deletion app/lib/backend/http/api/memories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:friend_private/backend/schema/memory.dart';
import 'package:friend_private/backend/schema/structured.dart';
import 'package:friend_private/backend/schema/transcript_segment.dart';
import 'package:friend_private/env/env.dart';
import 'package:friend_private/utils/analytics/growthbook.dart';
import 'package:http/http.dart' as http;
import 'package:instabug_flutter/instabug_flutter.dart';
import 'package:path/path.dart';
Expand Down Expand Up @@ -86,6 +85,23 @@ Future<List<ServerMemory>> getMemories({int limit = 50, int offset = 0}) async {
return [];
}

Future<List<ServerProcessingMemory>> getProcessingMemories(
{List<String> filterIds = const [], int limit = 5, int offset = 0}) async {
var url = '${Env.apiBaseUrl}v1/processing-memories?filter_ids=${filterIds.join(",")}&limit=$limit&offset=$offset';
var response = await makeApiCall(url: url, headers: {}, method: 'GET', body: '');
if (response == null) return [];
if (response.statusCode == 200) {
// decode body bytes to utf8 string and then parse json so as to avoid utf8 char issues
var body = utf8.decode(response.bodyBytes);
var memories =
(jsonDecode(body)["result"] as List<dynamic>).map((memory) => ServerProcessingMemory.fromJson(memory)).toList();
return memories;
} else {
debugPrint("[API-Error] $url - ${response.statusCode}");
}
return [];
}

Future<ServerMemory?> reProcessMemoryServer(String memoryId) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/memories/$memoryId/reprocess',
Expand Down
10 changes: 0 additions & 10 deletions app/lib/backend/http/api/plugins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ Future<void> reviewPlugin(String pluginId, double score, {String review = ''}) a
debugPrint('reviewPlugin: ${response?.body}');
}

Future<void> migrateUserServer(String prevUid, String newUid) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}migrate-user?prev_uid=$prevUid&new_uid=$newUid',
headers: {},
method: 'POST',
body: '',
);
debugPrint('migrateUser: ${response?.body}');
}

Future<String> getPluginMarkdown(String pluginMarkdownPath) async {
var response = await makeApiCall(
url: 'https://raw.githubusercontent.com/BasedHardware/Omi/main$pluginMarkdownPath',
Expand Down
28 changes: 24 additions & 4 deletions app/lib/backend/http/api/processing_memories.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:friend_private/backend/http/shared.dart';
import 'package:friend_private/backend/schema/geolocation.dart';
import 'package:friend_private/backend/schema/memory.dart';
Expand All @@ -21,16 +20,13 @@ Future<UpdateProcessingMemoryResponse?> updateProcessingMemoryServer({
bodyData.addAll({"geolocation": geolocation.toJson()});
}

debugPrint(jsonEncode(bodyData));

var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/processing-memories/$id',
headers: {},
method: 'PATCH',
body: jsonEncode(bodyData),
);
if (response == null) return null;
debugPrint('updateProcessingMemoryServer: ${response.body}');
if (response.statusCode == 200) {
return UpdateProcessingMemoryResponse.fromJson(jsonDecode(response.body));
} else {
Expand All @@ -46,3 +42,27 @@ Future<UpdateProcessingMemoryResponse?> updateProcessingMemoryServer({
}
return null;
}

Future<ProcessingMemoryResponse?> fetchProcessingMemoryServer({required String id}) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v1/processing-memories/$id',
headers: {},
method: 'GET',
body: "",
);
if (response == null) return null;
if (response.statusCode == 200) {
return ProcessingMemoryResponse.fromJson(jsonDecode(response.body));
} else {
// TODO: Server returns 304 doesn't recover
CrashReporting.reportHandledCrash(
Exception('Failed to create memory'),
StackTrace.current,
level: NonFatalExceptionLevel.info,
userAttributes: {
'response': response.body,
},
);
}
return null;
}
15 changes: 0 additions & 15 deletions app/lib/backend/http/api/speech_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ Future<String?> getUserSpeechProfile() async {
return null;
}

Future<bool> uploadProfileBytes(List<List<int>> bytes, int duration) async {
var response = await makeApiCall(
url: '${Env.apiBaseUrl}v3/upload-bytes',
headers: {},
body: jsonEncode({'bytes': bytes, 'duration': duration}),
method: 'POST',
);
debugPrint('uploadProfileBytes: ${response?.body}');
if (response == null) return false;
if (response.statusCode != 200) return false;
return true;
}

Future<bool> uploadProfile(File file) async {
var request = http.MultipartRequest(
'POST',
Expand Down Expand Up @@ -91,8 +78,6 @@ Future<List<String>> getExpandedProfileSamples() async {
return [];
}

// DELETE v3/speech-profile/expand?memory_id&segment_idx

Future<bool> deleteProfileSample(
String memoryId,
int segmentIdx, {
Expand Down
12 changes: 12 additions & 0 deletions app/lib/backend/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class SharedPreferencesUtil {

set gcpBucketName(String value) => saveString('gcpBucketName', value);

bool get showSummarizeConfirmation => getBool('showSummarizeConfirmation') ?? true;

set showSummarizeConfirmation(bool value) => saveBool('showSummarizeConfirmation', value);

String get webhookOnMemoryCreated => getString('webhookUrl') ?? '';

set webhookOnMemoryCreated(String value) => saveString('webhookUrl', value);
Expand Down Expand Up @@ -402,4 +406,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);
}
Loading

0 comments on commit f1db67e

Please sign in to comment.