2323#include <pbio/task.h>
2424#include <pbio/util.h>
2525#include <pbio/version.h>
26+ #include <pbsys/app.h>
2627
2728#include <contiki.h>
2829#include <lego_lwp3.h>
@@ -131,7 +132,8 @@ static uint16_t gap_service_handle, gap_service_end_handle;
131132// Device information service handles
132133static uint16_t dev_info_service_handle , dev_info_service_end_handle ;
133134// Pybricks service handles
134- static uint16_t pybricks_service_handle , pybricks_service_end_handle , pybricks_char_handle ;
135+ static uint16_t pybricks_service_handle , pybricks_service_end_handle ;
136+ static uint16_t pybricks_command_event_char_handle , pybricks_capabilities_char_handle ;
135137// Pybricks tx notifications enabled
136138static bool pybricks_notify_en ;
137139// Nordic UART service handles
@@ -379,7 +381,7 @@ static PT_THREAD(send_value_notification(struct pt *pt, pbio_task_t *task))
379381 task -> status = PBIO_ERROR_INVALID_OP ;
380382 goto done ;
381383 }
382- attr_handle = pybricks_char_handle ;
384+ attr_handle = pybricks_command_event_char_handle ;
383385 } else if (send -> connection == PBDRV_BLUETOOTH_CONNECTION_UART ) {
384386 if (!uart_tx_notify_en ) {
385387 task -> status = PBIO_ERROR_INVALID_OP ;
@@ -808,9 +810,12 @@ static void handle_event(uint8_t *packet) {
808810
809811 switch (event_code ) {
810812 case ATT_EVENT_EXCHANGE_MTU_REQ : {
813+ // uint16_t client_mtu = (data[7] << 8) | data[6];
811814 attExchangeMTURsp_t rsp ;
812815
813- rsp .serverRxMTU = 158 ;
816+ rsp .serverRxMTU = ATT_MAX_MTU_SIZE ;
817+ // REVISIT: may need to keep a table of min(client_mtu, MAX_ATT_MTU_SIZE)
818+ // for each connection if any known clients have smaller MTU
814819 ATT_ExchangeMTURsp (connection_handle , & rsp );
815820 }
816821 break ;
@@ -867,7 +872,11 @@ static void handle_event(uint8_t *packet) {
867872 } else if (start_handle <= pybricks_service_handle + 1 ) {
868873 read_by_type_response_uuid128 (connection_handle , pybricks_service_handle + 1 ,
869874 GATT_PROP_WRITE_NO_RSP | GATT_PROP_WRITE | GATT_PROP_NOTIFY ,
870- pbio_pybricks_control_char_uuid );
875+ pbio_pybricks_command_event_char_uuid );
876+ } else if (start_handle <= pybricks_service_handle + 4 ) {
877+ read_by_type_response_uuid128 (connection_handle , pybricks_service_handle + 4 ,
878+ GATT_PROP_READ ,
879+ pbio_pybricks_hub_capabilities_char_uuid );
871880 } else if (start_handle <= uart_service_handle + 1 ) {
872881 read_by_type_response_uuid128 (connection_handle , uart_service_handle + 1 ,
873882 GATT_PROP_WRITE_NO_RSP , pbio_nus_rx_char_uuid );
@@ -971,7 +980,7 @@ static void handle_event(uint8_t *packet) {
971980 rsp .len = 7 ;
972981 rsp .pValue = buf ;
973982 ATT_ReadRsp (connection_handle , & rsp );
974- } else if (handle == pybricks_char_handle + 1 ) {
983+ } else if (handle == pybricks_command_event_char_handle + 1 ) {
975984 attReadRsp_t rsp ;
976985 uint8_t buf [ATT_MTU_SIZE - 1 ];
977986
@@ -980,6 +989,15 @@ static void handle_event(uint8_t *packet) {
980989 rsp .len = 2 ;
981990 rsp .pValue = buf ;
982991 ATT_ReadRsp (connection_handle , & rsp );
992+ } else if (handle == pybricks_capabilities_char_handle + 1 ) {
993+ attReadRsp_t rsp ;
994+ uint8_t buf [PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE ];
995+
996+ // REVISIT: client MTU may be smaller, in which case we can't used fixed value for MTU
997+ pbio_pybricks_hub_capabilities (buf , ATT_MAX_MTU_SIZE - 3 , PBSYS_APP_HUB_FEATURE_FLAGS , PBSYS_APP_USER_PROGRAM_SIZE );
998+ rsp .len = sizeof (buf );
999+ rsp .pValue = buf ;
1000+ ATT_ReadRsp (connection_handle , & rsp );
9831001 } else if (handle == uart_tx_char_handle + 1 ) {
9841002 attReadRsp_t rsp ;
9851003 uint8_t buf [ATT_MTU_SIZE - 1 ];
@@ -1089,11 +1107,11 @@ static void handle_event(uint8_t *packet) {
10891107 uint16_t char_handle = (data [9 ] << 8 ) | data [8 ];
10901108
10911109 DBG ("w: %04X %04X %d" , char_handle , uart_tx_char_handle , pdu_len - 4 );
1092- if (char_handle == pybricks_char_handle ) {
1110+ if (char_handle == pybricks_command_event_char_handle ) {
10931111 if (receive_handler ) {
10941112 receive_handler (PBDRV_BLUETOOTH_CONNECTION_PYBRICKS , & data [10 ], pdu_len - 4 );
10951113 }
1096- } else if (char_handle == pybricks_char_handle + 1 ) {
1114+ } else if (char_handle == pybricks_command_event_char_handle + 1 ) {
10971115 pybricks_notify_en = data [10 ];
10981116 DBG ("noti: %d" , pybricks_notify_en );
10991117 } else if (char_handle == uart_rx_char_handle ) {
@@ -1529,7 +1547,7 @@ static PT_THREAD(init_pybricks_service(struct pt *pt)) {
15291547 PT_BEGIN (pt );
15301548
15311549 PT_WAIT_WHILE (pt , write_xfer_size );
1532- GATT_AddService (GATT_PRIMARY_SERVICE_UUID , 4 , GATT_MIN_ENCRYPT_KEY_SIZE );
1550+ GATT_AddService (GATT_PRIMARY_SERVICE_UUID , 6 , GATT_MIN_ENCRYPT_KEY_SIZE );
15331551 PT_WAIT_UNTIL (pt , hci_command_status );
15341552 // ignoring response data
15351553
@@ -1539,19 +1557,30 @@ static PT_THREAD(init_pybricks_service(struct pt *pt)) {
15391557 // ignoring response data
15401558
15411559 PT_WAIT_WHILE (pt , write_xfer_size );
1542- GATT_AddAttribute2 (pbio_pybricks_control_char_uuid , GATT_PERMIT_READ | GATT_PERMIT_WRITE );
1560+ GATT_AddAttribute2 (pbio_pybricks_command_event_char_uuid , GATT_PERMIT_READ | GATT_PERMIT_WRITE );
15431561 PT_WAIT_UNTIL (pt , hci_command_status );
15441562 // ignoring response data
15451563
15461564 PT_WAIT_WHILE (pt , write_xfer_size );
15471565 GATT_AddAttribute (GATT_CLIENT_CHAR_CFG_UUID , GATT_PERMIT_READ | GATT_PERMIT_WRITE );
15481566 PT_WAIT_UNTIL (pt , hci_command_status );
15491567
1568+ PT_WAIT_WHILE (pt , write_xfer_size );
1569+ GATT_AddAttribute (GATT_CHARACTER_UUID , GATT_PERMIT_READ );
1570+ PT_WAIT_UNTIL (pt , hci_command_status );
1571+ // ignoring response data
1572+
1573+ PT_WAIT_WHILE (pt , write_xfer_size );
1574+ GATT_AddAttribute2 (pbio_pybricks_hub_capabilities_char_uuid , GATT_PERMIT_READ | GATT_PERMIT_WRITE );
1575+ PT_WAIT_UNTIL (pt , hci_command_status );
1576+ // ignoring response data
1577+
15501578 // the response to the last GATT_AddAttribute contains the first and last handles
15511579 // that were allocated.
15521580 pybricks_service_handle = (read_buf [13 ] << 8 ) | read_buf [12 ];
15531581 pybricks_service_end_handle = (read_buf [15 ] << 8 ) | read_buf [14 ];
1554- pybricks_char_handle = pybricks_service_handle + 2 ;
1582+ pybricks_command_event_char_handle = pybricks_service_handle + 2 ;
1583+ pybricks_capabilities_char_handle = pybricks_service_handle + 4 ;
15551584 DBG ("pybricks: %04X" , pybricks_service_handle );
15561585
15571586 PT_END (pt );
0 commit comments