working towards notifications
This commit is contained in:
		
							parent
							
								
									be16b2c01c
								
							
						
					
					
						commit
						35fa84a3ff
					
				
					 5 changed files with 45 additions and 26 deletions
				
			
		|  | @ -8,6 +8,7 @@ typedef struct { | ||||||
|   event_callback_t callback; |   event_callback_t callback; | ||||||
| } event_callback_handle_t; | } event_callback_handle_t; | ||||||
| 
 | 
 | ||||||
|  | int8_t ble_send_notification(void *buf, uint8_t size); | ||||||
| void ble_init(); | void ble_init(); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | @ -19,4 +19,6 @@ uint8_t pumps_get_time(uint8_t idx); | ||||||
| uint8_t pumps_set_enabled(uint8_t idx, uint8_t value); | uint8_t pumps_set_enabled(uint8_t idx, uint8_t value); | ||||||
| uint8_t pumps_get_enabled(uint8_t idx); | uint8_t pumps_get_enabled(uint8_t idx); | ||||||
| 
 | 
 | ||||||
|  | uint8_t pumps_get_state(uint8_t idx); | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
							
								
								
									
										47
									
								
								main/ble.c
									
										
									
									
									
								
							
							
						
						
									
										47
									
								
								main/ble.c
									
										
									
									
									
								
							|  | @ -28,10 +28,11 @@ static const char *device_name = "Barback"; | ||||||
| static uint8_t blehr_addr_type; | static uint8_t blehr_addr_type; | ||||||
| static uint8_t callback_handler_count = 0; | static uint8_t callback_handler_count = 0; | ||||||
| static uint16_t conn_handle; | static uint16_t conn_handle; | ||||||
|  | static uint16_t ble_svc_handle; | ||||||
|  | static uint16_t svc_handle_button; | ||||||
| static event_callback_handle_t callback_handlers[CONFIG_CACO_MAX_SERVICES] = { 0 }; | static event_callback_handle_t callback_handlers[CONFIG_CACO_MAX_SERVICES] = { 0 }; | ||||||
| 
 | 
 | ||||||
| static int ble_gap_event(struct ble_gap_event *event, void *arg); | static int ble_gap_event(struct ble_gap_event *event, void *arg); | ||||||
| static uint16_t ble_svc_handle; |  | ||||||
| static int ble_svc_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); | static int ble_svc_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); | ||||||
| static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); | static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); | ||||||
| static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); | static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); | ||||||
|  | @ -119,7 +120,7 @@ static const struct ble_gatt_svc_def service_defs[] = { | ||||||
|       }, { |       }, { | ||||||
|         .uuid = BLE_UUID16_DECLARE(CHAR_BUTTON), |         .uuid = BLE_UUID16_DECLARE(CHAR_BUTTON), | ||||||
|         .access_cb = svc_access_system, |         .access_cb = svc_access_system, | ||||||
|         .val_handle = &ble_svc_handle, |         .val_handle = &svc_handle_button, | ||||||
|         .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |         .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY | ||||||
|       }, |       }, | ||||||
|       { 0 } |       { 0 } | ||||||
|  | @ -236,13 +237,18 @@ static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, str | ||||||
|   uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid); |   uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid); | ||||||
|   uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1); |   uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1); | ||||||
| 
 | 
 | ||||||
| 	if(ctxt->op != BLE_GATT_ACCESS_OP_READ_CHR) { |   ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op); | ||||||
|  | 
 | ||||||
