}
}
+inline void guardErrorCode(OCStackResult resultCode, std::string message)
+{
+ if (resultCode != OC_STACK_OK)
+ throw std::exception();
+}
+
+string getDeviceID()
+{
+ OCUUIdentity deviceId;
+ guardErrorCode(OC::OCPlatform::getDeviceId(&deviceId), "OCPlatform::getDeviceId()");
+
+ char s[128];
+ unsigned char *id = deviceId.id;
+
+ snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ id[0], id[1], id[2], id[3],
+ id[4], id[5], id[6], id[7],
+ id[8], id[9], id[10], id[11],
+ id[12], id[13], id[14], id[15]);
+
+ return string(s);
+
+}
+
int main(int argc, char* argv[])
{
setPersStoragePath();
"0.0.0.0", 0, OC::QualityOfService::LowQos, &ps };
OCPlatform::Configure(cfg);
+ cout << "Own device id:DUID:" << getDeviceID() << endl << flush;
+
try
{
NotificationResource notifResource(isSecured);
using namespace std;
using namespace OC;
#define TAG "NetworkManager"
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)
namespace
{
}
}
}
+
}
namespace NetworkManager
const std::string IoTivity::DEFAULT_PROVIDER = "samsung";
const int IoTivity::DEFAULT_TIMEOUT = 10;
+const char PERSISTENT_STORAGE_PATH[] = TOSTRING(PS_PATH) "/./nmlib_ps.dat"; /**< Path to persistent storage to get device id */
struct Params
{
OC::PlatformConfig config;
- OCPersistentStorage ps;
OCAccountManager::Ptr accountMgr;
OCPlatform::OCPresenceHandle presenceHandle;
bool subscribed;
IoTivity* IoTivity::instance = nullptr;
+static FILE* open_ps(const char*, const char *mode)
+{
+ return fopen(PERSISTENT_STORAGE_PATH, mode);
+}
+
std::string IoTivity::getAuthCode()
{
// TODO add normal implementation
IoTivity::IoTivity(): signedIn(false)
{
- params = new Params{OC::PlatformConfig{
- OC::ServiceType::InProc,
- OC::ModeType::Client,
- "0.0.0.0",
- 0,
- OC::QualityOfService::HighQos
- },
-// ,{open_ps, fread, fwrite, fclose, unlink}
- };
+ static OCPersistentStorage ps{open_ps, fread, fwrite, fclose, unlink};
+ static OC::PlatformConfig pc{OC::ServiceType::InProc, OC::ModeType::Both,
+ "0.0.0.0", 0, OC::QualityOfService::HighQos, &ps};
+ params = new Params{pc};
params->subscribed = false;
params->presenceHandle = nullptr;
{
if (instance == nullptr)
{
+ cout << "PERSISTENT_STORAGE_PATH=" << PERSISTENT_STORAGE_PATH << endl;
try
{
instance = new IoTivity;
signedIn = false;
}
-void devicePresenceHandle(Params* params, const HeaderOptions& hOptions, const OCRepresentation& rep, const int eCode, const int seqN)
+void devicePresenceHandle(Params* params, const HeaderOptions& hOptions, const OCRepresentation& rep, const int eCode,
+ const int seqN)
{
if (params == nullptr) return;
- auto processor = [params](const OCRepresentation& r)
+ auto processor = [params](const OCRepresentation & r)
{
std::string state{"off"};
r.getValue("state", state);
params->owned.emplace(di, dev);
}
- if ((bool)params->presence_hook) {
+ if ((bool)params->presence_hook)
+ {
params->presence_hook(di, state == "on");
}
}
params->accountMgr->host(),
{},
CT_DEFAULT,
- std::bind(devicePresenceHandle, params, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)
+ std::bind(devicePresenceHandle, params, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+ std::placeholders::_4)
);
std::this_thread::sleep_for(std::chrono::seconds(3));
string IoTivity::getDeviceID()
{
- OCUUIdentity deviceId;
- OCStackResult res = OC::OCPlatform::getDeviceId(&deviceId);
- if (res != OC_STACK_OK)
- {
- cout << "OCPlatform::getDeviceId error: " << res << endl;
- return string("");
- }
+ OCUUIdentity deviceId;
+ guardErrorCode(OC::OCPlatform::getDeviceId(&deviceId), "OCPlatform::getDeviceId()");
- char s[128];
- unsigned char *id = deviceId.id;
+ char s[128];
+ unsigned char *id = deviceId.id;
- snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
- id[0], id[1], id[2], id[3],
- id[4], id[5], id[6], id[7],
- id[8], id[9], id[10], id[11],
- id[12], id[13], id[14], id[15]);
+ snprintf(s, sizeof(s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+ id[0], id[1], id[2], id[3],
+ id[4], id[5], id[6], id[7],
+ id[8], id[9], id[10], id[11],
+ id[12], id[13], id[14], id[15]);
- return string(s);
+ return string(s);
}
-void IoTivity::subscribeNotifications(NotificationCallback callback) {
+void IoTivity::subscribeNotifications(NotificationCallback callback)
+{
throw IoTInternalError("subscribeNotifications() not implemented", EC_NOT_IMPLEMENTED_YET);
}
-void IoTivity::unsubscribeNotifications() {
+void IoTivity::unsubscribeNotifications()
+{
throw IoTInternalError("unsubscribeNotifications() not implemented", EC_NOT_IMPLEMENTED_YET);
}
}
#endif
+/**
+ * Test check correct work of IoTivity::signIn()
+ * 1. Input correct signIn credentials.
+ * 2. Check signedIn flag is true
+ * 3. Sign out
+ * 4. Check signedIn flag is false
+ */
TEST(test_IoT, signInCorrect)
{
std::string login("login");
ASSERT_FALSE(iot->isSignedIn());
}
+/**
+ * Test check incorrect work of IoTivity::signIn() for empty arguments
+ */
TEST(test_IoT, signInIncorrectInput)
{
std::string login("login");
ASSERT_ANY_THROW(IoTivity::getInstance()->signIn(host, "", password));
ASSERT_ANY_THROW(IoTivity::getInstance()->signIn(host, login, ""));
}
+
+static void notificationCb(std::string notifMessage) {
+ cout << "notificationCb()" << endl << flush;
+}
+
+/**
+ * Test check correct work of IoTivity::subscribeNotifications()
+ */
+TEST(test_IoT, notificationCorrect)
+{
+ std::string login("login");
+ std::string password("password");
+ std::string host("coap+tcp://106.125.46.44:5683");
+
+ ASSERT_NO_THROW(IoTivity::getInstance()->signIn(host, login, password));
+ ASSERT_ANY_THROW(IoTivity::getInstance()->subscribeNotifications(notificationCb));
+}
+
+/**
+ * Test checks IoTivity::getDeviceID()
+ * 1. Device id should be not empty
+ * 2. No exceptions should be thrown
+ */
+TEST(test_IoT, getDeviceIdCorrect)
+{
+ string duid;
+ auto iot = IoTivity::getInstance();
+ ASSERT_NO_THROW(duid = iot->getDeviceID());
+ cout << duid << endl;
+ ASSERT_NE("", duid);
+}