From masoom.shaikh at gmail.com Sat Oct 3 14:27:02 2009 From: masoom.shaikh at gmail.com (Masoom Shaikh) Date: Sat Oct 3 14:27:09 2009 Subject: remote_name_request, using libbluetooth Message-ID: Hi, today i spent ages hacking hccontrol to do something similar it does when invoked as below hccontrol remote_name_request somehow i couldn't succeed ;-( what really i want is to search for devices and request their names i have a ruby script which does the same by using hccontrol and manipulating it console output but am interested in C version. pasted below is full source. #include #include #include #include #include /* * removes duplicate entries from result and returns the new size * free()'s the original array, result is calloc()ed * TODO: implement in a better way */ int do_uniq( const int size, struct bt_devinquiry** result) { struct bt_devinquiry* newResult = (struct bt_devinquiry*)calloc( size, sizeof(struct bt_devinquiry)); struct bt_devinquiry* srcCurr = *result; struct bt_devinquiry* dstCurr = newResult; int count = 0; int index = 0; for ( ; index < size; ++index) { int j = 0; int found = 0; while ( j < count) { if ( bdaddr_same( &( newResult[j++].bdaddr), &( srcCurr->bdaddr))) { found = 1; break; } } if ( !found) { *dstCurr = *srcCurr; ++dstCurr; ++count; } ++srcCurr; } free(*result); *result = newResult; return count; } int main( int argc, char* argv[]) { /* search devices */ struct bt_devinquiry* result = 0; int num = bt_devinquiry( 0, 0, 0, &result); if ( num <= 0) { if ( h_errno) herror( "no devices found"); else printf( "no devices found\n"); return num; } /* remove duplicate entries */ num = do_uniq( num, &result); printf( "%d device(s) found\n", num); /* try to query device's name */ int s = bt_devopen( "ubt0hci"); if ( s == -1) { if ( h_errno) herror( "bt_devopen error\n"); else printf( "bt_devopen error\n"); return -1; } int i = 0; for ( ; i < num; ++i) { struct bt_devreq request; memset( &request, 0, sizeof(request)); request.opcode = NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, NG_HCI_OCF_REMOTE_NAME_REQ); request.event = NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL; ng_hci_remote_name_req_cp cp; memset(&cp, 0, sizeof(cp)); bdaddr_copy( &cp.bdaddr, &result->bdaddr); cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0; cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE; request.cparam = (void*)&cp; request.clen = sizeof(cp); char buffer[512]; memset( buffer, 0, 512); request.rparam = (void*)buffer; request.rlen = 512; int status = bt_devreq( s, &request, 0); if ( status == 0) { ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t*)buffer; ng_hci_remote_name_req_compl_ep *ep = (ng_hci_remote_name_req_compl_ep*)(e + 1); printf( "status: %d\n", ep->status); printf( "name: %s\n", ep->name); } else if (status == -1) { if ( h_errno) herror( "bt_devreq error\n"); else printf( "bt_devreq error\n"); } else { printf("bt_devreq unknown return value\n"); } } bt_devclose(s); return 0; } From masoom.shaikh at gmail.com Sun Oct 4 04:57:00 2009 From: masoom.shaikh at gmail.com (Masoom Shaikh) Date: Sun Oct 4 04:57:06 2009 Subject: remote_name_request, using libbluetooth In-Reply-To: <1254600808.002635.1652.nullmailer@galant.ukfsn.org> References: <1254600808.002635.1652.nullmailer@galant.ukfsn.org> Message-ID: On Sat, Oct 3, 2009 at 8:13 PM, Iain Hibbert wrote: > On Sat, 3 Oct 2009, Masoom Shaikh wrote: > > > Hi, > > > > today i spent ages hacking hccontrol to do something similar it does when > > invoked as below > > > > hccontrol remote_name_request > > > > somehow i couldn't succeed ;-( > > you didn't say exactly the failure you have reached, did you use hcidump > to check the actions? > > > what really i want is to search for devices and request their names > > i have a ruby script which does the same by using hccontrol and > manipulating > > it console output > > but am interested in C version. > > > > pasted below is full source. > > > > #include > > #include > > #include > > #include > > #include > > > > /* > > * removes duplicate entries from result and returns the new size > > * free()'s the original array, result is calloc()ed > > * TODO: implement in a better way > > */ > > int do_uniq( const int size, struct bt_devinquiry** result) > > { > > struct bt_devinquiry* newResult = (struct bt_devinquiry*)calloc( > size, > > sizeof(struct bt_devinquiry)); > > > > struct bt_devinquiry* srcCurr = *result; > > struct bt_devinquiry* dstCurr = newResult; > > > > int count = 0; > > int index = 0; > > for ( ; index < size; ++index) > > { > > int j = 0; > > int found = 0; > > while ( j < count) > > { > > if ( bdaddr_same( &( newResult[j++].bdaddr), &( > > srcCurr->bdaddr))) > > { > > found = 1; > > break; > > } > > } > > > > if ( !found) > > { > > *dstCurr = *srcCurr; > > ++dstCurr; > > ++count; > > } > > ++srcCurr; > > } > > free(*result); > > *result = newResult; > > return count; > > } > > > > int main( int argc, char* argv[]) > > { > > /* search devices */ > > struct bt_devinquiry* result = 0; > > int num = bt_devinquiry( 0, 0, 0, &result); > > if ( num <= 0) > > { > > if ( h_errno) > > what is h_errno? I think these checks are uninvolved with the code at > hand.. > > > herror( "no devices found"); > > else > > printf( "no devices found\n"); > > return num; > > } > > /* remove duplicate entries */ > > num = do_uniq( num, &result); > > printf( "%d device(s) found\n", num); > > also, I don't think the do_uniq() step should be necessary -- we did > discuss it at least and in the version i wrote for NetBSD it doesn't > return duplicate results > > > /* try to query device's name */ > > int s = bt_devopen( "ubt0hci"); > > if ( s == -1) > > { > > if ( h_errno) > > herror( "bt_devopen error\n"); > > else > > printf( "bt_devopen error\n"); > > return -1; > > } > > int i = 0; > > for ( ; i < num; ++i) > > { > > struct bt_devreq request; > > memset( &request, 0, sizeof(request)); > > request.opcode = NG_HCI_OPCODE(NG_HCI_OGF_LINK_CONTROL, > > NG_HCI_OCF_REMOTE_NAME_REQ); > > request.event = NG_HCI_EVENT_REMOTE_NAME_REQ_COMPL; > > > > ng_hci_remote_name_req_cp cp; > > memset(&cp, 0, sizeof(cp)); > > bdaddr_copy( &cp.bdaddr, &result->bdaddr); > > cp.page_scan_rep_mode = NG_HCI_SCAN_REP_MODE0; > > cp.page_scan_mode = NG_HCI_MANDATORY_PAGE_SCAN_MODE; > > request.cparam = (void*)&cp; > > request.clen = sizeof(cp); > > > > char buffer[512]; > > memset( buffer, 0, 512); > > request.rparam = (void*)buffer; > > request.rlen = 512; > > > > int status = bt_devreq( s, &request, 0); > > if ( status == 0) > > { > > ng_hci_event_pkt_t *e = (ng_hci_event_pkt_t*)buffer; > > ng_hci_remote_name_req_compl_ep *ep = > > (ng_hci_remote_name_req_compl_ep*)(e + 1); > > printf( "status: %d\n", ep->status); > > printf( "name: %s\n", ep->name); > > } > > I think your problem might be here. The event header is not returned, only > the remote_name_req_compl_ep event packet.. (I would have used that > directly in the rparam field rather than a separate buffer -- bt_devreq > will not overflow the given space) > > > else if (status == -1) > > { > > if ( h_errno) > > herror( "bt_devreq error\n"); > > else > > printf( "bt_devreq error\n"); > > } > > else > > { > > printf("bt_devreq unknown return value\n"); > > } > > } > > bt_devclose(s); > > return 0; > > } > > iain > > > thanks for replying yes, i did forgot to say where it failed, devices are inquired properly no issues there but bt_devreq() fails with status == -1 comment taken i will remove the separate buffer, thank you again here is the hcidump output HCIDump - HCI packet analyzer ver 1.5 device: any snap_len: 65535 filter: 0xffffffffffffffff < HCI Command: Inquiry(0x01|0x0001) plen 5 > HCI Event: Command Status(0x0f) plen 4 > HCI Event: Inquiry Result(0x02) plen 15 > HCI Event: Inquiry Result(0x02) plen 15 > HCI Event: Inquiry Complete(0x01) plen 1 < HCI Command: Remote Name Request(0x01|0x0019) plen 10 > HCI Event: Command Status(0x0f) plen 4 > HCI Event: Remote Name Req Complete(0x07) plen 255 i guess the last line indicates that we does receive device name but some how bt_devreq() returns fails i will poke around bt_devreq() for clues From masoom.shaikh at gmail.com Sun Oct 4 14:39:43 2009 From: masoom.shaikh at gmail.com (Masoom Shaikh) Date: Sun Oct 4 14:39:50 2009 Subject: remote_name_request, using libbluetooth In-Reply-To: <1254643032.101648.162.nullmailer@galant.ukfsn.org> References: <1254600808.002635.1652.nullmailer@galant.ukfsn.org> <1254643032.101648.162.nullmailer@galant.ukfsn.org> Message-ID: On Sun, Oct 4, 2009 at 7:57 AM, Iain Hibbert wrote: > On Sun, 4 Oct 2009, Masoom Shaikh wrote: > > > > On Sat, 3 Oct 2009, Masoom Shaikh wrote: > > > > > > > int status = bt_devreq( s, &request, 0); > > > > yes, i did forgot to say where it failed, devices are inquired properly > no > > issues there > > but bt_devreq() fails with status == -1 > > ah, I see.. you should put a non-zero timeout value there (waiting for 0 > seconds will not leave enough time for the response :) > > if you use eg > > if (status == -1) > err(1, "bt_devreq (RemoteNameRequest)") > > then you would see an "Operation timed out" message.. I should think 5 > seconds would be reasonable.. > > regards, > iain > > > u got the bug by neck, and i would have slit its neck by the time this mail is in your inbox ;-) that was the exact error, timeout value of 5 didn't work for me, thus am using 60, and it works thanks Iain From masoom.shaikh at gmail.com Sun Oct 4 17:42:07 2009 From: masoom.shaikh at gmail.com (Masoom Shaikh) Date: Sun Oct 4 17:42:13 2009 Subject: bluetooth.h, c++ include error In-Reply-To: References: Message-ID: posting here after @current and @questions ignored Hello, while going through bluetooth.h, I observed int bt_devfilter(int s, struct bt_devfilter const *new, struct bt_devfilter *old); this line appears in bluetooth.h @ line # 166, rv197571 see ? the variable named 'new' will cause trouble to c++ source files i honestly believe this is not intentional, c++ programmers will have to perform some acrobats to get past this thanks, Masoom Shaikh From maksim.yevmenkin at gmail.com Sun Oct 4 18:26:57 2009 From: maksim.yevmenkin at gmail.com (Maksim Yevmenkin) Date: Sun Oct 4 18:27:04 2009 Subject: bluetooth.h, c++ include error In-Reply-To: References: Message-ID: On Sun, Oct 4, 2009 at 10:42 AM, Masoom Shaikh wrote: > posting here after @current and @questions ignored > > Hello, > > while going through bluetooth.h, I observed > > int bt_devfilter(int s, struct bt_devfilter const *new, struct bt_devfilter > *old); > > this line appears in bluetooth.h @ line # 166, rv197571 > > see ? the variable named 'new' will cause trouble to c++ source files > i honestly believe this is not intentional, c++ programmers will have to > perform some acrobats to get past this patches are welcome thanks, max From masoom.shaikh at gmail.com Mon Oct 5 16:58:12 2009 From: masoom.shaikh at gmail.com (Masoom Shaikh) Date: Mon Oct 5 16:58:18 2009 Subject: bluetooth.h, c++ include error In-Reply-To: References: Message-ID: On Sun, Oct 4, 2009 at 6:26 PM, Maksim Yevmenkin wrote: > On Sun, Oct 4, 2009 at 10:42 AM, Masoom Shaikh > wrote: > > posting here after @current and @questions ignored > > > > Hello, > > > > while going through bluetooth.h, I observed > > > > int bt_devfilter(int s, struct bt_devfilter const *new, struct > bt_devfilter > > *old); > > > > this line appears in bluetooth.h @ line # 166, rv197571 > > > > see ? the variable named 'new' will cause trouble to c++ source files > > i honestly believe this is not intentional, c++ programmers will have to > > perform some acrobats to get past this > > > patches are welcome > > thanks, > max > what i am requesting is just variable name change ;-) my tree is at revision 197571, thus the patch i am presenting is also generated against it reject this if you want patch against latest revision renamed second argument of bt_devfilter() from 'new' to 'dfnew'. df for device filter to maintain uniformity renamed third argument from 'old' to 'dfold'. the patch follows Index: bluetooth.h =================================================================== --- bluetooth.h (revision 197571) +++ bluetooth.h (working copy) @@ -28,7 +28,7 @@ * SUCH DAMAGE. * * $Id: bluetooth.h,v 1.5 2003/09/14 23:28:42 max Exp $ - * $FreeBSD$ + * $FreeBSD: stable/8/lib/libbluetooth/bluetooth.h 191388 2009-04-22 15:50:03Z emax $ */ #ifndef _BLUETOOTH_H_ @@ -163,8 +163,8 @@ int bt_devsend (int s, uint16_t opcode, void *param, size_t plen); ssize_t bt_devrecv (int s, void *buf, size_t size, time_t to); int bt_devreq (int s, struct bt_devreq *r, time_t to); -int bt_devfilter(int s, struct bt_devfilter const *new, - struct bt_devfilter *old); +int bt_devfilter(int s, struct bt_devfilter const *dfnew, + struct bt_devfilter *dfold); void bt_devfilter_pkt_set(struct bt_devfilter *filter, uint8_t type); void bt_devfilter_pkt_clr(struct bt_devfilter *filter, uint8_t type); int bt_devfilter_pkt_tst(struct bt_devfilter const *filter, uint8_t type); From plunky at rya-online.net Mon Oct 5 17:25:25 2009 From: plunky at rya-online.net (Iain Hibbert) Date: Mon Oct 5 17:25:48 2009 Subject: bluetooth.h, c++ include error In-Reply-To: References: Message-ID: <1254763597.419838.805.nullmailer@galant.ukfsn.org> On Mon, 5 Oct 2009, Masoom Shaikh wrote: > what i am requesting is just variable name change ;-) my tree is at > revision 197571, thus the patch i am presenting is also generated > against it reject this if you want patch against latest revision > > renamed second argument of bt_devfilter() from 'new' to 'dfnew'. df for > device filter to maintain uniformity renamed third argument from 'old' > to 'dfold'. style(9) mentions that for kernel include files exposed to userland (technically, this is always in userland but perhaps the same rules apply) the prototype tag arguments should be made safe, eg starting with a _ perhaps for much this reason.. so, my suggestion would be to use _new and _old etc.. iain (or, just don't use them, they are not especially required :) From masoom.shaikh at gmail.com Tue Oct 6 15:48:04 2009 From: masoom.shaikh at gmail.com (Masoom Shaikh) Date: Tue Oct 6 15:48:10 2009 Subject: bluetooth.h, c++ include error In-Reply-To: <1254763597.419838.805.nullmailer@galant.ukfsn.org> References: <1254763597.419838.805.nullmailer@galant.ukfsn.org> Message-ID: On Mon, Oct 5, 2009 at 5:26 PM, Iain Hibbert wrote: > On Mon, 5 Oct 2009, Masoom Shaikh wrote: > > > what i am requesting is just variable name change ;-) my tree is at > > revision 197571, thus the patch i am presenting is also generated > > against it reject this if you want patch against latest revision > > > > renamed second argument of bt_devfilter() from 'new' to 'dfnew'. df for > > device filter to maintain uniformity renamed third argument from 'old' > > to 'dfold'. > > style(9) mentions that for kernel include files exposed to userland > (technically, this is always in userland but perhaps the same rules apply) > the prototype tag arguments should be made safe, eg starting with a _ > perhaps for much this reason.. > > so, my suggestion would be to use _new and _old etc.. > > iain > > (or, just don't use them, they are not especially required :) > > > > > so this shall work i just renamed bt_devfilter() variable names, because it was the one which caused trouble will someone commit this ? Index: bluetooth.h =================================================================== --- bluetooth.h (revision 197804) +++ bluetooth.h (working copy) @@ -163,8 +163,8 @@ int bt_devsend (int s, uint16_t opcode, void *param, size_t plen); ssize_t bt_devrecv (int s, void *buf, size_t size, time_t to); int bt_devreq (int s, struct bt_devreq *r, time_t to); -int bt_devfilter(int s, struct bt_devfilter const *new, - struct bt_devfilter *old); +int bt_devfilter(int s, struct bt_devfilter const *_new, + struct bt_devfilter *_old); void bt_devfilter_pkt_set(struct bt_devfilter *filter, uint8_t type); void bt_devfilter_pkt_clr(struct bt_devfilter *filter, uint8_t type); int bt_devfilter_pkt_tst(struct bt_devfilter const *filter, uint8_t type); From maksim.yevmenkin at gmail.com Mon Oct 26 17:43:36 2009 From: maksim.yevmenkin at gmail.com (Maksim Yevmenkin) Date: Mon Oct 26 17:43:41 2009 Subject: bluetooth.h, c++ include error In-Reply-To: References: <1254763597.419838.805.nullmailer@galant.ukfsn.org> Message-ID: On Tue, Oct 6, 2009 at 8:48 AM, Masoom Shaikh wrote: > > > On Mon, Oct 5, 2009 at 5:26 PM, Iain Hibbert wrote: >> >> On Mon, 5 Oct 2009, Masoom Shaikh wrote: >> >> > what i am requesting is just variable name change ;-) my tree is at >> > revision 197571, thus the patch i am presenting is also generated >> > against it reject this if you want patch against latest revision >> > >> > renamed second argument of bt_devfilter() from 'new' to 'dfnew'. df for >> > device filter to maintain uniformity renamed third argument from 'old' >> > to 'dfold'. >> >> style(9) mentions that for kernel include files exposed to userland >> (technically, this is always in userland but perhaps the same rules apply) >> the prototype tag arguments should be made safe, eg starting with a _ >> perhaps for much this reason.. >> >> so, my suggestion would be to use _new and _old etc.. >> >> iain >> >> (or, just don't use them, they are not especially required :) >> > so this shall work > i just renamed bt_devfilter() variable names, because it was the one which > caused trouble > will someone commit this ? i just committed fix for this. thanks max