|  | 	if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { | ||||||
| 		ESP_LOGW(TAG, "attempting non-read op on read only characteristics"); | 		ESP_LOGW(TAG, "attempting non-read op on read only characteristics"); | ||||||
| 
 | 
 | ||||||
| 		return 1; | 		return 1; | ||||||
| 	} |   } else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { | ||||||
|  |     uint8_t state = pumps_get_state(idx); | ||||||
| 
 | 
 | ||||||
|   ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op); |     ESP_LOGI(TAG, "pumps_state[%d] = %d", idx, state); | ||||||
|  |     os_mbuf_append(ctxt->om, &state, sizeof state); | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|  | @ -299,8 +305,6 @@ static void ble_advertise(void) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static int ble_gap_event(struct ble_gap_event *event, void *arg) { | static int ble_gap_event(struct ble_gap_event *event, void *arg) { | ||||||
|   uint8_t i = 0; |  | ||||||
| 
 |  | ||||||
|   switch (event->type) { |   switch (event->type) { | ||||||
|     case BLE_GAP_EVENT_CONNECT: |     case BLE_GAP_EVENT_CONNECT: | ||||||
|       /* A new connection was established or a connection attempt failed */ |       /* A new connection was established or a connection attempt failed */ | ||||||
|  | @ -334,22 +338,14 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) { | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case BLE_GAP_EVENT_SUBSCRIBE: |     case BLE_GAP_EVENT_SUBSCRIBE: | ||||||
|       /*
 |       ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; " | ||||||
|       ESP_LOGI(TAG, "handling BLE_GAP_EVENT_SUBSCRIBE"); |                     "val_handle=%d\n", | ||||||
|       ESP_LOGI(TAG, "attr_handle: %d", event->subscribe.attr_handle); |                     event->subscribe.cur_notify, svc_handle_button); | ||||||
|       ESP_LOGI(TAG, "callback_handler_count: %d", callback_handler_count); |  | ||||||
| 
 | 
 | ||||||
|       for(i = 0; (i < CONFIG_CACO_MAX_SERVICES); i++) { |       if(event->subscribe.attr_handle == svc_handle_button) { | ||||||
|         if(callback_handlers[i].handle != NULL) continue; |  | ||||||
| 
 |  | ||||||
|         ESP_LOGI(TAG, "callback_handler: %d", (uint32_t)(callback_handlers[i].handle)); |  | ||||||
| 
 |  | ||||||
|         if (event->subscribe.attr_handle == (uint32_t)(*callback_handlers[i].handle)) { |  | ||||||
|           callback_handlers[i].callback(event, arg); |  | ||||||
|         } |  | ||||||
|       } |       } | ||||||
|  | 
 | ||||||
|       break; |       break; | ||||||
|       */ |  | ||||||
| 
 | 
 | ||||||
|     case BLE_GAP_EVENT_CONN_UPDATE: |     case BLE_GAP_EVENT_CONN_UPDATE: | ||||||
|       ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d", |       ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d", | ||||||
|  | @ -372,9 +368,9 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) { | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     default: |     default: | ||||||
|       ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d mtu=%d\n", |       ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d type=%d\n", | ||||||
| 				event->mtu.conn_handle, | 				event->mtu.conn_handle, | ||||||
| 				event->mtu.value | 				event->type | ||||||
| 			); | 			); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | @ -404,6 +400,13 @@ void nimble_host_task(void *param) { | ||||||
|   nimble_port_run(); |   nimble_port_run(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int8_t ble_send_notification(void *buf, uint8_t size) { | ||||||
|  |   struct os_mbuf *om; | ||||||
|  |   om = ble_hs_mbuf_from_flat(buf, size); | ||||||
|  | 
 | ||||||
|  |   return ble_gattc_notify_custom(conn_handle, svc_handle_button, om); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void ble_init() { | void ble_init() { | ||||||
|   esp_err_t err; |   esp_err_t err; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -14,7 +14,7 @@ uint8_t running = 0; | ||||||
| TimerHandle_t pump_timers[PUMPS]; | TimerHandle_t pump_timers[PUMPS]; | ||||||
| static uint8_t pumps_duration[PUMPS] = {0}; | static uint8_t pumps_duration[PUMPS] = {0}; | ||||||
| static uint8_t pumps_enabled[PUMPS] = {0}; | static uint8_t pumps_enabled[PUMPS] = {0}; | ||||||
| static uint8_t pumps_state[PUMPS]; | static uint8_t pumps_state[PUMPS] = {0}; | ||||||
| static uint8_t safety = 1; | static uint8_t safety = 1; | ||||||
| 
 | 
 | ||||||
| uint8_t pumps_set_time(uint8_t idx, uint8_t time) { | uint8_t pumps_set_time(uint8_t idx, uint8_t time) { | ||||||
|  | @ -48,6 +48,10 @@ uint8_t pumps_get_time(uint8_t idx) { | ||||||
|   return pumps_duration[idx]; |   return pumps_duration[idx]; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | uint8_t pumps_get_state(uint8_t idx) { | ||||||
|  |   return pumps_state[idx]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| uint8_t pump_enable(int8_t i) { | uint8_t pump_enable(int8_t i) { | ||||||
|   return gpio_set_level(pump_gpio_map[i], 0); |   return gpio_set_level(pump_gpio_map[i], 0); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ | ||||||
| 
 | 
 | ||||||
| #include "main.h" | #include "main.h" | ||||||
| #include "pumps.h" | #include "pumps.h" | ||||||
|  | #include "ble.h" | ||||||
| 
 | 
 | ||||||
| #define GPIO_USER_BUTTON      (19) | #define GPIO_USER_BUTTON      (19) | ||||||
| 
 | 
 | ||||||
|  | @ -14,16 +15,24 @@ static void IRAM_ATTR gpio_isr_handler(void* arg) { | ||||||
|   xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); |   xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void gpio_task_example(void* arg) { | static void gpio_task(void* arg) { | ||||||
|   uint32_t io_num; |   uint32_t io_num; | ||||||
|  |   uint8_t on = 1; | ||||||
|  |   uint8_t off = 0; | ||||||
| 
 | 
 | ||||||
|   for(;;) { |   for(;;) { | ||||||
|     if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { |     if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { | ||||||
|       printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num)); |       uint8_t level = gpio_get_level(io_num); | ||||||
|  |       printf("GPIO[%d] intr, val: %d\n", io_num, level); | ||||||
| 
 | 
 | ||||||
|       pumps_run(); |       pumps_run(); | ||||||
|  | 
 | ||||||
|  |       ble_send_notification((void*)&on, 1); | ||||||
|  | 
 | ||||||
|       // just a wee debounce
 |       // just a wee debounce
 | ||||||
|       vTaskDelay(500 / portTICK_PERIOD_MS); |       vTaskDelay(500 / portTICK_PERIOD_MS); | ||||||
|  | 
 | ||||||
|  |       ble_send_notification((void*)&off, 1); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | @ -40,7 +49,7 @@ void user_button_init() { | ||||||
|   gpio_config(&io_conf); |   gpio_config(&io_conf); | ||||||
| 
 | 
 | ||||||
|   gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); |   gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); | ||||||
|   xTaskCreate(gpio_task_example, "gpio_task_example", 4096, NULL, 10, NULL); |   xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 10, NULL); | ||||||
| 
 | 
 | ||||||
|   gpio_install_isr_service(0); |   gpio_install_isr_service(0); | ||||||
|   gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*) 0); |   gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*) 0); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue