xref: /btstack/platform/daemon/example/rfcomm_test.c (revision 45756c9055f8addf0237a07bda21b418904b6139)
12531c97eSMatthias Ringwald /*
22531c97eSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
32531c97eSMatthias Ringwald  *
42531c97eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
52531c97eSMatthias Ringwald  * modification, are permitted provided that the following conditions
62531c97eSMatthias Ringwald  * are met:
72531c97eSMatthias Ringwald  *
82531c97eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
92531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
102531c97eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
112531c97eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
122531c97eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
132531c97eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
142531c97eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
152531c97eSMatthias Ringwald  *    from this software without specific prior written permission.
162531c97eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
172531c97eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
182531c97eSMatthias Ringwald  *    monetary gain.
192531c97eSMatthias Ringwald  *
202531c97eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
212531c97eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
222531c97eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
252531c97eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
262531c97eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
272531c97eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
282531c97eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
292531c97eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
302531c97eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312531c97eSMatthias Ringwald  * SUCH DAMAGE.
322531c97eSMatthias Ringwald  *
332531c97eSMatthias Ringwald  * Please inquire about commercial licensing options at
342531c97eSMatthias Ringwald  * [email protected]
352531c97eSMatthias Ringwald  *
362531c97eSMatthias Ringwald  */
372531c97eSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "rfcomm_test.c"
39ab2c6ae4SMatthias Ringwald 
402531c97eSMatthias Ringwald /*
412531c97eSMatthias Ringwald  *  rfcomm_echo.c
422531c97eSMatthias Ringwald  */
432531c97eSMatthias Ringwald 
442531c97eSMatthias Ringwald #include <unistd.h>
452531c97eSMatthias Ringwald #include <stdio.h>
462531c97eSMatthias Ringwald #include <stdlib.h>
472531c97eSMatthias Ringwald #include <strings.h>
482531c97eSMatthias Ringwald #include <errno.h>
492531c97eSMatthias Ringwald #include <string.h>
502531c97eSMatthias Ringwald #include <fcntl.h>
512531c97eSMatthias Ringwald #include <sys/types.h>
522531c97eSMatthias Ringwald #include <sys/stat.h>
532531c97eSMatthias Ringwald 
542531c97eSMatthias Ringwald #include "btstack_client.h"
552531c97eSMatthias Ringwald #include "classic/sdp_util.h"
562531c97eSMatthias Ringwald 
57fa968b99SMatthias Ringwald #ifdef _WIN32
582ca78d18SMatthias Ringwald #include "btstack_run_loop_windows.h"
59fa968b99SMatthias Ringwald #else
60fa968b99SMatthias Ringwald #include "btstack_run_loop_posix.h"
61fa968b99SMatthias Ringwald #endif
62fa968b99SMatthias Ringwald 
632531c97eSMatthias Ringwald #define NUM_ROWS 25
642531c97eSMatthias Ringwald #define NUM_COLS 80
652531c97eSMatthias Ringwald 
662531c97eSMatthias Ringwald // input from command line arguments
672531c97eSMatthias Ringwald bd_addr_t addr = { };
68711e6c80SMatthias Ringwald hci_con_handle_t con_handle;
692531c97eSMatthias Ringwald char pin[17];
702531c97eSMatthias Ringwald int counter = 0;
712531c97eSMatthias Ringwald uint16_t rfcomm_channel_id = 0;
722531c97eSMatthias Ringwald uint16_t mtu = 0;
732531c97eSMatthias Ringwald static uint8_t   spp_service_buffer[150];
742531c97eSMatthias Ringwald uint8_t test_data[NUM_ROWS * NUM_COLS + 1];
752531c97eSMatthias Ringwald 
create_test_data(void)762531c97eSMatthias Ringwald void create_test_data(void){
772531c97eSMatthias Ringwald     int x,y;
782531c97eSMatthias Ringwald     for (y=0;y<NUM_ROWS;y++){
792531c97eSMatthias Ringwald         for (x=0;x<NUM_COLS-2;x++){
802531c97eSMatthias Ringwald             test_data[y*NUM_COLS+x] = '0' + (x % 10);
812531c97eSMatthias Ringwald         }
822531c97eSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-2] = '\n';
832531c97eSMatthias Ringwald         test_data[y*NUM_COLS+NUM_COLS-1] = '\r';
842531c97eSMatthias Ringwald     }
852531c97eSMatthias Ringwald     test_data[NUM_COLS*NUM_ROWS] = 0;
862531c97eSMatthias Ringwald }
872531c97eSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)882531c97eSMatthias Ringwald void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
892531c97eSMatthias Ringwald 	bd_addr_t event_addr;
902531c97eSMatthias Ringwald 	uint16_t rfcomm_channel_nr;
912531c97eSMatthias Ringwald 
922531c97eSMatthias Ringwald 	switch (packet_type) {
932531c97eSMatthias Ringwald 
942531c97eSMatthias Ringwald 		case RFCOMM_DATA_PACKET:
952531c97eSMatthias Ringwald 			printf("Received RFCOMM data on channel id %u, size %u\n", channel, size);
962531c97eSMatthias Ringwald 			printf_hexdump(packet, size);
972531c97eSMatthias Ringwald             bt_send_rfcomm(channel, packet, size);
982531c97eSMatthias Ringwald 			break;
992531c97eSMatthias Ringwald 
1002531c97eSMatthias Ringwald 		case HCI_EVENT_PACKET:
1010e2df43fSMatthias Ringwald 			switch (hci_event_packet_get_type(packet)) {
1022531c97eSMatthias Ringwald 
1032531c97eSMatthias Ringwald 				case BTSTACK_EVENT_POWERON_FAILED:
1042531c97eSMatthias Ringwald 					// handle HCI init failure
1052531c97eSMatthias Ringwald 					printf("HCI Init failed - make sure you have turned off Bluetooth in the System Settings\n");
1062531c97eSMatthias Ringwald 					exit(1);
1072531c97eSMatthias Ringwald 					break;
1082531c97eSMatthias Ringwald 
1092531c97eSMatthias Ringwald 				case BTSTACK_EVENT_STATE:
1102531c97eSMatthias Ringwald 					// bt stack activated, get started
111be7cc9a0SMilanka Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
1122531c97eSMatthias Ringwald                         // get persistent RFCOMM channel
1132531c97eSMatthias Ringwald                         printf("HCI_STATE_WORKING\n");
1142531c97eSMatthias Ringwald                         bt_send_cmd(&rfcomm_persistent_channel_for_service_cmd, "ch.ringwald.btstack.rfcomm_test");
1152531c97eSMatthias Ringwald                   	}
1162531c97eSMatthias Ringwald 					break;
1172531c97eSMatthias Ringwald 
1181bb145c9SMatthias Ringwald                 case DAEMON_EVENT_RFCOMM_PERSISTENT_CHANNEL:
1192531c97eSMatthias Ringwald                     rfcomm_channel_nr = packet[3];
1202531c97eSMatthias Ringwald                     printf("RFCOMM channel %u was assigned by BTdaemon\n", rfcomm_channel_nr);
1212531c97eSMatthias Ringwald                     bt_send_cmd(&rfcomm_register_service_cmd, rfcomm_channel_nr, 0xffff);  // reserved channel, mtu limited by l2cap
1222531c97eSMatthias Ringwald                     break;
1232531c97eSMatthias Ringwald 
12462c64df1SMatthias Ringwald                 case DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED:
12562c64df1SMatthias Ringwald                     printf("DAEMON_EVENT_RFCOMM_SERVICE_REGISTERED\n");
1262531c97eSMatthias Ringwald                     rfcomm_channel_nr = packet[3];
1272531c97eSMatthias Ringwald                     // register SDP for our SPP
128efda0b48SMatthias Ringwald 				    spp_create_sdp_record((uint8_t*) spp_service_buffer, 0x10001, rfcomm_channel_nr, "RFCOMM Test");
1292531c97eSMatthias Ringwald                     bt_send_cmd(&sdp_register_service_record_cmd, spp_service_buffer);
1302531c97eSMatthias Ringwald                     bt_send_cmd(&btstack_set_discoverable, 1);
1312531c97eSMatthias Ringwald                     break;
1322531c97eSMatthias Ringwald 
1332531c97eSMatthias Ringwald 				case HCI_EVENT_PIN_CODE_REQUEST:
1342531c97eSMatthias Ringwald 					// inform about pin code request
1352531c97eSMatthias Ringwald 					printf("Using PIN 0000\n");
136a6ef64baSMilanka Ringwald 					hci_event_pin_code_request_get_bd_addr(packet, event_addr);
1372531c97eSMatthias Ringwald 					bt_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
1382531c97eSMatthias Ringwald 					break;
1392531c97eSMatthias Ringwald 
1402531c97eSMatthias Ringwald                 case RFCOMM_EVENT_INCOMING_CONNECTION:
1412531c97eSMatthias Ringwald                     // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
142caa82391SMilanka Ringwald                     rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
143caa82391SMilanka Ringwald                     rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet);
144caa82391SMilanka Ringwald                     rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
1452531c97eSMatthias Ringwald                     printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr));
146a232af04SMatthias Ringwald                     bt_send_cmd(&rfcomm_accept_connection_cmd, rfcomm_channel_id);
1472531c97eSMatthias Ringwald                     break;
1482531c97eSMatthias Ringwald 
149f8f6a918SMatthias Ringwald                 case RFCOMM_EVENT_CHANNEL_OPENED:
150caa82391SMilanka Ringwald                     // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
151caa82391SMilanka Ringwald                     if (rfcomm_event_channel_opened_get_status(packet)) {
152caa82391SMilanka Ringwald                         printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet));
1532531c97eSMatthias Ringwald                     } else {
154caa82391SMilanka Ringwald                         rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
155caa82391SMilanka Ringwald                         mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
1562531c97eSMatthias Ringwald                         printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu);
1572531c97eSMatthias Ringwald                     }
1582531c97eSMatthias Ringwald                     break;
1592531c97eSMatthias Ringwald 
1602531c97eSMatthias Ringwald 				case HCI_EVENT_DISCONNECTION_COMPLETE:
1612531c97eSMatthias Ringwald 					// connection closed -> quit test app
1622531c97eSMatthias Ringwald 					printf("Basebank connection closed\n");
1632531c97eSMatthias Ringwald 					break;
1642531c97eSMatthias Ringwald 
1652531c97eSMatthias Ringwald 				default:
1662531c97eSMatthias Ringwald 					break;
1672531c97eSMatthias Ringwald 			}
1682531c97eSMatthias Ringwald 			break;
1692531c97eSMatthias Ringwald 		default:
1702531c97eSMatthias Ringwald 			break;
1712531c97eSMatthias Ringwald 	}
1722531c97eSMatthias Ringwald }
1732531c97eSMatthias Ringwald 
1742531c97eSMatthias Ringwald 
main(int argc,const char * argv[])1752531c97eSMatthias Ringwald int main (int argc, const char * argv[]){
1762531c97eSMatthias Ringwald 
1772531c97eSMatthias Ringwald     create_test_data();
1782531c97eSMatthias Ringwald     printf("created test data: \n%s\n", test_data);
1792531c97eSMatthias Ringwald 
180b9dcd1ccSMatthias Ringwald #ifdef _WIN32
181b9dcd1ccSMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_windows_get_instance());
182fa968b99SMatthias Ringwald #else
183fa968b99SMatthias Ringwald 	btstack_run_loop_init(btstack_run_loop_posix_get_instance());
184b9dcd1ccSMatthias Ringwald #endif
1852531c97eSMatthias Ringwald 	int err = bt_open();
1862531c97eSMatthias Ringwald 	if (err) {
1872531c97eSMatthias Ringwald 		fprintf(stderr,"Failed to open connection to BTdaemon, err %d\n",err);
1882531c97eSMatthias Ringwald 		return 1;
1892531c97eSMatthias Ringwald 	}
1902531c97eSMatthias Ringwald 	bt_register_packet_handler(packet_handler);
1912531c97eSMatthias Ringwald 
1922531c97eSMatthias Ringwald 	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
1932531c97eSMatthias Ringwald 	btstack_run_loop_execute();
1942531c97eSMatthias Ringwald 	bt_close();
1952531c97eSMatthias Ringwald     return 0;
1962531c97eSMatthias Ringwald }
197