Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Mar 7, 2024
1 parent 440ccf7 commit 8698627
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 121 deletions.
2 changes: 0 additions & 2 deletions applications/system/js_app/application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,3 @@ App(
requires=["js_app"],
sources=["modules/js_keyboard.c"],
)


97 changes: 44 additions & 53 deletions applications/system/js_app/modules/js_blebeacon.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ typedef struct {
GapExtraBeaconConfig beacon_config;
} JSblebeaconInst;


struct OUI_MAP_ENTRY {
const char *brand;
const char *oui;
const char* brand;
const char* oui;
};

struct OUI_MAP_ENTRY OUI_MAP[] = {
Expand All @@ -36,41 +35,39 @@ struct OUI_MAP_ENTRY OUI_MAP[] = {

#define OUI_MAP_SIZE (sizeof(OUI_MAP) / sizeof(OUI_MAP[0]))


int rand_range(int min, int max) {
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}

void byte_to_hex(char *output, unsigned char byte) {
void byte_to_hex(char* output, unsigned char byte) {
static const char hex_chars[] = "0123456789ABCDEF";
output[0] = hex_chars[byte >> 4];
output[1] = hex_chars[byte & 0x0F];
}

static char* generate_mac_address(const char *brand) {
char *mac_address = (char*)malloc(18 * sizeof(char));
if (mac_address == NULL) {
static char* generate_mac_address(const char* brand) {
char* mac_address = (char*)malloc(18 * sizeof(char));
if(mac_address == NULL) {
FURI_LOG_D("BLE", "Memory allocation failed.\n");
return NULL;
}

const char *oui = NULL;
for (unsigned int i = 0; i < OUI_MAP_SIZE; ++i) {
if (strcmp(brand, OUI_MAP[i].brand) == 0) {
const char* oui = NULL;
for(unsigned int i = 0; i < OUI_MAP_SIZE; ++i) {
if(strcmp(brand, OUI_MAP[i].brand) == 0) {
oui = OUI_MAP[i].oui;
break;
}
}

if (oui == NULL) {
if(oui == NULL) {
FURI_LOG_D("BLE", "Brand not found.\n");
free(mac_address);
return NULL;
}


char last_bytes[6];
for (int i = 0; i < 3; ++i) {
for(int i = 0; i < 3; ++i) {
unsigned char byte = rand_range(0x00, 0xFF);
byte_to_hex(&last_bytes[i * 2], byte);
}
Expand Down Expand Up @@ -114,9 +111,9 @@ static bool get_str_arg(struct mjs* mjs, size_t index, char** value) {
ret_bad_args(mjs, "Bad string argument");
return false;
}

*value = (char*)malloc(str_len + 1);
if (!*value) {
if(!*value) {
ret_bad_args(mjs, "Memory allocation failed");
return false;
}
Expand All @@ -126,24 +123,23 @@ static bool get_str_arg(struct mjs* mjs, size_t index, char** value) {
}

static uint8_t hex_char_to_uint(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return 10 + c - 'a';
if (c >= 'A' && c <= 'F') return 10 + c - 'A';
if(c >= '0' && c <= '9') return c - '0';
if(c >= 'a' && c <= 'f') return 10 + c - 'a';
if(c >= 'A' && c <= 'F') return 10 + c - 'A';
return 0;
}

static uint8_t* macstr_to_uint8(const char* macstr) {
if (strlen(macstr) != 17) return NULL;
if(strlen(macstr) != 17) return NULL;

uint8_t* mac_bytes = (uint8_t*)malloc(6 * sizeof(uint8_t));
if (!mac_bytes) return NULL;

for (size_t i = 0, j = 0; i < 17; i += 3, ++j) {
if(!mac_bytes) return NULL;

for(size_t i = 0, j = 0; i < 17; i += 3, ++j) {
mac_bytes[j] = (hex_char_to_uint(macstr[i]) << 4) | hex_char_to_uint(macstr[i + 1]);

if (i < 15 && macstr[i + 2] != ':' && macstr[i + 2] != '-') {
free(mac_bytes);
if(i < 15 && macstr[i + 2] != ':' && macstr[i + 2] != '-') {
free(mac_bytes);
return NULL;
}
}
Expand All @@ -153,43 +149,42 @@ static uint8_t* macstr_to_uint8(const char* macstr) {

static uint8_t* hexstr_to_uint8(const char* hexstr, size_t* out_length) {
size_t len = strlen(hexstr);
if (len % 2 != 0) return NULL;
if(len % 2 != 0) return NULL;

if (len > EXTRA_BEACON_MAX_DATA_SIZE + 1) return NULL;
if(len > EXTRA_BEACON_MAX_DATA_SIZE + 1) return NULL;

*out_length = len / 2;
uint8_t* bytes = (uint8_t*)malloc(*out_length);
if (!bytes) return NULL;
if(!bytes) return NULL;

for (size_t i = 0; i < *out_length; ++i) {
for(size_t i = 0; i < *out_length; ++i) {
bytes[i] = (hex_char_to_uint(hexstr[i * 2]) << 4) | hex_char_to_uint(hexstr[i * 2 + 1]);
}

return bytes;
}

static void js_blebeacon_set_data(struct mjs *mjs) {
static void js_blebeacon_set_data(struct mjs* mjs) {
FURI_LOG_D("BLE", "Setting data");
if (!check_arg_count(mjs, 1)) return;
if(!check_arg_count(mjs, 1)) return;
JSblebeaconInst* inst = get_this_ctx(mjs);
if (!inst) {
if(!inst) {
FURI_LOG_D("BLE", "Beacon instance is null");
ret_bad_args(mjs, "Beacon instance is null");
return;
}

if (inst->data) {
if(inst->data) {
FURI_LOG_D("BLE", "Freeing existing data");
free(inst->data);
inst->data = NULL;
inst->data = NULL;
}


if (!get_str_arg(mjs, 0, &(inst->data))) return; // get_str_arg now modifies inst->data directly
if(!get_str_arg(mjs, 0, &(inst->data))) return; // get_str_arg now modifies inst->data directly

size_t data_len = 0;
uint8_t* beacon_data = hexstr_to_uint8(inst->data, &data_len);
if (!beacon_data) {
if(!beacon_data) {
FURI_LOG_D("BLE", "Failed to convert data to hex");
ret_bad_args(mjs, "Failed to convert data to hex");
return;
Expand All @@ -201,10 +196,9 @@ static void js_blebeacon_set_data(struct mjs *mjs) {
mjs_return(mjs, MJS_UNDEFINED);
}

static void js_blebeacon_generate_mac(struct mjs *mjs)
{
static void js_blebeacon_generate_mac(struct mjs* mjs) {
char* company = "";
if (!get_str_arg(mjs, 0, &company)) return;
if(!get_str_arg(mjs, 0, &company)) return;

char* mac = generate_mac_address(company);

Expand All @@ -213,17 +207,16 @@ static void js_blebeacon_generate_mac(struct mjs *mjs)
return mjs_return(mjs, js_mac_address);
}

static void js_blebeacon_set_mac(struct mjs *mjs) {
static void js_blebeacon_set_mac(struct mjs* mjs) {
FURI_LOG_D("BLE", "Setting Mac");
if (!check_arg_count(mjs, 1))
{
if(!check_arg_count(mjs, 1)) {
ret_bad_args(mjs, "Bad args");
return;
}

JSblebeaconInst* inst = get_this_ctx(mjs);
char* mac_addr = "";
if (!get_str_arg(mjs, 0, &mac_addr)) return;
if(!get_str_arg(mjs, 0, &mac_addr)) return;
inst->mac_addr = mac_addr;
inst->beacon_config.min_adv_interval_ms = 50;
inst->beacon_config.max_adv_interval_ms = 150;
Expand All @@ -234,7 +227,7 @@ static void js_blebeacon_set_mac(struct mjs *mjs) {
inst->beacon_config.address_type = GapAddressTypePublic;

uint8_t* mac = macstr_to_uint8(mac_addr);
if (mac) {
if(mac) {
memcpy(inst->beacon_config.address, mac, 6);
furi_hal_bt_extra_beacon_set_config(&inst->beacon_config);
mjs_return(mjs, MJS_UNDEFINED);
Expand All @@ -245,21 +238,19 @@ static void js_blebeacon_set_mac(struct mjs *mjs) {
}
}

static void js_blebeacon_send(struct mjs *mjs) {

static void js_blebeacon_send(struct mjs* mjs) {
furi_hal_bt_extra_beacon_start();

mjs_return(mjs, MJS_UNDEFINED);
}

static void js_blebeacon_stop(struct mjs *mjs) {

static void js_blebeacon_stop(struct mjs* mjs) {
furi_hal_bt_extra_beacon_stop();

mjs_return(mjs, MJS_UNDEFINED);
}

static void* js_blebeacon_create(struct mjs *mjs, mjs_val_t* object) {
static void* js_blebeacon_create(struct mjs* mjs, mjs_val_t* object) {
JSblebeaconInst* inst = malloc(sizeof(JSblebeaconInst));
mjs_val_t blebeacon_obj = mjs_mk_object(mjs);
mjs_set(mjs, blebeacon_obj, INST_PROP_NAME, ~0, mjs_mk_foreign(mjs, inst));
Expand All @@ -272,9 +263,9 @@ static void* js_blebeacon_create(struct mjs *mjs, mjs_val_t* object) {
return inst;
}

static void js_blebeacon_destroy(void *ptr) {
static void js_blebeacon_destroy(void* ptr) {
JSblebeaconInst* inst = (JSblebeaconInst*)ptr;
if (inst) {
if(inst) {
free(inst->data);
free(inst->mac_addr);
free(inst);
Expand Down
63 changes: 35 additions & 28 deletions applications/system/js_app/modules/js_keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static void ret_bad_args(struct mjs* mjs, const char* error) {

char nibble_to_hex_character(uint8_t nibble) {
nibble &= 0xF;
if (nibble < 10) {
if(nibble < 10) {
return '0' + nibble;
} else {
return 'A' + (nibble - 10);
Expand All @@ -29,12 +29,12 @@ char nibble_to_hex_character(uint8_t nibble) {

char* bytes_to_hex_string(uint8_t* bytes, size_t num_bytes) {
char* hex_string = (char*)malloc(num_bytes * 2 + 1);
if (hex_string == NULL) {
if(hex_string == NULL) {
FURI_LOG_D("LOG", "Memory allocation failed\n");
return NULL;
}

for (size_t i = 0; i < num_bytes; ++i) {
for(size_t i = 0; i < num_bytes; ++i) {
hex_string[i * 2] = nibble_to_hex_character(bytes[i] >> 4);
hex_string[i * 2 + 1] = nibble_to_hex_character(bytes[i] & 0xF);
}
Expand Down Expand Up @@ -74,20 +74,17 @@ static JSkeyboardInst* get_this_ctx(struct mjs* mjs) {
return storage;
}

void text_input_callback(void* context)
{
void text_input_callback(void* context) {
JSkeyboardInst* keyboardinst = (JSkeyboardInst*)context;
view_dispatcher_stop(keyboardinst->view_dispatcher);
}

void byte_input_callback(void* context)
{
void byte_input_callback(void* context) {
JSkeyboardInst* keyboardinst = (JSkeyboardInst*)context;
view_dispatcher_stop(keyboardinst->view_dispatcher);
}

static void js_keyboard_text(struct mjs *mjs) {

static void js_keyboard_text(struct mjs* mjs) {
JSkeyboardInst* keyboardinst = get_this_ctx(mjs);

int MaxInputLength;
Expand All @@ -100,18 +97,24 @@ static void js_keyboard_text(struct mjs *mjs) {
mjs_val_t bool_obj = mjs_arg(mjs, 2);
ShouldSelect = (int)mjs_get_bool(mjs, bool_obj);

if (keyboardinst->textinput && keyboardinst->view_dispatcher)
{
if (strlen(defaultText) > 0)
{
if(keyboardinst->textinput && keyboardinst->view_dispatcher) {
if(strlen(defaultText) > 0) {
text_input_set_header_text(keyboardinst->textinput, defaultText);
}

view_dispatcher_attach_to_gui(
keyboardinst->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
keyboardinst->view_dispatcher,
furi_record_open(RECORD_GUI),
ViewDispatcherTypeFullscreen);
furi_record_close(RECORD_GUI);

text_input_set_result_callback(keyboardinst->textinput, text_input_callback, keyboardinst, keyboardinst->data, MaxInputLength, ShouldSelect);
text_input_set_result_callback(
keyboardinst->textinput,
text_input_callback,
keyboardinst,
keyboardinst->data,
MaxInputLength,
ShouldSelect);

view_dispatcher_switch_to_view(keyboardinst->view_dispatcher, 0);

Expand All @@ -123,10 +126,7 @@ static void js_keyboard_text(struct mjs *mjs) {
mjs_return(mjs, mjs_mk_string(mjs, keyboardinst->data, strlen(keyboardinst->data), 1));
}

static void js_keyboard_byte(struct mjs *mjs) {



static void js_keyboard_byte(struct mjs* mjs) {
JSkeyboardInst* keyboardinst = get_this_ctx(mjs);

int MaxInputLength;
Expand All @@ -135,18 +135,24 @@ static void js_keyboard_byte(struct mjs *mjs) {
const char* defaultText;
get_str_arg(mjs, 1, &defaultText);

if (keyboardinst->byteinputview && keyboardinst->view_dispatcher)
{
if (strlen(defaultText) > 0)
{
if(keyboardinst->byteinputview && keyboardinst->view_dispatcher) {
if(strlen(defaultText) > 0) {
byte_input_set_header_text(keyboardinst->byteinputview, defaultText);
}

view_dispatcher_attach_to_gui(
keyboardinst->view_dispatcher, furi_record_open(RECORD_GUI), ViewDispatcherTypeFullscreen);
keyboardinst->view_dispatcher,
furi_record_open(RECORD_GUI),
ViewDispatcherTypeFullscreen);
furi_record_close(RECORD_GUI);

byte_input_set_result_callback(keyboardinst->byteinputview, byte_input_callback, NULL, keyboardinst, keyboardinst->byteinput, 10);
byte_input_set_result_callback(
keyboardinst->byteinputview,
byte_input_callback,
NULL,
keyboardinst,
keyboardinst->byteinput,
10);

view_dispatcher_switch_to_view(keyboardinst->view_dispatcher, 1);

Expand All @@ -160,7 +166,6 @@ static void js_keyboard_byte(struct mjs *mjs) {
FURI_LOG_D("SHIT", "DID THING 2");
}


static void* js_keyboard_create(struct mjs* mjs, mjs_val_t* object) {
JSkeyboardInst* keyboardinst = malloc(sizeof(JSkeyboardInst));
mjs_val_t keyboard_obj = mjs_mk_object(mjs);
Expand All @@ -173,8 +178,10 @@ static void* js_keyboard_create(struct mjs* mjs, mjs_val_t* object) {
keyboardinst->data = malloc(100);
keyboardinst->byteinput = malloc(100);
view_dispatcher_enable_queue(keyboardinst->view_dispatcher);
view_dispatcher_add_view(keyboardinst->view_dispatcher, 0, text_input_get_view(keyboardinst->textinput));
view_dispatcher_add_view(keyboardinst->view_dispatcher, 1, byte_input_get_view(keyboardinst->byteinputview));
view_dispatcher_add_view(
keyboardinst->view_dispatcher, 0, text_input_get_view(keyboardinst->textinput));
view_dispatcher_add_view(
keyboardinst->view_dispatcher, 1, byte_input_get_view(keyboardinst->byteinputview));
*object = keyboard_obj;
return keyboardinst;
}
Expand Down
Loading

0 comments on commit 8698627

Please sign in to comment.