User Tools

Site Tools


initialize_setting

IoTivity Initialization and Setting

Stack Blocks

The Resource API stack consists of several thin layers of software. In unconstrained environments such as Linux*, Tizen*, Android*, iOS*, or Microsoft* Windows*, the stack provides APIs in C and C++ that allow developers to talk to both constrained and unconstrained devices via IP networks, with potential support for additional network protocols and wireless technologies.

Here is a basic overview of IoTivity architecture

The next diagram shows that OC Stack can be entered at different level using C or C++ functions :

  • (1) C++ SDK : OCPlatform::Configure
  • (2) C SDK : OCInit

Setup

First, if you haven't built IoTivity for your OS, please build IoTivity from sources before continuing this guide.

C SDK

The base layer of IoTivity is a C library.

Example

Let's study a basic example to initialize :

iotivity/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp

Even if this code is in C++, it will only call the C API, it does much more than initialization, but this guide will only focus on few OC functions.

API

Here are the relevant functions.

From iotivity/resource/csdk/stack/include/ocstack.h:

  • OCInit - This function initializes the OC stack, and it's called prior to starting the stack.
  • OCProcess - This function allows low-level processing of stack services, and it's called in the main loop of the OC client or server.
  • OCStop - This function stops the OC stack and is used for a controlled shutdown.

Please refer to the CSDK API documentation for more information.

OCInit

OCInit can be used with default parameters:

Server:

if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) ...

Client:

if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) ...

This will call OCInit1 internally, so this call can be also replaced with OCInit1.

OCProcess

Once initialized you need to create a loop that calls OCProcess until stopped.

  while (!gQuitFlag)
  {
      if (OCProcess() != OC_STACK_OK)
      {
      ...
      }
   }

OCStop

Before quitting you need to stop OCStack :

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

Also note this “OIC_LOG” macro it's a logging facility that can be used (if LOGGING option is enabled).

C++ SDK

The IoTivity service layer is a shared C++ library.

The library's entry point is the public static function “OCPlatform::Configure” which needs a configuration class as argument.

Example Code:

The rest of this article will cover elements from the following two examples:

Flow

API

The important functions are

  • OCPlatform::Configure - An API for overwriting the default configuration of the OCPlatform object.

Check :

OCPlatform::Configure

This method calls OCInit as explained in the C SDK chapter above, and there is no need to stop OCStack because it's done internally by OCPlatform's destructor.

Server

The first step is to start with the server:

PlatformConfig cfg {
        OC::ServiceType::InProc,
        OC::ModeType::Server,
        "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
        0,         // Uses randomly available port
        OC::QualityOfService::LowQos,
};
    
OCPlatform::Configure(cfg);

Server (Secure)

If iotivity is built with SECURITY mode on, you need to also load extra files using the OCPersistentStore class. Here is the sample code for this:

static const char* SVR_DB_FILE_NAME = "./oic_svr_db_server.dat";


static FILE* client_open(const char* /*path*/, const char *mode)
{
    return fopen(SVR_DB_FILE_NAME, mode);
}

OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };

PlatformConfig cfg {
        OC::ServiceType::InProc,
        OC::ModeType::Server,
        "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
        0,         // Uses randomly available port
        OC::QualityOfService::LowQos,
        &ps
};
    
OCPlatform::Configure(cfg);

Client

Next, is the client, which is very similar except for the second parameter:

static const char* SVR_DB_FILE_NAME = "./oic_svr_db_client.dat";

static FILE* client_open(const char* /*path*/, const char *mode)
{
    return fopen(SVR_DB_FILE_NAME, mode);
}

OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };

PlatformConfig cfg {
        OC::ServiceType::InProc,
        OC::ModeType::Both,
        "0.0.0.0",
        0,
        OC::QualityOfService::LowQos,
        &ps
};

OCPlatform::Configure(cfg);

Running Demo Example :

Start the simple server, which will emulate an OCF/OIC device, and in this case, a light:

cd /opt/iotivity/examples/resource/cpp/ ; ./simpleserver
 
Usage : simpleserver 
    Default - Non-secure resource and notify all observers
    1 - Non-secure resource and notify list of observers
 
    2 - Secure resource and notify all observers
    3 - Secure resource and notify list of observers
 
    4 - Non-secure resource, GET slow response, notify all observers
Created resource.
Added Interface and Type
Waiting

Now start the simple client in the another shell session:

$ cd /opt/iotivity/examples/resource/cpp/ ; ./simpleclient
 
---------------------------------------------------------------------
Usage : simpleclient 
   ObserveType : 1 - Observe
   ObserveType : 2 - ObserveAll
---------------------------------------------------------------------
 
 
 
Finding Resource...

TODO:

On my build (as of Nov 11'th 2016), when I run the client, I see the IPv6 address (partially masked in the sample I provide) of the sample light (I presume this resource is being advertised by the simpleserver program). I also see some exceptions:

DISCOVERED Resource:
	URI of the resource: /a/light
	Host address of the resource: coap://[fe80::a00:XXXX:XXXX:XXXX%eth0]:47790
Querying for platform information... 
Exception in foundResource: result_guard(): 20: Invalid URI

Notes

Now we can jump to next chapter : “resource_find_registration”.

This page is based on the Linux Programming Guide:

initialize_setting.txt · Last modified: 2017/08/25 09:15 by Phil Coval