Skip to content

Commit

Permalink
Merge pull request #98 from ezloteam/EZPI344-ble-service-addition
Browse files Browse the repository at this point in the history
Ezpi344 ble service addition
  • Loading branch information
lomasSE89 authored Apr 4, 2024
2 parents c0ec5fe + 5fa7c9e commit 985a0b9
Show file tree
Hide file tree
Showing 17 changed files with 455 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ extern "C"
\"op_inv\": false,\
\"val_ip\": false,\
\"val_op\": false\
},\
{\
\"dev_type\": 10,\
\"dev_name\": \"GY61\",\
\"id_room\": \"\",\
\"id_item\": 28,\
\"gpio1\": 33,\
\"gpio2\": 34,\
\"gpio3\": 35\
}\
], \
\"dev_total\": 5}";
Expand Down
1 change: 1 addition & 0 deletions ezlopi-core/ezlopi-core-timer/ezlopi_core_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void ezlopi_timer_start_1000ms(void)
#else
void ezlopi_timer_start_1000ms(void)
{
TRACE_D("initializing timer for 1000 ms interrupt...");
ezlopi_timer_init_timer_event(0, 1000, EZLOPI_ACTION_NOTIFY_1000_MS);
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
#include "ezlopi_core_websocket_client.h"

static esp_websocket_client_handle_t client = NULL;
static void (*__msg_upcall)(const char *, uint32_t) = NULL;
static void (*__msg_upcall)(const char*, uint32_t) = NULL;

typedef struct s_ws_event_arg
{
esp_websocket_client_handle_t client;
void (*msg_upcall)(const char *, uint32_t);
void (*msg_upcall)(const char*, uint32_t);
void (*connection_upcall)(bool connected);
} s_ws_event_arg_t;

typedef struct s_ws_data_buffer
{
char *buffer;
char* buffer;
uint32_t len;
uint32_t tot_len;
struct s_ws_data_buffer *next;
struct s_ws_data_buffer* next;
} s_ws_data_buffer_t;

static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
static void websocket_event_handler(void* handler_args, esp_event_base_t base, int32_t event_id, void* event_data);

int ezlopi_websocket_client_send(char *data, uint32_t len)
int ezlopi_websocket_client_send(char* data, uint32_t len)
{
int ret = 0;

Expand Down Expand Up @@ -65,14 +65,14 @@ void ezlopi_websocket_client_kill(void)
client = NULL;
}

esp_websocket_client_handle_t ezlopi_websocket_client_init(cJSON *uri, void (*msg_upcall)(const char *, uint32_t), void (*connection_upcall)(bool connected))
esp_websocket_client_handle_t ezlopi_websocket_client_init(cJSON* uri, void (*msg_upcall)(const char*, uint32_t), void (*connection_upcall)(bool connected))
{

if ((NULL == client) && (NULL != uri) && (NULL != uri->valuestring) && (NULL != msg_upcall))
{
char *ca_cert = ezlopi_factory_info_v3_get_ca_certificate();
char *ssl_shared = ezlopi_factory_info_v3_get_ssl_shared_key();
char *ssl_priv = ezlopi_factory_info_v3_get_ssl_private_key();
char* ca_cert = ezlopi_factory_info_v3_get_ca_certificate();
char* ssl_shared = ezlopi_factory_info_v3_get_ssl_shared_key();
char* ssl_priv = ezlopi_factory_info_v3_get_ssl_private_key();
__msg_upcall = msg_upcall;

static s_ws_event_arg_t event_arg;
Expand All @@ -97,7 +97,7 @@ esp_websocket_client_handle_t ezlopi_websocket_client_init(cJSON *uri, void (*ms
client = esp_websocket_client_init(&websocket_cfg);
if (client)
{
esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)&event_arg);
esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void*)&event_arg);
esp_websocket_client_start(client);
}
else
Expand All @@ -115,10 +115,10 @@ esp_websocket_client_handle_t ezlopi_websocket_client_init(cJSON *uri, void (*ms
return client;
}

static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
static void websocket_event_handler(void* handler_args, esp_event_base_t base, int32_t event_id, void* event_data)
{
s_ws_event_arg_t *event_arg = (s_ws_event_arg_t *)handler_args;
esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
s_ws_event_arg_t* event_arg = (s_ws_event_arg_t*)handler_args;
esp_websocket_event_data_t* data = (esp_websocket_event_data_t*)event_data;
switch (event_id)
{
case WEBSOCKET_EVENT_CONNECTED:
Expand Down
1 change: 1 addition & 0 deletions ezlopi-main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ezlopi-service-ble
ezlopi-service-meshbot
ezlopi-service-ws-server
ezlopi-service-broadcast
ezlopi-service-led-indicator
)

# Register the components
Expand Down
13 changes: 12 additions & 1 deletion ezlopi-main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ menu "EzloPi User Config"

config EZLOPI_BLE_ENABLE
bool "Enable BLE"
default n
default y
help
Enable BLE

Expand Down Expand Up @@ -101,6 +101,17 @@ menu "EzloPi User Config"

endmenu

choice
prompt "Select the indicator LED available in the device (current target $(IDF_TARGET))"
depends on IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32
config EZPI_ON_OFF_LED
bool "Onboard LED"
depends on IDF_TARGET_ESP32
config EZPI_RGB_LED
bool "Onboard addressable LED"
depends on IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32C3
endchoice

endmenu


51 changes: 3 additions & 48 deletions ezlopi-main/ezlopi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,16 @@
#include "ezlopi_service_webprov.h"
#include "ezlopi_service_ws_server.h"
#include "ezlopi_service_broadcast.h"
#include "ezlopi_service_led_indicator.h"

#include "pt.h"

#define ENABLE_HEARTBIT_LED 1

static void __blinky(void* pv);
static void __init_heartbeat_led(void);
static void __toggle_heartbeat_led(void);

static struct pt pt1;

PT_THREAD(example(struct pt* pt))
{
static uint32_t curr_ticks;
PT_BEGIN(pt);

while (1)
{
curr_ticks = xTaskGetTickCount();
PT_WAIT_UNTIL(pt, (xTaskGetTickCount() - curr_ticks) > 1000);
__toggle_heartbeat_led();
}

PT_END(pt);
}

void app_main(void)
{
ezlopi_service_led_indicator_init();
gpio_install_isr_service(0);

gpio_isr_service_init();
Expand All @@ -80,39 +62,13 @@ void app_main(void)
ezlopi_scenes_meshbot_init();
#endif

xTaskCreate(__blinky, "__blinky", 2 * 2048, NULL, 1, NULL);
xTaskCreate(__blinky, "__blinky", 2 * 2048, NULL, 0, NULL);
}

static void __init_heartbeat_led(void)
{
#if (1 == ENABLE_HEARTBIT_LED)
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << GPIO_NUM_2),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};

gpio_config(&io_conf);
#endif
}

static void __toggle_heartbeat_led(void)
{
#if (1 == ENABLE_HEARTBIT_LED)
static uint32_t state = 0;

state ^= 1;
gpio_set_level(GPIO_NUM_2, state);
#endif
}

static void __blinky(void* pv)
{
__init_heartbeat_led();

PT_INIT(&pt1);
uint32_t count = 0;

while (1)
Expand All @@ -128,6 +84,5 @@ static void __blinky(void* pv)
}

vTaskDelay(5 / portTICK_PERIOD_MS);
example(&pt1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ uint32_t settings_ids[2];
int device_0001_digitalOut_generic(e_ezlopi_actions_t action, l_ezlopi_item_t* item, void* arg, void* user_arg)
{
int ret = 0;

switch (action)
{
case EZLOPI_ACTION_PREPARE:
Expand Down
1 change: 1 addition & 0 deletions ezlopi-services/ezlopi-service-ble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(EZLOPI_CORE_COMPONENTS
ezlopi-core-devices
ezlopi-core-ble
ezlopi-core-reset
ezlopi-core-ping
# ezlopi-user-config
)

Expand Down
3 changes: 3 additions & 0 deletions ezlopi-services/ezlopi-service-ble/ezlopi_service_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
#define BLE_DEVICE_INFO_ID_HANDLE 3
#define BLE_DEVICE_INFO_SERVICE_UUID 0x00E4
#define BLE_DEVICE_INFO_CHAR_UUID 0xE401
#define BLE_DEVICE_STATUS_CHAR_NET_INFO_UUDI 0xE402
#define BLE_DEVICE_INFO_MAC_CHAR_UUID 0xE403

#define BLE_DYNAMIC_CONFIG_HANDLE 4
#define BLE_DYNAMIC_CONFIG_SERVICE_UUID 0x00E5
#define BLE_DYNAMIC_CONFIG_CHAR_UUID 0xE501


void ezlopi_ble_service_init(void);
int ezlopi_ble_service_get_ble_mac(uint8_t mac[6]);
Expand Down
118 changes: 118 additions & 0 deletions ezlopi-services/ezlopi-service-ble/ezlopi_service_ble_device_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "ezlopi_core_ble_buffer.h"
#include "ezlopi_core_ble_profile.h"
#include "ezlopi_core_factory_info.h"
#include "ezlopi_core_event_group.h"

#include "ezlopi_hal_system_info.h"

Expand All @@ -29,6 +30,8 @@ static s_gatt_service_t* g_device_info_service = NULL;
static char* device_info_jsonify(void);
static void __add_factory_info_to_root(cJSON* root, char* key, char* value);
static void device_info_read_func(esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param);
static void device_status_read_func(esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param);
static void device_mac_read_func(esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param);

void ezlopi_ble_service_device_info_init(void)
{
Expand All @@ -47,8 +50,123 @@ void ezlopi_ble_service_device_info_init(void)
properties = ESP_GATT_CHAR_PROP_BIT_READ;
ezlopi_ble_gatt_add_characteristic(g_device_info_service, &uuid, permission, properties, device_info_read_func, NULL, NULL);
TRACE_W("'provisioning_service' character added to ezlopi-ble-stack");

uuid.uuid.uuid16 = BLE_DEVICE_STATUS_CHAR_NET_INFO_UUDI;
uuid.len = ESP_UUID_LEN_16;
permission = ESP_GATT_PERM_READ;
properties = ESP_GATT_CHAR_PROP_BIT_NOTIFY | ESP_GATT_CHAR_PROP_BIT_READ;
ezlopi_ble_gatt_add_characteristic(g_device_info_service, &uuid, permission, properties, device_status_read_func, NULL, NULL);

uuid.uuid.uuid16 = BLE_DEVICE_INFO_MAC_CHAR_UUID;
uuid.len = ESP_UUID_LEN_16;
permission = ESP_GATT_PERM_READ;
properties = ESP_GATT_CHAR_PROP_BIT_READ;
ezlopi_ble_gatt_add_characteristic(g_device_info_service, &uuid, permission, properties, device_mac_read_func, NULL, NULL);
}

static void ble_device_info_send_data(const cJSON* cj_response_data, esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param)
{
char* send_data = cJSON_Print(cj_response_data);
if (send_data)
{
cJSON_Minify(send_data);

uint32_t total_data_len = strlen(send_data);
uint32_t max_data_buffer_size = ezlopi_ble_gatt_get_max_data_size();
uint32_t copy_size = ((total_data_len - param->read.offset) < max_data_buffer_size) ? (total_data_len - param->read.offset) : max_data_buffer_size;

if ((0 != total_data_len) && (total_data_len > param->read.offset))
{
strncpy((char*)value->value, send_data + param->read.offset, copy_size);
value->len = copy_size;
}
if ((param->read.offset + copy_size) >= total_data_len)
{
free(send_data);
send_data = NULL;
}
}
else
{
TRACE_E("No data to send");
value->len = 1;
value->value[0] = 0; // Read 0 if the device not provisioned yet.
}
}

static void device_mac_read_func(esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param)
{
if (value)
{
cJSON* cj_device_mac = cJSON_CreateObject();
if (cj_device_mac)
{
char* device_mac = ezlopi_factory_info_v3_get_ezlopi_mac();
cJSON_AddStringToObject(cj_device_mac, "ezlopi_mac", device_mac ? device_mac : "unknown");

uint8_t mac[6];
ezlopi_wifi_get_wifi_mac(mac);
char mac_str[20];
memset(mac_str, 0, sizeof(mac_str));
snprintf(mac_str, 20, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
cJSON_AddStringToObject(cj_device_mac, "wifi_mac", mac_str);

memset(mac, 0, sizeof(mac));
ezlopi_ble_service_get_ble_mac(mac);
memset(mac_str, 0, sizeof(mac_str));
snprintf(mac_str, 20, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
cJSON_AddStringToObject(cj_device_mac, "ble_mac", mac_str);

ble_device_info_send_data(cj_device_mac, value, param);

cJSON_Delete(cj_device_mac);
free(device_mac);
}
else
{
TRACE_E("Couldn't allocate memory for device mac json");
}
}
else
{
TRACE_E("Value is empty");
}
}

static void device_status_read_func(esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param)
{
if (value)
{
cJSON* cj_device_status = cJSON_CreateObject();
if (cj_device_status)
{
e_ezlopi_event_t event = ezlopi_get_event_bit_status();
e_ping_status_t ping_status = ezlopi_ping_get_internet_status();

cJSON_AddBoolToObject(cj_device_status, "wifi_connection_status", (event & EZLOPI_EVENT_WIFI_CONNECTED) == EZLOPI_EVENT_WIFI_CONNECTED);
cJSON_AddBoolToObject(cj_device_status, "internet_connection_status", ping_status == EZLOPI_PING_STATUS_LIVE);

cJSON_AddBoolToObject(cj_device_status, "cloud_connection_status", (event & EZLOPI_EVENT_NMA_REG) == EZLOPI_EVENT_NMA_REG);
cJSON_AddBoolToObject(cj_device_status, "provision_completion_status", ezlopi_factory_info_v3_get_provisioning_status());

cJSON_AddTrueToObject(cj_device_status, "powered_on");

ble_device_info_send_data(cj_device_status, value, param);

cJSON_Delete(cj_device_status);
}
else
{
TRACE_E("Couldn't allocate memory for device status");
}
}
else
{
TRACE_E("Value is NULL");
}
}


static void device_info_read_func(esp_gatt_value_t* value, esp_ble_gatts_cb_param_t* param)
{
static char* json_str_device_info;
Expand Down
Loading

0 comments on commit 985a0b9

Please sign in to comment.