int main()
{
    struct timespec timeout;

    OIC_LOG(DEBUG, TAG, "OCServer is starting...");

    // Initialize Persistent Storage for SVR database
    OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};

    OCRegisterPersistentStorageHandler(&ps);

    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /*
     * Declare and create the example resource: LED
     */
    createLEDResource(gResourceUri, &LED, false, 0);

    timeout.tv_sec  = 0;
    timeout.tv_nsec = 100000000L;

    // Break from loop with Ctrl-C
    OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }
        nanosleep(&timeout, NULL);
    }

    OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 2
0
OCStackResult InitProvisioningHandler() {
    OCStackResult ret = OC_STACK_ERROR;
    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
        OIC_LOG(ERROR, ES_PROV_TAG, "OCStack init error");
        return ret;
    }


    pthread_t thread_handle;

    if (pthread_create(&thread_handle, NULL, listeningFunc, NULL)) {
        OIC_LOG(DEBUG, ES_PROV_TAG, "Thread creation failed");
        return OC_STACK_ERROR;
    }

    return OC_STACK_OK;
}
Exemplo n.º 3
0
int main()
{
    OIC_LOG(DEBUG, TAG, "OCServer is starting...");
    SetPersistentHandler(&ps);
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /*
     * Declare and create the example resource: Light
     */
    createLightResource(gResourceUri, &Light);

    CASelectCipherSuite(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);

    struct timespec timeout;
    timeout.tv_sec  = 0;
    timeout.tv_nsec = 100000000L;

    // Break from loop with Ctrl-C
    OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        nanosleep(&timeout, NULL);
    }

    OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 4
0
//The setup function is called once at startup of the sketch
void setup()
{
    // Add your initialization code here
    // Note : This will initialize Serial port on Arduino at 115200 bauds
   	OIC_LOG_INIT();
    OIC_LOG(DEBUG, TAG, ("OCServer is starting..."));


    // Connect to Ethernet or WiFi network
    if (ConnectToNetwork() != 0)
    {
    	Serial.print("Unable to connect to Network");
        OIC_LOG(ERROR, TAG, ("Unable to connect to network"));
        return;
    }

    // Initialize the OC Stack in Server mode
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, ("OCStack init error"));
        return;
    }

    // DEBUG PIN
    pinMode(LED_PIN, OUTPUT);

    // Button resource
    OCBaseResourceT *buttonResource = createResource("/a/button", OIC_DEVICE_BUTTON, OC_RSRVD_INTERFACE_DEFAULT,
                                                      (OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW), buttonIOHandler);

    buttonResource->name = "Marks Button";

    OCIOPort port;
    port.pin = TEST_BUT_PIN;
    port.type = IN;

    ResourceData data;
    data.b = false;
    addAttribute(&buttonResource->attribute, "state", data, BOOL, &port);

    printResource(buttonResource);
}
Exemplo n.º 5
0
//The setup function is called once at startup of the sketch
void setup()
{
    Serial.begin(115200);

    // Add your initialization code here
    OC_LOG_INIT();

    OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
    //    uint16_t port = OC_WELL_KNOWN_PORT;

    // Connect to Ethernet or WiFi network
    if (ConnectToNetwork() != 0)
    {
        OC_LOG(ERROR, TAG, "Unable to connect to network");
        return;
    }

    // Initialize the OC Stack in Server mode
    if (OCInit(NULL, OC_WELL_KNOWN_PORT, OC_SERVER) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, PCF("OCStack init error"));
        return;
    }

    OCStartPresence(60);
    // Declare and create the example resource
    createResource();

    // This call displays the amount of free SRAM available on Arduino
    PrintArduinoMemoryStats();
#if (ARDUINO == 0)
    ble.init( (long)115200, BLE_MASTER, trackeeID);
#elif (ARDUINO == 1)
    ble.init( (long)115200, BLE_SLAVER, slaveList[0]);
#elif (ARDUINO == 2)
    ble.init( (long)115200, BLE_SLAVER, slaveList[1]);
#endif

