|
|||
|
I'm having a problem figuring out how to printf the value of BD_ADDR
from this macro. What I'm tring to do is get it to display a Bluetooth Address in the format "BD_ADDR = xx:xx:xx:xx:xx:xx" Several people have told me that it's written wrong and breaks the rules of C, but I did not write it, it's part of a bluetooth lib I can't do anything about how it's written, I just want to printf the value of BD_ADDR but I have never before worked with a stucture like this. Below is a snippet of the lib I'm trying to work with. /* * Copyright (c) 2003 EISLAB, Lulea University of Technology. * All rights reserved. * * This file is part of the lwBT Bluetooth stack. * * Author: Conny Ohult <conny@sm.luth.se> * */ #ifndef __BD_ADDR_H__ #define __BD_ADDR_H__ struct bd_addr { u8 addr[6]; }; #define BD_ADDR_LEN 6 #define BD_ADDR_ANY (&(struct bd_addr){{0,0,0,0,0,0}}) #define BD_ADDR_LOCAL (&(struct bd_addr){{0,0,0,0xff,0xff,0xff}}) #define BD_ADDR(bdaddr, a, b, c, d, e, f) do{ \ (bdaddr)->addr[0] = a; \ (bdaddr)->addr[1] = b; \ (bdaddr)->addr[2] = c; \ (bdaddr)->addr[3] = d; \ (bdaddr)->addr[4] = e; \ (bdaddr)->addr[5] = f; }while(0) |
|
|
||||
|
||||
|
|
|
|||
|
In article <4qs3n5hhdgtacqcgjguatko6uset2m6uv2@4ax.com>, idontuse@email.com writes:
> "BD_ADDR = xx:xx:xx:xx:xx:xx" > struct bd_addr { > u8 addr[6]; > }; int print_bd_addr(FILE *f, const struct bd_addr *p) { return fprintf(f, "BD_ADDR = %02x:%02x:%02x:%02x:%02x:%02x", (unsigned)p->addr[0], (unsigned)p->addr[1], (unsigned)p->addr[2], (unsigned)p->addr[3], (unsigned)p->addr[4], (unsigned)p->addr[5] ); } If this is what you want. Cheers, lacos |
|
|||
|
On 10 Feb 2010 01:28:34 +0100, lacos@ludens.elte.hu (Ersek, Laszlo)
wrote: >In article <4qs3n5hhdgtacqcgjguatko6uset2m6uv2@4ax.com>, idontuse@email.com writes: > >> "BD_ADDR = xx:xx:xx:xx:xx:xx" > >> struct bd_addr { >> u8 addr[6]; >> }; > > >int >print_bd_addr(FILE *f, const struct bd_addr *p) >{ > return fprintf(f, "BD_ADDR = %02x:%02x:%02x:%02x:%02x:%02x", > (unsigned)p->addr[0], (unsigned)p->addr[1], > (unsigned)p->addr[2], (unsigned)p->addr[3], > (unsigned)p->addr[4], (unsigned)p->addr[5] > ); >} > >If this is what you want. > >Cheers, >lacos Hi lacos, Thanks for the post. Sorry for my ignorance, but wouyld I put this inside the lib I'm including, or would I put this in my main.c ? No matter where I put it, would I just: int ret; ret = print_bd_addr(); to use it? As you can tell, I don't do a whole lot of c programming and this is the first time I've had to use any type of structure/macro. Thanks! Jason |
|
|||
|
In article <c514n5hhtj10hej94njnhq52670cbpdep6@4ax.com>, idontuse@email.com writes:
> On 10 Feb 2010 01:28:34 +0100, lacos@ludens.elte.hu (Ersek, Laszlo) > wrote: > >>In article <4qs3n5hhdgtacqcgjguatko6uset2m6uv2@4ax.com>, idontuse@email.com writes: >> >>> "BD_ADDR = xx:xx:xx:xx:xx:xx" >> >>> struct bd_addr { >>> u8 addr[6]; >>> }; >> >> >>int >>print_bd_addr(FILE *f, const struct bd_addr *p) >>{ >> return fprintf(f, "BD_ADDR = %02x:%02x:%02x:%02x:%02x:%02x", >> (unsigned)p->addr[0], (unsigned)p->addr[1], >> (unsigned)p->addr[2], (unsigned)p->addr[3], >> (unsigned)p->addr[4], (unsigned)p->addr[5] >> ); >>} > No matter where I put it, would I just: > > int ret; > ret = print_bd_addr(); > > to use it? Please describe what you're trying to achieve. Cheers, lacos |
|
|||
|
An example of using Ersek's Function:
int main(void) { FILE *file = fopen("output.txt","wb"); struct bd_addr BD_ADDR; BD_ADDR->addr[0] = 0xff; BD_ADDR->addr[1] = 0xff; BD_ADDR->addr[2] = 0xff; BD_ADDR->addr[3] = 0xff; BD_ADDR->addr[4] = 0xff; BD_ADDR->addr[5] = 0xff; printf_bd_addr(file,&BD_ADDR); fclose(file); return 0; } int print_bd_addr(FILE *f, const struct bd_addr *p) { return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", (unsigned char)p->addr[0],(unsigned char)p->addr[1], (unsigned char)p->addr[2],(unsigned char)p->addr[3], (unsigned char)p->addr[4],(unsigned char)p->addr[5] ); } |
|
|||
|
On Feb 9, 5:02*pm, idont...@email.com wrote:
> On 10 Feb 2010 01:28:34 +0100, la...@ludens.elte.hu (Ersek, Laszlo) > wrote: > > > > >In article <4qs3n5hhdgtacqcgjguatko6uset2m6...@4ax.com>, idont...@email.com writes: > > >> "BD_ADDR = xx:xx:xx:xx:xx:xx" > > >> struct bd_addr { > >> * u8 addr[6]; > >> }; > > >int > >print_bd_addr(FILE *f, const struct bd_addr *p) > >{ > > *return fprintf(f, "BD_ADDR = %02x:%02x:%02x:%02x:%02x:%02x", > > * * *(unsigned)p->addr[0], (unsigned)p->addr[1], > > * * *(unsigned)p->addr[2], (unsigned)p->addr[3], > > * * *(unsigned)p->addr[4], (unsigned)p->addr[5] > > *); > >} > > >If this is what you want. > > >Cheers, > >lacos > > Hi lacos, > > Thanks for the post. > > Sorry for my ignorance, but wouyld I put this inside the lib I'm > including, or would I put this in my main.c ? > > No matter where I put it, would I just: > > * int ret; > * ret = print_bd_addr(); > > to use it? > > As you can tell, I don't do a whole lot of c programming and this is > the first time I've had to use any type of structure/macro. > > Thanks! > Jason An example of using Ersek's Function: int main(void) { FILE *file = fopen("output.txt","wb"); struct bd_addr *BD_ADDR; BD_ADDR->addr[0] = 0xff; BD_ADDR->addr[1] = 0xff; BD_ADDR->addr[2] = 0xff; BD_ADDR->addr[3] = 0xff; BD_ADDR->addr[4] = 0xff; BD_ADDR->addr[5] = 0xff; printf_bd_addr(file,&BD_ADDR); fclose(file); return 0; } int print_bd_addr(FILE *f, const struct bd_addr *p) { return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", (unsigned char)p->addr[0],(unsigned char)p->addr[1], (unsigned char)p->addr[2],(unsigned char)p->addr[3], (unsigned char)p->addr[4],(unsigned char)p->addr[5] ); } |
|
|||
|
J wrote:
> An example of using Ersek's Function: > > int print_bd_addr(FILE *f, const struct bd_addr *p) > { > return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", > (unsigned char)p->addr[0],(unsigned char)p->addr[1], > (unsigned char)p->addr[2],(unsigned char)p->addr[3], > (unsigned char)p->addr[4],(unsigned char)p->addr[5] > ); > } Why are you casting to unsigned char? -- Ian Collins |
|
|||
|
On Feb 9, 6:00*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> J wrote: > > An example of using Ersek's Function: > > > int print_bd_addr(FILE *f, const struct bd_addr *p) > > { > > * return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", > > * * * (unsigned char)p->addr[0],(unsigned char)p->addr[1], > > * * * (unsigned char)p->addr[2],(unsigned char)p->addr[3], > > * * * (unsigned char)p->addr[4],(unsigned char)p->addr[5] > > * ); > > } > > Why are you casting to unsigned char? > > -- > Ian Collins I wasn't sure what type u8 was until I found this definition using Google Search: typedef U8 unsigned char It could be u8. I must see the definition of u8 in the library. I assumed it was unsigned char. |
|
|||
|
On 10 Feb 2010 02:18:00 +0100, lacos@ludens.elte.hu (Ersek, Laszlo)
wrote: .... > > >Please describe what you're trying to achieve. > >Cheers, >lacos Hi lacos, OK here's my story. I'm obviously a beginning c programmer; hence my probably explaining this wrong, but here's my best shot. I'm editing a main.c that I created that has an #include "bte.h" bte.h has an include of #include "bd_addr.h" Inside bd_addr.h is the BD_ADDR function that has the bluetooth address the system found on boot-up. The main.c program I am writting currently compiles OK, but I need to exact one more piece of information that it has access to, the bluetooth address of the attached device. (The device is a remote controller for a video game (a wii remote)) I was able to get all of the bluetooth functions I'm after to work just fine, but I need to add a feature where I press the "B" Button and it will display the attached remote's BlueTooth address. There are many other functions, such as button_pressed, battery level and so on that I was able to display with little trouble, but when I came across this...macro I guess it is... I got stuck. So, in closing, what I want to do is be able to call one of the built-in functions of bte_address.h and get the BlueTooth Address and printf it on the screen. Below are links to the two includes that are part of my main.c http://pastie.org/817526 = the entire bte.h http://pastie.org/817534 = the entire bd_addr.h |
|
|||
|
On Feb 9, 6:00*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> J wrote: > > An example of using Ersek's Function: > > > int print_bd_addr(FILE *f, const struct bd_addr *p) > > { > > * return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", > > * * * (unsigned char)p->addr[0],(unsigned char)p->addr[1], > > * * * (unsigned char)p->addr[2],(unsigned char)p->addr[3], > > * * * (unsigned char)p->addr[4],(unsigned char)p->addr[5] > > * ); > > } > > Why are you casting to unsigned char? > > -- > Ian Collins An example of using Ersek's Function: int main(void) { FILE *file = fopen("output.txt","wb"); struct bd_addr *BD_ADDR; BD_ADDR->addr[0] = 0xff; BD_ADDR->addr[1] = 0xff; BD_ADDR->addr[2] = 0xff; BD_ADDR->addr[3] = 0xff; BD_ADDR->addr[4] = 0xff; BD_ADDR->addr[5] = 0xff; printf_bd_addr(file,&BD_ADDR); fclose(file); return 0; } int print_bd_addr(FILE *f, const struct bd_addr *p) { return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", (unsigned)p->addr[0],(unsigned)p->addr[1], (unsigned)p->addr[2],(unsigned)p->addr[3], (unsigned)p->addr[4],(unsigned)p->addr[5] ); } |
|
|||
|
On Tue, 9 Feb 2010 18:20:12 -0800 (PST), J <seaworthyjeremy@gmail.com>
wrote: >On Feb 9, 6:00*pm, Ian Collins <ian-n...@hotmail.com> wrote: >> J wrote: >> > An example of using Ersek's Function: >> >> > int print_bd_addr(FILE *f, const struct bd_addr *p) >> > { >> > * return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", >> > * * * (unsigned char)p->addr[0],(unsigned char)p->addr[1], >> > * * * (unsigned char)p->addr[2],(unsigned char)p->addr[3], >> > * * * (unsigned char)p->addr[4],(unsigned char)p->addr[5] >> > * ); >> > } >> >> Why are you casting to unsigned char? >> >> -- >> Ian Collins > >An example of using Ersek's Function: > >int main(void) >{ > FILE *file = fopen("output.txt","wb"); > struct bd_addr *BD_ADDR; > > BD_ADDR->addr[0] = 0xff; > BD_ADDR->addr[1] = 0xff; > BD_ADDR->addr[2] = 0xff; > BD_ADDR->addr[3] = 0xff; > BD_ADDR->addr[4] = 0xff; > BD_ADDR->addr[5] = 0xff; > > printf_bd_addr(file,&BD_ADDR); > fclose(file); > return 0; > >} >int print_bd_addr(FILE *f, const struct bd_addr *p) >{ > return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", > (unsigned)p->addr[0],(unsigned)p->addr[1], > (unsigned)p->addr[2],(unsigned)p->addr[3], > (unsigned)p->addr[4],(unsigned)p->addr[5] > ); >} Thanks J, I really appreciate your showing how this works... I would have never figuered it out on my own. I'm getting ready to give this a try. One question though.. With my limited knowledge of, it looks like this is going to write the value to a text file called output.txt ? If so, what do I need to change to get it to just printf it to the output window? |
|
|||
|
On Tue, 09 Feb 2010 20:39:18 -0800, idontuse@email.com wrote:
>On Tue, 9 Feb 2010 18:20:12 -0800 (PST), J <seaworthyjeremy@gmail.com> >wrote: > >>On Feb 9, 6:00*pm, Ian Collins <ian-n...@hotmail.com> wrote: >>> J wrote: >>> > An example of using Ersek's Function: >>> >>> > int print_bd_addr(FILE *f, const struct bd_addr *p) >>> > { >>> > * return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", >>> > * * * (unsigned char)p->addr[0],(unsigned char)p->addr[1], >>> > * * * (unsigned char)p->addr[2],(unsigned char)p->addr[3], >>> > * * * (unsigned char)p->addr[4],(unsigned char)p->addr[5] >>> > * ); > >>> > } >>> >>> Why are you casting to unsigned char? >>> >>> -- >>> Ian Collins >> >>An example of using Ersek's Function: >> >>int main(void) >>{ >> FILE *file = fopen("output.txt","wb"); >> struct bd_addr *BD_ADDR; >> >> BD_ADDR->addr[0] = 0xff; >> BD_ADDR->addr[1] = 0xff; >> BD_ADDR->addr[2] = 0xff; >> BD_ADDR->addr[3] = 0xff; >> BD_ADDR->addr[4] = 0xff; >> BD_ADDR->addr[5] = 0xff; >> >> printf_bd_addr(file,&BD_ADDR); >> fclose(file); >> return 0; >> >>} >>int print_bd_addr(FILE *f, const struct bd_addr *p) >>{ >> return fprintf(f,"BD_ADDR = %2x:%2x:%2x:%2x:%2x:%2x", >> (unsigned)p->addr[0],(unsigned)p->addr[1], >> (unsigned)p->addr[2],(unsigned)p->addr[3], >> (unsigned)p->addr[4],(unsigned)p->addr[5] >> ); >>} > > >Thanks J, > >I really appreciate your showing how this works... I would have never >figuered it out on my own. > >I'm getting ready to give this a try. >One question though.. > >With my limited knowledge of, it looks like this is going to write the >value to a text file called output.txt ? > >If so, what do I need to change to get it to just printf it to the >output window? > Well, it compiles with a few warnings, so I must be getting closer... main.c l:/devkitPro/mrc/src/source/main.c: In function 'get_bt_info': l:/devkitPro/mrc/src/source/main.c:359: warning: passing argument 2 of 'print_bd_addr' from incompatible pointer type l:/devkitPro/mrc/src/source/main.c:283: note: expected 'const struct bd_addr *' but argument is of type 'struct bd_addr **' l:/devkitPro/mrc/src/source/main.c:288: warning: dereferencing pointer 'BD_ADDR.56' does break strict-aliasing rules l:/devkitPro/mrc/src/source/main.c:288: warning: dereferencing pointer 'BD_ADDR.56' does break strict-aliasing rules l:/devkitPro/mrc/src/source/main.c:287: warning: dereferencing pointer 'BD_ADDR.56' does break strict-aliasing rules l:/devkitPro/mrc/src/source/main.c:287: warning: dereferencing pointer 'BD_ADDR.56' does break strict-aliasing rules l:/devkitPro/mrc/src/source/main.c:286: warning: dereferencing pointer 'BD_ADDR.56' does break strict-aliasing rules l:/devkitPro/mrc/src/source/main.c:286: warning: dereferencing pointer 'BD_ADDR.56' does break strict-aliasing rules l:/devkitPro/mrc/src/source/main.c:359: note: initialized from here tools.c linking ... src.elf output ... src.dol > Process Exit Code: 0 > Time Taken: 00:17 |
|
|||
|
idontuse@email.com writes:
<snip> > Well, it compiles with a few warnings, so I must be getting closer... > > main.c > l:/devkitPro/mrc/src/source/main.c: In function 'get_bt_info': > l:/devkitPro/mrc/src/source/main.c:359: warning: passing argument 2 of > 'print_bd_addr' from incompatible pointer type > l:/devkitPro/mrc/src/source/main.c:283: note: expected 'const struct > bd_addr *' but argument is of type 'struct bd_addr **' > l:/devkitPro/mrc/src/source/main.c:288: warning: dereferencing pointer > 'BD_ADDR.56' does break strict-aliasing rules Some of these are very serious. You can't consider then to be "a few warnings". It is in the nature of C that a very serious error can go undetected by the compiler. To get a warning is a bonus, and they would not be ignored unless you know that it is safe to do so. -- Ben. |
|
|||
|
In article <te34n556tav479lc8c7klcbsmgcql6ooge@4ax.com>, idontuse@email.com writes:
> I'm editing a main.c that I created that has an #include "bte.h" > > bte.h has an include of #include "bd_addr.h" > > Inside bd_addr.h is the BD_ADDR function that has the bluetooth > address the system found on boot-up. > http://pastie.org/817526 = the entire bte.h > > http://pastie.org/817534 = the entire bd_addr.h BD_ADDR is a function-like macro defined in "bd_addr.h", and it doesn't return the boot-up bluetooth address or some such -- it fills in a programmer-supplied "struct bt_addr" object with the six programmer-supplied octets. In other words, it's a convenience macro for assigning a value to a "bluetooth address" object. > So, in closing, what I want to do is be able to call one of the > built-in functions of bte_address.h and get the BlueTooth Address and > printf it on the screen. I've never seen this library before, but from the structure declarations and function declarations (prototypes) in "bte.h", I'd try something like this: #define MAXDEVICES 10u /* Device identifier of the device we care about, or whatever. */ static const u8 mycod[] = { 0xAAu, 0xBBu, 0xCCu }; /* Return value: negative : device found, print error 0 : no device found / device not found positive : device found and printed (no trailing newline) */ static int print_it(void) { u8 max_cnt, flush; s32 inq_res; struct inquiry_info info[MAXDEVICES]; /* Set up as appropriate, for example: */ max_cnt = MAXDEVICES; flush = ...; inq_res = bte_inquiry(info, max_cnt, flush); /* Check inq_res. I guess it returns the number of devices found, or a negative value in case of error. If it succeeds, search for the device you're interested in based on "cod" or whatever, and print its address. */ if (0 < inq_res) { unsigned devidx; for (devidx = 0u; devidx < (unsigned)inq_res; ++devidx) { if (0 == memcmp(info[devidx].cod, mycod, sizeof mycod)) { const u8 *addr; addr = info[devidx].bdaddr.addr; return fprintf(stdout, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned)addr[0], (unsigned)addr[1], (unsigned)addr[2], (unsigned)addr[3], (unsigned)addr[4], (unsigned)addr[5]); } } } return 0; } As mentioned above, print_it() doesn't print a trailing newline after the address. It doesn't flush stdout either. (I'm not even sure if you've meant the standard output stream by "printing to the screen".) Cheers, lacos |
|
|||
|
idontuse@email.com wrote:
> I'm having a problem figuring out how to printf the value of BD_ADDR > from this macro. > > What I'm tring to do is get it to display a Bluetooth Address in the > format > > "BD_ADDR = xx:xx:xx:xx:xx:xx" > > Several people have told me that it's written wrong and breaks the > rules of C, but I did not write it, it's part of a bluetooth lib Sometimes there are good reasons for non-portable libraries to do strange things. I'm not going to look look at the details... > I can't do anything about how it's written, I just want to printf the > value of BD_ADDR but I have never before worked with a stucture like > this. You just deal with the individual components rather than doing the thing as a whole. > Below is a snippet of the lib I'm trying to work with. <snip> > struct bd_addr { > u8 addr[6]; > }; <snip> Depends on how you are using this. If you have something like... struct bd_addr myaddr; You can access the elements as myaddr.addr[0] etc. Then you can print them as normal and put in the colons yourself. Remember that u8 is probably a typedef for "unsigned char" so you need to select your format specifiers appropriately. -- Flash Gordon |
|
|
![]() |
| Popular Tags in the Forum |
| macro, printf, values |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: A better more flexible macro interface - response | Toby Dunn | Newsgroup comp.soft-sys.sas | 1 | 06-30-2009 01:00 AM |
| Macro Zoom Lens For Canon | Abbs@gertrude.com | Newsgroup comp.lang.pascal.delphi.misc | 0 | 05-18-2009 04:44 AM |
| Re: Help Documentation - Macro Comment section | Ian Whitlock | Newsgroup comp.soft-sys.sas | 0 | 10-17-2007 01:19 PM |
| Re: Macro Quoting Puzzle!!! | Ian Whitlock | Newsgroup comp.soft-sys.sas | 0 | 05-28-2007 03:41 PM |
| Re: Defining macro variable with another variable? | Jim Groeneveld | Newsgroup comp.soft-sys.sas | 1 | 09-08-2006 02:38 PM |