1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 #include <list>
20 #include <vector>
21 
22 #include "types/bluetooth/uuid.h"
23 #include "types/raw_address.h"
24 
25 namespace bluetooth {
26 namespace groups {
27 
28 static constexpr int kGroupUnknown = -1;
29 static const bluetooth::Uuid kGenericContextUuid = bluetooth::Uuid::From16Bit(0x0000);
30 
31 class DeviceGroupsCallbacks {
32 public:
33   virtual ~DeviceGroupsCallbacks() = default;
34 
35   /* Notifies first group appearance.
36    * This callback also contains first group member and uuid of the group.
37    */
38   virtual void OnGroupAdded(const RawAddress& address, const bluetooth::Uuid& group_uuid,
39                             int group_id) = 0;
40 
41   /* Followed group members are notified with this callback */
42   virtual void OnGroupMemberAdded(const RawAddress& address, int group_id) = 0;
43 
44   /* Group removal callback */
45   virtual void OnGroupRemoved(const bluetooth::Uuid& group_uuid, int group_id) = 0;
46 
47   /* Callback with group status update */
48   virtual void OnGroupMemberRemoved(const RawAddress& address, int group_id) = 0;
49 
50   /* Callback with group information added from storage */
51   virtual void OnGroupAddFromStorage(const RawAddress& address, const bluetooth::Uuid& group_uuid,
52                                      int group_id) = 0;
53 };
54 
55 class DeviceGroups {
56 public:
57   virtual ~DeviceGroups() = default;
58   static void Initialize(DeviceGroupsCallbacks* callbacks);
59   static void AddFromStorage(const RawAddress& addr, const std::vector<uint8_t>& in);
60   static bool GetForStorage(const RawAddress& addr, std::vector<uint8_t>& out);
61   static void CleanUp(DeviceGroupsCallbacks* callbacks);
62   static DeviceGroups* Get();
63   static void DebugDump(int fd);
64   /** To add to the existing group, group_id needs to be provided.
65    *  Otherwise a new group for the given context uuid will be created.
66    */
67   virtual int AddDevice(const RawAddress& addr,
68                         bluetooth::Uuid uuid = bluetooth::groups::kGenericContextUuid,
69                         int group_id = bluetooth::groups::kGroupUnknown) = 0;
70   virtual int GetGroupId(const RawAddress& addr,
71                          bluetooth::Uuid uuid = bluetooth::groups::kGenericContextUuid) const = 0;
72   virtual void RemoveDevice(const RawAddress& addr,
73                             int group_id = bluetooth::groups::kGroupUnknown) = 0;
74 };
75 
76 }  // namespace groups
77 }  // namespace bluetooth
78