//  ble.StatusRead();

    OC_LOG_V(INFO, TAG, "Program Start-\r\n");
}
OCStackResult InitProvisioningHandler() {
    OCStackResult ret = OC_STACK_ERROR;
    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return ret;
    }


    char *string = "listeningFunc invoked in a thread";

    pthread_t thread_handle;

    if (pthread_create(&thread_handle, NULL, listeningFunc, (void *) string)) {
        OIC_LOG(DEBUG, TAG, "Thread creation failed");
        return OC_STACK_ERROR;
    }

    return OC_STACK_OK;
}
Exemplo n.º 7
0
int main()
{
    printf("OCResourceDirectory is starting...\n");
    OCStackResult result = OCInit(NULL, 0, OC_CLIENT_SERVER);
    if (result != OC_STACK_OK)
    {
        printf("Failed starting RD server ...\n");
        return 0;
    }
    if (OCRDStart() != OC_STACK_OK)
    {
        printf("OCRDStart failed...\n");
        return 0;
    }

    printf("OCRDStart successfully...\n");

    signal(SIGINT, handleSigInt);
    while (!g_quitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OCRDStop();
            printf("OCStack process error\n");
            return 0;
        }
    }

    if (OCRDStop() != OC_STACK_OK)
    {
        printf("OCRDStop failed...\n");
    }
    else
    {
        printf("OCRDStop success...\n");
    }

    printf("Exiting OCResourceDirectory main loop...\n");
    return 0;

}
Exemplo n.º 8
0
InProcClientWrapper::InProcClientWrapper(
    std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
    : m_threadRun(false), m_csdkLock(csdkLock),
      m_cfg { cfg }
{
    // if the config type is server, we ought to never get called.  If the config type
    // is both, we count on the server to run the thread and do the initialize

    if(m_cfg.mode == ModeType::Client)
    {
        OCStackResult result = OCInit(m_cfg.ipAddress.c_str(), m_cfg.port, OC_CLIENT);

        if(OC_STACK_OK != result)
        {
            throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
        }

        m_threadRun = true;
        m_listeningThread = std::thread(&InProcClientWrapper::listeningFunc, this);
    }
}
Exemplo n.º 9
0
//The setup function is called once at startup of the sketch
void setup()
{
    // Add your initialization code here
    // Note : This will initialize Serial port on Arduino at 115200 bauds
   // OC_LOG_INIT();
   
    Serial.begin(115200);
    Serial1.begin(115200);
  
  //  OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
//    OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));
	
    // Connect to Ethernet or WiFi network
   
     if (ConnectToNetwork() != 0)
    {
     //   OC_LOG(ERROR, TAG, PCF("Unable to connect to network"));
        return;
    }
	
    // Initialize the OC Stack in Server mode
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
     //   OC_LOG(ERROR, TAG, PCF("OCStack init error"));
        return;
    }
	
    // Declare and create the example resource: Light
    createLightResource();
    
    speaker.state = 0;
    speaker.volume = 40;
    speaker.next_song = 1;
    speaker.present_song = 1;
    speaker.time = 0;
    
    
    
    //OC_LOG(DEBUG, TAG, PCF("소라 놋북 터진 날 15.07.31 ..."));
}
Exemplo n.º 10
0
int main() {
    OIC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);

    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /* Start a discovery query*/
    char szQueryUri[64] = { 0 };
    strcpy(szQueryUri, OC_EXPLICIT_DEVICE_DISCOVERY_URI);
    if (OCDoResource(NULL, OC_REST_GET, szQueryUri, 0, 0, OC_LOW_QOS,
                     0, 0, 0) != OC_STACK_OK) {
        OIC_LOG(ERROR, TAG, "OCStack resource error");
        return 0;
    }

    // Break from loop with Ctrl+C
    OIC_LOG(INFO, TAG, "Entering occlient main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag) {

        if (OCProcess() != OC_STACK_OK) {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(1);
    }

    OIC_LOG(INFO, TAG, "Exiting occlient main loop...");

    if (OCStop() != OC_STACK_OK) {
        OIC_LOG(ERROR, TAG, "OCStack stop error");
    }

    return 0;
}
Exemplo n.º 11
0
int
main(int argc, char** argv) {
    uint8_t 	addr[20] = {0};
    uint8_t* 	paddr = NULL;
    uint16_t    port = USE_RANDOM_PORT;
    uint8_t 	ifname[] = "wlan0";
    int 		opt;

    printf ("OCClient is starting...\n");
    if (OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
                              sizeof(addr)) == ERR_SUCCESS) {
        printf ("Starting occlient on address %s\n",addr);
        paddr = addr;
    }

    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
        printf ("OCStack init error\n");
        return 0;
    }

    InitDiscovery ();

    signal(SIGINT, handleSigInt);
    while (!gQuitFlag) {
        if (OCProcess() != OC_STACK_OK) {
            printf ("OCStack process error\n");
            return 0;
        }

        sleep(2);
    }

    if (OCStop() != OC_STACK_OK) {
        printf ("OCStack process error\n");
    }

    return 0;
}
int main(int /*argc*/, char* /*argv*/[])
{
    OC_LOG(DEBUG, TAG, "OCServer is starting...");
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /*
     * Declare and create the example resource: LED
     */
    createLEDResource(gResourceUri, &LED, false, 0);

    // Break from loop with Ctrl-C
    OC_LOG(INFO, TAG, "Entering ocserver main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    }

    OC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 13
0
int main() {
    OIC_LOG_V(INFO, TAG, "Starting ocserver");
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /*
     * Declare and create the example resource: Light
     */
    if(createLightResource() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack cannot create resource...");
    }

    // Break from loop with Ctrl-C
    OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag) {

        if (OCProcess() != OC_STACK_OK) {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(1);
    }

    OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK) {
        OIC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 14
0
//The setup function is called once at startup of the sketch
void setup()
{
    // Add your initialization code here
    // Note : This will initialize Serial port on Arduino at 115200 bauds
    OC_LOG_INIT();
    OC_LOG(DEBUG, TAG, PCF("OCServer is starting..."));

    // Connect to Ethernet or WiFi network
    if (ConnectToNetwork() != 0)
    {
        OC_LOG(ERROR, TAG, PCF("Unable to connect to network"));
        return;
    }

    // Initialize the OC Stack in Server mode
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, PCF("OCStack init error"));
        return;
    }

    // Declare and create the example resource: Light
    createLightResource();
}
Exemplo n.º 15
0
int main(int argc, char* argv[])
{
    int opt;
    resourceList = NULL;
    while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
    {
        switch(opt)
        {
            case 'u':
                UNICAST_DISCOVERY = atoi(optarg);
                break;
            case 't':
                TEST_CASE = atoi(optarg);
                break;
            case 'c':
                // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
                // OC_CONNTYPE = OCConnectivityType(atoi(optarg));
                OC_CONNTYPE = OC_IPV4;
                OC_LOG(INFO, TAG, "Using default IPv4, IPv6 not currently supported.");
                break;
            default:
                PrintUsage();
                return -1;
        }
    }

    if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
            (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
    {
        PrintUsage();
        return -1;
    }

    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    InitDiscovery();

    // Break from loop with Ctrl+C
    signal(SIGINT, handleSigInt);

    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }
        sleep(2);
    }

    freeResourceList();
    OC_LOG(INFO, TAG, "Exiting occlient main loop...");
    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack stop error");
    }
    return 0;
}
Exemplo n.º 16
0
// function(s) for provisioning client using C-level provisioning API
static int initProvisionClient(void)
{
    // initialize persistent storage for SVR DB
    static OCPersistentStorage pstStr =
    {
        .open = fopen_prvnMng,
        .read = fread,
        .write = fwrite,
        .close = fclose,
        .unlink = unlink
    };
    if(OC_STACK_OK != OCRegisterPersistentStorageHandler(&pstStr))
    {
        OIC_LOG(ERROR, TAG, "OCRegisterPersistentStorageHandler error");
        return -1;
    }

    // initialize OC stack and provisioning manager
    if(OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER))
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return -1;
    }

    if (access(PRVN_DB_FILE_NAME, F_OK) != -1)
    {
        printf("************************************************************\n");
        printf("************Provisioning DB file already exists.************\n");
        printf("************************************************************\n");
    }
    else
    {
        printf("*************************************************************\n");
        printf("************No provisioning DB file, creating new************\n");
        printf("*************************************************************\n");
    }

    if(OC_STACK_OK != OCInitPM(PRVN_DB_FILE_NAME))
    {
        OIC_LOG(ERROR, TAG, "OC_PM init error");
        return -1;
    }

    // register callback function(s) to each OxM
    OTMCallbackData_t otmcb =
    {
        .loadSecretCB = LoadSecretJustWorksCallback,
        .createSecureSessionCB = CreateSecureSessionJustWorksCallback,
        .createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload,
        .createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload
    };
    if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb))
    {
        OIC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_JUST_WORKS");
        return -1;
    }
    otmcb.loadSecretCB = InputPinCodeCallback;
    otmcb.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
    otmcb.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
    otmcb.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
    if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &otmcb))
    {
        OIC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_RANDOM_DEVICE_PIN");
        return -1;
    }
    SetInputPinCB(inputPinCB);

    return 0;
}
Exemplo n.º 17
0
/* Delete all the items in the container and initialize it.
Caller is responsible for deleting any pointed-to data first. */
void OC_clear(struct Ordered_container* c_ptr) {
	g_Container_items_allocated -= c_ptr->allocation;
	g_Container_items_in_use -= c_ptr->size;
	free(c_ptr->array);
	OCInit(c_ptr);
}
Exemplo n.º 18
0
int main()
{
    OIC_LOG(INFO, TAG, "Initializing IoTivity...");
    OCStackResult result = OCInit(NULL, 0, OC_SERVER);
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "OCInit Failed %d", result);
        return -1;
    }

    result = SetPlatformInfo();
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed %d", result);
        goto IotivityStop;
    }

    result  = SetDeviceInfo();
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed: %d", result);
        goto IotivityStop;
    }

    result  = OCStartPresence(0);
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
        goto IotivityStop;
    }

    // PIStartPlugin
    PIPlugin* plugin = NULL;
    OIC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
    result = PIStartPlugin(defaultComPort, PLUGIN_ZIGBEE, &plugin);
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
        goto IotivityStop;
    }

    if (signal(SIGINT, processCancel) == SIG_ERR)
    {
        OIC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
    }
    else
    {
        OIC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
        // Loop until sigint
        while (!processSignal(false) && result == OC_STACK_OK)
        {
            result = OCProcess();
            if (result != OC_STACK_OK)
            {
                OIC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
                break;
            }

            result = PIProcess(plugin);
            if (result != OC_STACK_OK)
            {
                OIC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
            }
        }
    }

    OIC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
    result = PIStopPlugin(plugin);
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
    }
    OIC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
    // OCStop
