[IMPROVE] create AppInst* 60/40960/3
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Wed, 10 Jun 2015 10:33:02 +0000 (13:33 +0300)
committerDmitry Kovalenko <d.kovalenko@samsung.com>
Thu, 11 Jun 2015 15:53:46 +0000 (08:53 -0700)
Change-Id: I4fef70c75c3142a7a3dd869011a6f7892848ec07
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
daemon/Makefile
daemon/cpp/common.h
daemon/cpp/inst/AppInst.cpp [new file with mode: 0644]
daemon/cpp/inst/AppInst.h [new file with mode: 0644]
daemon/cpp/inst/AppInstCommon.h [new file with mode: 0644]
daemon/cpp/inst/AppInstCont.cpp [new file with mode: 0644]
daemon/cpp/inst/AppInstCont.h [new file with mode: 0644]
daemon/cpp/inst/AppInstRunning.h [new file with mode: 0644]
daemon/cpp/inst/AppInstTizen.h [new file with mode: 0644]
daemon/cpp/inst/AppInstWeb.h [new file with mode: 0644]
daemon/da_protocol.h

index 04439ca..a6c0cf1 100644 (file)
@@ -83,7 +83,10 @@ SRC_CPP := \
        cpp/features/feature.cpp \
        cpp/features/feature_manager.cpp \
        cpp/features/feature_manager_c.cpp \
-       cpp/features/feature_wsp.cpp
+       cpp/features/feature_wsp.cpp \
+\
+       cpp/inst/AppInst.cpp \
+       cpp/inst/AppInstCont.cpp
 
 OBJS_C := $(SRC_C:.c=.o)
 OBJS_CPP := $(SRC_CPP:.cpp=.o)
index 9fe1860..2e9b1b9 100644 (file)
@@ -37,6 +37,31 @@ static inline std::string addr2hex(unsigned long val)
     return "0x" + ss.str();
 }
 
+static inline bool str2pid(const char *str, pid_t &pid)
+{
+    char c;
+    std::stringstream ss(str);
+    ss >> pid;
+    if (ss.fail() || ss.get(c)) {
+        // not an integer
+        return false;
+    }
+    return true;
+}
+
+static inline bool buf2uint64(const char *buf, size_t len, uint64_t &out)
+{
+    if (len != 8)
+        return false;
+
+    out = 0;
+    for (int i = len - 1; i >= 0; --i) {
+        out <<= 8;
+        out += (uint8_t)buf[i];
+    }
+
+    return true;
+}
 
 static inline ssize_t write_to_file(const char *filename,
                                     const void *buf, size_t len)
