[IoTivity, #1] OCF 표준을 따르는 소프트웨어 플랫폼인 IoTivity 란?
1. IoTivity란?
IoTivity는 끊김없이 장치 간 연결을 가능하게 하여 사물(Thing)의 인터넷에 대해 새로운 요구들을 해결하는 개방형 소스 소프트웨어 프레임 워크이다.
2. IoTivity
2.1. IoTivity 초기화 및 설정
아래 내용은 https://wiki.iotivity.org/initialize_setting 내용을 바탕으로 작성되어 있으며, 개발자 측면에서 개인적인 의견과 작업 내용을 추가 했다.
Resource API 스택은 여러 개의 얇은 소프트웨어 레이어로 구성됩니다. Android, iOS 또는 Microsoft Windows와 같은 자유로운 환경에서 IoTivity 스텍은 개발자에게 C 및 C ++ API를 제공하므로 개발자는 이를 사용하여 추가적인 네트워크 프로토콜 및 무선 기술기반의 IP 네트워크 통해 제한된 장치 또는 제한되지 않은 장치 모두와 대화 할 수 있다.
IoTivity 아키텍처 의 기본 개요는 다음과 같다.
다음 그림은 OS 스텍이 C 또는 C++ 함수을 사용하여 다른 레벨로 접근할 수 있는 것을 보여준다.
- C++ SDK : OCPlatform::Configure
- C SDK : OCInit
1) Setup
먼저 IoTivity를 개발할 OS에 빌드가 되지 않았다면, 아래 가이드를 참고하기 전에 먼저 빌드가 필요하다.2) C SDK
IoTivity의 기본 레이어는 C 라이브러리이다.Example
초기화를 위해 기본 예제는 다음 소스를 참고한다.
iotivity/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
이 코드가 C++로 작성되었다 하더라고, 여기서는 C API를 호출하고, 초기화보다 더 많은 것을 진행하지만, 이 예제에서는 몇 개의 OC 함수들만 설명할 것이다.
a) API
필요한 함수들의 목록이다.
함수 위치 --> 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.
b) OCInit
OCInit can be used with default parameters:
Server:
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) ...
if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) ...
c) 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).
3) 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.
The rest of this article will cover elements from the following two examples:
b) Flow
c) API
The important functions are- OCPlatform::Configure - An API for overwriting the default configuration of the OCPlatform object.
Check :
d) 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):
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);
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);
3) 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
--------------------------------------------
2. Notes
Now we can jump to next chapter : “resource_find_registration”.
This page is based on the Linux Programming Guide:
댓글
댓글 쓰기