IotivityStop:
    OIC_LOG(INFO, TAG, "Stopping IoTivity...");
    result = OCStop();
    if (result != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
        return 0;
    }

    OIC_LOG(INFO, TAG, "Application Completed Successfully");
    return 0;
}
Exemplo n.º 19
0
/**
 * Provisioning client sample using ProvisioningAPI
 */
int main()
{
    OCStackResult res = OC_STACK_OK;
    int unused;
    (void)unused;

    // Initialize Persistent Storage for SVR database
    OCPersistentStorage ps = { .open = NULL,
                               .read = NULL,
                               .write = NULL,
                               .close = NULL,
                               .unlink = NULL };
    ps.open = client_fopen;
    ps.read = fread;
    ps.write = fwrite;
    ps.close = fclose;
    ps.unlink = unlink;
    OCRegisterPersistentStorageHandler(&ps);

    if (OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER))
    {
        OC_LOG(ERROR, TAG, "OCStack init error");
        goto error;
    }

    OCProvisionDev_t* pDeviceList = NULL;
    res = OCDiscoverUnownedDevices(PREDEFINED_TIMEOUT, &pDeviceList);
    if(OC_STACK_OK != res)
    {
        OC_LOG_V(ERROR, TAG, "Failed to PMDeviceDiscovery : %d", res);
        goto error;
    }

    OCProvisionDev_t* pCurDev = pDeviceList;
    int i;
    while(pCurDev !=NULL)
    {
        for(i = 0; i < UUID_LENGTH; i++)
            printf("%c", pCurDev->doxm->deviceID.id[i]);
        printf("\n");
        pCurDev = pCurDev->next;
    }

    //Register callback function to each OxM
    OTMCallbackData_t justWorksCBData = {.loadSecretCB=NULL,
                                         .createSecureSessionCB=NULL,
                                         .createSelectOxmPayloadCB=NULL,
                                         .createOwnerTransferPayloadCB=NULL};
    justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
    justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
    justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
    justWorksCBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
    OTMSetOwnershipTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData);

    OTMCallbackData_t pinBasedCBData = {.loadSecretCB=NULL,
                                         .createSecureSessionCB=NULL,
                                         .createSelectOxmPayloadCB=NULL,
                                         .createOwnerTransferPayloadCB=NULL};
    pinBasedCBData.loadSecretCB = InputPinCodeCallback;
    pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
    pinBasedCBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
    pinBasedCBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
    OTMSetOwnershipTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &pinBasedCBData);

    SetInputPinCB(&InputPinCB);

    char* myContext = "OTM Context";
    //Perform ownership transfer
    res = OCDoOwnershipTransfer((void*)myContext, pDeviceList, OwnershipTransferCB);
    if(OC_STACK_OK == res)
    {
        OC_LOG(INFO, TAG, "Request for ownership transfer is sent successfully.");
    }
    else
    {
        OC_LOG_V(ERROR, TAG, "Failed to OCDoOwnershipTransfer : %d", res);
    }

    gOwnershipState = 0;
    while ( gOwnershipState == 0 )
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            goto error;
        }
        sleep(1);
    }