diff --git a/daemon/cpp/inst/AppInst.cpp b/daemon/cpp/inst/AppInst.cpp
new file mode 100644 (file)
index 0000000..26df714
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#include "AppInst.h"
+#include "AppInstTizen.h"
+#include "AppInstRunning.h"
+#include "AppInstCommon.h"
+#include "AppInstWeb.h"
+#include "debug.h"
+
+
+AppInst *AppInst::create(AppType type, const AppInstInfo &info)
+{
+    AppInst *app(0);
+
+    switch (type) {
+    case AT_TIZEN:
+        app = new AppInstTizen();
+        break;
+    case AT_RUNNING:
+        app = new AppInstRunning();
+        break;
+    case AT_COMMON:
+        app = new AppInstCommon();
+        break;
+    case AT_WEB:
+        app = new AppInstWeb();
+        break;
+    default:
+        LOGE("'app type' incorrect, type=%d\n", type);
+        return 0;
+    }
+
+    if (app) {
+        int ret = app->init(info);
+        if (ret) {
+            LOGE("[%s] initialization error, ret=%d\n", type2str(type), ret);
+            delete app;
+            app = 0;
+        }
+    }
+
+    return app;
+}
+
+void AppInst::destroy(AppInst *app)
+{
+    app->uninit();
+    delete app;
+}
+
+int AppInst::init(const AppInstInfo &info)
+{
+    _info = new AppInstInfo(info);
+    if (_info == 0)
+        return -ENOMEM;
+
+    return doInit();
+}
+
+void AppInst::uninit()
+{
+    doUninit();
+    delete _info;
+    _info = 0;
+}
+
diff --git a/daemon/cpp/inst/AppInst.h b/daemon/cpp/inst/AppInst.h
new file mode 100644 (file)
index 0000000..b25b33e
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#ifndef APPINST_H
+#define APPINST_H
+
+
+#include <string>
+#include <vector>
+
+
+typedef std::vector<char> ByteArray;
+
+
+#define APP_TYPE_LIST \
+    X(UNKNOWN) \
+    X(TIZEN) \
+    X(RUNNING) \
+    X(COMMON) \
+    X(WEB)
+
+
+#define X(type) AT_##type,
+enum AppType {
+    /* AT_xxx */
+    APP_TYPE_LIST
+};
+#undef X
+
+static inline const char *type2str(AppType type)
+{
+#define X(type) case AT_##type: return #type;
+    switch (type) {
+    APP_TYPE_LIST
+    }
+#undef X
+    return "error_type";
+}
+
+
+class AppInstInfo
+{
+public:
+    AppInstInfo(const std::string &id, const std::string &path,
+                const ByteArray &data) :
+        _id(id),
+        _path(path),
+        _data(data)
+    {}
+
+    const std::string &id() const { return _id; }
+    const std::string &path() const { return _path; }
+    const ByteArray &data() const { return _data; }
+
+private:
+    const std::string _id;
+    const std::string _path;
+    const ByteArray _data;
+};
+
+
+class AppInst
+{
+public:
+    static AppInst *create(AppType type, const AppInstInfo &info);
+    static void destroy(AppInst *app);
+
+    const AppInstInfo &info() const { return *_info; }
+
+protected:
+    AppInst() : _info(0) {}
+    virtual ~AppInst() {}
+
+private:
+    int init(const AppInstInfo &info);
+    void uninit();
+
+    int virtual doInit() { return 0; }
+    void virtual doUninit() {}
+
+    const AppInstInfo *_info;
+};
+
+
+#endif // APPINST_H
diff --git a/daemon/cpp/inst/AppInstCommon.h b/daemon/cpp/inst/AppInstCommon.h
new file mode 100644 (file)
index 0000000..0437187
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#ifndef APPINSTCOMMON_H
+#define APPINSTCOMMON_H
+
+#include "AppInst.h"
+#include "common.h"
+#include "debug.h"
+
+
+class AppInstCommon : public AppInst
+{
+    int doInit()
+    {
+        if (info().id().size()) {
+            LOGE("[%s] error 'app ID' is not empty '%s'\n",
+                 type2str(AT_COMMON), info().id().c_str());
+            return -EINVAL;
+        }
+
+        return 0;
+    }
+};
+
+
+#endif // APPINSTCOMMON_H
diff --git a/daemon/cpp/inst/AppInstCont.cpp b/daemon/cpp/inst/AppInstCont.cpp
new file mode 100644 (file)
index 0000000..344f890
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#include "AppInstCont.h"
+#include "AppInst.h"
+
+
+AppInstCont &AppInstCont::instance()
+{
+    static AppInstCont cont;
+
+    return cont;
+}
+
+AppInstCont::AppInstCont()
+{
+}
+
+AppInstCont::~AppInstCont()
+{
+    clear();
+}
+
+int AppInstCont::add(AppType type, const std::string &id,
+                     const std::string &path, const ByteArray &data)
+{
+    AppInstInfo info(id, path, data);
+
+    AppInst *app = AppInst::create(type, info);
+    if (app == 0)
+        return -EINVAL;
+
+    _list.push_back(app);
+
+    return 0;
+}
+
+void AppInstCont::clear()
+{
+    for (AppInstList::const_iterator it = _list.begin(); it != _list.end(); ++it)
+        AppInst::destroy(*it);
+
+    _list.clear();
+}
diff --git a/daemon/cpp/inst/AppInstCont.h b/daemon/cpp/inst/AppInstCont.h
new file mode 100644 (file)
index 0000000..10371c8
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#ifndef APPINSTCONT_H
+#define APPINSTCONT_H
+
+
+#include <list>
+#include "AppInst.h"
+
+
+class AppInstCont
+{
+public:
+    static AppInstCont &instance();
+
+    int add(AppType type, const std::string &id, const std::string &path, const ByteArray &data);
+    void clear();
+
+    template <typename T, typename Data>
+    void for_each(void (*func)(const T&, Data&), Data& data)
+    {
+        for (AppInstList::const_iterator it = _list.begin(); it != _list.end(); ++it) {
+            T *app = dynamic_cast<T*>(*it);
+            if (app)
+                func(*app, data);
+        }
+    }
+
+private:
+    AppInstCont();
+    ~AppInstCont();
+
+    typedef std::list<AppInst *> AppInstList;
+
+    AppInstList _list;
+};
+
+
+#endif // APPINSTCONT_H
diff --git a/daemon/cpp/inst/AppInstRunning.h b/daemon/cpp/inst/AppInstRunning.h
new file mode 100644 (file)
index 0000000..1e5c627
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#ifndef APPINSTRUNNING_H
+#define APPINSTRUNNING_H
+
+
+#include "AppInst.h"
+#include "common.h"
+#include "debug.h"
+
+
+class AppInstRunning : public AppInst
+{
+public:
+    pid_t pid() { return _pid; }
+
+private:
+    int doInit()
+    {
+        if (!str2pid(info().id().c_str(), _pid)) {
+            LOGE("[%s] error convert 'app ID to pid'\n", type2str(AT_RUNNING));
+            return -EINVAL;
+        }
+
+        return 0;
+    }
+
+    pid_t _pid;
+};
+
+#endif // APPINSTRUNNING_H
diff --git a/daemon/cpp/inst/AppInstTizen.h b/daemon/cpp/inst/AppInstTizen.h
new file mode 100644 (file)
index 0000000..fb07f1d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#ifndef APPINSTTIZEN_H
+#define APPINSTTIZEN_H
+
+
+#include "AppInst.h"
+#include "common.h"
+#include "debug.h"
+
+
+class AppInstTizen : public AppInst
+{
+public:
+    uint64_t mainOffset() const { return _mainOffset; }
+
+private:
+    int doInit()
+    {
+        const ByteArray &data = info().data();
+
+        if (!buf2uint64(data.data(), data.size(), _mainOffset)) {
+            LOGE("[%s] error convert 'setup data'\n", type2str(AT_TIZEN));
+            return -EINVAL;
+        }
+
+        return 0;
+    }
+
+    uint64_t _mainOffset;
+};
+
+
+#endif // APPINSTTIZEN_H
diff --git a/daemon/cpp/inst/AppInstWeb.h b/daemon/cpp/inst/AppInstWeb.h
new file mode 100644 (file)
index 0000000..187b753
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) Samsung Electronics, 2015
+ *
+ * 2015         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
+ *
+ */
+
+
+#ifndef APPINSTWEB_H
+#define APPINSTWEB_H
+
+
+#include "AppInst.h"
+#include "debug.h"
+
+
+class AppInstWeb : public AppInst
+{
+    int doInit()
+    {
+        if (info().id().empty()) {
+            LOGE("[%s] error 'app ID' is empty\n", type2str(AT_WEB));
+            return -EINVAL;
+        }
+
+        return 0;
+    }
+};
+
+
+#endif // APPINSTWEB_H
index cd1ba4f..5882977 100644 (file)
@@ -160,11 +160,6 @@ enum probe_type {
 #define IS_OPT_SET_IN(OPT, reg) (reg & (OPT))
 #define IS_OPT_SET(OPT) IS_OPT_SET_IN((OPT), prof_session.conf.use_features0)
 
-enum app_type {
-       AT_TIZEN        =0x01,
-       AT_LAUNCHED     =0x02,
-       AT_COMMON       =0x03
-};
 enum supported_device {
        DEVICE_FLASH,
        DEVICE_CPU,