#import "RootViewController.h"
#include "slp.h"

SLPBoolean MySLPSrvURLCallback(
                               SLPHandle hslp,
                               const char* srvurl,
                               unsigned short lifetime,
                               SLPError errcode,
                               void* cookie )
{
    if(errcode == SLP_OK || errcode == SLP_LAST_CALL)
    {
        printf("Service URL     = %s\n",srvurl);
        printf("Service Timeout = %i\n",lifetime);
        *(SLPError*)cookie = SLP_OK;
    }
    else
    {
        *(SLPError*)cookie = errcode;
    }
    
    /* return SLP_TRUE because we want to be called again */
    /* if more services were found                        */
    
    return SLP_TRUE;
}

int findDevicesUsingSLP()
{
    SLPError err;
    SLPError callbackerr;
    SLPHandle hslp;
    
    err = SLPOpen("en",SLP_FALSE,&hslp);
    
    if(err != SLP_OK)
    {
        printf("Error opening slp handle %i\n",err);
        return err;
    }
    
    err =
    SLPFindSrvs( hslp,
                "acn.esta",
                0,  /* use configured scopes */
                "",   /* no attr filter        */
                MySLPSrvURLCallback,                         
                &callbackerr );
    
    /* err may contain an error code that occurred as the slp library    */
    /* _prepared_ to make the call.                                      */
    
    if((err != SLP_OK) || (callbackerr != SLP_OK))
    {
        printf("Error registering service with slp %i\n",err);
        return err;
    }
    
    /* callbackerr may contain an error code (that was assigned through  */
    /* the callback cookie) that occurred as slp packets were sent on    */
    /* the wire */
    
    if( callbackerr != SLP_OK)
    {
        printf("Error registering service with slp %i\n",callbackerr);
        return callbackerr;
    }
    
    /* Now that we're done using slp, close the slp handle */
    SLPClose(hslp);
    
    /* rest of program */
    return err;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    findDevicesUsingSLP();
    
}