// Credential & ACL provisioning between two devices.

    OCProvisionDev_t *pOwnedList = NULL;
    OCProvisionDev_t *pOwnedDevices [MAX_OWNED_DEVICE] = {0,};
    int nOwnedDevice = 0;

    res = OCDiscoverOwnedDevices(PREDEFINED_TIMEOUT, &pOwnedList);
    if (OC_STACK_OK == res)
    {
        printf("################## Owned Device List #######################\n");
        while (pOwnedList != NULL)
        {
            nOwnedDevice ++;
            printf(" %d : ", nOwnedDevice);
            for (int i = 0; i < UUID_LENGTH; i++)
            {
                printf("%c", pOwnedList->doxm->deviceID.id[i]);
            }
            printf("\n");
            pOwnedDevices[nOwnedDevice] = pOwnedList;
            pOwnedList = pOwnedList->next;
        }
    }
    else
    {
        OC_LOG(ERROR, TAG, "Error while Owned Device Discovery");
    }

    int Device1 = 0;
    int Device2 = 0;

    printf("Select 2 devices for Credential & ACL provisioning\n");
    printf("Device 1: ");
    unused = scanf("%d", &Device1);
    printf("Device 2: ");
    unused = scanf("%d", &Device2);


    gAcl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
    if (NULL == gAcl1)
    {
        OC_LOG(ERROR, TAG, "Error while memory allocation");
        goto error;
    }

    gAcl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
    if (NULL == gAcl2)
    {
        OC_LOG(ERROR, TAG, "Error while memory allocation");
        goto error;
    }

    printf("Input ACL for Device1\n");
    if ( 0 == InputACL(gAcl1))
    {
        printf("Success Input ACL\n");
    }
    else
    {
        OC_LOG(ERROR, TAG, "InputACL error");
        goto error;
    }

    printf("Input ACL for Device2\n");
    if (0 == InputACL(gAcl2))
    {
        printf("Success Input ACL\n");
    }
    else
    {
        OC_LOG(ERROR, TAG, "InputACL error");
        goto error;
    }
    char *ctx = "DUMMY";
    OCProvisionPairwiseDevices(ctx,SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_128, pOwnedDevices[Device1],
                           gAcl1, pOwnedDevices[Device2], gAcl2, ProvisionPairwiseCB);

    gOwnershipState = 0;
    while ( gOwnershipState == 0 )
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            goto error;
        }
        sleep(1);
    }

    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack process error");
        goto error;
    }

