Home of the Squeezebox™ & Transporter® network music players.
Page 5 of 6 FirstFirst ... 3456 LastLast
Results 41 to 50 of 55
  1. #41
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    I added/included your comments and now the receiving works!

    Now I could parse the first JSON content...

    Only issues now: I get core dumps, when I add more variables to the structure. (e.g. https://github.com/chincheta0815/Hue...e_common.h#L38)

    When I uncomment the structure items I get Segfaults, even after initializing them alltogether (see https://github.com/chincheta0815/Hue...ze2hue.c#L1005)

    I cannot get the reason. In my opinion everything is initialized and I do not change the things...

    Is there a easy way to find out why there are those segfaults?
    LMS-7.9@solaris. 2x Radio, 2x Duet, 1x Chromecast v1, ShairTunes, 1x Philips Hue System

  2. #42
    Senior Member
    Join Date
    May 2008
    Location
    Canada
    Posts
    3,103
    Quote Originally Posted by chincheta0815 View Post
    I added/included your comments and now the receiving works!

    Now I could parse the first JSON content...

    Only issues now: I get core dumps, when I add more variables to the structure. (e.g. https://github.com/chincheta0815/Hue...e_common.h#L38)

    When I uncomment the structure items I get Segfaults, even after initializing them alltogether (see https://github.com/chincheta0815/Hue...ze2hue.c#L1005)

    I cannot get the reason. In my opinion everything is initialized and I do not change the things...

    Is there a easy way to find out why there are those segfaults?
    I've not looked into details, but it might be that some of the char* are not allocated.

    Remember that in C, all pointers must be allocated/freed. Doing something like char *a="some string" means that the pointer "a" points to the string of length "some strings", no more. Later, if you change that string (which I do not recommend), you cannot exceed the length of "some string". Also, when if you have two pointers, char *a,*b doing a = b does not copy the _string_ from b to a, it justs copies pointers. In other words
    Code:
    char *a, *b;
    
    b = strdup("some string");
    a = b;
    free(b);
    // here, a does not point any more to a valid string/memory zone
    In other places of the code, when you use JSON to extract a string value, you also need to allocate it or "strdup" them. For example, in mac = json_object_get(root, "mac"), remember that "mac" points to an internal JSON string that should not be modified and will be automatically freed by the JSON library when the "root" object is not used anymore (which happens when you call json_decref(root)). It does not allocate memory for "mac". If you intend to keep "mac", you need to do mac = strdup(json_object_get(root, "mac")); Otherwise, after the json_decref call, "mac will point to a memory zone that you should not use

    Another example
    Code:
                if ((p = strnstr(response_message, "HTTP", len)) != NULL) {
                    if (!strstr(p, "\r\n")) {
                        continue;	// EoL not in buffer, next time then
                    }
                    sscanf(p, "HTTP/1.1 %d %s", &response->status_code, response->status_desc);
                }
    Here, even if the structure "response" is allocated, you _must_ allocate the memory for response->status_desc. When doing malloc for response, only a pointer for status_desc is allocated (usually 4 bytes), but no memory for the string itsef is allocated. You will typically have a memory crash, as sscanf will write wherever status_desc points, which is a random zone of memory

    You should run your application using "gdb" and "valgrind", this tools will help you locate where the segfaults happen and identify memory leaks. Tols like coverity are also useful for static code analysis but they are more difficult to use
    Last edited by philippe_44; 2017-04-26 at 20:13.
    LMS 7.7, 7.8 and 7.9 - 5xRadio, 3xBoom, 4xDuet, 1xTouch, 1 SB2. Sonos PLAY:3, PLAY:5, Marantz NR1603, JBL OnBeat, XBoxOne, XBMC, Foobar2000, ShairPortW, JRiver 21, 2xChromecast Audio, Chromecast v1 and v2, , Pi B3, B2, Pi B+, 2xPi A+, Odroid-C1, Odroid-C2, Cubie2, Yamaha WX-010, AppleTV 4, Airport Express

  3. #43
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    Quote Originally Posted by philippe_44 View Post
    Here, even if the structure "response" is allocated, you _must_ allocate the memory for response->status_desc. When doing malloc for response, only a pointer for status_desc is allocated (usually 4 bytes), but no memory for the string itsef is allocated. You will typically have a memory crash, as sscanf will write wherever status_desc points, which is a random zone of memory
    I thought that it might be some allocation issue with stings. But how would I then allocate memory for a string of an unknown length when using sscanf?
    Is the return from sscanf allocated and ready for copying/strdupping?

    What puzzles me in that context:
    This line "https://github.com/chincheta0815/HueLightning/blob/master/application/squeeze2hue/squeeze2hue.c#L1003" seems to work without allocation...
    LMS-7.9@solaris. 2x Radio, 2x Duet, 1x Chromecast v1, ShairTunes, 1x Philips Hue System

  4. #44
    Senior Member
    Join Date
    May 2008
    Location
    Canada
    Posts
    3,103
    Quote Originally Posted by chincheta0815 View Post
    I thought that it might be some allocation issue with stings. But how would I then allocate memory for a string of an unknown length when using sscanf?
    Is the return from sscanf allocated and ready for copying/strdupping?
    I think this is one of the unhappy thing about scanf. The suggestion is to limit the size of the aquired string by scanf("%20s", s) which means that only 20 char will be acquired. So you need to pass a string "s" that has at least 20 char allocated (need to check if the trailing 0 is included, I keep forgetting that)
    What puzzles me in that context:
    This line "https://github.com/chincheta0815/HueLightning/blob/master/application/squeeze2hue/squeeze2hue.c#L1003" seems to work without allocation...
    Yes, you absolutely can do that, per my previous post. The var "huebridge->ipAddress" is a pointer and you make it point to a string which is pre-buld, "192.168.47.251". But remember, you do _not_ have _copied_ the string, you just made the pointer point to a valid memory location. That would be the difference with huebridge->ipAddress = strdup("192.168.47.251");
    But when you do huebridge->ipAddress = "192.168.47.251" be very careful if you modify the content of huebridge->ipAddress later, because space is only allocated for 15 chars.
    LMS 7.7, 7.8 and 7.9 - 5xRadio, 3xBoom, 4xDuet, 1xTouch, 1 SB2. Sonos PLAY:3, PLAY:5, Marantz NR1603, JBL OnBeat, XBoxOne, XBMC, Foobar2000, ShairPortW, JRiver 21, 2xChromecast Audio, Chromecast v1 and v2, , Pi B3, B2, Pi B+, 2xPi A+, Odroid-C1, Odroid-C2, Cubie2, Yamaha WX-010, AppleTV 4, Airport Express

  5. #45
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    Okay, I simply left out some problematic things and I will keep them for later...

    I am right now optimizing the structures a little bit, e.g. storing the IP address as a in_addr and the mac as a uint8_t...
    Can you tell me where you have hidden the function that change the mac address from string to uint8_t?
    For the ip they tell that in_addr is deprecated and shall be change to inet_pton or so... But that's seem a little bit problematic...

    Once I got that I try to implement that into the framework...
    LMS-7.9@solaris. 2x Radio, 2x Duet, 1x Chromecast v1, ShairTunes, 1x Philips Hue System

  6. #46
    Senior Member
    Join Date
    May 2008
    Location
    Canada
    Posts
    3,103
    Quote Originally Posted by chincheta0815 View Post
    Okay, I simply left out some problematic things and I will keep them for later...

    I am right now optimizing the structures a little bit, e.g. storing the IP address as a in_addr and the mac as a uint8_t...
    Can you tell me where you have hidden the function that change the mac address from string to uint8_t?
    For the ip they tell that in_addr is deprecated and shall be change to inet_pton or so... But that's seem a little bit problematic...

    Once I got that I try to implement that into the framework...
    Nothing very elegant for mac extraction, you can do
    Code:
    for (i = 0; i < 6; i++) mac_u8[i] = strtol(mac + i*3, NULL, 16);
    For ip conversion, I usually use inet_addr
    LMS 7.7, 7.8 and 7.9 - 5xRadio, 3xBoom, 4xDuet, 1xTouch, 1 SB2. Sonos PLAY:3, PLAY:5, Marantz NR1603, JBL OnBeat, XBoxOne, XBMC, Foobar2000, ShairPortW, JRiver 21, 2xChromecast Audio, Chromecast v1 and v2, , Pi B3, B2, Pi B+, 2xPi A+, Odroid-C1, Odroid-C2, Cubie2, Yamaha WX-010, AppleTV 4, Airport Express

  7. #47
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    Quote Originally Posted by philippe_44 View Post
    For ip conversion, I usually use inet_addr
    Concerning that: I read that inet_addr is deprecated since it does not support ipv6 and one should use inet_pton and inet_ntop, which also works, but the structures are weird. Do you think that can wait a little bit until it has to be used in squeeze2xxx? Another thing with inet_addr: I could not manage it so far to re-convert the in_addr to a string which is acually needed for building up the ipAddress for the HTTP-request. Did you get that working in one of your bridges and have a hint how to manage that?

    And last but not least I have a conceptual question: The hue bridge stores the mac address and it's name and can send that via JSON. Is it better to use this JSON info or would you rather use, e.g. SendARP for getting the mac address (that's I think the way you do it)?
    LMS-7.9@solaris. 2x Radio, 2x Duet, 1x Chromecast v1, ShairTunes, 1x Philips Hue System

  8. #48
    Senior Member
    Join Date
    May 2008
    Location
    Canada
    Posts
    3,103
    Use inet_ntoa to reconvert. I do no think you should be worried at all about the inet_addr at this point. For MAC address, I would avoid sendarp. My multi platform version if that does not work well
    LMS 7.7, 7.8 and 7.9 - 5xRadio, 3xBoom, 4xDuet, 1xTouch, 1 SB2. Sonos PLAY:3, PLAY:5, Marantz NR1603, JBL OnBeat, XBoxOne, XBMC, Foobar2000, ShairPortW, JRiver 21, 2xChromecast Audio, Chromecast v1 and v2, , Pi B3, B2, Pi B+, 2xPi A+, Odroid-C1, Odroid-C2, Cubie2, Yamaha WX-010, AppleTV 4, Airport Express

  9. #49
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    Quote Originally Posted by philippe_44 View Post
    Use inet_ntoa to reconvert. I do no think you should be worried at all about the inet_addr at this point. For MAC address, I would avoid sendarp. My multi platform version if that does not work well
    For the mac address, I would then try to read the mac form the hue directly and convert it to uint8_t using the code you gave me.
    LMS-7.9@solaris. 2x Radio, 2x Duet, 1x Chromecast v1, ShairTunes, 1x Philips Hue System

  10. #50
    Senior Member
    Join Date
    Jan 2011
    Location
    Germany
    Posts
    226
    Hi philippe44,

    I am now working with the framework you made for me. I did the first compilation with it and got some compiler messages. I can get rid of them, but I do not wat to break things up. Could you do me a favor an look into them?

    The first one is the following:
    Code:
    9/build/inc -I../libupnp/1.6.19/threadutil/inc -I../jansson/2.10/include -I./libhuec/include ./squeezetiny/resample.c -c -o bin/sunos/resample.o
    ./squeezetiny/resample.c:35:0: warning: "LINKALL" redefined
     #define LINKALL 1
     ^
    In file included from ./squeezetiny/resample.c:24:0:
    ./squeezetiny/squeezelite.h:70:0: note: this is the location of the previous definition
     #define LINKALL   0
     ^
    gcc -Wall -fPIC -O2  -I. -I./squeezetiny -I./tools -I./squeeze2hue -I./squ
    The next one is:
    Code:
    ./squeeze2hue/squeeze2hue.c: In function ‘AddHueDevice’:
    ./squeeze2hue/squeeze2hue.c:743:56: warning: passing argument 3 of ‘SendARP’ from incompatible pointer type [-Wincompatible-pointer-types]
       if (SendARP(*((in_addr_t*) &Device->ip), INADDR_ANY, Device->sq_config.mac, &mac_size)) {
                                                            ^
    In file included from ./squeezetiny/squeezedefs.h:29:0,
                     from ./squeeze2hue/squeeze2hue.c:38:
    ./tools/platform.h:104:5: note: expected ‘u32_t * {aka unsigned int *}’ but argument is of type ‘u8_t * {aka unsigned char *}’
     int SendARP(in_addr_t src, in_addr_t dst, u32_t *mac, u32_t *size);
         ^
    gcc -Wall -fPIC -O2  -I. -I./squeezetiny -I./tools -I./squ
    Since this is not working very good in terms of platform dependencies etc. I am left that SendARP out and use now the MAC I am reading from the hue's config info. All I saw is that the mac is only needed for "naming" squeezelite... Can I then also delte the "MakeMacUnique" function?
    Last edited by chincheta0815; 2017-04-29 at 04:33.
    LMS-7.9@solaris. 2x Radio, 2x Duet, 1x Chromecast v1, ShairTunes, 1x Philips Hue System

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •