No UDP response from bulb

Hi LIFX,

I’m trying to get a UDP response from a bulb on my network.
I can see the responses going out of my app and I compared them to the lifx app request with wireshark but I can’t see any response from the bulb for either app.

The docs say the payload doesn’t matter but it’s the only difference in my UDP request and the LIFX app. Although I could have missed something else, I was using the LIFXKit as a reference.

Here’s some code to look at

import Foundation
import CocoaAsyncSocket

class LifxUDPManager : BulbProtocol, GCDAsyncUdpSocketDelegate {
var socket:GCDAsyncUdpSocket!
var connected:Bool = false

func connect(){
if(connected == false) {
var error : NSError?
socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
socket.bindToPort(56700, error: &error)
socket.beginReceiving(&error)
socket.enableBroadcast(true,error: &error)
connected = true
}
}

func play(lightSequence:LightSequence)
{
let message = "ping"
let data = message.dataUsingEncoding(NSUTF8StringEncoding)
socket.sendData(data, toHost: “255.255.255.255”, port: 56700, withTimeout: -1, tag: 1)
println(“scan”)
}

}

Thanks,
Kate

I’ve found the bulb response in wireshark. It responds to the udp requests from the lifx app but not the ones from my app. The only difference in the requests that I can see is the payload and the source port.

I found the problems, it’s a two part answer
1 I thought the payload mentioned in the docs was the request data, I copied an existing request message from wireshark and was able to start getting responses
let bytes:[UInt8] = [0x24, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]

2 swift doesn’t keep a strong reference to the delegate so my delegate methods weren’t called once I got a response happening, see

So everything is working now? If you like I can summon one of our iOS developers to help you out?

Yes thanks, it’s working now. This post was extremely useful Building a LIFX packet

If one of your iOS developer are handy I would be interested to know what data type they use to manually create a byte that contains several fields, I’m doing a few string to decimal conversions which doesn’t seem that efficient. I’d also like to know if there is an automatic way to handle little endian as I’m manually switching the bytes around at the moment.

Glad to hear that post helped you.

I’m not an IOS developer, but if you want to create a datatype that squeezes multiple field into a byte you should be able to use C structs. You can specify bit lengths for each field. Here is an example from our public iOS SDK:

#pragma pack(push, 1)
typedef struct lx_frame_t {
  #define LX_FRAME_FIELDS   \
    uint16_t size;          \
    uint16_t protocol:12;   \
    uint8_t  addressable:1; \
    uint8_t  tagged:1;      \
    uint8_t  origin:2;      \
    uint32_t source;

  LX_FRAME_FIELDS
} lx_frame_t;
#pragma pack(pop)

You can then use read the raw data from this structure by giving it as a pointer argument to functions that take bytes. You’ll also be able to calculate its size using sizeof(). This should work in both C and Objective C.

The best bit about doing it this way is that the iPhone and Intel CPUs are actually little endian by default. So there is no need to convert the endianness it will be stored in memory the correct way.

Cool thanks! I’ll look into it.