error:
    deleteACL(gAcl1);
    deleteACL(gAcl2);
    OCDeleteDiscoveredDevices(&pDeviceList);
    OCDeleteDiscoveredDevices(&pOwnedList);

    return 0;
}
Exemplo n.º 20
0
int main(int argc, char* argv[])
{
    char host[] = "localhost";
    char user[] = "test1";
    char pass[] = "intel123";
    char empstr[] = "";
    OCRAInfo_t rainfo = {};

    rainfo.hostname = host;
    rainfo.port = 5222;
    rainfo.xmpp_domain = host;
    rainfo.username = user;
    rainfo.password = pass;
    rainfo.resource = empstr;
    rainfo.user_jid = empstr;
    rainfo.jidbound = jidbound;

    int opt = 0;
    while ((opt = getopt(argc, argv, "t:s:p:d:u:w:r:j:")) != -1)
    {
        switch(opt)
        {
            case 't':
                TEST_CASE = atoi(optarg);
                break;
            case 's':
                rainfo.hostname = optarg;
                break;
            case 'p':
                rainfo.port = atoi(optarg);
                break;
            case 'd':
                rainfo.xmpp_domain = optarg;
                break;
            case 'u':
                rainfo.username = optarg;
                break;
            case 'w':
                rainfo.password = optarg;
                break;
            case 'j':
                rainfo.user_jid = optarg;
                break;
            case 'r':
                rainfo.resource = optarg;
                break;
            default:
                PrintUsage();
                return -1;
        }
    }

    if (OCSetRAInfo(&rainfo) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "Error initiating remote access adapter");
        return 0;
    }

    if ((TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS))
    {
        PrintUsage();
        return -1;
    }

    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    OIC_LOG(INFO, TAG, "Enter JID of remote server");
    if (fgets(remoteServerJabberID, MAX_ADDR_STR_SIZE, stdin))
    {
        StripNewLineChar(remoteServerJabberID);
    }
    else
    {
        OIC_LOG(ERROR, TAG, "Bad input for jabberID");
        return OC_STACK_INVALID_PARAM;
    }

    InitDiscovery(OC_LOW_QOS);

    // Break from loop with Ctrl+C
    OIC_LOG(INFO, TAG, "Press CTRL+C to stop the stack");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    }
    OIC_LOG(INFO, TAG, "Exiting ocremoteaccessclient main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack stop error");
    }

    return 0;
}
Exemplo n.º 21
0
int main(int argc, char* argv[])
{
    uint8_t addr[20] = {0};
    uint8_t* paddr = NULL;
    uint16_t port = USE_RANDOM_PORT;
    uint8_t ifname[] = "eth0";
    int opt;

    while ((opt = getopt(argc, argv, "u:t:")) != -1)
    {
        switch(opt)
        {
            case 'u':
                UNICAST_DISCOVERY = atoi(optarg);
                break;
            case 't':
                TEST_CASE = atoi(optarg);
                break;
            default:
                PrintUsage();
                return -1;
        }
    }

    if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
            (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
    {
        PrintUsage();
        return -1;
    }


    /*Get Ip address on defined interface and initialize coap on it with random port number
     * this port number will be used as a source port in all coap communications*/
    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
                sizeof(addr)) == ERR_SUCCESS)
    {
        OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
        paddr = addr;
    }

    /* Initialize OCStack*/
    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    InitDiscovery();

    // Break from loop with Ctrl+C
    OC_LOG(INFO, TAG, "Entering occlient main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    }
    OC_LOG(INFO, TAG, "Exiting occlient main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack stop error");
    }

    return 0;
}
Exemplo n.º 22
0
int main(int argc, char* argv[])
{
    int opt;

    while ((opt = getopt(argc, argv, "u:t:c:")) != -1)
    {
        switch(opt)
        {
            case 'u':
                UNICAST_DISCOVERY = atoi(optarg);
                break;
            case 't':
                TEST_CASE = atoi(optarg);
                break;
            case 'c':
                OC_CONNTYPE = OCConnectivityType(atoi(optarg));
                break;
            default:
                PrintUsage();
                return -1;
        }
    }

    if ((UNICAST_DISCOVERY != 0 && UNICAST_DISCOVERY != 1) ||
            (TEST_CASE < TEST_DISCOVER_REQ || TEST_CASE >= MAX_TESTS) )
    {
        PrintUsage();
        return -1;
    }

    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }
    if (UNICAST_DISCOVERY)
    {
        printf("Enter IPv4 address of the Server hosting resource (Ex: 192.168.0.15)\n");
        if (fgets(ipv4addr, IPV4_ADDR_SIZE, stdin))
        {
            //Strip newline char from ipv4addr
            StripNewLineChar(ipv4addr);
        }
        else
        {
            OC_LOG(ERROR, TAG, "!! Bad input for IPV4 address. !!");
            return OC_STACK_INVALID_PARAM;
        }
     }

    if(UNICAST_DISCOVERY  == 0  && TEST_CASE == TEST_DISCOVER_DEV_REQ)
    {
        InitDeviceDiscovery(OC_LOW_QOS);
    }
    else
    {
        InitDiscovery(OC_LOW_QOS);
    }

    // Break from loop with Ctrl+C
    OC_LOG(INFO, TAG, "Entering occlient main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {

        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    }
    OC_LOG(INFO, TAG, "Exiting occlient main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack stop error");
    }

    return 0;
}
Exemplo n.º 23
0
int main(int argc, char* argv[])
{
    uint8_t addr[20] = {0};
    uint8_t* paddr = NULL;
    uint16_t port = OC_WELL_KNOWN_PORT;
    uint8_t ifname[] = "eth0";


    OC_LOG(DEBUG, TAG, "OCServer is starting...");
    /*Get Ip address on defined interface and initialize coap on it with random port number
     * this port number will be used as a source port in all coap communications*/
    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
                sizeof(addr)) == ERR_SUCCESS)
    {
        OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
        paddr = addr;
    }

    if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /*
     * Declare and create the example resource: LED
     */
    createLEDResource(gResourceUri, &LED, false, 0);

    // Initialize slow response alarm
    signal(SIGALRM, AlarmHandler);

    // Break from loop with Ctrl-C
    OC_LOG(INFO, TAG, "Entering ocserver main loop...");
    signal(SIGINT, handleSigInt);

    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    }

    OC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    // Free requests
    if (!gRequestList.empty())
    {
        for (auto iter = gRequestList.begin(); iter != gRequestList.end(); ++iter)
        {
            OCFree((*iter)->query);
            OCFree((*iter)->reqJSONPayload);
            OCFree(*iter);
        }
        gRequestList.clear();
    }

    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 24
0
int main(int argc, char* argv[])
{
    uint8_t addr[20] = {0};
    uint8_t* paddr = NULL;
    uint16_t port = 0;
    uint8_t ifname[] = "eth0";
    pthread_t threadId;
    int opt;

    while ((opt = getopt(argc, argv, "t:")) != -1)
    {
        switch(opt)
        {
        case 't':
            TEST = atoi(optarg);
            break;
        default:
            PrintUsage();
            return -1;
        }
    }
    if(TEST <= TEST_INVALID || TEST >= MAX_TESTS){
        PrintUsage();
        return -1;
    }

    OC_LOG(DEBUG, TAG, "OCServer is starting...");
    /*Get Ip address on defined interface and initialize coap on it with random port number
     * this port number will be used as a source port in all coap communications*/
    if (OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
                               sizeof(addr)) == ERR_SUCCESS)
    {
        OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
        paddr = addr;
    }

    if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    /*
     * Declare and create the example resource: light
     */
    createResources();

    /*
     * Create a thread for changing the representation of the light
     */
    pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);

    // Break from loop with Ctrl-C
    OC_LOG(INFO, TAG, "Entering ocserver main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }
        sleep(2);
    }

    /*
     * Cancel the light thread and wait for it to terminate
     */
    pthread_cancel(threadId);
    pthread_join(threadId, NULL);

    OC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 25
