aboutsummaryrefslogtreecommitdiffstats
path: root/src/activity_manager.hpp
blob: ebd5762b16bbcd597dced2ba69c9a2d17802f25d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (c) 2018 Panasonic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef ACTIVITY_MANAGER_HPP
#define ACTIVITY_MANAGER_HPP

#include <bitset>

extern "C"
{
#include <afb/afb-binding.h>
}

namespace lcm
{

#define LCM_EVENT_STATUS_CHANGED "statusChanged"
#define LCM_API_GET_ACTIVITY_STATUS "getActivityStatus"
#define LCM_API    "api"
#define LCM_TARGET "target"
#define LCM_STATE  "state"

enum activity_status
{
  NOTEXISTS,

  CREATED,  // after ON_CREATE
  DESTROYED,// after ON_DESTROY

  STARTED,  // after ON_START, ON_RESTART
  STOPPED,  // after ON_STOP

  FOREGROUND,  // after ON_FOREGROUND
  BACKGROUND,  // after ON_BACKGROUND

  NUM_STATUS,
};

#define ACTIVITY_FILTER_ALL_SET "111111"  // For demo, always notify all states

struct _observer_context {
  afb_event_t event;  // onStatusChanged
  std::bitset<NUM_STATUS> filter; // not supported yet
};

using observer = struct _observer_context;

class ActivityManager
{
 public:
  explicit ActivityManager();
  ~ActivityManager() = default;

  void api_register_activity_observer (afb_req_t req, void* &obs_ctx);
  void api_unregister_activity_observer (afb_req_t req, void* &obs_ctx);
  wm::result<json_object *> api_get_activity_status(const char *appid);

  void emit_activity_status_changed(const char* appid, const char* state);
  void lcm_clear_context (void* &lcm_ctx);

  // map of <"string:target $appid", "vector of registered observers">
  std::map<std::string, std::vector<observer*>> map_observers;
  // map of <"id:target", "current_state">
  std::map<std::string, int> states;

  const char *states_s[NUM_STATUS];

 private:
  void remove_observer (observer* obs, std::vector<observer*>& obs_v);
};

} // namespace lcm
#endif /* ACTIVITY_MANAGER_HPP */