Пример #1
0
static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
	switch (packet_type){
		case ATT_DATA_PACKET:
			// log_info("att_data_packet with opcode 0x%x", packet[0]);
			if (packet[0] & 1){
				// odd PDUs are sent from server to client
				if (!att_client_handler) return;
				att_client_handler(packet_type, handle, packet, size);
			} else {
				// even PDUs are sent from client to server
				if (!att_server_handler) return;
				att_server_handler(packet_type, handle, packet, size);
			}
		case HCI_EVENT_PACKET:
			if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
			if (att_server_handler && att_server_waiting_for_can_send){
				att_server_waiting_for_can_send = 0;
				att_server_handler(packet_type, handle, packet, size);
				// stop if client cannot send anymore
				if (!hci_can_send_acl_le_packet_now()) break;
			}
			if (att_client_handler && att_client_waiting_for_can_send){
				att_client_waiting_for_can_send = 0;
				att_client_handler(packet_type, handle, packet, size);
			}
			break;
		default:
			break;
	}
}
Пример #2
0
static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
    uint8_t index;
    uint8_t i;
    switch (packet_type){
        case ATT_DATA_PACKET:
            // odd PDUs are sent from server to client - even PDUs are sent from client to server
            index = packet[0] & 1;
            // log_info("att_data_packet with opcode 0x%x", packet[0]);
            if (!subscriptions[index].packet_handler) return;
            subscriptions[index].packet_handler(packet_type, handle, packet, size);
            break;
        case HCI_EVENT_PACKET:
            if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
            can_send_now_pending = 0;
            for (i = 0; i < ATT_MAX; i++){
                index = (att_round_robin + i) & 1;
                if (subscriptions[index].packet_handler && subscriptions[index].waiting_for_can_send){
                    subscriptions[index].waiting_for_can_send = 0;
                    subscriptions[index].packet_handler(packet_type, handle, packet, size);
                    // fairness: prioritize next service
                    att_round_robin = (index + 1) % ATT_MAX;
                    // stop if client cannot send anymore
                    if (!hci_can_send_acl_le_packet_now()) break;
                }
            }
            // check if more can send now events are needed
            if (!can_send_now_pending){
                for (i = 0; i < ATT_MAX; i++){
                    if (subscriptions[i].packet_handler && subscriptions[i].waiting_for_can_send){
                        can_send_now_pending = 1;        
                        // note: con_handle is not used, so we can pass in anything
                        l2cap_request_can_send_fix_channel_now_event(0, L2CAP_CID_ATTRIBUTE_PROTOCOL);
                        break;
                    }
                }
            }
            break;
        default:
            break;
    }
}