0
int main(int argc, char* argv[])
{
    int opt;
    struct timespec timeout;
    OCPersistentStorage ps;

    while ((opt = getopt(argc, argv, "u:t:c:d:p:")) != -1)
    {
        switch(opt)
        {
            case 'u':
                UnicastDiscovery = atoi(optarg);
                break;
            case 't':
                TestCase = atoi(optarg);
                break;
            case 'c':
                ConnType = atoi(optarg);
                break;
            case 'd':
                DevOwner = atoi(optarg);
                break;
            case 'p':
            {
                WithTcp = atoi(optarg);
                if(WithTcp > 1)
                {
                    PrintUsage();
                    return -1;
                }
            }
                break;
            default:
                PrintUsage();
                return -1;
        }
    }

    if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) ||
            (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS)||
            (ConnType < CT_ADAPTER_DEFAULT || ConnType >= MAX_CT))
    {
        PrintUsage();
        return -1;
    }


    if(ConnType == CT_ADAPTER_DEFAULT || ConnType ==  CT_IP)
    {
        discoveryReqConnType = CT_DEFAULT;
    }
    else
    {
        OIC_LOG(INFO, TAG, "Using Default Connectivity type");
        PrintUsage();
    }


    // Initialize Persistent Storage for SVR database
    if (DevOwner)
        ps = { client_fopen_devowner, fread, fwrite, fclose, unlink };
    else
        ps = { client_fopen_nondevowner, fread, fwrite, fclose, unlink };
    OCRegisterPersistentStorageHandler(&ps);

    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    InitDiscovery();

    timeout.tv_sec  = 0;
    timeout.tv_nsec = 100000000L;

    // Break from loop with Ctrl+C
    OIC_LOG(INFO, TAG, "Entering occlient main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        nanosleep(&timeout, NULL);
    }
    OIC_LOG(INFO, TAG, "Exiting occlient main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack stop error");
    }

    return 0;
}
Exemplo n.º 26
0
int main( int argc, char* argv[] )
{
    uint8_t addr[20] = {0};
    uint8_t* paddr = NULL;
    uint16_t port = USE_RANDOM_PORT;
    xmpp_ctx_t *ctx = NULL;
    xmpp_conn_t* conn = NULL;    
    int reg_act = DMC_REGISTER;
    int errornum=0;
    void *ptr;

    pthread_attr_init(&attr1);
    pthread_attr_setstacksize(&attr1, PTHREAD_STACK_MIN * 2);
   	
    if(!setPIN("2345678901"))
    { 	
	fprintf(stderr, "PIN read error\n");
	return 1;
    }	
    else
	printf("\t\tRead PIN : %s\n", getPIN());

    if( argc < 2 )
    {
	exit(-1);
    }
    uint8_t *ifname = (uint8_t *)argv[1];
	
    /*Get Ip address on defined interface and initialize coap on it with random port number
     * this port number will be used as a source port in all coap communications*/
    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
                sizeof(addr)) == ERR_SUCCESS)
    {
        OC_LOG_V(INFO, TAG, "Starting occlient on address %s",addr);
        paddr = addr;
    }

    /* Initialize OCStack*/
    if (OCInit((char *) paddr, port, OC_CLIENT) != OC_STACK_OK) {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    time(&last_discovery_time);
    InitDiscovery();
//    conn = XMPP_Init( xmpp_un , xmpp_pw ,  xmpp_server, &ctx);
       
    // Break from loop with Ctrl+C
    OC_LOG(INFO, TAG, "Entering occlient main loop...");

    signal(SIGINT, handleSigInt);

    while (!gQuitFlag) 
	{	
		
		if( cmd_head != NULL )
		{
			switch( cmd_head->command )
			{
				case COMMAND_DISCOVERY:
					InitDiscovery();
					break;
				case COMMAND_GET:
					InitGetRequest(cmd_head);
					break;
				case COMMAND_PUT:
					InitPutRequest(cmd_head);
					break;
				case COMMAND_POST:
                                        InitPutRequest(cmd_head);
                                        break;
				case COMMAND_OBSERVE:
					break;
				default:
					break;
			}
		
			struct cmd *cmd_temp = cmd_head;
			cmd_head = cmd_head->next;
			free(cmd_temp);
		}
		else
		{
			//do discovery every 10 seconds
			time_t now;
			time(&now);
			if( now - last_discovery_time > DMC_INTERVAL )
			{
				struct dmc_result *res = dmc_discovery_finish(reg_act);                                			
				if( res->state == REG_SUCCEED )
				{
					reg_act = DMC_UPDATE;
					snprintf( xmpp_un, sizeof(xmpp_un), "%s", res->username );
					snprintf( xmpp_pw, sizeof(xmpp_pw), "%s", res->password );
					snprintf( xmpp_server, sizeof(xmpp_server), "%s", res->server );
					free( res );
					printf("%s\n%s\n%s\n", xmpp_server, xmpp_un, xmpp_pw);
					if(conn==NULL)
 					{ 

                                            conn = XMPP_Init( \
					    xmpp_un , xmpp_pw,
                                            xmpp_server, &ctx);
         
                                            if(ctx !=NULL)
                                                pthread_create(&pid1, \
				&attr1, (void*)xmpp_run, (void*)ctx );
                                            else 
						printf("ctx null\n");

					}
				}
				else
					free(res);	

				OC_LOG_V( INFO, TAG, "DM client returns state %s", get_dmcresult_char(res->state) );
				InitDiscovery();
				time(&last_discovery_time);
			}
		}

        if (OCProcess() != OC_STACK_OK) 
		{
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        usleep(200*1000);
    }
    
    printf("Quit\n");	
     
    OC_LOG(INFO, TAG, "Exiting occlient main loop...");
    
    if (OCStop() != OC_STACK_OK) {
        OC_LOG_V(ERROR, TAG, "OCStack stop error");
    }

    xmpp_stop(ctx);
    sleep(1);
    XMPP_Close(ctx, conn);
   

    if ((errornum = pthread_cancel(pid1)) != 0)
      	OC_LOG_V(INFO, TAG, "pthread_cancel: %s", strerror(errornum));
    if ((errornum = pthread_join(pid1, &ptr)) != 0)
	OC_LOG_V(INFO, TAG, "pthread_join: %s", strerror(errornum));


    printf("Done\n");
    return 0;
}
Exemplo n.º 27
0
int main(int argc, char* argv[])
{
    int opt;

    while ((opt = getopt(argc, argv, "t:c:")) != -1)
    {
        switch (opt)
        {
            case 't':
                TestType = atoi(optarg);
                break;
            case 'c':
                ConnectivityType = atoi(optarg);
                break;
            default:
                PrintUsage();
                return -1;
        }
    }
    if ((TestType <= TEST_INVALID || TestType >= MAX_TESTS) ||
        ConnectivityType >= MAX_CT)
    {
        PrintUsage();
        return -1;
    }

    /* Initialize OCStack*/
    if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }

    if(ConnectivityType == CT_ADAPTER_DEFAULT || ConnectivityType == CT_IP)
    {
        ConnType = CT_ADAPTER_IP;
    }
    else
    {
        OIC_LOG(INFO, TAG, "Default Connectivity type selected...");
        ConnType = CT_ADAPTER_IP;
    }

    InitDiscovery();

    // Break from loop with Ctrl+C
    OIC_LOG(INFO, TAG, "Entering occlient main loop...");
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag)
    {

        if (OCProcess() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    } OIC_LOG(INFO, TAG, "Exiting occlient main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack stop error");
    }

    return 0;
}
Exemplo n.º 28
0
int main(int argc, char* argv[])
{
    uint8_t addr[20] = {0};
    uint8_t* paddr = NULL;
    uint16_t port = OC_WELL_KNOWN_PORT;
    uint8_t ifname[] = "eth0";
    pthread_t threadId;
    pthread_t threadId_presence;
    int opt;

    while ((opt = getopt(argc, argv, "o:")) != -1)
    {
        switch(opt)
        {
            case 'o':
                gObserveNotifyType = atoi(optarg);
                break;
            default:
                PrintUsage();
                return -1;
        }
    }

    if ((gObserveNotifyType != 0) && (gObserveNotifyType != 1))
    {
        PrintUsage();
        return -1;
    }

    OC_LOG(DEBUG, TAG, "OCServer is starting...");
    /*Get Ip address on defined interface and initialize coap on it with random port number
     * this port number will be used as a source port in all coap communications*/
    if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
                sizeof(addr)) == ERR_SUCCESS)
    {
        OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
        paddr = addr;
    }

    if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK) {
        OC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }
#ifdef WITH_PRESENCE
    if (OCStartPresence(0) != OC_STACK_OK) {
        OC_LOG(ERROR, TAG, "OCStack presence/discovery error");
        return 0;
    }
#endif

    OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb);

    OCStackResult deviceResult = SetDeviceInfo(contentType, dateOfManufacture, deviceName,
            deviceUUID, firmwareVersion, hostName, manufacturerName,
            manufacturerUrl, modelNumber, platformVersion, supportUrl, version);

    if (deviceResult != OC_STACK_OK)
    {
        OC_LOG(INFO, TAG, "Device Registration failed!");
        exit (EXIT_FAILURE);
    }

    deviceResult = OCSetDeviceInfo(deviceInfo);

    if (deviceResult != OC_STACK_OK)
    {
        OC_LOG(INFO, TAG, "Device Registration failed!");
        exit (EXIT_FAILURE);
    }

    /*
     * Declare and create the example resource: Light
     */
    createLightResource(gResourceUri, &Light);

    // Initialize observations data structure for the resource
    for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
    {
        interestedObservers[i].valid = false;
    }

    /*
     * Create a thread for changing the representation of the Light
     */
    pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);

    /*
     * Create a thread for generating changes that cause presence notifications
     * to be sent to clients
     */
    pthread_create(&threadId_presence, NULL, presenceNotificationGenerator, (void *)NULL);

    // Break from loop with Ctrl-C
    OC_LOG(INFO, TAG, "Entering ocserver main loop...");
    DeleteDeviceInfo();
    signal(SIGINT, handleSigInt);
    while (!gQuitFlag) {
        if (OCProcess() != OC_STACK_OK) {
            OC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }

        sleep(2);
    }

    /*
     * Cancel the Light thread and wait for it to terminate
     */
    pthread_cancel(threadId);
    pthread_join(threadId, NULL);
    pthread_cancel(threadId_presence);
    pthread_join(threadId_presence, NULL);

    OC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK) {
        OC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
int main(int argc, char* argv[])
{

#ifdef RA_ADAPTER
    char host[] = "localhost";
    char user[] = "test1";
    char pass[] = "intel123";
    char empstr[] = "";
    OCRAInfo_t rainfo = {};

    rainfo.hostname = host;
    rainfo.port = 5222;
    rainfo.xmpp_domain = host;
    rainfo.username = user;
    rainfo.password = pass;
    rainfo.resource = empstr;
    rainfo.user_jid = empstr;
    rainfo.jidbound = jidbound;
#endif

    int opt = 0;
    while ((opt = getopt(argc, argv, "o:s:p:d:u:w:r:j:")) != -1)
    {
        switch(opt)
        {
            case 'o':
                gObserveNotifyType = atoi(optarg);
                break;
#ifdef RA_ADAPTER
            case 's':
                rainfo.hostname = optarg;
                break;
            case 'p':
                rainfo.port = atoi(optarg);
                break;
            case 'd':
                rainfo.xmpp_domain = optarg;
                break;
            case 'u':
                rainfo.username = optarg;
                break;
            case 'w':
                rainfo.password = optarg;
                break;
            case 'j':
                rainfo.user_jid = optarg;
                break;
            case 'r':
                rainfo.resource = optarg;
                break;
#endif
            default:
                PrintUsage();
                return -1;
        }
    }

    if ((gObserveNotifyType != 0) && (gObserveNotifyType != 1))
    {
        PrintUsage();
        return -1;
    }

#ifdef RA_ADAPTER
    OCSetRAInfo(&rainfo);
#endif

    OIC_LOG(DEBUG, TAG, "OCServer is starting...");

    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack init error");
        return 0;
    }
#ifdef WITH_PRESENCE
    if (OCStartPresence(0) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack presence/discovery error");
        return 0;
    }
#endif

    OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb, NULL);

    OCStackResult registrationResult =
        SetPlatformInfo(platformID, manufacturerName, manufacturerUrl, modelNumber,
            dateOfManufacture, platformVersion,  operatingSystemVersion,  hardwareVersion,
            firmwareVersion,  supportUrl, systemTime);

    if (registrationResult != OC_STACK_OK)
    {
        OIC_LOG(INFO, TAG, "Platform info setting failed locally!");
        exit (EXIT_FAILURE);
    }

    registrationResult = OCSetPlatformInfo(platformInfo);

    if (registrationResult != OC_STACK_OK)
    {
        OIC_LOG(INFO, TAG, "Platform Registration failed!");
        exit (EXIT_FAILURE);
    }

    registrationResult = SetDeviceInfo(deviceName, specVersion, dataModelVersions);

    if (registrationResult != OC_STACK_OK)
    {
        OIC_LOG(INFO, TAG, "Device info setting failed locally!");
        exit (EXIT_FAILURE);
    }

    OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");

    registrationResult = OCSetDeviceInfo(deviceInfo);

    if (registrationResult != OC_STACK_OK)
    {
        OIC_LOG(INFO, TAG, "Device Registration failed!");
        exit (EXIT_FAILURE);
    }

    /*
     * Declare and create the example resource: Light
     */
    createLightResource(gResourceUri, &Light);

    // Initialize observations data structure for the resource
    for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
    {
        interestedObservers[i].valid = false;
    }


    /*
     * Create a thread for generating changes that cause presence notifications
     * to be sent to clients
     */

    #ifdef WITH_PRESENCE
    pthread_create(&threadId_presence, NULL, presenceNotificationGenerator, (void *)NULL);
    #endif

    // Break from loop with Ctrl-C
    OIC_LOG(INFO, TAG, "Entering ocserver main loop...");

    DeletePlatformInfo();
    DeleteDeviceInfo();

    signal(SIGINT, handleSigInt);

    while (!gQuitFlag)
    {
        if (OCProcess() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, TAG, "OCStack process error");
            return 0;
        }
    }

    if (observeThreadStarted)
    {
        pthread_cancel(threadId_observe);
        pthread_join(threadId_observe, NULL);
    }

    pthread_cancel(threadId_presence);
    pthread_join(threadId_presence, NULL);

    OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");

    if (OCStop() != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, "OCStack process error");
    }

    return 0;
}
Exemplo n.º 30
0
//The setup function is called once at startup of the sketch
void setup()
{
    // Add your initialization code here
    // Note : This will initialize Serial port on Arduino at 115200 bauds
   	OIC_LOG_INIT();
    OIC_LOG(DEBUG, TAG, ("OCServer is starting..."));

    // mDNSAP
    pinMode(LED_PIN, OUTPUT);
    pinMode(TEST_LED_PIN, OUTPUT);
    pinMode(TEST_BUT_PIN, INPUT);
    digitalWrite(LED_PIN, LOW);
    digitalWrite(TEST_LED_PIN, LOW);

    // Connect to Ethernet or WiFi network
    if (ConnectToNetwork() != 0)
    {
    	Serial.print("Unable to connect to Network");
        OIC_LOG(ERROR, TAG, ("Unable to connect to network"));
        return;
    }

    // Initialize the OC Stack in Server mode
    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
    {
        OIC_LOG(ERROR, TAG, ("OCStack init error"));
        return;
    }


    OCBaseResourceT *newResource = createResource("/a/button", "oic.r.button", OC_RSRVD_INTERFACE_DEFAULT,
                                                  (OC_DISCOVERABLE | OC_OBSERVABLE), buttonIOHandler);

    newResource->name = "Marks Button";
/*
    OCIOPort port;
    port.pin = TEST_BUT_PIN;
    port.type = IN;

    Value value = malloc(sizeof(int));
    int intVal = 0;
    *((int*)value) = intVal;
    addAttribute(&newResource->attribute, "power", value, INT, &port);*/

    //printResourceData(newResource);

    OCBaseResourceT *resource = createResource("/a/light/hosting", "oic.r.light", OC_RSRVD_INTERFACE_DEFAULT,
                                               (OC_DISCOVERABLE | OC_OBSERVABLE), lightIOHandler);
    resource->name = "Mark's Light";

    addType(resource, "oic.r.switch.binary");
    addType(resource, "oic.r.light.brightness");
    addType(resource, "oic.r.resourcehosting");

    OCIOPort port;
    port.pin = TEST_LED_PIN;
    port.type = OUT;

    Value value = malloc(sizeof(bool));
    bool boolVal = false;
    *((bool*)value) = boolVal;
    addAttribute(&resource->attribute, "state", value, BOOL, &port);

    value = malloc(sizeof(int));
    int intVal = 0;
    *((int*)value) = intVal;
    addAttribute(&resource->attribute, "power", value, INT, &port);

    //printResourceData(resource);

 /*
    OCBaseResourceT *humidtyResource = createResource("/a/temperatureSensor", "oic.r.sensor", OC_RSRVD_INTERFACE_DEFAULT,
                                    (OC_DISCOVERABLE | OC_OBSERVABLE), NULL);
*/


    // Start the thread to take for change in the input of the resources
    Scheduler.startLoop(checkInputThread);

    // Alive LED
    Scheduler.startLoop(aliveThread);

   /* char *heapend=sbrk(0);
    register char * stack_ptr asm ("sp");
    struct mallinfo mi=mallinfo();
    OIC_LOG_V(DEBUG, TAG, "\nDynamic ram used: %d\n",mi.uordblks);
    OIC_LOG_V(DEBUG, TAG, "Program static ram used %d\n",&_end - ramstart);
    OIC_LOG_V(DEBUG, TAG, "Stack ram used %d\n\n",ramend - stack_ptr);
    OIC_LOG_V(DEBUG, TAG, "My guess at free mem: %d\n",stack_ptr - heapend + mi.fordblks);*/
}